aboutsummaryrefslogtreecommitdiff
path: root/resources/src/main/java/org
diff options
context:
space:
mode:
authorBrett Chabot <brettchabot@google.com>2017-08-29 10:11:01 -0700
committerBrett Chabot <brettchabot@google.com>2017-08-29 10:11:01 -0700
commit3f2f68b3b39e337695b42372ed3fc61a56afe09b (patch)
treee454dc01a4aa5c05025cf76a577c7c8261b473f8 /resources/src/main/java/org
parent56267eb0c9414e80df0d482fa3ce94b261989351 (diff)
downloadrobolectric-shadows-3f2f68b3b39e337695b42372ed3fc61a56afe09b.tar.gz
Adding attributes and intent filters to BroadcastReceiverData and ServiceData as parsed from the Android manifest.
Diffstat (limited to 'resources/src/main/java/org')
-rw-r--r--resources/src/main/java/org/robolectric/manifest/ActivityData.java3
-rw-r--r--resources/src/main/java/org/robolectric/manifest/AndroidManifest.java74
-rw-r--r--resources/src/main/java/org/robolectric/manifest/BroadcastReceiverData.java58
-rw-r--r--resources/src/main/java/org/robolectric/manifest/ServiceData.java56
4 files changed, 150 insertions, 41 deletions
diff --git a/resources/src/main/java/org/robolectric/manifest/ActivityData.java b/resources/src/main/java/org/robolectric/manifest/ActivityData.java
index 7088115b1..0da5d19b2 100644
--- a/resources/src/main/java/org/robolectric/manifest/ActivityData.java
+++ b/resources/src/main/java/org/robolectric/manifest/ActivityData.java
@@ -15,7 +15,6 @@ public class ActivityData {
private static final String EXPORTED = "exported";
private static final String FINISH_ON_TASK_LAUNCH = "finishOnTaskLaunch";
private static final String HARDWARE_ACCELERATED = "hardwareAccelerated";
- private static final String ICON = "icon";
private static final String LABEL = "label";
private static final String LAUNCH_MODE = "launchMode";
private static final String MULTIPROCESS = "multiprocess";
@@ -86,7 +85,7 @@ public class ActivityData {
}
public boolean isExported() {
- boolean defaultValue = false;
+ boolean defaultValue = !intentFilters.isEmpty();
return getBooleanAttr(withXMLNS(EXPORTED), defaultValue);
}
diff --git a/resources/src/main/java/org/robolectric/manifest/AndroidManifest.java b/resources/src/main/java/org/robolectric/manifest/AndroidManifest.java
index e4f6b419b..7fb10543b 100644
--- a/resources/src/main/java/org/robolectric/manifest/AndroidManifest.java
+++ b/resources/src/main/java/org/robolectric/manifest/AndroidManifest.java
@@ -243,15 +243,32 @@ public class AndroidManifest {
return attributeNode == null ? null : attributeNode.getTextContent();
}
+ private static HashMap<String, String> parseNodeAttributes(Node node) {
+ final NamedNodeMap attributes = node.getAttributes();
+ final int attrCount = attributes.getLength();
+ final HashMap<String, String> receiverAttrs = new HashMap<>(attributes.getLength());
+ for (int i = 0; i < attrCount; i++) {
+ Node attribute = attributes.item(i);
+ String value = attribute.getNodeValue();
+ if (value != null) {
+ receiverAttrs.put(attribute.getNodeName(), value);
+ }
+ }
+ return receiverAttrs;
+ }
+
private void parseReceivers(Node applicationNode) {
for (Node receiverNode : getChildrenTags(applicationNode, "receiver")) {
- Node namedItem = receiverNode.getAttributes().getNamedItem("android:name");
- if (namedItem == null) continue;
+ final HashMap<String, String> receiverAttrs = parseNodeAttributes(receiverNode);
+
+ String receiverName = resolveClassRef(receiverAttrs.get("android:name"));
+ receiverAttrs.put("android:name", receiverName);
- String receiverName = resolveClassRef(namedItem.getTextContent());
MetaData metaData = new MetaData(getChildrenTags(receiverNode, "meta-data"));
- BroadcastReceiverData receiver = new BroadcastReceiverData(receiverName, metaData);
+ final List<IntentFilterData> intentFilterData = parseIntentFilters(receiverNode);
+ BroadcastReceiverData receiver =
+ new BroadcastReceiverData(receiverAttrs, metaData, intentFilterData);
List<Node> intentFilters = getChildrenTags(receiverNode, "intent-filter");
for (Node intentFilterNode : intentFilters) {
for (Node actionNode : getChildrenTags(intentFilterNode, "action")) {
@@ -261,26 +278,22 @@ public class AndroidManifest {
}
}
}
-
- Node permissionItem = receiverNode.getAttributes().getNamedItem("android:permission");
- if (permissionItem != null) {
- receiver.setPermission(permissionItem.getTextContent());
- }
-
+
receivers.add(receiver);
}
}
private void parseServices(Node applicationNode) {
for (Node serviceNode : getChildrenTags(applicationNode, "service")) {
- Node namedItem = serviceNode.getAttributes().getNamedItem("android:name");
- if (namedItem == null) continue;
+ final HashMap<String, String> serviceAttrs = parseNodeAttributes(serviceNode);
+
+ String serviceName = resolveClassRef(serviceAttrs.get("android:name"));
+ serviceAttrs.put("android:name", serviceName);
- String serviceName = resolveClassRef(namedItem.getTextContent());
MetaData metaData = new MetaData(getChildrenTags(serviceNode, "meta-data"));
final List<IntentFilterData> intentFilterData = parseIntentFilters(serviceNode);
- ServiceData service = new ServiceData(serviceName, metaData, intentFilterData);
+ ServiceData service = new ServiceData(serviceAttrs, metaData, intentFilterData);
List<Node> intentFilters = getChildrenTags(serviceNode, "intent-filter");
for (Node intentFilterNode : intentFilters) {
for (Node actionNode : getChildrenTags(intentFilterNode, "action")) {
@@ -291,10 +304,6 @@ public class AndroidManifest {
}
}
- Node permissionItem = serviceNode.getAttributes().getNamedItem("android:permission");
- if (permissionItem != null) {
- service.setPermission(permissionItem.getTextContent());
- }
serviceDatas.put(serviceName, service);
}
}
@@ -318,18 +327,9 @@ public class AndroidManifest {
}
private void parseActivity(Node activityNode, boolean isAlias) {
- final NamedNodeMap attributes = activityNode.getAttributes();
- final int attrCount = attributes.getLength();
final List<IntentFilterData> intentFilterData = parseIntentFilters(activityNode);
final MetaData metaData = new MetaData(getChildrenTags(activityNode, "meta-data"));
- final HashMap<String, String> activityAttrs = new HashMap<>(attrCount);
- for(int i = 0; i < attrCount; i++) {
- Node attr = attributes.item(i);
- String v = attr.getNodeValue();
- if( v != null) {
- activityAttrs.put(attr.getNodeName(), v);
- }
- }
+ final HashMap<String, String> activityAttrs = parseNodeAttributes(activityNode);
String activityName = resolveClassRef(activityAttrs.get(ActivityData.getNameAttr("android")));
if (activityName == null) {
@@ -596,6 +596,7 @@ public class AndroidManifest {
}
public ServiceData getServiceData(String serviceClassName) {
+ parseAndroidManifest();
return serviceDatas.get(serviceClassName);
}
@@ -659,4 +660,21 @@ public class AndroidManifest {
parseAndroidManifest();
return permissions;
}
+
+ /**
+ * Returns data for the broadcast receiver with the provided name from this manifest. If no
+ * receiver with the class name can be found, returns null.
+ *
+ * @param className the fully resolved class name of the receiver
+ * @return data for the receiver or null if it cannot be found
+ */
+ public @Nullable BroadcastReceiverData getBroadcastReceiver(String className) {
+ parseAndroidManifest();
+ for (BroadcastReceiverData receiver : receivers) {
+ if (receiver.getClassName().equals(className)) {
+ return receiver;
+ }
+ }
+ return null;
+ }
}
diff --git a/resources/src/main/java/org/robolectric/manifest/BroadcastReceiverData.java b/resources/src/main/java/org/robolectric/manifest/BroadcastReceiverData.java
index 9d549de4b..37b08631c 100644
--- a/resources/src/main/java/org/robolectric/manifest/BroadcastReceiverData.java
+++ b/resources/src/main/java/org/robolectric/manifest/BroadcastReceiverData.java
@@ -1,15 +1,35 @@
package org.robolectric.manifest;
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
+import javax.annotation.Nonnull;
public class BroadcastReceiverData extends PackageItemData {
+
+ private static final String EXPORTED = "android:exported";
+ private static final String NAME = "android:name";
+ private static final String PERMISSION = "android:permission";
+
+ private final Map<String, String> attributes;
private final List<String> actions;
- private String permission;
+ private List<IntentFilterData> intentFilters;
+
+ public BroadcastReceiverData(
+ Map<String, String> attributes, MetaData metaData, List<IntentFilterData> intentFilters) {
+ super(attributes.get(NAME), metaData);
+ this.attributes = attributes;
+ this.actions = new ArrayList<>();
+ this.intentFilters = new LinkedList<>(intentFilters);
+ }
public BroadcastReceiverData(String className, MetaData metaData) {
super(className, metaData);
this.actions = new ArrayList<>();
+ this.attributes = new HashMap<>();
+ intentFilters = new LinkedList<>();
}
public List<String> getActions() {
@@ -21,10 +41,42 @@ public class BroadcastReceiverData extends PackageItemData {
}
public void setPermission(final String permission) {
- this.permission = permission;
+ attributes.put(PERMISSION, permission);
}
public String getPermission() {
- return permission;
+ return attributes.get(PERMISSION);
+ }
+
+ /**
+ * Get the intent filters defined for the broadcast receiver.
+ *
+ * @return A list of intent filters.
+ */
+ @Nonnull
+ public List<IntentFilterData> getIntentFilters() {
+ return intentFilters;
+ }
+
+ /**
+ * Get the map for all attributes defined for the broadcast receiver.
+ *
+ * @return map of attributes names to values from the manifest.
+ */
+ @Nonnull
+ public Map<String, String> getAllAttributes() {
+ return attributes;
+ }
+
+ /**
+ * Returns whether this broadcast receiver is exported by checking the XML attribute.
+ *
+ * @return true if the broadcast receiver is exported
+ */
+ public boolean isExported() {
+ boolean defaultValue = !intentFilters.isEmpty();
+ return (attributes.containsKey(EXPORTED)
+ ? Boolean.parseBoolean(attributes.get(EXPORTED))
+ : defaultValue);
}
}
diff --git a/resources/src/main/java/org/robolectric/manifest/ServiceData.java b/resources/src/main/java/org/robolectric/manifest/ServiceData.java
index cde5bcc59..b76359540 100644
--- a/resources/src/main/java/org/robolectric/manifest/ServiceData.java
+++ b/resources/src/main/java/org/robolectric/manifest/ServiceData.java
@@ -1,25 +1,41 @@
package org.robolectric.manifest;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
+import javax.annotation.Nonnull;
public class ServiceData {
- private final String className;
+
+ private static final String EXPORTED = "android:exported";
+ private static final String NAME = "android:name";
+ private static final String PERMISSION = "android:permission";
+
+ private final Map<String, String> attributes;
private final MetaData metaData;
private final List<String> actions;
- private String permission;
private List<IntentFilterData> intentFilters;
+ public ServiceData(
+ Map<String, String> attributes, MetaData metaData, List<IntentFilterData> intentFilters) {
+ this.attributes = attributes;
+ this.actions = new ArrayList<>();
+ this.metaData = metaData;
+ this.intentFilters = new LinkedList<>(intentFilters);
+ }
+
public ServiceData(String className, MetaData metaData, List<IntentFilterData> intentFilterData) {
+ this.attributes = new HashMap<>();
+ this.attributes.put(NAME, className);
this.actions = new ArrayList<>();
- this.className = className;
this.metaData = metaData;
intentFilters = new LinkedList<>(intentFilterData);
}
public String getClassName() {
- return className;
+ return attributes.get(NAME);
}
public List<String> getActions() {
@@ -35,18 +51,42 @@ public class ServiceData {
}
public void setPermission(final String permission) {
- this.permission = permission;
+ attributes.put(PERMISSION, permission);
}
public String getPermission() {
- return permission;
+ return attributes.get(PERMISSION);
}
/**
- * Get the intent filters defined for activity.
- * @return A list of intent filters. Not null.
+ * Get the intent filters defined for the service.
+ *
+ * @return A list of intent filters.
*/
+ @Nonnull
public List<IntentFilterData> getIntentFilters() {
return intentFilters;
}
+
+ /**
+ * Get the map for all attributes defined for the service.
+ *
+ * @return map of attributes names to values from the manifest.
+ */
+ @Nonnull
+ public Map<String, String> getAllAttributes() {
+ return attributes;
+ }
+
+ /**
+ * Returns whether this service is exported by checking the XML attribute.
+ *
+ * @return true if the service is exported
+ */
+ public boolean isExported() {
+ boolean defaultValue = !intentFilters.isEmpty();
+ return (attributes.containsKey(EXPORTED)
+ ? Boolean.parseBoolean(attributes.get(EXPORTED))
+ : defaultValue);
+ }
}