Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
      4 import static android.content.pm.PackageManager.GET_META_DATA;
      5 import static android.content.pm.PackageManager.GET_SIGNATURES;
      6 import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS;
      7 import static android.content.pm.PackageManager.MATCH_UNINSTALLED_PACKAGES;
      8 import static android.content.pm.PackageManager.SIGNATURE_UNKNOWN_PACKAGE;
      9 import static android.os.Build.VERSION_CODES.JELLY_BEAN;
     10 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
     11 import static android.os.Build.VERSION_CODES.M;
     12 import static android.os.Build.VERSION_CODES.N;
     13 import static android.os.Build.VERSION_CODES.O_MR1;
     14 import static android.os.Build.VERSION_CODES.P;
     15 
     16 import android.annotation.DrawableRes;
     17 import android.annotation.NonNull;
     18 import android.annotation.Nullable;
     19 import android.annotation.UserIdInt;
     20 import android.app.ApplicationPackageManager;
     21 import android.content.ComponentName;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.content.IntentSender;
     25 import android.content.pm.ActivityInfo;
     26 import android.content.pm.ApplicationInfo;
     27 import android.content.pm.FeatureInfo;
     28 import android.content.pm.IPackageDataObserver;
     29 import android.content.pm.IPackageDeleteObserver;
     30 import android.content.pm.IPackageStatsObserver;
     31 import android.content.pm.InstrumentationInfo;
     32 import android.content.pm.IntentFilterVerificationInfo;
     33 import android.content.pm.PackageInfo;
     34 import android.content.pm.PackageItemInfo;
     35 import android.content.pm.PackageManager;
     36 import android.content.pm.PackageManager.NameNotFoundException;
     37 import android.content.pm.PackageParser;
     38 import android.content.pm.PackageParser.Activity;
     39 import android.content.pm.PackageParser.Package;
     40 import android.content.pm.PackageParser.PermissionGroup;
     41 import android.content.pm.PackageParser.Service;
     42 import android.content.pm.PackageStats;
     43 import android.content.pm.PermissionGroupInfo;
     44 import android.content.pm.PermissionInfo;
     45 import android.content.pm.ProviderInfo;
     46 import android.content.pm.ResolveInfo;
     47 import android.content.pm.ServiceInfo;
     48 import android.content.pm.VerifierDeviceIdentity;
     49 import android.content.res.AssetManager;
     50 import android.content.res.Resources;
     51 import android.graphics.Rect;
     52 import android.graphics.drawable.Drawable;
     53 import android.net.Uri;
     54 import android.os.Handler;
     55 import android.os.Looper;
     56 import android.os.RemoteException;
     57 import android.os.UserHandle;
     58 import android.os.storage.VolumeInfo;
     59 import android.util.Pair;
     60 import java.util.ArrayList;
     61 import java.util.Collections;
     62 import java.util.HashSet;
     63 import java.util.Iterator;
     64 import java.util.List;
     65 import java.util.Objects;
     66 import java.util.Set;
     67 import org.robolectric.RuntimeEnvironment;
     68 import org.robolectric.annotation.Config;
     69 import org.robolectric.annotation.Implementation;
     70 import org.robolectric.annotation.Implements;
     71 
     72 @Implements(value = ApplicationPackageManager.class, isInAndroidSdk = false, looseSignatures = true)
     73 public class ShadowApplicationPackageManager extends ShadowPackageManager {
     74 
     75   @Implementation
     76   public List<PackageInfo> getInstalledPackages(int flags) {
     77     List<PackageInfo> result = new ArrayList<>();
     78     for (PackageInfo packageInfo : packageInfos.values()) {
     79       if (applicationEnabledSettingMap.get(packageInfo.packageName)
     80           != COMPONENT_ENABLED_STATE_DISABLED
     81           || (flags & MATCH_UNINSTALLED_PACKAGES) == MATCH_UNINSTALLED_PACKAGES
     82           || (flags & MATCH_DISABLED_COMPONENTS) == MATCH_DISABLED_COMPONENTS) {
     83         result.add(packageInfo);
     84       }
     85     }
     86 
     87     return result;
     88   }
     89 
     90   @Implementation
     91   public ActivityInfo getActivityInfo(ComponentName component, int flags) throws NameNotFoundException {
     92     String activityName = component.getClassName();
     93     String packageName = component.getPackageName();
     94     PackageInfo packageInfo = packageInfos.get(packageName);
     95 
     96     if (packageInfo != null) {
     97       if (packageInfo.activities != null) {
     98         for (ActivityInfo activity : packageInfo.activities) {
     99           if (activityName.equals(activity.name)) {
    100             ActivityInfo result = new ActivityInfo(activity);
    101             if ((flags & GET_META_DATA) != 0) {
    102               result.metaData = activity.metaData;
    103             }
    104 
    105             return result;
    106           }
    107         }
    108       }
    109 
    110       // Activity is requested is not listed in the AndroidManifest.xml
    111       ActivityInfo result = new ActivityInfo();
    112       result.name = activityName;
    113       result.packageName = packageName;
    114       result.applicationInfo = new ApplicationInfo(packageInfo.applicationInfo);
    115       return result;
    116     }
    117 
    118     // TODO: Should throw a NameNotFoundException
    119     // In the cases where an Activity from another package has been requested.
    120     ActivityInfo result = new ActivityInfo();
    121     result.name = activityName;
    122     result.packageName = packageName;
    123     result.applicationInfo = new ApplicationInfo();
    124     result.applicationInfo.packageName = packageName;
    125     return result;
    126   }
    127 
    128   @Implementation
    129   public boolean hasSystemFeature(String name) {
    130     return systemFeatureList.containsKey(name) ? systemFeatureList.get(name) : false;
    131   }
    132 
    133   @Implementation
    134   public int getComponentEnabledSetting(ComponentName componentName) {
    135     ComponentState state = componentList.get(componentName);
    136     return state != null ? state.newState : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
    137   }
    138 
    139   @Implementation
    140   public @Nullable String getNameForUid(int uid) {
    141     return namesForUid.get(uid);
    142   }
    143 
    144   @Implementation
    145   public @Nullable String[] getPackagesForUid(int uid) {
    146     String[] packageNames = packagesForUid.get(uid);
    147     if (packageNames != null) {
    148       return packageNames;
    149     }
    150 
    151     Set<String> results = new HashSet<>();
    152     for (PackageInfo packageInfo : packageInfos.values()) {
    153       if (packageInfo.applicationInfo != null && packageInfo.applicationInfo.uid == uid) {
    154         results.add(packageInfo.packageName);
    155       }
    156     }
    157 
    158     return results.isEmpty()
    159         ? null
    160         :results.toArray(new String[results.size()]);
    161   }
    162 
    163   @Implementation
    164   public int getApplicationEnabledSetting(String packageName) {
    165     try {
    166         getPackageInfo(packageName, -1);
    167     } catch (NameNotFoundException e) {
    168         throw new IllegalArgumentException(e);
    169     }
    170 
    171     return applicationEnabledSettingMap.get(packageName);
    172   }
    173 
    174   @Implementation
    175   public ProviderInfo getProviderInfo(ComponentName component, int flags) throws NameNotFoundException {
    176     String packageName = component.getPackageName();
    177 
    178     PackageInfo packageInfo = packageInfos.get(packageName);
    179     if (packageInfo != null && packageInfo.providers != null) {
    180       for (ProviderInfo provider : packageInfo.providers) {
    181         if (resolvePackageName(packageName, component).equals(provider.name)) {
    182           ProviderInfo result = new ProviderInfo();
    183           result.packageName = provider.packageName;
    184           result.name = provider.name;
    185           result.authority = provider.authority;
    186           result.readPermission = provider.readPermission;
    187           result.writePermission = provider.writePermission;
    188           result.pathPermissions = provider.pathPermissions;
    189 
    190           if ((flags & GET_META_DATA) != 0) {
    191             result.metaData = provider.metaData;
    192           }
    193           return result;
    194         }
    195       }
    196     }
    197 
    198     throw new NameNotFoundException("Package not found: " + packageName);
    199   }
    200 
    201   @Implementation
    202   public void setComponentEnabledSetting(ComponentName componentName, int newState, int flags) {
    203     componentList.put(componentName, new ComponentState(newState, flags));
    204   }
    205 
    206   @Override @Implementation
    207   public void setApplicationEnabledSetting(String packageName, int newState, int flags) {
    208     applicationEnabledSettingMap.put(packageName, newState);
    209   }
    210 
    211   @Implementation
    212   public ResolveInfo resolveActivity(Intent intent, int flags) {
    213     List<ResolveInfo> candidates = queryIntentActivities(intent, flags);
    214     return candidates.isEmpty() ? null : candidates.get(0);
    215   }
    216 
    217   @Implementation
    218   public ProviderInfo resolveContentProvider(String name, int flags) {
    219     if (name == null) {
    220       return null;
    221     }
    222     for (PackageInfo packageInfo : packageInfos.values()) {
    223       if (packageInfo.providers == null) continue;
    224 
    225       for (ProviderInfo providerInfo : packageInfo.providers) {
    226         if (name.equals(providerInfo.authority)) { // todo: support multiple authorities
    227           return providerInfo;
    228         }
    229       }
    230     }
    231     return null;
    232   }
    233 
    234   @Implementation
    235   public ProviderInfo resolveContentProviderAsUser(String name, int flags, @UserIdInt int userId) {
    236     return null;
    237   }
    238 
    239   @Implementation
    240   public PackageInfo getPackageInfo(String packageName, int flags) throws NameNotFoundException {
    241     PackageInfo info = packageInfos.get(packageName);
    242     if (info != null) {
    243       if (applicationEnabledSettingMap.get(packageName) == COMPONENT_ENABLED_STATE_DISABLED
    244           && (flags & MATCH_UNINSTALLED_PACKAGES) != MATCH_UNINSTALLED_PACKAGES
    245           && (flags & MATCH_DISABLED_COMPONENTS) != MATCH_DISABLED_COMPONENTS) {
    246         throw new NameNotFoundException("Package is disabled, can't find");
    247       }
    248       return info;
    249     } else {
    250       throw new NameNotFoundException(packageName);
    251     }
    252   }
    253 
    254   @Implementation
    255   public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
    256     // Check the manually added resolve infos first.
    257     List<ResolveInfo> resolveInfos = queryOverriddenIntents(intent, flags);
    258     if (!resolveInfos.isEmpty()) {
    259       return resolveInfos;
    260     }
    261 
    262     resolveInfos = new ArrayList<>();
    263     for (Package appPackage : packages.values()) {
    264       if (resolveInfos.isEmpty()) {
    265         for (Service service : appPackage.services) {
    266           IntentFilter intentFilter = matchIntentFilter(intent, service.intents, flags);
    267           if (intentFilter != null) {
    268             resolveInfos.add(getResolveInfo(service, intentFilter));
    269           }
    270         }
    271       }
    272     }
    273 
    274     return resolveInfos;
    275   }
    276 
    277   /**
    278    * Behaves as {@link #queryIntentServices(Intent, int)} and currently ignores userId.
    279    */
    280   @Implementation(minSdk = JELLY_BEAN_MR1)
    281   public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
    282     return queryIntentServices(intent, flags);
    283   }
    284 
    285   @Implementation
    286   public List<ResolveInfo> queryIntentActivities(Intent intent, int flags) {
    287     List<ResolveInfo> resolveInfoList = queryOverriddenIntents(intent, flags);
    288 
    289     if (resolveInfoList.isEmpty()) {
    290       if (isExplicitIntent(intent)) {
    291         ResolveInfo resolvedActivity = resolveActivityForExplicitIntent(intent);
    292         if (resolvedActivity != null) {
    293           resolveInfoList = new ArrayList<>();
    294           resolveInfoList.add(resolvedActivity);
    295         }
    296       } else {
    297         resolveInfoList = queryImplicitIntentActivities(intent, flags);
    298       }
    299     }
    300 
    301     // If the flag is set, no further filtering will happen.
    302     if ((flags & PackageManager.MATCH_ALL) == PackageManager.MATCH_ALL) {
    303       return resolveInfoList;
    304     }
    305 
    306     // Create a copy of the list for filtering
    307     resolveInfoList = new ArrayList<>(resolveInfoList);
    308 
    309     if ((flags & PackageManager.MATCH_SYSTEM_ONLY) == PackageManager.MATCH_SYSTEM_ONLY) {
    310       for (Iterator<ResolveInfo> iterator = resolveInfoList.iterator(); iterator.hasNext();) {
    311         ResolveInfo resolveInfo = iterator.next();
    312         if (resolveInfo.activityInfo == null || resolveInfo.activityInfo.applicationInfo == null) {
    313           iterator.remove();
    314         } else {
    315           final int applicationFlags = resolveInfo.activityInfo.applicationInfo.flags;
    316           if ((applicationFlags & ApplicationInfo.FLAG_SYSTEM) != ApplicationInfo.FLAG_SYSTEM) {
    317             iterator.remove();
    318           }
    319         }
    320       }
    321     }
    322 
    323     return resolveInfoList;
    324   }
    325 
    326   /**
    327    * Behaves as {@link #queryIntentActivities(Intent, int)} and currently ignores userId.
    328    */
    329   @Implementation(minSdk = JELLY_BEAN_MR1)
    330   protected List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent, int flags, int userId) {
    331     return queryIntentActivities(intent, flags);
    332   }
    333 
    334   /**
    335    * Returns true if intent has specified a specific component.
    336    */
    337   private static boolean isExplicitIntent(Intent intent) {
    338     return intent.getComponent() != null
    339         || (intent.getSelector() != null && intent.getSelector().getComponent() != null);
    340   }
    341 
    342   private ResolveInfo resolveActivityForExplicitIntent(Intent intent) {
    343     ComponentName component = intent.getComponent();
    344     if (component == null) {
    345       if (intent.getSelector() != null) {
    346         intent = intent.getSelector();
    347         component = intent.getComponent();
    348       }
    349     }
    350 
    351     if (component != null) {
    352       for (Package appPackage : packages.values()) {
    353 
    354         for (Activity activity : appPackage.activities) {
    355           if (component.equals(activity.getComponentName())) {
    356             return buildResolveInfo(activity);
    357           }
    358         }
    359       }
    360     }
    361 
    362     return null;
    363   }
    364 
    365   private List<ResolveInfo> queryImplicitIntentActivities(Intent intent, int flags) {
    366     List<ResolveInfo> resolveInfoList = new ArrayList<>();
    367     for (Package appPackage : packages.values()) {
    368       if (intent.getPackage() == null || intent.getPackage().equals(appPackage.packageName)) {
    369         for (Activity activity : appPackage.activities) {
    370           if (matchIntentFilter(intent, activity.intents, flags) != null) {
    371             resolveInfoList.add(buildResolveInfo(activity));
    372           }
    373         }
    374       }
    375     }
    376 
    377     return resolveInfoList;
    378   }
    379 
    380   private ResolveInfo buildResolveInfo(Activity activity) {
    381     ResolveInfo resolveInfo = new ResolveInfo();
    382     resolveInfo.resolvePackageName = activity.info.applicationInfo.packageName;
    383     resolveInfo.activityInfo = activity.info;
    384     return resolveInfo;
    385   }
    386 
    387   @Implementation
    388   public int checkPermission(String permName, String pkgName) {
    389     PackageInfo permissionsInfo = packageInfos.get(pkgName);
    390     if (permissionsInfo == null || permissionsInfo.requestedPermissions == null) {
    391       return PackageManager.PERMISSION_DENIED;
    392     }
    393     for (String permission : permissionsInfo.requestedPermissions) {
    394       if (permission != null && permission.equals(permName)) {
    395         return PackageManager.PERMISSION_GRANTED;
    396       }
    397     }
    398     return PackageManager.PERMISSION_DENIED;
    399   }
    400 
    401   @Implementation
    402   public ActivityInfo getReceiverInfo(ComponentName className, int flags) throws NameNotFoundException {
    403     String packageName = className.getPackageName();
    404 
    405     PackageInfo packageInfo = packageInfos.get(packageName);
    406     if (packageInfo != null && packageInfo.receivers != null) {
    407       for (ActivityInfo receiver : packageInfo.receivers) {
    408         if (resolvePackageName(packageName, className).equals(receiver.name)) {
    409           ActivityInfo result = new ActivityInfo();
    410           result.packageName = receiver.packageName;
    411           result.name = receiver.name;
    412           if ((flags & GET_META_DATA) != 0) {
    413             result.metaData = receiver.metaData;
    414           }
    415           return result;
    416         }
    417       }
    418     }
    419 
    420     return null;
    421   }
    422 
    423   @Implementation
    424   public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
    425     // Check the manually added resolve infos first.
    426     List<ResolveInfo> resolveInfos = queryOverriddenIntents(intent, flags);
    427     if (!resolveInfos.isEmpty()) {
    428       return resolveInfos;
    429     }
    430 
    431     resolveInfos = new ArrayList<>();
    432     for (Package appPackage : packages.values()) {
    433       if (resolveInfos.isEmpty()) {
    434         for (Activity receiver : appPackage.receivers) {
    435 
    436           IntentFilter intentFilter = matchIntentFilter(intent, receiver.intents, flags);
    437           if (intentFilter != null) {
    438             resolveInfos.add(getResolveInfo(receiver, intentFilter));
    439           }
    440         }
    441       }
    442     }
    443 
    444     return resolveInfos;
    445   }
    446 
    447   private static IntentFilter matchIntentFilter(
    448       Intent intent, ArrayList<? extends PackageParser.IntentInfo> intentFilters, int flags) {
    449     for (PackageParser.IntentInfo intentInfo : intentFilters) {
    450       if (intentInfo.match(
    451               intent.getAction(),
    452               intent.getType(),
    453               intent.getScheme(),
    454               intent.getData(),
    455               intent.getCategories(),
    456               "ShadowPackageManager")
    457           >= 0) {
    458         return intentInfo;
    459       }
    460     }
    461     return null;
    462   }
    463 
    464   @Implementation
    465   public ResolveInfo resolveService(Intent intent, int flags) {
    466     List<ResolveInfo> candidates = queryIntentServices(intent, flags);
    467     return candidates.isEmpty() ? null : candidates.get(0);
    468   }
    469 
    470   @Implementation
    471   public ServiceInfo getServiceInfo(ComponentName className, int flags) throws NameNotFoundException {
    472     String packageName = className.getPackageName();
    473     PackageInfo packageInfo = packageInfos.get(packageName);
    474 
    475     if (packageInfo != null) {
    476       String serviceName = className.getClassName();
    477       if (packageInfo.services != null) {
    478         for (ServiceInfo service : packageInfo.services) {
    479           if (serviceName.equals(service.name)) {
    480             ServiceInfo result = new ServiceInfo();
    481             result.packageName = service.packageName;
    482             result.name = service.name;
    483             result.applicationInfo = service.applicationInfo;
    484             result.permission = service.permission;
    485             if ((flags & GET_META_DATA) != 0) {
    486               result.metaData = service.metaData;
    487             }
    488             return result;
    489           }
    490         }
    491       }
    492       throw new NameNotFoundException(serviceName);
    493     }
    494     return null;
    495   }
    496 
    497   @Implementation
    498   public Resources getResourcesForApplication(@NonNull ApplicationInfo applicationInfo) throws PackageManager.NameNotFoundException {
    499     return getResourcesForApplication(applicationInfo.packageName);
    500   }
    501 
    502   @Implementation
    503   public List<ApplicationInfo> getInstalledApplications(int flags) {
    504     List<ApplicationInfo> result = new ArrayList<>();
    505 
    506     for (PackageInfo packageInfo : packageInfos.values()) {
    507       result.add(packageInfo.applicationInfo);
    508     }
    509     return result;
    510   }
    511 
    512   @Implementation
    513   public String getInstallerPackageName(String packageName) {
    514     return packageInstallerMap.get(packageName);
    515   }
    516 
    517   @Implementation
    518   public PermissionInfo getPermissionInfo(String name, int flags) throws NameNotFoundException {
    519     PermissionInfo permissionInfo = extraPermissions.get(name);
    520     if (permissionInfo != null) {
    521       return permissionInfo;
    522     }
    523 
    524     for (PackageInfo packageInfo : packageInfos.values()) {
    525       if (packageInfo.permissions != null) {
    526         for (PermissionInfo permission : packageInfo.permissions) {
    527           if (name.equals(permission.name)) {
    528             return createCopyPermissionInfo(permission, flags);
    529           }
    530         }
    531       }
    532     }
    533 
    534     throw new NameNotFoundException(name);
    535   }
    536 
    537   @Implementation(minSdk = M)
    538   public boolean shouldShowRequestPermissionRationale(String permission) {
    539     return permissionRationaleMap.containsKey(permission) ? permissionRationaleMap.get(permission) : false;
    540   }
    541 
    542   @Implementation
    543   public FeatureInfo[] getSystemAvailableFeatures() {
    544     return systemAvailableFeatures.isEmpty() ? null : systemAvailableFeatures.toArray(new FeatureInfo[systemAvailableFeatures.size()]);
    545   }
    546 
    547   @Implementation
    548   public void verifyPendingInstall(int id, int verificationCode) {
    549     if (verificationResults.containsKey(id)) {
    550       throw new IllegalStateException("Multiple verifications for id=" + id);
    551     }
    552     verificationResults.put(id, verificationCode);
    553   }
    554 
    555   @Implementation
    556   public void extendVerificationTimeout(int id, int verificationCodeAtTimeout, long millisecondsToDelay) {
    557     verificationTimeoutExtension.put(id, millisecondsToDelay);
    558   }
    559 
    560   @Implementation
    561   @Override
    562   public void freeStorageAndNotify(long freeStorageSize, IPackageDataObserver observer) {
    563   }
    564 
    565   @Implementation
    566   public void freeStorageAndNotify(String volumeUuid, long freeStorageSize, IPackageDataObserver observer) {
    567   }
    568 
    569   @Implementation
    570   public void setInstallerPackageName(String targetPackage, String installerPackageName) {
    571     packageInstallerMap.put(targetPackage, installerPackageName);
    572   }
    573 
    574   @Implementation
    575   public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
    576     return Collections.emptyList();
    577   }
    578 
    579   @Implementation
    580   public List<ResolveInfo> queryIntentContentProvidersAsUser(Intent intent, int flags, int userId) {
    581     return Collections.emptyList();
    582   }
    583 
    584   @Implementation
    585   public String getPermissionControllerPackageName() {
    586     return null;
    587   }
    588 
    589   @Implementation(maxSdk = JELLY_BEAN)
    590   public void getPackageSizeInfo(Object pkgName, Object observer) {
    591     final PackageStats packageStats = packageStatsMap.get((String) pkgName);
    592     new Handler(Looper.getMainLooper()).post(() -> {
    593       try {
    594         ((IPackageStatsObserver) observer).onGetStatsCompleted(packageStats, packageStats != null);
    595       } catch (RemoteException remoteException) {
    596         remoteException.rethrowFromSystemServer();
    597       }
    598     });
    599   }
    600 
    601   @Implementation(minSdk = JELLY_BEAN_MR1, maxSdk = M)
    602   public void getPackageSizeInfo(Object pkgName, Object uid, final Object observer) {
    603     final PackageStats packageStats = packageStatsMap.get((String) pkgName);
    604     new Handler(Looper.getMainLooper()).post(() -> {
    605       try {
    606         ((IPackageStatsObserver) observer).onGetStatsCompleted(packageStats, packageStats != null);
    607       } catch (RemoteException remoteException) {
    608         remoteException.rethrowFromSystemServer();
    609       }
    610     });
    611   }
    612 
    613   @Implementation(minSdk = N)
    614   public void getPackageSizeInfoAsUser(Object pkgName, Object uid, final Object observer) {
    615     final PackageStats packageStats = packageStatsMap.get((String) pkgName);
    616     new Handler(Looper.getMainLooper()).post(() -> {
    617       try {
    618         ((IPackageStatsObserver) observer).onGetStatsCompleted(packageStats, packageStats != null);
    619       } catch (RemoteException remoteException) {
    620         remoteException.rethrowFromSystemServer();
    621       }
    622     });
    623   }
    624 
    625   @Implementation
    626   public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
    627     pendingDeleteCallbacks.put(packageName, observer);
    628   }
    629 
    630   @Implementation
    631   public String[] currentToCanonicalPackageNames(String[] names) {
    632     String[] out = new String[names.length];
    633     for (int i = names.length - 1; i >= 0; i--) {
    634       if (currentToCanonicalNames.containsKey(names[i])) {
    635         out[i] = currentToCanonicalNames.get(names[i]);
    636       } else {
    637         out[i] = names[i];
    638       }
    639     }
    640     return out;
    641   }
    642 
    643   @Implementation
    644   public boolean isSafeMode() {
    645     return false;
    646   }
    647 
    648   @Implementation
    649   public Drawable getApplicationIcon(String packageName) throws NameNotFoundException {
    650     return applicationIcons.get(packageName);
    651   }
    652 
    653   @Implementation
    654   public Drawable getApplicationIcon(ApplicationInfo info) {
    655     return null;
    656   }
    657 
    658   @Implementation
    659   public Drawable getUserBadgeForDensity(UserHandle userHandle, int i) {
    660     return null;
    661   }
    662 
    663   @Implementation
    664   public int checkSignatures(String pkg1, String pkg2) {
    665     try {
    666       PackageInfo packageInfo1 = getPackageInfo(pkg1, GET_SIGNATURES);
    667       PackageInfo packageInfo2 = getPackageInfo(pkg2, GET_SIGNATURES);
    668       return compareSignature(packageInfo1.signatures, packageInfo2.signatures);
    669     } catch (NameNotFoundException e) {
    670       return SIGNATURE_UNKNOWN_PACKAGE;
    671     }
    672   }
    673 
    674   @Implementation
    675   public int checkSignatures(int uid1, int uid2) {
    676     return 0;
    677   }
    678 
    679   @Implementation
    680   public List<PermissionInfo> queryPermissionsByGroup(String group, int flags) throws NameNotFoundException {
    681     List<PermissionInfo> result = new ArrayList<>();
    682     for (PermissionInfo permissionInfo : extraPermissions.values()) {
    683       if (Objects.equals(permissionInfo.group, group)) {
    684         result.add(permissionInfo);
    685       }
    686     }
    687 
    688     for (PackageInfo packageInfo : packageInfos.values()) {
    689       if (packageInfo.permissions != null) {
    690         for (PermissionInfo permission : packageInfo.permissions) {
    691           if (Objects.equals(group, permission.group)) {
    692             result.add(createCopyPermissionInfo(permission, flags));
    693           }
    694         }
    695       }
    696     }
    697 
    698     return result;
    699   }
    700 
    701   private static PermissionInfo createCopyPermissionInfo(PermissionInfo src, int flags) {
    702     PermissionInfo matchedPermission = new PermissionInfo(src);
    703     if ((flags & GET_META_DATA) != GET_META_DATA) {
    704       matchedPermission.metaData = null;
    705     }
    706     return matchedPermission;
    707   }
    708 
    709   @Implementation
    710   public Intent getLaunchIntentForPackage(String packageName) {
    711     Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
    712     intentToResolve.addCategory(Intent.CATEGORY_INFO);
    713     intentToResolve.setPackage(packageName);
    714     List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
    715 
    716     if (ris == null || ris.isEmpty()) {
    717       intentToResolve.removeCategory(Intent.CATEGORY_INFO);
    718       intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
    719       intentToResolve.setPackage(packageName);
    720       ris = queryIntentActivities(intentToResolve, 0);
    721     }
    722     if (ris == null || ris.isEmpty()) {
    723       return null;
    724     }
    725     Intent intent = new Intent(intentToResolve);
    726     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    727     intent.setClassName(packageName, ris.get(0).activityInfo.name);
    728     return intent;
    729   }
    730 
    731   ////////////////////////////
    732 
    733   @Implementation
    734   public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
    735       throws NameNotFoundException {
    736     return null;
    737   }
    738 
    739   @Implementation
    740   public String[] canonicalToCurrentPackageNames(String[] names) {
    741     return new String[0];
    742   }
    743 
    744   @Implementation
    745   public Intent getLeanbackLaunchIntentForPackage(String packageName) {
    746     return null;
    747   }
    748 
    749   @Implementation
    750   public int[] getPackageGids(String packageName) throws NameNotFoundException {
    751     return new int[0];
    752   }
    753 
    754   @Implementation
    755   public int[] getPackageGids(String packageName, int flags) throws NameNotFoundException {
    756     return null;
    757   }
    758 
    759   @Implementation
    760   public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
    761     Integer uid = uidForPackage.get(packageName);
    762     if (uid == null) {
    763       throw new NameNotFoundException(packageName);
    764     }
    765     return uid;
    766   }
    767 
    768   @Implementation
    769   public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
    770     return 0;
    771   }
    772 
    773   @Implementation
    774   public int getPackageUidAsUser(String packageName, int flags, int userId)
    775       throws NameNotFoundException {
    776     return 0;
    777   }
    778 
    779   @Implementation
    780   public PermissionGroupInfo getPermissionGroupInfo(String name, int flags)
    781       throws NameNotFoundException {
    782     if (extraPermissionGroups.containsKey(name)) {
    783       return new PermissionGroupInfo(extraPermissionGroups.get(name));
    784     }
    785 
    786     for (Package pkg : packages.values()) {
    787       for (PermissionGroup permissionGroup : pkg.permissionGroups) {
    788         if (name.equals(permissionGroup.info.name)) {
    789           return PackageParser.generatePermissionGroupInfo(permissionGroup, flags);
    790         }
    791       }
    792     }
    793 
    794     throw new NameNotFoundException(name);
    795   }
    796 
    797   @Implementation
    798   public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
    799     ArrayList<PermissionGroupInfo> allPermissionGroups = new ArrayList<PermissionGroupInfo>();
    800     // To be consistent with Android's implementation, return at most one PermissionGroupInfo object
    801     // per permission group string
    802     HashSet<String> handledPermissionGroups = new HashSet<>();
    803 
    804     for (PermissionGroupInfo permissionGroupInfo : extraPermissionGroups.values()) {
    805       allPermissionGroups.add(new PermissionGroupInfo(permissionGroupInfo));
    806       handledPermissionGroups.add(permissionGroupInfo.name);
    807     }
    808 
    809     for (Package pkg : packages.values()) {
    810       for (PermissionGroup permissionGroup : pkg.permissionGroups) {
    811         if (!handledPermissionGroups.contains(permissionGroup.info.name)) {
    812           PermissionGroupInfo permissionGroupInfo = PackageParser
    813               .generatePermissionGroupInfo(permissionGroup, flags);
    814           allPermissionGroups.add(new PermissionGroupInfo(permissionGroupInfo));
    815           handledPermissionGroups.add(permissionGroup.info.name);
    816         }
    817       }
    818     }
    819 
    820     return allPermissionGroups;
    821   }
    822 
    823   @Implementation
    824   public ApplicationInfo getApplicationInfo(String packageName, int flags) throws NameNotFoundException {
    825     PackageInfo info = packageInfos.get(packageName);
    826     if (info != null) {
    827       try {
    828         PackageInfo packageInfo = getPackageInfo(packageName, -1);
    829       } catch (NameNotFoundException e) {
    830         throw new IllegalArgumentException(e);
    831       }
    832 
    833       if (applicationEnabledSettingMap.get(packageName) == COMPONENT_ENABLED_STATE_DISABLED
    834           && (flags & MATCH_UNINSTALLED_PACKAGES) != MATCH_UNINSTALLED_PACKAGES
    835           && (flags & MATCH_DISABLED_COMPONENTS) != MATCH_DISABLED_COMPONENTS) {
    836         throw new NameNotFoundException("Package is disabled, can't find");
    837       }
    838       return info.applicationInfo;
    839     } else {
    840       throw new NameNotFoundException(packageName);
    841     }
    842   }
    843 
    844   @Implementation
    845   public String[] getSystemSharedLibraryNames() {
    846     return new String[0];
    847   }
    848 
    849   @Implementation
    850   public
    851   @NonNull
    852   String getServicesSystemSharedLibraryPackageName() {
    853     return null;
    854   }
    855 
    856   @Implementation
    857   public
    858   @NonNull
    859   String getSharedSystemSharedLibraryPackageName() {
    860     return "";
    861   }
    862 
    863   @Implementation
    864   public boolean hasSystemFeature(String name, int version) {
    865     return false;
    866   }
    867 
    868   @Implementation
    869   public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
    870     return false;
    871   }
    872 
    873   @Implementation
    874   public boolean addPermission(PermissionInfo info) {
    875     return false;
    876   }
    877 
    878   @Implementation
    879   public boolean addPermissionAsync(PermissionInfo info) {
    880     return false;
    881   }
    882 
    883   @Implementation
    884   public void removePermission(String name) {
    885   }
    886 
    887   @Implementation
    888   public void grantRuntimePermission(String packageName, String permissionName, UserHandle user) {
    889   }
    890 
    891   @Implementation
    892   public void revokeRuntimePermission(String packageName, String permissionName, UserHandle user) {
    893   }
    894 
    895   @Implementation
    896   public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
    897     return 0;
    898   }
    899 
    900   @Implementation
    901   public void updatePermissionFlags(String permissionName, String packageName, int flagMask,
    902       int flagValues, UserHandle user) {
    903   }
    904 
    905   @Implementation
    906   public int getUidForSharedUser(String sharedUserName) throws NameNotFoundException {
    907     return 0;
    908   }
    909 
    910   @Implementation
    911   public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
    912     return null;
    913   }
    914 
    915   @Implementation
    916   public List<PackageInfo> getPackagesHoldingPermissions(String[] permissions, int flags) {
    917     return null;
    918   }
    919 
    920   @Implementation
    921   public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
    922     return null;
    923   }
    924 
    925   @Implementation
    926   public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller, Intent[] specifics, Intent intent, int flags) {
    927     return null;
    928   }
    929 
    930   @Implementation
    931   public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
    932     return null;
    933   }
    934 
    935   @Implementation
    936   public List<ProviderInfo> queryContentProviders(String processName, int uid, int flags) {
    937     return null;
    938   }
    939 
    940   @Implementation
    941   public InstrumentationInfo getInstrumentationInfo(ComponentName className, int flags) throws NameNotFoundException {
    942     return null;
    943   }
    944 
    945   @Implementation
    946   public List<InstrumentationInfo> queryInstrumentation(String targetPackage, int flags) {
    947     return null;
    948   }
    949 
    950   @Override @Nullable
    951   @Implementation
    952   public Drawable getDrawable(String packageName, @DrawableRes int resId, @Nullable ApplicationInfo appInfo) {
    953     return drawables.get(new Pair<>(packageName, resId));
    954   }
    955 
    956   @Override @Implementation
    957   public Drawable getActivityIcon(ComponentName activityName) throws NameNotFoundException {
    958     return drawableList.get(activityName);
    959   }
    960 
    961   @Override public Drawable getActivityIcon(Intent intent) throws NameNotFoundException {
    962     return drawableList.get(intent.getComponent());
    963   }
    964 
    965   @Implementation
    966   public Drawable getDefaultActivityIcon() {
    967     return Resources.getSystem().getDrawable(com.android.internal.R.drawable.sym_def_app_icon);
    968   }
    969 
    970   @Implementation
    971   public Drawable getActivityBanner(ComponentName activityName) throws NameNotFoundException {
    972     return null;
    973   }
    974 
    975   @Implementation
    976   public Drawable getActivityBanner(Intent intent) throws NameNotFoundException {
    977     return null;
    978   }
    979 
    980   @Implementation
    981   public Drawable getApplicationBanner(ApplicationInfo info) {
    982     return null;
    983   }
    984 
    985   @Implementation
    986   public Drawable getApplicationBanner(String packageName) throws NameNotFoundException {
    987     return null;
    988   }
    989 
    990   @Implementation
    991   public Drawable getActivityLogo(ComponentName activityName) throws NameNotFoundException {
    992     return null;
    993   }
    994 
    995   @Implementation
    996   public Drawable getActivityLogo(Intent intent) throws NameNotFoundException {
    997     return null;
    998   }
    999 
   1000   @Implementation
   1001   public Drawable getApplicationLogo(ApplicationInfo info) {
   1002     return null;
   1003   }
   1004 
   1005   @Implementation
   1006   public Drawable getApplicationLogo(String packageName) throws NameNotFoundException {
   1007     return null;
   1008   }
   1009 
   1010   @Implementation
   1011   public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
   1012     return null;
   1013   }
   1014 
   1015   @Implementation
   1016   public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user, Rect badgeLocation, int badgeDensity) {
   1017     return null;
   1018   }
   1019 
   1020   @Implementation
   1021   public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
   1022     return null;
   1023   }
   1024 
   1025   @Implementation
   1026   public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
   1027     return null;
   1028   }
   1029 
   1030   @Implementation
   1031   public Resources getResourcesForActivity(ComponentName activityName) throws NameNotFoundException {
   1032     return null;
   1033   }
   1034 
   1035   @Implementation
   1036   public Resources getResourcesForApplication(String appPackageName) throws NameNotFoundException {
   1037     if (RuntimeEnvironment.application.getPackageName().equals(appPackageName)) {
   1038       return RuntimeEnvironment.application.getResources();
   1039     } else if (packageInfos.containsKey(appPackageName)) {
   1040       Resources appResources = resources.get(appPackageName);
   1041       if (appResources == null) {
   1042         appResources = new Resources(new AssetManager(), null, null);
   1043         resources.put(appPackageName, appResources);
   1044       }
   1045       return appResources;
   1046     }
   1047     throw new NameNotFoundException(appPackageName);
   1048   }
   1049 
   1050   @Implementation
   1051   public Resources getResourcesForApplicationAsUser(String appPackageName, int userId) throws NameNotFoundException {
   1052     return null;
   1053   }
   1054 
   1055   @Implementation
   1056   public void addOnPermissionsChangeListener(Object listener) {
   1057   }
   1058 
   1059   @Implementation
   1060   public void removeOnPermissionsChangeListener(Object listener) {
   1061   }
   1062 
   1063   @Implementation
   1064   public void installPackage(Object packageURI, Object observer, Object flags, Object installerPackageName) {
   1065   }
   1066 
   1067   @Implementation
   1068   public int installExistingPackage(String packageName) throws NameNotFoundException {
   1069     return 0;
   1070   }
   1071 
   1072   @Implementation
   1073   public int installExistingPackageAsUser(String packageName, int userId) throws NameNotFoundException {
   1074     return 0;
   1075   }
   1076 
   1077   @Implementation
   1078   public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
   1079   }
   1080 
   1081   @Implementation
   1082   public int getIntentVerificationStatusAsUser(String packageName, int userId) {
   1083     return 0;
   1084   }
   1085 
   1086   @Implementation
   1087   public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
   1088     return false;
   1089   }
   1090 
   1091   @Implementation
   1092   public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
   1093     return null;
   1094   }
   1095 
   1096   @Implementation
   1097   public List<IntentFilter> getAllIntentFilters(String packageName) {
   1098     return null;
   1099   }
   1100 
   1101   @Implementation
   1102   public String getDefaultBrowserPackageNameAsUser(int userId) {
   1103     return null;
   1104   }
   1105 
   1106   @Implementation
   1107   public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
   1108     return false;
   1109   }
   1110 
   1111   @Implementation
   1112   public int getMoveStatus(int moveId) {
   1113     return 0;
   1114   }
   1115 
   1116   @Implementation
   1117   public void registerMoveCallback(Object callback, Object handler) {
   1118   }
   1119 
   1120   @Implementation
   1121   public void unregisterMoveCallback(Object callback) {
   1122   }
   1123 
   1124   @Implementation
   1125   public Object movePackage(Object packageName, Object vol) {
   1126     return 0;
   1127   }
   1128 
   1129   @Implementation
   1130   public Object getPackageCurrentVolume(Object app) {
   1131     return null;
   1132   }
   1133 
   1134   @Implementation
   1135   public List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
   1136     return null;
   1137   }
   1138 
   1139   @Implementation
   1140   public Object movePrimaryStorage(Object vol) {
   1141     return 0;
   1142   }
   1143 
   1144   @Implementation
   1145   public @Nullable Object getPrimaryStorageCurrentVolume() {
   1146     return null;
   1147   }
   1148 
   1149   @Implementation
   1150   public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
   1151     return null;
   1152   }
   1153 
   1154   @Implementation
   1155   public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags, int userId) {
   1156   }
   1157 
   1158   @Implementation
   1159   public void clearApplicationUserData(String packageName, IPackageDataObserver observer) {
   1160   }
   1161 
   1162   @Implementation
   1163   public void deleteApplicationCacheFiles(String packageName, IPackageDataObserver observer) {
   1164   }
   1165 
   1166   @Implementation
   1167   public void deleteApplicationCacheFilesAsUser(String packageName, int userId, IPackageDataObserver observer) {
   1168   }
   1169 
   1170   @Implementation
   1171   public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
   1172   }
   1173 
   1174   @Implementation
   1175   public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended, int userId) {
   1176     return null;
   1177   }
   1178 
   1179   @Implementation
   1180   public boolean isPackageSuspendedForUser(String packageName, int userId) {
   1181     return false;
   1182   }
   1183 
   1184   @Implementation
   1185   public void addPackageToPreferred(String packageName) {
   1186   }
   1187 
   1188   @Implementation
   1189   public void removePackageFromPreferred(String packageName) {
   1190   }
   1191 
   1192   @Implementation
   1193   public List<PackageInfo> getPreferredPackages(int flags) {
   1194     return null;
   1195   }
   1196 
   1197   @Override public void addPreferredActivity(IntentFilter filter, int match, ComponentName[] set, ComponentName activity) {
   1198     preferredActivities.put(filter, activity);
   1199   }
   1200 
   1201   @Implementation
   1202   public void replacePreferredActivity(IntentFilter filter, int match, ComponentName[] set, ComponentName activity) {
   1203   }
   1204 
   1205   @Implementation
   1206   public void clearPackagePreferredActivities(String packageName) {
   1207   }
   1208 
   1209   @Override public int getPreferredActivities(List<IntentFilter> outFilters,
   1210       List<ComponentName> outActivities, String packageName) {
   1211     if (outFilters == null) {
   1212       return 0;
   1213     }
   1214 
   1215     Set<IntentFilter> filters = preferredActivities.keySet();
   1216     for (IntentFilter filter : outFilters) {
   1217       step:
   1218       for (IntentFilter testFilter : filters) {
   1219         ComponentName name = preferredActivities.get(testFilter);
   1220         // filter out based on the given packageName;
   1221         if (packageName != null && !name.getPackageName().equals(packageName)) {
   1222           continue step;
   1223         }
   1224 
   1225         // Check actions
   1226         Iterator<String> iterator = filter.actionsIterator();
   1227         while (iterator.hasNext()) {
   1228           if (!testFilter.matchAction(iterator.next())) {
   1229             continue step;
   1230           }
   1231         }
   1232 
   1233         iterator = filter.categoriesIterator();
   1234         while (iterator.hasNext()) {
   1235           if (!filter.hasCategory(iterator.next())) {
   1236             continue step;
   1237           }
   1238         }
   1239 
   1240         if (outActivities == null) {
   1241           outActivities = new ArrayList<>();
   1242         }
   1243 
   1244         outActivities.add(name);
   1245       }
   1246     }
   1247 
   1248     return 0;
   1249   }
   1250 
   1251   @Implementation
   1252   public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
   1253     return null;
   1254   }
   1255 
   1256   @Implementation
   1257   public void flushPackageRestrictionsAsUser(int userId) {
   1258   }
   1259 
   1260   @Implementation
   1261   public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden, UserHandle user) {
   1262     return false;
   1263   }
   1264 
   1265   @Implementation
   1266   public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
   1267     return false;
   1268   }
   1269 
   1270   @Implementation
   1271   public Object getKeySetByAlias(String packageName, String alias) {
   1272     return null;
   1273   }
   1274 
   1275   @Implementation
   1276   public Object getSigningKeySet(String packageName) {
   1277     return null;
   1278   }
   1279 
   1280   @Implementation
   1281   public boolean isSignedBy(String packageName, Object ks) {
   1282     return false;
   1283   }
   1284 
   1285   @Implementation
   1286   public boolean isSignedByExactly(String packageName, Object ks) {
   1287     return false;
   1288   }
   1289 
   1290   @Implementation
   1291   public VerifierDeviceIdentity getVerifierDeviceIdentity() {
   1292     return null;
   1293   }
   1294 
   1295   @Implementation
   1296   public boolean isUpgrade() {
   1297     return false;
   1298   }
   1299 
   1300   @Implementation
   1301   public boolean isPackageAvailable(String packageName) {
   1302     return false;
   1303   }
   1304 
   1305   @Implementation
   1306   public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId, int flags) {
   1307   }
   1308 
   1309   @Implementation
   1310   public void clearCrossProfileIntentFilters(int sourceUserId) {
   1311   }
   1312 
   1313   @Implementation
   1314   public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
   1315     return null;
   1316   }
   1317 
   1318   @Implementation
   1319   public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
   1320     return null;
   1321   }
   1322 
   1323   // BEGIN-INTERNAL
   1324   @Implementation(minSdk = P)
   1325   public String getSystemTextClassifierPackageName() {
   1326     return "";
   1327   }
   1328   // END-INTERNAL
   1329 }
   1330