blob: 42b18384c588f06d05eaf0a06cb80e95edd12117 [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;
Alan Viveretteecd585a2015-04-13 10:32:51 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.StringRes;
23import android.annotation.XmlRes;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080024import android.content.ComponentName;
25import android.content.ContentResolver;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.IntentSender;
29import android.content.pm.ActivityInfo;
30import android.content.pm.ApplicationInfo;
31import android.content.pm.ComponentInfo;
Anonymous Cowardceb1b0b2012-04-24 10:35:16 -070032import android.content.pm.ContainerEncryptionParams;
Svet Ganov2acf0632015-11-24 19:10:59 -080033import android.content.pm.EphemeralApplicationInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080034import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070035import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080036import android.content.pm.IPackageDataObserver;
37import android.content.pm.IPackageDeleteObserver;
38import android.content.pm.IPackageInstallObserver;
39import android.content.pm.IPackageManager;
40import android.content.pm.IPackageMoveObserver;
41import android.content.pm.IPackageStatsObserver;
42import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080043import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070044import android.content.pm.KeySet;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080045import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070046import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010047import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080048import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070049import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080050import android.content.pm.PermissionGroupInfo;
51import android.content.pm.PermissionInfo;
52import android.content.pm.ProviderInfo;
53import android.content.pm.ResolveInfo;
54import android.content.pm.ServiceInfo;
Svetoslavc7d62f02014-09-04 15:39:54 -070055import android.content.pm.UserInfo;
rich cannings706e8ba2012-08-20 13:20:14 -070056import android.content.pm.VerificationParams;
Kenny Root0aaa0d92011-09-12 16:42:55 -070057import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080058import android.content.res.Resources;
59import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070060import android.graphics.Bitmap;
61import android.graphics.Canvas;
62import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010063import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080064import android.graphics.drawable.Drawable;
65import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070066import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070067import android.os.Handler;
68import android.os.Looper;
69import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080070import android.os.Process;
71import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070072import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080073import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010074import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070075import android.os.storage.StorageManager;
76import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070077import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070078import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080079import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070080import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070081
82import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070083
84import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070085import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070086import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010087import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080088import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070089
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080090import java.lang.ref.WeakReference;
91import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080092import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070093import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080094import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070095import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070096import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080097
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070098/** @hide */
99public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800100 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800101 private final static boolean DEBUG_ICONS = false;
102
Svet Ganov2acf0632015-11-24 19:10:59 -0800103 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
104
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700105 // Default flags to use with PackageManager when no flags are given.
106 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
107
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700108 private final Object mLock = new Object();
109
110 @GuardedBy("mLock")
111 private UserManager mUserManager;
112 @GuardedBy("mLock")
113 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100114
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700115 @GuardedBy("mDelegates")
116 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
117
Svet Ganovf1b7f202015-07-29 08:33:42 -0700118 @GuardedBy("mLock")
119 private String mPermissionsControllerPackageName;
120
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100121 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700122 synchronized (mLock) {
123 if (mUserManager == null) {
124 mUserManager = UserManager.get(mContext);
125 }
126 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100127 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100128 }
129
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800130 @Override
131 public PackageInfo getPackageInfo(String packageName, int flags)
132 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100133 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
134 }
135
136 @Override
137 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
138 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800139 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100140 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800141 if (pi != null) {
142 return pi;
143 }
144 } catch (RemoteException e) {
145 throw new RuntimeException("Package manager has died", e);
146 }
147
148 throw new NameNotFoundException(packageName);
149 }
150
151 @Override
152 public String[] currentToCanonicalPackageNames(String[] names) {
153 try {
154 return mPM.currentToCanonicalPackageNames(names);
155 } catch (RemoteException e) {
156 throw new RuntimeException("Package manager has died", e);
157 }
158 }
159
160 @Override
161 public String[] canonicalToCurrentPackageNames(String[] names) {
162 try {
163 return mPM.canonicalToCurrentPackageNames(names);
164 } catch (RemoteException e) {
165 throw new RuntimeException("Package manager has died", e);
166 }
167 }
168
169 @Override
170 public Intent getLaunchIntentForPackage(String packageName) {
171 // First see if the package has an INFO activity; the existence of
172 // such an activity is implied to be the desired front-door for the
173 // overall package (such as if it has multiple launcher entries).
174 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
175 intentToResolve.addCategory(Intent.CATEGORY_INFO);
176 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800177 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800178
179 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800180 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800181 // reuse the intent instance
182 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
183 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
184 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800185 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800186 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800187 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800188 return null;
189 }
190 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800191 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800192 intent.setClassName(ris.get(0).activityInfo.packageName,
193 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800194 return intent;
195 }
196
197 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700198 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
199 // Try to find a main leanback_launcher activity.
200 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
201 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
202 intentToResolve.setPackage(packageName);
203 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
204
205 if (ris == null || ris.size() <= 0) {
206 return null;
207 }
208 Intent intent = new Intent(intentToResolve);
209 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
210 intent.setClassName(ris.get(0).activityInfo.packageName,
211 ris.get(0).activityInfo.name);
212 return intent;
213 }
214
215 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700216 public int[] getPackageGids(String packageName) throws NameNotFoundException {
217 return getPackageGids(packageName, 0);
218 }
219
220 @Override
221 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800222 throws NameNotFoundException {
223 try {
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700224 int[] gids = mPM.getPackageGidsEtc(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800225 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800226 return gids;
227 }
228 } catch (RemoteException e) {
229 throw new RuntimeException("Package manager has died", e);
230 }
231
232 throw new NameNotFoundException(packageName);
233 }
234
235 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700236 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
237 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
238 }
239
240 @Override
241 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
242 return getPackageUidAsUser(packageName, 0, userId);
243 }
244
245 @Override
246 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800247 throws NameNotFoundException {
248 try {
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700249 int uid = mPM.getPackageUidEtc(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800250 if (uid >= 0) {
251 return uid;
252 }
253 } catch (RemoteException e) {
254 throw new RuntimeException("Package manager has died", e);
255 }
256
257 throw new NameNotFoundException(packageName);
258 }
259
260 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800261 public PermissionInfo getPermissionInfo(String name, int flags)
262 throws NameNotFoundException {
263 try {
264 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
265 if (pi != null) {
266 return pi;
267 }
268 } catch (RemoteException e) {
269 throw new RuntimeException("Package manager has died", e);
270 }
271
272 throw new NameNotFoundException(name);
273 }
274
275 @Override
276 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
277 throws NameNotFoundException {
278 try {
279 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags);
280 if (pi != null) {
281 return pi;
282 }
283 } catch (RemoteException e) {
284 throw new RuntimeException("Package manager has died", e);
285 }
286
287 throw new NameNotFoundException(group);
288 }
289
290 @Override
291 public PermissionGroupInfo getPermissionGroupInfo(String name,
292 int flags) throws NameNotFoundException {
293 try {
294 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
295 if (pgi != null) {
296 return pgi;
297 }
298 } catch (RemoteException e) {
299 throw new RuntimeException("Package manager has died", e);
300 }
301
302 throw new NameNotFoundException(name);
303 }
304
305 @Override
306 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
307 try {
308 return mPM.getAllPermissionGroups(flags);
309 } catch (RemoteException e) {
310 throw new RuntimeException("Package manager has died", e);
311 }
312 }
313
314 @Override
315 public ApplicationInfo getApplicationInfo(String packageName, int flags)
316 throws NameNotFoundException {
317 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700318 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800319 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100320 // This is a temporary hack. Callers must use
321 // createPackageContext(packageName).getApplicationInfo() to
322 // get the right paths.
323 maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800324 return ai;
325 }
326 } catch (RemoteException e) {
327 throw new RuntimeException("Package manager has died", e);
328 }
329
330 throw new NameNotFoundException(packageName);
331 }
332
Narayan Kamathcaa71192014-07-16 11:06:43 +0100333 private static void maybeAdjustApplicationInfo(ApplicationInfo info) {
334 // If we're dealing with a multi-arch application that has both
335 // 32 and 64 bit shared libraries, we might need to choose the secondary
336 // depending on what the current runtime's instruction set is.
337 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
338 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500339
340 // Get the instruction set that the libraries of secondary Abi is supported.
341 // In presence of a native bridge this might be different than the one secondary Abi used.
342 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
343 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
344 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100345
346 // If the runtimeIsa is the same as the primary isa, then we do nothing.
347 // Everything will be set up correctly because info.nativeLibraryDir will
348 // correspond to the right ISA.
349 if (runtimeIsa.equals(secondaryIsa)) {
350 info.nativeLibraryDir = info.secondaryNativeLibraryDir;
351 }
352 }
353 }
354
355
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800356 @Override
357 public ActivityInfo getActivityInfo(ComponentName className, int flags)
358 throws NameNotFoundException {
359 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700360 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800361 if (ai != null) {
362 return ai;
363 }
364 } catch (RemoteException e) {
365 throw new RuntimeException("Package manager has died", e);
366 }
367
368 throw new NameNotFoundException(className.toString());
369 }
370
371 @Override
372 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
373 throws NameNotFoundException {
374 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700375 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800376 if (ai != null) {
377 return ai;
378 }
379 } catch (RemoteException e) {
380 throw new RuntimeException("Package manager has died", e);
381 }
382
383 throw new NameNotFoundException(className.toString());
384 }
385
386 @Override
387 public ServiceInfo getServiceInfo(ComponentName className, int flags)
388 throws NameNotFoundException {
389 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700390 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800391 if (si != null) {
392 return si;
393 }
394 } catch (RemoteException e) {
395 throw new RuntimeException("Package manager has died", e);
396 }
397
398 throw new NameNotFoundException(className.toString());
399 }
400
401 @Override
402 public ProviderInfo getProviderInfo(ComponentName className, int flags)
403 throws NameNotFoundException {
404 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700405 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800406 if (pi != null) {
407 return pi;
408 }
409 } catch (RemoteException e) {
410 throw new RuntimeException("Package manager has died", e);
411 }
412
413 throw new NameNotFoundException(className.toString());
414 }
415
416 @Override
417 public String[] getSystemSharedLibraryNames() {
418 try {
419 return mPM.getSystemSharedLibraryNames();
420 } catch (RemoteException e) {
421 throw new RuntimeException("Package manager has died", e);
422 }
423 }
424
425 @Override
426 public FeatureInfo[] getSystemAvailableFeatures() {
427 try {
428 return mPM.getSystemAvailableFeatures();
429 } catch (RemoteException e) {
430 throw new RuntimeException("Package manager has died", e);
431 }
432 }
433
434 @Override
435 public boolean hasSystemFeature(String name) {
436 try {
437 return mPM.hasSystemFeature(name);
438 } catch (RemoteException e) {
439 throw new RuntimeException("Package manager has died", e);
440 }
441 }
442
443 @Override
444 public int checkPermission(String permName, String pkgName) {
445 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800446 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800447 } catch (RemoteException e) {
448 throw new RuntimeException("Package manager has died", e);
449 }
450 }
451
452 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700453 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
454 try {
455 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
456 } catch (RemoteException e) {
457 throw new RuntimeException("Package manager has died", e);
458 }
459 }
460
Svet Ganovf1b7f202015-07-29 08:33:42 -0700461 /**
462 * @hide
463 */
464 @Override
465 public String getPermissionControllerPackageName() {
466 synchronized (mLock) {
467 if (mPermissionsControllerPackageName == null) {
468 try {
469 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
470 } catch (RemoteException e) {
471 throw new RuntimeException("Package manager has died", e);
472 }
473 }
474 return mPermissionsControllerPackageName;
475 }
476 }
477
Svet Ganovad3b2972015-07-07 22:49:17 -0700478 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800479 public boolean addPermission(PermissionInfo info) {
480 try {
481 return mPM.addPermission(info);
482 } catch (RemoteException e) {
483 throw new RuntimeException("Package manager has died", e);
484 }
485 }
486
487 @Override
488 public boolean addPermissionAsync(PermissionInfo info) {
489 try {
490 return mPM.addPermissionAsync(info);
491 } catch (RemoteException e) {
492 throw new RuntimeException("Package manager has died", e);
493 }
494 }
495
496 @Override
497 public void removePermission(String name) {
498 try {
499 mPM.removePermission(name);
500 } catch (RemoteException e) {
501 throw new RuntimeException("Package manager has died", e);
502 }
503 }
504
505 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700506 public void grantRuntimePermission(String packageName, String permissionName,
507 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800508 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700509 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800510 } catch (RemoteException e) {
511 throw new RuntimeException("Package manager has died", e);
512 }
513 }
514
515 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700516 public void revokeRuntimePermission(String packageName, String permissionName,
517 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800518 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700519 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
520 } catch (RemoteException e) {
521 throw new RuntimeException("Package manager has died", e);
522 }
523 }
524
525 @Override
526 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
527 try {
528 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
529 } catch (RemoteException e) {
530 throw new RuntimeException("Package manager has died", e);
531 }
532 }
533
534 @Override
535 public void updatePermissionFlags(String permissionName, String packageName,
536 int flagMask, int flagValues, UserHandle user) {
537 try {
538 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
539 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800540 } catch (RemoteException e) {
541 throw new RuntimeException("Package manager has died", e);
542 }
543 }
544
545 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700546 public boolean shouldShowRequestPermissionRationale(String permission) {
547 try {
548 return mPM.shouldShowRequestPermissionRationale(permission,
549 mContext.getPackageName(), mContext.getUserId());
550 } catch (RemoteException e) {
551 throw new RuntimeException("Package manager has died", e);
552 }
553 }
554
555 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800556 public int checkSignatures(String pkg1, String pkg2) {
557 try {
558 return mPM.checkSignatures(pkg1, pkg2);
559 } catch (RemoteException e) {
560 throw new RuntimeException("Package manager has died", e);
561 }
562 }
563
564 @Override
565 public int checkSignatures(int uid1, int uid2) {
566 try {
567 return mPM.checkUidSignatures(uid1, uid2);
568 } catch (RemoteException e) {
569 throw new RuntimeException("Package manager has died", e);
570 }
571 }
572
573 @Override
574 public String[] getPackagesForUid(int uid) {
575 try {
576 return mPM.getPackagesForUid(uid);
577 } catch (RemoteException e) {
578 throw new RuntimeException("Package manager has died", e);
579 }
580 }
581
582 @Override
583 public String getNameForUid(int uid) {
584 try {
585 return mPM.getNameForUid(uid);
586 } catch (RemoteException e) {
587 throw new RuntimeException("Package manager has died", e);
588 }
589 }
590
591 @Override
592 public int getUidForSharedUser(String sharedUserName)
593 throws NameNotFoundException {
594 try {
595 int uid = mPM.getUidForSharedUser(sharedUserName);
596 if(uid != -1) {
597 return uid;
598 }
599 } catch (RemoteException e) {
600 throw new RuntimeException("Package manager has died", e);
601 }
602 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
603 }
604
Kenny Roote6cd0c72011-05-19 12:48:14 -0700605 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800606 @Override
607 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700608 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700609 }
610
611 /** @hide */
612 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700613 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800614 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800615 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
616 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800617 } catch (RemoteException e) {
618 throw new RuntimeException("Package manager has died", e);
619 }
620 }
621
Kenny Roote6cd0c72011-05-19 12:48:14 -0700622 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800623 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800624 public List<PackageInfo> getPackagesHoldingPermissions(
625 String[] permissions, int flags) {
626 final int userId = mContext.getUserId();
627 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800628 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
629 permissions, flags, userId);
630 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800631 } catch (RemoteException e) {
632 throw new RuntimeException("Package manager has died", e);
633 }
634 }
635
636 @SuppressWarnings("unchecked")
637 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800638 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700639 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800640 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800641 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
642 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800643 } catch (RemoteException e) {
644 throw new RuntimeException("Package manager has died", e);
645 }
646 }
647
Svet Ganov2acf0632015-11-24 19:10:59 -0800648 /** @hide */
649 @SuppressWarnings("unchecked")
650 @Override
651 public List<EphemeralApplicationInfo> getEphemeralApplications() {
652 try {
653 ParceledListSlice<EphemeralApplicationInfo> slice =
654 mPM.getEphemeralApplications(mContext.getUserId());
655 if (slice != null) {
656 return slice.getList();
657 }
658 return Collections.emptyList();
659 } catch (RemoteException e) {
660 throw new RuntimeException("Package manager has died", e);
661 }
662 }
663
664 /** @hide */
665 @Override
666 public Drawable getEphemeralApplicationIcon(String packageName) {
667 try {
668 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
669 packageName, mContext.getUserId());
670 if (bitmap != null) {
671 return new BitmapDrawable(null, bitmap);
672 }
673 return null;
674 } catch (RemoteException e) {
675 throw new RuntimeException("Package manager has died", e);
676 }
677 }
678
679 @Override
680 public boolean isEphemeralApplication() {
681 try {
682 return mPM.isEphemeralApplication(
683 mContext.getPackageName(), mContext.getUserId());
684 } catch (RemoteException e) {
685 Log.e(TAG, "System server is dead", e);
686 }
687 return false;
688 }
689
690 @Override
691 public int getEphemeralCookieMaxSizeBytes() {
692 return Settings.Global.getInt(mContext.getContentResolver(),
693 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
694 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
695 }
696
697 @Override
698 public @NonNull byte[] getEphemeralCookie() {
699 try {
700 final byte[] cookie = mPM.getEphemeralApplicationCookie(
701 mContext.getPackageName(), mContext.getUserId());
702 if (cookie != null) {
703 return cookie;
704 }
705 } catch (RemoteException e) {
706 Log.e(TAG, "System server is dead", e);
707 }
708 return EmptyArray.BYTE;
709 }
710
711 @Override
712 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
713 try {
714 return mPM.setEphemeralApplicationCookie(
715 mContext.getPackageName(), cookie, mContext.getUserId());
716 } catch (RemoteException e) {
717 Log.e(TAG, "System server is dead", e);
718 }
719 return false;
720 }
721
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800722 @Override
723 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700724 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700725 }
726
727 @Override
728 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800729 try {
730 return mPM.resolveIntent(
731 intent,
732 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700733 flags,
734 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800735 } catch (RemoteException e) {
736 throw new RuntimeException("Package manager has died", e);
737 }
738 }
739
740 @Override
741 public List<ResolveInfo> queryIntentActivities(Intent intent,
742 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700743 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700744 }
745
746 /** @hide Same as above but for a specific user */
747 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700748 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700749 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800750 try {
751 return mPM.queryIntentActivities(
752 intent,
753 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700754 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700755 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800756 } catch (RemoteException e) {
757 throw new RuntimeException("Package manager has died", e);
758 }
759 }
760
761 @Override
762 public List<ResolveInfo> queryIntentActivityOptions(
763 ComponentName caller, Intent[] specifics, Intent intent,
764 int flags) {
765 final ContentResolver resolver = mContext.getContentResolver();
766
767 String[] specificTypes = null;
768 if (specifics != null) {
769 final int N = specifics.length;
770 for (int i=0; i<N; i++) {
771 Intent sp = specifics[i];
772 if (sp != null) {
773 String t = sp.resolveTypeIfNeeded(resolver);
774 if (t != null) {
775 if (specificTypes == null) {
776 specificTypes = new String[N];
777 }
778 specificTypes[i] = t;
779 }
780 }
781 }
782 }
783
784 try {
785 return mPM.queryIntentActivityOptions(caller, specifics,
786 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700787 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800788 } catch (RemoteException e) {
789 throw new RuntimeException("Package manager has died", e);
790 }
791 }
792
Amith Yamasanif203aee2012-08-29 18:41:53 -0700793 /**
794 * @hide
795 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800796 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700797 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800798 try {
799 return mPM.queryIntentReceivers(
800 intent,
801 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700802 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -0700803 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800804 } catch (RemoteException e) {
805 throw new RuntimeException("Package manager has died", e);
806 }
807 }
808
809 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700810 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700811 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700812 }
813
814 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800815 public ResolveInfo resolveService(Intent intent, int flags) {
816 try {
817 return mPM.resolveService(
818 intent,
819 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700820 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700821 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800822 } catch (RemoteException e) {
823 throw new RuntimeException("Package manager has died", e);
824 }
825 }
826
827 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700828 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800829 try {
830 return mPM.queryIntentServices(
831 intent,
832 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700833 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700834 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800835 } catch (RemoteException e) {
836 throw new RuntimeException("Package manager has died", e);
837 }
838 }
839
840 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700841 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700842 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700843 }
844
845 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700846 public List<ResolveInfo> queryIntentContentProvidersAsUser(
847 Intent intent, int flags, int userId) {
848 try {
849 return mPM.queryIntentContentProviders(intent,
850 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
851 } catch (RemoteException e) {
852 throw new RuntimeException("Package manager has died", e);
853 }
854 }
855
856 @Override
857 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
858 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
859 }
860
861 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100862 public ProviderInfo resolveContentProvider(String name, int flags) {
863 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
864 }
865
866 /** @hide **/
867 @Override
868 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800869 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100870 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800871 } catch (RemoteException e) {
872 throw new RuntimeException("Package manager has died", e);
873 }
874 }
875
876 @Override
877 public List<ProviderInfo> queryContentProviders(String processName,
878 int uid, int flags) {
879 try {
Dianne Hackborn28ec27c2015-08-03 15:28:28 -0700880 ParceledListSlice<ProviderInfo> slice
881 = mPM.queryContentProviders(processName, uid, flags);
882 return slice != null ? slice.getList() : null;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800883 } catch (RemoteException e) {
884 throw new RuntimeException("Package manager has died", e);
885 }
886 }
887
888 @Override
889 public InstrumentationInfo getInstrumentationInfo(
890 ComponentName className, int flags)
891 throws NameNotFoundException {
892 try {
893 InstrumentationInfo ii = mPM.getInstrumentationInfo(
894 className, flags);
895 if (ii != null) {
896 return ii;
897 }
898 } catch (RemoteException e) {
899 throw new RuntimeException("Package manager has died", e);
900 }
901
902 throw new NameNotFoundException(className.toString());
903 }
904
905 @Override
906 public List<InstrumentationInfo> queryInstrumentation(
907 String targetPackage, int flags) {
908 try {
909 return mPM.queryInstrumentation(targetPackage, flags);
910 } catch (RemoteException e) {
911 throw new RuntimeException("Package manager has died", e);
912 }
913 }
914
Alan Viveretteecd585a2015-04-13 10:32:51 -0700915 @Nullable
916 @Override
917 public Drawable getDrawable(String packageName, @DrawableRes int resId,
918 @Nullable ApplicationInfo appInfo) {
919 final ResourceName name = new ResourceName(packageName, resId);
920 final Drawable cachedIcon = getCachedIcon(name);
921 if (cachedIcon != null) {
922 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800923 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700924
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800925 if (appInfo == null) {
926 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700927 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800928 } catch (NameNotFoundException e) {
929 return null;
930 }
931 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700932
933 if (resId != 0) {
934 try {
935 final Resources r = getResourcesForApplication(appInfo);
936 final Drawable dr = r.getDrawable(resId, null);
937 if (dr != null) {
938 putCachedIcon(name, dr);
939 }
940
941 if (false) {
942 RuntimeException e = new RuntimeException("here");
943 e.fillInStackTrace();
944 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
945 + " from package " + packageName
946 + ": app scale=" + r.getCompatibilityInfo().applicationScale
947 + ", caller scale=" + mContext.getResources()
948 .getCompatibilityInfo().applicationScale,
949 e);
950 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100951 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700952 Log.v(TAG, "Getting drawable 0x"
953 + Integer.toHexString(resId) + " from " + r
954 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100955 }
956 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700957 } catch (NameNotFoundException e) {
958 Log.w("PackageManager", "Failure retrieving resources for "
959 + appInfo.packageName);
960 } catch (Resources.NotFoundException e) {
961 Log.w("PackageManager", "Failure retrieving resources for "
962 + appInfo.packageName + ": " + e.getMessage());
963 } catch (Exception e) {
964 // If an exception was thrown, fall through to return
965 // default icon.
966 Log.w("PackageManager", "Failure retrieving icon 0x"
967 + Integer.toHexString(resId) + " in package "
968 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800969 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800970 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700971
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800972 return null;
973 }
974
975 @Override public Drawable getActivityIcon(ComponentName activityName)
976 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700977 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800978 }
979
980 @Override public Drawable getActivityIcon(Intent intent)
981 throws NameNotFoundException {
982 if (intent.getComponent() != null) {
983 return getActivityIcon(intent.getComponent());
984 }
985
986 ResolveInfo info = resolveActivity(
987 intent, PackageManager.MATCH_DEFAULT_ONLY);
988 if (info != null) {
989 return info.activityInfo.loadIcon(this);
990 }
991
Romain Guy39fe17c2011-11-30 10:34:07 -0800992 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800993 }
994
995 @Override public Drawable getDefaultActivityIcon() {
996 return Resources.getSystem().getDrawable(
997 com.android.internal.R.drawable.sym_def_app_icon);
998 }
999
1000 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1001 return info.loadIcon(this);
1002 }
1003
1004 @Override public Drawable getApplicationIcon(String packageName)
1005 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001006 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001007 }
1008
1009 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001010 public Drawable getActivityBanner(ComponentName activityName)
1011 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001012 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001013 }
1014
1015 @Override
1016 public Drawable getActivityBanner(Intent intent)
1017 throws NameNotFoundException {
1018 if (intent.getComponent() != null) {
1019 return getActivityBanner(intent.getComponent());
1020 }
1021
1022 ResolveInfo info = resolveActivity(
1023 intent, PackageManager.MATCH_DEFAULT_ONLY);
1024 if (info != null) {
1025 return info.activityInfo.loadBanner(this);
1026 }
1027
1028 throw new NameNotFoundException(intent.toUri(0));
1029 }
1030
1031 @Override
1032 public Drawable getApplicationBanner(ApplicationInfo info) {
1033 return info.loadBanner(this);
1034 }
1035
1036 @Override
1037 public Drawable getApplicationBanner(String packageName)
1038 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001039 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001040 }
1041
1042 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001043 public Drawable getActivityLogo(ComponentName activityName)
1044 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001045 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001046 }
1047
1048 @Override
1049 public Drawable getActivityLogo(Intent intent)
1050 throws NameNotFoundException {
1051 if (intent.getComponent() != null) {
1052 return getActivityLogo(intent.getComponent());
1053 }
1054
1055 ResolveInfo info = resolveActivity(
1056 intent, PackageManager.MATCH_DEFAULT_ONLY);
1057 if (info != null) {
1058 return info.activityInfo.loadLogo(this);
1059 }
1060
1061 throw new NameNotFoundException(intent.toUri(0));
1062 }
1063
1064 @Override
1065 public Drawable getApplicationLogo(ApplicationInfo info) {
1066 return info.loadLogo(this);
1067 }
1068
1069 @Override
1070 public Drawable getApplicationLogo(String packageName)
1071 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001072 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001073 }
1074
Svetoslavc7d62f02014-09-04 15:39:54 -07001075 @Override
1076 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1077 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1078 if (badgeResId == 0) {
1079 return icon;
1080 }
1081 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1082 return getBadgedDrawable(icon, badgeIcon, null, true);
1083 }
1084
1085 @Override
1086 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1087 Rect badgeLocation, int badgeDensity) {
1088 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1089 if (badgeDrawable == null) {
1090 return drawable;
1091 }
1092 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1093 }
1094
1095 @Override
1096 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
1097 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1098 if (userInfo != null && userInfo.isManagedProfile()) {
1099 if (density <= 0) {
1100 density = mContext.getResources().getDisplayMetrics().densityDpi;
1101 }
1102 return Resources.getSystem().getDrawableForDensity(
1103 com.android.internal.R.drawable.ic_corp_badge, density);
1104 }
1105 return null;
1106 }
1107
1108 @Override
1109 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
1110 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1111 if (userInfo != null && userInfo.isManagedProfile()) {
1112 return Resources.getSystem().getString(
1113 com.android.internal.R.string.managed_profile_label_badge, label);
1114 }
1115 return label;
1116 }
1117
Alan Viveretteecd585a2015-04-13 10:32:51 -07001118 @Override
1119 public Resources getResourcesForActivity(ComponentName activityName)
1120 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001121 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001122 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001123 }
1124
Alan Viveretteecd585a2015-04-13 10:32:51 -07001125 @Override
1126 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1127 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001128 if (app.packageName.equals("system")) {
1129 return mContext.mMainThread.getSystemContext().getResources();
1130 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001131 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001132 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001133 sameUid ? app.sourceDir : app.publicSourceDir,
1134 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001135 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1136 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001137 if (r != null) {
1138 return r;
1139 }
1140 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1141 }
1142
Alan Viveretteecd585a2015-04-13 10:32:51 -07001143 @Override
1144 public Resources getResourcesForApplication(String appPackageName)
1145 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001146 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001147 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001148 }
1149
Amith Yamasani98edc952012-09-25 14:09:27 -07001150 /** @hide */
1151 @Override
1152 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1153 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001154 if (userId < 0) {
1155 throw new IllegalArgumentException(
1156 "Call does not support special user #" + userId);
1157 }
1158 if ("system".equals(appPackageName)) {
1159 return mContext.mMainThread.getSystemContext().getResources();
1160 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001161 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001162 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001163 if (ai != null) {
1164 return getResourcesForApplication(ai);
1165 }
1166 } catch (RemoteException e) {
1167 throw new RuntimeException("Package manager has died", e);
1168 }
1169 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1170 }
1171
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001172 int mCachedSafeMode = -1;
1173 @Override public boolean isSafeMode() {
1174 try {
1175 if (mCachedSafeMode < 0) {
1176 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1177 }
1178 return mCachedSafeMode != 0;
1179 } catch (RemoteException e) {
1180 throw new RuntimeException("Package manager has died", e);
1181 }
1182 }
1183
Svetoslavf7c06eb2015-06-10 18:43:22 -07001184 @Override
1185 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1186 synchronized (mPermissionListeners) {
1187 if (mPermissionListeners.get(listener) != null) {
1188 return;
1189 }
1190 OnPermissionsChangeListenerDelegate delegate =
1191 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1192 try {
1193 mPM.addOnPermissionsChangeListener(delegate);
1194 mPermissionListeners.put(listener, delegate);
1195 } catch (RemoteException e) {
1196 throw new RuntimeException("Package manager has died", e);
1197 }
1198 }
1199 }
1200
1201 @Override
1202 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1203 synchronized (mPermissionListeners) {
1204 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1205 if (delegate != null) {
1206 try {
1207 mPM.removeOnPermissionsChangeListener(delegate);
1208 mPermissionListeners.remove(listener);
1209 } catch (RemoteException e) {
1210 throw new RuntimeException("Package manager has died", e);
1211 }
1212 }
1213 }
1214 }
1215
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001216 static void configurationChanged() {
1217 synchronized (sSync) {
1218 sIconCache.clear();
1219 sStringCache.clear();
1220 }
1221 }
1222
1223 ApplicationPackageManager(ContextImpl context,
1224 IPackageManager pm) {
1225 mContext = context;
1226 mPM = pm;
1227 }
1228
Alan Viveretteecd585a2015-04-13 10:32:51 -07001229 @Nullable
1230 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001231 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001232 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001233 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1234 + name + ": " + wr);
1235 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001236 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001237 if (state != null) {
1238 if (DEBUG_ICONS) {
1239 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1240 }
1241 // Note: It's okay here to not use the newDrawable(Resources) variant
1242 // of the API. The ConstantState comes from a drawable that was
1243 // originally created by passing the proper app Resources instance
1244 // which means the state should already contain the proper
1245 // resources specific information (like density.) See
1246 // BitmapDrawable.BitmapState for instance.
1247 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001248 }
1249 // our entry has been purged
1250 sIconCache.remove(name);
1251 }
1252 }
1253 return null;
1254 }
1255
Alan Viveretteecd585a2015-04-13 10:32:51 -07001256 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001257 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001258 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001259 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001260 }
1261 }
1262
Romain Guy39fe17c2011-11-30 10:34:07 -08001263 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001264 boolean immediateGc = false;
1265 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1266 immediateGc = true;
1267 }
1268 if (pkgList != null && (pkgList.length > 0)) {
1269 boolean needCleanup = false;
1270 for (String ssp : pkgList) {
1271 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001272 for (int i=sIconCache.size()-1; i>=0; i--) {
1273 ResourceName nm = sIconCache.keyAt(i);
1274 if (nm.packageName.equals(ssp)) {
1275 //Log.i(TAG, "Removing cached drawable for " + nm);
1276 sIconCache.removeAt(i);
1277 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001278 }
1279 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001280 for (int i=sStringCache.size()-1; i>=0; i--) {
1281 ResourceName nm = sStringCache.keyAt(i);
1282 if (nm.packageName.equals(ssp)) {
1283 //Log.i(TAG, "Removing cached string for " + nm);
1284 sStringCache.removeAt(i);
1285 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001286 }
1287 }
1288 }
1289 }
1290 if (needCleanup || hasPkgInfo) {
1291 if (immediateGc) {
1292 // Schedule an immediate gc.
1293 Runtime.getRuntime().gc();
1294 } else {
1295 ActivityThread.currentActivityThread().scheduleGcIdler();
1296 }
1297 }
1298 }
1299 }
1300
1301 private static final class ResourceName {
1302 final String packageName;
1303 final int iconId;
1304
1305 ResourceName(String _packageName, int _iconId) {
1306 packageName = _packageName;
1307 iconId = _iconId;
1308 }
1309
1310 ResourceName(ApplicationInfo aInfo, int _iconId) {
1311 this(aInfo.packageName, _iconId);
1312 }
1313
1314 ResourceName(ComponentInfo cInfo, int _iconId) {
1315 this(cInfo.applicationInfo.packageName, _iconId);
1316 }
1317
1318 ResourceName(ResolveInfo rInfo, int _iconId) {
1319 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1320 }
1321
1322 @Override
1323 public boolean equals(Object o) {
1324 if (this == o) return true;
1325 if (o == null || getClass() != o.getClass()) return false;
1326
1327 ResourceName that = (ResourceName) o;
1328
1329 if (iconId != that.iconId) return false;
1330 return !(packageName != null ?
1331 !packageName.equals(that.packageName) : that.packageName != null);
1332
1333 }
1334
1335 @Override
1336 public int hashCode() {
1337 int result;
1338 result = packageName.hashCode();
1339 result = 31 * result + iconId;
1340 return result;
1341 }
1342
1343 @Override
1344 public String toString() {
1345 return "{ResourceName " + packageName + " / " + iconId + "}";
1346 }
1347 }
1348
1349 private CharSequence getCachedString(ResourceName name) {
1350 synchronized (sSync) {
1351 WeakReference<CharSequence> wr = sStringCache.get(name);
1352 if (wr != null) { // we have the activity
1353 CharSequence cs = wr.get();
1354 if (cs != null) {
1355 return cs;
1356 }
1357 // our entry has been purged
1358 sStringCache.remove(name);
1359 }
1360 }
1361 return null;
1362 }
1363
1364 private void putCachedString(ResourceName name, CharSequence cs) {
1365 synchronized (sSync) {
1366 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1367 }
1368 }
1369
1370 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001371 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001372 ApplicationInfo appInfo) {
1373 ResourceName name = new ResourceName(packageName, resid);
1374 CharSequence text = getCachedString(name);
1375 if (text != null) {
1376 return text;
1377 }
1378 if (appInfo == null) {
1379 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001380 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001381 } catch (NameNotFoundException e) {
1382 return null;
1383 }
1384 }
1385 try {
1386 Resources r = getResourcesForApplication(appInfo);
1387 text = r.getText(resid);
1388 putCachedString(name, text);
1389 return text;
1390 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001391 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001392 + appInfo.packageName);
1393 } catch (RuntimeException e) {
1394 // If an exception was thrown, fall through to return
1395 // default icon.
1396 Log.w("PackageManager", "Failure retrieving text 0x"
1397 + Integer.toHexString(resid) + " in package "
1398 + packageName, e);
1399 }
1400 return null;
1401 }
1402
1403 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001404 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001405 ApplicationInfo appInfo) {
1406 if (appInfo == null) {
1407 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001408 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001409 } catch (NameNotFoundException e) {
1410 return null;
1411 }
1412 }
1413 try {
1414 Resources r = getResourcesForApplication(appInfo);
1415 return r.getXml(resid);
1416 } catch (RuntimeException e) {
1417 // If an exception was thrown, fall through to return
1418 // default icon.
1419 Log.w("PackageManager", "Failure retrieving xml 0x"
1420 + Integer.toHexString(resid) + " in package "
1421 + packageName, e);
1422 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001423 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001424 + appInfo.packageName);
1425 }
1426 return null;
1427 }
1428
1429 @Override
1430 public CharSequence getApplicationLabel(ApplicationInfo info) {
1431 return info.loadLabel(this);
1432 }
1433
1434 @Override
1435 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1436 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001437 final VerificationParams verificationParams = new VerificationParams(null, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001438 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001439 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001440 installerPackageName, verificationParams, null, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001441 }
1442
1443 @Override
Kenny Root5ab21572011-07-27 11:11:19 -07001444 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1445 int flags, String installerPackageName, Uri verificationURI,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001446 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001447 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001448 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001449 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001450 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Kenny Root5ab21572011-07-27 11:11:19 -07001451 }
1452
1453 @Override
John Spurlock8a985d22014-02-25 09:40:05 -05001454 public void installPackageWithVerificationAndEncryption(Uri packageURI,
rich cannings706e8ba2012-08-20 13:20:14 -07001455 IPackageInstallObserver observer, int flags, String installerPackageName,
1456 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001457 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001458 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001459 }
1460
Christopher Tatef1977b42014-03-24 16:25:51 -07001461 @Override
1462 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1463 int flags, String installerPackageName) {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001464 installPackageAsUser(packageURI, observer, flags, installerPackageName,
Robin Lee0e27c872015-09-28 14:37:40 +01001465 mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001466 }
1467
1468 @Override
1469 public void installPackageAsUser(Uri packageURI, PackageInstallObserver observer, int flags,
1470 String installerPackageName, int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001471 final VerificationParams verificationParams = new VerificationParams(null, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001472 null, VerificationParams.NO_UID);
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001473 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null,
1474 userId);
Christopher Tatef1977b42014-03-24 16:25:51 -07001475 }
1476
1477 @Override
1478 public void installPackageWithVerification(Uri packageURI,
1479 PackageInstallObserver observer, int flags, String installerPackageName,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001480 Uri verificationURI,
Christopher Tatef1977b42014-03-24 16:25:51 -07001481 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001482 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001483 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001484 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001485 encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001486 }
1487
1488 @Override
1489 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1490 PackageInstallObserver observer, int flags, String installerPackageName,
1491 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001492 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001493 encryptionParams, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001494 }
1495
1496 private void installCommon(Uri packageURI,
1497 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001498 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams,
1499 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001500 if (!"file".equals(packageURI.getScheme())) {
1501 throw new UnsupportedOperationException("Only file:// URIs are supported");
1502 }
1503 if (encryptionParams != null) {
1504 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1505 }
1506
1507 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001508 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001509 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
1510 verificationParams, null, userId);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001511 } catch (RemoteException ignored) {
rich cannings706e8ba2012-08-20 13:20:14 -07001512 }
1513 }
1514
1515 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001516 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001517 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001518 }
1519
1520 @Override
1521 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001522 throws NameNotFoundException {
1523 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001524 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001525 if (res == INSTALL_FAILED_INVALID_URI) {
1526 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1527 }
1528 return res;
1529 } catch (RemoteException e) {
1530 // Should never happen!
1531 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1532 }
1533 }
1534
1535 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001536 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001537 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001538 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001539 } catch (RemoteException e) {
1540 // Should never happen!
1541 }
1542 }
1543
1544 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001545 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1546 long millisecondsToDelay) {
1547 try {
1548 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1549 } catch (RemoteException e) {
1550 // Should never happen!
1551 }
1552 }
1553
1554 @Override
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001555 public void verifyIntentFilter(int id, int verificationCode, List<String> outFailedDomains) {
1556 try {
1557 mPM.verifyIntentFilter(id, verificationCode, outFailedDomains);
1558 } catch (RemoteException e) {
1559 // Should never happen!
1560 }
1561 }
1562
1563 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001564 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001565 try {
1566 return mPM.getIntentVerificationStatus(packageName, userId);
1567 } catch (RemoteException e) {
1568 // Should never happen!
1569 return PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1570 }
1571 }
1572
1573 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001574 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001575 try {
1576 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1577 } catch (RemoteException e) {
1578 // Should never happen!
1579 return false;
1580 }
1581 }
1582
1583 @Override
1584 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1585 try {
1586 return mPM.getIntentFilterVerifications(packageName);
1587 } catch (RemoteException e) {
1588 // Should never happen!
1589 return null;
1590 }
1591 }
1592
1593 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001594 public List<IntentFilter> getAllIntentFilters(String packageName) {
1595 try {
1596 return mPM.getAllIntentFilters(packageName);
1597 } catch (RemoteException e) {
1598 // Should never happen!
1599 return null;
1600 }
1601 }
1602
1603 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001604 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001605 try {
1606 return mPM.getDefaultBrowserPackageName(userId);
1607 } catch (RemoteException e) {
1608 // Should never happen!
1609 return null;
1610 }
1611 }
1612
1613 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001614 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001615 try {
1616 return mPM.setDefaultBrowserPackageName(packageName, userId);
1617 } catch (RemoteException e) {
1618 // Should never happen!
1619 return false;
1620 }
1621 }
1622
1623 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001624 public void setInstallerPackageName(String targetPackage,
1625 String installerPackageName) {
1626 try {
1627 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1628 } catch (RemoteException e) {
1629 // Should never happen!
1630 }
1631 }
1632
1633 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001634 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001635 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001636 return mPM.getInstallerPackageName(packageName);
1637 } catch (RemoteException e) {
1638 // Should never happen!
1639 }
1640 return null;
1641 }
1642
1643 @Override
1644 public int getMoveStatus(int moveId) {
1645 try {
1646 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001647 } catch (RemoteException e) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001648 throw e.rethrowAsRuntimeException();
1649 }
1650 }
1651
1652 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001653 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1654 synchronized (mDelegates) {
1655 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1656 handler.getLooper());
1657 try {
1658 mPM.registerMoveCallback(delegate);
1659 } catch (RemoteException e) {
1660 throw e.rethrowAsRuntimeException();
1661 }
1662 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001663 }
1664 }
1665
1666 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001667 public void unregisterMoveCallback(MoveCallback callback) {
1668 synchronized (mDelegates) {
1669 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1670 final MoveCallbackDelegate delegate = i.next();
1671 if (delegate.mCallback == callback) {
1672 try {
1673 mPM.unregisterMoveCallback(delegate);
1674 } catch (RemoteException e) {
1675 throw e.rethrowAsRuntimeException();
1676 }
1677 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001678 }
1679 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001680 }
1681 }
1682
1683 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001684 public int movePackage(String packageName, VolumeInfo vol) {
1685 try {
1686 final String volumeUuid;
1687 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1688 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1689 } else if (vol.isPrimaryPhysical()) {
1690 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1691 } else {
1692 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1693 }
1694
1695 return mPM.movePackage(packageName, volumeUuid);
1696 } catch (RemoteException e) {
1697 throw e.rethrowAsRuntimeException();
1698 }
1699 }
1700
1701 @Override
1702 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001703 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001704 if (app.isInternal()) {
1705 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1706 } else if (app.isExternalAsec()) {
1707 return storage.getPrimaryPhysicalVolume();
1708 } else {
1709 return storage.findVolumeByUuid(app.volumeUuid);
1710 }
1711 }
1712
1713 @Override
1714 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1715 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1716 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001717 final List<VolumeInfo> vols = storage.getVolumes();
1718 final List<VolumeInfo> candidates = new ArrayList<>();
1719 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001720 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001721 candidates.add(vol);
1722 }
1723 }
1724 return candidates;
1725 }
1726
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001727 private static boolean isPackageCandidateVolume(
1728 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1729 final boolean forceAllowOnExternal = Settings.Global.getInt(
1730 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001731 // Private internal is always an option
1732 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1733 return true;
1734 }
1735
1736 // System apps and apps demanding internal storage can't be moved
1737 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001738 if (app.isSystemApp()) {
1739 return false;
1740 }
1741 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001742 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1743 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001744 return false;
1745 }
1746
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001747 // Gotta be able to write there
1748 if (!vol.isMountedWritable()) {
1749 return false;
1750 }
1751
1752 // Moving into an ASEC on public primary is only option internal
1753 if (vol.isPrimaryPhysical()) {
1754 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001755 }
1756
1757 // Otherwise we can move to any private volume
1758 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1759 }
1760
1761 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001762 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001763 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001764 final String volumeUuid;
1765 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1766 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1767 } else if (vol.isPrimaryPhysical()) {
1768 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1769 } else {
1770 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1771 }
1772
1773 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001774 } catch (RemoteException e) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001775 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001776 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001777 }
1778
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001779 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001780 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1781 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1782 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001783 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001784 }
1785
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001786 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001787 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1788 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1789 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1790 final List<VolumeInfo> vols = storage.getVolumes();
1791 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001792 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1793 storage.getPrimaryStorageUuid()) && currentVol != null) {
1794 // TODO: support moving primary physical to emulated volume
1795 candidates.add(currentVol);
1796 } else {
1797 for (VolumeInfo vol : vols) {
1798 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1799 candidates.add(vol);
1800 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001801 }
1802 }
1803 return candidates;
1804 }
1805
1806 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1807 // Private internal is always an option
1808 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1809 return true;
1810 }
1811
1812 // Gotta be able to write there
1813 if (!vol.isMountedWritable()) {
1814 return false;
1815 }
1816
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001817 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001818 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001819 }
1820
1821 @Override
1822 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001823 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001824 }
1825
1826 @Override
1827 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1828 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001829 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001830 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001831 } catch (RemoteException e) {
1832 // Should never happen!
1833 }
1834 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001835
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001836 @Override
1837 public void clearApplicationUserData(String packageName,
1838 IPackageDataObserver observer) {
1839 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001840 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001841 } catch (RemoteException e) {
1842 // Should never happen!
1843 }
1844 }
1845 @Override
1846 public void deleteApplicationCacheFiles(String packageName,
1847 IPackageDataObserver observer) {
1848 try {
1849 mPM.deleteApplicationCacheFiles(packageName, observer);
1850 } catch (RemoteException e) {
1851 // Should never happen!
1852 }
1853 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001854
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001855 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001856 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1857 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001858 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001859 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001860 } catch (RemoteException e) {
1861 // Should never happen!
1862 }
1863 }
1864
1865 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001866 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001867 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001868 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001869 } catch (RemoteException e) {
1870 // Should never happen!
1871 }
1872 }
1873
1874 @Override
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001875 public boolean setPackageSuspendedAsUser(String packageName, boolean suspended, int userId) {
1876 try {
1877 return mPM.setPackageSuspendedAsUser(packageName, suspended, userId);
1878 } catch (RemoteException e) {
1879 // Should never happen!
1880 }
1881 return false;
1882 }
1883
1884 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001885 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001886 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001887 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001888 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001889 } catch (RemoteException e) {
1890 // Should never happen!
1891 }
1892 }
1893 @Override
1894 public void addPackageToPreferred(String packageName) {
1895 try {
1896 mPM.addPackageToPreferred(packageName);
1897 } catch (RemoteException e) {
1898 // Should never happen!
1899 }
1900 }
1901
1902 @Override
1903 public void removePackageFromPreferred(String packageName) {
1904 try {
1905 mPM.removePackageFromPreferred(packageName);
1906 } catch (RemoteException e) {
1907 // Should never happen!
1908 }
1909 }
1910
1911 @Override
1912 public List<PackageInfo> getPreferredPackages(int flags) {
1913 try {
1914 return mPM.getPreferredPackages(flags);
1915 } catch (RemoteException e) {
1916 // Should never happen!
1917 }
1918 return new ArrayList<PackageInfo>();
1919 }
1920
1921 @Override
1922 public void addPreferredActivity(IntentFilter filter,
1923 int match, ComponentName[] set, ComponentName activity) {
1924 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001925 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001926 } catch (RemoteException e) {
1927 // Should never happen!
1928 }
1929 }
1930
1931 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001932 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07001933 ComponentName[] set, ComponentName activity, int userId) {
1934 try {
1935 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001936 } catch (RemoteException e) {
1937 // Should never happen!
1938 }
1939 }
1940
1941 @Override
1942 public void replacePreferredActivity(IntentFilter filter,
1943 int match, ComponentName[] set, ComponentName activity) {
1944 try {
Robin Lee0e27c872015-09-28 14:37:40 +01001945 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001946 } catch (RemoteException e) {
1947 // Should never happen!
1948 }
1949 }
1950
1951 @Override
1952 public void replacePreferredActivityAsUser(IntentFilter filter,
1953 int match, ComponentName[] set, ComponentName activity,
1954 int userId) {
1955 try {
1956 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001957 } catch (RemoteException e) {
1958 // Should never happen!
1959 }
1960 }
1961
1962 @Override
1963 public void clearPackagePreferredActivities(String packageName) {
1964 try {
1965 mPM.clearPackagePreferredActivities(packageName);
1966 } catch (RemoteException e) {
1967 // Should never happen!
1968 }
1969 }
1970
1971 @Override
1972 public int getPreferredActivities(List<IntentFilter> outFilters,
1973 List<ComponentName> outActivities, String packageName) {
1974 try {
1975 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
1976 } catch (RemoteException e) {
1977 // Should never happen!
1978 }
1979 return 0;
1980 }
1981
1982 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07001983 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
1984 try {
1985 return mPM.getHomeActivities(outActivities);
1986 } catch (RemoteException e) {
1987 // Should never happen!
1988 }
1989 return null;
1990 }
1991
1992 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001993 public void setComponentEnabledSetting(ComponentName componentName,
1994 int newState, int flags) {
1995 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001996 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001997 } catch (RemoteException e) {
1998 // Should never happen!
1999 }
2000 }
2001
2002 @Override
2003 public int getComponentEnabledSetting(ComponentName componentName) {
2004 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002005 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002006 } catch (RemoteException e) {
2007 // Should never happen!
2008 }
2009 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2010 }
2011
2012 @Override
2013 public void setApplicationEnabledSetting(String packageName,
2014 int newState, int flags) {
2015 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002016 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002017 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002018 } catch (RemoteException e) {
2019 // Should never happen!
2020 }
2021 }
2022
2023 @Override
2024 public int getApplicationEnabledSetting(String packageName) {
2025 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002026 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002027 } catch (RemoteException e) {
2028 // Should never happen!
2029 }
2030 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2031 }
2032
Amith Yamasani655d0e22013-06-12 14:19:10 -07002033 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002034 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002035 UserHandle user) {
2036 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002037 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002038 user.getIdentifier());
2039 } catch (RemoteException re) {
2040 // Should never happen!
2041 }
2042 return false;
2043 }
2044
2045 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002046 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002047 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002048 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Amith Yamasani655d0e22013-06-12 14:19:10 -07002049 } catch (RemoteException re) {
2050 // Should never happen!
2051 }
2052 return false;
2053 }
2054
dcashmanc6f22492014-08-14 09:54:51 -07002055 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002056 @Override
2057 public KeySet getKeySetByAlias(String packageName, String alias) {
2058 Preconditions.checkNotNull(packageName);
2059 Preconditions.checkNotNull(alias);
dcashmanc6f22492014-08-14 09:54:51 -07002060 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002061 try {
dcashmanc6f22492014-08-14 09:54:51 -07002062 ks = mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002063 } catch (RemoteException e) {
2064 return null;
2065 }
dcashmanc6f22492014-08-14 09:54:51 -07002066 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002067 }
2068
dcashmanc6f22492014-08-14 09:54:51 -07002069 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002070 @Override
2071 public KeySet getSigningKeySet(String packageName) {
2072 Preconditions.checkNotNull(packageName);
dcashmanc6f22492014-08-14 09:54:51 -07002073 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002074 try {
dcashmanc6f22492014-08-14 09:54:51 -07002075 ks = mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002076 } catch (RemoteException e) {
2077 return null;
2078 }
dcashmanc6f22492014-08-14 09:54:51 -07002079 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002080 }
2081
dcashmanc6f22492014-08-14 09:54:51 -07002082 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002083 @Override
2084 public boolean isSignedBy(String packageName, KeySet ks) {
2085 Preconditions.checkNotNull(packageName);
2086 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002087 try {
dcashmanc6f22492014-08-14 09:54:51 -07002088 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002089 } catch (RemoteException e) {
2090 return false;
2091 }
2092 }
2093
dcashmanc6f22492014-08-14 09:54:51 -07002094 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002095 @Override
2096 public boolean isSignedByExactly(String packageName, KeySet ks) {
2097 Preconditions.checkNotNull(packageName);
2098 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002099 try {
dcashmanc6f22492014-08-14 09:54:51 -07002100 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002101 } catch (RemoteException e) {
2102 return false;
2103 }
2104 }
2105
Kenny Root0aaa0d92011-09-12 16:42:55 -07002106 /**
2107 * @hide
2108 */
2109 @Override
2110 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2111 try {
2112 return mPM.getVerifierDeviceIdentity();
2113 } catch (RemoteException e) {
2114 // Should never happen!
2115 }
2116 return null;
2117 }
2118
Jeff Hao9f60c082014-10-28 18:51:07 -07002119 /**
2120 * @hide
2121 */
2122 @Override
2123 public boolean isUpgrade() {
2124 try {
2125 return mPM.isUpgrade();
2126 } catch (RemoteException e) {
2127 return false;
2128 }
2129 }
2130
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002131 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002132 public PackageInstaller getPackageInstaller() {
2133 synchronized (mLock) {
2134 if (mInstaller == null) {
2135 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002136 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002137 mContext.getPackageName(), mContext.getUserId());
2138 } catch (RemoteException e) {
2139 throw e.rethrowAsRuntimeException();
2140 }
2141 }
2142 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002143 }
2144 }
2145
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002146 @Override
2147 public boolean isPackageAvailable(String packageName) {
2148 try {
2149 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2150 } catch (RemoteException e) {
2151 throw e.rethrowAsRuntimeException();
2152 }
2153 }
2154
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002155 /**
2156 * @hide
2157 */
2158 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002159 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2160 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002161 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002162 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002163 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002164 } catch (RemoteException e) {
2165 // Should never happen!
2166 }
2167 }
2168
2169 /**
2170 * @hide
2171 */
2172 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002173 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002174 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002175 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002176 } catch (RemoteException e) {
2177 // Should never happen!
2178 }
2179 }
2180
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002181 /**
2182 * @hide
2183 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002184 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002185 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2186 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2187 return dr;
2188 }
2189 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2190 }
2191
2192 /**
2193 * @hide
2194 */
2195 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002196 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002197 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2198 if (bitmap == null) {
2199 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2200 }
2201 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002202 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002203 Drawable dr = null;
2204 if (itemInfo.packageName != null) {
2205 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2206 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002207 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002208 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002209 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002210 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002211 }
2212
2213 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2214 Rect badgeLocation, boolean tryBadgeInPlace) {
2215 final int badgedWidth = drawable.getIntrinsicWidth();
2216 final int badgedHeight = drawable.getIntrinsicHeight();
2217 final boolean canBadgeInPlace = tryBadgeInPlace
2218 && (drawable instanceof BitmapDrawable)
2219 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2220
2221 final Bitmap bitmap;
2222 if (canBadgeInPlace) {
2223 bitmap = ((BitmapDrawable) drawable).getBitmap();
2224 } else {
2225 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2226 }
2227 Canvas canvas = new Canvas(bitmap);
2228
2229 if (!canBadgeInPlace) {
2230 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2231 drawable.draw(canvas);
2232 }
2233
2234 if (badgeLocation != null) {
2235 if (badgeLocation.left < 0 || badgeLocation.top < 0
2236 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2237 throw new IllegalArgumentException("Badge location " + badgeLocation
2238 + " not in badged drawable bounds "
2239 + new Rect(0, 0, badgedWidth, badgedHeight));
2240 }
2241 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2242
2243 canvas.save();
2244 canvas.translate(badgeLocation.left, badgeLocation.top);
2245 badgeDrawable.draw(canvas);
2246 canvas.restore();
2247 } else {
2248 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2249 badgeDrawable.draw(canvas);
2250 }
2251
2252 if (!canBadgeInPlace) {
2253 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2254
2255 if (drawable instanceof BitmapDrawable) {
2256 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2257 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2258 }
2259
2260 return mergedDrawable;
2261 }
2262
2263 return drawable;
2264 }
2265
2266 private int getBadgeResIdForUser(int userHandle) {
2267 // Return the framework-provided badge.
2268 UserInfo userInfo = getUserIfProfile(userHandle);
2269 if (userInfo != null && userInfo.isManagedProfile()) {
2270 return com.android.internal.R.drawable.ic_corp_icon_badge;
2271 }
2272 return 0;
2273 }
2274
2275 private UserInfo getUserIfProfile(int userHandle) {
Robin Lee0e27c872015-09-28 14:37:40 +01002276 List<UserInfo> userProfiles = getUserManager().getProfiles(mContext.getUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07002277 for (UserInfo user : userProfiles) {
2278 if (user.id == userHandle) {
2279 return user;
2280 }
2281 }
2282 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002283 }
2284
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002285 /** {@hide} */
2286 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2287 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002288 private static final int MSG_CREATED = 1;
2289 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002290
2291 final MoveCallback mCallback;
2292 final Handler mHandler;
2293
2294 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2295 mCallback = callback;
2296 mHandler = new Handler(looper, this);
2297 }
2298
2299 @Override
2300 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002301 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002302 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002303 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002304 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002305 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002306 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002307 }
2308 case MSG_STATUS_CHANGED: {
2309 final SomeArgs args = (SomeArgs) msg.obj;
2310 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2311 args.recycle();
2312 return true;
2313 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002314 }
2315 return false;
2316 }
2317
2318 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002319 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002320 final SomeArgs args = SomeArgs.obtain();
2321 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002322 args.arg2 = extras;
2323 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2324 }
2325
2326 @Override
2327 public void onStatusChanged(int moveId, int status, long estMillis) {
2328 final SomeArgs args = SomeArgs.obtain();
2329 args.argi1 = moveId;
2330 args.argi2 = status;
2331 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002332 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002333 }
2334 }
2335
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002336 private final ContextImpl mContext;
2337 private final IPackageManager mPM;
2338
2339 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002340 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2341 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2342 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2343 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002344
2345 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2346 mPermissionListeners = new ArrayMap<>();
2347
2348 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2349 implements Handler.Callback{
2350 private static final int MSG_PERMISSIONS_CHANGED = 1;
2351
2352 private final OnPermissionsChangedListener mListener;
2353 private final Handler mHandler;
2354
2355
2356 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2357 Looper looper) {
2358 mListener = listener;
2359 mHandler = new Handler(looper, this);
2360 }
2361
2362 @Override
2363 public void onPermissionsChanged(int uid) {
2364 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2365 }
2366
2367 @Override
2368 public boolean handleMessage(Message msg) {
2369 switch (msg.what) {
2370 case MSG_PERMISSIONS_CHANGED: {
2371 final int uid = msg.arg1;
2372 mListener.onPermissionsChanged(uid);
2373 return true;
2374 }
2375 }
2376 return false;
2377 }
2378 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002379}