• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.manifest;
2 
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 
8 public class BroadcastReceiverData extends PackageItemData {
9 
10   private static final String EXPORTED = "android:exported";
11   private static final String NAME = "android:name";
12   private static final String PERMISSION = "android:permission";
13   private static final String ENABLED = "android:enabled";
14 
15   private final Map<String, String> attributes;
16   private final List<String> actions;
17   private List<IntentFilterData> intentFilters;
18 
BroadcastReceiverData( Map<String, String> attributes, MetaData metaData, List<IntentFilterData> intentFilters)19   public BroadcastReceiverData(
20       Map<String, String> attributes, MetaData metaData, List<IntentFilterData> intentFilters) {
21     super(attributes.get(NAME), metaData);
22     this.attributes = attributes;
23     this.actions = new ArrayList<>();
24     this.intentFilters = new ArrayList<>(intentFilters);
25   }
26 
BroadcastReceiverData(String className, MetaData metaData)27   public BroadcastReceiverData(String className, MetaData metaData) {
28     super(className, metaData);
29     this.actions = new ArrayList<>();
30     this.attributes = new HashMap<>();
31     intentFilters = new ArrayList<>();
32   }
33 
getActions()34   public List<String> getActions() {
35     return actions;
36   }
37 
addAction(String action)38   public void addAction(String action) {
39     this.actions.add(action);
40   }
41 
setPermission(final String permission)42   public void setPermission(final String permission) {
43     attributes.put(PERMISSION, permission);
44   }
45 
getPermission()46   public String getPermission() {
47     return attributes.get(PERMISSION);
48   }
49 
50   /**
51    * Get the intent filters defined for the broadcast receiver.
52    *
53    * @return A list of intent filters.
54    */
getIntentFilters()55   public List<IntentFilterData> getIntentFilters() {
56     return intentFilters;
57   }
58 
59   /**
60    * Get the map for all attributes defined for the broadcast receiver.
61    *
62    * @return map of attributes names to values from the manifest.
63    */
getAllAttributes()64   public Map<String, String> getAllAttributes() {
65     return attributes;
66   }
67 
68   /**
69    * Returns whether this broadcast receiver is exported by checking the XML attribute.
70    *
71    * @return true if the broadcast receiver is exported
72    */
isExported()73   public boolean isExported() {
74     boolean defaultValue = !intentFilters.isEmpty();
75     return (attributes.containsKey(EXPORTED)
76         ? Boolean.parseBoolean(attributes.get(EXPORTED))
77         : defaultValue);
78   }
79 
isEnabled()80   public boolean isEnabled() {
81     return attributes.containsKey(ENABLED) ? Boolean.parseBoolean(attributes.get(ENABLED)) : true;
82   }
83 }
84