blob: 9f2fffddde5ec5b4abaa17a8f9edfd63b3a35cba [file] [log] [blame]
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001/*
2 * Copyright (C) 2010 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
17package android.app;
18
Tor Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.DrawableRes;
Svet Ganov67882122016-12-11 16:36:34 -080020import android.annotation.IntRange;
Alan Viveretteecd585a2015-04-13 10:32:51 -070021import android.annotation.NonNull;
22import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070023import android.annotation.StringRes;
24import android.annotation.XmlRes;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080025import android.content.ComponentName;
26import android.content.ContentResolver;
Yao Chen022b8ea2016-12-16 11:03:28 -080027import android.content.Context;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080028import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.IntentSender;
31import android.content.pm.ActivityInfo;
32import android.content.pm.ApplicationInfo;
33import android.content.pm.ComponentInfo;
Svet Ganov2acf0632015-11-24 19:10:59 -080034import android.content.pm.EphemeralApplicationInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080035import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070036import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080037import android.content.pm.IPackageDataObserver;
38import android.content.pm.IPackageDeleteObserver;
39import android.content.pm.IPackageInstallObserver;
40import android.content.pm.IPackageManager;
41import android.content.pm.IPackageMoveObserver;
42import android.content.pm.IPackageStatsObserver;
43import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080044import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070045import android.content.pm.KeySet;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080046import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070047import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010048import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080049import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070050import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080051import android.content.pm.PermissionGroupInfo;
52import android.content.pm.PermissionInfo;
53import android.content.pm.ProviderInfo;
54import android.content.pm.ResolveInfo;
55import android.content.pm.ServiceInfo;
Svet Ganov67882122016-12-11 16:36:34 -080056import android.content.pm.SharedLibraryInfo;
Kenny Root0aaa0d92011-09-12 16:42:55 -070057import android.content.pm.VerifierDeviceIdentity;
Svet Ganov67882122016-12-11 16:36:34 -080058import android.content.pm.VersionedPackage;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080059import android.content.res.Resources;
60import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070061import android.graphics.Bitmap;
62import android.graphics.Canvas;
63import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010064import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080065import android.graphics.drawable.Drawable;
Kenny Guy02c89902016-11-15 19:36:38 +000066import android.graphics.drawable.LayerDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080067import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070068import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070069import android.os.Handler;
70import android.os.Looper;
71import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080072import android.os.Process;
73import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070074import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080075import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010076import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070077import android.os.storage.StorageManager;
78import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070079import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070080import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080081import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070082import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070083
84import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070085
86import com.android.internal.annotations.GuardedBy;
Kenny Guy02c89902016-11-15 19:36:38 +000087import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070088import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070089import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010090import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080091import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070092
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080093import java.lang.ref.WeakReference;
94import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080095import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070096import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080097import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070098import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070099import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800100
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700101/** @hide */
102public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800103 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800104 private final static boolean DEBUG_ICONS = false;
105
Svet Ganov2acf0632015-11-24 19:10:59 -0800106 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
107
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700108 // Default flags to use with PackageManager when no flags are given.
109 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
110
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700111 private final Object mLock = new Object();
112
113 @GuardedBy("mLock")
114 private UserManager mUserManager;
115 @GuardedBy("mLock")
116 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100117
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700118 @GuardedBy("mDelegates")
119 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
120
Svet Ganovf1b7f202015-07-29 08:33:42 -0700121 @GuardedBy("mLock")
122 private String mPermissionsControllerPackageName;
123
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100124 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700125 synchronized (mLock) {
126 if (mUserManager == null) {
127 mUserManager = UserManager.get(mContext);
128 }
129 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100130 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100131 }
132
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800133 @Override
134 public PackageInfo getPackageInfo(String packageName, int flags)
135 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100136 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
137 }
138
139 @Override
Svet Ganov67882122016-12-11 16:36:34 -0800140 public PackageInfo getPackageInfo(VersionedPackage versionedPackage, int flags)
141 throws NameNotFoundException {
142 try {
143 PackageInfo pi = mPM.getPackageInfoVersioned(versionedPackage, flags,
144 mContext.getUserId());
145 if (pi != null) {
146 return pi;
147 }
148 } catch (RemoteException e) {
149 throw e.rethrowFromSystemServer();
150 }
151 throw new NameNotFoundException(versionedPackage.toString());
152 }
153
154 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100155 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
156 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800157 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100158 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800159 if (pi != null) {
160 return pi;
161 }
162 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700163 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800164 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800165 throw new NameNotFoundException(packageName);
166 }
167
168 @Override
169 public String[] currentToCanonicalPackageNames(String[] names) {
170 try {
171 return mPM.currentToCanonicalPackageNames(names);
172 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700173 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800174 }
175 }
176
177 @Override
178 public String[] canonicalToCurrentPackageNames(String[] names) {
179 try {
180 return mPM.canonicalToCurrentPackageNames(names);
181 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700182 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800183 }
184 }
185
186 @Override
187 public Intent getLaunchIntentForPackage(String packageName) {
188 // First see if the package has an INFO activity; the existence of
189 // such an activity is implied to be the desired front-door for the
190 // overall package (such as if it has multiple launcher entries).
191 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
192 intentToResolve.addCategory(Intent.CATEGORY_INFO);
193 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800194 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800195
196 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800197 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800198 // reuse the intent instance
199 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
200 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
201 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800202 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800203 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800204 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800205 return null;
206 }
207 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800208 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800209 intent.setClassName(ris.get(0).activityInfo.packageName,
210 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800211 return intent;
212 }
213
214 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700215 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
216 // Try to find a main leanback_launcher activity.
217 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
218 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
219 intentToResolve.setPackage(packageName);
220 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
221
222 if (ris == null || ris.size() <= 0) {
223 return null;
224 }
225 Intent intent = new Intent(intentToResolve);
226 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
227 intent.setClassName(ris.get(0).activityInfo.packageName,
228 ris.get(0).activityInfo.name);
229 return intent;
230 }
231
232 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700233 public int[] getPackageGids(String packageName) throws NameNotFoundException {
234 return getPackageGids(packageName, 0);
235 }
236
237 @Override
238 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800239 throws NameNotFoundException {
240 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700241 int[] gids = mPM.getPackageGids(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800242 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800243 return gids;
244 }
245 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700246 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800247 }
248
249 throw new NameNotFoundException(packageName);
250 }
251
252 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700253 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
254 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
255 }
256
257 @Override
258 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
259 return getPackageUidAsUser(packageName, 0, userId);
260 }
261
262 @Override
263 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800264 throws NameNotFoundException {
265 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700266 int uid = mPM.getPackageUid(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800267 if (uid >= 0) {
268 return uid;
269 }
270 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700271 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800272 }
273
274 throw new NameNotFoundException(packageName);
275 }
276
277 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800278 public PermissionInfo getPermissionInfo(String name, int flags)
279 throws NameNotFoundException {
280 try {
281 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
282 if (pi != null) {
283 return pi;
284 }
285 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700286 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800287 }
288
289 throw new NameNotFoundException(name);
290 }
291
292 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700293 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800294 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
295 throws NameNotFoundException {
296 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700297 ParceledListSlice<PermissionInfo> parceledList =
298 mPM.queryPermissionsByGroup(group, flags);
299 if (parceledList != null) {
300 List<PermissionInfo> pi = parceledList.getList();
301 if (pi != null) {
302 return pi;
303 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800304 }
305 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700306 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800307 }
308
309 throw new NameNotFoundException(group);
310 }
311
312 @Override
313 public PermissionGroupInfo getPermissionGroupInfo(String name,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700314 int flags) throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800315 try {
316 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
317 if (pgi != null) {
318 return pgi;
319 }
320 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700321 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800322 }
323
324 throw new NameNotFoundException(name);
325 }
326
327 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700328 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800329 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
330 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700331 ParceledListSlice<PermissionGroupInfo> parceledList =
332 mPM.getAllPermissionGroups(flags);
333 if (parceledList == null) {
334 return Collections.emptyList();
335 }
336 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800337 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700338 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800339 }
340 }
341
342 @Override
343 public ApplicationInfo getApplicationInfo(String packageName, int flags)
344 throws NameNotFoundException {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700345 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
346 }
347
348 @Override
349 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
350 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800351 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700352 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800353 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100354 // This is a temporary hack. Callers must use
355 // createPackageContext(packageName).getApplicationInfo() to
356 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800357 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800358 }
359 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700360 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800361 }
362
363 throw new NameNotFoundException(packageName);
364 }
365
Tao Baic9a02372016-01-12 15:02:24 -0800366 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100367 // If we're dealing with a multi-arch application that has both
368 // 32 and 64 bit shared libraries, we might need to choose the secondary
369 // depending on what the current runtime's instruction set is.
370 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
371 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500372
373 // Get the instruction set that the libraries of secondary Abi is supported.
374 // In presence of a native bridge this might be different than the one secondary Abi used.
375 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
376 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
377 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100378
379 // If the runtimeIsa is the same as the primary isa, then we do nothing.
380 // Everything will be set up correctly because info.nativeLibraryDir will
381 // correspond to the right ISA.
382 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800383 ApplicationInfo modified = new ApplicationInfo(info);
384 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
385 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100386 }
387 }
Tao Baic9a02372016-01-12 15:02:24 -0800388 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100389 }
390
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800391 @Override
392 public ActivityInfo getActivityInfo(ComponentName className, int flags)
393 throws NameNotFoundException {
394 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700395 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800396 if (ai != null) {
397 return ai;
398 }
399 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700400 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800401 }
402
403 throw new NameNotFoundException(className.toString());
404 }
405
406 @Override
407 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
408 throws NameNotFoundException {
409 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700410 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800411 if (ai != null) {
412 return ai;
413 }
414 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700415 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800416 }
417
418 throw new NameNotFoundException(className.toString());
419 }
420
421 @Override
422 public ServiceInfo getServiceInfo(ComponentName className, int flags)
423 throws NameNotFoundException {
424 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700425 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800426 if (si != null) {
427 return si;
428 }
429 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700430 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800431 }
432
433 throw new NameNotFoundException(className.toString());
434 }
435
436 @Override
437 public ProviderInfo getProviderInfo(ComponentName className, int flags)
438 throws NameNotFoundException {
439 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700440 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800441 if (pi != null) {
442 return pi;
443 }
444 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700445 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800446 }
447
448 throw new NameNotFoundException(className.toString());
449 }
450
451 @Override
452 public String[] getSystemSharedLibraryNames() {
453 try {
454 return mPM.getSystemSharedLibraryNames();
455 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700456 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800457 }
458 }
459
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800460 /** @hide */
461 @Override
Svet Ganov67882122016-12-11 16:36:34 -0800462 public @NonNull List<SharedLibraryInfo> getSharedLibraries(int flags) {
463 return getSharedLibrariesAsUser(flags, mContext.getUserId());
464 }
465
466 /** @hide */
467 @Override
468 @SuppressWarnings("unchecked")
469 public @NonNull List<SharedLibraryInfo> getSharedLibrariesAsUser(int flags, int userId) {
470 try {
471 ParceledListSlice<SharedLibraryInfo> sharedLibs = mPM.getSharedLibraries(
472 flags, userId);
473 if (sharedLibs == null) {
474 return Collections.emptyList();
475 }
476 return sharedLibs.getList();
477 } catch (RemoteException e) {
478 throw e.rethrowFromSystemServer();
479 }
480 }
481
482 /** @hide */
483 @Override
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700484 public @NonNull String getServicesSystemSharedLibraryPackageName() {
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800485 try {
486 return mPM.getServicesSystemSharedLibraryPackageName();
487 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700488 throw e.rethrowFromSystemServer();
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800489 }
490 }
491
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700492 /**
493 * @hide
494 */
495 public @NonNull String getSharedSystemSharedLibraryPackageName() {
496 try {
497 return mPM.getSharedSystemSharedLibraryPackageName();
498 } catch (RemoteException e) {
499 throw e.rethrowFromSystemServer();
500 }
501 }
502
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800503 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700504 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800505 public FeatureInfo[] getSystemAvailableFeatures() {
506 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700507 ParceledListSlice<FeatureInfo> parceledList =
508 mPM.getSystemAvailableFeatures();
509 if (parceledList == null) {
510 return new FeatureInfo[0];
511 }
512 final List<FeatureInfo> list = parceledList.getList();
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700513 final FeatureInfo[] res = new FeatureInfo[list.size()];
514 for (int i = 0; i < res.length; i++) {
515 res[i] = list.get(i);
516 }
517 return res;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800518 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700519 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800520 }
521 }
522
523 @Override
524 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700525 return hasSystemFeature(name, 0);
526 }
527
528 @Override
529 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800530 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700531 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800532 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700533 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800534 }
535 }
536
537 @Override
538 public int checkPermission(String permName, String pkgName) {
539 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800540 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800541 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700542 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800543 }
544 }
545
546 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700547 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
548 try {
549 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
550 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700551 throw e.rethrowFromSystemServer();
Svet Ganovad3b2972015-07-07 22:49:17 -0700552 }
553 }
554
Svet Ganovf1b7f202015-07-29 08:33:42 -0700555 /**
556 * @hide
557 */
558 @Override
559 public String getPermissionControllerPackageName() {
560 synchronized (mLock) {
561 if (mPermissionsControllerPackageName == null) {
562 try {
563 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
564 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700565 throw e.rethrowFromSystemServer();
Svet Ganovf1b7f202015-07-29 08:33:42 -0700566 }
567 }
568 return mPermissionsControllerPackageName;
569 }
570 }
571
Svet Ganovad3b2972015-07-07 22:49:17 -0700572 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800573 public boolean addPermission(PermissionInfo info) {
574 try {
575 return mPM.addPermission(info);
576 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700577 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800578 }
579 }
580
581 @Override
582 public boolean addPermissionAsync(PermissionInfo info) {
583 try {
584 return mPM.addPermissionAsync(info);
585 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700586 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800587 }
588 }
589
590 @Override
591 public void removePermission(String name) {
592 try {
593 mPM.removePermission(name);
594 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700595 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800596 }
597 }
598
599 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700600 public void grantRuntimePermission(String packageName, String permissionName,
601 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800602 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700603 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800604 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700605 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800606 }
607 }
608
609 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700610 public void revokeRuntimePermission(String packageName, String permissionName,
611 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800612 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700613 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
614 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700615 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700616 }
617 }
618
619 @Override
620 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
621 try {
622 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
623 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700624 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700625 }
626 }
627
628 @Override
629 public void updatePermissionFlags(String permissionName, String packageName,
630 int flagMask, int flagValues, UserHandle user) {
631 try {
632 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
633 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800634 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700635 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800636 }
637 }
638
639 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700640 public boolean shouldShowRequestPermissionRationale(String permission) {
641 try {
642 return mPM.shouldShowRequestPermissionRationale(permission,
643 mContext.getPackageName(), mContext.getUserId());
644 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700645 throw e.rethrowFromSystemServer();
Svetoslav20770dd2015-05-29 15:43:04 -0700646 }
647 }
648
649 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800650 public int checkSignatures(String pkg1, String pkg2) {
651 try {
652 return mPM.checkSignatures(pkg1, pkg2);
653 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700654 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800655 }
656 }
657
658 @Override
659 public int checkSignatures(int uid1, int uid2) {
660 try {
661 return mPM.checkUidSignatures(uid1, uid2);
662 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700663 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800664 }
665 }
666
667 @Override
668 public String[] getPackagesForUid(int uid) {
669 try {
670 return mPM.getPackagesForUid(uid);
671 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700672 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800673 }
674 }
675
676 @Override
677 public String getNameForUid(int uid) {
678 try {
679 return mPM.getNameForUid(uid);
680 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700681 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800682 }
683 }
684
685 @Override
686 public int getUidForSharedUser(String sharedUserName)
687 throws NameNotFoundException {
688 try {
689 int uid = mPM.getUidForSharedUser(sharedUserName);
690 if(uid != -1) {
691 return uid;
692 }
693 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700694 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800695 }
696 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
697 }
698
Kenny Roote6cd0c72011-05-19 12:48:14 -0700699 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800700 @Override
701 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700702 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700703 }
704
705 /** @hide */
706 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700707 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700708 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800709 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700710 ParceledListSlice<PackageInfo> parceledList =
711 mPM.getInstalledPackages(flags, userId);
712 if (parceledList == null) {
713 return Collections.emptyList();
714 }
715 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800716 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700717 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800718 }
719 }
720
Kenny Roote6cd0c72011-05-19 12:48:14 -0700721 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800722 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800723 public List<PackageInfo> getPackagesHoldingPermissions(
724 String[] permissions, int flags) {
725 final int userId = mContext.getUserId();
726 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700727 ParceledListSlice<PackageInfo> parceledList =
728 mPM.getPackagesHoldingPermissions(permissions, flags, userId);
729 if (parceledList == null) {
730 return Collections.emptyList();
731 }
732 return parceledList.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800733 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700734 throw e.rethrowFromSystemServer();
Dianne Hackborne7991752013-01-16 17:56:46 -0800735 }
736 }
737
738 @SuppressWarnings("unchecked")
739 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800740 public List<ApplicationInfo> getInstalledApplications(int flags) {
Bartosz Fabianowski11334242016-11-17 20:49:16 +0100741 return getInstalledApplicationsAsUser(flags, mContext.getUserId());
742 }
743
744 /** @hide */
745 @SuppressWarnings("unchecked")
746 @Override
747 public List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800748 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700749 ParceledListSlice<ApplicationInfo> parceledList =
750 mPM.getInstalledApplications(flags, userId);
751 if (parceledList == null) {
752 return Collections.emptyList();
753 }
754 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800755 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700756 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800757 }
758 }
759
Svet Ganov2acf0632015-11-24 19:10:59 -0800760 /** @hide */
761 @SuppressWarnings("unchecked")
762 @Override
763 public List<EphemeralApplicationInfo> getEphemeralApplications() {
764 try {
765 ParceledListSlice<EphemeralApplicationInfo> slice =
766 mPM.getEphemeralApplications(mContext.getUserId());
767 if (slice != null) {
768 return slice.getList();
769 }
770 return Collections.emptyList();
771 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700772 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800773 }
774 }
775
776 /** @hide */
777 @Override
778 public Drawable getEphemeralApplicationIcon(String packageName) {
779 try {
780 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
781 packageName, mContext.getUserId());
782 if (bitmap != null) {
783 return new BitmapDrawable(null, bitmap);
784 }
785 return null;
786 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700787 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800788 }
789 }
790
791 @Override
792 public boolean isEphemeralApplication() {
793 try {
794 return mPM.isEphemeralApplication(
795 mContext.getPackageName(), mContext.getUserId());
796 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700797 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800798 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800799 }
800
801 @Override
802 public int getEphemeralCookieMaxSizeBytes() {
803 return Settings.Global.getInt(mContext.getContentResolver(),
804 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
805 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
806 }
807
808 @Override
809 public @NonNull byte[] getEphemeralCookie() {
810 try {
811 final byte[] cookie = mPM.getEphemeralApplicationCookie(
812 mContext.getPackageName(), mContext.getUserId());
813 if (cookie != null) {
814 return cookie;
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700815 } else {
816 return EmptyArray.BYTE;
Svet Ganov2acf0632015-11-24 19:10:59 -0800817 }
818 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700819 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800820 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800821 }
822
823 @Override
824 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
825 try {
826 return mPM.setEphemeralApplicationCookie(
827 mContext.getPackageName(), cookie, mContext.getUserId());
828 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700829 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800830 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800831 }
832
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800833 @Override
834 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700835 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700836 }
837
838 @Override
839 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800840 try {
841 return mPM.resolveIntent(
842 intent,
843 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700844 flags,
845 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800846 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700847 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800848 }
849 }
850
851 @Override
852 public List<ResolveInfo> queryIntentActivities(Intent intent,
853 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700854 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700855 }
856
857 /** @hide Same as above but for a specific user */
858 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700859 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700860 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700861 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800862 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700863 ParceledListSlice<ResolveInfo> parceledList =
864 mPM.queryIntentActivities(intent,
865 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
866 flags, userId);
867 if (parceledList == null) {
868 return Collections.emptyList();
869 }
870 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800871 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700872 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800873 }
874 }
875
876 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700877 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800878 public List<ResolveInfo> queryIntentActivityOptions(
879 ComponentName caller, Intent[] specifics, Intent intent,
880 int flags) {
881 final ContentResolver resolver = mContext.getContentResolver();
882
883 String[] specificTypes = null;
884 if (specifics != null) {
885 final int N = specifics.length;
886 for (int i=0; i<N; i++) {
887 Intent sp = specifics[i];
888 if (sp != null) {
889 String t = sp.resolveTypeIfNeeded(resolver);
890 if (t != null) {
891 if (specificTypes == null) {
892 specificTypes = new String[N];
893 }
894 specificTypes[i] = t;
895 }
896 }
897 }
898 }
899
900 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700901 ParceledListSlice<ResolveInfo> parceledList =
902 mPM.queryIntentActivityOptions(caller, specifics, specificTypes, intent,
903 intent.resolveTypeIfNeeded(resolver), flags, mContext.getUserId());
904 if (parceledList == null) {
905 return Collections.emptyList();
906 }
907 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800908 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700909 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800910 }
911 }
912
Amith Yamasanif203aee2012-08-29 18:41:53 -0700913 /**
914 * @hide
915 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800916 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700917 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700918 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800919 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700920 ParceledListSlice<ResolveInfo> parceledList =
921 mPM.queryIntentReceivers(intent,
922 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
923 flags, userId);
924 if (parceledList == null) {
925 return Collections.emptyList();
926 }
927 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800928 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700929 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800930 }
931 }
932
933 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700934 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700935 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700936 }
937
938 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800939 public ResolveInfo resolveService(Intent intent, int flags) {
940 try {
941 return mPM.resolveService(
942 intent,
943 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700944 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700945 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800946 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700947 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800948 }
949 }
950
951 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700952 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700953 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800954 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700955 ParceledListSlice<ResolveInfo> parceledList =
956 mPM.queryIntentServices(intent,
957 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
958 flags, userId);
959 if (parceledList == null) {
960 return Collections.emptyList();
961 }
962 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800963 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700964 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800965 }
966 }
967
968 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700969 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700970 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700971 }
972
973 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700974 @SuppressWarnings("unchecked")
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700975 public List<ResolveInfo> queryIntentContentProvidersAsUser(
976 Intent intent, int flags, int userId) {
977 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700978 ParceledListSlice<ResolveInfo> parceledList =
979 mPM.queryIntentContentProviders(intent,
980 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
981 flags, userId);
982 if (parceledList == null) {
983 return Collections.emptyList();
984 }
985 return parceledList.getList();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700986 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700987 throw e.rethrowFromSystemServer();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700988 }
989 }
990
991 @Override
992 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
993 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
994 }
995
996 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100997 public ProviderInfo resolveContentProvider(String name, int flags) {
998 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
999 }
1000
1001 /** @hide **/
1002 @Override
1003 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001004 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01001005 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001006 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001007 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001008 }
1009 }
1010
1011 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001012 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001013 public List<ProviderInfo> queryContentProviders(String processName,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001014 int uid, int flags) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001015 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001016 ParceledListSlice<ProviderInfo> slice =
1017 mPM.queryContentProviders(processName, uid, flags);
1018 return slice != null ? slice.getList() : Collections.<ProviderInfo>emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001019 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001020 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001021 }
1022 }
1023
1024 @Override
1025 public InstrumentationInfo getInstrumentationInfo(
1026 ComponentName className, int flags)
1027 throws NameNotFoundException {
1028 try {
1029 InstrumentationInfo ii = mPM.getInstrumentationInfo(
1030 className, flags);
1031 if (ii != null) {
1032 return ii;
1033 }
1034 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001035 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001036 }
1037
1038 throw new NameNotFoundException(className.toString());
1039 }
1040
1041 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001042 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001043 public List<InstrumentationInfo> queryInstrumentation(
1044 String targetPackage, int flags) {
1045 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001046 ParceledListSlice<InstrumentationInfo> parceledList =
1047 mPM.queryInstrumentation(targetPackage, flags);
1048 if (parceledList == null) {
1049 return Collections.emptyList();
1050 }
1051 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001052 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001053 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001054 }
1055 }
1056
Alan Viveretteecd585a2015-04-13 10:32:51 -07001057 @Nullable
1058 @Override
1059 public Drawable getDrawable(String packageName, @DrawableRes int resId,
1060 @Nullable ApplicationInfo appInfo) {
1061 final ResourceName name = new ResourceName(packageName, resId);
1062 final Drawable cachedIcon = getCachedIcon(name);
1063 if (cachedIcon != null) {
1064 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001065 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001066
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001067 if (appInfo == null) {
1068 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001069 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001070 } catch (NameNotFoundException e) {
1071 return null;
1072 }
1073 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001074
1075 if (resId != 0) {
1076 try {
1077 final Resources r = getResourcesForApplication(appInfo);
1078 final Drawable dr = r.getDrawable(resId, null);
1079 if (dr != null) {
1080 putCachedIcon(name, dr);
1081 }
1082
1083 if (false) {
1084 RuntimeException e = new RuntimeException("here");
1085 e.fillInStackTrace();
1086 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
1087 + " from package " + packageName
1088 + ": app scale=" + r.getCompatibilityInfo().applicationScale
1089 + ", caller scale=" + mContext.getResources()
1090 .getCompatibilityInfo().applicationScale,
1091 e);
1092 }
Ricky Wai3ce46252015-04-15 16:12:22 +01001093 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001094 Log.v(TAG, "Getting drawable 0x"
1095 + Integer.toHexString(resId) + " from " + r
1096 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +01001097 }
1098 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -07001099 } catch (NameNotFoundException e) {
1100 Log.w("PackageManager", "Failure retrieving resources for "
1101 + appInfo.packageName);
1102 } catch (Resources.NotFoundException e) {
1103 Log.w("PackageManager", "Failure retrieving resources for "
1104 + appInfo.packageName + ": " + e.getMessage());
1105 } catch (Exception e) {
1106 // If an exception was thrown, fall through to return
1107 // default icon.
1108 Log.w("PackageManager", "Failure retrieving icon 0x"
1109 + Integer.toHexString(resId) + " in package "
1110 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001111 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001112 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001113
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001114 return null;
1115 }
1116
1117 @Override public Drawable getActivityIcon(ComponentName activityName)
1118 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001119 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001120 }
1121
1122 @Override public Drawable getActivityIcon(Intent intent)
1123 throws NameNotFoundException {
1124 if (intent.getComponent() != null) {
1125 return getActivityIcon(intent.getComponent());
1126 }
1127
1128 ResolveInfo info = resolveActivity(
1129 intent, PackageManager.MATCH_DEFAULT_ONLY);
1130 if (info != null) {
1131 return info.activityInfo.loadIcon(this);
1132 }
1133
Romain Guy39fe17c2011-11-30 10:34:07 -08001134 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001135 }
1136
1137 @Override public Drawable getDefaultActivityIcon() {
1138 return Resources.getSystem().getDrawable(
1139 com.android.internal.R.drawable.sym_def_app_icon);
1140 }
1141
1142 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1143 return info.loadIcon(this);
1144 }
1145
1146 @Override public Drawable getApplicationIcon(String packageName)
1147 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001148 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001149 }
1150
1151 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001152 public Drawable getActivityBanner(ComponentName activityName)
1153 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001154 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001155 }
1156
1157 @Override
1158 public Drawable getActivityBanner(Intent intent)
1159 throws NameNotFoundException {
1160 if (intent.getComponent() != null) {
1161 return getActivityBanner(intent.getComponent());
1162 }
1163
1164 ResolveInfo info = resolveActivity(
1165 intent, PackageManager.MATCH_DEFAULT_ONLY);
1166 if (info != null) {
1167 return info.activityInfo.loadBanner(this);
1168 }
1169
1170 throw new NameNotFoundException(intent.toUri(0));
1171 }
1172
1173 @Override
1174 public Drawable getApplicationBanner(ApplicationInfo info) {
1175 return info.loadBanner(this);
1176 }
1177
1178 @Override
1179 public Drawable getApplicationBanner(String packageName)
1180 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001181 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001182 }
1183
1184 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001185 public Drawable getActivityLogo(ComponentName activityName)
1186 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001187 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001188 }
1189
1190 @Override
1191 public Drawable getActivityLogo(Intent intent)
1192 throws NameNotFoundException {
1193 if (intent.getComponent() != null) {
1194 return getActivityLogo(intent.getComponent());
1195 }
1196
1197 ResolveInfo info = resolveActivity(
1198 intent, PackageManager.MATCH_DEFAULT_ONLY);
1199 if (info != null) {
1200 return info.activityInfo.loadLogo(this);
1201 }
1202
1203 throw new NameNotFoundException(intent.toUri(0));
1204 }
1205
1206 @Override
1207 public Drawable getApplicationLogo(ApplicationInfo info) {
1208 return info.loadLogo(this);
1209 }
1210
1211 @Override
1212 public Drawable getApplicationLogo(String packageName)
1213 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001214 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001215 }
1216
Svetoslavc7d62f02014-09-04 15:39:54 -07001217 @Override
1218 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
Kenny Guy02c89902016-11-15 19:36:38 +00001219 if (!isManagedProfile(user.getIdentifier())) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001220 return icon;
1221 }
Kenny Guy02c89902016-11-15 19:36:38 +00001222 Drawable badgeShadow = getDrawable("system",
1223 com.android.internal.R.drawable.ic_corp_icon_badge_shadow, null);
1224 Drawable badgeColor = getDrawable("system",
1225 com.android.internal.R.drawable.ic_corp_icon_badge_color, null);
1226 badgeColor.setTint(getUserBadgeColor(user));
1227 Drawable badgeForeground = getDrawable("system",
1228 com.android.internal.R.drawable.ic_corp_icon_badge_case, null);
1229
1230 Drawable badge = new LayerDrawable(
1231 new Drawable[] {badgeShadow, badgeColor, badgeForeground });
1232 return getBadgedDrawable(icon, badge, null, true);
Svetoslavc7d62f02014-09-04 15:39:54 -07001233 }
1234
1235 @Override
1236 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1237 Rect badgeLocation, int badgeDensity) {
1238 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1239 if (badgeDrawable == null) {
1240 return drawable;
1241 }
1242 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1243 }
1244
Kenny Guy02c89902016-11-15 19:36:38 +00001245 // Should have enough colors to cope with UserManagerService.getMaxManagedProfiles()
1246 @VisibleForTesting
1247 public static final int[] CORP_BADGE_COLORS = new int[] {
1248 com.android.internal.R.color.profile_badge_1,
1249 com.android.internal.R.color.profile_badge_2,
1250 com.android.internal.R.color.profile_badge_3
1251 };
1252
1253 @VisibleForTesting
1254 public static final int[] CORP_BADGE_LABEL_RES_ID = new int[] {
1255 com.android.internal.R.string.managed_profile_label_badge,
1256 com.android.internal.R.string.managed_profile_label_badge_2,
1257 com.android.internal.R.string.managed_profile_label_badge_3
1258 };
1259
1260 private int getUserBadgeColor(UserHandle user) {
1261 int badge = getUserManager().getManagedProfileBadge(user.getIdentifier());
1262 if (badge < 0) {
1263 badge = 0;
1264 }
1265 int resourceId = CORP_BADGE_COLORS[badge % CORP_BADGE_COLORS.length];
1266 return Resources.getSystem().getColor(resourceId, null);
1267 }
1268
Svetoslavc7d62f02014-09-04 15:39:54 -07001269 @Override
1270 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Kenny Guy02c89902016-11-15 19:36:38 +00001271 Drawable badgeColor = getManagedProfileIconForDensity(user,
1272 com.android.internal.R.drawable.ic_corp_badge_color, density);
1273 if (badgeColor == null) {
1274 return null;
1275 }
1276 badgeColor.setTint(getUserBadgeColor(user));
1277 Drawable badgeForeground = getDrawableForDensity(
1278 com.android.internal.R.drawable.ic_corp_badge_case, density);
1279 Drawable badge = new LayerDrawable(
1280 new Drawable[] {badgeColor, badgeForeground });
1281 return badge;
Selim Cineke6ff9462016-01-15 15:07:06 -08001282 }
1283
1284 @Override
1285 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
Kenny Guy02c89902016-11-15 19:36:38 +00001286 Drawable badge = getManagedProfileIconForDensity(user,
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001287 com.android.internal.R.drawable.ic_corp_badge_no_background, density);
Kenny Guy02c89902016-11-15 19:36:38 +00001288 if (badge != null) {
1289 badge.setTint(getUserBadgeColor(user));
1290 }
1291 return badge;
Selim Cineke6ff9462016-01-15 15:07:06 -08001292 }
1293
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001294 private Drawable getDrawableForDensity(int drawableId, int density) {
1295 if (density <= 0) {
1296 density = mContext.getResources().getDisplayMetrics().densityDpi;
1297 }
1298 return Resources.getSystem().getDrawableForDensity(drawableId, density);
1299 }
1300
1301 private Drawable getManagedProfileIconForDensity(UserHandle user, int drawableId, int density) {
Tony Mak8673b282016-03-21 21:10:59 +00001302 if (isManagedProfile(user.getIdentifier())) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001303 return getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001304 }
1305 return null;
1306 }
1307
1308 @Override
1309 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
Tony Mak8673b282016-03-21 21:10:59 +00001310 if (isManagedProfile(user.getIdentifier())) {
Kenny Guy02c89902016-11-15 19:36:38 +00001311 int badge = getUserManager().getManagedProfileBadge(user.getIdentifier());
1312 int resourceId = CORP_BADGE_LABEL_RES_ID[badge % CORP_BADGE_LABEL_RES_ID.length];
1313 return Resources.getSystem().getString(resourceId, label);
Svetoslavc7d62f02014-09-04 15:39:54 -07001314 }
1315 return label;
1316 }
1317
Alan Viveretteecd585a2015-04-13 10:32:51 -07001318 @Override
1319 public Resources getResourcesForActivity(ComponentName activityName)
1320 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001321 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001322 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001323 }
1324
Alan Viveretteecd585a2015-04-13 10:32:51 -07001325 @Override
1326 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1327 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001328 if (app.packageName.equals("system")) {
1329 return mContext.mMainThread.getSystemContext().getResources();
1330 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001331 final boolean sameUid = (app.uid == Process.myUid());
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001332 final Resources r = mContext.mMainThread.getTopLevelResources(
Adam Lesinskic82f28a2016-06-08 17:19:09 -07001333 sameUid ? app.sourceDir : app.publicSourceDir,
1334 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
1335 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1336 mContext.mPackageInfo);
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001337 if (r != null) {
1338 return r;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001339 }
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001340 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1341
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001342 }
1343
Alan Viveretteecd585a2015-04-13 10:32:51 -07001344 @Override
1345 public Resources getResourcesForApplication(String appPackageName)
1346 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001347 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001348 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001349 }
1350
Amith Yamasani98edc952012-09-25 14:09:27 -07001351 /** @hide */
1352 @Override
1353 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1354 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001355 if (userId < 0) {
1356 throw new IllegalArgumentException(
1357 "Call does not support special user #" + userId);
1358 }
1359 if ("system".equals(appPackageName)) {
1360 return mContext.mMainThread.getSystemContext().getResources();
1361 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001362 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001363 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001364 if (ai != null) {
1365 return getResourcesForApplication(ai);
1366 }
1367 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001368 throw e.rethrowFromSystemServer();
Amith Yamasani98edc952012-09-25 14:09:27 -07001369 }
1370 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1371 }
1372
Jeff Sharkeycd654482016-01-08 17:42:11 -07001373 volatile int mCachedSafeMode = -1;
1374
1375 @Override
1376 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001377 try {
1378 if (mCachedSafeMode < 0) {
1379 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1380 }
1381 return mCachedSafeMode != 0;
1382 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001383 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001384 }
1385 }
1386
Svetoslavf7c06eb2015-06-10 18:43:22 -07001387 @Override
1388 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1389 synchronized (mPermissionListeners) {
1390 if (mPermissionListeners.get(listener) != null) {
1391 return;
1392 }
1393 OnPermissionsChangeListenerDelegate delegate =
1394 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1395 try {
1396 mPM.addOnPermissionsChangeListener(delegate);
1397 mPermissionListeners.put(listener, delegate);
1398 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001399 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001400 }
1401 }
1402 }
1403
1404 @Override
1405 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1406 synchronized (mPermissionListeners) {
1407 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1408 if (delegate != null) {
1409 try {
1410 mPM.removeOnPermissionsChangeListener(delegate);
1411 mPermissionListeners.remove(listener);
1412 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001413 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001414 }
1415 }
1416 }
1417 }
1418
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001419 static void configurationChanged() {
1420 synchronized (sSync) {
1421 sIconCache.clear();
1422 sStringCache.clear();
1423 }
1424 }
1425
Yao Chen022b8ea2016-12-16 11:03:28 -08001426 protected ApplicationPackageManager(ContextImpl context,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001427 IPackageManager pm) {
1428 mContext = context;
1429 mPM = pm;
1430 }
1431
Alan Viveretteecd585a2015-04-13 10:32:51 -07001432 @Nullable
1433 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001434 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001435 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001436 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1437 + name + ": " + wr);
1438 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001439 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001440 if (state != null) {
1441 if (DEBUG_ICONS) {
1442 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1443 }
1444 // Note: It's okay here to not use the newDrawable(Resources) variant
1445 // of the API. The ConstantState comes from a drawable that was
1446 // originally created by passing the proper app Resources instance
1447 // which means the state should already contain the proper
1448 // resources specific information (like density.) See
1449 // BitmapDrawable.BitmapState for instance.
1450 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001451 }
1452 // our entry has been purged
1453 sIconCache.remove(name);
1454 }
1455 }
1456 return null;
1457 }
1458
Alan Viveretteecd585a2015-04-13 10:32:51 -07001459 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001460 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001461 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001462 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001463 }
1464 }
1465
Romain Guy39fe17c2011-11-30 10:34:07 -08001466 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001467 boolean immediateGc = false;
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001468 if (cmd == ApplicationThreadConstants.EXTERNAL_STORAGE_UNAVAILABLE) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001469 immediateGc = true;
1470 }
1471 if (pkgList != null && (pkgList.length > 0)) {
1472 boolean needCleanup = false;
1473 for (String ssp : pkgList) {
1474 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001475 for (int i=sIconCache.size()-1; i>=0; i--) {
1476 ResourceName nm = sIconCache.keyAt(i);
1477 if (nm.packageName.equals(ssp)) {
1478 //Log.i(TAG, "Removing cached drawable for " + nm);
1479 sIconCache.removeAt(i);
1480 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001481 }
1482 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001483 for (int i=sStringCache.size()-1; i>=0; i--) {
1484 ResourceName nm = sStringCache.keyAt(i);
1485 if (nm.packageName.equals(ssp)) {
1486 //Log.i(TAG, "Removing cached string for " + nm);
1487 sStringCache.removeAt(i);
1488 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001489 }
1490 }
1491 }
1492 }
1493 if (needCleanup || hasPkgInfo) {
1494 if (immediateGc) {
1495 // Schedule an immediate gc.
1496 Runtime.getRuntime().gc();
1497 } else {
1498 ActivityThread.currentActivityThread().scheduleGcIdler();
1499 }
1500 }
1501 }
1502 }
1503
1504 private static final class ResourceName {
1505 final String packageName;
1506 final int iconId;
1507
1508 ResourceName(String _packageName, int _iconId) {
1509 packageName = _packageName;
1510 iconId = _iconId;
1511 }
1512
1513 ResourceName(ApplicationInfo aInfo, int _iconId) {
1514 this(aInfo.packageName, _iconId);
1515 }
1516
1517 ResourceName(ComponentInfo cInfo, int _iconId) {
1518 this(cInfo.applicationInfo.packageName, _iconId);
1519 }
1520
1521 ResourceName(ResolveInfo rInfo, int _iconId) {
1522 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1523 }
1524
1525 @Override
1526 public boolean equals(Object o) {
1527 if (this == o) return true;
1528 if (o == null || getClass() != o.getClass()) return false;
1529
1530 ResourceName that = (ResourceName) o;
1531
1532 if (iconId != that.iconId) return false;
1533 return !(packageName != null ?
1534 !packageName.equals(that.packageName) : that.packageName != null);
1535
1536 }
1537
1538 @Override
1539 public int hashCode() {
1540 int result;
1541 result = packageName.hashCode();
1542 result = 31 * result + iconId;
1543 return result;
1544 }
1545
1546 @Override
1547 public String toString() {
1548 return "{ResourceName " + packageName + " / " + iconId + "}";
1549 }
1550 }
1551
1552 private CharSequence getCachedString(ResourceName name) {
1553 synchronized (sSync) {
1554 WeakReference<CharSequence> wr = sStringCache.get(name);
1555 if (wr != null) { // we have the activity
1556 CharSequence cs = wr.get();
1557 if (cs != null) {
1558 return cs;
1559 }
1560 // our entry has been purged
1561 sStringCache.remove(name);
1562 }
1563 }
1564 return null;
1565 }
1566
1567 private void putCachedString(ResourceName name, CharSequence cs) {
1568 synchronized (sSync) {
1569 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1570 }
1571 }
1572
1573 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001574 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001575 ApplicationInfo appInfo) {
1576 ResourceName name = new ResourceName(packageName, resid);
1577 CharSequence text = getCachedString(name);
1578 if (text != null) {
1579 return text;
1580 }
1581 if (appInfo == null) {
1582 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001583 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001584 } catch (NameNotFoundException e) {
1585 return null;
1586 }
1587 }
1588 try {
1589 Resources r = getResourcesForApplication(appInfo);
1590 text = r.getText(resid);
1591 putCachedString(name, text);
1592 return text;
1593 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001594 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001595 + appInfo.packageName);
1596 } catch (RuntimeException e) {
1597 // If an exception was thrown, fall through to return
1598 // default icon.
1599 Log.w("PackageManager", "Failure retrieving text 0x"
1600 + Integer.toHexString(resid) + " in package "
1601 + packageName, e);
1602 }
1603 return null;
1604 }
1605
1606 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001607 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001608 ApplicationInfo appInfo) {
1609 if (appInfo == null) {
1610 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001611 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001612 } catch (NameNotFoundException e) {
1613 return null;
1614 }
1615 }
1616 try {
1617 Resources r = getResourcesForApplication(appInfo);
1618 return r.getXml(resid);
1619 } catch (RuntimeException e) {
1620 // If an exception was thrown, fall through to return
1621 // default icon.
1622 Log.w("PackageManager", "Failure retrieving xml 0x"
1623 + Integer.toHexString(resid) + " in package "
1624 + packageName, e);
1625 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001626 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001627 + appInfo.packageName);
1628 }
1629 return null;
1630 }
1631
1632 @Override
1633 public CharSequence getApplicationLabel(ApplicationInfo info) {
1634 return info.loadLabel(this);
1635 }
1636
1637 @Override
1638 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1639 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001640 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Todd Kennedya6793232016-02-24 22:46:00 +00001641 installerPackageName, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001642 }
1643
Christopher Tatef1977b42014-03-24 16:25:51 -07001644 @Override
1645 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1646 int flags, String installerPackageName) {
Todd Kennedya6793232016-02-24 22:46:00 +00001647 installCommon(packageURI, observer, flags, installerPackageName, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001648 }
1649
1650 private void installCommon(Uri packageURI,
1651 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001652 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001653 if (!"file".equals(packageURI.getScheme())) {
1654 throw new UnsupportedOperationException("Only file:// URIs are supported");
1655 }
Jeff Sharkey513a0742014-07-08 17:10:32 -07001656
1657 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001658 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001659 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
Todd Kennedya6793232016-02-24 22:46:00 +00001660 userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001661 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001662 throw e.rethrowFromSystemServer();
rich cannings706e8ba2012-08-20 13:20:14 -07001663 }
1664 }
1665
1666 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001667 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001668 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001669 }
1670
1671 @Override
1672 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001673 throws NameNotFoundException {
1674 try {
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +01001675 int res = mPM.installExistingPackageAsUser(packageName, userId,
1676 PackageManager.INSTALL_REASON_UNKNOWN);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001677 if (res == INSTALL_FAILED_INVALID_URI) {
1678 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1679 }
1680 return res;
1681 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001682 throw e.rethrowFromSystemServer();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001683 }
1684 }
1685
1686 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001687 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001688 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001689 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001690 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001691 throw e.rethrowFromSystemServer();
Kenny Root5ab21572011-07-27 11:11:19 -07001692 }
1693 }
1694
1695 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001696 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1697 long millisecondsToDelay) {
1698 try {
1699 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1700 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001701 throw e.rethrowFromSystemServer();
rich canningsd9ef3e52012-08-22 14:28:05 -07001702 }
1703 }
1704
1705 @Override
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001706 public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001707 try {
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001708 mPM.verifyIntentFilter(id, verificationCode, failedDomains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001709 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001710 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001711 }
1712 }
1713
1714 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001715 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001716 try {
1717 return mPM.getIntentVerificationStatus(packageName, userId);
1718 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001719 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001720 }
1721 }
1722
1723 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001724 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001725 try {
1726 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1727 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001728 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001729 }
1730 }
1731
1732 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001733 @SuppressWarnings("unchecked")
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001734 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1735 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001736 ParceledListSlice<IntentFilterVerificationInfo> parceledList =
1737 mPM.getIntentFilterVerifications(packageName);
1738 if (parceledList == null) {
1739 return Collections.emptyList();
1740 }
1741 return parceledList.getList();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001742 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001743 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001744 }
1745 }
1746
1747 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001748 @SuppressWarnings("unchecked")
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001749 public List<IntentFilter> getAllIntentFilters(String packageName) {
1750 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001751 ParceledListSlice<IntentFilter> parceledList =
1752 mPM.getAllIntentFilters(packageName);
1753 if (parceledList == null) {
1754 return Collections.emptyList();
1755 }
1756 return parceledList.getList();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001757 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001758 throw e.rethrowFromSystemServer();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001759 }
1760 }
1761
1762 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001763 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001764 try {
1765 return mPM.getDefaultBrowserPackageName(userId);
1766 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001767 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001768 }
1769 }
1770
1771 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001772 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001773 try {
1774 return mPM.setDefaultBrowserPackageName(packageName, userId);
1775 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001776 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001777 }
1778 }
1779
1780 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001781 public void setInstallerPackageName(String targetPackage,
1782 String installerPackageName) {
1783 try {
1784 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1785 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001786 throw e.rethrowFromSystemServer();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001787 }
1788 }
1789
1790 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001791 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001792 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001793 return mPM.getInstallerPackageName(packageName);
1794 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001795 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001796 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001797 }
1798
1799 @Override
1800 public int getMoveStatus(int moveId) {
1801 try {
1802 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001803 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001804 throw e.rethrowFromSystemServer();
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001805 }
1806 }
1807
1808 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001809 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1810 synchronized (mDelegates) {
1811 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1812 handler.getLooper());
1813 try {
1814 mPM.registerMoveCallback(delegate);
1815 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001816 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001817 }
1818 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001819 }
1820 }
1821
1822 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001823 public void unregisterMoveCallback(MoveCallback callback) {
1824 synchronized (mDelegates) {
1825 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1826 final MoveCallbackDelegate delegate = i.next();
1827 if (delegate.mCallback == callback) {
1828 try {
1829 mPM.unregisterMoveCallback(delegate);
1830 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001831 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001832 }
1833 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001834 }
1835 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001836 }
1837 }
1838
1839 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001840 public int movePackage(String packageName, VolumeInfo vol) {
1841 try {
1842 final String volumeUuid;
1843 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1844 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1845 } else if (vol.isPrimaryPhysical()) {
1846 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1847 } else {
1848 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1849 }
1850
1851 return mPM.movePackage(packageName, volumeUuid);
1852 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001853 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001854 }
1855 }
1856
1857 @Override
1858 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001859 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Yao Chen022b8ea2016-12-16 11:03:28 -08001860 return getPackageCurrentVolume(app, storage);
1861 }
1862
1863 @VisibleForTesting
1864 protected @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app,
1865 StorageManager storage) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001866 if (app.isInternal()) {
1867 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1868 } else if (app.isExternalAsec()) {
1869 return storage.getPrimaryPhysicalVolume();
1870 } else {
1871 return storage.findVolumeByUuid(app.volumeUuid);
1872 }
1873 }
1874
1875 @Override
1876 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
Yao Chen022b8ea2016-12-16 11:03:28 -08001877 final StorageManager storageManager = mContext.getSystemService(StorageManager.class);
1878 return getPackageCandidateVolumes(app, storageManager, mPM);
1879 }
1880
1881 @VisibleForTesting
1882 protected @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app,
1883 StorageManager storageManager, IPackageManager pm) {
1884 final VolumeInfo currentVol = getPackageCurrentVolume(app, storageManager);
1885 final List<VolumeInfo> vols = storageManager.getVolumes();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001886 final List<VolumeInfo> candidates = new ArrayList<>();
1887 for (VolumeInfo vol : vols) {
Yao Chen022b8ea2016-12-16 11:03:28 -08001888 if (Objects.equals(vol, currentVol)
1889 || isPackageCandidateVolume(mContext, app, vol, pm)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001890 candidates.add(vol);
1891 }
1892 }
1893 return candidates;
1894 }
1895
Yao Chen022b8ea2016-12-16 11:03:28 -08001896 @VisibleForTesting
1897 protected boolean isForceAllowOnExternal(Context context) {
1898 return Settings.Global.getInt(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001899 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Yao Chen022b8ea2016-12-16 11:03:28 -08001900 }
1901
1902 @VisibleForTesting
1903 protected boolean isAllow3rdPartyOnInternal(Context context) {
1904 return context.getResources().getBoolean(
1905 com.android.internal.R.bool.config_allow3rdPartyAppOnInternal);
1906 }
1907
1908 private boolean isPackageCandidateVolume(
1909 ContextImpl context, ApplicationInfo app, VolumeInfo vol, IPackageManager pm) {
1910 final boolean forceAllowOnExternal = isForceAllowOnExternal(context);
1911
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001912 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
Yao Chen022b8ea2016-12-16 11:03:28 -08001913 return app.isSystemApp() || isAllow3rdPartyOnInternal(context);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001914 }
1915
1916 // System apps and apps demanding internal storage can't be moved
1917 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001918 if (app.isSystemApp()) {
1919 return false;
1920 }
1921 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001922 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1923 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001924 return false;
1925 }
1926
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001927 // Gotta be able to write there
1928 if (!vol.isMountedWritable()) {
1929 return false;
1930 }
1931
1932 // Moving into an ASEC on public primary is only option internal
1933 if (vol.isPrimaryPhysical()) {
1934 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001935 }
1936
Makoto Onukif34db0a2016-02-17 11:17:15 -08001937 // Some apps can't be moved. (e.g. device admins)
1938 try {
Yao Chen022b8ea2016-12-16 11:03:28 -08001939 if (pm.isPackageDeviceAdminOnAnyUser(app.packageName)) {
Makoto Onukif34db0a2016-02-17 11:17:15 -08001940 return false;
1941 }
1942 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001943 throw e.rethrowFromSystemServer();
Makoto Onukif34db0a2016-02-17 11:17:15 -08001944 }
1945
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001946 // Otherwise we can move to any private volume
1947 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1948 }
1949
1950 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001951 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001952 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001953 final String volumeUuid;
1954 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1955 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1956 } else if (vol.isPrimaryPhysical()) {
1957 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1958 } else {
1959 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1960 }
1961
1962 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001963 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001964 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001965 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001966 }
1967
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001968 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001969 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1970 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1971 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001972 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001973 }
1974
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001975 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001976 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1977 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1978 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1979 final List<VolumeInfo> vols = storage.getVolumes();
1980 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001981 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1982 storage.getPrimaryStorageUuid()) && currentVol != null) {
1983 // TODO: support moving primary physical to emulated volume
1984 candidates.add(currentVol);
1985 } else {
1986 for (VolumeInfo vol : vols) {
1987 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1988 candidates.add(vol);
1989 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001990 }
1991 }
1992 return candidates;
1993 }
1994
1995 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1996 // Private internal is always an option
1997 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1998 return true;
1999 }
2000
2001 // Gotta be able to write there
2002 if (!vol.isMountedWritable()) {
2003 return false;
2004 }
2005
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002006 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002007 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002008 }
2009
2010 @Override
2011 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01002012 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01002013 }
2014
2015 @Override
Svet Ganov67882122016-12-11 16:36:34 -08002016 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer,
2017 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002018 try {
Svet Ganov67882122016-12-11 16:36:34 -08002019 mPM.deletePackageAsUser(packageName, PackageManager.VERSION_CODE_HIGHEST,
2020 observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002021 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002022 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002023 }
2024 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07002025
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002026 @Override
2027 public void clearApplicationUserData(String packageName,
2028 IPackageDataObserver observer) {
2029 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002030 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002031 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002032 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002033 }
2034 }
2035 @Override
2036 public void deleteApplicationCacheFiles(String packageName,
2037 IPackageDataObserver observer) {
2038 try {
2039 mPM.deleteApplicationCacheFiles(packageName, observer);
2040 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002041 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002042 }
2043 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002044
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002045 @Override
Suprabh Shukla78c9eb82016-04-12 15:51:35 -07002046 public void deleteApplicationCacheFilesAsUser(String packageName, int userId,
2047 IPackageDataObserver observer) {
2048 try {
2049 mPM.deleteApplicationCacheFilesAsUser(packageName, userId, observer);
2050 } catch (RemoteException e) {
2051 throw e.rethrowFromSystemServer();
2052 }
2053 }
2054
2055 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002056 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
2057 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002058 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002059 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002060 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002061 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002062 }
2063 }
2064
2065 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002066 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002067 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002068 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002069 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002070 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002071 }
2072 }
2073
2074 @Override
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00002075 public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended,
2076 int userId) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002077 try {
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00002078 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, userId);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002079 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002080 throw e.rethrowFromSystemServer();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002081 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002082 }
2083
2084 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002085 public boolean isPackageSuspendedForUser(String packageName, int userId) {
2086 try {
2087 return mPM.isPackageSuspendedForUser(packageName, userId);
2088 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002089 throw e.rethrowFromSystemServer();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002090 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002091 }
2092
Jeff Sharkey9bc89af2017-01-11 11:25:50 -07002093 /** @hide */
2094 @Override
2095 public void setApplicationCategoryHint(String packageName, int categoryHint) {
2096 try {
2097 mPM.setApplicationCategoryHint(packageName, categoryHint,
2098 mContext.getOpPackageName());
2099 } catch (RemoteException e) {
2100 throw e.rethrowFromSystemServer();
2101 }
2102 }
2103
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002104 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002105 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07002106 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002107 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07002108 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002109 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002110 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002111 }
2112 }
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002113
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002114 @Override
2115 public void addPackageToPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002116 Log.w(TAG, "addPackageToPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002117 }
2118
2119 @Override
2120 public void removePackageFromPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002121 Log.w(TAG, "removePackageFromPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002122 }
2123
2124 @Override
2125 public List<PackageInfo> getPreferredPackages(int flags) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002126 Log.w(TAG, "getPreferredPackages() is a no-op");
2127 return Collections.emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002128 }
2129
2130 @Override
2131 public void addPreferredActivity(IntentFilter filter,
2132 int match, ComponentName[] set, ComponentName activity) {
2133 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002134 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07002135 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002136 throw e.rethrowFromSystemServer();
Amith Yamasania3f133a2012-08-09 17:11:28 -07002137 }
2138 }
2139
2140 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002141 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07002142 ComponentName[] set, ComponentName activity, int userId) {
2143 try {
2144 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002145 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002146 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002147 }
2148 }
2149
2150 @Override
2151 public void replacePreferredActivity(IntentFilter filter,
2152 int match, ComponentName[] set, ComponentName activity) {
2153 try {
Robin Lee0e27c872015-09-28 14:37:40 +01002154 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002155 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002156 throw e.rethrowFromSystemServer();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002157 }
2158 }
2159
2160 @Override
2161 public void replacePreferredActivityAsUser(IntentFilter filter,
2162 int match, ComponentName[] set, ComponentName activity,
2163 int userId) {
2164 try {
2165 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002166 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002167 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002168 }
2169 }
2170
2171 @Override
2172 public void clearPackagePreferredActivities(String packageName) {
2173 try {
2174 mPM.clearPackagePreferredActivities(packageName);
2175 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002176 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002177 }
2178 }
2179
2180 @Override
2181 public int getPreferredActivities(List<IntentFilter> outFilters,
2182 List<ComponentName> outActivities, String packageName) {
2183 try {
2184 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2185 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002186 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002187 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002188 }
2189
2190 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002191 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2192 try {
2193 return mPM.getHomeActivities(outActivities);
2194 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002195 throw e.rethrowFromSystemServer();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002196 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002197 }
2198
2199 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002200 public void setComponentEnabledSetting(ComponentName componentName,
2201 int newState, int flags) {
2202 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002203 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002204 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002205 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002206 }
2207 }
2208
2209 @Override
2210 public int getComponentEnabledSetting(ComponentName componentName) {
2211 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002212 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002213 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002214 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002215 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002216 }
2217
2218 @Override
2219 public void setApplicationEnabledSetting(String packageName,
2220 int newState, int flags) {
2221 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002222 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002223 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002224 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002225 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002226 }
2227 }
2228
2229 @Override
2230 public int getApplicationEnabledSetting(String packageName) {
2231 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002232 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002233 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002234 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002235 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002236 }
2237
Amith Yamasani655d0e22013-06-12 14:19:10 -07002238 @Override
Sudheer Shankabbb3ff22015-07-09 15:39:23 +01002239 public void flushPackageRestrictionsAsUser(int userId) {
2240 try {
2241 mPM.flushPackageRestrictionsAsUser(userId);
2242 } catch (RemoteException e) {
2243 throw e.rethrowFromSystemServer();
2244 }
2245 }
2246
2247 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002248 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002249 UserHandle user) {
2250 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002251 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002252 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002253 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002254 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002255 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002256 }
2257
2258 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002259 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002260 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002261 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002262 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002263 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002264 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002265 }
2266
dcashmanc6f22492014-08-14 09:54:51 -07002267 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002268 @Override
2269 public KeySet getKeySetByAlias(String packageName, String alias) {
2270 Preconditions.checkNotNull(packageName);
2271 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002272 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002273 return mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002274 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002275 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002276 }
dcashman9d2f4412014-06-09 09:27:54 -07002277 }
2278
dcashmanc6f22492014-08-14 09:54:51 -07002279 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002280 @Override
2281 public KeySet getSigningKeySet(String packageName) {
2282 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002283 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002284 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002285 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002286 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002287 }
dcashman9d2f4412014-06-09 09:27:54 -07002288 }
2289
dcashmanc6f22492014-08-14 09:54:51 -07002290 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002291 @Override
2292 public boolean isSignedBy(String packageName, KeySet ks) {
2293 Preconditions.checkNotNull(packageName);
2294 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002295 try {
dcashmanc6f22492014-08-14 09:54:51 -07002296 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002297 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002298 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002299 }
2300 }
2301
dcashmanc6f22492014-08-14 09:54:51 -07002302 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002303 @Override
2304 public boolean isSignedByExactly(String packageName, KeySet ks) {
2305 Preconditions.checkNotNull(packageName);
2306 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002307 try {
dcashmanc6f22492014-08-14 09:54:51 -07002308 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002309 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002310 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002311 }
2312 }
2313
Kenny Root0aaa0d92011-09-12 16:42:55 -07002314 /**
2315 * @hide
2316 */
2317 @Override
2318 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2319 try {
2320 return mPM.getVerifierDeviceIdentity();
2321 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002322 throw e.rethrowFromSystemServer();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002323 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002324 }
2325
Jeff Hao9f60c082014-10-28 18:51:07 -07002326 /**
2327 * @hide
2328 */
2329 @Override
2330 public boolean isUpgrade() {
2331 try {
2332 return mPM.isUpgrade();
2333 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002334 throw e.rethrowFromSystemServer();
Jeff Hao9f60c082014-10-28 18:51:07 -07002335 }
2336 }
2337
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002338 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002339 public PackageInstaller getPackageInstaller() {
2340 synchronized (mLock) {
2341 if (mInstaller == null) {
2342 try {
Svet Ganov67882122016-12-11 16:36:34 -08002343 mInstaller = new PackageInstaller(mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002344 mContext.getPackageName(), mContext.getUserId());
2345 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002346 throw e.rethrowFromSystemServer();
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002347 }
2348 }
2349 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002350 }
2351 }
2352
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002353 @Override
2354 public boolean isPackageAvailable(String packageName) {
2355 try {
2356 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2357 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002358 throw e.rethrowFromSystemServer();
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002359 }
2360 }
2361
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002362 /**
2363 * @hide
2364 */
2365 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002366 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2367 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002368 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002369 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002370 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002371 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002372 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002373 }
2374 }
2375
2376 /**
2377 * @hide
2378 */
2379 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002380 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002381 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002382 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002383 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002384 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002385 }
2386 }
2387
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002388 /**
2389 * @hide
2390 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002391 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002392 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2393 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2394 return dr;
2395 }
2396 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2397 }
2398
2399 /**
2400 * @hide
2401 */
2402 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002403 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002404 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2405 if (bitmap == null) {
2406 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2407 }
2408 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002409 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002410 Drawable dr = null;
2411 if (itemInfo.packageName != null) {
2412 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2413 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002414 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002415 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002416 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002417 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002418 }
2419
2420 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2421 Rect badgeLocation, boolean tryBadgeInPlace) {
2422 final int badgedWidth = drawable.getIntrinsicWidth();
2423 final int badgedHeight = drawable.getIntrinsicHeight();
2424 final boolean canBadgeInPlace = tryBadgeInPlace
2425 && (drawable instanceof BitmapDrawable)
2426 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2427
2428 final Bitmap bitmap;
2429 if (canBadgeInPlace) {
2430 bitmap = ((BitmapDrawable) drawable).getBitmap();
2431 } else {
2432 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2433 }
2434 Canvas canvas = new Canvas(bitmap);
2435
2436 if (!canBadgeInPlace) {
2437 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2438 drawable.draw(canvas);
2439 }
2440
2441 if (badgeLocation != null) {
2442 if (badgeLocation.left < 0 || badgeLocation.top < 0
2443 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2444 throw new IllegalArgumentException("Badge location " + badgeLocation
2445 + " not in badged drawable bounds "
2446 + new Rect(0, 0, badgedWidth, badgedHeight));
2447 }
2448 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2449
2450 canvas.save();
2451 canvas.translate(badgeLocation.left, badgeLocation.top);
2452 badgeDrawable.draw(canvas);
2453 canvas.restore();
2454 } else {
2455 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2456 badgeDrawable.draw(canvas);
2457 }
2458
2459 if (!canBadgeInPlace) {
2460 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2461
2462 if (drawable instanceof BitmapDrawable) {
2463 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2464 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2465 }
2466
2467 return mergedDrawable;
2468 }
2469
2470 return drawable;
2471 }
2472
Tony Mak8673b282016-03-21 21:10:59 +00002473 private boolean isManagedProfile(int userId) {
2474 return getUserManager().isManagedProfile(userId);
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002475 }
2476
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +01002477 /**
2478 * @hide
2479 */
2480 @Override
2481 public int getInstallReason(String packageName, UserHandle user) {
2482 try {
2483 return mPM.getInstallReason(packageName, user.getIdentifier());
2484 } catch (RemoteException e) {
2485 throw e.rethrowFromSystemServer();
2486 }
2487 }
2488
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002489 /** {@hide} */
2490 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2491 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002492 private static final int MSG_CREATED = 1;
2493 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002494
2495 final MoveCallback mCallback;
2496 final Handler mHandler;
2497
2498 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2499 mCallback = callback;
2500 mHandler = new Handler(looper, this);
2501 }
2502
2503 @Override
2504 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002505 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002506 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002507 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002508 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002509 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002510 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002511 }
2512 case MSG_STATUS_CHANGED: {
2513 final SomeArgs args = (SomeArgs) msg.obj;
2514 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2515 args.recycle();
2516 return true;
2517 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002518 }
2519 return false;
2520 }
2521
2522 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002523 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002524 final SomeArgs args = SomeArgs.obtain();
2525 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002526 args.arg2 = extras;
2527 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2528 }
2529
2530 @Override
2531 public void onStatusChanged(int moveId, int status, long estMillis) {
2532 final SomeArgs args = SomeArgs.obtain();
2533 args.argi1 = moveId;
2534 args.argi2 = status;
2535 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002536 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002537 }
2538 }
2539
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002540 private final ContextImpl mContext;
2541 private final IPackageManager mPM;
2542
2543 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002544 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2545 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2546 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2547 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002548
2549 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2550 mPermissionListeners = new ArrayMap<>();
2551
2552 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2553 implements Handler.Callback{
2554 private static final int MSG_PERMISSIONS_CHANGED = 1;
2555
2556 private final OnPermissionsChangedListener mListener;
2557 private final Handler mHandler;
2558
2559
2560 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2561 Looper looper) {
2562 mListener = listener;
2563 mHandler = new Handler(looper, this);
2564 }
2565
2566 @Override
2567 public void onPermissionsChanged(int uid) {
2568 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2569 }
2570
2571 @Override
2572 public boolean handleMessage(Message msg) {
2573 switch (msg.what) {
2574 case MSG_PERMISSIONS_CHANGED: {
2575 final int uid = msg.arg1;
2576 mListener.onPermissionsChanged(uid);
2577 return true;
2578 }
2579 }
2580 return false;
2581 }
2582 }
Suprabh Shuklaaef25132017-01-23 18:09:03 -08002583
2584 @Override
2585 public boolean canRequestPackageInstalls() {
2586 try {
2587 return mPM.canRequestPackageInstalls(mContext.getPackageName(), mContext.getUserId());
2588 } catch (RemoteException e) {
2589 throw e.rethrowAsRuntimeException();
2590 }
2591 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002592}