• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base.test.util;
6 
7 import android.content.ComponentName;
8 import android.content.Intent;
9 import android.content.IntentFilter;
10 import android.content.pm.ActivityInfo;
11 import android.content.pm.ApplicationInfo;
12 import android.content.pm.ChangedPackages;
13 import android.content.pm.FeatureInfo;
14 import android.content.pm.InstrumentationInfo;
15 import android.content.pm.PackageInfo;
16 import android.content.pm.PackageInstaller;
17 import android.content.pm.PackageManager;
18 import android.content.pm.PermissionGroupInfo;
19 import android.content.pm.PermissionInfo;
20 import android.content.pm.ProviderInfo;
21 import android.content.pm.ResolveInfo;
22 import android.content.pm.ServiceInfo;
23 import android.content.pm.SharedLibraryInfo;
24 import android.content.pm.VersionedPackage;
25 import android.content.res.Resources;
26 import android.content.res.XmlResourceParser;
27 import android.graphics.Rect;
28 import android.graphics.drawable.Drawable;
29 import android.os.UserHandle;
30 
31 import java.util.List;
32 
33 /**
34  * Allows instrumentation tests to wrap the real PackageManager and override calls they wish to
35  * control the return values of (eg. by returning a wrapped real PackageManager from the Application
36  * Context).
37  */
38 public class PackageManagerWrapper extends PackageManager {
39     private PackageManager mWrapped;
40 
PackageManagerWrapper(PackageManager wrapped)41     public PackageManagerWrapper(PackageManager wrapped) {
42         super();
43         mWrapped = wrapped;
44     }
45 
46     @Override
47     @Deprecated
addPackageToPreferred(String packageName)48     public void addPackageToPreferred(String packageName) {
49         mWrapped.addPackageToPreferred(packageName);
50     }
51 
52     @Override
addPermission(PermissionInfo info)53     public boolean addPermission(PermissionInfo info) {
54         return mWrapped.addPermission(info);
55     }
56 
57     @Override
addPermissionAsync(PermissionInfo info)58     public boolean addPermissionAsync(PermissionInfo info) {
59         return mWrapped.addPermissionAsync(info);
60     }
61 
62     @Override
63     @Deprecated
addPreferredActivity( IntentFilter filter, int match, ComponentName[] set, ComponentName activity)64     public void addPreferredActivity(
65             IntentFilter filter, int match, ComponentName[] set, ComponentName activity) {
66         mWrapped.addPreferredActivity(filter, match, set, activity);
67     }
68 
69     @Override
canonicalToCurrentPackageNames(String[] names)70     public String[] canonicalToCurrentPackageNames(String[] names) {
71         return mWrapped.canonicalToCurrentPackageNames(names);
72     }
73 
74     @Override
checkPermission(String permName, String pkgName)75     public int checkPermission(String permName, String pkgName) {
76         return mWrapped.checkPermission(permName, pkgName);
77     }
78 
79     @Override
checkSignatures(String pkg1, String pkg2)80     public int checkSignatures(String pkg1, String pkg2) {
81         return mWrapped.checkSignatures(pkg1, pkg2);
82     }
83 
84     @Override
checkSignatures(int uid1, int uid2)85     public int checkSignatures(int uid1, int uid2) {
86         return mWrapped.checkSignatures(uid1, uid2);
87     }
88 
89     @Override
clearInstantAppCookie()90     public void clearInstantAppCookie() {
91         mWrapped.clearInstantAppCookie();
92     }
93 
94     @Override
currentToCanonicalPackageNames(String[] names)95     public String[] currentToCanonicalPackageNames(String[] names) {
96         return mWrapped.currentToCanonicalPackageNames(names);
97     }
98 
99     @Override
getActivityInfo(ComponentName component, int flags)100     public ActivityInfo getActivityInfo(ComponentName component, int flags)
101             throws NameNotFoundException {
102         return mWrapped.getActivityInfo(component, flags);
103     }
104 
105     @Override
getAllPermissionGroups(int flags)106     public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
107         return mWrapped.getAllPermissionGroups(flags);
108     }
109 
110     @Override
getApplicationInfo(String packageName, int flags)111     public ApplicationInfo getApplicationInfo(String packageName, int flags)
112             throws NameNotFoundException {
113         return mWrapped.getApplicationInfo(packageName, flags);
114     }
115 
116     @Override
getInstalledPackages(int flags)117     public List<PackageInfo> getInstalledPackages(int flags) {
118         return mWrapped.getInstalledPackages(flags);
119     }
120 
121     @Override
getLaunchIntentForPackage(String packageName)122     public Intent getLaunchIntentForPackage(String packageName) {
123         return mWrapped.getLaunchIntentForPackage(packageName);
124     }
125 
126     @Override
getLeanbackLaunchIntentForPackage(String packageName)127     public Intent getLeanbackLaunchIntentForPackage(String packageName) {
128         return mWrapped.getLeanbackLaunchIntentForPackage(packageName);
129     }
130 
131     @Override
getPackageGids(String packageName)132     public int[] getPackageGids(String packageName) throws NameNotFoundException {
133         return mWrapped.getPackageGids(packageName);
134     }
135 
136     @Override
getPackageGids(String packageName, int flags)137     public int[] getPackageGids(String packageName, int flags) throws NameNotFoundException {
138         return mWrapped.getPackageGids(packageName, flags);
139     }
140 
141     @Override
getPackageInfo(String packageName, int flags)142     public PackageInfo getPackageInfo(String packageName, int flags) throws NameNotFoundException {
143         return mWrapped.getPackageInfo(packageName, flags);
144     }
145 
146     @Override
getPackageUid(String packageName, int flags)147     public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
148         return mWrapped.getPackageUid(packageName, flags);
149     }
150 
151     @Override
getPackagesHoldingPermissions(String[] permissions, int flags)152     public List<PackageInfo> getPackagesHoldingPermissions(String[] permissions, int flags) {
153         return mWrapped.getPackagesHoldingPermissions(permissions, flags);
154     }
155 
156     @Override
getPermissionGroupInfo(String name, int flags)157     public PermissionGroupInfo getPermissionGroupInfo(String name, int flags)
158             throws NameNotFoundException {
159         return mWrapped.getPermissionGroupInfo(name, flags);
160     }
161 
162     @Override
getPermissionInfo(String name, int flags)163     public PermissionInfo getPermissionInfo(String name, int flags) throws NameNotFoundException {
164         return mWrapped.getPermissionInfo(name, flags);
165     }
166 
167     @Override
getProviderInfo(ComponentName component, int flags)168     public ProviderInfo getProviderInfo(ComponentName component, int flags)
169             throws NameNotFoundException {
170         return mWrapped.getProviderInfo(component, flags);
171     }
172 
173     @Override
getReceiverInfo(ComponentName component, int flags)174     public ActivityInfo getReceiverInfo(ComponentName component, int flags)
175             throws NameNotFoundException {
176         return mWrapped.getReceiverInfo(component, flags);
177     }
178 
179     @Override
getServiceInfo(ComponentName component, int flags)180     public ServiceInfo getServiceInfo(ComponentName component, int flags)
181             throws NameNotFoundException {
182         return mWrapped.getServiceInfo(component, flags);
183     }
184 
185     @Override
isPermissionRevokedByPolicy(String permName, String pkgName)186     public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
187         return mWrapped.isPermissionRevokedByPolicy(permName, pkgName);
188     }
189 
190     @Override
queryPermissionsByGroup(String group, int flags)191     public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
192             throws NameNotFoundException {
193         return mWrapped.queryPermissionsByGroup(group, flags);
194     }
195 
196     @Override
removePermission(String name)197     public void removePermission(String name) {
198         mWrapped.removePermission(name);
199     }
200 
201     @Override
getPackagesForUid(int uid)202     public String[] getPackagesForUid(int uid) {
203         return mWrapped.getPackagesForUid(uid);
204     }
205 
206     @Override
getNameForUid(int uid)207     public String getNameForUid(int uid) {
208         return mWrapped.getNameForUid(uid);
209     }
210 
211     @Override
getInstalledApplications(int flags)212     public List<ApplicationInfo> getInstalledApplications(int flags) {
213         return mWrapped.getInstalledApplications(flags);
214     }
215 
216     @Override
getSystemSharedLibraryNames()217     public String[] getSystemSharedLibraryNames() {
218         return mWrapped.getSystemSharedLibraryNames();
219     }
220 
221     @Override
getSystemAvailableFeatures()222     public FeatureInfo[] getSystemAvailableFeatures() {
223         return mWrapped.getSystemAvailableFeatures();
224     }
225 
226     @Override
hasSystemFeature(String name)227     public boolean hasSystemFeature(String name) {
228         return mWrapped.hasSystemFeature(name);
229     }
230 
231     @Override
hasSystemFeature(String name, int version)232     public boolean hasSystemFeature(String name, int version) {
233         return mWrapped.hasSystemFeature(name, version);
234     }
235 
236     @Override
resolveActivity(Intent intent, int flags)237     public ResolveInfo resolveActivity(Intent intent, int flags) {
238         return mWrapped.resolveActivity(intent, flags);
239     }
240 
241     @Override
queryIntentActivities(Intent intent, int flags)242     public List<ResolveInfo> queryIntentActivities(Intent intent, int flags) {
243         return mWrapped.queryIntentActivities(intent, flags);
244     }
245 
246     @Override
queryIntentActivityOptions( ComponentName caller, Intent[] specifics, Intent intent, int flags)247     public List<ResolveInfo> queryIntentActivityOptions(
248             ComponentName caller, Intent[] specifics, Intent intent, int flags) {
249         return mWrapped.queryIntentActivityOptions(caller, specifics, intent, flags);
250     }
251 
252     @Override
queryBroadcastReceivers(Intent intent, int flags)253     public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
254         return mWrapped.queryBroadcastReceivers(intent, flags);
255     }
256 
257     @Override
resolveService(Intent intent, int flags)258     public ResolveInfo resolveService(Intent intent, int flags) {
259         return mWrapped.resolveService(intent, flags);
260     }
261 
262     @Override
queryIntentServices(Intent intent, int flags)263     public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
264         return mWrapped.queryIntentServices(intent, flags);
265     }
266 
267     @Override
queryIntentContentProviders(Intent intent, int flags)268     public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
269         return mWrapped.queryIntentContentProviders(intent, flags);
270     }
271 
272     @Override
resolveContentProvider(String name, int flags)273     public ProviderInfo resolveContentProvider(String name, int flags) {
274         return mWrapped.resolveContentProvider(name, flags);
275     }
276 
277     @Override
queryContentProviders(String processName, int uid, int flags)278     public List<ProviderInfo> queryContentProviders(String processName, int uid, int flags) {
279         return mWrapped.queryContentProviders(processName, uid, flags);
280     }
281 
282     @Override
getInstrumentationInfo(ComponentName className, int flags)283     public InstrumentationInfo getInstrumentationInfo(ComponentName className, int flags)
284             throws NameNotFoundException {
285         return mWrapped.getInstrumentationInfo(className, flags);
286     }
287 
288     @Override
queryInstrumentation(String targetPackage, int flags)289     public List<InstrumentationInfo> queryInstrumentation(String targetPackage, int flags) {
290         return mWrapped.queryInstrumentation(targetPackage, flags);
291     }
292 
293     @Override
getDrawable(String packageName, int resid, ApplicationInfo appInfo)294     public Drawable getDrawable(String packageName, int resid, ApplicationInfo appInfo) {
295         return mWrapped.getDrawable(packageName, resid, appInfo);
296     }
297 
298     @Override
getActivityIcon(ComponentName activityName)299     public Drawable getActivityIcon(ComponentName activityName) throws NameNotFoundException {
300         return mWrapped.getActivityIcon(activityName);
301     }
302 
303     @Override
getActivityIcon(Intent intent)304     public Drawable getActivityIcon(Intent intent) throws NameNotFoundException {
305         return mWrapped.getActivityIcon(intent);
306     }
307 
308     @Override
getActivityBanner(ComponentName activityName)309     public Drawable getActivityBanner(ComponentName activityName) throws NameNotFoundException {
310         return mWrapped.getActivityBanner(activityName);
311     }
312 
313     @Override
getActivityBanner(Intent intent)314     public Drawable getActivityBanner(Intent intent) throws NameNotFoundException {
315         return mWrapped.getActivityBanner(intent);
316     }
317 
318     @Override
getDefaultActivityIcon()319     public Drawable getDefaultActivityIcon() {
320         return mWrapped.getDefaultActivityIcon();
321     }
322 
323     @Override
getApplicationIcon(ApplicationInfo info)324     public Drawable getApplicationIcon(ApplicationInfo info) {
325         return mWrapped.getApplicationIcon(info);
326     }
327 
328     @Override
getApplicationIcon(String packageName)329     public Drawable getApplicationIcon(String packageName) throws NameNotFoundException {
330         return mWrapped.getApplicationIcon(packageName);
331     }
332 
333     @Override
getApplicationBanner(ApplicationInfo info)334     public Drawable getApplicationBanner(ApplicationInfo info) {
335         return mWrapped.getApplicationBanner(info);
336     }
337 
338     @Override
getApplicationBanner(String packageName)339     public Drawable getApplicationBanner(String packageName) throws NameNotFoundException {
340         return mWrapped.getApplicationBanner(packageName);
341     }
342 
343     @Override
getActivityLogo(ComponentName activityName)344     public Drawable getActivityLogo(ComponentName activityName) throws NameNotFoundException {
345         return mWrapped.getActivityLogo(activityName);
346     }
347 
348     @Override
getActivityLogo(Intent intent)349     public Drawable getActivityLogo(Intent intent) throws NameNotFoundException {
350         return mWrapped.getActivityLogo(intent);
351     }
352 
353     @Override
getApplicationLogo(ApplicationInfo info)354     public Drawable getApplicationLogo(ApplicationInfo info) {
355         return mWrapped.getApplicationLogo(info);
356     }
357 
358     @Override
getApplicationLogo(String packageName)359     public Drawable getApplicationLogo(String packageName) throws NameNotFoundException {
360         return mWrapped.getApplicationLogo(packageName);
361     }
362 
363     @Override
getUserBadgedIcon(Drawable icon, UserHandle user)364     public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
365         return mWrapped.getUserBadgedIcon(icon, user);
366     }
367 
368     @Override
getUserBadgedDrawableForDensity( Drawable drawable, UserHandle user, Rect badgeLocation, int badgeDensity)369     public Drawable getUserBadgedDrawableForDensity(
370             Drawable drawable, UserHandle user, Rect badgeLocation, int badgeDensity) {
371         return mWrapped.getUserBadgedDrawableForDensity(
372                 drawable, user, badgeLocation, badgeDensity);
373     }
374 
375     @Override
getUserBadgedLabel(CharSequence label, UserHandle user)376     public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
377         return mWrapped.getUserBadgedLabel(label, user);
378     }
379 
380     @Override
getText(String packageName, int resid, ApplicationInfo appInfo)381     public CharSequence getText(String packageName, int resid, ApplicationInfo appInfo) {
382         return mWrapped.getText(packageName, resid, appInfo);
383     }
384 
385     @Override
getXml(String packageName, int resid, ApplicationInfo appInfo)386     public XmlResourceParser getXml(String packageName, int resid, ApplicationInfo appInfo) {
387         return mWrapped.getXml(packageName, resid, appInfo);
388     }
389 
390     @Override
getApplicationLabel(ApplicationInfo info)391     public CharSequence getApplicationLabel(ApplicationInfo info) {
392         return mWrapped.getApplicationLabel(info);
393     }
394 
395     @Override
getResourcesForActivity(ComponentName activityName)396     public Resources getResourcesForActivity(ComponentName activityName)
397             throws NameNotFoundException {
398         return mWrapped.getResourcesForActivity(activityName);
399     }
400 
401     @Override
getResourcesForApplication(ApplicationInfo app)402     public Resources getResourcesForApplication(ApplicationInfo app) throws NameNotFoundException {
403         return mWrapped.getResourcesForApplication(app);
404     }
405 
406     @Override
getResourcesForApplication(String appPackageName)407     public Resources getResourcesForApplication(String appPackageName)
408             throws NameNotFoundException {
409         return mWrapped.getResourcesForApplication(appPackageName);
410     }
411 
412     @Override
verifyPendingInstall(int id, int verificationCode)413     public void verifyPendingInstall(int id, int verificationCode) {
414         mWrapped.verifyPendingInstall(id, verificationCode);
415     }
416 
417     @Override
extendVerificationTimeout( int id, int verificationCodeAtTimeout, long millisecondsToDelay)418     public void extendVerificationTimeout(
419             int id, int verificationCodeAtTimeout, long millisecondsToDelay) {
420         mWrapped.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
421     }
422 
423     @Override
setInstallerPackageName(String targetPackage, String installerPackageName)424     public void setInstallerPackageName(String targetPackage, String installerPackageName) {
425         mWrapped.setInstallerPackageName(targetPackage, installerPackageName);
426     }
427 
428     @Override
getInstallerPackageName(String packageName)429     public String getInstallerPackageName(String packageName) {
430         return mWrapped.getInstallerPackageName(packageName);
431     }
432 
433     @Deprecated
434     @Override
removePackageFromPreferred(String packageName)435     public void removePackageFromPreferred(String packageName) {
436         mWrapped.removePackageFromPreferred(packageName);
437     }
438 
439     @Override
getPreferredPackages(int flags)440     public List<PackageInfo> getPreferredPackages(int flags) {
441         return mWrapped.getPreferredPackages(flags);
442     }
443 
444     @Override
clearPackagePreferredActivities(String packageName)445     public void clearPackagePreferredActivities(String packageName) {
446         mWrapped.clearPackagePreferredActivities(packageName);
447     }
448 
449     @Override
getPreferredActivities( List<IntentFilter> outFilters, List<ComponentName> outActivities, String packageName)450     public int getPreferredActivities(
451             List<IntentFilter> outFilters, List<ComponentName> outActivities, String packageName) {
452         return mWrapped.getPreferredActivities(outFilters, outActivities, packageName);
453     }
454 
455     @Override
setComponentEnabledSetting(ComponentName componentName, int newState, int flags)456     public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) {
457         mWrapped.setComponentEnabledSetting(componentName, newState, flags);
458     }
459 
460     @Override
getComponentEnabledSetting(ComponentName componentName)461     public int getComponentEnabledSetting(ComponentName componentName) {
462         return mWrapped.getComponentEnabledSetting(componentName);
463     }
464 
465     @Override
setApplicationEnabledSetting(String packageName, int newState, int flags)466     public void setApplicationEnabledSetting(String packageName, int newState, int flags) {
467         mWrapped.setApplicationEnabledSetting(packageName, newState, flags);
468     }
469 
470     @Override
getApplicationEnabledSetting(String packageName)471     public int getApplicationEnabledSetting(String packageName) {
472         return mWrapped.getApplicationEnabledSetting(packageName);
473     }
474 
475     @Override
isSafeMode()476     public boolean isSafeMode() {
477         return mWrapped.isSafeMode();
478     }
479 
480     @Override
getPackageInstaller()481     public PackageInstaller getPackageInstaller() {
482         return mWrapped.getPackageInstaller();
483     }
484 
485     // O Developer Preview
486 
487     @Override
canRequestPackageInstalls()488     public boolean canRequestPackageInstalls() {
489         return mWrapped.canRequestPackageInstalls();
490     }
491 
492     @Override
getChangedPackages(int sequenceNumber)493     public ChangedPackages getChangedPackages(int sequenceNumber) {
494         return mWrapped.getChangedPackages(sequenceNumber);
495     }
496 
497     @Override
getInstantAppCookie()498     public byte[] getInstantAppCookie() {
499         return mWrapped.getInstantAppCookie();
500     }
501 
502     @Override
getInstantAppCookieMaxBytes()503     public int getInstantAppCookieMaxBytes() {
504         return mWrapped.getInstantAppCookieMaxBytes();
505     }
506 
507     @Override
getPackageInfo(VersionedPackage versionedPackage, int flags)508     public PackageInfo getPackageInfo(VersionedPackage versionedPackage, int flags)
509             throws PackageManager.NameNotFoundException {
510         return mWrapped.getPackageInfo(versionedPackage, flags);
511     }
512 
513     @Override
getSharedLibraries(int flags)514     public List<SharedLibraryInfo> getSharedLibraries(int flags) {
515         return mWrapped.getSharedLibraries(flags);
516     }
517 
518     @Override
isInstantApp()519     public boolean isInstantApp() {
520         return mWrapped.isInstantApp();
521     }
522 
523     @Override
isInstantApp(String packageName)524     public boolean isInstantApp(String packageName) {
525         return mWrapped.isInstantApp(packageName);
526     }
527 
528     @Override
setApplicationCategoryHint(String packageName, int categoryHint)529     public void setApplicationCategoryHint(String packageName, int categoryHint) {
530         mWrapped.setApplicationCategoryHint(packageName, categoryHint);
531     }
532 
533     @Override
updateInstantAppCookie(byte[] cookie)534     public void updateInstantAppCookie(byte[] cookie) {
535         mWrapped.updateInstantAppCookie(cookie);
536     }
537 
538     // This is a hidden abstract method in the base class so we can't mark it as
539     // overridden, or call the real implementation without reflection. Nothing
540     // currently seems to actually care about the result of this function under
541     // testing, so we can just return null for now.
getUserBadgeForDensity(UserHandle userHandle, int i)542     protected Drawable getUserBadgeForDensity(UserHandle userHandle, int i) {
543         return null;
544     }
545 }
546