blob: 1e1a6130bb186a3599e0e92239155d3a2499bb26 [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
19import android.content.ComponentName;
20import android.content.ContentResolver;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.IntentSender;
24import android.content.pm.ActivityInfo;
25import android.content.pm.ApplicationInfo;
26import android.content.pm.ComponentInfo;
Anonymous Cowardceb1b0b2012-04-24 10:35:16 -070027import android.content.pm.ContainerEncryptionParams;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080028import android.content.pm.FeatureInfo;
29import android.content.pm.IPackageDataObserver;
30import android.content.pm.IPackageDeleteObserver;
31import android.content.pm.IPackageInstallObserver;
32import android.content.pm.IPackageManager;
33import android.content.pm.IPackageMoveObserver;
34import android.content.pm.IPackageStatsObserver;
35import android.content.pm.InstrumentationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070036import android.content.pm.KeySet;
Jeff Sharkey513a0742014-07-08 17:10:32 -070037import android.content.pm.ManifestDigest;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080038import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070039import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010040import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080041import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070042import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080043import android.content.pm.PermissionGroupInfo;
44import android.content.pm.PermissionInfo;
45import android.content.pm.ProviderInfo;
46import android.content.pm.ResolveInfo;
47import android.content.pm.ServiceInfo;
Svetoslavc7d62f02014-09-04 15:39:54 -070048import android.content.pm.UserInfo;
rich cannings706e8ba2012-08-20 13:20:14 -070049import android.content.pm.VerificationParams;
Kenny Root0aaa0d92011-09-12 16:42:55 -070050import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080051import android.content.res.Resources;
52import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
55import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010056import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080057import android.graphics.drawable.Drawable;
58import android.net.Uri;
59import android.os.Process;
60import android.os.RemoteException;
Amith Yamasani67df64b2012-12-14 12:09:36 -080061import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010062import android.os.UserManager;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070063import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080064import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070065import android.view.Display;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070066
67import com.android.internal.annotations.GuardedBy;
dcashman9d2f4412014-06-09 09:27:54 -070068import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010069import com.android.internal.util.UserIcons;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070070
Narayan Kamathcaa71192014-07-16 11:06:43 +010071import dalvik.system.VMRuntime;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080072
73import java.lang.ref.WeakReference;
74import java.util.ArrayList;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080075import java.util.List;
76
77/*package*/
78final class ApplicationPackageManager extends PackageManager {
79 private static final String TAG = "ApplicationPackageManager";
80 private final static boolean DEBUG = false;
81 private final static boolean DEBUG_ICONS = false;
82
Adam Lesinskid00bb5e2014-10-07 12:14:45 -070083 // Default flags to use with PackageManager when no flags are given.
84 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
85
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070086 private final Object mLock = new Object();
87
88 @GuardedBy("mLock")
89 private UserManager mUserManager;
90 @GuardedBy("mLock")
91 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010092
93 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070094 synchronized (mLock) {
95 if (mUserManager == null) {
96 mUserManager = UserManager.get(mContext);
97 }
98 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010099 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100100 }
101
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800102 @Override
103 public PackageInfo getPackageInfo(String packageName, int flags)
104 throws NameNotFoundException {
105 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700106 PackageInfo pi = mPM.getPackageInfo(packageName, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800107 if (pi != null) {
108 return pi;
109 }
110 } catch (RemoteException e) {
111 throw new RuntimeException("Package manager has died", e);
112 }
113
114 throw new NameNotFoundException(packageName);
115 }
116
117 @Override
118 public String[] currentToCanonicalPackageNames(String[] names) {
119 try {
120 return mPM.currentToCanonicalPackageNames(names);
121 } catch (RemoteException e) {
122 throw new RuntimeException("Package manager has died", e);
123 }
124 }
125
126 @Override
127 public String[] canonicalToCurrentPackageNames(String[] names) {
128 try {
129 return mPM.canonicalToCurrentPackageNames(names);
130 } catch (RemoteException e) {
131 throw new RuntimeException("Package manager has died", e);
132 }
133 }
134
135 @Override
136 public Intent getLaunchIntentForPackage(String packageName) {
137 // First see if the package has an INFO activity; the existence of
138 // such an activity is implied to be the desired front-door for the
139 // overall package (such as if it has multiple launcher entries).
140 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
141 intentToResolve.addCategory(Intent.CATEGORY_INFO);
142 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800143 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800144
145 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800146 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800147 // reuse the intent instance
148 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
149 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
150 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800151 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800152 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800153 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800154 return null;
155 }
156 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800157 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800158 intent.setClassName(ris.get(0).activityInfo.packageName,
159 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800160 return intent;
161 }
162
163 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700164 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
165 // Try to find a main leanback_launcher activity.
166 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
167 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
168 intentToResolve.setPackage(packageName);
169 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
170
171 if (ris == null || ris.size() <= 0) {
172 return null;
173 }
174 Intent intent = new Intent(intentToResolve);
175 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
176 intent.setClassName(ris.get(0).activityInfo.packageName,
177 ris.get(0).activityInfo.name);
178 return intent;
179 }
180
181 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800182 public int[] getPackageGids(String packageName)
183 throws NameNotFoundException {
184 try {
185 int[] gids = mPM.getPackageGids(packageName);
186 if (gids == null || gids.length > 0) {
187 return gids;
188 }
189 } catch (RemoteException e) {
190 throw new RuntimeException("Package manager has died", e);
191 }
192
193 throw new NameNotFoundException(packageName);
194 }
195
196 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800197 public int getPackageUid(String packageName, int userHandle)
198 throws NameNotFoundException {
199 try {
200 int uid = mPM.getPackageUid(packageName, userHandle);
201 if (uid >= 0) {
202 return uid;
203 }
204 } catch (RemoteException e) {
205 throw new RuntimeException("Package manager has died", e);
206 }
207
208 throw new NameNotFoundException(packageName);
209 }
210
211 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800212 public PermissionInfo getPermissionInfo(String name, int flags)
213 throws NameNotFoundException {
214 try {
215 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
216 if (pi != null) {
217 return pi;
218 }
219 } catch (RemoteException e) {
220 throw new RuntimeException("Package manager has died", e);
221 }
222
223 throw new NameNotFoundException(name);
224 }
225
226 @Override
227 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
228 throws NameNotFoundException {
229 try {
230 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags);
231 if (pi != null) {
232 return pi;
233 }
234 } catch (RemoteException e) {
235 throw new RuntimeException("Package manager has died", e);
236 }
237
238 throw new NameNotFoundException(group);
239 }
240
241 @Override
242 public PermissionGroupInfo getPermissionGroupInfo(String name,
243 int flags) throws NameNotFoundException {
244 try {
245 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
246 if (pgi != null) {
247 return pgi;
248 }
249 } catch (RemoteException e) {
250 throw new RuntimeException("Package manager has died", e);
251 }
252
253 throw new NameNotFoundException(name);
254 }
255
256 @Override
257 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
258 try {
259 return mPM.getAllPermissionGroups(flags);
260 } catch (RemoteException e) {
261 throw new RuntimeException("Package manager has died", e);
262 }
263 }
264
265 @Override
266 public ApplicationInfo getApplicationInfo(String packageName, int flags)
267 throws NameNotFoundException {
268 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700269 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800270 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100271 // This is a temporary hack. Callers must use
272 // createPackageContext(packageName).getApplicationInfo() to
273 // get the right paths.
274 maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800275 return ai;
276 }
277 } catch (RemoteException e) {
278 throw new RuntimeException("Package manager has died", e);
279 }
280
281 throw new NameNotFoundException(packageName);
282 }
283
Narayan Kamathcaa71192014-07-16 11:06:43 +0100284 private static void maybeAdjustApplicationInfo(ApplicationInfo info) {
285 // If we're dealing with a multi-arch application that has both
286 // 32 and 64 bit shared libraries, we might need to choose the secondary
287 // depending on what the current runtime's instruction set is.
288 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
289 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
290 final String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
291
292 // If the runtimeIsa is the same as the primary isa, then we do nothing.
293 // Everything will be set up correctly because info.nativeLibraryDir will
294 // correspond to the right ISA.
295 if (runtimeIsa.equals(secondaryIsa)) {
296 info.nativeLibraryDir = info.secondaryNativeLibraryDir;
297 }
298 }
299 }
300
301
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800302 @Override
303 public ActivityInfo getActivityInfo(ComponentName className, int flags)
304 throws NameNotFoundException {
305 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700306 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800307 if (ai != null) {
308 return ai;
309 }
310 } catch (RemoteException e) {
311 throw new RuntimeException("Package manager has died", e);
312 }
313
314 throw new NameNotFoundException(className.toString());
315 }
316
317 @Override
318 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
319 throws NameNotFoundException {
320 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700321 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800322 if (ai != null) {
323 return ai;
324 }
325 } catch (RemoteException e) {
326 throw new RuntimeException("Package manager has died", e);
327 }
328
329 throw new NameNotFoundException(className.toString());
330 }
331
332 @Override
333 public ServiceInfo getServiceInfo(ComponentName className, int flags)
334 throws NameNotFoundException {
335 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700336 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800337 if (si != null) {
338 return si;
339 }
340 } catch (RemoteException e) {
341 throw new RuntimeException("Package manager has died", e);
342 }
343
344 throw new NameNotFoundException(className.toString());
345 }
346
347 @Override
348 public ProviderInfo getProviderInfo(ComponentName className, int flags)
349 throws NameNotFoundException {
350 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700351 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800352 if (pi != null) {
353 return pi;
354 }
355 } catch (RemoteException e) {
356 throw new RuntimeException("Package manager has died", e);
357 }
358
359 throw new NameNotFoundException(className.toString());
360 }
361
362 @Override
363 public String[] getSystemSharedLibraryNames() {
364 try {
365 return mPM.getSystemSharedLibraryNames();
366 } catch (RemoteException e) {
367 throw new RuntimeException("Package manager has died", e);
368 }
369 }
370
371 @Override
372 public FeatureInfo[] getSystemAvailableFeatures() {
373 try {
374 return mPM.getSystemAvailableFeatures();
375 } catch (RemoteException e) {
376 throw new RuntimeException("Package manager has died", e);
377 }
378 }
379
380 @Override
381 public boolean hasSystemFeature(String name) {
382 try {
383 return mPM.hasSystemFeature(name);
384 } catch (RemoteException e) {
385 throw new RuntimeException("Package manager has died", e);
386 }
387 }
388
389 @Override
390 public int checkPermission(String permName, String pkgName) {
391 try {
392 return mPM.checkPermission(permName, pkgName);
393 } catch (RemoteException e) {
394 throw new RuntimeException("Package manager has died", e);
395 }
396 }
397
398 @Override
399 public boolean addPermission(PermissionInfo info) {
400 try {
401 return mPM.addPermission(info);
402 } catch (RemoteException e) {
403 throw new RuntimeException("Package manager has died", e);
404 }
405 }
406
407 @Override
408 public boolean addPermissionAsync(PermissionInfo info) {
409 try {
410 return mPM.addPermissionAsync(info);
411 } catch (RemoteException e) {
412 throw new RuntimeException("Package manager has died", e);
413 }
414 }
415
416 @Override
417 public void removePermission(String name) {
418 try {
419 mPM.removePermission(name);
420 } catch (RemoteException e) {
421 throw new RuntimeException("Package manager has died", e);
422 }
423 }
424
425 @Override
Dianne Hackborne639da72012-02-21 15:11:13 -0800426 public void grantPermission(String packageName, String permissionName) {
427 try {
428 mPM.grantPermission(packageName, permissionName);
429 } catch (RemoteException e) {
430 throw new RuntimeException("Package manager has died", e);
431 }
432 }
433
434 @Override
435 public void revokePermission(String packageName, String permissionName) {
436 try {
437 mPM.revokePermission(packageName, permissionName);
438 } catch (RemoteException e) {
439 throw new RuntimeException("Package manager has died", e);
440 }
441 }
442
443 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800444 public int checkSignatures(String pkg1, String pkg2) {
445 try {
446 return mPM.checkSignatures(pkg1, pkg2);
447 } catch (RemoteException e) {
448 throw new RuntimeException("Package manager has died", e);
449 }
450 }
451
452 @Override
453 public int checkSignatures(int uid1, int uid2) {
454 try {
455 return mPM.checkUidSignatures(uid1, uid2);
456 } catch (RemoteException e) {
457 throw new RuntimeException("Package manager has died", e);
458 }
459 }
460
461 @Override
462 public String[] getPackagesForUid(int uid) {
463 try {
464 return mPM.getPackagesForUid(uid);
465 } catch (RemoteException e) {
466 throw new RuntimeException("Package manager has died", e);
467 }
468 }
469
470 @Override
471 public String getNameForUid(int uid) {
472 try {
473 return mPM.getNameForUid(uid);
474 } catch (RemoteException e) {
475 throw new RuntimeException("Package manager has died", e);
476 }
477 }
478
479 @Override
480 public int getUidForSharedUser(String sharedUserName)
481 throws NameNotFoundException {
482 try {
483 int uid = mPM.getUidForSharedUser(sharedUserName);
484 if(uid != -1) {
485 return uid;
486 }
487 } catch (RemoteException e) {
488 throw new RuntimeException("Package manager has died", e);
489 }
490 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
491 }
492
Kenny Roote6cd0c72011-05-19 12:48:14 -0700493 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800494 @Override
495 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700496 return getInstalledPackages(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700497 }
498
499 /** @hide */
500 @Override
501 public List<PackageInfo> getInstalledPackages(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800502 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800503 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
504 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800505 } catch (RemoteException e) {
506 throw new RuntimeException("Package manager has died", e);
507 }
508 }
509
Kenny Roote6cd0c72011-05-19 12:48:14 -0700510 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800511 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800512 public List<PackageInfo> getPackagesHoldingPermissions(
513 String[] permissions, int flags) {
514 final int userId = mContext.getUserId();
515 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800516 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
517 permissions, flags, userId);
518 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800519 } catch (RemoteException e) {
520 throw new RuntimeException("Package manager has died", e);
521 }
522 }
523
524 @SuppressWarnings("unchecked")
525 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800526 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700527 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800528 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800529 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
530 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800531 } catch (RemoteException e) {
532 throw new RuntimeException("Package manager has died", e);
533 }
534 }
535
536 @Override
537 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700538 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700539 }
540
541 @Override
542 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800543 try {
544 return mPM.resolveIntent(
545 intent,
546 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700547 flags,
548 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800549 } catch (RemoteException e) {
550 throw new RuntimeException("Package manager has died", e);
551 }
552 }
553
554 @Override
555 public List<ResolveInfo> queryIntentActivities(Intent intent,
556 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700557 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700558 }
559
560 /** @hide Same as above but for a specific user */
561 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700562 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700563 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800564 try {
565 return mPM.queryIntentActivities(
566 intent,
567 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700568 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700569 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800570 } catch (RemoteException e) {
571 throw new RuntimeException("Package manager has died", e);
572 }
573 }
574
575 @Override
576 public List<ResolveInfo> queryIntentActivityOptions(
577 ComponentName caller, Intent[] specifics, Intent intent,
578 int flags) {
579 final ContentResolver resolver = mContext.getContentResolver();
580
581 String[] specificTypes = null;
582 if (specifics != null) {
583 final int N = specifics.length;
584 for (int i=0; i<N; i++) {
585 Intent sp = specifics[i];
586 if (sp != null) {
587 String t = sp.resolveTypeIfNeeded(resolver);
588 if (t != null) {
589 if (specificTypes == null) {
590 specificTypes = new String[N];
591 }
592 specificTypes[i] = t;
593 }
594 }
595 }
596 }
597
598 try {
599 return mPM.queryIntentActivityOptions(caller, specifics,
600 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700601 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800602 } catch (RemoteException e) {
603 throw new RuntimeException("Package manager has died", e);
604 }
605 }
606
Amith Yamasanif203aee2012-08-29 18:41:53 -0700607 /**
608 * @hide
609 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800610 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700611 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800612 try {
613 return mPM.queryIntentReceivers(
614 intent,
615 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700616 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -0700617 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800618 } catch (RemoteException e) {
619 throw new RuntimeException("Package manager has died", e);
620 }
621 }
622
623 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700624 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700625 return queryBroadcastReceivers(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700626 }
627
628 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800629 public ResolveInfo resolveService(Intent intent, int flags) {
630 try {
631 return mPM.resolveService(
632 intent,
633 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700634 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700635 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800636 } catch (RemoteException e) {
637 throw new RuntimeException("Package manager has died", e);
638 }
639 }
640
641 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700642 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800643 try {
644 return mPM.queryIntentServices(
645 intent,
646 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700647 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700648 userId);
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> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700656 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700657 }
658
659 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700660 public List<ResolveInfo> queryIntentContentProvidersAsUser(
661 Intent intent, int flags, int userId) {
662 try {
663 return mPM.queryIntentContentProviders(intent,
664 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
665 } catch (RemoteException e) {
666 throw new RuntimeException("Package manager has died", e);
667 }
668 }
669
670 @Override
671 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
672 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
673 }
674
675 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100676 public ProviderInfo resolveContentProvider(String name, int flags) {
677 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
678 }
679
680 /** @hide **/
681 @Override
682 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800683 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100684 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800685 } catch (RemoteException e) {
686 throw new RuntimeException("Package manager has died", e);
687 }
688 }
689
690 @Override
691 public List<ProviderInfo> queryContentProviders(String processName,
692 int uid, int flags) {
693 try {
694 return mPM.queryContentProviders(processName, uid, flags);
695 } catch (RemoteException e) {
696 throw new RuntimeException("Package manager has died", e);
697 }
698 }
699
700 @Override
701 public InstrumentationInfo getInstrumentationInfo(
702 ComponentName className, int flags)
703 throws NameNotFoundException {
704 try {
705 InstrumentationInfo ii = mPM.getInstrumentationInfo(
706 className, flags);
707 if (ii != null) {
708 return ii;
709 }
710 } catch (RemoteException e) {
711 throw new RuntimeException("Package manager has died", e);
712 }
713
714 throw new NameNotFoundException(className.toString());
715 }
716
717 @Override
718 public List<InstrumentationInfo> queryInstrumentation(
719 String targetPackage, int flags) {
720 try {
721 return mPM.queryInstrumentation(targetPackage, flags);
722 } catch (RemoteException e) {
723 throw new RuntimeException("Package manager has died", e);
724 }
725 }
726
727 @Override public Drawable getDrawable(String packageName, int resid,
728 ApplicationInfo appInfo) {
729 ResourceName name = new ResourceName(packageName, resid);
730 Drawable dr = getCachedIcon(name);
731 if (dr != null) {
732 return dr;
733 }
734 if (appInfo == null) {
735 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700736 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800737 } catch (NameNotFoundException e) {
738 return null;
739 }
740 }
741 try {
742 Resources r = getResourcesForApplication(appInfo);
743 dr = r.getDrawable(resid);
744 if (false) {
745 RuntimeException e = new RuntimeException("here");
746 e.fillInStackTrace();
747 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resid)
748 + " from package " + packageName
749 + ": app scale=" + r.getCompatibilityInfo().applicationScale
750 + ", caller scale=" + mContext.getResources().getCompatibilityInfo().applicationScale,
751 e);
752 }
753 if (DEBUG_ICONS) Log.v(TAG, "Getting drawable 0x"
754 + Integer.toHexString(resid) + " from " + r
755 + ": " + dr);
756 putCachedIcon(name, dr);
757 return dr;
758 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700759 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800760 + appInfo.packageName);
Joe Onorato08f16542011-01-20 11:49:56 -0800761 } catch (Resources.NotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -0700762 Log.w("PackageManager", "Failure retrieving resources for "
Joe Onorato08f16542011-01-20 11:49:56 -0800763 + appInfo.packageName + ": " + e.getMessage());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800764 } catch (RuntimeException e) {
765 // If an exception was thrown, fall through to return
766 // default icon.
767 Log.w("PackageManager", "Failure retrieving icon 0x"
768 + Integer.toHexString(resid) + " in package "
769 + packageName, e);
770 }
771 return null;
772 }
773
774 @Override public Drawable getActivityIcon(ComponentName activityName)
775 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700776 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800777 }
778
779 @Override public Drawable getActivityIcon(Intent intent)
780 throws NameNotFoundException {
781 if (intent.getComponent() != null) {
782 return getActivityIcon(intent.getComponent());
783 }
784
785 ResolveInfo info = resolveActivity(
786 intent, PackageManager.MATCH_DEFAULT_ONLY);
787 if (info != null) {
788 return info.activityInfo.loadIcon(this);
789 }
790
Romain Guy39fe17c2011-11-30 10:34:07 -0800791 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800792 }
793
794 @Override public Drawable getDefaultActivityIcon() {
795 return Resources.getSystem().getDrawable(
796 com.android.internal.R.drawable.sym_def_app_icon);
797 }
798
799 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
800 return info.loadIcon(this);
801 }
802
803 @Override public Drawable getApplicationIcon(String packageName)
804 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700805 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800806 }
807
808 @Override
Jose Limaf78e3122014-03-06 12:13:15 -0800809 public Drawable getActivityBanner(ComponentName activityName)
810 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700811 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -0800812 }
813
814 @Override
815 public Drawable getActivityBanner(Intent intent)
816 throws NameNotFoundException {
817 if (intent.getComponent() != null) {
818 return getActivityBanner(intent.getComponent());
819 }
820
821 ResolveInfo info = resolveActivity(
822 intent, PackageManager.MATCH_DEFAULT_ONLY);
823 if (info != null) {
824 return info.activityInfo.loadBanner(this);
825 }
826
827 throw new NameNotFoundException(intent.toUri(0));
828 }
829
830 @Override
831 public Drawable getApplicationBanner(ApplicationInfo info) {
832 return info.loadBanner(this);
833 }
834
835 @Override
836 public Drawable getApplicationBanner(String packageName)
837 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700838 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -0800839 }
840
841 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800842 public Drawable getActivityLogo(ComponentName activityName)
843 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700844 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800845 }
846
847 @Override
848 public Drawable getActivityLogo(Intent intent)
849 throws NameNotFoundException {
850 if (intent.getComponent() != null) {
851 return getActivityLogo(intent.getComponent());
852 }
853
854 ResolveInfo info = resolveActivity(
855 intent, PackageManager.MATCH_DEFAULT_ONLY);
856 if (info != null) {
857 return info.activityInfo.loadLogo(this);
858 }
859
860 throw new NameNotFoundException(intent.toUri(0));
861 }
862
863 @Override
864 public Drawable getApplicationLogo(ApplicationInfo info) {
865 return info.loadLogo(this);
866 }
867
868 @Override
869 public Drawable getApplicationLogo(String packageName)
870 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700871 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800872 }
873
Svetoslavc7d62f02014-09-04 15:39:54 -0700874 @Override
875 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
876 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
877 if (badgeResId == 0) {
878 return icon;
879 }
880 Drawable badgeIcon = getDrawable("system", badgeResId, null);
881 return getBadgedDrawable(icon, badgeIcon, null, true);
882 }
883
884 @Override
885 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
886 Rect badgeLocation, int badgeDensity) {
887 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
888 if (badgeDrawable == null) {
889 return drawable;
890 }
891 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
892 }
893
894 @Override
895 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
896 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
897 if (userInfo != null && userInfo.isManagedProfile()) {
898 if (density <= 0) {
899 density = mContext.getResources().getDisplayMetrics().densityDpi;
900 }
901 return Resources.getSystem().getDrawableForDensity(
902 com.android.internal.R.drawable.ic_corp_badge, density);
903 }
904 return null;
905 }
906
907 @Override
908 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
909 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
910 if (userInfo != null && userInfo.isManagedProfile()) {
911 return Resources.getSystem().getString(
912 com.android.internal.R.string.managed_profile_label_badge, label);
913 }
914 return label;
915 }
916
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800917 @Override public Resources getResourcesForActivity(
918 ComponentName activityName) throws NameNotFoundException {
919 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700920 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800921 }
922
923 @Override public Resources getResourcesForApplication(
924 ApplicationInfo app) throws NameNotFoundException {
925 if (app.packageName.equals("system")) {
926 return mContext.mMainThread.getSystemContext().getResources();
927 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700928 final boolean sameUid = (app.uid == Process.myUid());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800929 Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700930 sameUid ? app.sourceDir : app.publicSourceDir,
931 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700932 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
933 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800934 if (r != null) {
935 return r;
936 }
937 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
938 }
939
940 @Override public Resources getResourcesForApplication(
941 String appPackageName) throws NameNotFoundException {
942 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700943 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800944 }
945
Amith Yamasani98edc952012-09-25 14:09:27 -0700946 /** @hide */
947 @Override
948 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
949 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700950 if (userId < 0) {
951 throw new IllegalArgumentException(
952 "Call does not support special user #" + userId);
953 }
954 if ("system".equals(appPackageName)) {
955 return mContext.mMainThread.getSystemContext().getResources();
956 }
Amith Yamasani98edc952012-09-25 14:09:27 -0700957 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700958 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -0700959 if (ai != null) {
960 return getResourcesForApplication(ai);
961 }
962 } catch (RemoteException e) {
963 throw new RuntimeException("Package manager has died", e);
964 }
965 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
966 }
967
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800968 int mCachedSafeMode = -1;
969 @Override public boolean isSafeMode() {
970 try {
971 if (mCachedSafeMode < 0) {
972 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
973 }
974 return mCachedSafeMode != 0;
975 } catch (RemoteException e) {
976 throw new RuntimeException("Package manager has died", e);
977 }
978 }
979
980 static void configurationChanged() {
981 synchronized (sSync) {
982 sIconCache.clear();
983 sStringCache.clear();
984 }
985 }
986
987 ApplicationPackageManager(ContextImpl context,
988 IPackageManager pm) {
989 mContext = context;
990 mPM = pm;
991 }
992
993 private Drawable getCachedIcon(ResourceName name) {
994 synchronized (sSync) {
Romain Guy39fe17c2011-11-30 10:34:07 -0800995 WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800996 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
997 + name + ": " + wr);
998 if (wr != null) { // we have the activity
Romain Guy39fe17c2011-11-30 10:34:07 -0800999 Drawable.ConstantState state = wr.get();
1000 if (state != null) {
1001 if (DEBUG_ICONS) {
1002 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1003 }
1004 // Note: It's okay here to not use the newDrawable(Resources) variant
1005 // of the API. The ConstantState comes from a drawable that was
1006 // originally created by passing the proper app Resources instance
1007 // which means the state should already contain the proper
1008 // resources specific information (like density.) See
1009 // BitmapDrawable.BitmapState for instance.
1010 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001011 }
1012 // our entry has been purged
1013 sIconCache.remove(name);
1014 }
1015 }
1016 return null;
1017 }
1018
1019 private void putCachedIcon(ResourceName name, Drawable dr) {
1020 synchronized (sSync) {
Romain Guy39fe17c2011-11-30 10:34:07 -08001021 sIconCache.put(name, new WeakReference<Drawable.ConstantState>(dr.getConstantState()));
1022 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001023 }
1024 }
1025
Romain Guy39fe17c2011-11-30 10:34:07 -08001026 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001027 boolean immediateGc = false;
1028 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1029 immediateGc = true;
1030 }
1031 if (pkgList != null && (pkgList.length > 0)) {
1032 boolean needCleanup = false;
1033 for (String ssp : pkgList) {
1034 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001035 for (int i=sIconCache.size()-1; i>=0; i--) {
1036 ResourceName nm = sIconCache.keyAt(i);
1037 if (nm.packageName.equals(ssp)) {
1038 //Log.i(TAG, "Removing cached drawable for " + nm);
1039 sIconCache.removeAt(i);
1040 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001041 }
1042 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001043 for (int i=sStringCache.size()-1; i>=0; i--) {
1044 ResourceName nm = sStringCache.keyAt(i);
1045 if (nm.packageName.equals(ssp)) {
1046 //Log.i(TAG, "Removing cached string for " + nm);
1047 sStringCache.removeAt(i);
1048 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001049 }
1050 }
1051 }
1052 }
1053 if (needCleanup || hasPkgInfo) {
1054 if (immediateGc) {
1055 // Schedule an immediate gc.
1056 Runtime.getRuntime().gc();
1057 } else {
1058 ActivityThread.currentActivityThread().scheduleGcIdler();
1059 }
1060 }
1061 }
1062 }
1063
1064 private static final class ResourceName {
1065 final String packageName;
1066 final int iconId;
1067
1068 ResourceName(String _packageName, int _iconId) {
1069 packageName = _packageName;
1070 iconId = _iconId;
1071 }
1072
1073 ResourceName(ApplicationInfo aInfo, int _iconId) {
1074 this(aInfo.packageName, _iconId);
1075 }
1076
1077 ResourceName(ComponentInfo cInfo, int _iconId) {
1078 this(cInfo.applicationInfo.packageName, _iconId);
1079 }
1080
1081 ResourceName(ResolveInfo rInfo, int _iconId) {
1082 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1083 }
1084
1085 @Override
1086 public boolean equals(Object o) {
1087 if (this == o) return true;
1088 if (o == null || getClass() != o.getClass()) return false;
1089
1090 ResourceName that = (ResourceName) o;
1091
1092 if (iconId != that.iconId) return false;
1093 return !(packageName != null ?
1094 !packageName.equals(that.packageName) : that.packageName != null);
1095
1096 }
1097
1098 @Override
1099 public int hashCode() {
1100 int result;
1101 result = packageName.hashCode();
1102 result = 31 * result + iconId;
1103 return result;
1104 }
1105
1106 @Override
1107 public String toString() {
1108 return "{ResourceName " + packageName + " / " + iconId + "}";
1109 }
1110 }
1111
1112 private CharSequence getCachedString(ResourceName name) {
1113 synchronized (sSync) {
1114 WeakReference<CharSequence> wr = sStringCache.get(name);
1115 if (wr != null) { // we have the activity
1116 CharSequence cs = wr.get();
1117 if (cs != null) {
1118 return cs;
1119 }
1120 // our entry has been purged
1121 sStringCache.remove(name);
1122 }
1123 }
1124 return null;
1125 }
1126
1127 private void putCachedString(ResourceName name, CharSequence cs) {
1128 synchronized (sSync) {
1129 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1130 }
1131 }
1132
1133 @Override
1134 public CharSequence getText(String packageName, int resid,
1135 ApplicationInfo appInfo) {
1136 ResourceName name = new ResourceName(packageName, resid);
1137 CharSequence text = getCachedString(name);
1138 if (text != null) {
1139 return text;
1140 }
1141 if (appInfo == null) {
1142 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001143 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001144 } catch (NameNotFoundException e) {
1145 return null;
1146 }
1147 }
1148 try {
1149 Resources r = getResourcesForApplication(appInfo);
1150 text = r.getText(resid);
1151 putCachedString(name, text);
1152 return text;
1153 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001154 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001155 + appInfo.packageName);
1156 } catch (RuntimeException e) {
1157 // If an exception was thrown, fall through to return
1158 // default icon.
1159 Log.w("PackageManager", "Failure retrieving text 0x"
1160 + Integer.toHexString(resid) + " in package "
1161 + packageName, e);
1162 }
1163 return null;
1164 }
1165
1166 @Override
1167 public XmlResourceParser getXml(String packageName, int resid,
1168 ApplicationInfo appInfo) {
1169 if (appInfo == null) {
1170 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001171 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001172 } catch (NameNotFoundException e) {
1173 return null;
1174 }
1175 }
1176 try {
1177 Resources r = getResourcesForApplication(appInfo);
1178 return r.getXml(resid);
1179 } catch (RuntimeException e) {
1180 // If an exception was thrown, fall through to return
1181 // default icon.
1182 Log.w("PackageManager", "Failure retrieving xml 0x"
1183 + Integer.toHexString(resid) + " in package "
1184 + packageName, e);
1185 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001186 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001187 + appInfo.packageName);
1188 }
1189 return null;
1190 }
1191
1192 @Override
1193 public CharSequence getApplicationLabel(ApplicationInfo info) {
1194 return info.loadLabel(this);
1195 }
1196
1197 @Override
1198 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1199 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001200 final VerificationParams verificationParams = new VerificationParams(null, null,
1201 null, VerificationParams.NO_UID, null);
1202 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1203 installerPackageName, verificationParams, null);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001204 }
1205
1206 @Override
Kenny Root5ab21572011-07-27 11:11:19 -07001207 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1208 int flags, String installerPackageName, Uri verificationURI,
Rich Canningse1d7c712012-08-08 12:46:06 -07001209 ManifestDigest manifestDigest, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001210 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1211 null, VerificationParams.NO_UID, manifestDigest);
1212 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1213 installerPackageName, verificationParams, encryptionParams);
Kenny Root5ab21572011-07-27 11:11:19 -07001214 }
1215
1216 @Override
John Spurlock8a985d22014-02-25 09:40:05 -05001217 public void installPackageWithVerificationAndEncryption(Uri packageURI,
rich cannings706e8ba2012-08-20 13:20:14 -07001218 IPackageInstallObserver observer, int flags, String installerPackageName,
1219 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001220 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1221 installerPackageName, verificationParams, encryptionParams);
Christopher Tatef1977b42014-03-24 16:25:51 -07001222 }
1223
Christopher Tatef1977b42014-03-24 16:25:51 -07001224 @Override
1225 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1226 int flags, String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001227 final VerificationParams verificationParams = new VerificationParams(null, null,
1228 null, VerificationParams.NO_UID, null);
1229 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null);
Christopher Tatef1977b42014-03-24 16:25:51 -07001230 }
1231
1232 @Override
1233 public void installPackageWithVerification(Uri packageURI,
1234 PackageInstallObserver observer, int flags, String installerPackageName,
1235 Uri verificationURI, ManifestDigest manifestDigest,
1236 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001237 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1238 null, VerificationParams.NO_UID, manifestDigest);
1239 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
1240 encryptionParams);
Christopher Tatef1977b42014-03-24 16:25:51 -07001241 }
1242
1243 @Override
1244 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1245 PackageInstallObserver observer, int flags, String installerPackageName,
1246 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001247 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
1248 encryptionParams);
1249 }
1250
1251 private void installCommon(Uri packageURI,
1252 PackageInstallObserver observer, int flags, String installerPackageName,
1253 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
1254 if (!"file".equals(packageURI.getScheme())) {
1255 throw new UnsupportedOperationException("Only file:// URIs are supported");
1256 }
1257 if (encryptionParams != null) {
1258 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1259 }
1260
1261 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001262 try {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001263 mPM.installPackage(originPath, observer.getBinder(), flags, installerPackageName,
1264 verificationParams, null);
1265 } catch (RemoteException ignored) {
rich cannings706e8ba2012-08-20 13:20:14 -07001266 }
1267 }
1268
1269 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001270 public int installExistingPackage(String packageName)
1271 throws NameNotFoundException {
1272 try {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001273 int res = mPM.installExistingPackageAsUser(packageName, UserHandle.myUserId());
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001274 if (res == INSTALL_FAILED_INVALID_URI) {
1275 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1276 }
1277 return res;
1278 } catch (RemoteException e) {
1279 // Should never happen!
1280 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1281 }
1282 }
1283
1284 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001285 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001286 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001287 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001288 } catch (RemoteException e) {
1289 // Should never happen!
1290 }
1291 }
1292
1293 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001294 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1295 long millisecondsToDelay) {
1296 try {
1297 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1298 } catch (RemoteException e) {
1299 // Should never happen!
1300 }
1301 }
1302
1303 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001304 public void setInstallerPackageName(String targetPackage,
1305 String installerPackageName) {
1306 try {
1307 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1308 } catch (RemoteException e) {
1309 // Should never happen!
1310 }
1311 }
1312
1313 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001314 public void movePackage(String packageName, IPackageMoveObserver observer, int flags) {
1315 try {
1316 mPM.movePackage(packageName, observer, flags);
1317 } catch (RemoteException e) {
1318 // Should never happen!
1319 }
1320 }
1321
1322 @Override
1323 public String getInstallerPackageName(String packageName) {
1324 try {
1325 return mPM.getInstallerPackageName(packageName);
1326 } catch (RemoteException e) {
1327 // Should never happen!
1328 }
1329 return null;
1330 }
1331
1332 @Override
1333 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
1334 try {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001335 mPM.deletePackageAsUser(packageName, observer, UserHandle.myUserId(), flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001336 } catch (RemoteException e) {
1337 // Should never happen!
1338 }
1339 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001340
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001341 @Override
1342 public void clearApplicationUserData(String packageName,
1343 IPackageDataObserver observer) {
1344 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001345 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001346 } catch (RemoteException e) {
1347 // Should never happen!
1348 }
1349 }
1350 @Override
1351 public void deleteApplicationCacheFiles(String packageName,
1352 IPackageDataObserver observer) {
1353 try {
1354 mPM.deleteApplicationCacheFiles(packageName, observer);
1355 } catch (RemoteException e) {
1356 // Should never happen!
1357 }
1358 }
1359 @Override
1360 public void freeStorageAndNotify(long idealStorageSize, IPackageDataObserver observer) {
1361 try {
1362 mPM.freeStorageAndNotify(idealStorageSize, observer);
1363 } catch (RemoteException e) {
1364 // Should never happen!
1365 }
1366 }
1367
1368 @Override
1369 public void freeStorage(long freeStorageSize, IntentSender pi) {
1370 try {
1371 mPM.freeStorage(freeStorageSize, pi);
1372 } catch (RemoteException e) {
1373 // Should never happen!
1374 }
1375 }
1376
1377 @Override
Dianne Hackborn0c380492012-08-20 17:23:30 -07001378 public void getPackageSizeInfo(String packageName, int userHandle,
1379 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001380 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001381 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001382 } catch (RemoteException e) {
1383 // Should never happen!
1384 }
1385 }
1386 @Override
1387 public void addPackageToPreferred(String packageName) {
1388 try {
1389 mPM.addPackageToPreferred(packageName);
1390 } catch (RemoteException e) {
1391 // Should never happen!
1392 }
1393 }
1394
1395 @Override
1396 public void removePackageFromPreferred(String packageName) {
1397 try {
1398 mPM.removePackageFromPreferred(packageName);
1399 } catch (RemoteException e) {
1400 // Should never happen!
1401 }
1402 }
1403
1404 @Override
1405 public List<PackageInfo> getPreferredPackages(int flags) {
1406 try {
1407 return mPM.getPreferredPackages(flags);
1408 } catch (RemoteException e) {
1409 // Should never happen!
1410 }
1411 return new ArrayList<PackageInfo>();
1412 }
1413
1414 @Override
1415 public void addPreferredActivity(IntentFilter filter,
1416 int match, ComponentName[] set, ComponentName activity) {
1417 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001418 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001419 } catch (RemoteException e) {
1420 // Should never happen!
1421 }
1422 }
1423
1424 @Override
1425 public void addPreferredActivity(IntentFilter filter, int match,
1426 ComponentName[] set, ComponentName activity, int userId) {
1427 try {
1428 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001429 } catch (RemoteException e) {
1430 // Should never happen!
1431 }
1432 }
1433
1434 @Override
1435 public void replacePreferredActivity(IntentFilter filter,
1436 int match, ComponentName[] set, ComponentName activity) {
1437 try {
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001438 mPM.replacePreferredActivity(filter, match, set, activity, UserHandle.myUserId());
1439 } catch (RemoteException e) {
1440 // Should never happen!
1441 }
1442 }
1443
1444 @Override
1445 public void replacePreferredActivityAsUser(IntentFilter filter,
1446 int match, ComponentName[] set, ComponentName activity,
1447 int userId) {
1448 try {
1449 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001450 } catch (RemoteException e) {
1451 // Should never happen!
1452 }
1453 }
1454
1455 @Override
1456 public void clearPackagePreferredActivities(String packageName) {
1457 try {
1458 mPM.clearPackagePreferredActivities(packageName);
1459 } catch (RemoteException e) {
1460 // Should never happen!
1461 }
1462 }
1463
1464 @Override
1465 public int getPreferredActivities(List<IntentFilter> outFilters,
1466 List<ComponentName> outActivities, String packageName) {
1467 try {
1468 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
1469 } catch (RemoteException e) {
1470 // Should never happen!
1471 }
1472 return 0;
1473 }
1474
1475 @Override
Christopher Tatea2a08502013-09-05 16:38:58 -07001476 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
1477 try {
1478 return mPM.getHomeActivities(outActivities);
1479 } catch (RemoteException e) {
1480 // Should never happen!
1481 }
1482 return null;
1483 }
1484
1485 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001486 public void setComponentEnabledSetting(ComponentName componentName,
1487 int newState, int flags) {
1488 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001489 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001490 } catch (RemoteException e) {
1491 // Should never happen!
1492 }
1493 }
1494
1495 @Override
1496 public int getComponentEnabledSetting(ComponentName componentName) {
1497 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001498 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001499 } catch (RemoteException e) {
1500 // Should never happen!
1501 }
1502 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1503 }
1504
1505 @Override
1506 public void setApplicationEnabledSetting(String packageName,
1507 int newState, int flags) {
1508 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001509 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07001510 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001511 } catch (RemoteException e) {
1512 // Should never happen!
1513 }
1514 }
1515
1516 @Override
1517 public int getApplicationEnabledSetting(String packageName) {
1518 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001519 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001520 } catch (RemoteException e) {
1521 // Should never happen!
1522 }
1523 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1524 }
1525
Amith Yamasani655d0e22013-06-12 14:19:10 -07001526 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001527 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07001528 UserHandle user) {
1529 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001530 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07001531 user.getIdentifier());
1532 } catch (RemoteException re) {
1533 // Should never happen!
1534 }
1535 return false;
1536 }
1537
1538 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001539 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001540 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001541 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Amith Yamasani655d0e22013-06-12 14:19:10 -07001542 } catch (RemoteException re) {
1543 // Should never happen!
1544 }
1545 return false;
1546 }
1547
dcashmanc6f22492014-08-14 09:54:51 -07001548 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001549 @Override
1550 public KeySet getKeySetByAlias(String packageName, String alias) {
1551 Preconditions.checkNotNull(packageName);
1552 Preconditions.checkNotNull(alias);
dcashmanc6f22492014-08-14 09:54:51 -07001553 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07001554 try {
dcashmanc6f22492014-08-14 09:54:51 -07001555 ks = mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07001556 } catch (RemoteException e) {
1557 return null;
1558 }
dcashmanc6f22492014-08-14 09:54:51 -07001559 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07001560 }
1561
dcashmanc6f22492014-08-14 09:54:51 -07001562 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001563 @Override
1564 public KeySet getSigningKeySet(String packageName) {
1565 Preconditions.checkNotNull(packageName);
dcashmanc6f22492014-08-14 09:54:51 -07001566 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07001567 try {
dcashmanc6f22492014-08-14 09:54:51 -07001568 ks = mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07001569 } catch (RemoteException e) {
1570 return null;
1571 }
dcashmanc6f22492014-08-14 09:54:51 -07001572 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07001573 }
1574
dcashmanc6f22492014-08-14 09:54:51 -07001575 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001576 @Override
1577 public boolean isSignedBy(String packageName, KeySet ks) {
1578 Preconditions.checkNotNull(packageName);
1579 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07001580 try {
dcashmanc6f22492014-08-14 09:54:51 -07001581 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07001582 } catch (RemoteException e) {
1583 return false;
1584 }
1585 }
1586
dcashmanc6f22492014-08-14 09:54:51 -07001587 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001588 @Override
1589 public boolean isSignedByExactly(String packageName, KeySet ks) {
1590 Preconditions.checkNotNull(packageName);
1591 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07001592 try {
dcashmanc6f22492014-08-14 09:54:51 -07001593 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07001594 } catch (RemoteException e) {
1595 return false;
1596 }
1597 }
1598
Kenny Root0aaa0d92011-09-12 16:42:55 -07001599 /**
1600 * @hide
1601 */
1602 @Override
1603 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
1604 try {
1605 return mPM.getVerifierDeviceIdentity();
1606 } catch (RemoteException e) {
1607 // Should never happen!
1608 }
1609 return null;
1610 }
1611
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001612 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07001613 public PackageInstaller getPackageInstaller() {
1614 synchronized (mLock) {
1615 if (mInstaller == null) {
1616 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07001617 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07001618 mContext.getPackageName(), mContext.getUserId());
1619 } catch (RemoteException e) {
1620 throw e.rethrowAsRuntimeException();
1621 }
1622 }
1623 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001624 }
1625 }
1626
Jeff Sharkey6c833e02014-07-14 22:44:30 -07001627 @Override
1628 public boolean isPackageAvailable(String packageName) {
1629 try {
1630 return mPM.isPackageAvailable(packageName, mContext.getUserId());
1631 } catch (RemoteException e) {
1632 throw e.rethrowAsRuntimeException();
1633 }
1634 }
1635
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001636 /**
1637 * @hide
1638 */
1639 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01001640 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
1641 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001642 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01001643 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
1644 mContext.getUserId(), sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001645 } catch (RemoteException e) {
1646 // Should never happen!
1647 }
1648 }
1649
1650 /**
1651 * @hide
1652 */
1653 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01001654 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001655 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01001656 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName(),
1657 mContext.getUserId());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001658 } catch (RemoteException e) {
1659 // Should never happen!
1660 }
1661 }
1662
Nicolas Prevot88cc3462014-05-14 14:51:48 +01001663 /**
1664 * @hide
1665 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001666 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
1667 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01001668 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
1669 if (bitmap == null) {
1670 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
1671 }
1672 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001673 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01001674 Drawable dr = null;
1675 if (itemInfo.packageName != null) {
1676 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
1677 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01001678 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01001679 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001680 }
Amith Yamasanifdf169c2014-09-07 15:45:57 -07001681 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
Svetoslavc7d62f02014-09-04 15:39:54 -07001682 }
1683
1684 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
1685 Rect badgeLocation, boolean tryBadgeInPlace) {
1686 final int badgedWidth = drawable.getIntrinsicWidth();
1687 final int badgedHeight = drawable.getIntrinsicHeight();
1688 final boolean canBadgeInPlace = tryBadgeInPlace
1689 && (drawable instanceof BitmapDrawable)
1690 && ((BitmapDrawable) drawable).getBitmap().isMutable();
1691
1692 final Bitmap bitmap;
1693 if (canBadgeInPlace) {
1694 bitmap = ((BitmapDrawable) drawable).getBitmap();
1695 } else {
1696 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
1697 }
1698 Canvas canvas = new Canvas(bitmap);
1699
1700 if (!canBadgeInPlace) {
1701 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
1702 drawable.draw(canvas);
1703 }
1704
1705 if (badgeLocation != null) {
1706 if (badgeLocation.left < 0 || badgeLocation.top < 0
1707 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
1708 throw new IllegalArgumentException("Badge location " + badgeLocation
1709 + " not in badged drawable bounds "
1710 + new Rect(0, 0, badgedWidth, badgedHeight));
1711 }
1712 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
1713
1714 canvas.save();
1715 canvas.translate(badgeLocation.left, badgeLocation.top);
1716 badgeDrawable.draw(canvas);
1717 canvas.restore();
1718 } else {
1719 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
1720 badgeDrawable.draw(canvas);
1721 }
1722
1723 if (!canBadgeInPlace) {
1724 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
1725
1726 if (drawable instanceof BitmapDrawable) {
1727 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
1728 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
1729 }
1730
1731 return mergedDrawable;
1732 }
1733
1734 return drawable;
1735 }
1736
1737 private int getBadgeResIdForUser(int userHandle) {
1738 // Return the framework-provided badge.
1739 UserInfo userInfo = getUserIfProfile(userHandle);
1740 if (userInfo != null && userInfo.isManagedProfile()) {
1741 return com.android.internal.R.drawable.ic_corp_icon_badge;
1742 }
1743 return 0;
1744 }
1745
1746 private UserInfo getUserIfProfile(int userHandle) {
Svetoslav7de2abb2014-09-05 11:28:00 -07001747 List<UserInfo> userProfiles = getUserManager().getProfiles(UserHandle.myUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07001748 for (UserInfo user : userProfiles) {
1749 if (user.id == userHandle) {
1750 return user;
1751 }
1752 }
1753 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01001754 }
1755
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001756 private final ContextImpl mContext;
1757 private final IPackageManager mPM;
1758
1759 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001760 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
1761 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
1762 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
1763 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001764}