1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.testing.shadows; 18 19 import static android.content.pm.PackageManager.NameNotFoundException; 20 21 import android.app.ApplicationPackageManager; 22 import android.content.Intent; 23 import android.content.pm.PackageInfo; 24 import android.content.pm.ResolveInfo; 25 import android.os.UserHandle; 26 import android.util.ArrayMap; 27 28 import org.robolectric.annotation.Implements; 29 import org.robolectric.annotation.Resetter; 30 31 import java.util.ArrayList; 32 import java.util.HashMap; 33 import java.util.List; 34 import java.util.Map; 35 36 /** 37 * Extends {@link org.robolectric.shadows.ShadowApplicationPackageManager} to return the correct 38 * package in user-specific invocations. 39 */ 40 @Implements(value = ApplicationPackageManager.class) 41 public class ShadowApplicationPackageManager 42 extends org.robolectric.shadows.ShadowApplicationPackageManager { 43 private static final Map<String, PackageInfo> sPackageInfos = new ArrayMap<>(); 44 private static final List<PackageInfo> sInstalledPackages = new ArrayList<>(); 45 private static final Map<String, Integer> sPackageUids = new ArrayMap<>(); 46 private static final Map<Integer, Map<String, Integer>> sUserPackageUids = new ArrayMap<>(); 47 48 /** 49 * Registers the package {@code packageName} to be returned when invoking {@link 50 * ApplicationPackageManager#getPackageInfoAsUser(String, int, int)} and {@link 51 * ApplicationPackageManager#getInstalledPackagesAsUser(int, int)}. 52 */ addInstalledPackage(String packageName, PackageInfo packageInfo)53 public static void addInstalledPackage(String packageName, PackageInfo packageInfo) { 54 sPackageInfos.put(packageName, packageInfo); 55 sInstalledPackages.add(packageInfo); 56 } 57 58 /** 59 * Sets the package uid {@code packageUid} for the package {@code packageName} to be returned 60 * when invoking {@link ApplicationPackageManager#getPackageUidAsUser(String, int, int)}. 61 */ setPackageUid(String packageName, int packageUid)62 public static void setPackageUid(String packageName, int packageUid) { 63 sPackageUids.put(packageName, packageUid); 64 } 65 66 /** 67 * Sets the package uid {@code packageUid} for the package {@code packageName} to be returned 68 * when invoking {@link ApplicationPackageManager#getPackageUidAsUser(String, int, int)}. 69 */ setPackageUidAsUser(String packageName, int packageUid, int userId)70 public static void setPackageUidAsUser(String packageName, int packageUid, int userId) { 71 final Map<String, Integer> userPackageUids = 72 sUserPackageUids.containsKey(userId) 73 ? sUserPackageUids.get(userId) 74 : new HashMap<>(); 75 userPackageUids.put(packageName, packageUid); 76 sUserPackageUids.put(userId, userPackageUids); 77 } 78 79 @Override getPackageInfoAsUser(String packageName, int flags, int userId)80 protected PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId) 81 throws NameNotFoundException { 82 if (!sPackageInfos.containsKey(packageName)) { 83 throw new NameNotFoundException(packageName); 84 } 85 return sPackageInfos.get(packageName); 86 } 87 88 @Override getInstalledPackagesAsUser(int flags, int userId)89 protected List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) { 90 return sInstalledPackages; 91 } 92 93 @Override getPackageUidAsUser(String packageName, int flags, int userId)94 protected int getPackageUidAsUser(String packageName, int flags, int userId) 95 throws NameNotFoundException { 96 if (sUserPackageUids.containsKey(userId) 97 && sUserPackageUids.get(userId).containsKey(packageName)) { 98 return sUserPackageUids.get(userId).get(packageName); 99 } 100 if (!sPackageUids.containsKey(packageName)) { 101 throw new NameNotFoundException(packageName); 102 } 103 return sPackageUids.get(packageName); 104 } 105 106 @Override queryBroadcastReceiversAsUser( Intent intent, int flags, UserHandle userHandle)107 protected List<ResolveInfo> queryBroadcastReceiversAsUser( 108 Intent intent, int flags, UserHandle userHandle) { 109 // Currently does not handle multi-user. 110 return queryBroadcastReceivers(intent, flags); 111 } 112 113 /** Clear package state. */ 114 @Resetter reset()115 public static void reset() { 116 sPackageInfos.clear(); 117 sInstalledPackages.clear(); 118 org.robolectric.shadows.ShadowApplicationPackageManager.reset(); 119 } 120 } 121