blob: 460e68cfb14f09a003d6e9ae678698d4f6909983 [file] [log] [blame]
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.DrawableRes;
Alan Viveretteecd585a2015-04-13 10:32:51 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.StringRes;
23import android.annotation.XmlRes;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080024import android.content.ComponentName;
25import android.content.ContentResolver;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.IntentSender;
29import android.content.pm.ActivityInfo;
30import android.content.pm.ApplicationInfo;
31import android.content.pm.ComponentInfo;
Anonymous Cowardceb1b0b2012-04-24 10:35:16 -070032import android.content.pm.ContainerEncryptionParams;
Svet Ganov2acf0632015-11-24 19:10:59 -080033import android.content.pm.EphemeralApplicationInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080034import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070035import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080036import android.content.pm.IPackageDataObserver;
37import android.content.pm.IPackageDeleteObserver;
38import android.content.pm.IPackageInstallObserver;
39import android.content.pm.IPackageManager;
40import android.content.pm.IPackageMoveObserver;
41import android.content.pm.IPackageStatsObserver;
42import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080043import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070044import android.content.pm.KeySet;
Jeff Sharkey513a0742014-07-08 17:10:32 -070045import android.content.pm.ManifestDigest;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080046import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070047import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010048import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080049import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070050import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080051import android.content.pm.PermissionGroupInfo;
52import android.content.pm.PermissionInfo;
53import android.content.pm.ProviderInfo;
54import android.content.pm.ResolveInfo;
55import android.content.pm.ServiceInfo;
Svetoslavc7d62f02014-09-04 15:39:54 -070056import android.content.pm.UserInfo;
rich cannings706e8ba2012-08-20 13:20:14 -070057import android.content.pm.VerificationParams;
Kenny Root0aaa0d92011-09-12 16:42:55 -070058import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080059import android.content.res.Resources;
60import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070061import android.graphics.Bitmap;
62import android.graphics.Canvas;
63import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010064import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080065import android.graphics.drawable.Drawable;
66import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070067import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070068import android.os.Handler;
69import android.os.Looper;
70import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080071import android.os.Process;
72import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070073import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080074import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010075import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070076import android.os.storage.StorageManager;
77import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070078import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070079import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080080import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070081import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070082
83import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070084
85import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070086import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070087import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010088import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080089import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070090
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080091import java.lang.ref.WeakReference;
92import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080093import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070094import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080095import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070096import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070097import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080098
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070099/** @hide */
100public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800101 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800102 private final static boolean DEBUG_ICONS = false;
103
Svet Ganov2acf0632015-11-24 19:10:59 -0800104 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
105
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700106 // Default flags to use with PackageManager when no flags are given.
107 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
108
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700109 private final Object mLock = new Object();
110
111 @GuardedBy("mLock")
112 private UserManager mUserManager;
113 @GuardedBy("mLock")
114 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100115
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700116 @GuardedBy("mDelegates")
117 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
118
Svet Ganovf1b7f202015-07-29 08:33:42 -0700119 @GuardedBy("mLock")
120 private String mPermissionsControllerPackageName;
121
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100122 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700123 synchronized (mLock) {
124 if (mUserManager == null) {
125 mUserManager = UserManager.get(mContext);
126 }
127 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100128 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100129 }
130
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800131 @Override
132 public PackageInfo getPackageInfo(String packageName, int flags)
133 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100134 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
135 }
136
137 @Override
138 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
139 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800140 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100141 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800142 if (pi != null) {
143 return pi;
144 }
145 } catch (RemoteException e) {
146 throw new RuntimeException("Package manager has died", e);
147 }
148
149 throw new NameNotFoundException(packageName);
150 }
151
152 @Override
153 public String[] currentToCanonicalPackageNames(String[] names) {
154 try {
155 return mPM.currentToCanonicalPackageNames(names);
156 } catch (RemoteException e) {
157 throw new RuntimeException("Package manager has died", e);
158 }
159 }
160
161 @Override
162 public String[] canonicalToCurrentPackageNames(String[] names) {
163 try {
164 return mPM.canonicalToCurrentPackageNames(names);
165 } catch (RemoteException e) {
166 throw new RuntimeException("Package manager has died", e);
167 }
168 }
169
170 @Override
171 public Intent getLaunchIntentForPackage(String packageName) {
172 // First see if the package has an INFO activity; the existence of
173 // such an activity is implied to be the desired front-door for the
174 // overall package (such as if it has multiple launcher entries).
175 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
176 intentToResolve.addCategory(Intent.CATEGORY_INFO);
177 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800178 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800179
180 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800181 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800182 // reuse the intent instance
183 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
184 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
185 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800186 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800187 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800188 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800189 return null;
190 }
191 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800192 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800193 intent.setClassName(ris.get(0).activityInfo.packageName,
194 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800195 return intent;
196 }
197
198 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700199 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
200 // Try to find a main leanback_launcher activity.
201 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
202 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
203 intentToResolve.setPackage(packageName);
204 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
205
206 if (ris == null || ris.size() <= 0) {
207 return null;
208 }
209 Intent intent = new Intent(intentToResolve);
210 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
211 intent.setClassName(ris.get(0).activityInfo.packageName,
212 ris.get(0).activityInfo.name);
213 return intent;
214 }
215
216 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800217 public int[] getPackageGids(String packageName)
218 throws NameNotFoundException {
219 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800220 int[] gids = mPM.getPackageGids(packageName, mContext.getUserId());
221 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800222 return gids;
223 }
224 } catch (RemoteException e) {
225 throw new RuntimeException("Package manager has died", e);
226 }
227
228 throw new NameNotFoundException(packageName);
229 }
230
231 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800232 public int getPackageUid(String packageName, int userHandle)
233 throws NameNotFoundException {
234 try {
235 int uid = mPM.getPackageUid(packageName, userHandle);
236 if (uid >= 0) {
237 return uid;
238 }
239 } catch (RemoteException e) {
240 throw new RuntimeException("Package manager has died", e);
241 }
242
243 throw new NameNotFoundException(packageName);
244 }
245
246 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800247 public PermissionInfo getPermissionInfo(String name, int flags)
248 throws NameNotFoundException {
249 try {
250 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
251 if (pi != null) {
252 return pi;
253 }
254 } catch (RemoteException e) {
255 throw new RuntimeException("Package manager has died", e);
256 }
257
258 throw new NameNotFoundException(name);
259 }
260
261 @Override
262 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
263 throws NameNotFoundException {
264 try {
265 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags);
266 if (pi != null) {
267 return pi;
268 }
269 } catch (RemoteException e) {
270 throw new RuntimeException("Package manager has died", e);
271 }
272
273 throw new NameNotFoundException(group);
274 }
275
276 @Override
277 public PermissionGroupInfo getPermissionGroupInfo(String name,
278 int flags) throws NameNotFoundException {
279 try {
280 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
281 if (pgi != null) {
282 return pgi;
283 }
284 } catch (RemoteException e) {
285 throw new RuntimeException("Package manager has died", e);
286 }
287
288 throw new NameNotFoundException(name);
289 }
290
291 @Override
292 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
293 try {
294 return mPM.getAllPermissionGroups(flags);
295 } catch (RemoteException e) {
296 throw new RuntimeException("Package manager has died", e);
297 }
298 }
299
300 @Override
301 public ApplicationInfo getApplicationInfo(String packageName, int flags)
302 throws NameNotFoundException {
303 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700304 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800305 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100306 // This is a temporary hack. Callers must use
307 // createPackageContext(packageName).getApplicationInfo() to
308 // get the right paths.
309 maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800310 return ai;
311 }
312 } catch (RemoteException e) {
313 throw new RuntimeException("Package manager has died", e);
314 }
315
316 throw new NameNotFoundException(packageName);
317 }
318
Narayan Kamathcaa71192014-07-16 11:06:43 +0100319 private static void maybeAdjustApplicationInfo(ApplicationInfo info) {
320 // If we're dealing with a multi-arch application that has both
321 // 32 and 64 bit shared libraries, we might need to choose the secondary
322 // depending on what the current runtime's instruction set is.
323 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
324 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500325
326 // Get the instruction set that the libraries of secondary Abi is supported.
327 // In presence of a native bridge this might be different than the one secondary Abi used.
328 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
329 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
330 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100331
332 // If the runtimeIsa is the same as the primary isa, then we do nothing.
333 // Everything will be set up correctly because info.nativeLibraryDir will
334 // correspond to the right ISA.
335 if (runtimeIsa.equals(secondaryIsa)) {
336 info.nativeLibraryDir = info.secondaryNativeLibraryDir;
337 }
338 }
339 }
340
341
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800342 @Override
343 public ActivityInfo getActivityInfo(ComponentName className, int flags)
344 throws NameNotFoundException {
345 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700346 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800347 if (ai != null) {
348 return ai;
349 }
350 } catch (RemoteException e) {
351 throw new RuntimeException("Package manager has died", e);
352 }
353
354 throw new NameNotFoundException(className.toString());
355 }
356
357 @Override
358 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
359 throws NameNotFoundException {
360 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700361 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800362 if (ai != null) {
363 return ai;
364 }
365 } catch (RemoteException e) {
366 throw new RuntimeException("Package manager has died", e);
367 }
368
369 throw new NameNotFoundException(className.toString());
370 }
371
372 @Override
373 public ServiceInfo getServiceInfo(ComponentName className, int flags)
374 throws NameNotFoundException {
375 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700376 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800377 if (si != null) {
378 return si;
379 }
380 } catch (RemoteException e) {
381 throw new RuntimeException("Package manager has died", e);
382 }
383
384 throw new NameNotFoundException(className.toString());
385 }
386
387 @Override
388 public ProviderInfo getProviderInfo(ComponentName className, int flags)
389 throws NameNotFoundException {
390 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700391 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800392 if (pi != null) {
393 return pi;
394 }
395 } catch (RemoteException e) {
396 throw new RuntimeException("Package manager has died", e);
397 }
398
399 throw new NameNotFoundException(className.toString());
400 }
401
402 @Override
403 public String[] getSystemSharedLibraryNames() {
404 try {
405 return mPM.getSystemSharedLibraryNames();
406 } catch (RemoteException e) {
407 throw new RuntimeException("Package manager has died", e);
408 }
409 }
410
411 @Override
412 public FeatureInfo[] getSystemAvailableFeatures() {
413 try {
414 return mPM.getSystemAvailableFeatures();
415 } catch (RemoteException e) {
416 throw new RuntimeException("Package manager has died", e);
417 }
418 }
419
420 @Override
421 public boolean hasSystemFeature(String name) {
422 try {
423 return mPM.hasSystemFeature(name);
424 } catch (RemoteException e) {
425 throw new RuntimeException("Package manager has died", e);
426 }
427 }
428
429 @Override
430 public int checkPermission(String permName, String pkgName) {
431 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800432 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800433 } catch (RemoteException e) {
434 throw new RuntimeException("Package manager has died", e);
435 }
436 }
437
438 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700439 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
440 try {
441 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
442 } catch (RemoteException e) {
443 throw new RuntimeException("Package manager has died", e);
444 }
445 }
446
Svet Ganovf1b7f202015-07-29 08:33:42 -0700447 /**
448 * @hide
449 */
450 @Override
451 public String getPermissionControllerPackageName() {
452 synchronized (mLock) {
453 if (mPermissionsControllerPackageName == null) {
454 try {
455 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
456 } catch (RemoteException e) {
457 throw new RuntimeException("Package manager has died", e);
458 }
459 }
460 return mPermissionsControllerPackageName;
461 }
462 }
463
Svet Ganovad3b2972015-07-07 22:49:17 -0700464 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800465 public boolean addPermission(PermissionInfo info) {
466 try {
467 return mPM.addPermission(info);
468 } catch (RemoteException e) {
469 throw new RuntimeException("Package manager has died", e);
470 }
471 }
472
473 @Override
474 public boolean addPermissionAsync(PermissionInfo info) {
475 try {
476 return mPM.addPermissionAsync(info);
477 } catch (RemoteException e) {
478 throw new RuntimeException("Package manager has died", e);
479 }
480 }
481
482 @Override
483 public void removePermission(String name) {
484 try {
485 mPM.removePermission(name);
486 } catch (RemoteException e) {
487 throw new RuntimeException("Package manager has died", e);
488 }
489 }
490
491 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700492 public void grantRuntimePermission(String packageName, String permissionName,
493 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800494 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700495 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800496 } catch (RemoteException e) {
497 throw new RuntimeException("Package manager has died", e);
498 }
499 }
500
501 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700502 public void revokeRuntimePermission(String packageName, String permissionName,
503 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800504 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700505 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
506 } catch (RemoteException e) {
507 throw new RuntimeException("Package manager has died", e);
508 }
509 }
510
511 @Override
512 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
513 try {
514 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
515 } catch (RemoteException e) {
516 throw new RuntimeException("Package manager has died", e);
517 }
518 }
519
520 @Override
521 public void updatePermissionFlags(String permissionName, String packageName,
522 int flagMask, int flagValues, UserHandle user) {
523 try {
524 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
525 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800526 } catch (RemoteException e) {
527 throw new RuntimeException("Package manager has died", e);
528 }
529 }
530
531 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700532 public boolean shouldShowRequestPermissionRationale(String permission) {
533 try {
534 return mPM.shouldShowRequestPermissionRationale(permission,
535 mContext.getPackageName(), mContext.getUserId());
536 } catch (RemoteException e) {
537 throw new RuntimeException("Package manager has died", e);
538 }
539 }
540
541 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800542 public int checkSignatures(String pkg1, String pkg2) {
543 try {
544 return mPM.checkSignatures(pkg1, pkg2);
545 } catch (RemoteException e) {
546 throw new RuntimeException("Package manager has died", e);
547 }
548 }
549
550 @Override
551 public int checkSignatures(int uid1, int uid2) {
552 try {
553 return mPM.checkUidSignatures(uid1, uid2);
554 } catch (RemoteException e) {
555 throw new RuntimeException("Package manager has died", e);
556 }
557 }
558
559 @Override
560 public String[] getPackagesForUid(int uid) {
561 try {
562 return mPM.getPackagesForUid(uid);
563 } catch (RemoteException e) {
564 throw new RuntimeException("Package manager has died", e);
565 }
566 }
567
568 @Override
569 public String getNameForUid(int uid) {
570 try {
571 return mPM.getNameForUid(uid);
572 } catch (RemoteException e) {
573 throw new RuntimeException("Package manager has died", e);
574 }
575 }
576
577 @Override
578 public int getUidForSharedUser(String sharedUserName)
579 throws NameNotFoundException {
580 try {
581 int uid = mPM.getUidForSharedUser(sharedUserName);
582 if(uid != -1) {
583 return uid;
584 }
585 } catch (RemoteException e) {
586 throw new RuntimeException("Package manager has died", e);
587 }
588 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
589 }
590
Kenny Roote6cd0c72011-05-19 12:48:14 -0700591 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800592 @Override
593 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700594 return getInstalledPackages(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700595 }
596
597 /** @hide */
598 @Override
599 public List<PackageInfo> getInstalledPackages(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800600 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800601 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
602 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800603 } catch (RemoteException e) {
604 throw new RuntimeException("Package manager has died", e);
605 }
606 }
607
Kenny Roote6cd0c72011-05-19 12:48:14 -0700608 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800609 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800610 public List<PackageInfo> getPackagesHoldingPermissions(
611 String[] permissions, int flags) {
612 final int userId = mContext.getUserId();
613 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800614 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
615 permissions, flags, userId);
616 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800617 } catch (RemoteException e) {
618 throw new RuntimeException("Package manager has died", e);
619 }
620 }
621
622 @SuppressWarnings("unchecked")
623 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800624 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700625 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800626 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800627 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
628 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800629 } catch (RemoteException e) {
630 throw new RuntimeException("Package manager has died", e);
631 }
632 }
633
Svet Ganov2acf0632015-11-24 19:10:59 -0800634 /** @hide */
635 @SuppressWarnings("unchecked")
636 @Override
637 public List<EphemeralApplicationInfo> getEphemeralApplications() {
638 try {
639 ParceledListSlice<EphemeralApplicationInfo> slice =
640 mPM.getEphemeralApplications(mContext.getUserId());
641 if (slice != null) {
642 return slice.getList();
643 }
644 return Collections.emptyList();
645 } catch (RemoteException e) {
646 throw new RuntimeException("Package manager has died", e);
647 }
648 }
649
650 /** @hide */
651 @Override
652 public Drawable getEphemeralApplicationIcon(String packageName) {
653 try {
654 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
655 packageName, mContext.getUserId());
656 if (bitmap != null) {
657 return new BitmapDrawable(null, bitmap);
658 }
659 return null;
660 } catch (RemoteException e) {
661 throw new RuntimeException("Package manager has died", e);
662 }
663 }
664
665 @Override
666 public boolean isEphemeralApplication() {
667 try {
668 return mPM.isEphemeralApplication(
669 mContext.getPackageName(), mContext.getUserId());
670 } catch (RemoteException e) {
671 Log.e(TAG, "System server is dead", e);
672 }
673 return false;
674 }
675
676 @Override
677 public int getEphemeralCookieMaxSizeBytes() {
678 return Settings.Global.getInt(mContext.getContentResolver(),
679 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
680 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
681 }
682
683 @Override
684 public @NonNull byte[] getEphemeralCookie() {
685 try {
686 final byte[] cookie = mPM.getEphemeralApplicationCookie(
687 mContext.getPackageName(), mContext.getUserId());
688 if (cookie != null) {
689 return cookie;
690 }
691 } catch (RemoteException e) {
692 Log.e(TAG, "System server is dead", e);
693 }
694 return EmptyArray.BYTE;
695 }
696
697 @Override
698 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
699 try {
700 return mPM.setEphemeralApplicationCookie(
701 mContext.getPackageName(), cookie, mContext.getUserId());
702 } catch (RemoteException e) {
703 Log.e(TAG, "System server is dead", e);
704 }
705 return false;
706 }
707
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800708 @Override
709 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700710 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700711 }
712
713 @Override
714 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800715 try {
716 return mPM.resolveIntent(
717 intent,
718 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700719 flags,
720 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800721 } catch (RemoteException e) {
722 throw new RuntimeException("Package manager has died", e);
723 }
724 }
725
726 @Override
727 public List<ResolveInfo> queryIntentActivities(Intent intent,
728 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700729 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700730 }
731
732 /** @hide Same as above but for a specific user */
733 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700734 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700735 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800736 try {
737 return mPM.queryIntentActivities(
738 intent,
739 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700740 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700741 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800742 } catch (RemoteException e) {
743 throw new RuntimeException("Package manager has died", e);
744 }
745 }
746
747 @Override
748 public List<ResolveInfo> queryIntentActivityOptions(
749 ComponentName caller, Intent[] specifics, Intent intent,
750 int flags) {
751 final ContentResolver resolver = mContext.getContentResolver();
752
753 String[] specificTypes = null;
754 if (specifics != null) {
755 final int N = specifics.length;
756 for (int i=0; i<N; i++) {
757 Intent sp = specifics[i];
758 if (sp != null) {
759 String t = sp.resolveTypeIfNeeded(resolver);
760 if (t != null) {
761 if (specificTypes == null) {
762 specificTypes = new String[N];
763 }
764 specificTypes[i] = t;
765 }
766 }
767 }
768 }
769
770 try {
771 return mPM.queryIntentActivityOptions(caller, specifics,
772 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700773 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800774 } catch (RemoteException e) {
775 throw new RuntimeException("Package manager has died", e);
776 }
777 }
778
Amith Yamasanif203aee2012-08-29 18:41:53 -0700779 /**
780 * @hide
781 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800782 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700783 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800784 try {
785 return mPM.queryIntentReceivers(
786 intent,
787 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700788 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -0700789 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800790 } catch (RemoteException e) {
791 throw new RuntimeException("Package manager has died", e);
792 }
793 }
794
795 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700796 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700797 return queryBroadcastReceivers(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700798 }
799
800 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800801 public ResolveInfo resolveService(Intent intent, int flags) {
802 try {
803 return mPM.resolveService(
804 intent,
805 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700806 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700807 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800808 } catch (RemoteException e) {
809 throw new RuntimeException("Package manager has died", e);
810 }
811 }
812
813 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700814 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800815 try {
816 return mPM.queryIntentServices(
817 intent,
818 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700819 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700820 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800821 } catch (RemoteException e) {
822 throw new RuntimeException("Package manager has died", e);
823 }
824 }
825
826 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700827 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700828 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700829 }
830
831 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700832 public List<ResolveInfo> queryIntentContentProvidersAsUser(
833 Intent intent, int flags, int userId) {
834 try {
835 return mPM.queryIntentContentProviders(intent,
836 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
837 } catch (RemoteException e) {
838 throw new RuntimeException("Package manager has died", e);
839 }
840 }
841
842 @Override
843 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
844 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
845 }
846
847 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100848 public ProviderInfo resolveContentProvider(String name, int flags) {
849 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
850 }
851
852 /** @hide **/
853 @Override
854 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800855 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100856 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800857 } catch (RemoteException e) {
858 throw new RuntimeException("Package manager has died", e);
859 }
860 }
861
862 @Override
863 public List<ProviderInfo> queryContentProviders(String processName,
864 int uid, int flags) {
865 try {
Dianne Hackborn28ec27c2015-08-03 15:28:28 -0700866 ParceledListSlice<ProviderInfo> slice
867 = mPM.queryContentProviders(processName, uid, flags);
868 return slice != null ? slice.getList() : null;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800869 } catch (RemoteException e) {
870 throw new RuntimeException("Package manager has died", e);
871 }
872 }
873
874 @Override
875 public InstrumentationInfo getInstrumentationInfo(
876 ComponentName className, int flags)
877 throws NameNotFoundException {
878 try {
879 InstrumentationInfo ii = mPM.getInstrumentationInfo(
880 className, flags);
881 if (ii != null) {
882 return ii;
883 }
884 } catch (RemoteException e) {
885 throw new RuntimeException("Package manager has died", e);
886 }
887
888 throw new NameNotFoundException(className.toString());
889 }
890
891 @Override
892 public List<InstrumentationInfo> queryInstrumentation(
893 String targetPackage, int flags) {
894 try {
895 return mPM.queryInstrumentation(targetPackage, flags);
896 } catch (RemoteException e) {
897 throw new RuntimeException("Package manager has died", e);
898 }
899 }
900
Alan Viveretteecd585a2015-04-13 10:32:51 -0700901 @Nullable
902 @Override
903 public Drawable getDrawable(String packageName, @DrawableRes int resId,
904 @Nullable ApplicationInfo appInfo) {
905 final ResourceName name = new ResourceName(packageName, resId);
906 final Drawable cachedIcon = getCachedIcon(name);
907 if (cachedIcon != null) {
908 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800909 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700910
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800911 if (appInfo == null) {
912 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700913 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800914 } catch (NameNotFoundException e) {
915 return null;
916 }
917 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700918
919 if (resId != 0) {
920 try {
921 final Resources r = getResourcesForApplication(appInfo);
922 final Drawable dr = r.getDrawable(resId, null);
923 if (dr != null) {
924 putCachedIcon(name, dr);
925 }
926
927 if (false) {
928 RuntimeException e = new RuntimeException("here");
929 e.fillInStackTrace();
930 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
931 + " from package " + packageName
932 + ": app scale=" + r.getCompatibilityInfo().applicationScale
933 + ", caller scale=" + mContext.getResources()
934 .getCompatibilityInfo().applicationScale,
935 e);
936 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100937 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700938 Log.v(TAG, "Getting drawable 0x"
939 + Integer.toHexString(resId) + " from " + r
940 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100941 }
942 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700943 } catch (NameNotFoundException e) {
944 Log.w("PackageManager", "Failure retrieving resources for "
945 + appInfo.packageName);
946 } catch (Resources.NotFoundException e) {
947 Log.w("PackageManager", "Failure retrieving resources for "
948 + appInfo.packageName + ": " + e.getMessage());
949 } catch (Exception e) {
950 // If an exception was thrown, fall through to return
951 // default icon.
952 Log.w("PackageManager", "Failure retrieving icon 0x"
953 + Integer.toHexString(resId) + " in package "
954 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800955 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800956 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700957
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800958 return null;
959 }
960
961 @Override public Drawable getActivityIcon(ComponentName activityName)
962 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700963 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800964 }
965
966 @Override public Drawable getActivityIcon(Intent intent)
967 throws NameNotFoundException {
968 if (intent.getComponent() != null) {
969 return getActivityIcon(intent.getComponent());
970 }
971
972 ResolveInfo info = resolveActivity(
973 intent, PackageManager.MATCH_DEFAULT_ONLY);
974 if (info != null) {
975 return info.activityInfo.loadIcon(this);
976 }
977
Romain Guy39fe17c2011-11-30 10:34:07 -0800978 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800979 }
980
981 @Override public Drawable getDefaultActivityIcon() {
982 return Resources.getSystem().getDrawable(
983 com.android.internal.R.drawable.sym_def_app_icon);
984 }
985
986 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
987 return info.loadIcon(this);
988 }
989
990 @Override public Drawable getApplicationIcon(String packageName)
991 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700992 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800993 }
994
995 @Override
Jose Limaf78e3122014-03-06 12:13:15 -0800996 public Drawable getActivityBanner(ComponentName activityName)
997 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700998 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -0800999 }
1000
1001 @Override
1002 public Drawable getActivityBanner(Intent intent)
1003 throws NameNotFoundException {
1004 if (intent.getComponent() != null) {
1005 return getActivityBanner(intent.getComponent());
1006 }
1007
1008 ResolveInfo info = resolveActivity(
1009 intent, PackageManager.MATCH_DEFAULT_ONLY);
1010 if (info != null) {
1011 return info.activityInfo.loadBanner(this);
1012 }
1013
1014 throw new NameNotFoundException(intent.toUri(0));
1015 }
1016
1017 @Override
1018 public Drawable getApplicationBanner(ApplicationInfo info) {
1019 return info.loadBanner(this);
1020 }
1021
1022 @Override
1023 public Drawable getApplicationBanner(String packageName)
1024 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001025 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001026 }
1027
1028 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001029 public Drawable getActivityLogo(ComponentName activityName)
1030 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001031 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001032 }
1033
1034 @Override
1035 public Drawable getActivityLogo(Intent intent)
1036 throws NameNotFoundException {
1037 if (intent.getComponent() != null) {
1038 return getActivityLogo(intent.getComponent());
1039 }
1040
1041 ResolveInfo info = resolveActivity(
1042 intent, PackageManager.MATCH_DEFAULT_ONLY);
1043 if (info != null) {
1044 return info.activityInfo.loadLogo(this);
1045 }
1046
1047 throw new NameNotFoundException(intent.toUri(0));
1048 }
1049
1050 @Override
1051 public Drawable getApplicationLogo(ApplicationInfo info) {
1052 return info.loadLogo(this);
1053 }
1054
1055 @Override
1056 public Drawable getApplicationLogo(String packageName)
1057 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001058 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001059 }
1060
Svetoslavc7d62f02014-09-04 15:39:54 -07001061 @Override
1062 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1063 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1064 if (badgeResId == 0) {
1065 return icon;
1066 }
1067 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1068 return getBadgedDrawable(icon, badgeIcon, null, true);
1069 }
1070
1071 @Override
1072 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1073 Rect badgeLocation, int badgeDensity) {
1074 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1075 if (badgeDrawable == null) {
1076 return drawable;
1077 }
1078 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1079 }
1080
1081 @Override
1082 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
1083 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1084 if (userInfo != null && userInfo.isManagedProfile()) {
1085 if (density <= 0) {
1086 density = mContext.getResources().getDisplayMetrics().densityDpi;
1087 }
1088 return Resources.getSystem().getDrawableForDensity(
1089 com.android.internal.R.drawable.ic_corp_badge, density);
1090 }
1091 return null;
1092 }
1093
1094 @Override
1095 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
1096 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1097 if (userInfo != null && userInfo.isManagedProfile()) {
1098 return Resources.getSystem().getString(
1099 com.android.internal.R.string.managed_profile_label_badge, label);
1100 }
1101 return label;
1102 }
1103
Alan Viveretteecd585a2015-04-13 10:32:51 -07001104 @Override
1105 public Resources getResourcesForActivity(ComponentName activityName)
1106 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001107 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001108 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001109 }
1110
Alan Viveretteecd585a2015-04-13 10:32:51 -07001111 @Override
1112 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1113 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001114 if (app.packageName.equals("system")) {
1115 return mContext.mMainThread.getSystemContext().getResources();
1116 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001117 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001118 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001119 sameUid ? app.sourceDir : app.publicSourceDir,
1120 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001121 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1122 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001123 if (r != null) {
1124 return r;
1125 }
1126 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1127 }
1128
Alan Viveretteecd585a2015-04-13 10:32:51 -07001129 @Override
1130 public Resources getResourcesForApplication(String appPackageName)
1131 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001132 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001133 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001134 }
1135
Amith Yamasani98edc952012-09-25 14:09:27 -07001136 /** @hide */
1137 @Override
1138 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1139 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001140 if (userId < 0) {
1141 throw new IllegalArgumentException(
1142 "Call does not support special user #" + userId);
1143 }
1144 if ("system".equals(appPackageName)) {
1145 return mContext.mMainThread.getSystemContext().getResources();
1146 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001147 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001148 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001149 if (ai != null) {
1150 return getResourcesForApplication(ai);
1151 }
1152 } catch (RemoteException e) {
1153 throw new RuntimeException("Package manager has died", e);
1154 }
1155 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1156 }
1157
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001158 int mCachedSafeMode = -1;
1159 @Override public boolean isSafeMode() {
1160 try {
1161 if (mCachedSafeMode < 0) {
1162 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1163 }
1164 return mCachedSafeMode != 0;
1165 } catch (RemoteException e) {
1166 throw new RuntimeException("Package manager has died", e);
1167 }
1168 }
1169
Svetoslavf7c06eb2015-06-10 18:43:22 -07001170 @Override
1171 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1172 synchronized (mPermissionListeners) {
1173 if (mPermissionListeners.get(listener) != null) {
1174 return;
1175 }
1176 OnPermissionsChangeListenerDelegate delegate =
1177 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1178 try {
1179 mPM.addOnPermissionsChangeListener(delegate);
1180 mPermissionListeners.put(listener, delegate);
1181 } catch (RemoteException e) {
1182 throw new RuntimeException("Package manager has died", e);
1183 }
1184 }
1185 }
1186
1187 @Override
1188 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1189 synchronized (mPermissionListeners) {
1190 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1191 if (delegate != null) {
1192 try {
1193 mPM.removeOnPermissionsChangeListener(delegate);
1194 mPermissionListeners.remove(listener);
1195 } catch (RemoteException e) {
1196 throw new RuntimeException("Package manager has died", e);
1197 }
1198 }
1199 }
1200 }
1201
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001202 static void configurationChanged() {
1203 synchronized (sSync) {
1204 sIconCache.clear();
1205 sStringCache.clear();
1206 }
1207 }
1208
1209 ApplicationPackageManager(ContextImpl context,
1210 IPackageManager pm) {
1211 mContext = context;
1212 mPM = pm;
1213 }
1214
Alan Viveretteecd585a2015-04-13 10:32:51 -07001215 @Nullable
1216 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001217 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001218 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001219 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1220 + name + ": " + wr);
1221 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001222 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001223 if (state != null) {
1224 if (DEBUG_ICONS) {
1225 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1226 }
1227 // Note: It's okay here to not use the newDrawable(Resources) variant
1228 // of the API. The ConstantState comes from a drawable that was
1229 // originally created by passing the proper app Resources instance
1230 // which means the state should already contain the proper
1231 // resources specific information (like density.) See
1232 // BitmapDrawable.BitmapState for instance.
1233 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001234 }
1235 // our entry has been purged
1236 sIconCache.remove(name);
1237 }
1238 }
1239 return null;
1240 }
1241
Alan Viveretteecd585a2015-04-13 10:32:51 -07001242 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001243 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001244 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001245 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001246 }
1247 }
1248
Romain Guy39fe17c2011-11-30 10:34:07 -08001249 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001250 boolean immediateGc = false;
1251 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1252 immediateGc = true;
1253 }
1254 if (pkgList != null && (pkgList.length > 0)) {
1255 boolean needCleanup = false;
1256 for (String ssp : pkgList) {
1257 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001258 for (int i=sIconCache.size()-1; i>=0; i--) {
1259 ResourceName nm = sIconCache.keyAt(i);
1260 if (nm.packageName.equals(ssp)) {
1261 //Log.i(TAG, "Removing cached drawable for " + nm);
1262 sIconCache.removeAt(i);
1263 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001264 }
1265 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001266 for (int i=sStringCache.size()-1; i>=0; i--) {
1267 ResourceName nm = sStringCache.keyAt(i);
1268 if (nm.packageName.equals(ssp)) {
1269 //Log.i(TAG, "Removing cached string for " + nm);
1270 sStringCache.removeAt(i);
1271 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001272 }
1273 }
1274 }
1275 }
1276 if (needCleanup || hasPkgInfo) {
1277 if (immediateGc) {
1278 // Schedule an immediate gc.
1279 Runtime.getRuntime().gc();
1280 } else {
1281 ActivityThread.currentActivityThread().scheduleGcIdler();
1282 }
1283 }
1284 }
1285 }
1286
1287 private static final class ResourceName {
1288 final String packageName;
1289 final int iconId;
1290
1291 ResourceName(String _packageName, int _iconId) {
1292 packageName = _packageName;
1293 iconId = _iconId;
1294 }
1295
1296 ResourceName(ApplicationInfo aInfo, int _iconId) {
1297 this(aInfo.packageName, _iconId);
1298 }
1299
1300 ResourceName(ComponentInfo cInfo, int _iconId) {
1301 this(cInfo.applicationInfo.packageName, _iconId);
1302 }
1303
1304 ResourceName(ResolveInfo rInfo, int _iconId) {
1305 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1306 }
1307
1308 @Override
1309 public boolean equals(Object o) {
1310 if (this == o) return true;
1311 if (o == null || getClass() != o.getClass()) return false;
1312
1313 ResourceName that = (ResourceName) o;
1314
1315 if (iconId != that.iconId) return false;
1316 return !(packageName != null ?
1317 !packageName.equals(that.packageName) : that.packageName != null);
1318
1319 }
1320
1321 @Override
1322 public int hashCode() {
1323 int result;
1324 result = packageName.hashCode();
1325 result = 31 * result + iconId;
1326 return result;
1327 }
1328
1329 @Override
1330 public String toString() {
1331 return "{ResourceName " + packageName + " / " + iconId + "}";
1332 }
1333 }
1334
1335 private CharSequence getCachedString(ResourceName name) {
1336 synchronized (sSync) {
1337 WeakReference<CharSequence> wr = sStringCache.get(name);
1338 if (wr != null) { // we have the activity
1339 CharSequence cs = wr.get();
1340 if (cs != null) {
1341 return cs;
1342 }
1343 // our entry has been purged
1344 sStringCache.remove(name);
1345 }
1346 }
1347 return null;
1348 }
1349
1350 private void putCachedString(ResourceName name, CharSequence cs) {
1351 synchronized (sSync) {
1352 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1353 }
1354 }
1355
1356 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001357 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001358 ApplicationInfo appInfo) {
1359 ResourceName name = new ResourceName(packageName, resid);
1360 CharSequence text = getCachedString(name);
1361 if (text != null) {
1362 return text;
1363 }
1364 if (appInfo == null) {
1365 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001366 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001367 } catch (NameNotFoundException e) {
1368 return null;
1369 }
1370 }
1371 try {
1372 Resources r = getResourcesForApplication(appInfo);
1373 text = r.getText(resid);
1374 putCachedString(name, text);
1375 return text;
1376 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001377 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001378 + appInfo.packageName);
1379 } catch (RuntimeException e) {
1380 // If an exception was thrown, fall through to return
1381 // default icon.
1382 Log.w("PackageManager", "Failure retrieving text 0x"
1383 + Integer.toHexString(resid) + " in package "
1384 + packageName, e);
1385 }
1386 return null;
1387 }
1388
1389 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001390 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001391 ApplicationInfo appInfo) {
1392 if (appInfo == null) {
1393 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001394 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001395 } catch (NameNotFoundException e) {
1396 return null;
1397 }
1398 }
1399 try {
1400 Resources r = getResourcesForApplication(appInfo);
1401 return r.getXml(resid);
1402 } catch (RuntimeException e) {
1403 // If an exception was thrown, fall through to return
1404 // default icon.
1405 Log.w("PackageManager", "Failure retrieving xml 0x"
1406 + Integer.toHexString(resid) + " in package "
1407 + packageName, e);
1408 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001409 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001410 + appInfo.packageName);
1411 }
1412 return null;
1413 }
1414
1415 @Override
1416 public CharSequence getApplicationLabel(ApplicationInfo info) {
1417 return info.loadLabel(this);
1418 }
1419
1420 @Override
1421 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1422 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001423 final VerificationParams verificationParams = new VerificationParams(null, null,
1424 null, VerificationParams.NO_UID, null);
1425 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001426 installerPackageName, verificationParams, null, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001427 }
1428
1429 @Override
Kenny Root5ab21572011-07-27 11:11:19 -07001430 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1431 int flags, String installerPackageName, Uri verificationURI,
Rich Canningse1d7c712012-08-08 12:46:06 -07001432 ManifestDigest manifestDigest, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001433 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1434 null, VerificationParams.NO_UID, manifestDigest);
1435 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001436 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Kenny Root5ab21572011-07-27 11:11:19 -07001437 }
1438
1439 @Override
John Spurlock8a985d22014-02-25 09:40:05 -05001440 public void installPackageWithVerificationAndEncryption(Uri packageURI,
rich cannings706e8ba2012-08-20 13:20:14 -07001441 IPackageInstallObserver observer, int flags, String installerPackageName,
1442 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001443 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001444 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001445 }
1446
Christopher Tatef1977b42014-03-24 16:25:51 -07001447 @Override
1448 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1449 int flags, String installerPackageName) {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001450 installPackageAsUser(packageURI, observer, flags, installerPackageName,
Robin Lee0e27c872015-09-28 14:37:40 +01001451 mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001452 }
1453
1454 @Override
1455 public void installPackageAsUser(Uri packageURI, PackageInstallObserver observer, int flags,
1456 String installerPackageName, int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001457 final VerificationParams verificationParams = new VerificationParams(null, null,
1458 null, VerificationParams.NO_UID, null);
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001459 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null,
1460 userId);
Christopher Tatef1977b42014-03-24 16:25:51 -07001461 }
1462
1463 @Override
1464 public void installPackageWithVerification(Uri packageURI,
1465 PackageInstallObserver observer, int flags, String installerPackageName,
1466 Uri verificationURI, ManifestDigest manifestDigest,
1467 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001468 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1469 null, VerificationParams.NO_UID, manifestDigest);
1470 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001471 encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001472 }
1473
1474 @Override
1475 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1476 PackageInstallObserver observer, int flags, String installerPackageName,
1477 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001478 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001479 encryptionParams, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001480 }
1481
1482 private void installCommon(Uri packageURI,
1483 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001484 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams,
1485 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001486 if (!"file".equals(packageURI.getScheme())) {
1487 throw new UnsupportedOperationException("Only file:// URIs are supported");
1488 }
1489 if (encryptionParams != null) {
1490 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1491 }
1492
1493 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001494 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001495 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
1496 verificationParams, null, userId);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001497 } catch (RemoteException ignored) {
rich cannings706e8ba2012-08-20 13:20:14 -07001498 }
1499 }
1500
1501 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001502 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001503 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001504 }
1505
1506 @Override
1507 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001508 throws NameNotFoundException {
1509 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001510 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001511 if (res == INSTALL_FAILED_INVALID_URI) {
1512 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1513 }
1514 return res;
1515 } catch (RemoteException e) {
1516 // Should never happen!
1517 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1518 }
1519 }
1520
1521 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001522 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001523 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001524 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001525 } catch (RemoteException e) {
1526 // Should never happen!
1527 }
1528 }
1529
1530 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001531 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1532 long millisecondsToDelay) {
1533 try {
1534 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1535 } catch (RemoteException e) {
1536 // Should never happen!
1537 }
1538 }
1539
1540 @Override
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001541 public void verifyIntentFilter(int id, int verificationCode, List<String> outFailedDomains) {
1542 try {
1543 mPM.verifyIntentFilter(id, verificationCode, outFailedDomains);
1544 } catch (RemoteException e) {
1545 // Should never happen!
1546 }
1547 }
1548
1549 @Override
1550 public int getIntentVerificationStatus(String packageName, int userId) {
1551 try {
1552 return mPM.getIntentVerificationStatus(packageName, userId);
1553 } catch (RemoteException e) {
1554 // Should never happen!
1555 return PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1556 }
1557 }
1558
1559 @Override
1560 public boolean updateIntentVerificationStatus(String packageName, int status, int userId) {
1561 try {
1562 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1563 } catch (RemoteException e) {
1564 // Should never happen!
1565 return false;
1566 }
1567 }
1568
1569 @Override
1570 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1571 try {
1572 return mPM.getIntentFilterVerifications(packageName);
1573 } catch (RemoteException e) {
1574 // Should never happen!
1575 return null;
1576 }
1577 }
1578
1579 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001580 public List<IntentFilter> getAllIntentFilters(String packageName) {
1581 try {
1582 return mPM.getAllIntentFilters(packageName);
1583 } catch (RemoteException e) {
1584 // Should never happen!
1585 return null;
1586 }
1587 }
1588
1589 @Override
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001590 public String getDefaultBrowserPackageName(int userId) {
1591 try {
1592 return mPM.getDefaultBrowserPackageName(userId);
1593 } catch (RemoteException e) {
1594 // Should never happen!
1595 return null;
1596 }
1597 }
1598
1599 @Override
1600 public boolean setDefaultBrowserPackageName(String packageName, int userId) {
1601 try {
1602 return mPM.setDefaultBrowserPackageName(packageName, userId);
1603 } catch (RemoteException e) {
1604 // Should never happen!
1605 return false;
1606 }
1607 }
1608
1609 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001610 public void setInstallerPackageName(String targetPackage,
1611 String installerPackageName) {
1612 try {
1613 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1614 } catch (RemoteException e) {
1615 // Should never happen!
1616 }
1617 }
1618
1619 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001620 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001621 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001622 return mPM.getInstallerPackageName(packageName);
1623 } catch (RemoteException e) {
1624 // Should never happen!
1625 }
1626 return null;
1627 }
1628
1629 @Override
1630 public int getMoveStatus(int moveId) {
1631 try {
1632 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001633 } catch (RemoteException e) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001634 throw e.rethrowAsRuntimeException();
1635 }
1636 }
1637
1638 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001639 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1640 synchronized (mDelegates) {
1641 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1642 handler.getLooper());
1643 try {
1644 mPM.registerMoveCallback(delegate);
1645 } catch (RemoteException e) {
1646 throw e.rethrowAsRuntimeException();
1647 }
1648 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001649 }
1650 }
1651
1652 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001653 public void unregisterMoveCallback(MoveCallback callback) {
1654 synchronized (mDelegates) {
1655 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1656 final MoveCallbackDelegate delegate = i.next();
1657 if (delegate.mCallback == callback) {
1658 try {
1659 mPM.unregisterMoveCallback(delegate);
1660 } catch (RemoteException e) {
1661 throw e.rethrowAsRuntimeException();
1662 }
1663 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001664 }
1665 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001666 }
1667 }
1668
1669 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001670 public int movePackage(String packageName, VolumeInfo vol) {
1671 try {
1672 final String volumeUuid;
1673 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1674 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1675 } else if (vol.isPrimaryPhysical()) {
1676 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1677 } else {
1678 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1679 }
1680
1681 return mPM.movePackage(packageName, volumeUuid);
1682 } catch (RemoteException e) {
1683 throw e.rethrowAsRuntimeException();
1684 }
1685 }
1686
1687 @Override
1688 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001689 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001690 if (app.isInternal()) {
1691 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1692 } else if (app.isExternalAsec()) {
1693 return storage.getPrimaryPhysicalVolume();
1694 } else {
1695 return storage.findVolumeByUuid(app.volumeUuid);
1696 }
1697 }
1698
1699 @Override
1700 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1701 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1702 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001703 final List<VolumeInfo> vols = storage.getVolumes();
1704 final List<VolumeInfo> candidates = new ArrayList<>();
1705 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001706 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001707 candidates.add(vol);
1708 }
1709 }
1710 return candidates;
1711 }
1712
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001713 private static boolean isPackageCandidateVolume(
1714 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1715 final boolean forceAllowOnExternal = Settings.Global.getInt(
1716 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001717 // Private internal is always an option
1718 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1719 return true;
1720 }
1721
1722 // System apps and apps demanding internal storage can't be moved
1723 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001724 if (app.isSystemApp()) {
1725 return false;
1726 }
1727 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001728 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1729 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001730 return false;
1731 }
1732
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001733 // Gotta be able to write there
1734 if (!vol.isMountedWritable()) {
1735 return false;
1736 }
1737
1738 // Moving into an ASEC on public primary is only option internal
1739 if (vol.isPrimaryPhysical()) {
1740 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001741 }
1742
1743 // Otherwise we can move to any private volume
1744 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1745 }
1746
1747 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001748 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001749 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001750 final String volumeUuid;
1751 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1752 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1753 } else if (vol.isPrimaryPhysical()) {
1754 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1755 } else {
1756 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1757 }
1758
1759 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001760 } catch (RemoteException e) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001761 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001762 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001763 }
1764
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001765 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001766 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1767 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1768 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001769 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001770 }
1771
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001772 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001773 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1774 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1775 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1776 final List<VolumeInfo> vols = storage.getVolumes();
1777 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001778 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1779 storage.getPrimaryStorageUuid()) && currentVol != null) {
1780 // TODO: support moving primary physical to emulated volume
1781 candidates.add(currentVol);
1782 } else {
1783 for (VolumeInfo vol : vols) {
1784 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1785 candidates.add(vol);
1786 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001787 }
1788 }
1789 return candidates;
1790 }
1791
1792 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1793 // Private internal is always an option
1794 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1795 return true;
1796 }
1797
1798 // Gotta be able to write there
1799 if (!vol.isMountedWritable()) {
1800 return false;
1801 }
1802
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001803 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001804 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001805 }
1806
1807 @Override
1808 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001809 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001810 }
1811
1812 @Override
1813 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1814 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001815 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001816 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001817 } catch (RemoteException e) {
1818 // Should never happen!
1819 }
1820 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001821
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001822 @Override
1823 public void clearApplicationUserData(String packageName,
1824 IPackageDataObserver observer) {
1825 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001826 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001827 } catch (RemoteException e) {
1828 // Should never happen!
1829 }
1830 }
1831 @Override
1832 public void deleteApplicationCacheFiles(String packageName,
1833 IPackageDataObserver observer) {
1834 try {
1835 mPM.deleteApplicationCacheFiles(packageName, observer);
1836 } catch (RemoteException e) {
1837 // Should never happen!
1838 }
1839 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001840
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001841 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001842 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1843 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001844 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001845 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001846 } catch (RemoteException e) {
1847 // Should never happen!
1848 }
1849 }
1850
1851 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001852 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001853 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001854 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001855 } catch (RemoteException e) {
1856 // Should never happen!
1857 }
1858 }
1859
1860 @Override
Dianne Hackborn0c380492012-08-20 17:23:30 -07001861 public void getPackageSizeInfo(String packageName, int userHandle,
1862 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001863 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001864 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001865 } catch (RemoteException e) {
1866 // Should never happen!
1867 }
1868 }
1869 @Override
1870 public void addPackageToPreferred(String packageName) {
1871 try {
1872 mPM.addPackageToPreferred(packageName);
1873 } catch (RemoteException e) {
1874 // Should never happen!
1875 }
1876 }
1877
1878 @Override
1879 public void removePackageFromPreferred(String packageName) {
1880 try {
1881 mPM.removePackageFromPreferred(packageName);
1882 } catch (RemoteException e) {
1883 // Should never happen!
1884 }
1885 }
1886
1887 @Override
1888 public List<PackageInfo> getPreferredPackages(int flags) {
1889 try {
1890 return mPM.getPreferredPackages(flags);
1891 } catch (RemoteException e) {
1892 // Should never happen!
1893 }
1894 return new ArrayList<PackageInfo>();
1895 }
1896
1897 @Override
1898 public void addPreferredActivity(IntentFilter filter,
1899 int match, ComponentName[] set, ComponentName activity) {
1900 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001901 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001902 } catch (RemoteException e) {
1903 // Should never happen!
1904 }
1905 }
1906
1907 @Override
1908 public void addPreferredActivity(IntentFilter filter, int match,
1909 ComponentName[] set, ComponentName activity, int userId) {
1910 try {
1911 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001912 } catch (RemoteException e) {
1913 // Should never happen!
1914 }
1915 }
1916
1917 @Override
1918 public void replacePreferredActivity(IntentFilter filter,
1919 int match, ComponentName[] set, ComponentName activity) {
1920 try {
Robin Lee0e27c872015-09-28 14:37:40 +01001921 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001922 } catch (RemoteException e) {
1923 // Should never happen!
1924 }
1925 }
1926
1927 @Override
1928 public void replacePreferredActivityAsUser(IntentFilter filter,
1929 int match, ComponentName[] set, ComponentName activity,
1930 int userId) {
1931 try {
1932 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001933 } catch (RemoteException e) {
1934 // Should never happen!
1935 }
1936 }
1937
1938 @Override
1939 public void clearPackagePreferredActivities(String packageName) {
1940 try {
1941 mPM.clearPackagePreferredActivities(packageName);
1942 } catch (RemoteException e) {
1943 // Should never happen!
1944 }
1945 }
1946
1947 @Override
1948 public int getPreferredActivities(List<IntentFilter> outFilters,
1949 List<ComponentName> outActivities, String packageName) {
1950 try {
1951 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
1952 } catch (RemoteException e) {
1953 // Should never happen!
1954 }
1955 return 0;
1956 }
1957
1958 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07001959 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
1960 try {
1961 return mPM.getHomeActivities(outActivities);
1962 } catch (RemoteException e) {
1963 // Should never happen!
1964 }
1965 return null;
1966 }
1967
1968 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001969 public void setComponentEnabledSetting(ComponentName componentName,
1970 int newState, int flags) {
1971 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001972 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001973 } catch (RemoteException e) {
1974 // Should never happen!
1975 }
1976 }
1977
1978 @Override
1979 public int getComponentEnabledSetting(ComponentName componentName) {
1980 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001981 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001982 } catch (RemoteException e) {
1983 // Should never happen!
1984 }
1985 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1986 }
1987
1988 @Override
1989 public void setApplicationEnabledSetting(String packageName,
1990 int newState, int flags) {
1991 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001992 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07001993 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001994 } catch (RemoteException e) {
1995 // Should never happen!
1996 }
1997 }
1998
1999 @Override
2000 public int getApplicationEnabledSetting(String packageName) {
2001 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002002 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002003 } catch (RemoteException e) {
2004 // Should never happen!
2005 }
2006 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2007 }
2008
Amith Yamasani655d0e22013-06-12 14:19:10 -07002009 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002010 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002011 UserHandle user) {
2012 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002013 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002014 user.getIdentifier());
2015 } catch (RemoteException re) {
2016 // Should never happen!
2017 }
2018 return false;
2019 }
2020
2021 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002022 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002023 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002024 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Amith Yamasani655d0e22013-06-12 14:19:10 -07002025 } catch (RemoteException re) {
2026 // Should never happen!
2027 }
2028 return false;
2029 }
2030
dcashmanc6f22492014-08-14 09:54:51 -07002031 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002032 @Override
2033 public KeySet getKeySetByAlias(String packageName, String alias) {
2034 Preconditions.checkNotNull(packageName);
2035 Preconditions.checkNotNull(alias);
dcashmanc6f22492014-08-14 09:54:51 -07002036 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002037 try {
dcashmanc6f22492014-08-14 09:54:51 -07002038 ks = mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002039 } catch (RemoteException e) {
2040 return null;
2041 }
dcashmanc6f22492014-08-14 09:54:51 -07002042 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002043 }
2044
dcashmanc6f22492014-08-14 09:54:51 -07002045 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002046 @Override
2047 public KeySet getSigningKeySet(String packageName) {
2048 Preconditions.checkNotNull(packageName);
dcashmanc6f22492014-08-14 09:54:51 -07002049 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002050 try {
dcashmanc6f22492014-08-14 09:54:51 -07002051 ks = mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002052 } catch (RemoteException e) {
2053 return null;
2054 }
dcashmanc6f22492014-08-14 09:54:51 -07002055 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002056 }
2057
dcashmanc6f22492014-08-14 09:54:51 -07002058 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002059 @Override
2060 public boolean isSignedBy(String packageName, KeySet ks) {
2061 Preconditions.checkNotNull(packageName);
2062 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002063 try {
dcashmanc6f22492014-08-14 09:54:51 -07002064 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002065 } catch (RemoteException e) {
2066 return false;
2067 }
2068 }
2069
dcashmanc6f22492014-08-14 09:54:51 -07002070 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002071 @Override
2072 public boolean isSignedByExactly(String packageName, KeySet ks) {
2073 Preconditions.checkNotNull(packageName);
2074 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002075 try {
dcashmanc6f22492014-08-14 09:54:51 -07002076 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002077 } catch (RemoteException e) {
2078 return false;
2079 }
2080 }
2081
Kenny Root0aaa0d92011-09-12 16:42:55 -07002082 /**
2083 * @hide
2084 */
2085 @Override
2086 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2087 try {
2088 return mPM.getVerifierDeviceIdentity();
2089 } catch (RemoteException e) {
2090 // Should never happen!
2091 }
2092 return null;
2093 }
2094
Jeff Hao9f60c082014-10-28 18:51:07 -07002095 /**
2096 * @hide
2097 */
2098 @Override
2099 public boolean isUpgrade() {
2100 try {
2101 return mPM.isUpgrade();
2102 } catch (RemoteException e) {
2103 return false;
2104 }
2105 }
2106
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002107 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002108 public PackageInstaller getPackageInstaller() {
2109 synchronized (mLock) {
2110 if (mInstaller == null) {
2111 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002112 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002113 mContext.getPackageName(), mContext.getUserId());
2114 } catch (RemoteException e) {
2115 throw e.rethrowAsRuntimeException();
2116 }
2117 }
2118 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002119 }
2120 }
2121
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002122 @Override
2123 public boolean isPackageAvailable(String packageName) {
2124 try {
2125 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2126 } catch (RemoteException e) {
2127 throw e.rethrowAsRuntimeException();
2128 }
2129 }
2130
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002131 /**
2132 * @hide
2133 */
2134 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002135 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2136 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002137 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002138 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002139 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002140 } catch (RemoteException e) {
2141 // Should never happen!
2142 }
2143 }
2144
2145 /**
2146 * @hide
2147 */
2148 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002149 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002150 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002151 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002152 } catch (RemoteException e) {
2153 // Should never happen!
2154 }
2155 }
2156
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002157 /**
2158 * @hide
2159 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002160 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002161 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2162 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2163 return dr;
2164 }
2165 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2166 }
2167
2168 /**
2169 * @hide
2170 */
2171 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002172 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002173 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2174 if (bitmap == null) {
2175 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2176 }
2177 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002178 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002179 Drawable dr = null;
2180 if (itemInfo.packageName != null) {
2181 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2182 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002183 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002184 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002185 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002186 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002187 }
2188
2189 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2190 Rect badgeLocation, boolean tryBadgeInPlace) {
2191 final int badgedWidth = drawable.getIntrinsicWidth();
2192 final int badgedHeight = drawable.getIntrinsicHeight();
2193 final boolean canBadgeInPlace = tryBadgeInPlace
2194 && (drawable instanceof BitmapDrawable)
2195 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2196
2197 final Bitmap bitmap;
2198 if (canBadgeInPlace) {
2199 bitmap = ((BitmapDrawable) drawable).getBitmap();
2200 } else {
2201 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2202 }
2203 Canvas canvas = new Canvas(bitmap);
2204
2205 if (!canBadgeInPlace) {
2206 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2207 drawable.draw(canvas);
2208 }
2209
2210 if (badgeLocation != null) {
2211 if (badgeLocation.left < 0 || badgeLocation.top < 0
2212 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2213 throw new IllegalArgumentException("Badge location " + badgeLocation
2214 + " not in badged drawable bounds "
2215 + new Rect(0, 0, badgedWidth, badgedHeight));
2216 }
2217 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2218
2219 canvas.save();
2220 canvas.translate(badgeLocation.left, badgeLocation.top);
2221 badgeDrawable.draw(canvas);
2222 canvas.restore();
2223 } else {
2224 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2225 badgeDrawable.draw(canvas);
2226 }
2227
2228 if (!canBadgeInPlace) {
2229 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2230
2231 if (drawable instanceof BitmapDrawable) {
2232 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2233 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2234 }
2235
2236 return mergedDrawable;
2237 }
2238
2239 return drawable;
2240 }
2241
2242 private int getBadgeResIdForUser(int userHandle) {
2243 // Return the framework-provided badge.
2244 UserInfo userInfo = getUserIfProfile(userHandle);
2245 if (userInfo != null && userInfo.isManagedProfile()) {
2246 return com.android.internal.R.drawable.ic_corp_icon_badge;
2247 }
2248 return 0;
2249 }
2250
2251 private UserInfo getUserIfProfile(int userHandle) {
Robin Lee0e27c872015-09-28 14:37:40 +01002252 List<UserInfo> userProfiles = getUserManager().getProfiles(mContext.getUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07002253 for (UserInfo user : userProfiles) {
2254 if (user.id == userHandle) {
2255 return user;
2256 }
2257 }
2258 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002259 }
2260
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002261 /** {@hide} */
2262 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2263 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002264 private static final int MSG_CREATED = 1;
2265 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002266
2267 final MoveCallback mCallback;
2268 final Handler mHandler;
2269
2270 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2271 mCallback = callback;
2272 mHandler = new Handler(looper, this);
2273 }
2274
2275 @Override
2276 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002277 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002278 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002279 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002280 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002281 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002282 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002283 }
2284 case MSG_STATUS_CHANGED: {
2285 final SomeArgs args = (SomeArgs) msg.obj;
2286 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2287 args.recycle();
2288 return true;
2289 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002290 }
2291 return false;
2292 }
2293
2294 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002295 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002296 final SomeArgs args = SomeArgs.obtain();
2297 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002298 args.arg2 = extras;
2299 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2300 }
2301
2302 @Override
2303 public void onStatusChanged(int moveId, int status, long estMillis) {
2304 final SomeArgs args = SomeArgs.obtain();
2305 args.argi1 = moveId;
2306 args.argi2 = status;
2307 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002308 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002309 }
2310 }
2311
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002312 private final ContextImpl mContext;
2313 private final IPackageManager mPM;
2314
2315 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002316 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2317 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2318 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2319 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002320
2321 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2322 mPermissionListeners = new ArrayMap<>();
2323
2324 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2325 implements Handler.Callback{
2326 private static final int MSG_PERMISSIONS_CHANGED = 1;
2327
2328 private final OnPermissionsChangedListener mListener;
2329 private final Handler mHandler;
2330
2331
2332 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2333 Looper looper) {
2334 mListener = listener;
2335 mHandler = new Handler(looper, this);
2336 }
2337
2338 @Override
2339 public void onPermissionsChanged(int uid) {
2340 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2341 }
2342
2343 @Override
2344 public boolean handleMessage(Message msg) {
2345 switch (msg.what) {
2346 case MSG_PERMISSIONS_CHANGED: {
2347 final int uid = msg.arg1;
2348 mListener.onPermissionsChanged(uid);
2349 return true;
2350 }
2351 }
2352 return false;
2353 }
2354 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002355}