RobolectricPackageManager.java revision 99fafb79bf98b7aa1946bbda1f0a225cefa2d35d
1package com.xtremelabs.robolectric.res;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8
9import android.content.ComponentName;
10import android.content.ContextWrapper;
11import android.content.Intent;
12import android.content.pm.ApplicationInfo;
13import android.content.pm.PackageInfo;
14import android.content.pm.ResolveInfo;
15import android.graphics.drawable.Drawable;
16
17import com.xtremelabs.robolectric.RobolectricConfig;
18import com.xtremelabs.robolectric.tester.android.content.pm.StubPackageManager;
19
20public class RobolectricPackageManager extends StubPackageManager {
21
22    private Map<String, PackageInfo> packageList;
23    private Map<Intent, List<ResolveInfo>> resolveList = new HashMap<Intent, List<ResolveInfo>>();
24    private Map<ComponentName, ComponentState> componentList = new HashMap<ComponentName,ComponentState>();
25    private Map<ComponentName, Drawable> drawableList = new HashMap<ComponentName, Drawable>();
26    private Map<String, Boolean> systemFeatureList = new HashMap<String, Boolean>();
27
28    private ContextWrapper contextWrapper;
29    private RobolectricConfig config;
30    private ApplicationInfo applicationInfo;
31
32    public RobolectricPackageManager(ContextWrapper contextWrapper, RobolectricConfig config) {
33        this.contextWrapper = contextWrapper;
34        this.config = config;
35        initializePackageInfo();
36    }
37
38    @Override
39    public PackageInfo getPackageInfo(String packageName, int flags) throws NameNotFoundException {
40        if (packageList.containsKey(packageName)) {
41        	return packageList.get(packageName);
42        }
43
44        throw new NameNotFoundException();
45    }
46
47    @Override
48    public ApplicationInfo getApplicationInfo(String packageName, int flags) throws NameNotFoundException {
49
50        if (config.getPackageName().equals(packageName)) {
51            if (applicationInfo == null) {
52                applicationInfo = new ApplicationInfo();
53                applicationInfo.flags = config.getApplicationFlags();
54                applicationInfo.targetSdkVersion = config.getSdkVersion();
55                applicationInfo.packageName = config.getPackageName();
56                applicationInfo.processName = config.getProcessName();
57                applicationInfo.name = config.getApplicationName();
58            }
59            return applicationInfo;
60        }
61
62        PackageInfo info;
63        if ((info = packageList.get(packageName)) != null) {
64        	return info.applicationInfo;
65        }
66
67        throw new NameNotFoundException();
68    }
69
70    @Override
71    public List<PackageInfo> getInstalledPackages(int flags) {
72        return new ArrayList<PackageInfo>(packageList.values());
73    }
74
75    @Override
76    public List<ResolveInfo> queryIntentActivities( Intent intent, int flags ) {
77        return queryIntent(intent, flags);
78    }
79
80    @Override
81    public List<ResolveInfo> queryIntentServices( Intent intent, int flags ) {
82        return queryIntent(intent, flags);
83    }
84
85    @Override
86    public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
87        return queryIntent(intent, flags);
88    }
89
90    @Override
91    public ResolveInfo resolveActivity(Intent intent, int flags) {
92    	List<ResolveInfo> candidates = queryIntentActivities(intent, flags);
93    	return candidates.isEmpty() ? null : candidates.get(0);
94    }
95
96    @Override
97    public ResolveInfo resolveService(Intent intent, int flags) {
98        return resolveActivity(intent, flags);
99    }
100
101    public void addResolveInfoForIntent( Intent intent, List<ResolveInfo> info ) {
102        resolveList.put(intent, info);
103    }
104
105    public void addResolveInfoForIntent(Intent intent, ResolveInfo info) {
106        List<ResolveInfo> l = resolveList.get(intent);
107        if (l == null) {
108            l = new ArrayList<ResolveInfo>();
109            resolveList.put(intent, l);
110        }
111        l.add(info);
112    }
113
114    @Override
115    public Drawable getActivityIcon(Intent intent) {
116    	return drawableList.get(intent.getComponent());
117    }
118
119    @Override
120    public Drawable getActivityIcon(ComponentName componentName) {
121    	return drawableList.get(componentName);
122    }
123
124    public void addActivityIcon( ComponentName component, Drawable d ) {
125    	drawableList.put( component, d);
126    }
127
128    public void addActivityIcon( Intent intent, Drawable d ) {
129    	drawableList.put( intent.getComponent(), d);
130    }
131
132	@Override
133	public Intent getLaunchIntentForPackage(String packageName) {
134		Intent i = new Intent();
135		i.setComponent( new ComponentName(packageName, "") );
136		return i;
137	}
138
139	@Override
140	public CharSequence getApplicationLabel(ApplicationInfo info) {
141		return info.name;
142	}
143
144	@Override
145	public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) {
146		componentList.put(componentName, new ComponentState(newState, flags));
147	}
148
149	/**
150	 * Non-Android accessor.  Use to make assertions on values passed to
151	 * setComponentEnabledSetting.
152	 *
153	 * @param componentName
154	 * @return
155	 */
156	public RobolectricPackageManager.ComponentState getComponentState(ComponentName componentName) {
157		return componentList.get(componentName);
158	}
159
160    /**
161     * Non-Android accessor.  Used to add a package to the list of those
162     * already 'installed' on system.
163     *
164     * @param packageInfo
165     */
166    public void addPackage( PackageInfo packageInfo ) {
167    	 packageList.put(packageInfo.packageName, packageInfo);
168    }
169
170    public void addPackage( String packageName ) {
171    	PackageInfo info = new PackageInfo();
172    	info.packageName = packageName;
173    	addPackage( info );
174    }
175
176    @Override
177    public boolean hasSystemFeature(String name) {
178        return systemFeatureList.containsKey(name) ? systemFeatureList.get(name) : false;
179    }
180
181    /**
182     * Non-Android accessor.  Used to declare a system feature is
183     * or is not supported.
184     *
185     * @param name
186     * @param supported
187     */
188    public void setSystemFeature(String name, boolean supported) {
189    	systemFeatureList.put(name, supported);
190    }
191
192    private List<ResolveInfo> queryIntent(Intent intent, int flags) {
193        List<ResolveInfo> result = resolveList.get(intent);
194        if (result == null) {
195            return Collections.emptyList();
196        } else {
197            return result;
198        }
199    }
200
201    private void initializePackageInfo() {
202    	if (packageList != null) { return; }
203
204        PackageInfo packageInfo = new PackageInfo();
205        packageInfo.packageName = contextWrapper.getPackageName();
206        packageInfo.versionName = "1.0";
207
208        packageList = new HashMap<String, PackageInfo>();
209        addPackage( packageInfo );
210    }
211
212    public class ComponentState {
213    	public int newState;
214    	public int flags;
215
216		public ComponentState(int newState, int flags) {
217			this.newState = newState;
218			this.flags = flags;
219		}
220    }
221}
222