• 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 ActivityData {
9   private static final String ALLOW_TASK_REPARENTING = "allowTaskReparenting";
10   private static final String ALWAYS_RETAIN_TASK_STATE = "alwaysRetainTaskState";
11   private static final String CLEAR_TASK_ON_LAUNCH = "clearTaskOnLaunch";
12   private static final String CONFIG_CHANGES = "configChanges";
13   private static final String ENABLED = "enabled";
14   private static final String EXCLUDE_FROM_RECENTS = "excludeFromRecents";
15   private static final String EXPORTED = "exported";
16   private static final String FINISH_ON_TASK_LAUNCH = "finishOnTaskLaunch";
17   private static final String HARDWARE_ACCELERATED = "hardwareAccelerated";
18   private static final String LABEL = "label";
19   private static final String LAUNCH_MODE = "launchMode";
20   private static final String MULTIPROCESS = "multiprocess";
21   private static final String NAME = "name";
22   private static final String NO_HISTORY = "noHistory";
23   private static final String PARENT_ACTIVITY_NAME = "parentActivityName";
24   private static final String PERMISSION = "permission";
25   private static final String PROCESS = "process";
26   private static final String SCREEN_ORIENTATION = "screenOrientation";
27   private static final String STATE_NOT_NEEDED = "stateNotNeeded";
28   private static final String TARGET_ACTIVITY = "targetActivity";
29   private static final String TASK_AFFINITY = "taskAffinity";
30   private static final String THEME = "theme";
31   private static final String UI_OPTIONS = "uiOptions";
32   private static final String WINDOW_SOFT_INPUT_MODE = "windowSoftInputMode";
33 
34   private final List<IntentFilterData> intentFilters;
35   private final HashMap<String, String> attrs;
36   // Non-null only for activity-alias'es.
37   private final ActivityData targetActivity;
38 
39   /**
40    * XML Namespace used for android.
41    */
42   private final String xmlns;
43   private final MetaData metaData;
44 
ActivityData(Map<String, String> attrMap, List<IntentFilterData> intentFilters)45   public ActivityData(Map<String, String> attrMap, List<IntentFilterData> intentFilters) {
46     this("android", attrMap, intentFilters);
47   }
48 
ActivityData(String xmlns, Map<String, String> attrMap, List<IntentFilterData> intentFilters)49   public ActivityData(String xmlns, Map<String, String> attrMap, List<IntentFilterData> intentFilters) {
50     this(xmlns, attrMap, intentFilters, null, null);
51   }
52 
ActivityData(String xmlns, Map<String, String> attrMap, List<IntentFilterData> intentFilters, ActivityData targetActivity, MetaData metaData)53   public ActivityData(String xmlns, Map<String, String> attrMap, List<IntentFilterData> intentFilters,
54       ActivityData targetActivity, MetaData metaData) {
55     this.xmlns = xmlns;
56     attrs = new HashMap<>();
57     attrs.putAll(attrMap);
58     this.intentFilters = new ArrayList<>(intentFilters);
59     this.targetActivity = targetActivity;
60     this.metaData = metaData;
61   }
62 
isAllowTaskReparenting()63   public boolean isAllowTaskReparenting() {
64     return getBooleanAttr(withXMLNS(ALLOW_TASK_REPARENTING), false);
65   }
66 
isAlwaysRetainTaskState()67   public boolean isAlwaysRetainTaskState() {
68     return getBooleanAttr(withXMLNS(ALWAYS_RETAIN_TASK_STATE), false);
69   }
70 
isClearTaskOnLaungh()71   public boolean isClearTaskOnLaungh() {
72     return getBooleanAttr(withXMLNS(CLEAR_TASK_ON_LAUNCH), false);
73   }
74 
getConfigChanges()75   public String getConfigChanges() {
76     return attrs.get(withXMLNS(CONFIG_CHANGES));
77   }
78 
isEnabled()79   public boolean isEnabled() {
80     return getBooleanAttr(withXMLNS(ENABLED), true);
81   }
82 
isExcludedFromRecents()83   public boolean isExcludedFromRecents() {
84     return getBooleanAttr(withXMLNS(EXCLUDE_FROM_RECENTS), false);
85   }
86 
isExported()87   public boolean isExported() {
88     boolean defaultValue = !intentFilters.isEmpty();
89     return getBooleanAttr(withXMLNS(EXPORTED), defaultValue);
90   }
91 
isFinishOnTaskLaunch()92   public boolean isFinishOnTaskLaunch() {
93     return getBooleanAttr(withXMLNS(FINISH_ON_TASK_LAUNCH), false);
94   }
95 
isHardwareAccelerated()96   public boolean isHardwareAccelerated() {
97     return getBooleanAttr(withXMLNS(HARDWARE_ACCELERATED), false);
98   }
99 
100   /* TODO: public boolean getIcon() {} */
101 
getLabel()102   public String getLabel() {
103     return attrs.get(withXMLNS(LABEL));
104   }
105 
getLaunchMode()106   public String getLaunchMode() {
107     return attrs.get(withXMLNS(LAUNCH_MODE));
108   }
109 
isMultiprocess()110   public boolean isMultiprocess() {
111     return getBooleanAttr(withXMLNS(MULTIPROCESS), false);
112   }
113 
getName()114   public String getName() {
115     return attrs.get(withXMLNS(NAME));
116   }
117 
isNoHistory()118   public boolean isNoHistory() {
119     return getBooleanAttr(withXMLNS(NO_HISTORY), false);
120   }
121 
getParentActivityName()122   public String getParentActivityName() {
123     return attrs.get(withXMLNS(PARENT_ACTIVITY_NAME));
124   }
125 
getPermission()126   public String getPermission() {
127     return attrs.get(withXMLNS(PERMISSION));
128   }
129 
getProcess()130   public String getProcess() {
131     return attrs.get(withXMLNS(PROCESS));
132   }
133 
getScreenOrientation()134   public String getScreenOrientation() {
135     return attrs.get(withXMLNS(SCREEN_ORIENTATION));
136   }
137 
isStateNotNeeded()138   public boolean isStateNotNeeded() {
139     return getBooleanAttr(withXMLNS(STATE_NOT_NEEDED), false);
140   }
141 
getTargetActivityName()142   public String getTargetActivityName() {
143     return attrs.get(withXMLNS(TARGET_ACTIVITY));
144   }
145 
getTaskAffinity()146   public String getTaskAffinity() {
147     return attrs.get(withXMLNS(TASK_AFFINITY));
148   }
149 
150   /**
151    * Convenience accessor for value of android:THEME attribute.
152    *
153    * @return The theme attribute.
154    */
getThemeRef()155   public String getThemeRef() {
156     return attrs.get(withXMLNS(THEME));
157   }
158 
getUIOptions()159   public String getUIOptions() {
160     return attrs.get(withXMLNS(UI_OPTIONS));
161   }
162 
getWindowSoftInputMode()163   public String getWindowSoftInputMode() {
164     return attrs.get(withXMLNS(WINDOW_SOFT_INPUT_MODE));
165   }
166 
getBooleanAttr(String n, boolean defaultValue)167   private boolean getBooleanAttr(String n, boolean defaultValue) {
168     return (attrs.containsKey(n) ? Boolean.parseBoolean(attrs.get(n)): defaultValue);
169   }
170 
withXMLNS(String attr)171   private String withXMLNS(String attr) {
172     return withXMLNS(xmlns, attr);
173   }
174 
175   /**
176    * Get the map for all attributes defined for the activity XML.
177    * @return map of attributes names to values from the manifest. Not null.
178    */
getAllAttributes()179   public Map<String, String> getAllAttributes() {
180     return attrs;
181   }
182 
183   /**
184    * Get the intent filters defined for activity.
185    * @return A list of intent filters. Not null.
186    */
getIntentFilters()187   public List<IntentFilterData> getIntentFilters() {
188     return intentFilters;
189   }
190 
getMetaData()191   public MetaData getMetaData() {
192     return metaData;
193   }
194 
getTargetActivity()195   public ActivityData getTargetActivity() {
196     return targetActivity;
197   }
198 
withXMLNS(String xmlns, String attr)199   private static String withXMLNS(String xmlns, String attr) {
200     return String.format("%s:%s", xmlns, attr);
201   }
202 
getNameAttr(String xmlns)203   public static String getNameAttr(String xmlns) {
204     return withXMLNS(xmlns, NAME);
205   }
206 
getTargetAttr(String xmlns)207   public static String getTargetAttr(String xmlns) {
208     return withXMLNS("android", TARGET_ACTIVITY);
209   }
210 }
211