blob: dfe7e18b09a09a4cd024f46f1d661df4e4b5015d [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;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080033import android.content.pm.FeatureInfo;
34import android.content.pm.IPackageDataObserver;
35import android.content.pm.IPackageDeleteObserver;
36import android.content.pm.IPackageInstallObserver;
37import android.content.pm.IPackageManager;
38import android.content.pm.IPackageMoveObserver;
39import android.content.pm.IPackageStatsObserver;
40import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080041import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070042import android.content.pm.KeySet;
Jeff Sharkey513a0742014-07-08 17:10:32 -070043import android.content.pm.ManifestDigest;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080044import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070045import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010046import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080047import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070048import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080049import android.content.pm.PermissionGroupInfo;
50import android.content.pm.PermissionInfo;
51import android.content.pm.ProviderInfo;
52import android.content.pm.ResolveInfo;
53import android.content.pm.ServiceInfo;
Svetoslavc7d62f02014-09-04 15:39:54 -070054import android.content.pm.UserInfo;
rich cannings706e8ba2012-08-20 13:20:14 -070055import android.content.pm.VerificationParams;
Kenny Root0aaa0d92011-09-12 16:42:55 -070056import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080057import android.content.res.Resources;
58import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070059import android.graphics.Bitmap;
60import android.graphics.Canvas;
61import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010062import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080063import android.graphics.drawable.Drawable;
64import android.net.Uri;
65import android.os.Process;
66import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070067import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080068import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010069import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070070import android.os.storage.StorageManager;
71import android.os.storage.VolumeInfo;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070072import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080073import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070074import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070075
76import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070077
78import com.android.internal.annotations.GuardedBy;
dcashman9d2f4412014-06-09 09:27:54 -070079import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010080import com.android.internal.util.UserIcons;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070081
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080082import java.lang.ref.WeakReference;
83import java.util.ArrayList;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080084import java.util.List;
85
86/*package*/
87final class ApplicationPackageManager extends PackageManager {
88 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080089 private final static boolean DEBUG_ICONS = false;
90
Adam Lesinskid00bb5e2014-10-07 12:14:45 -070091 // Default flags to use with PackageManager when no flags are given.
92 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
93
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070094 private final Object mLock = new Object();
95
96 @GuardedBy("mLock")
97 private UserManager mUserManager;
98 @GuardedBy("mLock")
99 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100100
101 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700102 synchronized (mLock) {
103 if (mUserManager == null) {
104 mUserManager = UserManager.get(mContext);
105 }
106 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100107 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100108 }
109
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800110 @Override
111 public PackageInfo getPackageInfo(String packageName, int flags)
112 throws NameNotFoundException {
113 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700114 PackageInfo pi = mPM.getPackageInfo(packageName, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800115 if (pi != null) {
116 return pi;
117 }
118 } catch (RemoteException e) {
119 throw new RuntimeException("Package manager has died", e);
120 }
121
122 throw new NameNotFoundException(packageName);
123 }
124
125 @Override
126 public String[] currentToCanonicalPackageNames(String[] names) {
127 try {
128 return mPM.currentToCanonicalPackageNames(names);
129 } catch (RemoteException e) {
130 throw new RuntimeException("Package manager has died", e);
131 }
132 }
133
134 @Override
135 public String[] canonicalToCurrentPackageNames(String[] names) {
136 try {
137 return mPM.canonicalToCurrentPackageNames(names);
138 } catch (RemoteException e) {
139 throw new RuntimeException("Package manager has died", e);
140 }
141 }
142
143 @Override
144 public Intent getLaunchIntentForPackage(String packageName) {
145 // First see if the package has an INFO activity; the existence of
146 // such an activity is implied to be the desired front-door for the
147 // overall package (such as if it has multiple launcher entries).
148 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
149 intentToResolve.addCategory(Intent.CATEGORY_INFO);
150 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800151 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800152
153 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800154 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800155 // reuse the intent instance
156 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
157 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
158 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800159 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800160 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800161 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800162 return null;
163 }
164 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800165 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800166 intent.setClassName(ris.get(0).activityInfo.packageName,
167 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800168 return intent;
169 }
170
171 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700172 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
173 // Try to find a main leanback_launcher activity.
174 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
175 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
176 intentToResolve.setPackage(packageName);
177 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
178
179 if (ris == null || ris.size() <= 0) {
180 return null;
181 }
182 Intent intent = new Intent(intentToResolve);
183 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
184 intent.setClassName(ris.get(0).activityInfo.packageName,
185 ris.get(0).activityInfo.name);
186 return intent;
187 }
188
189 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800190 public int[] getPackageGids(String packageName)
191 throws NameNotFoundException {
192 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800193 int[] gids = mPM.getPackageGids(packageName, mContext.getUserId());
194 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800195 return gids;
196 }
197 } catch (RemoteException e) {
198 throw new RuntimeException("Package manager has died", e);
199 }
200
201 throw new NameNotFoundException(packageName);
202 }
203
204 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800205 public int getPackageUid(String packageName, int userHandle)
206 throws NameNotFoundException {
207 try {
208 int uid = mPM.getPackageUid(packageName, userHandle);
209 if (uid >= 0) {
210 return uid;
211 }
212 } catch (RemoteException e) {
213 throw new RuntimeException("Package manager has died", e);
214 }
215
216 throw new NameNotFoundException(packageName);
217 }
218
219 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800220 public PermissionInfo getPermissionInfo(String name, int flags)
221 throws NameNotFoundException {
222 try {
223 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
224 if (pi != null) {
225 return pi;
226 }
227 } catch (RemoteException e) {
228 throw new RuntimeException("Package manager has died", e);
229 }
230
231 throw new NameNotFoundException(name);
232 }
233
234 @Override
235 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
236 throws NameNotFoundException {
237 try {
238 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags);
239 if (pi != null) {
240 return pi;
241 }
242 } catch (RemoteException e) {
243 throw new RuntimeException("Package manager has died", e);
244 }
245
246 throw new NameNotFoundException(group);
247 }
248
249 @Override
250 public PermissionGroupInfo getPermissionGroupInfo(String name,
251 int flags) throws NameNotFoundException {
252 try {
253 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
254 if (pgi != null) {
255 return pgi;
256 }
257 } catch (RemoteException e) {
258 throw new RuntimeException("Package manager has died", e);
259 }
260
261 throw new NameNotFoundException(name);
262 }
263
264 @Override
265 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
266 try {
267 return mPM.getAllPermissionGroups(flags);
268 } catch (RemoteException e) {
269 throw new RuntimeException("Package manager has died", e);
270 }
271 }
272
273 @Override
274 public ApplicationInfo getApplicationInfo(String packageName, int flags)
275 throws NameNotFoundException {
276 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700277 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800278 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100279 // This is a temporary hack. Callers must use
280 // createPackageContext(packageName).getApplicationInfo() to
281 // get the right paths.
282 maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800283 return ai;
284 }
285 } catch (RemoteException e) {
286 throw new RuntimeException("Package manager has died", e);
287 }
288
289 throw new NameNotFoundException(packageName);
290 }
291
Narayan Kamathcaa71192014-07-16 11:06:43 +0100292 private static void maybeAdjustApplicationInfo(ApplicationInfo info) {
293 // If we're dealing with a multi-arch application that has both
294 // 32 and 64 bit shared libraries, we might need to choose the secondary
295 // depending on what the current runtime's instruction set is.
296 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
297 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500298
299 // Get the instruction set that the libraries of secondary Abi is supported.
300 // In presence of a native bridge this might be different than the one secondary Abi used.
301 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
302 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
303 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100304
305 // If the runtimeIsa is the same as the primary isa, then we do nothing.
306 // Everything will be set up correctly because info.nativeLibraryDir will
307 // correspond to the right ISA.
308 if (runtimeIsa.equals(secondaryIsa)) {
309 info.nativeLibraryDir = info.secondaryNativeLibraryDir;
310 }
311 }
312 }
313
314
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800315 @Override
316 public ActivityInfo getActivityInfo(ComponentName className, int flags)
317 throws NameNotFoundException {
318 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700319 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800320 if (ai != null) {
321 return ai;
322 }
323 } catch (RemoteException e) {
324 throw new RuntimeException("Package manager has died", e);
325 }
326
327 throw new NameNotFoundException(className.toString());
328 }
329
330 @Override
331 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
332 throws NameNotFoundException {
333 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700334 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800335 if (ai != null) {
336 return ai;
337 }
338 } catch (RemoteException e) {
339 throw new RuntimeException("Package manager has died", e);
340 }
341
342 throw new NameNotFoundException(className.toString());
343 }
344
345 @Override
346 public ServiceInfo getServiceInfo(ComponentName className, int flags)
347 throws NameNotFoundException {
348 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700349 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800350 if (si != null) {
351 return si;
352 }
353 } catch (RemoteException e) {
354 throw new RuntimeException("Package manager has died", e);
355 }
356
357 throw new NameNotFoundException(className.toString());
358 }
359
360 @Override
361 public ProviderInfo getProviderInfo(ComponentName className, int flags)
362 throws NameNotFoundException {
363 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700364 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800365 if (pi != null) {
366 return pi;
367 }
368 } catch (RemoteException e) {
369 throw new RuntimeException("Package manager has died", e);
370 }
371
372 throw new NameNotFoundException(className.toString());
373 }
374
375 @Override
376 public String[] getSystemSharedLibraryNames() {
377 try {
378 return mPM.getSystemSharedLibraryNames();
379 } catch (RemoteException e) {
380 throw new RuntimeException("Package manager has died", e);
381 }
382 }
383
384 @Override
385 public FeatureInfo[] getSystemAvailableFeatures() {
386 try {
387 return mPM.getSystemAvailableFeatures();
388 } catch (RemoteException e) {
389 throw new RuntimeException("Package manager has died", e);
390 }
391 }
392
393 @Override
394 public boolean hasSystemFeature(String name) {
395 try {
396 return mPM.hasSystemFeature(name);
397 } catch (RemoteException e) {
398 throw new RuntimeException("Package manager has died", e);
399 }
400 }
401
402 @Override
403 public int checkPermission(String permName, String pkgName) {
404 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800405 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800406 } catch (RemoteException e) {
407 throw new RuntimeException("Package manager has died", e);
408 }
409 }
410
411 @Override
412 public boolean addPermission(PermissionInfo info) {
413 try {
414 return mPM.addPermission(info);
415 } catch (RemoteException e) {
416 throw new RuntimeException("Package manager has died", e);
417 }
418 }
419
420 @Override
421 public boolean addPermissionAsync(PermissionInfo info) {
422 try {
423 return mPM.addPermissionAsync(info);
424 } catch (RemoteException e) {
425 throw new RuntimeException("Package manager has died", e);
426 }
427 }
428
429 @Override
430 public void removePermission(String name) {
431 try {
432 mPM.removePermission(name);
433 } catch (RemoteException e) {
434 throw new RuntimeException("Package manager has died", e);
435 }
436 }
437
438 @Override
Svetoslavc6d1c342015-02-26 14:44:43 -0800439 public void grantPermission(String packageName, String permissionName, UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800440 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800441 mPM.grantPermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800442 } catch (RemoteException e) {
443 throw new RuntimeException("Package manager has died", e);
444 }
445 }
446
447 @Override
Svetoslavc6d1c342015-02-26 14:44:43 -0800448 public void revokePermission(String packageName, String permissionName, UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800449 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800450 mPM.revokePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800451 } catch (RemoteException e) {
452 throw new RuntimeException("Package manager has died", e);
453 }
454 }
455
456 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800457 public int checkSignatures(String pkg1, String pkg2) {
458 try {
459 return mPM.checkSignatures(pkg1, pkg2);
460 } catch (RemoteException e) {
461 throw new RuntimeException("Package manager has died", e);
462 }
463 }
464
465 @Override
466 public int checkSignatures(int uid1, int uid2) {
467 try {
468 return mPM.checkUidSignatures(uid1, uid2);
469 } catch (RemoteException e) {
470 throw new RuntimeException("Package manager has died", e);
471 }
472 }
473
474 @Override
475 public String[] getPackagesForUid(int uid) {
476 try {
477 return mPM.getPackagesForUid(uid);
478 } catch (RemoteException e) {
479 throw new RuntimeException("Package manager has died", e);
480 }
481 }
482
483 @Override
484 public String getNameForUid(int uid) {
485 try {
486 return mPM.getNameForUid(uid);
487 } catch (RemoteException e) {
488 throw new RuntimeException("Package manager has died", e);
489 }
490 }
491
492 @Override
493 public int getUidForSharedUser(String sharedUserName)
494 throws NameNotFoundException {
495 try {
496 int uid = mPM.getUidForSharedUser(sharedUserName);
497 if(uid != -1) {
498 return uid;
499 }
500 } catch (RemoteException e) {
501 throw new RuntimeException("Package manager has died", e);
502 }
503 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
504 }
505
Kenny Roote6cd0c72011-05-19 12:48:14 -0700506 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800507 @Override
508 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700509 return getInstalledPackages(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700510 }
511
512 /** @hide */
513 @Override
514 public List<PackageInfo> getInstalledPackages(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800515 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800516 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
517 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800518 } catch (RemoteException e) {
519 throw new RuntimeException("Package manager has died", e);
520 }
521 }
522
Kenny Roote6cd0c72011-05-19 12:48:14 -0700523 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800524 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800525 public List<PackageInfo> getPackagesHoldingPermissions(
526 String[] permissions, int flags) {
527 final int userId = mContext.getUserId();
528 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800529 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
530 permissions, flags, userId);
531 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800532 } catch (RemoteException e) {
533 throw new RuntimeException("Package manager has died", e);
534 }
535 }
536
537 @SuppressWarnings("unchecked")
538 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800539 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700540 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800541 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800542 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
543 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800544 } catch (RemoteException e) {
545 throw new RuntimeException("Package manager has died", e);
546 }
547 }
548
549 @Override
550 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700551 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700552 }
553
554 @Override
555 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800556 try {
557 return mPM.resolveIntent(
558 intent,
559 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700560 flags,
561 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800562 } catch (RemoteException e) {
563 throw new RuntimeException("Package manager has died", e);
564 }
565 }
566
567 @Override
568 public List<ResolveInfo> queryIntentActivities(Intent intent,
569 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700570 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700571 }
572
573 /** @hide Same as above but for a specific user */
574 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700575 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700576 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800577 try {
578 return mPM.queryIntentActivities(
579 intent,
580 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700581 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700582 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800583 } catch (RemoteException e) {
584 throw new RuntimeException("Package manager has died", e);
585 }
586 }
587
588 @Override
589 public List<ResolveInfo> queryIntentActivityOptions(
590 ComponentName caller, Intent[] specifics, Intent intent,
591 int flags) {
592 final ContentResolver resolver = mContext.getContentResolver();
593
594 String[] specificTypes = null;
595 if (specifics != null) {
596 final int N = specifics.length;
597 for (int i=0; i<N; i++) {
598 Intent sp = specifics[i];
599 if (sp != null) {
600 String t = sp.resolveTypeIfNeeded(resolver);
601 if (t != null) {
602 if (specificTypes == null) {
603 specificTypes = new String[N];
604 }
605 specificTypes[i] = t;
606 }
607 }
608 }
609 }
610
611 try {
612 return mPM.queryIntentActivityOptions(caller, specifics,
613 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700614 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800615 } catch (RemoteException e) {
616 throw new RuntimeException("Package manager has died", e);
617 }
618 }
619
Amith Yamasanif203aee2012-08-29 18:41:53 -0700620 /**
621 * @hide
622 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800623 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700624 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800625 try {
626 return mPM.queryIntentReceivers(
627 intent,
628 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700629 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -0700630 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800631 } catch (RemoteException e) {
632 throw new RuntimeException("Package manager has died", e);
633 }
634 }
635
636 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700637 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700638 return queryBroadcastReceivers(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700639 }
640
641 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800642 public ResolveInfo resolveService(Intent intent, int flags) {
643 try {
644 return mPM.resolveService(
645 intent,
646 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700647 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700648 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800649 } catch (RemoteException e) {
650 throw new RuntimeException("Package manager has died", e);
651 }
652 }
653
654 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700655 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800656 try {
657 return mPM.queryIntentServices(
658 intent,
659 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700660 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700661 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800662 } catch (RemoteException e) {
663 throw new RuntimeException("Package manager has died", e);
664 }
665 }
666
667 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700668 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700669 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700670 }
671
672 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700673 public List<ResolveInfo> queryIntentContentProvidersAsUser(
674 Intent intent, int flags, int userId) {
675 try {
676 return mPM.queryIntentContentProviders(intent,
677 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
678 } catch (RemoteException e) {
679 throw new RuntimeException("Package manager has died", e);
680 }
681 }
682
683 @Override
684 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
685 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
686 }
687
688 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100689 public ProviderInfo resolveContentProvider(String name, int flags) {
690 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
691 }
692
693 /** @hide **/
694 @Override
695 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800696 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100697 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800698 } catch (RemoteException e) {
699 throw new RuntimeException("Package manager has died", e);
700 }
701 }
702
703 @Override
704 public List<ProviderInfo> queryContentProviders(String processName,
705 int uid, int flags) {
706 try {
707 return mPM.queryContentProviders(processName, uid, flags);
708 } catch (RemoteException e) {
709 throw new RuntimeException("Package manager has died", e);
710 }
711 }
712
713 @Override
714 public InstrumentationInfo getInstrumentationInfo(
715 ComponentName className, int flags)
716 throws NameNotFoundException {
717 try {
718 InstrumentationInfo ii = mPM.getInstrumentationInfo(
719 className, flags);
720 if (ii != null) {
721 return ii;
722 }
723 } catch (RemoteException e) {
724 throw new RuntimeException("Package manager has died", e);
725 }
726
727 throw new NameNotFoundException(className.toString());
728 }
729
730 @Override
731 public List<InstrumentationInfo> queryInstrumentation(
732 String targetPackage, int flags) {
733 try {
734 return mPM.queryInstrumentation(targetPackage, flags);
735 } catch (RemoteException e) {
736 throw new RuntimeException("Package manager has died", e);
737 }
738 }
739
Alan Viveretteecd585a2015-04-13 10:32:51 -0700740 @Nullable
741 @Override
742 public Drawable getDrawable(String packageName, @DrawableRes int resId,
743 @Nullable ApplicationInfo appInfo) {
744 final ResourceName name = new ResourceName(packageName, resId);
745 final Drawable cachedIcon = getCachedIcon(name);
746 if (cachedIcon != null) {
747 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800748 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700749
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800750 if (appInfo == null) {
751 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700752 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800753 } catch (NameNotFoundException e) {
754 return null;
755 }
756 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700757
758 if (resId != 0) {
759 try {
760 final Resources r = getResourcesForApplication(appInfo);
761 final Drawable dr = r.getDrawable(resId, null);
762 if (dr != null) {
763 putCachedIcon(name, dr);
764 }
765
766 if (false) {
767 RuntimeException e = new RuntimeException("here");
768 e.fillInStackTrace();
769 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
770 + " from package " + packageName
771 + ": app scale=" + r.getCompatibilityInfo().applicationScale
772 + ", caller scale=" + mContext.getResources()
773 .getCompatibilityInfo().applicationScale,
774 e);
775 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100776 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700777 Log.v(TAG, "Getting drawable 0x"
778 + Integer.toHexString(resId) + " from " + r
779 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100780 }
781 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700782 } catch (NameNotFoundException e) {
783 Log.w("PackageManager", "Failure retrieving resources for "
784 + appInfo.packageName);
785 } catch (Resources.NotFoundException e) {
786 Log.w("PackageManager", "Failure retrieving resources for "
787 + appInfo.packageName + ": " + e.getMessage());
788 } catch (Exception e) {
789 // If an exception was thrown, fall through to return
790 // default icon.
791 Log.w("PackageManager", "Failure retrieving icon 0x"
792 + Integer.toHexString(resId) + " in package "
793 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800794 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800795 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700796
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800797 return null;
798 }
799
800 @Override public Drawable getActivityIcon(ComponentName activityName)
801 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700802 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800803 }
804
805 @Override public Drawable getActivityIcon(Intent intent)
806 throws NameNotFoundException {
807 if (intent.getComponent() != null) {
808 return getActivityIcon(intent.getComponent());
809 }
810
811 ResolveInfo info = resolveActivity(
812 intent, PackageManager.MATCH_DEFAULT_ONLY);
813 if (info != null) {
814 return info.activityInfo.loadIcon(this);
815 }
816
Romain Guy39fe17c2011-11-30 10:34:07 -0800817 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800818 }
819
820 @Override public Drawable getDefaultActivityIcon() {
821 return Resources.getSystem().getDrawable(
822 com.android.internal.R.drawable.sym_def_app_icon);
823 }
824
825 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
826 return info.loadIcon(this);
827 }
828
829 @Override public Drawable getApplicationIcon(String packageName)
830 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700831 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800832 }
833
834 @Override
Jose Limaf78e3122014-03-06 12:13:15 -0800835 public Drawable getActivityBanner(ComponentName activityName)
836 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700837 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -0800838 }
839
840 @Override
841 public Drawable getActivityBanner(Intent intent)
842 throws NameNotFoundException {
843 if (intent.getComponent() != null) {
844 return getActivityBanner(intent.getComponent());
845 }
846
847 ResolveInfo info = resolveActivity(
848 intent, PackageManager.MATCH_DEFAULT_ONLY);
849 if (info != null) {
850 return info.activityInfo.loadBanner(this);
851 }
852
853 throw new NameNotFoundException(intent.toUri(0));
854 }
855
856 @Override
857 public Drawable getApplicationBanner(ApplicationInfo info) {
858 return info.loadBanner(this);
859 }
860
861 @Override
862 public Drawable getApplicationBanner(String packageName)
863 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700864 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -0800865 }
866
867 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800868 public Drawable getActivityLogo(ComponentName activityName)
869 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700870 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800871 }
872
873 @Override
874 public Drawable getActivityLogo(Intent intent)
875 throws NameNotFoundException {
876 if (intent.getComponent() != null) {
877 return getActivityLogo(intent.getComponent());
878 }
879
880 ResolveInfo info = resolveActivity(
881 intent, PackageManager.MATCH_DEFAULT_ONLY);
882 if (info != null) {
883 return info.activityInfo.loadLogo(this);
884 }
885
886 throw new NameNotFoundException(intent.toUri(0));
887 }
888
889 @Override
890 public Drawable getApplicationLogo(ApplicationInfo info) {
891 return info.loadLogo(this);
892 }
893
894 @Override
895 public Drawable getApplicationLogo(String packageName)
896 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700897 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800898 }
899
Svetoslavc7d62f02014-09-04 15:39:54 -0700900 @Override
901 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
902 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
903 if (badgeResId == 0) {
904 return icon;
905 }
906 Drawable badgeIcon = getDrawable("system", badgeResId, null);
907 return getBadgedDrawable(icon, badgeIcon, null, true);
908 }
909
910 @Override
911 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
912 Rect badgeLocation, int badgeDensity) {
913 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
914 if (badgeDrawable == null) {
915 return drawable;
916 }
917 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
918 }
919
920 @Override
921 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
922 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
923 if (userInfo != null && userInfo.isManagedProfile()) {
924 if (density <= 0) {
925 density = mContext.getResources().getDisplayMetrics().densityDpi;
926 }
927 return Resources.getSystem().getDrawableForDensity(
928 com.android.internal.R.drawable.ic_corp_badge, density);
929 }
930 return null;
931 }
932
933 @Override
934 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
935 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
936 if (userInfo != null && userInfo.isManagedProfile()) {
937 return Resources.getSystem().getString(
938 com.android.internal.R.string.managed_profile_label_badge, label);
939 }
940 return label;
941 }
942
Alan Viveretteecd585a2015-04-13 10:32:51 -0700943 @Override
944 public Resources getResourcesForActivity(ComponentName activityName)
945 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800946 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700947 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800948 }
949
Alan Viveretteecd585a2015-04-13 10:32:51 -0700950 @Override
951 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
952 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800953 if (app.packageName.equals("system")) {
954 return mContext.mMainThread.getSystemContext().getResources();
955 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700956 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -0700957 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700958 sameUid ? app.sourceDir : app.publicSourceDir,
959 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700960 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
961 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800962 if (r != null) {
963 return r;
964 }
965 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
966 }
967
Alan Viveretteecd585a2015-04-13 10:32:51 -0700968 @Override
969 public Resources getResourcesForApplication(String appPackageName)
970 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800971 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700972 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800973 }
974
Amith Yamasani98edc952012-09-25 14:09:27 -0700975 /** @hide */
976 @Override
977 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
978 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700979 if (userId < 0) {
980 throw new IllegalArgumentException(
981 "Call does not support special user #" + userId);
982 }
983 if ("system".equals(appPackageName)) {
984 return mContext.mMainThread.getSystemContext().getResources();
985 }
Amith Yamasani98edc952012-09-25 14:09:27 -0700986 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700987 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -0700988 if (ai != null) {
989 return getResourcesForApplication(ai);
990 }
991 } catch (RemoteException e) {
992 throw new RuntimeException("Package manager has died", e);
993 }
994 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
995 }
996
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800997 int mCachedSafeMode = -1;
998 @Override public boolean isSafeMode() {
999 try {
1000 if (mCachedSafeMode < 0) {
1001 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1002 }
1003 return mCachedSafeMode != 0;
1004 } catch (RemoteException e) {
1005 throw new RuntimeException("Package manager has died", e);
1006 }
1007 }
1008
1009 static void configurationChanged() {
1010 synchronized (sSync) {
1011 sIconCache.clear();
1012 sStringCache.clear();
1013 }
1014 }
1015
1016 ApplicationPackageManager(ContextImpl context,
1017 IPackageManager pm) {
1018 mContext = context;
1019 mPM = pm;
1020 }
1021
Alan Viveretteecd585a2015-04-13 10:32:51 -07001022 @Nullable
1023 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001024 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001025 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001026 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1027 + name + ": " + wr);
1028 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001029 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001030 if (state != null) {
1031 if (DEBUG_ICONS) {
1032 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1033 }
1034 // Note: It's okay here to not use the newDrawable(Resources) variant
1035 // of the API. The ConstantState comes from a drawable that was
1036 // originally created by passing the proper app Resources instance
1037 // which means the state should already contain the proper
1038 // resources specific information (like density.) See
1039 // BitmapDrawable.BitmapState for instance.
1040 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001041 }
1042 // our entry has been purged
1043 sIconCache.remove(name);
1044 }
1045 }
1046 return null;
1047 }
1048
Alan Viveretteecd585a2015-04-13 10:32:51 -07001049 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001050 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001051 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001052 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001053 }
1054 }
1055
Romain Guy39fe17c2011-11-30 10:34:07 -08001056 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001057 boolean immediateGc = false;
1058 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1059 immediateGc = true;
1060 }
1061 if (pkgList != null && (pkgList.length > 0)) {
1062 boolean needCleanup = false;
1063 for (String ssp : pkgList) {
1064 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001065 for (int i=sIconCache.size()-1; i>=0; i--) {
1066 ResourceName nm = sIconCache.keyAt(i);
1067 if (nm.packageName.equals(ssp)) {
1068 //Log.i(TAG, "Removing cached drawable for " + nm);
1069 sIconCache.removeAt(i);
1070 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001071 }
1072 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001073 for (int i=sStringCache.size()-1; i>=0; i--) {
1074 ResourceName nm = sStringCache.keyAt(i);
1075 if (nm.packageName.equals(ssp)) {
1076 //Log.i(TAG, "Removing cached string for " + nm);
1077 sStringCache.removeAt(i);
1078 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001079 }
1080 }
1081 }
1082 }
1083 if (needCleanup || hasPkgInfo) {
1084 if (immediateGc) {
1085 // Schedule an immediate gc.
1086 Runtime.getRuntime().gc();
1087 } else {
1088 ActivityThread.currentActivityThread().scheduleGcIdler();
1089 }
1090 }
1091 }
1092 }
1093
1094 private static final class ResourceName {
1095 final String packageName;
1096 final int iconId;
1097
1098 ResourceName(String _packageName, int _iconId) {
1099 packageName = _packageName;
1100 iconId = _iconId;
1101 }
1102
1103 ResourceName(ApplicationInfo aInfo, int _iconId) {
1104 this(aInfo.packageName, _iconId);
1105 }
1106
1107 ResourceName(ComponentInfo cInfo, int _iconId) {
1108 this(cInfo.applicationInfo.packageName, _iconId);
1109 }
1110
1111 ResourceName(ResolveInfo rInfo, int _iconId) {
1112 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1113 }
1114
1115 @Override
1116 public boolean equals(Object o) {
1117 if (this == o) return true;
1118 if (o == null || getClass() != o.getClass()) return false;
1119
1120 ResourceName that = (ResourceName) o;
1121
1122 if (iconId != that.iconId) return false;
1123 return !(packageName != null ?
1124 !packageName.equals(that.packageName) : that.packageName != null);
1125
1126 }
1127
1128 @Override
1129 public int hashCode() {
1130 int result;
1131 result = packageName.hashCode();
1132 result = 31 * result + iconId;
1133 return result;
1134 }
1135
1136 @Override
1137 public String toString() {
1138 return "{ResourceName " + packageName + " / " + iconId + "}";
1139 }
1140 }
1141
1142 private CharSequence getCachedString(ResourceName name) {
1143 synchronized (sSync) {
1144 WeakReference<CharSequence> wr = sStringCache.get(name);
1145 if (wr != null) { // we have the activity
1146 CharSequence cs = wr.get();
1147 if (cs != null) {
1148 return cs;
1149 }
1150 // our entry has been purged
1151 sStringCache.remove(name);
1152 }
1153 }
1154 return null;
1155 }
1156
1157 private void putCachedString(ResourceName name, CharSequence cs) {
1158 synchronized (sSync) {
1159 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1160 }
1161 }
1162
1163 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001164 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001165 ApplicationInfo appInfo) {
1166 ResourceName name = new ResourceName(packageName, resid);
1167 CharSequence text = getCachedString(name);
1168 if (text != null) {
1169 return text;
1170 }
1171 if (appInfo == null) {
1172 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001173 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001174 } catch (NameNotFoundException e) {
1175 return null;
1176 }
1177 }
1178 try {
1179 Resources r = getResourcesForApplication(appInfo);
1180 text = r.getText(resid);
1181 putCachedString(name, text);
1182 return text;
1183 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001184 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001185 + appInfo.packageName);
1186 } catch (RuntimeException e) {
1187 // If an exception was thrown, fall through to return
1188 // default icon.
1189 Log.w("PackageManager", "Failure retrieving text 0x"
1190 + Integer.toHexString(resid) + " in package "
1191 + packageName, e);
1192 }
1193 return null;
1194 }
1195
1196 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001197 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001198 ApplicationInfo appInfo) {
1199 if (appInfo == null) {
1200 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001201 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001202 } catch (NameNotFoundException e) {
1203 return null;
1204 }
1205 }
1206 try {
1207 Resources r = getResourcesForApplication(appInfo);
1208 return r.getXml(resid);
1209 } catch (RuntimeException e) {
1210 // If an exception was thrown, fall through to return
1211 // default icon.
1212 Log.w("PackageManager", "Failure retrieving xml 0x"
1213 + Integer.toHexString(resid) + " in package "
1214 + packageName, e);
1215 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001216 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001217 + appInfo.packageName);
1218 }
1219 return null;
1220 }
1221
1222 @Override
1223 public CharSequence getApplicationLabel(ApplicationInfo info) {
1224 return info.loadLabel(this);
1225 }
1226
1227 @Override
1228 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1229 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001230 final VerificationParams verificationParams = new VerificationParams(null, null,
1231 null, VerificationParams.NO_UID, null);
1232 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1233 installerPackageName, verificationParams, null);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001234 }
1235
1236 @Override
Kenny Root5ab21572011-07-27 11:11:19 -07001237 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1238 int flags, String installerPackageName, Uri verificationURI,
Rich Canningse1d7c712012-08-08 12:46:06 -07001239 ManifestDigest manifestDigest, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001240 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1241 null, VerificationParams.NO_UID, manifestDigest);
1242 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1243 installerPackageName, verificationParams, encryptionParams);
Kenny Root5ab21572011-07-27 11:11:19 -07001244 }
1245
1246 @Override
John Spurlock8a985d22014-02-25 09:40:05 -05001247 public void installPackageWithVerificationAndEncryption(Uri packageURI,
rich cannings706e8ba2012-08-20 13:20:14 -07001248 IPackageInstallObserver observer, int flags, String installerPackageName,
1249 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001250 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1251 installerPackageName, verificationParams, encryptionParams);
Christopher Tatef1977b42014-03-24 16:25:51 -07001252 }
1253
Christopher Tatef1977b42014-03-24 16:25:51 -07001254 @Override
1255 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1256 int flags, String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001257 final VerificationParams verificationParams = new VerificationParams(null, null,
1258 null, VerificationParams.NO_UID, null);
1259 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null);
Christopher Tatef1977b42014-03-24 16:25:51 -07001260 }
1261
1262 @Override
1263 public void installPackageWithVerification(Uri packageURI,
1264 PackageInstallObserver observer, int flags, String installerPackageName,
1265 Uri verificationURI, ManifestDigest manifestDigest,
1266 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001267 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1268 null, VerificationParams.NO_UID, manifestDigest);
1269 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
1270 encryptionParams);
Christopher Tatef1977b42014-03-24 16:25:51 -07001271 }
1272
1273 @Override
1274 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1275 PackageInstallObserver observer, int flags, String installerPackageName,
1276 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001277 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
1278 encryptionParams);
1279 }
1280
1281 private void installCommon(Uri packageURI,
1282 PackageInstallObserver observer, int flags, String installerPackageName,
1283 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
1284 if (!"file".equals(packageURI.getScheme())) {
1285 throw new UnsupportedOperationException("Only file:// URIs are supported");
1286 }
1287 if (encryptionParams != null) {
1288 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1289 }
1290
1291 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001292 try {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001293 mPM.installPackage(originPath, observer.getBinder(), flags, installerPackageName,
1294 verificationParams, null);
1295 } catch (RemoteException ignored) {
rich cannings706e8ba2012-08-20 13:20:14 -07001296 }
1297 }
1298
1299 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001300 public int installExistingPackage(String packageName)
1301 throws NameNotFoundException {
1302 try {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001303 int res = mPM.installExistingPackageAsUser(packageName, UserHandle.myUserId());
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001304 if (res == INSTALL_FAILED_INVALID_URI) {
1305 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1306 }
1307 return res;
1308 } catch (RemoteException e) {
1309 // Should never happen!
1310 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1311 }
1312 }
1313
1314 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001315 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001316 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001317 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001318 } catch (RemoteException e) {
1319 // Should never happen!
1320 }
1321 }
1322
1323 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001324 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1325 long millisecondsToDelay) {
1326 try {
1327 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1328 } catch (RemoteException e) {
1329 // Should never happen!
1330 }
1331 }
1332
1333 @Override
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001334 public void verifyIntentFilter(int id, int verificationCode, List<String> outFailedDomains) {
1335 try {
1336 mPM.verifyIntentFilter(id, verificationCode, outFailedDomains);
1337 } catch (RemoteException e) {
1338 // Should never happen!
1339 }
1340 }
1341
1342 @Override
1343 public int getIntentVerificationStatus(String packageName, int userId) {
1344 try {
1345 return mPM.getIntentVerificationStatus(packageName, userId);
1346 } catch (RemoteException e) {
1347 // Should never happen!
1348 return PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1349 }
1350 }
1351
1352 @Override
1353 public boolean updateIntentVerificationStatus(String packageName, int status, int userId) {
1354 try {
1355 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1356 } catch (RemoteException e) {
1357 // Should never happen!
1358 return false;
1359 }
1360 }
1361
1362 @Override
1363 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1364 try {
1365 return mPM.getIntentFilterVerifications(packageName);
1366 } catch (RemoteException e) {
1367 // Should never happen!
1368 return null;
1369 }
1370 }
1371
1372 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001373 public List<IntentFilter> getAllIntentFilters(String packageName) {
1374 try {
1375 return mPM.getAllIntentFilters(packageName);
1376 } catch (RemoteException e) {
1377 // Should never happen!
1378 return null;
1379 }
1380 }
1381
1382 @Override
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001383 public String getDefaultBrowserPackageName(int userId) {
1384 try {
1385 return mPM.getDefaultBrowserPackageName(userId);
1386 } catch (RemoteException e) {
1387 // Should never happen!
1388 return null;
1389 }
1390 }
1391
1392 @Override
1393 public boolean setDefaultBrowserPackageName(String packageName, int userId) {
1394 try {
1395 return mPM.setDefaultBrowserPackageName(packageName, userId);
1396 } catch (RemoteException e) {
1397 // Should never happen!
1398 return false;
1399 }
1400 }
1401
1402 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001403 public void setInstallerPackageName(String targetPackage,
1404 String installerPackageName) {
1405 try {
1406 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1407 } catch (RemoteException e) {
1408 // Should never happen!
1409 }
1410 }
1411
1412 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001413 public void movePackage(String packageName, IPackageMoveObserver observer, int flags) {
1414 try {
1415 mPM.movePackage(packageName, observer, flags);
1416 } catch (RemoteException e) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001417 throw e.rethrowAsRuntimeException();
1418 }
1419 }
1420
1421 @Override
1422 public void movePackageAndData(String packageName, String volumeUuid,
1423 IPackageMoveObserver observer) {
1424 try {
1425 mPM.movePackageAndData(packageName, volumeUuid, observer);
1426 } catch (RemoteException e) {
1427 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001428 }
1429 }
1430
1431 @Override
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001432 public @NonNull VolumeInfo getApplicationCurrentVolume(ApplicationInfo app) {
1433 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1434 if (app.isInternal()) {
1435 return Preconditions.checkNotNull(
1436 storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL));
1437 } else if (app.isExternalAsec()) {
1438 final List<VolumeInfo> vols = storage.getVolumes();
1439 for (VolumeInfo vol : vols) {
1440 if ((vol.getType() == VolumeInfo.TYPE_PUBLIC) && vol.isPrimary()) {
1441 return vol;
1442 }
1443 }
1444 throw new IllegalStateException("Failed to find primary public volume");
1445 } else {
1446 return Preconditions.checkNotNull(storage.findVolumeByUuid(app.volumeUuid));
1447 }
1448 }
1449
1450 @Override
1451 public @NonNull List<VolumeInfo> getApplicationCandidateVolumes(ApplicationInfo app) {
1452 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1453 final List<VolumeInfo> vols = storage.getVolumes();
1454 final List<VolumeInfo> candidates = new ArrayList<>();
1455 for (VolumeInfo vol : vols) {
1456 if (isCandidateVolume(app, vol)) {
1457 candidates.add(vol);
1458 }
1459 }
1460 return candidates;
1461 }
1462
1463 private static boolean isCandidateVolume(ApplicationInfo app, VolumeInfo vol) {
1464 // Private internal is always an option
1465 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1466 return true;
1467 }
1468
1469 // System apps and apps demanding internal storage can't be moved
1470 // anywhere else
1471 if (app.isSystemApp()
1472 || app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) {
1473 return false;
1474 }
1475
1476 // Moving into an ASEC on public primary is only an option when app is
1477 // internal, or already in ASEC
1478 if ((vol.getType() == VolumeInfo.TYPE_PUBLIC) && vol.isPrimary()) {
1479 return app.isInternal() || app.isExternalAsec();
1480 }
1481
1482 // Otherwise we can move to any private volume
1483 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1484 }
1485
1486 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001487 public String getInstallerPackageName(String packageName) {
1488 try {
1489 return mPM.getInstallerPackageName(packageName);
1490 } catch (RemoteException e) {
1491 // Should never happen!
1492 }
1493 return null;
1494 }
1495
1496 @Override
1497 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
1498 try {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001499 mPM.deletePackageAsUser(packageName, observer, UserHandle.myUserId(), flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001500 } catch (RemoteException e) {
1501 // Should never happen!
1502 }
1503 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001504
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001505 @Override
1506 public void clearApplicationUserData(String packageName,
1507 IPackageDataObserver observer) {
1508 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001509 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001510 } catch (RemoteException e) {
1511 // Should never happen!
1512 }
1513 }
1514 @Override
1515 public void deleteApplicationCacheFiles(String packageName,
1516 IPackageDataObserver observer) {
1517 try {
1518 mPM.deleteApplicationCacheFiles(packageName, observer);
1519 } catch (RemoteException e) {
1520 // Should never happen!
1521 }
1522 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001523
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001524 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001525 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1526 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001527 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001528 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001529 } catch (RemoteException e) {
1530 // Should never happen!
1531 }
1532 }
1533
1534 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001535 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001536 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001537 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001538 } catch (RemoteException e) {
1539 // Should never happen!
1540 }
1541 }
1542
1543 @Override
Dianne Hackborn0c380492012-08-20 17:23:30 -07001544 public void getPackageSizeInfo(String packageName, int userHandle,
1545 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001546 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001547 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001548 } catch (RemoteException e) {
1549 // Should never happen!
1550 }
1551 }
1552 @Override
1553 public void addPackageToPreferred(String packageName) {
1554 try {
1555 mPM.addPackageToPreferred(packageName);
1556 } catch (RemoteException e) {
1557 // Should never happen!
1558 }
1559 }
1560
1561 @Override
1562 public void removePackageFromPreferred(String packageName) {
1563 try {
1564 mPM.removePackageFromPreferred(packageName);
1565 } catch (RemoteException e) {
1566 // Should never happen!
1567 }
1568 }
1569
1570 @Override
1571 public List<PackageInfo> getPreferredPackages(int flags) {
1572 try {
1573 return mPM.getPreferredPackages(flags);
1574 } catch (RemoteException e) {
1575 // Should never happen!
1576 }
1577 return new ArrayList<PackageInfo>();
1578 }
1579
1580 @Override
1581 public void addPreferredActivity(IntentFilter filter,
1582 int match, ComponentName[] set, ComponentName activity) {
1583 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001584 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001585 } catch (RemoteException e) {
1586 // Should never happen!
1587 }
1588 }
1589
1590 @Override
1591 public void addPreferredActivity(IntentFilter filter, int match,
1592 ComponentName[] set, ComponentName activity, int userId) {
1593 try {
1594 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001595 } catch (RemoteException e) {
1596 // Should never happen!
1597 }
1598 }
1599
1600 @Override
1601 public void replacePreferredActivity(IntentFilter filter,
1602 int match, ComponentName[] set, ComponentName activity) {
1603 try {
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001604 mPM.replacePreferredActivity(filter, match, set, activity, UserHandle.myUserId());
1605 } catch (RemoteException e) {
1606 // Should never happen!
1607 }
1608 }
1609
1610 @Override
1611 public void replacePreferredActivityAsUser(IntentFilter filter,
1612 int match, ComponentName[] set, ComponentName activity,
1613 int userId) {
1614 try {
1615 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001616 } catch (RemoteException e) {
1617 // Should never happen!
1618 }
1619 }
1620
1621 @Override
1622 public void clearPackagePreferredActivities(String packageName) {
1623 try {
1624 mPM.clearPackagePreferredActivities(packageName);
1625 } catch (RemoteException e) {
1626 // Should never happen!
1627 }
1628 }
1629
1630 @Override
1631 public int getPreferredActivities(List<IntentFilter> outFilters,
1632 List<ComponentName> outActivities, String packageName) {
1633 try {
1634 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
1635 } catch (RemoteException e) {
1636 // Should never happen!
1637 }
1638 return 0;
1639 }
1640
1641 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07001642 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
1643 try {
1644 return mPM.getHomeActivities(outActivities);
1645 } catch (RemoteException e) {
1646 // Should never happen!
1647 }
1648 return null;
1649 }
1650
1651 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001652 public void setComponentEnabledSetting(ComponentName componentName,
1653 int newState, int flags) {
1654 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001655 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001656 } catch (RemoteException e) {
1657 // Should never happen!
1658 }
1659 }
1660
1661 @Override
1662 public int getComponentEnabledSetting(ComponentName componentName) {
1663 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001664 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001665 } catch (RemoteException e) {
1666 // Should never happen!
1667 }
1668 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1669 }
1670
1671 @Override
1672 public void setApplicationEnabledSetting(String packageName,
1673 int newState, int flags) {
1674 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001675 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07001676 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001677 } catch (RemoteException e) {
1678 // Should never happen!
1679 }
1680 }
1681
1682 @Override
1683 public int getApplicationEnabledSetting(String packageName) {
1684 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001685 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001686 } catch (RemoteException e) {
1687 // Should never happen!
1688 }
1689 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1690 }
1691
Amith Yamasani655d0e22013-06-12 14:19:10 -07001692 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001693 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07001694 UserHandle user) {
1695 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001696 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07001697 user.getIdentifier());
1698 } catch (RemoteException re) {
1699 // Should never happen!
1700 }
1701 return false;
1702 }
1703
1704 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001705 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001706 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001707 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Amith Yamasani655d0e22013-06-12 14:19:10 -07001708 } catch (RemoteException re) {
1709 // Should never happen!
1710 }
1711 return false;
1712 }
1713
dcashmanc6f22492014-08-14 09:54:51 -07001714 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001715 @Override
1716 public KeySet getKeySetByAlias(String packageName, String alias) {
1717 Preconditions.checkNotNull(packageName);
1718 Preconditions.checkNotNull(alias);
dcashmanc6f22492014-08-14 09:54:51 -07001719 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07001720 try {
dcashmanc6f22492014-08-14 09:54:51 -07001721 ks = mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07001722 } catch (RemoteException e) {
1723 return null;
1724 }
dcashmanc6f22492014-08-14 09:54:51 -07001725 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07001726 }
1727
dcashmanc6f22492014-08-14 09:54:51 -07001728 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001729 @Override
1730 public KeySet getSigningKeySet(String packageName) {
1731 Preconditions.checkNotNull(packageName);
dcashmanc6f22492014-08-14 09:54:51 -07001732 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07001733 try {
dcashmanc6f22492014-08-14 09:54:51 -07001734 ks = mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07001735 } catch (RemoteException e) {
1736 return null;
1737 }
dcashmanc6f22492014-08-14 09:54:51 -07001738 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07001739 }
1740
dcashmanc6f22492014-08-14 09:54:51 -07001741 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001742 @Override
1743 public boolean isSignedBy(String packageName, KeySet ks) {
1744 Preconditions.checkNotNull(packageName);
1745 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07001746 try {
dcashmanc6f22492014-08-14 09:54:51 -07001747 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07001748 } catch (RemoteException e) {
1749 return false;
1750 }
1751 }
1752
dcashmanc6f22492014-08-14 09:54:51 -07001753 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001754 @Override
1755 public boolean isSignedByExactly(String packageName, KeySet ks) {
1756 Preconditions.checkNotNull(packageName);
1757 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07001758 try {
dcashmanc6f22492014-08-14 09:54:51 -07001759 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07001760 } catch (RemoteException e) {
1761 return false;
1762 }
1763 }
1764
Kenny Root0aaa0d92011-09-12 16:42:55 -07001765 /**
1766 * @hide
1767 */
1768 @Override
1769 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
1770 try {
1771 return mPM.getVerifierDeviceIdentity();
1772 } catch (RemoteException e) {
1773 // Should never happen!
1774 }
1775 return null;
1776 }
1777
Jeff Hao9f60c082014-10-28 18:51:07 -07001778 /**
1779 * @hide
1780 */
1781 @Override
1782 public boolean isUpgrade() {
1783 try {
1784 return mPM.isUpgrade();
1785 } catch (RemoteException e) {
1786 return false;
1787 }
1788 }
1789
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001790 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07001791 public PackageInstaller getPackageInstaller() {
1792 synchronized (mLock) {
1793 if (mInstaller == null) {
1794 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07001795 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07001796 mContext.getPackageName(), mContext.getUserId());
1797 } catch (RemoteException e) {
1798 throw e.rethrowAsRuntimeException();
1799 }
1800 }
1801 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001802 }
1803 }
1804
Jeff Sharkey6c833e02014-07-14 22:44:30 -07001805 @Override
1806 public boolean isPackageAvailable(String packageName) {
1807 try {
1808 return mPM.isPackageAvailable(packageName, mContext.getUserId());
1809 } catch (RemoteException e) {
1810 throw e.rethrowAsRuntimeException();
1811 }
1812 }
1813
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001814 /**
1815 * @hide
1816 */
1817 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01001818 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
1819 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001820 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01001821 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00001822 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001823 } catch (RemoteException e) {
1824 // Should never happen!
1825 }
1826 }
1827
1828 /**
1829 * @hide
1830 */
1831 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01001832 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001833 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00001834 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001835 } catch (RemoteException e) {
1836 // Should never happen!
1837 }
1838 }
1839
Nicolas Prevot88cc3462014-05-14 14:51:48 +01001840 /**
1841 * @hide
1842 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001843 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01001844 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
1845 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
1846 return dr;
1847 }
1848 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
1849 }
1850
1851 /**
1852 * @hide
1853 */
1854 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001855 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01001856 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
1857 if (bitmap == null) {
1858 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
1859 }
1860 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001861 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01001862 Drawable dr = null;
1863 if (itemInfo.packageName != null) {
1864 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
1865 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01001866 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01001867 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001868 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01001869 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07001870 }
1871
1872 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
1873 Rect badgeLocation, boolean tryBadgeInPlace) {
1874 final int badgedWidth = drawable.getIntrinsicWidth();
1875 final int badgedHeight = drawable.getIntrinsicHeight();
1876 final boolean canBadgeInPlace = tryBadgeInPlace
1877 && (drawable instanceof BitmapDrawable)
1878 && ((BitmapDrawable) drawable).getBitmap().isMutable();
1879
1880 final Bitmap bitmap;
1881 if (canBadgeInPlace) {
1882 bitmap = ((BitmapDrawable) drawable).getBitmap();
1883 } else {
1884 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
1885 }
1886 Canvas canvas = new Canvas(bitmap);
1887
1888 if (!canBadgeInPlace) {
1889 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
1890 drawable.draw(canvas);
1891 }
1892
1893 if (badgeLocation != null) {
1894 if (badgeLocation.left < 0 || badgeLocation.top < 0
1895 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
1896 throw new IllegalArgumentException("Badge location " + badgeLocation
1897 + " not in badged drawable bounds "
1898 + new Rect(0, 0, badgedWidth, badgedHeight));
1899 }
1900 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
1901
1902 canvas.save();
1903 canvas.translate(badgeLocation.left, badgeLocation.top);
1904 badgeDrawable.draw(canvas);
1905 canvas.restore();
1906 } else {
1907 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
1908 badgeDrawable.draw(canvas);
1909 }
1910
1911 if (!canBadgeInPlace) {
1912 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
1913
1914 if (drawable instanceof BitmapDrawable) {
1915 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
1916 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
1917 }
1918
1919 return mergedDrawable;
1920 }
1921
1922 return drawable;
1923 }
1924
1925 private int getBadgeResIdForUser(int userHandle) {
1926 // Return the framework-provided badge.
1927 UserInfo userInfo = getUserIfProfile(userHandle);
1928 if (userInfo != null && userInfo.isManagedProfile()) {
1929 return com.android.internal.R.drawable.ic_corp_icon_badge;
1930 }
1931 return 0;
1932 }
1933
1934 private UserInfo getUserIfProfile(int userHandle) {
Svetoslav7de2abb2014-09-05 11:28:00 -07001935 List<UserInfo> userProfiles = getUserManager().getProfiles(UserHandle.myUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07001936 for (UserInfo user : userProfiles) {
1937 if (user.id == userHandle) {
1938 return user;
1939 }
1940 }
1941 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01001942 }
1943
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001944 private final ContextImpl mContext;
1945 private final IPackageManager mPM;
1946
1947 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001948 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
1949 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
1950 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
1951 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001952}