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