blob: 93cb64e1b070d992d44a16522c19a966b04cb30d [file] [log] [blame]
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.DrawableRes;
Alan Viveretteecd585a2015-04-13 10:32:51 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.StringRes;
23import android.annotation.XmlRes;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080024import android.content.ComponentName;
25import android.content.ContentResolver;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.IntentSender;
29import android.content.pm.ActivityInfo;
30import android.content.pm.ApplicationInfo;
31import android.content.pm.ComponentInfo;
Anonymous Cowardceb1b0b2012-04-24 10:35:16 -070032import android.content.pm.ContainerEncryptionParams;
Svet Ganov2acf0632015-11-24 19:10:59 -080033import android.content.pm.EphemeralApplicationInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080034import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070035import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080036import android.content.pm.IPackageDataObserver;
37import android.content.pm.IPackageDeleteObserver;
38import android.content.pm.IPackageInstallObserver;
39import android.content.pm.IPackageManager;
40import android.content.pm.IPackageMoveObserver;
41import android.content.pm.IPackageStatsObserver;
42import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080043import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070044import android.content.pm.KeySet;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080045import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070046import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010047import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080048import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070049import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080050import android.content.pm.PermissionGroupInfo;
51import android.content.pm.PermissionInfo;
52import android.content.pm.ProviderInfo;
53import android.content.pm.ResolveInfo;
54import android.content.pm.ServiceInfo;
Svetoslavc7d62f02014-09-04 15:39:54 -070055import android.content.pm.UserInfo;
rich cannings706e8ba2012-08-20 13:20:14 -070056import android.content.pm.VerificationParams;
Kenny Root0aaa0d92011-09-12 16:42:55 -070057import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080058import android.content.res.Resources;
59import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070060import android.graphics.Bitmap;
61import android.graphics.Canvas;
62import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010063import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080064import android.graphics.drawable.Drawable;
65import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070066import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070067import android.os.Handler;
68import android.os.Looper;
69import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080070import android.os.Process;
71import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070072import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080073import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010074import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070075import android.os.storage.StorageManager;
76import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070077import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070078import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080079import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070080import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070081
82import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070083
84import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070085import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070086import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010087import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080088import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070089
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080090import java.lang.ref.WeakReference;
91import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080092import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070093import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080094import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070095import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070096import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080097
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070098/** @hide */
99public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800100 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800101 private final static boolean DEBUG_ICONS = false;
102
Svet Ganov2acf0632015-11-24 19:10:59 -0800103 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
104
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700105 // Default flags to use with PackageManager when no flags are given.
106 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
107
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700108 private final Object mLock = new Object();
109
110 @GuardedBy("mLock")
111 private UserManager mUserManager;
112 @GuardedBy("mLock")
113 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100114
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700115 @GuardedBy("mDelegates")
116 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
117
Svet Ganovf1b7f202015-07-29 08:33:42 -0700118 @GuardedBy("mLock")
119 private String mPermissionsControllerPackageName;
120
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100121 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700122 synchronized (mLock) {
123 if (mUserManager == null) {
124 mUserManager = UserManager.get(mContext);
125 }
126 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100127 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100128 }
129
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800130 @Override
131 public PackageInfo getPackageInfo(String packageName, int flags)
132 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100133 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
134 }
135
136 @Override
137 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
138 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800139 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100140 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800141 if (pi != null) {
142 return pi;
143 }
144 } catch (RemoteException e) {
145 throw new RuntimeException("Package manager has died", e);
146 }
147
148 throw new NameNotFoundException(packageName);
149 }
150
151 @Override
152 public String[] currentToCanonicalPackageNames(String[] names) {
153 try {
154 return mPM.currentToCanonicalPackageNames(names);
155 } catch (RemoteException e) {
156 throw new RuntimeException("Package manager has died", e);
157 }
158 }
159
160 @Override
161 public String[] canonicalToCurrentPackageNames(String[] names) {
162 try {
163 return mPM.canonicalToCurrentPackageNames(names);
164 } catch (RemoteException e) {
165 throw new RuntimeException("Package manager has died", e);
166 }
167 }
168
169 @Override
170 public Intent getLaunchIntentForPackage(String packageName) {
171 // First see if the package has an INFO activity; the existence of
172 // such an activity is implied to be the desired front-door for the
173 // overall package (such as if it has multiple launcher entries).
174 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
175 intentToResolve.addCategory(Intent.CATEGORY_INFO);
176 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800177 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800178
179 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800180 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800181 // reuse the intent instance
182 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
183 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
184 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800185 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800186 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800187 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800188 return null;
189 }
190 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800191 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800192 intent.setClassName(ris.get(0).activityInfo.packageName,
193 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800194 return intent;
195 }
196
197 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700198 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
199 // Try to find a main leanback_launcher activity.
200 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
201 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
202 intentToResolve.setPackage(packageName);
203 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
204
205 if (ris == null || ris.size() <= 0) {
206 return null;
207 }
208 Intent intent = new Intent(intentToResolve);
209 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
210 intent.setClassName(ris.get(0).activityInfo.packageName,
211 ris.get(0).activityInfo.name);
212 return intent;
213 }
214
215 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700216 public int[] getPackageGids(String packageName) throws NameNotFoundException {
217 return getPackageGids(packageName, 0);
218 }
219
220 @Override
221 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800222 throws NameNotFoundException {
223 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700224 int[] gids = mPM.getPackageGids(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800225 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800226 return gids;
227 }
228 } catch (RemoteException e) {
229 throw new RuntimeException("Package manager has died", e);
230 }
231
232 throw new NameNotFoundException(packageName);
233 }
234
235 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700236 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
237 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
238 }
239
240 @Override
241 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
242 return getPackageUidAsUser(packageName, 0, userId);
243 }
244
245 @Override
246 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800247 throws NameNotFoundException {
248 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700249 int uid = mPM.getPackageUid(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800250 if (uid >= 0) {
251 return uid;
252 }
253 } catch (RemoteException e) {
254 throw new RuntimeException("Package manager has died", e);
255 }
256
257 throw new NameNotFoundException(packageName);
258 }
259
260 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800261 public PermissionInfo getPermissionInfo(String name, int flags)
262 throws NameNotFoundException {
263 try {
264 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
265 if (pi != null) {
266 return pi;
267 }
268 } catch (RemoteException e) {
269 throw new RuntimeException("Package manager has died", e);
270 }
271
272 throw new NameNotFoundException(name);
273 }
274
275 @Override
276 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
277 throws NameNotFoundException {
278 try {
279 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags);
280 if (pi != null) {
281 return pi;
282 }
283 } catch (RemoteException e) {
284 throw new RuntimeException("Package manager has died", e);
285 }
286
287 throw new NameNotFoundException(group);
288 }
289
290 @Override
291 public PermissionGroupInfo getPermissionGroupInfo(String name,
292 int flags) throws NameNotFoundException {
293 try {
294 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
295 if (pgi != null) {
296 return pgi;
297 }
298 } catch (RemoteException e) {
299 throw new RuntimeException("Package manager has died", e);
300 }
301
302 throw new NameNotFoundException(name);
303 }
304
305 @Override
306 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
307 try {
308 return mPM.getAllPermissionGroups(flags);
309 } catch (RemoteException e) {
310 throw new RuntimeException("Package manager has died", e);
311 }
312 }
313
314 @Override
315 public ApplicationInfo getApplicationInfo(String packageName, int flags)
316 throws NameNotFoundException {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700317 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
318 }
319
320 @Override
321 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
322 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800323 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700324 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800325 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100326 // This is a temporary hack. Callers must use
327 // createPackageContext(packageName).getApplicationInfo() to
328 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800329 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800330 }
331 } catch (RemoteException e) {
332 throw new RuntimeException("Package manager has died", e);
333 }
334
335 throw new NameNotFoundException(packageName);
336 }
337
Tao Baic9a02372016-01-12 15:02:24 -0800338 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100339 // If we're dealing with a multi-arch application that has both
340 // 32 and 64 bit shared libraries, we might need to choose the secondary
341 // depending on what the current runtime's instruction set is.
342 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
343 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500344
345 // Get the instruction set that the libraries of secondary Abi is supported.
346 // In presence of a native bridge this might be different than the one secondary Abi used.
347 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
348 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
349 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100350
351 // If the runtimeIsa is the same as the primary isa, then we do nothing.
352 // Everything will be set up correctly because info.nativeLibraryDir will
353 // correspond to the right ISA.
354 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800355 ApplicationInfo modified = new ApplicationInfo(info);
356 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
357 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100358 }
359 }
Tao Baic9a02372016-01-12 15:02:24 -0800360 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100361 }
362
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800363 @Override
364 public ActivityInfo getActivityInfo(ComponentName className, int flags)
365 throws NameNotFoundException {
366 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700367 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800368 if (ai != null) {
369 return ai;
370 }
371 } catch (RemoteException e) {
372 throw new RuntimeException("Package manager has died", e);
373 }
374
375 throw new NameNotFoundException(className.toString());
376 }
377
378 @Override
379 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
380 throws NameNotFoundException {
381 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700382 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800383 if (ai != null) {
384 return ai;
385 }
386 } catch (RemoteException e) {
387 throw new RuntimeException("Package manager has died", e);
388 }
389
390 throw new NameNotFoundException(className.toString());
391 }
392
393 @Override
394 public ServiceInfo getServiceInfo(ComponentName className, int flags)
395 throws NameNotFoundException {
396 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700397 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800398 if (si != null) {
399 return si;
400 }
401 } catch (RemoteException e) {
402 throw new RuntimeException("Package manager has died", e);
403 }
404
405 throw new NameNotFoundException(className.toString());
406 }
407
408 @Override
409 public ProviderInfo getProviderInfo(ComponentName className, int flags)
410 throws NameNotFoundException {
411 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700412 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800413 if (pi != null) {
414 return pi;
415 }
416 } catch (RemoteException e) {
417 throw new RuntimeException("Package manager has died", e);
418 }
419
420 throw new NameNotFoundException(className.toString());
421 }
422
423 @Override
424 public String[] getSystemSharedLibraryNames() {
425 try {
426 return mPM.getSystemSharedLibraryNames();
427 } catch (RemoteException e) {
428 throw new RuntimeException("Package manager has died", e);
429 }
430 }
431
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800432 /** @hide */
433 @Override
434 public @Nullable String getServicesSystemSharedLibraryPackageName() {
435 try {
436 return mPM.getServicesSystemSharedLibraryPackageName();
437 } catch (RemoteException e) {
438 throw new RuntimeException("Package manager has died", e);
439 }
440 }
441
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800442 @Override
443 public FeatureInfo[] getSystemAvailableFeatures() {
444 try {
445 return mPM.getSystemAvailableFeatures();
446 } catch (RemoteException e) {
447 throw new RuntimeException("Package manager has died", e);
448 }
449 }
450
451 @Override
452 public boolean hasSystemFeature(String name) {
453 try {
454 return mPM.hasSystemFeature(name);
455 } catch (RemoteException e) {
456 throw new RuntimeException("Package manager has died", e);
457 }
458 }
459
460 @Override
461 public int checkPermission(String permName, String pkgName) {
462 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800463 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800464 } catch (RemoteException e) {
465 throw new RuntimeException("Package manager has died", e);
466 }
467 }
468
469 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700470 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
471 try {
472 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
473 } catch (RemoteException e) {
474 throw new RuntimeException("Package manager has died", e);
475 }
476 }
477
Svet Ganovf1b7f202015-07-29 08:33:42 -0700478 /**
479 * @hide
480 */
481 @Override
482 public String getPermissionControllerPackageName() {
483 synchronized (mLock) {
484 if (mPermissionsControllerPackageName == null) {
485 try {
486 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
487 } catch (RemoteException e) {
488 throw new RuntimeException("Package manager has died", e);
489 }
490 }
491 return mPermissionsControllerPackageName;
492 }
493 }
494
Svet Ganovad3b2972015-07-07 22:49:17 -0700495 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800496 public boolean addPermission(PermissionInfo info) {
497 try {
498 return mPM.addPermission(info);
499 } catch (RemoteException e) {
500 throw new RuntimeException("Package manager has died", e);
501 }
502 }
503
504 @Override
505 public boolean addPermissionAsync(PermissionInfo info) {
506 try {
507 return mPM.addPermissionAsync(info);
508 } catch (RemoteException e) {
509 throw new RuntimeException("Package manager has died", e);
510 }
511 }
512
513 @Override
514 public void removePermission(String name) {
515 try {
516 mPM.removePermission(name);
517 } catch (RemoteException e) {
518 throw new RuntimeException("Package manager has died", e);
519 }
520 }
521
522 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700523 public void grantRuntimePermission(String packageName, String permissionName,
524 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800525 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700526 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800527 } catch (RemoteException e) {
528 throw new RuntimeException("Package manager has died", e);
529 }
530 }
531
532 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700533 public void revokeRuntimePermission(String packageName, String permissionName,
534 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800535 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700536 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
537 } catch (RemoteException e) {
538 throw new RuntimeException("Package manager has died", e);
539 }
540 }
541
542 @Override
543 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
544 try {
545 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
546 } catch (RemoteException e) {
547 throw new RuntimeException("Package manager has died", e);
548 }
549 }
550
551 @Override
552 public void updatePermissionFlags(String permissionName, String packageName,
553 int flagMask, int flagValues, UserHandle user) {
554 try {
555 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
556 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800557 } catch (RemoteException e) {
558 throw new RuntimeException("Package manager has died", e);
559 }
560 }
561
562 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700563 public boolean shouldShowRequestPermissionRationale(String permission) {
564 try {
565 return mPM.shouldShowRequestPermissionRationale(permission,
566 mContext.getPackageName(), mContext.getUserId());
567 } catch (RemoteException e) {
568 throw new RuntimeException("Package manager has died", e);
569 }
570 }
571
572 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800573 public int checkSignatures(String pkg1, String pkg2) {
574 try {
575 return mPM.checkSignatures(pkg1, pkg2);
576 } catch (RemoteException e) {
577 throw new RuntimeException("Package manager has died", e);
578 }
579 }
580
581 @Override
582 public int checkSignatures(int uid1, int uid2) {
583 try {
584 return mPM.checkUidSignatures(uid1, uid2);
585 } catch (RemoteException e) {
586 throw new RuntimeException("Package manager has died", e);
587 }
588 }
589
590 @Override
591 public String[] getPackagesForUid(int uid) {
592 try {
593 return mPM.getPackagesForUid(uid);
594 } catch (RemoteException e) {
595 throw new RuntimeException("Package manager has died", e);
596 }
597 }
598
599 @Override
600 public String getNameForUid(int uid) {
601 try {
602 return mPM.getNameForUid(uid);
603 } catch (RemoteException e) {
604 throw new RuntimeException("Package manager has died", e);
605 }
606 }
607
608 @Override
609 public int getUidForSharedUser(String sharedUserName)
610 throws NameNotFoundException {
611 try {
612 int uid = mPM.getUidForSharedUser(sharedUserName);
613 if(uid != -1) {
614 return uid;
615 }
616 } catch (RemoteException e) {
617 throw new RuntimeException("Package manager has died", e);
618 }
619 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
620 }
621
Kenny Roote6cd0c72011-05-19 12:48:14 -0700622 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800623 @Override
624 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700625 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700626 }
627
628 /** @hide */
629 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700630 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800631 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800632 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
633 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800634 } catch (RemoteException e) {
635 throw new RuntimeException("Package manager has died", e);
636 }
637 }
638
Kenny Roote6cd0c72011-05-19 12:48:14 -0700639 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800640 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800641 public List<PackageInfo> getPackagesHoldingPermissions(
642 String[] permissions, int flags) {
643 final int userId = mContext.getUserId();
644 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800645 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
646 permissions, flags, userId);
647 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800648 } catch (RemoteException e) {
649 throw new RuntimeException("Package manager has died", e);
650 }
651 }
652
653 @SuppressWarnings("unchecked")
654 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800655 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700656 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800657 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800658 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
659 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800660 } catch (RemoteException e) {
661 throw new RuntimeException("Package manager has died", e);
662 }
663 }
664
Svet Ganov2acf0632015-11-24 19:10:59 -0800665 /** @hide */
666 @SuppressWarnings("unchecked")
667 @Override
668 public List<EphemeralApplicationInfo> getEphemeralApplications() {
669 try {
670 ParceledListSlice<EphemeralApplicationInfo> slice =
671 mPM.getEphemeralApplications(mContext.getUserId());
672 if (slice != null) {
673 return slice.getList();
674 }
675 return Collections.emptyList();
676 } catch (RemoteException e) {
677 throw new RuntimeException("Package manager has died", e);
678 }
679 }
680
681 /** @hide */
682 @Override
683 public Drawable getEphemeralApplicationIcon(String packageName) {
684 try {
685 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
686 packageName, mContext.getUserId());
687 if (bitmap != null) {
688 return new BitmapDrawable(null, bitmap);
689 }
690 return null;
691 } catch (RemoteException e) {
692 throw new RuntimeException("Package manager has died", e);
693 }
694 }
695
696 @Override
697 public boolean isEphemeralApplication() {
698 try {
699 return mPM.isEphemeralApplication(
700 mContext.getPackageName(), mContext.getUserId());
701 } catch (RemoteException e) {
702 Log.e(TAG, "System server is dead", e);
703 }
704 return false;
705 }
706
707 @Override
708 public int getEphemeralCookieMaxSizeBytes() {
709 return Settings.Global.getInt(mContext.getContentResolver(),
710 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
711 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
712 }
713
714 @Override
715 public @NonNull byte[] getEphemeralCookie() {
716 try {
717 final byte[] cookie = mPM.getEphemeralApplicationCookie(
718 mContext.getPackageName(), mContext.getUserId());
719 if (cookie != null) {
720 return cookie;
721 }
722 } catch (RemoteException e) {
723 Log.e(TAG, "System server is dead", e);
724 }
725 return EmptyArray.BYTE;
726 }
727
728 @Override
729 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
730 try {
731 return mPM.setEphemeralApplicationCookie(
732 mContext.getPackageName(), cookie, mContext.getUserId());
733 } catch (RemoteException e) {
734 Log.e(TAG, "System server is dead", e);
735 }
736 return false;
737 }
738
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800739 @Override
740 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700741 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700742 }
743
744 @Override
745 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800746 try {
747 return mPM.resolveIntent(
748 intent,
749 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700750 flags,
751 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800752 } catch (RemoteException e) {
753 throw new RuntimeException("Package manager has died", e);
754 }
755 }
756
757 @Override
758 public List<ResolveInfo> queryIntentActivities(Intent intent,
759 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700760 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700761 }
762
763 /** @hide Same as above but for a specific user */
764 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700765 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700766 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800767 try {
768 return mPM.queryIntentActivities(
769 intent,
770 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700771 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700772 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800773 } catch (RemoteException e) {
774 throw new RuntimeException("Package manager has died", e);
775 }
776 }
777
778 @Override
779 public List<ResolveInfo> queryIntentActivityOptions(
780 ComponentName caller, Intent[] specifics, Intent intent,
781 int flags) {
782 final ContentResolver resolver = mContext.getContentResolver();
783
784 String[] specificTypes = null;
785 if (specifics != null) {
786 final int N = specifics.length;
787 for (int i=0; i<N; i++) {
788 Intent sp = specifics[i];
789 if (sp != null) {
790 String t = sp.resolveTypeIfNeeded(resolver);
791 if (t != null) {
792 if (specificTypes == null) {
793 specificTypes = new String[N];
794 }
795 specificTypes[i] = t;
796 }
797 }
798 }
799 }
800
801 try {
802 return mPM.queryIntentActivityOptions(caller, specifics,
803 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700804 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800805 } catch (RemoteException e) {
806 throw new RuntimeException("Package manager has died", e);
807 }
808 }
809
Amith Yamasanif203aee2012-08-29 18:41:53 -0700810 /**
811 * @hide
812 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800813 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700814 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800815 try {
816 return mPM.queryIntentReceivers(
817 intent,
818 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700819 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -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
Amith Yamasanif203aee2012-08-29 18:41:53 -0700827 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700828 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700829 }
830
831 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800832 public ResolveInfo resolveService(Intent intent, int flags) {
833 try {
834 return mPM.resolveService(
835 intent,
836 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700837 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700838 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800839 } catch (RemoteException e) {
840 throw new RuntimeException("Package manager has died", e);
841 }
842 }
843
844 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700845 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800846 try {
847 return mPM.queryIntentServices(
848 intent,
849 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700850 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700851 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800852 } catch (RemoteException e) {
853 throw new RuntimeException("Package manager has died", e);
854 }
855 }
856
857 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700858 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700859 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700860 }
861
862 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700863 public List<ResolveInfo> queryIntentContentProvidersAsUser(
864 Intent intent, int flags, int userId) {
865 try {
866 return mPM.queryIntentContentProviders(intent,
867 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
868 } catch (RemoteException e) {
869 throw new RuntimeException("Package manager has died", e);
870 }
871 }
872
873 @Override
874 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
875 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
876 }
877
878 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100879 public ProviderInfo resolveContentProvider(String name, int flags) {
880 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
881 }
882
883 /** @hide **/
884 @Override
885 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800886 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100887 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800888 } catch (RemoteException e) {
889 throw new RuntimeException("Package manager has died", e);
890 }
891 }
892
893 @Override
894 public List<ProviderInfo> queryContentProviders(String processName,
895 int uid, int flags) {
896 try {
Dianne Hackborn28ec27c2015-08-03 15:28:28 -0700897 ParceledListSlice<ProviderInfo> slice
898 = mPM.queryContentProviders(processName, uid, flags);
899 return slice != null ? slice.getList() : null;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800900 } catch (RemoteException e) {
901 throw new RuntimeException("Package manager has died", e);
902 }
903 }
904
905 @Override
906 public InstrumentationInfo getInstrumentationInfo(
907 ComponentName className, int flags)
908 throws NameNotFoundException {
909 try {
910 InstrumentationInfo ii = mPM.getInstrumentationInfo(
911 className, flags);
912 if (ii != null) {
913 return ii;
914 }
915 } catch (RemoteException e) {
916 throw new RuntimeException("Package manager has died", e);
917 }
918
919 throw new NameNotFoundException(className.toString());
920 }
921
922 @Override
923 public List<InstrumentationInfo> queryInstrumentation(
924 String targetPackage, int flags) {
925 try {
926 return mPM.queryInstrumentation(targetPackage, flags);
927 } catch (RemoteException e) {
928 throw new RuntimeException("Package manager has died", e);
929 }
930 }
931
Alan Viveretteecd585a2015-04-13 10:32:51 -0700932 @Nullable
933 @Override
934 public Drawable getDrawable(String packageName, @DrawableRes int resId,
935 @Nullable ApplicationInfo appInfo) {
936 final ResourceName name = new ResourceName(packageName, resId);
937 final Drawable cachedIcon = getCachedIcon(name);
938 if (cachedIcon != null) {
939 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800940 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700941
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800942 if (appInfo == null) {
943 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700944 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800945 } catch (NameNotFoundException e) {
946 return null;
947 }
948 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700949
950 if (resId != 0) {
951 try {
952 final Resources r = getResourcesForApplication(appInfo);
953 final Drawable dr = r.getDrawable(resId, null);
954 if (dr != null) {
955 putCachedIcon(name, dr);
956 }
957
958 if (false) {
959 RuntimeException e = new RuntimeException("here");
960 e.fillInStackTrace();
961 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
962 + " from package " + packageName
963 + ": app scale=" + r.getCompatibilityInfo().applicationScale
964 + ", caller scale=" + mContext.getResources()
965 .getCompatibilityInfo().applicationScale,
966 e);
967 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100968 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700969 Log.v(TAG, "Getting drawable 0x"
970 + Integer.toHexString(resId) + " from " + r
971 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100972 }
973 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700974 } catch (NameNotFoundException e) {
975 Log.w("PackageManager", "Failure retrieving resources for "
976 + appInfo.packageName);
977 } catch (Resources.NotFoundException e) {
978 Log.w("PackageManager", "Failure retrieving resources for "
979 + appInfo.packageName + ": " + e.getMessage());
980 } catch (Exception e) {
981 // If an exception was thrown, fall through to return
982 // default icon.
983 Log.w("PackageManager", "Failure retrieving icon 0x"
984 + Integer.toHexString(resId) + " in package "
985 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800986 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800987 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700988
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800989 return null;
990 }
991
992 @Override public Drawable getActivityIcon(ComponentName activityName)
993 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700994 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800995 }
996
997 @Override public Drawable getActivityIcon(Intent intent)
998 throws NameNotFoundException {
999 if (intent.getComponent() != null) {
1000 return getActivityIcon(intent.getComponent());
1001 }
1002
1003 ResolveInfo info = resolveActivity(
1004 intent, PackageManager.MATCH_DEFAULT_ONLY);
1005 if (info != null) {
1006 return info.activityInfo.loadIcon(this);
1007 }
1008
Romain Guy39fe17c2011-11-30 10:34:07 -08001009 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001010 }
1011
1012 @Override public Drawable getDefaultActivityIcon() {
1013 return Resources.getSystem().getDrawable(
1014 com.android.internal.R.drawable.sym_def_app_icon);
1015 }
1016
1017 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1018 return info.loadIcon(this);
1019 }
1020
1021 @Override public Drawable getApplicationIcon(String packageName)
1022 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001023 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001024 }
1025
1026 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001027 public Drawable getActivityBanner(ComponentName activityName)
1028 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001029 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001030 }
1031
1032 @Override
1033 public Drawable getActivityBanner(Intent intent)
1034 throws NameNotFoundException {
1035 if (intent.getComponent() != null) {
1036 return getActivityBanner(intent.getComponent());
1037 }
1038
1039 ResolveInfo info = resolveActivity(
1040 intent, PackageManager.MATCH_DEFAULT_ONLY);
1041 if (info != null) {
1042 return info.activityInfo.loadBanner(this);
1043 }
1044
1045 throw new NameNotFoundException(intent.toUri(0));
1046 }
1047
1048 @Override
1049 public Drawable getApplicationBanner(ApplicationInfo info) {
1050 return info.loadBanner(this);
1051 }
1052
1053 @Override
1054 public Drawable getApplicationBanner(String packageName)
1055 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001056 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001057 }
1058
1059 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001060 public Drawable getActivityLogo(ComponentName activityName)
1061 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001062 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001063 }
1064
1065 @Override
1066 public Drawable getActivityLogo(Intent intent)
1067 throws NameNotFoundException {
1068 if (intent.getComponent() != null) {
1069 return getActivityLogo(intent.getComponent());
1070 }
1071
1072 ResolveInfo info = resolveActivity(
1073 intent, PackageManager.MATCH_DEFAULT_ONLY);
1074 if (info != null) {
1075 return info.activityInfo.loadLogo(this);
1076 }
1077
1078 throw new NameNotFoundException(intent.toUri(0));
1079 }
1080
1081 @Override
1082 public Drawable getApplicationLogo(ApplicationInfo info) {
1083 return info.loadLogo(this);
1084 }
1085
1086 @Override
1087 public Drawable getApplicationLogo(String packageName)
1088 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001089 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001090 }
1091
Svetoslavc7d62f02014-09-04 15:39:54 -07001092 @Override
1093 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1094 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1095 if (badgeResId == 0) {
1096 return icon;
1097 }
1098 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1099 return getBadgedDrawable(icon, badgeIcon, null, true);
1100 }
1101
1102 @Override
1103 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1104 Rect badgeLocation, int badgeDensity) {
1105 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1106 if (badgeDrawable == null) {
1107 return drawable;
1108 }
1109 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1110 }
1111
1112 @Override
1113 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Selim Cineke6ff9462016-01-15 15:07:06 -08001114 return getManagedProfileIconForDensity(user, density,
1115 com.android.internal.R.drawable.ic_corp_badge);
1116 }
1117
1118 @Override
1119 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
1120 return getManagedProfileIconForDensity(user, density,
1121 com.android.internal.R.drawable.ic_corp_badge_no_background);
1122 }
1123
1124 private Drawable getManagedProfileIconForDensity(UserHandle user, int density,
1125 int drawableId) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001126 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1127 if (userInfo != null && userInfo.isManagedProfile()) {
1128 if (density <= 0) {
1129 density = mContext.getResources().getDisplayMetrics().densityDpi;
1130 }
Selim Cineke6ff9462016-01-15 15:07:06 -08001131 return Resources.getSystem().getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001132 }
1133 return null;
1134 }
1135
1136 @Override
1137 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
1138 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1139 if (userInfo != null && userInfo.isManagedProfile()) {
1140 return Resources.getSystem().getString(
1141 com.android.internal.R.string.managed_profile_label_badge, label);
1142 }
1143 return label;
1144 }
1145
Alan Viveretteecd585a2015-04-13 10:32:51 -07001146 @Override
1147 public Resources getResourcesForActivity(ComponentName activityName)
1148 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001149 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001150 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001151 }
1152
Alan Viveretteecd585a2015-04-13 10:32:51 -07001153 @Override
1154 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1155 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001156 if (app.packageName.equals("system")) {
1157 return mContext.mMainThread.getSystemContext().getResources();
1158 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001159 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001160 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001161 sameUid ? app.sourceDir : app.publicSourceDir,
1162 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001163 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1164 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001165 if (r != null) {
1166 return r;
1167 }
1168 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1169 }
1170
Alan Viveretteecd585a2015-04-13 10:32:51 -07001171 @Override
1172 public Resources getResourcesForApplication(String appPackageName)
1173 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001174 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001175 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001176 }
1177
Amith Yamasani98edc952012-09-25 14:09:27 -07001178 /** @hide */
1179 @Override
1180 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1181 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001182 if (userId < 0) {
1183 throw new IllegalArgumentException(
1184 "Call does not support special user #" + userId);
1185 }
1186 if ("system".equals(appPackageName)) {
1187 return mContext.mMainThread.getSystemContext().getResources();
1188 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001189 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001190 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001191 if (ai != null) {
1192 return getResourcesForApplication(ai);
1193 }
1194 } catch (RemoteException e) {
1195 throw new RuntimeException("Package manager has died", e);
1196 }
1197 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1198 }
1199
Jeff Sharkeycd654482016-01-08 17:42:11 -07001200 volatile int mCachedSafeMode = -1;
1201
1202 @Override
1203 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001204 try {
1205 if (mCachedSafeMode < 0) {
1206 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1207 }
1208 return mCachedSafeMode != 0;
1209 } catch (RemoteException e) {
1210 throw new RuntimeException("Package manager has died", e);
1211 }
1212 }
1213
Svetoslavf7c06eb2015-06-10 18:43:22 -07001214 @Override
1215 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1216 synchronized (mPermissionListeners) {
1217 if (mPermissionListeners.get(listener) != null) {
1218 return;
1219 }
1220 OnPermissionsChangeListenerDelegate delegate =
1221 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1222 try {
1223 mPM.addOnPermissionsChangeListener(delegate);
1224 mPermissionListeners.put(listener, delegate);
1225 } catch (RemoteException e) {
1226 throw new RuntimeException("Package manager has died", e);
1227 }
1228 }
1229 }
1230
1231 @Override
1232 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1233 synchronized (mPermissionListeners) {
1234 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1235 if (delegate != null) {
1236 try {
1237 mPM.removeOnPermissionsChangeListener(delegate);
1238 mPermissionListeners.remove(listener);
1239 } catch (RemoteException e) {
1240 throw new RuntimeException("Package manager has died", e);
1241 }
1242 }
1243 }
1244 }
1245
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001246 static void configurationChanged() {
1247 synchronized (sSync) {
1248 sIconCache.clear();
1249 sStringCache.clear();
1250 }
1251 }
1252
1253 ApplicationPackageManager(ContextImpl context,
1254 IPackageManager pm) {
1255 mContext = context;
1256 mPM = pm;
1257 }
1258
Alan Viveretteecd585a2015-04-13 10:32:51 -07001259 @Nullable
1260 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001261 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001262 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001263 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1264 + name + ": " + wr);
1265 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001266 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001267 if (state != null) {
1268 if (DEBUG_ICONS) {
1269 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1270 }
1271 // Note: It's okay here to not use the newDrawable(Resources) variant
1272 // of the API. The ConstantState comes from a drawable that was
1273 // originally created by passing the proper app Resources instance
1274 // which means the state should already contain the proper
1275 // resources specific information (like density.) See
1276 // BitmapDrawable.BitmapState for instance.
1277 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001278 }
1279 // our entry has been purged
1280 sIconCache.remove(name);
1281 }
1282 }
1283 return null;
1284 }
1285
Alan Viveretteecd585a2015-04-13 10:32:51 -07001286 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001287 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001288 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001289 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001290 }
1291 }
1292
Romain Guy39fe17c2011-11-30 10:34:07 -08001293 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001294 boolean immediateGc = false;
1295 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1296 immediateGc = true;
1297 }
1298 if (pkgList != null && (pkgList.length > 0)) {
1299 boolean needCleanup = false;
1300 for (String ssp : pkgList) {
1301 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001302 for (int i=sIconCache.size()-1; i>=0; i--) {
1303 ResourceName nm = sIconCache.keyAt(i);
1304 if (nm.packageName.equals(ssp)) {
1305 //Log.i(TAG, "Removing cached drawable for " + nm);
1306 sIconCache.removeAt(i);
1307 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001308 }
1309 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001310 for (int i=sStringCache.size()-1; i>=0; i--) {
1311 ResourceName nm = sStringCache.keyAt(i);
1312 if (nm.packageName.equals(ssp)) {
1313 //Log.i(TAG, "Removing cached string for " + nm);
1314 sStringCache.removeAt(i);
1315 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001316 }
1317 }
1318 }
1319 }
1320 if (needCleanup || hasPkgInfo) {
1321 if (immediateGc) {
1322 // Schedule an immediate gc.
1323 Runtime.getRuntime().gc();
1324 } else {
1325 ActivityThread.currentActivityThread().scheduleGcIdler();
1326 }
1327 }
1328 }
1329 }
1330
1331 private static final class ResourceName {
1332 final String packageName;
1333 final int iconId;
1334
1335 ResourceName(String _packageName, int _iconId) {
1336 packageName = _packageName;
1337 iconId = _iconId;
1338 }
1339
1340 ResourceName(ApplicationInfo aInfo, int _iconId) {
1341 this(aInfo.packageName, _iconId);
1342 }
1343
1344 ResourceName(ComponentInfo cInfo, int _iconId) {
1345 this(cInfo.applicationInfo.packageName, _iconId);
1346 }
1347
1348 ResourceName(ResolveInfo rInfo, int _iconId) {
1349 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1350 }
1351
1352 @Override
1353 public boolean equals(Object o) {
1354 if (this == o) return true;
1355 if (o == null || getClass() != o.getClass()) return false;
1356
1357 ResourceName that = (ResourceName) o;
1358
1359 if (iconId != that.iconId) return false;
1360 return !(packageName != null ?
1361 !packageName.equals(that.packageName) : that.packageName != null);
1362
1363 }
1364
1365 @Override
1366 public int hashCode() {
1367 int result;
1368 result = packageName.hashCode();
1369 result = 31 * result + iconId;
1370 return result;
1371 }
1372
1373 @Override
1374 public String toString() {
1375 return "{ResourceName " + packageName + " / " + iconId + "}";
1376 }
1377 }
1378
1379 private CharSequence getCachedString(ResourceName name) {
1380 synchronized (sSync) {
1381 WeakReference<CharSequence> wr = sStringCache.get(name);
1382 if (wr != null) { // we have the activity
1383 CharSequence cs = wr.get();
1384 if (cs != null) {
1385 return cs;
1386 }
1387 // our entry has been purged
1388 sStringCache.remove(name);
1389 }
1390 }
1391 return null;
1392 }
1393
1394 private void putCachedString(ResourceName name, CharSequence cs) {
1395 synchronized (sSync) {
1396 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1397 }
1398 }
1399
1400 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001401 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001402 ApplicationInfo appInfo) {
1403 ResourceName name = new ResourceName(packageName, resid);
1404 CharSequence text = getCachedString(name);
1405 if (text != null) {
1406 return text;
1407 }
1408 if (appInfo == null) {
1409 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001410 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001411 } catch (NameNotFoundException e) {
1412 return null;
1413 }
1414 }
1415 try {
1416 Resources r = getResourcesForApplication(appInfo);
1417 text = r.getText(resid);
1418 putCachedString(name, text);
1419 return text;
1420 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001421 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001422 + appInfo.packageName);
1423 } catch (RuntimeException e) {
1424 // If an exception was thrown, fall through to return
1425 // default icon.
1426 Log.w("PackageManager", "Failure retrieving text 0x"
1427 + Integer.toHexString(resid) + " in package "
1428 + packageName, e);
1429 }
1430 return null;
1431 }
1432
1433 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001434 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001435 ApplicationInfo appInfo) {
1436 if (appInfo == null) {
1437 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001438 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001439 } catch (NameNotFoundException e) {
1440 return null;
1441 }
1442 }
1443 try {
1444 Resources r = getResourcesForApplication(appInfo);
1445 return r.getXml(resid);
1446 } catch (RuntimeException e) {
1447 // If an exception was thrown, fall through to return
1448 // default icon.
1449 Log.w("PackageManager", "Failure retrieving xml 0x"
1450 + Integer.toHexString(resid) + " in package "
1451 + packageName, e);
1452 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001453 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001454 + appInfo.packageName);
1455 }
1456 return null;
1457 }
1458
1459 @Override
1460 public CharSequence getApplicationLabel(ApplicationInfo info) {
1461 return info.loadLabel(this);
1462 }
1463
1464 @Override
1465 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1466 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001467 final VerificationParams verificationParams = new VerificationParams(null, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001468 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001469 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001470 installerPackageName, verificationParams, null, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001471 }
1472
1473 @Override
Kenny Root5ab21572011-07-27 11:11:19 -07001474 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1475 int flags, String installerPackageName, Uri verificationURI,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001476 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001477 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001478 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001479 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001480 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Kenny Root5ab21572011-07-27 11:11:19 -07001481 }
1482
1483 @Override
John Spurlock8a985d22014-02-25 09:40:05 -05001484 public void installPackageWithVerificationAndEncryption(Uri packageURI,
rich cannings706e8ba2012-08-20 13:20:14 -07001485 IPackageInstallObserver observer, int flags, String installerPackageName,
1486 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001487 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001488 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001489 }
1490
Christopher Tatef1977b42014-03-24 16:25:51 -07001491 @Override
1492 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1493 int flags, String installerPackageName) {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001494 installPackageAsUser(packageURI, observer, flags, installerPackageName,
Robin Lee0e27c872015-09-28 14:37:40 +01001495 mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001496 }
1497
1498 @Override
1499 public void installPackageAsUser(Uri packageURI, PackageInstallObserver observer, int flags,
1500 String installerPackageName, int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001501 final VerificationParams verificationParams = new VerificationParams(null, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001502 null, VerificationParams.NO_UID);
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001503 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null,
1504 userId);
Christopher Tatef1977b42014-03-24 16:25:51 -07001505 }
1506
1507 @Override
1508 public void installPackageWithVerification(Uri packageURI,
1509 PackageInstallObserver observer, int flags, String installerPackageName,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001510 Uri verificationURI,
Christopher Tatef1977b42014-03-24 16:25:51 -07001511 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001512 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001513 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001514 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001515 encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001516 }
1517
1518 @Override
1519 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1520 PackageInstallObserver observer, int flags, String installerPackageName,
1521 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001522 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001523 encryptionParams, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001524 }
1525
1526 private void installCommon(Uri packageURI,
1527 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001528 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams,
1529 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001530 if (!"file".equals(packageURI.getScheme())) {
1531 throw new UnsupportedOperationException("Only file:// URIs are supported");
1532 }
1533 if (encryptionParams != null) {
1534 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1535 }
1536
1537 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001538 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001539 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
1540 verificationParams, null, userId);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001541 } catch (RemoteException ignored) {
rich cannings706e8ba2012-08-20 13:20:14 -07001542 }
1543 }
1544
1545 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001546 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001547 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001548 }
1549
1550 @Override
1551 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001552 throws NameNotFoundException {
1553 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001554 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001555 if (res == INSTALL_FAILED_INVALID_URI) {
1556 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1557 }
1558 return res;
1559 } catch (RemoteException e) {
1560 // Should never happen!
1561 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1562 }
1563 }
1564
1565 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001566 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001567 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001568 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001569 } catch (RemoteException e) {
1570 // Should never happen!
1571 }
1572 }
1573
1574 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001575 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1576 long millisecondsToDelay) {
1577 try {
1578 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1579 } catch (RemoteException e) {
1580 // Should never happen!
1581 }
1582 }
1583
1584 @Override
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001585 public void verifyIntentFilter(int id, int verificationCode, List<String> outFailedDomains) {
1586 try {
1587 mPM.verifyIntentFilter(id, verificationCode, outFailedDomains);
1588 } catch (RemoteException e) {
1589 // Should never happen!
1590 }
1591 }
1592
1593 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001594 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001595 try {
1596 return mPM.getIntentVerificationStatus(packageName, userId);
1597 } catch (RemoteException e) {
1598 // Should never happen!
1599 return PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1600 }
1601 }
1602
1603 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001604 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001605 try {
1606 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1607 } catch (RemoteException e) {
1608 // Should never happen!
1609 return false;
1610 }
1611 }
1612
1613 @Override
1614 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1615 try {
1616 return mPM.getIntentFilterVerifications(packageName);
1617 } catch (RemoteException e) {
1618 // Should never happen!
1619 return null;
1620 }
1621 }
1622
1623 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001624 public List<IntentFilter> getAllIntentFilters(String packageName) {
1625 try {
1626 return mPM.getAllIntentFilters(packageName);
1627 } catch (RemoteException e) {
1628 // Should never happen!
1629 return null;
1630 }
1631 }
1632
1633 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001634 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001635 try {
1636 return mPM.getDefaultBrowserPackageName(userId);
1637 } catch (RemoteException e) {
1638 // Should never happen!
1639 return null;
1640 }
1641 }
1642
1643 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001644 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001645 try {
1646 return mPM.setDefaultBrowserPackageName(packageName, userId);
1647 } catch (RemoteException e) {
1648 // Should never happen!
1649 return false;
1650 }
1651 }
1652
1653 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001654 public void setInstallerPackageName(String targetPackage,
1655 String installerPackageName) {
1656 try {
1657 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1658 } catch (RemoteException e) {
1659 // Should never happen!
1660 }
1661 }
1662
1663 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001664 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001665 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001666 return mPM.getInstallerPackageName(packageName);
1667 } catch (RemoteException e) {
1668 // Should never happen!
1669 }
1670 return null;
1671 }
1672
1673 @Override
1674 public int getMoveStatus(int moveId) {
1675 try {
1676 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001677 } catch (RemoteException e) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001678 throw e.rethrowAsRuntimeException();
1679 }
1680 }
1681
1682 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001683 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1684 synchronized (mDelegates) {
1685 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1686 handler.getLooper());
1687 try {
1688 mPM.registerMoveCallback(delegate);
1689 } catch (RemoteException e) {
1690 throw e.rethrowAsRuntimeException();
1691 }
1692 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001693 }
1694 }
1695
1696 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001697 public void unregisterMoveCallback(MoveCallback callback) {
1698 synchronized (mDelegates) {
1699 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1700 final MoveCallbackDelegate delegate = i.next();
1701 if (delegate.mCallback == callback) {
1702 try {
1703 mPM.unregisterMoveCallback(delegate);
1704 } catch (RemoteException e) {
1705 throw e.rethrowAsRuntimeException();
1706 }
1707 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001708 }
1709 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001710 }
1711 }
1712
1713 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001714 public int movePackage(String packageName, VolumeInfo vol) {
1715 try {
1716 final String volumeUuid;
1717 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1718 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1719 } else if (vol.isPrimaryPhysical()) {
1720 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1721 } else {
1722 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1723 }
1724
1725 return mPM.movePackage(packageName, volumeUuid);
1726 } catch (RemoteException e) {
1727 throw e.rethrowAsRuntimeException();
1728 }
1729 }
1730
1731 @Override
1732 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001733 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001734 if (app.isInternal()) {
1735 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1736 } else if (app.isExternalAsec()) {
1737 return storage.getPrimaryPhysicalVolume();
1738 } else {
1739 return storage.findVolumeByUuid(app.volumeUuid);
1740 }
1741 }
1742
1743 @Override
1744 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1745 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1746 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001747 final List<VolumeInfo> vols = storage.getVolumes();
1748 final List<VolumeInfo> candidates = new ArrayList<>();
1749 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001750 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001751 candidates.add(vol);
1752 }
1753 }
1754 return candidates;
1755 }
1756
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001757 private static boolean isPackageCandidateVolume(
1758 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1759 final boolean forceAllowOnExternal = Settings.Global.getInt(
1760 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001761 // Private internal is always an option
1762 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1763 return true;
1764 }
1765
1766 // System apps and apps demanding internal storage can't be moved
1767 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001768 if (app.isSystemApp()) {
1769 return false;
1770 }
1771 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001772 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1773 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001774 return false;
1775 }
1776
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001777 // Gotta be able to write there
1778 if (!vol.isMountedWritable()) {
1779 return false;
1780 }
1781
1782 // Moving into an ASEC on public primary is only option internal
1783 if (vol.isPrimaryPhysical()) {
1784 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001785 }
1786
1787 // Otherwise we can move to any private volume
1788 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1789 }
1790
1791 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001792 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001793 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001794 final String volumeUuid;
1795 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1796 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1797 } else if (vol.isPrimaryPhysical()) {
1798 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1799 } else {
1800 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1801 }
1802
1803 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001804 } catch (RemoteException e) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001805 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001806 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001807 }
1808
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001809 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001810 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1811 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1812 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001813 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001814 }
1815
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001816 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001817 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1818 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1819 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1820 final List<VolumeInfo> vols = storage.getVolumes();
1821 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001822 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1823 storage.getPrimaryStorageUuid()) && currentVol != null) {
1824 // TODO: support moving primary physical to emulated volume
1825 candidates.add(currentVol);
1826 } else {
1827 for (VolumeInfo vol : vols) {
1828 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1829 candidates.add(vol);
1830 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001831 }
1832 }
1833 return candidates;
1834 }
1835
1836 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1837 // Private internal is always an option
1838 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1839 return true;
1840 }
1841
1842 // Gotta be able to write there
1843 if (!vol.isMountedWritable()) {
1844 return false;
1845 }
1846
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001847 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001848 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001849 }
1850
1851 @Override
1852 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001853 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001854 }
1855
1856 @Override
1857 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1858 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001859 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001860 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001861 } catch (RemoteException e) {
1862 // Should never happen!
1863 }
1864 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001865
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001866 @Override
1867 public void clearApplicationUserData(String packageName,
1868 IPackageDataObserver observer) {
1869 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001870 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001871 } catch (RemoteException e) {
1872 // Should never happen!
1873 }
1874 }
1875 @Override
1876 public void deleteApplicationCacheFiles(String packageName,
1877 IPackageDataObserver observer) {
1878 try {
1879 mPM.deleteApplicationCacheFiles(packageName, observer);
1880 } catch (RemoteException e) {
1881 // Should never happen!
1882 }
1883 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001884
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001885 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001886 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1887 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001888 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001889 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001890 } catch (RemoteException e) {
1891 // Should never happen!
1892 }
1893 }
1894
1895 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001896 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001897 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001898 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001899 } catch (RemoteException e) {
1900 // Should never happen!
1901 }
1902 }
1903
1904 @Override
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001905 public boolean setPackageSuspendedAsUser(String packageName, boolean suspended, int userId) {
1906 try {
1907 return mPM.setPackageSuspendedAsUser(packageName, suspended, userId);
1908 } catch (RemoteException e) {
1909 // Should never happen!
1910 }
1911 return false;
1912 }
1913
1914 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001915 public boolean isPackageSuspendedForUser(String packageName, int userId) {
1916 try {
1917 return mPM.isPackageSuspendedForUser(packageName, userId);
1918 } catch (RemoteException e) {
1919 // Should never happen!
1920 }
1921 return false;
1922 }
1923
1924 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001925 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001926 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001927 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001928 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001929 } catch (RemoteException e) {
1930 // Should never happen!
1931 }
1932 }
1933 @Override
1934 public void addPackageToPreferred(String packageName) {
1935 try {
1936 mPM.addPackageToPreferred(packageName);
1937 } catch (RemoteException e) {
1938 // Should never happen!
1939 }
1940 }
1941
1942 @Override
1943 public void removePackageFromPreferred(String packageName) {
1944 try {
1945 mPM.removePackageFromPreferred(packageName);
1946 } catch (RemoteException e) {
1947 // Should never happen!
1948 }
1949 }
1950
1951 @Override
1952 public List<PackageInfo> getPreferredPackages(int flags) {
1953 try {
1954 return mPM.getPreferredPackages(flags);
1955 } catch (RemoteException e) {
1956 // Should never happen!
1957 }
1958 return new ArrayList<PackageInfo>();
1959 }
1960
1961 @Override
1962 public void addPreferredActivity(IntentFilter filter,
1963 int match, ComponentName[] set, ComponentName activity) {
1964 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001965 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001966 } catch (RemoteException e) {
1967 // Should never happen!
1968 }
1969 }
1970
1971 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001972 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07001973 ComponentName[] set, ComponentName activity, int userId) {
1974 try {
1975 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001976 } catch (RemoteException e) {
1977 // Should never happen!
1978 }
1979 }
1980
1981 @Override
1982 public void replacePreferredActivity(IntentFilter filter,
1983 int match, ComponentName[] set, ComponentName activity) {
1984 try {
Robin Lee0e27c872015-09-28 14:37:40 +01001985 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001986 } catch (RemoteException e) {
1987 // Should never happen!
1988 }
1989 }
1990
1991 @Override
1992 public void replacePreferredActivityAsUser(IntentFilter filter,
1993 int match, ComponentName[] set, ComponentName activity,
1994 int userId) {
1995 try {
1996 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001997 } catch (RemoteException e) {
1998 // Should never happen!
1999 }
2000 }
2001
2002 @Override
2003 public void clearPackagePreferredActivities(String packageName) {
2004 try {
2005 mPM.clearPackagePreferredActivities(packageName);
2006 } catch (RemoteException e) {
2007 // Should never happen!
2008 }
2009 }
2010
2011 @Override
2012 public int getPreferredActivities(List<IntentFilter> outFilters,
2013 List<ComponentName> outActivities, String packageName) {
2014 try {
2015 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2016 } catch (RemoteException e) {
2017 // Should never happen!
2018 }
2019 return 0;
2020 }
2021
2022 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002023 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2024 try {
2025 return mPM.getHomeActivities(outActivities);
2026 } catch (RemoteException e) {
2027 // Should never happen!
2028 }
2029 return null;
2030 }
2031
2032 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002033 public void setComponentEnabledSetting(ComponentName componentName,
2034 int newState, int flags) {
2035 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002036 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002037 } catch (RemoteException e) {
2038 // Should never happen!
2039 }
2040 }
2041
2042 @Override
2043 public int getComponentEnabledSetting(ComponentName componentName) {
2044 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002045 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002046 } catch (RemoteException e) {
2047 // Should never happen!
2048 }
2049 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2050 }
2051
2052 @Override
2053 public void setApplicationEnabledSetting(String packageName,
2054 int newState, int flags) {
2055 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002056 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002057 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002058 } catch (RemoteException e) {
2059 // Should never happen!
2060 }
2061 }
2062
2063 @Override
2064 public int getApplicationEnabledSetting(String packageName) {
2065 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002066 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002067 } catch (RemoteException e) {
2068 // Should never happen!
2069 }
2070 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2071 }
2072
Amith Yamasani655d0e22013-06-12 14:19:10 -07002073 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002074 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002075 UserHandle user) {
2076 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002077 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002078 user.getIdentifier());
2079 } catch (RemoteException re) {
2080 // Should never happen!
2081 }
2082 return false;
2083 }
2084
2085 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002086 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002087 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002088 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Amith Yamasani655d0e22013-06-12 14:19:10 -07002089 } catch (RemoteException re) {
2090 // Should never happen!
2091 }
2092 return false;
2093 }
2094
dcashmanc6f22492014-08-14 09:54:51 -07002095 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002096 @Override
2097 public KeySet getKeySetByAlias(String packageName, String alias) {
2098 Preconditions.checkNotNull(packageName);
2099 Preconditions.checkNotNull(alias);
dcashmanc6f22492014-08-14 09:54:51 -07002100 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002101 try {
dcashmanc6f22492014-08-14 09:54:51 -07002102 ks = mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002103 } catch (RemoteException e) {
2104 return null;
2105 }
dcashmanc6f22492014-08-14 09:54:51 -07002106 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002107 }
2108
dcashmanc6f22492014-08-14 09:54:51 -07002109 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002110 @Override
2111 public KeySet getSigningKeySet(String packageName) {
2112 Preconditions.checkNotNull(packageName);
dcashmanc6f22492014-08-14 09:54:51 -07002113 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002114 try {
dcashmanc6f22492014-08-14 09:54:51 -07002115 ks = mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002116 } catch (RemoteException e) {
2117 return null;
2118 }
dcashmanc6f22492014-08-14 09:54:51 -07002119 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002120 }
2121
dcashmanc6f22492014-08-14 09:54:51 -07002122 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002123 @Override
2124 public boolean isSignedBy(String packageName, KeySet ks) {
2125 Preconditions.checkNotNull(packageName);
2126 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002127 try {
dcashmanc6f22492014-08-14 09:54:51 -07002128 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002129 } catch (RemoteException e) {
2130 return false;
2131 }
2132 }
2133
dcashmanc6f22492014-08-14 09:54:51 -07002134 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002135 @Override
2136 public boolean isSignedByExactly(String packageName, KeySet ks) {
2137 Preconditions.checkNotNull(packageName);
2138 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002139 try {
dcashmanc6f22492014-08-14 09:54:51 -07002140 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002141 } catch (RemoteException e) {
2142 return false;
2143 }
2144 }
2145
Kenny Root0aaa0d92011-09-12 16:42:55 -07002146 /**
2147 * @hide
2148 */
2149 @Override
2150 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2151 try {
2152 return mPM.getVerifierDeviceIdentity();
2153 } catch (RemoteException e) {
2154 // Should never happen!
2155 }
2156 return null;
2157 }
2158
Jeff Hao9f60c082014-10-28 18:51:07 -07002159 /**
2160 * @hide
2161 */
2162 @Override
2163 public boolean isUpgrade() {
2164 try {
2165 return mPM.isUpgrade();
2166 } catch (RemoteException e) {
2167 return false;
2168 }
2169 }
2170
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002171 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002172 public PackageInstaller getPackageInstaller() {
2173 synchronized (mLock) {
2174 if (mInstaller == null) {
2175 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002176 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002177 mContext.getPackageName(), mContext.getUserId());
2178 } catch (RemoteException e) {
2179 throw e.rethrowAsRuntimeException();
2180 }
2181 }
2182 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002183 }
2184 }
2185
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002186 @Override
2187 public boolean isPackageAvailable(String packageName) {
2188 try {
2189 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2190 } catch (RemoteException e) {
2191 throw e.rethrowAsRuntimeException();
2192 }
2193 }
2194
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002195 /**
2196 * @hide
2197 */
2198 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002199 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2200 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002201 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002202 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002203 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002204 } catch (RemoteException e) {
2205 // Should never happen!
2206 }
2207 }
2208
2209 /**
2210 * @hide
2211 */
2212 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002213 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002214 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002215 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002216 } catch (RemoteException e) {
2217 // Should never happen!
2218 }
2219 }
2220
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002221 /**
2222 * @hide
2223 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002224 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002225 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2226 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2227 return dr;
2228 }
2229 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2230 }
2231
2232 /**
2233 * @hide
2234 */
2235 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002236 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002237 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2238 if (bitmap == null) {
2239 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2240 }
2241 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002242 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002243 Drawable dr = null;
2244 if (itemInfo.packageName != null) {
2245 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2246 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002247 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002248 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002249 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002250 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002251 }
2252
2253 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2254 Rect badgeLocation, boolean tryBadgeInPlace) {
2255 final int badgedWidth = drawable.getIntrinsicWidth();
2256 final int badgedHeight = drawable.getIntrinsicHeight();
2257 final boolean canBadgeInPlace = tryBadgeInPlace
2258 && (drawable instanceof BitmapDrawable)
2259 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2260
2261 final Bitmap bitmap;
2262 if (canBadgeInPlace) {
2263 bitmap = ((BitmapDrawable) drawable).getBitmap();
2264 } else {
2265 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2266 }
2267 Canvas canvas = new Canvas(bitmap);
2268
2269 if (!canBadgeInPlace) {
2270 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2271 drawable.draw(canvas);
2272 }
2273
2274 if (badgeLocation != null) {
2275 if (badgeLocation.left < 0 || badgeLocation.top < 0
2276 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2277 throw new IllegalArgumentException("Badge location " + badgeLocation
2278 + " not in badged drawable bounds "
2279 + new Rect(0, 0, badgedWidth, badgedHeight));
2280 }
2281 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2282
2283 canvas.save();
2284 canvas.translate(badgeLocation.left, badgeLocation.top);
2285 badgeDrawable.draw(canvas);
2286 canvas.restore();
2287 } else {
2288 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2289 badgeDrawable.draw(canvas);
2290 }
2291
2292 if (!canBadgeInPlace) {
2293 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2294
2295 if (drawable instanceof BitmapDrawable) {
2296 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2297 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2298 }
2299
2300 return mergedDrawable;
2301 }
2302
2303 return drawable;
2304 }
2305
2306 private int getBadgeResIdForUser(int userHandle) {
2307 // Return the framework-provided badge.
2308 UserInfo userInfo = getUserIfProfile(userHandle);
2309 if (userInfo != null && userInfo.isManagedProfile()) {
2310 return com.android.internal.R.drawable.ic_corp_icon_badge;
2311 }
2312 return 0;
2313 }
2314
2315 private UserInfo getUserIfProfile(int userHandle) {
Robin Lee0e27c872015-09-28 14:37:40 +01002316 List<UserInfo> userProfiles = getUserManager().getProfiles(mContext.getUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07002317 for (UserInfo user : userProfiles) {
2318 if (user.id == userHandle) {
2319 return user;
2320 }
2321 }
2322 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002323 }
2324
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002325 /** {@hide} */
2326 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2327 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002328 private static final int MSG_CREATED = 1;
2329 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002330
2331 final MoveCallback mCallback;
2332 final Handler mHandler;
2333
2334 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2335 mCallback = callback;
2336 mHandler = new Handler(looper, this);
2337 }
2338
2339 @Override
2340 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002341 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002342 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002343 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002344 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002345 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002346 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002347 }
2348 case MSG_STATUS_CHANGED: {
2349 final SomeArgs args = (SomeArgs) msg.obj;
2350 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2351 args.recycle();
2352 return true;
2353 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002354 }
2355 return false;
2356 }
2357
2358 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002359 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002360 final SomeArgs args = SomeArgs.obtain();
2361 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002362 args.arg2 = extras;
2363 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2364 }
2365
2366 @Override
2367 public void onStatusChanged(int moveId, int status, long estMillis) {
2368 final SomeArgs args = SomeArgs.obtain();
2369 args.argi1 = moveId;
2370 args.argi2 = status;
2371 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002372 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002373 }
2374 }
2375
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002376 private final ContextImpl mContext;
2377 private final IPackageManager mPM;
2378
2379 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002380 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2381 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2382 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2383 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002384
2385 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2386 mPermissionListeners = new ArrayMap<>();
2387
2388 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2389 implements Handler.Callback{
2390 private static final int MSG_PERMISSIONS_CHANGED = 1;
2391
2392 private final OnPermissionsChangedListener mListener;
2393 private final Handler mHandler;
2394
2395
2396 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2397 Looper looper) {
2398 mListener = listener;
2399 mHandler = new Handler(looper, this);
2400 }
2401
2402 @Override
2403 public void onPermissionsChanged(int uid) {
2404 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2405 }
2406
2407 @Override
2408 public boolean handleMessage(Message msg) {
2409 switch (msg.what) {
2410 case MSG_PERMISSIONS_CHANGED: {
2411 final int uid = msg.arg1;
2412 mListener.onPermissionsChanged(uid);
2413 return true;
2414 }
2415 }
2416 return false;
2417 }
2418 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002419}