blob: 220fb607289ad6d8868a65d1405622107fa383d4 [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 Sharkeycd654482016-01-08 17:42:11 -0700224 int[] gids = mPM.getPackageGids(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 Sharkeycd654482016-01-08 17:42:11 -0700249 int uid = mPM.getPackageUid(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 {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700317 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
318 }
319
320 @Override
321 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
322 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800323 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700324 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800325 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100326 // This is a temporary hack. Callers must use
327 // createPackageContext(packageName).getApplicationInfo() to
328 // get the right paths.
329 maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800330 return ai;
331 }
332 } catch (RemoteException e) {
333 throw new RuntimeException("Package manager has died", e);
334 }
335
336 throw new NameNotFoundException(packageName);
337 }
338
Narayan Kamathcaa71192014-07-16 11:06:43 +0100339 private static void maybeAdjustApplicationInfo(ApplicationInfo info) {
340 // If we're dealing with a multi-arch application that has both
341 // 32 and 64 bit shared libraries, we might need to choose the secondary
342 // depending on what the current runtime's instruction set is.
343 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
344 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500345
346 // Get the instruction set that the libraries of secondary Abi is supported.
347 // In presence of a native bridge this might be different than the one secondary Abi used.
348 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
349 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
350 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100351
352 // If the runtimeIsa is the same as the primary isa, then we do nothing.
353 // Everything will be set up correctly because info.nativeLibraryDir will
354 // correspond to the right ISA.
355 if (runtimeIsa.equals(secondaryIsa)) {
356 info.nativeLibraryDir = info.secondaryNativeLibraryDir;
357 }
358 }
359 }
360
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800361 @Override
362 public ActivityInfo getActivityInfo(ComponentName className, int flags)
363 throws NameNotFoundException {
364 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700365 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800366 if (ai != null) {
367 return ai;
368 }
369 } catch (RemoteException e) {
370 throw new RuntimeException("Package manager has died", e);
371 }
372
373 throw new NameNotFoundException(className.toString());
374 }
375
376 @Override
377 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
378 throws NameNotFoundException {
379 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700380 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800381 if (ai != null) {
382 return ai;
383 }
384 } catch (RemoteException e) {
385 throw new RuntimeException("Package manager has died", e);
386 }
387
388 throw new NameNotFoundException(className.toString());
389 }
390
391 @Override
392 public ServiceInfo getServiceInfo(ComponentName className, int flags)
393 throws NameNotFoundException {
394 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700395 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800396 if (si != null) {
397 return si;
398 }
399 } catch (RemoteException e) {
400 throw new RuntimeException("Package manager has died", e);
401 }
402
403 throw new NameNotFoundException(className.toString());
404 }
405
406 @Override
407 public ProviderInfo getProviderInfo(ComponentName className, int flags)
408 throws NameNotFoundException {
409 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700410 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800411 if (pi != null) {
412 return pi;
413 }
414 } catch (RemoteException e) {
415 throw new RuntimeException("Package manager has died", e);
416 }
417
418 throw new NameNotFoundException(className.toString());
419 }
420
421 @Override
422 public String[] getSystemSharedLibraryNames() {
423 try {
424 return mPM.getSystemSharedLibraryNames();
425 } catch (RemoteException e) {
426 throw new RuntimeException("Package manager has died", e);
427 }
428 }
429
430 @Override
431 public FeatureInfo[] getSystemAvailableFeatures() {
432 try {
433 return mPM.getSystemAvailableFeatures();
434 } catch (RemoteException e) {
435 throw new RuntimeException("Package manager has died", e);
436 }
437 }
438
439 @Override
440 public boolean hasSystemFeature(String name) {
441 try {
442 return mPM.hasSystemFeature(name);
443 } catch (RemoteException e) {
444 throw new RuntimeException("Package manager has died", e);
445 }
446 }
447
448 @Override
449 public int checkPermission(String permName, String pkgName) {
450 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800451 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800452 } catch (RemoteException e) {
453 throw new RuntimeException("Package manager has died", e);
454 }
455 }
456
457 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700458 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
459 try {
460 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
461 } catch (RemoteException e) {
462 throw new RuntimeException("Package manager has died", e);
463 }
464 }
465
Svet Ganovf1b7f202015-07-29 08:33:42 -0700466 /**
467 * @hide
468 */
469 @Override
470 public String getPermissionControllerPackageName() {
471 synchronized (mLock) {
472 if (mPermissionsControllerPackageName == null) {
473 try {
474 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
475 } catch (RemoteException e) {
476 throw new RuntimeException("Package manager has died", e);
477 }
478 }
479 return mPermissionsControllerPackageName;
480 }
481 }
482
Svet Ganovad3b2972015-07-07 22:49:17 -0700483 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800484 public boolean addPermission(PermissionInfo info) {
485 try {
486 return mPM.addPermission(info);
487 } catch (RemoteException e) {
488 throw new RuntimeException("Package manager has died", e);
489 }
490 }
491
492 @Override
493 public boolean addPermissionAsync(PermissionInfo info) {
494 try {
495 return mPM.addPermissionAsync(info);
496 } catch (RemoteException e) {
497 throw new RuntimeException("Package manager has died", e);
498 }
499 }
500
501 @Override
502 public void removePermission(String name) {
503 try {
504 mPM.removePermission(name);
505 } catch (RemoteException e) {
506 throw new RuntimeException("Package manager has died", e);
507 }
508 }
509
510 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700511 public void grantRuntimePermission(String packageName, String permissionName,
512 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800513 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700514 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800515 } catch (RemoteException e) {
516 throw new RuntimeException("Package manager has died", e);
517 }
518 }
519
520 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700521 public void revokeRuntimePermission(String packageName, String permissionName,
522 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800523 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700524 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
525 } catch (RemoteException e) {
526 throw new RuntimeException("Package manager has died", e);
527 }
528 }
529
530 @Override
531 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
532 try {
533 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
534 } catch (RemoteException e) {
535 throw new RuntimeException("Package manager has died", e);
536 }
537 }
538
539 @Override
540 public void updatePermissionFlags(String permissionName, String packageName,
541 int flagMask, int flagValues, UserHandle user) {
542 try {
543 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
544 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800545 } catch (RemoteException e) {
546 throw new RuntimeException("Package manager has died", e);
547 }
548 }
549
550 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700551 public boolean shouldShowRequestPermissionRationale(String permission) {
552 try {
553 return mPM.shouldShowRequestPermissionRationale(permission,
554 mContext.getPackageName(), mContext.getUserId());
555 } catch (RemoteException e) {
556 throw new RuntimeException("Package manager has died", e);
557 }
558 }
559
560 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800561 public int checkSignatures(String pkg1, String pkg2) {
562 try {
563 return mPM.checkSignatures(pkg1, pkg2);
564 } catch (RemoteException e) {
565 throw new RuntimeException("Package manager has died", e);
566 }
567 }
568
569 @Override
570 public int checkSignatures(int uid1, int uid2) {
571 try {
572 return mPM.checkUidSignatures(uid1, uid2);
573 } catch (RemoteException e) {
574 throw new RuntimeException("Package manager has died", e);
575 }
576 }
577
578 @Override
579 public String[] getPackagesForUid(int uid) {
580 try {
581 return mPM.getPackagesForUid(uid);
582 } catch (RemoteException e) {
583 throw new RuntimeException("Package manager has died", e);
584 }
585 }
586
587 @Override
588 public String getNameForUid(int uid) {
589 try {
590 return mPM.getNameForUid(uid);
591 } catch (RemoteException e) {
592 throw new RuntimeException("Package manager has died", e);
593 }
594 }
595
596 @Override
597 public int getUidForSharedUser(String sharedUserName)
598 throws NameNotFoundException {
599 try {
600 int uid = mPM.getUidForSharedUser(sharedUserName);
601 if(uid != -1) {
602 return uid;
603 }
604 } catch (RemoteException e) {
605 throw new RuntimeException("Package manager has died", e);
606 }
607 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
608 }
609
Kenny Roote6cd0c72011-05-19 12:48:14 -0700610 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800611 @Override
612 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700613 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700614 }
615
616 /** @hide */
617 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700618 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800619 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800620 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
621 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800622 } catch (RemoteException e) {
623 throw new RuntimeException("Package manager has died", e);
624 }
625 }
626
Kenny Roote6cd0c72011-05-19 12:48:14 -0700627 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800628 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800629 public List<PackageInfo> getPackagesHoldingPermissions(
630 String[] permissions, int flags) {
631 final int userId = mContext.getUserId();
632 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800633 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
634 permissions, flags, userId);
635 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800636 } catch (RemoteException e) {
637 throw new RuntimeException("Package manager has died", e);
638 }
639 }
640
641 @SuppressWarnings("unchecked")
642 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800643 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700644 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800645 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800646 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
647 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800648 } catch (RemoteException e) {
649 throw new RuntimeException("Package manager has died", e);
650 }
651 }
652
Svet Ganov2acf0632015-11-24 19:10:59 -0800653 /** @hide */
654 @SuppressWarnings("unchecked")
655 @Override
656 public List<EphemeralApplicationInfo> getEphemeralApplications() {
657 try {
658 ParceledListSlice<EphemeralApplicationInfo> slice =
659 mPM.getEphemeralApplications(mContext.getUserId());
660 if (slice != null) {
661 return slice.getList();
662 }
663 return Collections.emptyList();
664 } catch (RemoteException e) {
665 throw new RuntimeException("Package manager has died", e);
666 }
667 }
668
669 /** @hide */
670 @Override
671 public Drawable getEphemeralApplicationIcon(String packageName) {
672 try {
673 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
674 packageName, mContext.getUserId());
675 if (bitmap != null) {
676 return new BitmapDrawable(null, bitmap);
677 }
678 return null;
679 } catch (RemoteException e) {
680 throw new RuntimeException("Package manager has died", e);
681 }
682 }
683
684 @Override
685 public boolean isEphemeralApplication() {
686 try {
687 return mPM.isEphemeralApplication(
688 mContext.getPackageName(), mContext.getUserId());
689 } catch (RemoteException e) {
690 Log.e(TAG, "System server is dead", e);
691 }
692 return false;
693 }
694
695 @Override
696 public int getEphemeralCookieMaxSizeBytes() {
697 return Settings.Global.getInt(mContext.getContentResolver(),
698 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
699 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
700 }
701
702 @Override
703 public @NonNull byte[] getEphemeralCookie() {
704 try {
705 final byte[] cookie = mPM.getEphemeralApplicationCookie(
706 mContext.getPackageName(), mContext.getUserId());
707 if (cookie != null) {
708 return cookie;
709 }
710 } catch (RemoteException e) {
711 Log.e(TAG, "System server is dead", e);
712 }
713 return EmptyArray.BYTE;
714 }
715
716 @Override
717 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
718 try {
719 return mPM.setEphemeralApplicationCookie(
720 mContext.getPackageName(), cookie, mContext.getUserId());
721 } catch (RemoteException e) {
722 Log.e(TAG, "System server is dead", e);
723 }
724 return false;
725 }
726
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800727 @Override
728 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700729 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700730 }
731
732 @Override
733 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800734 try {
735 return mPM.resolveIntent(
736 intent,
737 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700738 flags,
739 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800740 } catch (RemoteException e) {
741 throw new RuntimeException("Package manager has died", e);
742 }
743 }
744
745 @Override
746 public List<ResolveInfo> queryIntentActivities(Intent intent,
747 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700748 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700749 }
750
751 /** @hide Same as above but for a specific user */
752 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700753 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700754 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800755 try {
756 return mPM.queryIntentActivities(
757 intent,
758 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700759 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700760 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800761 } catch (RemoteException e) {
762 throw new RuntimeException("Package manager has died", e);
763 }
764 }
765
766 @Override
767 public List<ResolveInfo> queryIntentActivityOptions(
768 ComponentName caller, Intent[] specifics, Intent intent,
769 int flags) {
770 final ContentResolver resolver = mContext.getContentResolver();
771
772 String[] specificTypes = null;
773 if (specifics != null) {
774 final int N = specifics.length;
775 for (int i=0; i<N; i++) {
776 Intent sp = specifics[i];
777 if (sp != null) {
778 String t = sp.resolveTypeIfNeeded(resolver);
779 if (t != null) {
780 if (specificTypes == null) {
781 specificTypes = new String[N];
782 }
783 specificTypes[i] = t;
784 }
785 }
786 }
787 }
788
789 try {
790 return mPM.queryIntentActivityOptions(caller, specifics,
791 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700792 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800793 } catch (RemoteException e) {
794 throw new RuntimeException("Package manager has died", e);
795 }
796 }
797
Amith Yamasanif203aee2012-08-29 18:41:53 -0700798 /**
799 * @hide
800 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800801 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700802 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800803 try {
804 return mPM.queryIntentReceivers(
805 intent,
806 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700807 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -0700808 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800809 } catch (RemoteException e) {
810 throw new RuntimeException("Package manager has died", e);
811 }
812 }
813
814 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700815 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700816 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700817 }
818
819 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800820 public ResolveInfo resolveService(Intent intent, int flags) {
821 try {
822 return mPM.resolveService(
823 intent,
824 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700825 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700826 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800827 } catch (RemoteException e) {
828 throw new RuntimeException("Package manager has died", e);
829 }
830 }
831
832 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700833 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800834 try {
835 return mPM.queryIntentServices(
836 intent,
837 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700838 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700839 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800840 } catch (RemoteException e) {
841 throw new RuntimeException("Package manager has died", e);
842 }
843 }
844
845 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700846 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700847 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700848 }
849
850 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700851 public List<ResolveInfo> queryIntentContentProvidersAsUser(
852 Intent intent, int flags, int userId) {
853 try {
854 return mPM.queryIntentContentProviders(intent,
855 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
856 } catch (RemoteException e) {
857 throw new RuntimeException("Package manager has died", e);
858 }
859 }
860
861 @Override
862 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
863 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
864 }
865
866 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100867 public ProviderInfo resolveContentProvider(String name, int flags) {
868 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
869 }
870
871 /** @hide **/
872 @Override
873 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800874 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100875 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800876 } catch (RemoteException e) {
877 throw new RuntimeException("Package manager has died", e);
878 }
879 }
880
881 @Override
882 public List<ProviderInfo> queryContentProviders(String processName,
883 int uid, int flags) {
884 try {
Dianne Hackborn28ec27c2015-08-03 15:28:28 -0700885 ParceledListSlice<ProviderInfo> slice
886 = mPM.queryContentProviders(processName, uid, flags);
887 return slice != null ? slice.getList() : null;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800888 } catch (RemoteException e) {
889 throw new RuntimeException("Package manager has died", e);
890 }
891 }
892
893 @Override
894 public InstrumentationInfo getInstrumentationInfo(
895 ComponentName className, int flags)
896 throws NameNotFoundException {
897 try {
898 InstrumentationInfo ii = mPM.getInstrumentationInfo(
899 className, flags);
900 if (ii != null) {
901 return ii;
902 }
903 } catch (RemoteException e) {
904 throw new RuntimeException("Package manager has died", e);
905 }
906
907 throw new NameNotFoundException(className.toString());
908 }
909
910 @Override
911 public List<InstrumentationInfo> queryInstrumentation(
912 String targetPackage, int flags) {
913 try {
914 return mPM.queryInstrumentation(targetPackage, flags);
915 } catch (RemoteException e) {
916 throw new RuntimeException("Package manager has died", e);
917 }
918 }
919
Alan Viveretteecd585a2015-04-13 10:32:51 -0700920 @Nullable
921 @Override
922 public Drawable getDrawable(String packageName, @DrawableRes int resId,
923 @Nullable ApplicationInfo appInfo) {
924 final ResourceName name = new ResourceName(packageName, resId);
925 final Drawable cachedIcon = getCachedIcon(name);
926 if (cachedIcon != null) {
927 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800928 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700929
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800930 if (appInfo == null) {
931 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700932 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800933 } catch (NameNotFoundException e) {
934 return null;
935 }
936 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700937
938 if (resId != 0) {
939 try {
940 final Resources r = getResourcesForApplication(appInfo);
941 final Drawable dr = r.getDrawable(resId, null);
942 if (dr != null) {
943 putCachedIcon(name, dr);
944 }
945
946 if (false) {
947 RuntimeException e = new RuntimeException("here");
948 e.fillInStackTrace();
949 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
950 + " from package " + packageName
951 + ": app scale=" + r.getCompatibilityInfo().applicationScale
952 + ", caller scale=" + mContext.getResources()
953 .getCompatibilityInfo().applicationScale,
954 e);
955 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100956 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700957 Log.v(TAG, "Getting drawable 0x"
958 + Integer.toHexString(resId) + " from " + r
959 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100960 }
961 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700962 } catch (NameNotFoundException e) {
963 Log.w("PackageManager", "Failure retrieving resources for "
964 + appInfo.packageName);
965 } catch (Resources.NotFoundException e) {
966 Log.w("PackageManager", "Failure retrieving resources for "
967 + appInfo.packageName + ": " + e.getMessage());
968 } catch (Exception e) {
969 // If an exception was thrown, fall through to return
970 // default icon.
971 Log.w("PackageManager", "Failure retrieving icon 0x"
972 + Integer.toHexString(resId) + " in package "
973 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800974 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800975 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700976
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800977 return null;
978 }
979
980 @Override public Drawable getActivityIcon(ComponentName activityName)
981 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700982 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800983 }
984
985 @Override public Drawable getActivityIcon(Intent intent)
986 throws NameNotFoundException {
987 if (intent.getComponent() != null) {
988 return getActivityIcon(intent.getComponent());
989 }
990
991 ResolveInfo info = resolveActivity(
992 intent, PackageManager.MATCH_DEFAULT_ONLY);
993 if (info != null) {
994 return info.activityInfo.loadIcon(this);
995 }
996
Romain Guy39fe17c2011-11-30 10:34:07 -0800997 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800998 }
999
1000 @Override public Drawable getDefaultActivityIcon() {
1001 return Resources.getSystem().getDrawable(
1002 com.android.internal.R.drawable.sym_def_app_icon);
1003 }
1004
1005 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1006 return info.loadIcon(this);
1007 }
1008
1009 @Override public Drawable getApplicationIcon(String packageName)
1010 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001011 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001012 }
1013
1014 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001015 public Drawable getActivityBanner(ComponentName activityName)
1016 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001017 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001018 }
1019
1020 @Override
1021 public Drawable getActivityBanner(Intent intent)
1022 throws NameNotFoundException {
1023 if (intent.getComponent() != null) {
1024 return getActivityBanner(intent.getComponent());
1025 }
1026
1027 ResolveInfo info = resolveActivity(
1028 intent, PackageManager.MATCH_DEFAULT_ONLY);
1029 if (info != null) {
1030 return info.activityInfo.loadBanner(this);
1031 }
1032
1033 throw new NameNotFoundException(intent.toUri(0));
1034 }
1035
1036 @Override
1037 public Drawable getApplicationBanner(ApplicationInfo info) {
1038 return info.loadBanner(this);
1039 }
1040
1041 @Override
1042 public Drawable getApplicationBanner(String packageName)
1043 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001044 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001045 }
1046
1047 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001048 public Drawable getActivityLogo(ComponentName activityName)
1049 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001050 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001051 }
1052
1053 @Override
1054 public Drawable getActivityLogo(Intent intent)
1055 throws NameNotFoundException {
1056 if (intent.getComponent() != null) {
1057 return getActivityLogo(intent.getComponent());
1058 }
1059
1060 ResolveInfo info = resolveActivity(
1061 intent, PackageManager.MATCH_DEFAULT_ONLY);
1062 if (info != null) {
1063 return info.activityInfo.loadLogo(this);
1064 }
1065
1066 throw new NameNotFoundException(intent.toUri(0));
1067 }
1068
1069 @Override
1070 public Drawable getApplicationLogo(ApplicationInfo info) {
1071 return info.loadLogo(this);
1072 }
1073
1074 @Override
1075 public Drawable getApplicationLogo(String packageName)
1076 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001077 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001078 }
1079
Svetoslavc7d62f02014-09-04 15:39:54 -07001080 @Override
1081 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1082 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1083 if (badgeResId == 0) {
1084 return icon;
1085 }
1086 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1087 return getBadgedDrawable(icon, badgeIcon, null, true);
1088 }
1089
1090 @Override
1091 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1092 Rect badgeLocation, int badgeDensity) {
1093 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1094 if (badgeDrawable == null) {
1095 return drawable;
1096 }
1097 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1098 }
1099
1100 @Override
1101 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Selim Cineke6ff9462016-01-15 15:07:06 -08001102 return getManagedProfileIconForDensity(user, density,
1103 com.android.internal.R.drawable.ic_corp_badge);
1104 }
1105
1106 @Override
1107 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
1108 return getManagedProfileIconForDensity(user, density,
1109 com.android.internal.R.drawable.ic_corp_badge_no_background);
1110 }
1111
1112 private Drawable getManagedProfileIconForDensity(UserHandle user, int density,
1113 int drawableId) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001114 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1115 if (userInfo != null && userInfo.isManagedProfile()) {
1116 if (density <= 0) {
1117 density = mContext.getResources().getDisplayMetrics().densityDpi;
1118 }
Selim Cineke6ff9462016-01-15 15:07:06 -08001119 return Resources.getSystem().getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001120 }
1121 return null;
1122 }
1123
1124 @Override
1125 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
1126 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1127 if (userInfo != null && userInfo.isManagedProfile()) {
1128 return Resources.getSystem().getString(
1129 com.android.internal.R.string.managed_profile_label_badge, label);
1130 }
1131 return label;
1132 }
1133
Alan Viveretteecd585a2015-04-13 10:32:51 -07001134 @Override
1135 public Resources getResourcesForActivity(ComponentName activityName)
1136 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001137 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001138 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001139 }
1140
Alan Viveretteecd585a2015-04-13 10:32:51 -07001141 @Override
1142 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1143 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001144 if (app.packageName.equals("system")) {
1145 return mContext.mMainThread.getSystemContext().getResources();
1146 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001147 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001148 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001149 sameUid ? app.sourceDir : app.publicSourceDir,
1150 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001151 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1152 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001153 if (r != null) {
1154 return r;
1155 }
1156 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1157 }
1158
Alan Viveretteecd585a2015-04-13 10:32:51 -07001159 @Override
1160 public Resources getResourcesForApplication(String appPackageName)
1161 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001162 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001163 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001164 }
1165
Amith Yamasani98edc952012-09-25 14:09:27 -07001166 /** @hide */
1167 @Override
1168 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1169 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001170 if (userId < 0) {
1171 throw new IllegalArgumentException(
1172 "Call does not support special user #" + userId);
1173 }
1174 if ("system".equals(appPackageName)) {
1175 return mContext.mMainThread.getSystemContext().getResources();
1176 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001177 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001178 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001179 if (ai != null) {
1180 return getResourcesForApplication(ai);
1181 }
1182 } catch (RemoteException e) {
1183 throw new RuntimeException("Package manager has died", e);
1184 }
1185 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1186 }
1187
Jeff Sharkeycd654482016-01-08 17:42:11 -07001188 volatile int mCachedSafeMode = -1;
1189
1190 @Override
1191 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001192 try {
1193 if (mCachedSafeMode < 0) {
1194 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1195 }
1196 return mCachedSafeMode != 0;
1197 } catch (RemoteException e) {
1198 throw new RuntimeException("Package manager has died", e);
1199 }
1200 }
1201
Svetoslavf7c06eb2015-06-10 18:43:22 -07001202 @Override
1203 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1204 synchronized (mPermissionListeners) {
1205 if (mPermissionListeners.get(listener) != null) {
1206 return;
1207 }
1208 OnPermissionsChangeListenerDelegate delegate =
1209 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1210 try {
1211 mPM.addOnPermissionsChangeListener(delegate);
1212 mPermissionListeners.put(listener, delegate);
1213 } catch (RemoteException e) {
1214 throw new RuntimeException("Package manager has died", e);
1215 }
1216 }
1217 }
1218
1219 @Override
1220 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1221 synchronized (mPermissionListeners) {
1222 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1223 if (delegate != null) {
1224 try {
1225 mPM.removeOnPermissionsChangeListener(delegate);
1226 mPermissionListeners.remove(listener);
1227 } catch (RemoteException e) {
1228 throw new RuntimeException("Package manager has died", e);
1229 }
1230 }
1231 }
1232 }
1233
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001234 static void configurationChanged() {
1235 synchronized (sSync) {
1236 sIconCache.clear();
1237 sStringCache.clear();
1238 }
1239 }
1240
1241 ApplicationPackageManager(ContextImpl context,
1242 IPackageManager pm) {
1243 mContext = context;
1244 mPM = pm;
1245 }
1246
Alan Viveretteecd585a2015-04-13 10:32:51 -07001247 @Nullable
1248 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001249 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001250 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001251 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1252 + name + ": " + wr);
1253 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001254 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001255 if (state != null) {
1256 if (DEBUG_ICONS) {
1257 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1258 }
1259 // Note: It's okay here to not use the newDrawable(Resources) variant
1260 // of the API. The ConstantState comes from a drawable that was
1261 // originally created by passing the proper app Resources instance
1262 // which means the state should already contain the proper
1263 // resources specific information (like density.) See
1264 // BitmapDrawable.BitmapState for instance.
1265 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001266 }
1267 // our entry has been purged
1268 sIconCache.remove(name);
1269 }
1270 }
1271 return null;
1272 }
1273
Alan Viveretteecd585a2015-04-13 10:32:51 -07001274 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001275 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001276 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001277 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001278 }
1279 }
1280
Romain Guy39fe17c2011-11-30 10:34:07 -08001281 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001282 boolean immediateGc = false;
1283 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1284 immediateGc = true;
1285 }
1286 if (pkgList != null && (pkgList.length > 0)) {
1287 boolean needCleanup = false;
1288 for (String ssp : pkgList) {
1289 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001290 for (int i=sIconCache.size()-1; i>=0; i--) {
1291 ResourceName nm = sIconCache.keyAt(i);
1292 if (nm.packageName.equals(ssp)) {
1293 //Log.i(TAG, "Removing cached drawable for " + nm);
1294 sIconCache.removeAt(i);
1295 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001296 }
1297 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001298 for (int i=sStringCache.size()-1; i>=0; i--) {
1299 ResourceName nm = sStringCache.keyAt(i);
1300 if (nm.packageName.equals(ssp)) {
1301 //Log.i(TAG, "Removing cached string for " + nm);
1302 sStringCache.removeAt(i);
1303 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001304 }
1305 }
1306 }
1307 }
1308 if (needCleanup || hasPkgInfo) {
1309 if (immediateGc) {
1310 // Schedule an immediate gc.
1311 Runtime.getRuntime().gc();
1312 } else {
1313 ActivityThread.currentActivityThread().scheduleGcIdler();
1314 }
1315 }
1316 }
1317 }
1318
1319 private static final class ResourceName {
1320 final String packageName;
1321 final int iconId;
1322
1323 ResourceName(String _packageName, int _iconId) {
1324 packageName = _packageName;
1325 iconId = _iconId;
1326 }
1327
1328 ResourceName(ApplicationInfo aInfo, int _iconId) {
1329 this(aInfo.packageName, _iconId);
1330 }
1331
1332 ResourceName(ComponentInfo cInfo, int _iconId) {
1333 this(cInfo.applicationInfo.packageName, _iconId);
1334 }
1335
1336 ResourceName(ResolveInfo rInfo, int _iconId) {
1337 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1338 }
1339
1340 @Override
1341 public boolean equals(Object o) {
1342 if (this == o) return true;
1343 if (o == null || getClass() != o.getClass()) return false;
1344
1345 ResourceName that = (ResourceName) o;
1346
1347 if (iconId != that.iconId) return false;
1348 return !(packageName != null ?
1349 !packageName.equals(that.packageName) : that.packageName != null);
1350
1351 }
1352
1353 @Override
1354 public int hashCode() {
1355 int result;
1356 result = packageName.hashCode();
1357 result = 31 * result + iconId;
1358 return result;
1359 }
1360
1361 @Override
1362 public String toString() {
1363 return "{ResourceName " + packageName + " / " + iconId + "}";
1364 }
1365 }
1366
1367 private CharSequence getCachedString(ResourceName name) {
1368 synchronized (sSync) {
1369 WeakReference<CharSequence> wr = sStringCache.get(name);
1370 if (wr != null) { // we have the activity
1371 CharSequence cs = wr.get();
1372 if (cs != null) {
1373 return cs;
1374 }
1375 // our entry has been purged
1376 sStringCache.remove(name);
1377 }
1378 }
1379 return null;
1380 }
1381
1382 private void putCachedString(ResourceName name, CharSequence cs) {
1383 synchronized (sSync) {
1384 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1385 }
1386 }
1387
1388 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001389 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001390 ApplicationInfo appInfo) {
1391 ResourceName name = new ResourceName(packageName, resid);
1392 CharSequence text = getCachedString(name);
1393 if (text != null) {
1394 return text;
1395 }
1396 if (appInfo == null) {
1397 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001398 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001399 } catch (NameNotFoundException e) {
1400 return null;
1401 }
1402 }
1403 try {
1404 Resources r = getResourcesForApplication(appInfo);
1405 text = r.getText(resid);
1406 putCachedString(name, text);
1407 return text;
1408 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001409 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001410 + appInfo.packageName);
1411 } catch (RuntimeException e) {
1412 // If an exception was thrown, fall through to return
1413 // default icon.
1414 Log.w("PackageManager", "Failure retrieving text 0x"
1415 + Integer.toHexString(resid) + " in package "
1416 + packageName, e);
1417 }
1418 return null;
1419 }
1420
1421 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001422 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001423 ApplicationInfo appInfo) {
1424 if (appInfo == null) {
1425 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001426 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001427 } catch (NameNotFoundException e) {
1428 return null;
1429 }
1430 }
1431 try {
1432 Resources r = getResourcesForApplication(appInfo);
1433 return r.getXml(resid);
1434 } catch (RuntimeException e) {
1435 // If an exception was thrown, fall through to return
1436 // default icon.
1437 Log.w("PackageManager", "Failure retrieving xml 0x"
1438 + Integer.toHexString(resid) + " in package "
1439 + packageName, e);
1440 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001441 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001442 + appInfo.packageName);
1443 }
1444 return null;
1445 }
1446
1447 @Override
1448 public CharSequence getApplicationLabel(ApplicationInfo info) {
1449 return info.loadLabel(this);
1450 }
1451
1452 @Override
1453 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1454 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001455 final VerificationParams verificationParams = new VerificationParams(null, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001456 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001457 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001458 installerPackageName, verificationParams, null, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001459 }
1460
1461 @Override
Kenny Root5ab21572011-07-27 11:11:19 -07001462 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1463 int flags, String installerPackageName, Uri verificationURI,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001464 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001465 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001466 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001467 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001468 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Kenny Root5ab21572011-07-27 11:11:19 -07001469 }
1470
1471 @Override
John Spurlock8a985d22014-02-25 09:40:05 -05001472 public void installPackageWithVerificationAndEncryption(Uri packageURI,
rich cannings706e8ba2012-08-20 13:20:14 -07001473 IPackageInstallObserver observer, int flags, String installerPackageName,
1474 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001475 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001476 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001477 }
1478
Christopher Tatef1977b42014-03-24 16:25:51 -07001479 @Override
1480 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1481 int flags, String installerPackageName) {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001482 installPackageAsUser(packageURI, observer, flags, installerPackageName,
Robin Lee0e27c872015-09-28 14:37:40 +01001483 mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001484 }
1485
1486 @Override
1487 public void installPackageAsUser(Uri packageURI, PackageInstallObserver observer, int flags,
1488 String installerPackageName, int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001489 final VerificationParams verificationParams = new VerificationParams(null, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001490 null, VerificationParams.NO_UID);
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001491 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null,
1492 userId);
Christopher Tatef1977b42014-03-24 16:25:51 -07001493 }
1494
1495 @Override
1496 public void installPackageWithVerification(Uri packageURI,
1497 PackageInstallObserver observer, int flags, String installerPackageName,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001498 Uri verificationURI,
Christopher Tatef1977b42014-03-24 16:25:51 -07001499 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001500 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001501 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001502 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001503 encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001504 }
1505
1506 @Override
1507 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1508 PackageInstallObserver observer, int flags, String installerPackageName,
1509 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001510 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001511 encryptionParams, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001512 }
1513
1514 private void installCommon(Uri packageURI,
1515 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001516 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams,
1517 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001518 if (!"file".equals(packageURI.getScheme())) {
1519 throw new UnsupportedOperationException("Only file:// URIs are supported");
1520 }
1521 if (encryptionParams != null) {
1522 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1523 }
1524
1525 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001526 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001527 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
1528 verificationParams, null, userId);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001529 } catch (RemoteException ignored) {
rich cannings706e8ba2012-08-20 13:20:14 -07001530 }
1531 }
1532
1533 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001534 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001535 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001536 }
1537
1538 @Override
1539 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001540 throws NameNotFoundException {
1541 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001542 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001543 if (res == INSTALL_FAILED_INVALID_URI) {
1544 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1545 }
1546 return res;
1547 } catch (RemoteException e) {
1548 // Should never happen!
1549 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1550 }
1551 }
1552
1553 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001554 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001555 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001556 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001557 } catch (RemoteException e) {
1558 // Should never happen!
1559 }
1560 }
1561
1562 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001563 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1564 long millisecondsToDelay) {
1565 try {
1566 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1567 } catch (RemoteException e) {
1568 // Should never happen!
1569 }
1570 }
1571
1572 @Override
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001573 public void verifyIntentFilter(int id, int verificationCode, List<String> outFailedDomains) {
1574 try {
1575 mPM.verifyIntentFilter(id, verificationCode, outFailedDomains);
1576 } catch (RemoteException e) {
1577 // Should never happen!
1578 }
1579 }
1580
1581 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001582 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001583 try {
1584 return mPM.getIntentVerificationStatus(packageName, userId);
1585 } catch (RemoteException e) {
1586 // Should never happen!
1587 return PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1588 }
1589 }
1590
1591 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001592 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001593 try {
1594 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1595 } catch (RemoteException e) {
1596 // Should never happen!
1597 return false;
1598 }
1599 }
1600
1601 @Override
1602 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1603 try {
1604 return mPM.getIntentFilterVerifications(packageName);
1605 } catch (RemoteException e) {
1606 // Should never happen!
1607 return null;
1608 }
1609 }
1610
1611 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001612 public List<IntentFilter> getAllIntentFilters(String packageName) {
1613 try {
1614 return mPM.getAllIntentFilters(packageName);
1615 } catch (RemoteException e) {
1616 // Should never happen!
1617 return null;
1618 }
1619 }
1620
1621 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001622 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001623 try {
1624 return mPM.getDefaultBrowserPackageName(userId);
1625 } catch (RemoteException e) {
1626 // Should never happen!
1627 return null;
1628 }
1629 }
1630
1631 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001632 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001633 try {
1634 return mPM.setDefaultBrowserPackageName(packageName, userId);
1635 } catch (RemoteException e) {
1636 // Should never happen!
1637 return false;
1638 }
1639 }
1640
1641 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001642 public void setInstallerPackageName(String targetPackage,
1643 String installerPackageName) {
1644 try {
1645 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1646 } catch (RemoteException e) {
1647 // Should never happen!
1648 }
1649 }
1650
1651 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001652 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001653 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001654 return mPM.getInstallerPackageName(packageName);
1655 } catch (RemoteException e) {
1656 // Should never happen!
1657 }
1658 return null;
1659 }
1660
1661 @Override
1662 public int getMoveStatus(int moveId) {
1663 try {
1664 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001665 } catch (RemoteException e) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001666 throw e.rethrowAsRuntimeException();
1667 }
1668 }
1669
1670 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001671 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1672 synchronized (mDelegates) {
1673 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1674 handler.getLooper());
1675 try {
1676 mPM.registerMoveCallback(delegate);
1677 } catch (RemoteException e) {
1678 throw e.rethrowAsRuntimeException();
1679 }
1680 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001681 }
1682 }
1683
1684 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001685 public void unregisterMoveCallback(MoveCallback callback) {
1686 synchronized (mDelegates) {
1687 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1688 final MoveCallbackDelegate delegate = i.next();
1689 if (delegate.mCallback == callback) {
1690 try {
1691 mPM.unregisterMoveCallback(delegate);
1692 } catch (RemoteException e) {
1693 throw e.rethrowAsRuntimeException();
1694 }
1695 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001696 }
1697 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001698 }
1699 }
1700
1701 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001702 public int movePackage(String packageName, VolumeInfo vol) {
1703 try {
1704 final String volumeUuid;
1705 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1706 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1707 } else if (vol.isPrimaryPhysical()) {
1708 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1709 } else {
1710 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1711 }
1712
1713 return mPM.movePackage(packageName, volumeUuid);
1714 } catch (RemoteException e) {
1715 throw e.rethrowAsRuntimeException();
1716 }
1717 }
1718
1719 @Override
1720 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001721 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001722 if (app.isInternal()) {
1723 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1724 } else if (app.isExternalAsec()) {
1725 return storage.getPrimaryPhysicalVolume();
1726 } else {
1727 return storage.findVolumeByUuid(app.volumeUuid);
1728 }
1729 }
1730
1731 @Override
1732 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1733 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1734 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001735 final List<VolumeInfo> vols = storage.getVolumes();
1736 final List<VolumeInfo> candidates = new ArrayList<>();
1737 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001738 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001739 candidates.add(vol);
1740 }
1741 }
1742 return candidates;
1743 }
1744
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001745 private static boolean isPackageCandidateVolume(
1746 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1747 final boolean forceAllowOnExternal = Settings.Global.getInt(
1748 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001749 // Private internal is always an option
1750 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1751 return true;
1752 }
1753
1754 // System apps and apps demanding internal storage can't be moved
1755 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001756 if (app.isSystemApp()) {
1757 return false;
1758 }
1759 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001760 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1761 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001762 return false;
1763 }
1764
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001765 // Gotta be able to write there
1766 if (!vol.isMountedWritable()) {
1767 return false;
1768 }
1769
1770 // Moving into an ASEC on public primary is only option internal
1771 if (vol.isPrimaryPhysical()) {
1772 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001773 }
1774
1775 // Otherwise we can move to any private volume
1776 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1777 }
1778
1779 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001780 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001781 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001782 final String volumeUuid;
1783 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1784 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1785 } else if (vol.isPrimaryPhysical()) {
1786 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1787 } else {
1788 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1789 }
1790
1791 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001792 } catch (RemoteException e) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001793 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001794 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001795 }
1796
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001797 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001798 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1799 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1800 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001801 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001802 }
1803
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001804 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001805 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1806 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1807 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1808 final List<VolumeInfo> vols = storage.getVolumes();
1809 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001810 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1811 storage.getPrimaryStorageUuid()) && currentVol != null) {
1812 // TODO: support moving primary physical to emulated volume
1813 candidates.add(currentVol);
1814 } else {
1815 for (VolumeInfo vol : vols) {
1816 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1817 candidates.add(vol);
1818 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001819 }
1820 }
1821 return candidates;
1822 }
1823
1824 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1825 // Private internal is always an option
1826 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1827 return true;
1828 }
1829
1830 // Gotta be able to write there
1831 if (!vol.isMountedWritable()) {
1832 return false;
1833 }
1834
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001835 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001836 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001837 }
1838
1839 @Override
1840 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001841 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001842 }
1843
1844 @Override
1845 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1846 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001847 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001848 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001849 } catch (RemoteException e) {
1850 // Should never happen!
1851 }
1852 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001853
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001854 @Override
1855 public void clearApplicationUserData(String packageName,
1856 IPackageDataObserver observer) {
1857 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001858 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001859 } catch (RemoteException e) {
1860 // Should never happen!
1861 }
1862 }
1863 @Override
1864 public void deleteApplicationCacheFiles(String packageName,
1865 IPackageDataObserver observer) {
1866 try {
1867 mPM.deleteApplicationCacheFiles(packageName, observer);
1868 } catch (RemoteException e) {
1869 // Should never happen!
1870 }
1871 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001872
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001873 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001874 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1875 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001876 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001877 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001878 } catch (RemoteException e) {
1879 // Should never happen!
1880 }
1881 }
1882
1883 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001884 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001885 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001886 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001887 } catch (RemoteException e) {
1888 // Should never happen!
1889 }
1890 }
1891
1892 @Override
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001893 public boolean setPackageSuspendedAsUser(String packageName, boolean suspended, int userId) {
1894 try {
1895 return mPM.setPackageSuspendedAsUser(packageName, suspended, userId);
1896 } catch (RemoteException e) {
1897 // Should never happen!
1898 }
1899 return false;
1900 }
1901
1902 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001903 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001904 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001905 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001906 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001907 } catch (RemoteException e) {
1908 // Should never happen!
1909 }
1910 }
1911 @Override
1912 public void addPackageToPreferred(String packageName) {
1913 try {
1914 mPM.addPackageToPreferred(packageName);
1915 } catch (RemoteException e) {
1916 // Should never happen!
1917 }
1918 }
1919
1920 @Override
1921 public void removePackageFromPreferred(String packageName) {
1922 try {
1923 mPM.removePackageFromPreferred(packageName);
1924 } catch (RemoteException e) {
1925 // Should never happen!
1926 }
1927 }
1928
1929 @Override
1930 public List<PackageInfo> getPreferredPackages(int flags) {
1931 try {
1932 return mPM.getPreferredPackages(flags);
1933 } catch (RemoteException e) {
1934 // Should never happen!
1935 }
1936 return new ArrayList<PackageInfo>();
1937 }
1938
1939 @Override
1940 public void addPreferredActivity(IntentFilter filter,
1941 int match, ComponentName[] set, ComponentName activity) {
1942 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001943 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001944 } catch (RemoteException e) {
1945 // Should never happen!
1946 }
1947 }
1948
1949 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001950 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07001951 ComponentName[] set, ComponentName activity, int userId) {
1952 try {
1953 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001954 } catch (RemoteException e) {
1955 // Should never happen!
1956 }
1957 }
1958
1959 @Override
1960 public void replacePreferredActivity(IntentFilter filter,
1961 int match, ComponentName[] set, ComponentName activity) {
1962 try {
Robin Lee0e27c872015-09-28 14:37:40 +01001963 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001964 } catch (RemoteException e) {
1965 // Should never happen!
1966 }
1967 }
1968
1969 @Override
1970 public void replacePreferredActivityAsUser(IntentFilter filter,
1971 int match, ComponentName[] set, ComponentName activity,
1972 int userId) {
1973 try {
1974 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001975 } catch (RemoteException e) {
1976 // Should never happen!
1977 }
1978 }
1979
1980 @Override
1981 public void clearPackagePreferredActivities(String packageName) {
1982 try {
1983 mPM.clearPackagePreferredActivities(packageName);
1984 } catch (RemoteException e) {
1985 // Should never happen!
1986 }
1987 }
1988
1989 @Override
1990 public int getPreferredActivities(List<IntentFilter> outFilters,
1991 List<ComponentName> outActivities, String packageName) {
1992 try {
1993 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
1994 } catch (RemoteException e) {
1995 // Should never happen!
1996 }
1997 return 0;
1998 }
1999
2000 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002001 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2002 try {
2003 return mPM.getHomeActivities(outActivities);
2004 } catch (RemoteException e) {
2005 // Should never happen!
2006 }
2007 return null;
2008 }
2009
2010 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002011 public void setComponentEnabledSetting(ComponentName componentName,
2012 int newState, int flags) {
2013 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002014 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002015 } catch (RemoteException e) {
2016 // Should never happen!
2017 }
2018 }
2019
2020 @Override
2021 public int getComponentEnabledSetting(ComponentName componentName) {
2022 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002023 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002024 } catch (RemoteException e) {
2025 // Should never happen!
2026 }
2027 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2028 }
2029
2030 @Override
2031 public void setApplicationEnabledSetting(String packageName,
2032 int newState, int flags) {
2033 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002034 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002035 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002036 } catch (RemoteException e) {
2037 // Should never happen!
2038 }
2039 }
2040
2041 @Override
2042 public int getApplicationEnabledSetting(String packageName) {
2043 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002044 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002045 } catch (RemoteException e) {
2046 // Should never happen!
2047 }
2048 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2049 }
2050
Amith Yamasani655d0e22013-06-12 14:19:10 -07002051 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002052 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002053 UserHandle user) {
2054 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002055 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002056 user.getIdentifier());
2057 } catch (RemoteException re) {
2058 // Should never happen!
2059 }
2060 return false;
2061 }
2062
2063 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002064 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002065 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002066 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Amith Yamasani655d0e22013-06-12 14:19:10 -07002067 } catch (RemoteException re) {
2068 // Should never happen!
2069 }
2070 return false;
2071 }
2072
dcashmanc6f22492014-08-14 09:54:51 -07002073 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002074 @Override
2075 public KeySet getKeySetByAlias(String packageName, String alias) {
2076 Preconditions.checkNotNull(packageName);
2077 Preconditions.checkNotNull(alias);
dcashmanc6f22492014-08-14 09:54:51 -07002078 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002079 try {
dcashmanc6f22492014-08-14 09:54:51 -07002080 ks = mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002081 } catch (RemoteException e) {
2082 return null;
2083 }
dcashmanc6f22492014-08-14 09:54:51 -07002084 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002085 }
2086
dcashmanc6f22492014-08-14 09:54:51 -07002087 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002088 @Override
2089 public KeySet getSigningKeySet(String packageName) {
2090 Preconditions.checkNotNull(packageName);
dcashmanc6f22492014-08-14 09:54:51 -07002091 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002092 try {
dcashmanc6f22492014-08-14 09:54:51 -07002093 ks = mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002094 } catch (RemoteException e) {
2095 return null;
2096 }
dcashmanc6f22492014-08-14 09:54:51 -07002097 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002098 }
2099
dcashmanc6f22492014-08-14 09:54:51 -07002100 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002101 @Override
2102 public boolean isSignedBy(String packageName, KeySet ks) {
2103 Preconditions.checkNotNull(packageName);
2104 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002105 try {
dcashmanc6f22492014-08-14 09:54:51 -07002106 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002107 } catch (RemoteException e) {
2108 return false;
2109 }
2110 }
2111
dcashmanc6f22492014-08-14 09:54:51 -07002112 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002113 @Override
2114 public boolean isSignedByExactly(String packageName, KeySet ks) {
2115 Preconditions.checkNotNull(packageName);
2116 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002117 try {
dcashmanc6f22492014-08-14 09:54:51 -07002118 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002119 } catch (RemoteException e) {
2120 return false;
2121 }
2122 }
2123
Kenny Root0aaa0d92011-09-12 16:42:55 -07002124 /**
2125 * @hide
2126 */
2127 @Override
2128 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2129 try {
2130 return mPM.getVerifierDeviceIdentity();
2131 } catch (RemoteException e) {
2132 // Should never happen!
2133 }
2134 return null;
2135 }
2136
Jeff Hao9f60c082014-10-28 18:51:07 -07002137 /**
2138 * @hide
2139 */
2140 @Override
2141 public boolean isUpgrade() {
2142 try {
2143 return mPM.isUpgrade();
2144 } catch (RemoteException e) {
2145 return false;
2146 }
2147 }
2148
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002149 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002150 public PackageInstaller getPackageInstaller() {
2151 synchronized (mLock) {
2152 if (mInstaller == null) {
2153 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002154 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002155 mContext.getPackageName(), mContext.getUserId());
2156 } catch (RemoteException e) {
2157 throw e.rethrowAsRuntimeException();
2158 }
2159 }
2160 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002161 }
2162 }
2163
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002164 @Override
2165 public boolean isPackageAvailable(String packageName) {
2166 try {
2167 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2168 } catch (RemoteException e) {
2169 throw e.rethrowAsRuntimeException();
2170 }
2171 }
2172
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002173 /**
2174 * @hide
2175 */
2176 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002177 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2178 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002179 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002180 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002181 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002182 } catch (RemoteException e) {
2183 // Should never happen!
2184 }
2185 }
2186
2187 /**
2188 * @hide
2189 */
2190 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002191 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002192 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002193 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002194 } catch (RemoteException e) {
2195 // Should never happen!
2196 }
2197 }
2198
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002199 /**
2200 * @hide
2201 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002202 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002203 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2204 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2205 return dr;
2206 }
2207 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2208 }
2209
2210 /**
2211 * @hide
2212 */
2213 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002214 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002215 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2216 if (bitmap == null) {
2217 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2218 }
2219 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002220 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002221 Drawable dr = null;
2222 if (itemInfo.packageName != null) {
2223 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2224 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002225 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002226 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002227 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002228 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002229 }
2230
2231 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2232 Rect badgeLocation, boolean tryBadgeInPlace) {
2233 final int badgedWidth = drawable.getIntrinsicWidth();
2234 final int badgedHeight = drawable.getIntrinsicHeight();
2235 final boolean canBadgeInPlace = tryBadgeInPlace
2236 && (drawable instanceof BitmapDrawable)
2237 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2238
2239 final Bitmap bitmap;
2240 if (canBadgeInPlace) {
2241 bitmap = ((BitmapDrawable) drawable).getBitmap();
2242 } else {
2243 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2244 }
2245 Canvas canvas = new Canvas(bitmap);
2246
2247 if (!canBadgeInPlace) {
2248 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2249 drawable.draw(canvas);
2250 }
2251
2252 if (badgeLocation != null) {
2253 if (badgeLocation.left < 0 || badgeLocation.top < 0
2254 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2255 throw new IllegalArgumentException("Badge location " + badgeLocation
2256 + " not in badged drawable bounds "
2257 + new Rect(0, 0, badgedWidth, badgedHeight));
2258 }
2259 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2260
2261 canvas.save();
2262 canvas.translate(badgeLocation.left, badgeLocation.top);
2263 badgeDrawable.draw(canvas);
2264 canvas.restore();
2265 } else {
2266 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2267 badgeDrawable.draw(canvas);
2268 }
2269
2270 if (!canBadgeInPlace) {
2271 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2272
2273 if (drawable instanceof BitmapDrawable) {
2274 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2275 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2276 }
2277
2278 return mergedDrawable;
2279 }
2280
2281 return drawable;
2282 }
2283
2284 private int getBadgeResIdForUser(int userHandle) {
2285 // Return the framework-provided badge.
2286 UserInfo userInfo = getUserIfProfile(userHandle);
2287 if (userInfo != null && userInfo.isManagedProfile()) {
2288 return com.android.internal.R.drawable.ic_corp_icon_badge;
2289 }
2290 return 0;
2291 }
2292
2293 private UserInfo getUserIfProfile(int userHandle) {
Robin Lee0e27c872015-09-28 14:37:40 +01002294 List<UserInfo> userProfiles = getUserManager().getProfiles(mContext.getUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07002295 for (UserInfo user : userProfiles) {
2296 if (user.id == userHandle) {
2297 return user;
2298 }
2299 }
2300 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002301 }
2302
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002303 /** {@hide} */
2304 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2305 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002306 private static final int MSG_CREATED = 1;
2307 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002308
2309 final MoveCallback mCallback;
2310 final Handler mHandler;
2311
2312 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2313 mCallback = callback;
2314 mHandler = new Handler(looper, this);
2315 }
2316
2317 @Override
2318 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002319 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002320 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002321 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002322 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002323 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002324 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002325 }
2326 case MSG_STATUS_CHANGED: {
2327 final SomeArgs args = (SomeArgs) msg.obj;
2328 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2329 args.recycle();
2330 return true;
2331 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002332 }
2333 return false;
2334 }
2335
2336 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002337 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002338 final SomeArgs args = SomeArgs.obtain();
2339 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002340 args.arg2 = extras;
2341 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2342 }
2343
2344 @Override
2345 public void onStatusChanged(int moveId, int status, long estMillis) {
2346 final SomeArgs args = SomeArgs.obtain();
2347 args.argi1 = moveId;
2348 args.argi2 = status;
2349 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002350 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002351 }
2352 }
2353
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002354 private final ContextImpl mContext;
2355 private final IPackageManager mPM;
2356
2357 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002358 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2359 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2360 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2361 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002362
2363 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2364 mPermissionListeners = new ArrayMap<>();
2365
2366 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2367 implements Handler.Callback{
2368 private static final int MSG_PERMISSIONS_CHANGED = 1;
2369
2370 private final OnPermissionsChangedListener mListener;
2371 private final Handler mHandler;
2372
2373
2374 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2375 Looper looper) {
2376 mListener = listener;
2377 mHandler = new Handler(looper, this);
2378 }
2379
2380 @Override
2381 public void onPermissionsChanged(int uid) {
2382 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2383 }
2384
2385 @Override
2386 public boolean handleMessage(Message msg) {
2387 switch (msg.what) {
2388 case MSG_PERMISSIONS_CHANGED: {
2389 final int uid = msg.arg1;
2390 mListener.onPermissionsChanged(uid);
2391 return true;
2392 }
2393 }
2394 return false;
2395 }
2396 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002397}