blob: c071162ca5b9ce299b0b52f8942c7117fa419e62 [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
432 @Override
433 public FeatureInfo[] getSystemAvailableFeatures() {
434 try {
435 return mPM.getSystemAvailableFeatures();
436 } catch (RemoteException e) {
437 throw new RuntimeException("Package manager has died", e);
438 }
439 }
440
441 @Override
442 public boolean hasSystemFeature(String name) {
443 try {
444 return mPM.hasSystemFeature(name);
445 } catch (RemoteException e) {
446 throw new RuntimeException("Package manager has died", e);
447 }
448 }
449
450 @Override
451 public int checkPermission(String permName, String pkgName) {
452 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800453 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800454 } catch (RemoteException e) {
455 throw new RuntimeException("Package manager has died", e);
456 }
457 }
458
459 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700460 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
461 try {
462 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
463 } catch (RemoteException e) {
464 throw new RuntimeException("Package manager has died", e);
465 }
466 }
467
Svet Ganovf1b7f202015-07-29 08:33:42 -0700468 /**
469 * @hide
470 */
471 @Override
472 public String getPermissionControllerPackageName() {
473 synchronized (mLock) {
474 if (mPermissionsControllerPackageName == null) {
475 try {
476 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
477 } catch (RemoteException e) {
478 throw new RuntimeException("Package manager has died", e);
479 }
480 }
481 return mPermissionsControllerPackageName;
482 }
483 }
484
Svet Ganovad3b2972015-07-07 22:49:17 -0700485 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800486 public boolean addPermission(PermissionInfo info) {
487 try {
488 return mPM.addPermission(info);
489 } catch (RemoteException e) {
490 throw new RuntimeException("Package manager has died", e);
491 }
492 }
493
494 @Override
495 public boolean addPermissionAsync(PermissionInfo info) {
496 try {
497 return mPM.addPermissionAsync(info);
498 } catch (RemoteException e) {
499 throw new RuntimeException("Package manager has died", e);
500 }
501 }
502
503 @Override
504 public void removePermission(String name) {
505 try {
506 mPM.removePermission(name);
507 } catch (RemoteException e) {
508 throw new RuntimeException("Package manager has died", e);
509 }
510 }
511
512 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700513 public void grantRuntimePermission(String packageName, String permissionName,
514 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800515 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700516 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800517 } 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 revokeRuntimePermission(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.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
527 } catch (RemoteException e) {
528 throw new RuntimeException("Package manager has died", e);
529 }
530 }
531
532 @Override
533 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
534 try {
535 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
536 } catch (RemoteException e) {
537 throw new RuntimeException("Package manager has died", e);
538 }
539 }
540
541 @Override
542 public void updatePermissionFlags(String permissionName, String packageName,
543 int flagMask, int flagValues, UserHandle user) {
544 try {
545 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
546 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800547 } catch (RemoteException e) {
548 throw new RuntimeException("Package manager has died", e);
549 }
550 }
551
552 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700553 public boolean shouldShowRequestPermissionRationale(String permission) {
554 try {
555 return mPM.shouldShowRequestPermissionRationale(permission,
556 mContext.getPackageName(), mContext.getUserId());
557 } catch (RemoteException e) {
558 throw new RuntimeException("Package manager has died", e);
559 }
560 }
561
562 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800563 public int checkSignatures(String pkg1, String pkg2) {
564 try {
565 return mPM.checkSignatures(pkg1, pkg2);
566 } catch (RemoteException e) {
567 throw new RuntimeException("Package manager has died", e);
568 }
569 }
570
571 @Override
572 public int checkSignatures(int uid1, int uid2) {
573 try {
574 return mPM.checkUidSignatures(uid1, uid2);
575 } catch (RemoteException e) {
576 throw new RuntimeException("Package manager has died", e);
577 }
578 }
579
580 @Override
581 public String[] getPackagesForUid(int uid) {
582 try {
583 return mPM.getPackagesForUid(uid);
584 } catch (RemoteException e) {
585 throw new RuntimeException("Package manager has died", e);
586 }
587 }
588
589 @Override
590 public String getNameForUid(int uid) {
591 try {
592 return mPM.getNameForUid(uid);
593 } catch (RemoteException e) {
594 throw new RuntimeException("Package manager has died", e);
595 }
596 }
597
598 @Override
599 public int getUidForSharedUser(String sharedUserName)
600 throws NameNotFoundException {
601 try {
602 int uid = mPM.getUidForSharedUser(sharedUserName);
603 if(uid != -1) {
604 return uid;
605 }
606 } catch (RemoteException e) {
607 throw new RuntimeException("Package manager has died", e);
608 }
609 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
610 }
611
Kenny Roote6cd0c72011-05-19 12:48:14 -0700612 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800613 @Override
614 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700615 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700616 }
617
618 /** @hide */
619 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700620 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800621 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800622 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
623 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800624 } catch (RemoteException e) {
625 throw new RuntimeException("Package manager has died", e);
626 }
627 }
628
Kenny Roote6cd0c72011-05-19 12:48:14 -0700629 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800630 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800631 public List<PackageInfo> getPackagesHoldingPermissions(
632 String[] permissions, int flags) {
633 final int userId = mContext.getUserId();
634 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800635 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
636 permissions, flags, userId);
637 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800638 } catch (RemoteException e) {
639 throw new RuntimeException("Package manager has died", e);
640 }
641 }
642
643 @SuppressWarnings("unchecked")
644 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800645 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700646 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800647 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800648 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
649 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800650 } catch (RemoteException e) {
651 throw new RuntimeException("Package manager has died", e);
652 }
653 }
654
Svet Ganov2acf0632015-11-24 19:10:59 -0800655 /** @hide */
656 @SuppressWarnings("unchecked")
657 @Override
658 public List<EphemeralApplicationInfo> getEphemeralApplications() {
659 try {
660 ParceledListSlice<EphemeralApplicationInfo> slice =
661 mPM.getEphemeralApplications(mContext.getUserId());
662 if (slice != null) {
663 return slice.getList();
664 }
665 return Collections.emptyList();
666 } catch (RemoteException e) {
667 throw new RuntimeException("Package manager has died", e);
668 }
669 }
670
671 /** @hide */
672 @Override
673 public Drawable getEphemeralApplicationIcon(String packageName) {
674 try {
675 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
676 packageName, mContext.getUserId());
677 if (bitmap != null) {
678 return new BitmapDrawable(null, bitmap);
679 }
680 return null;
681 } catch (RemoteException e) {
682 throw new RuntimeException("Package manager has died", e);
683 }
684 }
685
686 @Override
687 public boolean isEphemeralApplication() {
688 try {
689 return mPM.isEphemeralApplication(
690 mContext.getPackageName(), mContext.getUserId());
691 } catch (RemoteException e) {
692 Log.e(TAG, "System server is dead", e);
693 }
694 return false;
695 }
696
697 @Override
698 public int getEphemeralCookieMaxSizeBytes() {
699 return Settings.Global.getInt(mContext.getContentResolver(),
700 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
701 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
702 }
703
704 @Override
705 public @NonNull byte[] getEphemeralCookie() {
706 try {
707 final byte[] cookie = mPM.getEphemeralApplicationCookie(
708 mContext.getPackageName(), mContext.getUserId());
709 if (cookie != null) {
710 return cookie;
711 }
712 } catch (RemoteException e) {
713 Log.e(TAG, "System server is dead", e);
714 }
715 return EmptyArray.BYTE;
716 }
717
718 @Override
719 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
720 try {
721 return mPM.setEphemeralApplicationCookie(
722 mContext.getPackageName(), cookie, mContext.getUserId());
723 } catch (RemoteException e) {
724 Log.e(TAG, "System server is dead", e);
725 }
726 return false;
727 }
728
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800729 @Override
730 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700731 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700732 }
733
734 @Override
735 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800736 try {
737 return mPM.resolveIntent(
738 intent,
739 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700740 flags,
741 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800742 } catch (RemoteException e) {
743 throw new RuntimeException("Package manager has died", e);
744 }
745 }
746
747 @Override
748 public List<ResolveInfo> queryIntentActivities(Intent intent,
749 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700750 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700751 }
752
753 /** @hide Same as above but for a specific user */
754 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700755 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700756 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800757 try {
758 return mPM.queryIntentActivities(
759 intent,
760 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700761 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700762 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800763 } catch (RemoteException e) {
764 throw new RuntimeException("Package manager has died", e);
765 }
766 }
767
768 @Override
769 public List<ResolveInfo> queryIntentActivityOptions(
770 ComponentName caller, Intent[] specifics, Intent intent,
771 int flags) {
772 final ContentResolver resolver = mContext.getContentResolver();
773
774 String[] specificTypes = null;
775 if (specifics != null) {
776 final int N = specifics.length;
777 for (int i=0; i<N; i++) {
778 Intent sp = specifics[i];
779 if (sp != null) {
780 String t = sp.resolveTypeIfNeeded(resolver);
781 if (t != null) {
782 if (specificTypes == null) {
783 specificTypes = new String[N];
784 }
785 specificTypes[i] = t;
786 }
787 }
788 }
789 }
790
791 try {
792 return mPM.queryIntentActivityOptions(caller, specifics,
793 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700794 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800795 } catch (RemoteException e) {
796 throw new RuntimeException("Package manager has died", e);
797 }
798 }
799
Amith Yamasanif203aee2012-08-29 18:41:53 -0700800 /**
801 * @hide
802 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800803 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700804 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800805 try {
806 return mPM.queryIntentReceivers(
807 intent,
808 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700809 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -0700810 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800811 } catch (RemoteException e) {
812 throw new RuntimeException("Package manager has died", e);
813 }
814 }
815
816 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700817 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700818 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700819 }
820
821 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800822 public ResolveInfo resolveService(Intent intent, int flags) {
823 try {
824 return mPM.resolveService(
825 intent,
826 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700827 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700828 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800829 } catch (RemoteException e) {
830 throw new RuntimeException("Package manager has died", e);
831 }
832 }
833
834 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700835 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800836 try {
837 return mPM.queryIntentServices(
838 intent,
839 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700840 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700841 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800842 } catch (RemoteException e) {
843 throw new RuntimeException("Package manager has died", e);
844 }
845 }
846
847 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700848 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700849 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700850 }
851
852 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700853 public List<ResolveInfo> queryIntentContentProvidersAsUser(
854 Intent intent, int flags, int userId) {
855 try {
856 return mPM.queryIntentContentProviders(intent,
857 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
858 } catch (RemoteException e) {
859 throw new RuntimeException("Package manager has died", e);
860 }
861 }
862
863 @Override
864 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
865 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
866 }
867
868 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100869 public ProviderInfo resolveContentProvider(String name, int flags) {
870 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
871 }
872
873 /** @hide **/
874 @Override
875 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800876 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100877 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800878 } catch (RemoteException e) {
879 throw new RuntimeException("Package manager has died", e);
880 }
881 }
882
883 @Override
884 public List<ProviderInfo> queryContentProviders(String processName,
885 int uid, int flags) {
886 try {
Dianne Hackborn28ec27c2015-08-03 15:28:28 -0700887 ParceledListSlice<ProviderInfo> slice
888 = mPM.queryContentProviders(processName, uid, flags);
889 return slice != null ? slice.getList() : null;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800890 } catch (RemoteException e) {
891 throw new RuntimeException("Package manager has died", e);
892 }
893 }
894
895 @Override
896 public InstrumentationInfo getInstrumentationInfo(
897 ComponentName className, int flags)
898 throws NameNotFoundException {
899 try {
900 InstrumentationInfo ii = mPM.getInstrumentationInfo(
901 className, flags);
902 if (ii != null) {
903 return ii;
904 }
905 } catch (RemoteException e) {
906 throw new RuntimeException("Package manager has died", e);
907 }
908
909 throw new NameNotFoundException(className.toString());
910 }
911
912 @Override
913 public List<InstrumentationInfo> queryInstrumentation(
914 String targetPackage, int flags) {
915 try {
916 return mPM.queryInstrumentation(targetPackage, flags);
917 } catch (RemoteException e) {
918 throw new RuntimeException("Package manager has died", e);
919 }
920 }
921
Alan Viveretteecd585a2015-04-13 10:32:51 -0700922 @Nullable
923 @Override
924 public Drawable getDrawable(String packageName, @DrawableRes int resId,
925 @Nullable ApplicationInfo appInfo) {
926 final ResourceName name = new ResourceName(packageName, resId);
927 final Drawable cachedIcon = getCachedIcon(name);
928 if (cachedIcon != null) {
929 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800930 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700931
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800932 if (appInfo == null) {
933 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700934 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800935 } catch (NameNotFoundException e) {
936 return null;
937 }
938 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700939
940 if (resId != 0) {
941 try {
942 final Resources r = getResourcesForApplication(appInfo);
943 final Drawable dr = r.getDrawable(resId, null);
944 if (dr != null) {
945 putCachedIcon(name, dr);
946 }
947
948 if (false) {
949 RuntimeException e = new RuntimeException("here");
950 e.fillInStackTrace();
951 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
952 + " from package " + packageName
953 + ": app scale=" + r.getCompatibilityInfo().applicationScale
954 + ", caller scale=" + mContext.getResources()
955 .getCompatibilityInfo().applicationScale,
956 e);
957 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100958 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700959 Log.v(TAG, "Getting drawable 0x"
960 + Integer.toHexString(resId) + " from " + r
961 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100962 }
963 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700964 } catch (NameNotFoundException e) {
965 Log.w("PackageManager", "Failure retrieving resources for "
966 + appInfo.packageName);
967 } catch (Resources.NotFoundException e) {
968 Log.w("PackageManager", "Failure retrieving resources for "
969 + appInfo.packageName + ": " + e.getMessage());
970 } catch (Exception e) {
971 // If an exception was thrown, fall through to return
972 // default icon.
973 Log.w("PackageManager", "Failure retrieving icon 0x"
974 + Integer.toHexString(resId) + " in package "
975 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800976 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800977 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700978
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800979 return null;
980 }
981
982 @Override public Drawable getActivityIcon(ComponentName activityName)
983 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700984 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800985 }
986
987 @Override public Drawable getActivityIcon(Intent intent)
988 throws NameNotFoundException {
989 if (intent.getComponent() != null) {
990 return getActivityIcon(intent.getComponent());
991 }
992
993 ResolveInfo info = resolveActivity(
994 intent, PackageManager.MATCH_DEFAULT_ONLY);
995 if (info != null) {
996 return info.activityInfo.loadIcon(this);
997 }
998
Romain Guy39fe17c2011-11-30 10:34:07 -0800999 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001000 }
1001
1002 @Override public Drawable getDefaultActivityIcon() {
1003 return Resources.getSystem().getDrawable(
1004 com.android.internal.R.drawable.sym_def_app_icon);
1005 }
1006
1007 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1008 return info.loadIcon(this);
1009 }
1010
1011 @Override public Drawable getApplicationIcon(String packageName)
1012 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001013 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001014 }
1015
1016 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001017 public Drawable getActivityBanner(ComponentName activityName)
1018 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001019 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001020 }
1021
1022 @Override
1023 public Drawable getActivityBanner(Intent intent)
1024 throws NameNotFoundException {
1025 if (intent.getComponent() != null) {
1026 return getActivityBanner(intent.getComponent());
1027 }
1028
1029 ResolveInfo info = resolveActivity(
1030 intent, PackageManager.MATCH_DEFAULT_ONLY);
1031 if (info != null) {
1032 return info.activityInfo.loadBanner(this);
1033 }
1034
1035 throw new NameNotFoundException(intent.toUri(0));
1036 }
1037
1038 @Override
1039 public Drawable getApplicationBanner(ApplicationInfo info) {
1040 return info.loadBanner(this);
1041 }
1042
1043 @Override
1044 public Drawable getApplicationBanner(String packageName)
1045 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001046 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001047 }
1048
1049 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001050 public Drawable getActivityLogo(ComponentName activityName)
1051 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001052 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001053 }
1054
1055 @Override
1056 public Drawable getActivityLogo(Intent intent)
1057 throws NameNotFoundException {
1058 if (intent.getComponent() != null) {
1059 return getActivityLogo(intent.getComponent());
1060 }
1061
1062 ResolveInfo info = resolveActivity(
1063 intent, PackageManager.MATCH_DEFAULT_ONLY);
1064 if (info != null) {
1065 return info.activityInfo.loadLogo(this);
1066 }
1067
1068 throw new NameNotFoundException(intent.toUri(0));
1069 }
1070
1071 @Override
1072 public Drawable getApplicationLogo(ApplicationInfo info) {
1073 return info.loadLogo(this);
1074 }
1075
1076 @Override
1077 public Drawable getApplicationLogo(String packageName)
1078 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001079 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001080 }
1081
Svetoslavc7d62f02014-09-04 15:39:54 -07001082 @Override
1083 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1084 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1085 if (badgeResId == 0) {
1086 return icon;
1087 }
1088 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1089 return getBadgedDrawable(icon, badgeIcon, null, true);
1090 }
1091
1092 @Override
1093 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1094 Rect badgeLocation, int badgeDensity) {
1095 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1096 if (badgeDrawable == null) {
1097 return drawable;
1098 }
1099 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1100 }
1101
1102 @Override
1103 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Selim Cineke6ff9462016-01-15 15:07:06 -08001104 return getManagedProfileIconForDensity(user, density,
1105 com.android.internal.R.drawable.ic_corp_badge);
1106 }
1107
1108 @Override
1109 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
1110 return getManagedProfileIconForDensity(user, density,
1111 com.android.internal.R.drawable.ic_corp_badge_no_background);
1112 }
1113
1114 private Drawable getManagedProfileIconForDensity(UserHandle user, int density,
1115 int drawableId) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001116 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1117 if (userInfo != null && userInfo.isManagedProfile()) {
1118 if (density <= 0) {
1119 density = mContext.getResources().getDisplayMetrics().densityDpi;
1120 }
Selim Cineke6ff9462016-01-15 15:07:06 -08001121 return Resources.getSystem().getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001122 }
1123 return null;
1124 }
1125
1126 @Override
1127 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
1128 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1129 if (userInfo != null && userInfo.isManagedProfile()) {
1130 return Resources.getSystem().getString(
1131 com.android.internal.R.string.managed_profile_label_badge, label);
1132 }
1133 return label;
1134 }
1135
Alan Viveretteecd585a2015-04-13 10:32:51 -07001136 @Override
1137 public Resources getResourcesForActivity(ComponentName activityName)
1138 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001139 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001140 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001141 }
1142
Alan Viveretteecd585a2015-04-13 10:32:51 -07001143 @Override
1144 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1145 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001146 if (app.packageName.equals("system")) {
1147 return mContext.mMainThread.getSystemContext().getResources();
1148 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001149 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001150 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001151 sameUid ? app.sourceDir : app.publicSourceDir,
1152 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001153 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1154 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001155 if (r != null) {
1156 return r;
1157 }
1158 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1159 }
1160
Alan Viveretteecd585a2015-04-13 10:32:51 -07001161 @Override
1162 public Resources getResourcesForApplication(String appPackageName)
1163 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001164 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001165 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001166 }
1167
Amith Yamasani98edc952012-09-25 14:09:27 -07001168 /** @hide */
1169 @Override
1170 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1171 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001172 if (userId < 0) {
1173 throw new IllegalArgumentException(
1174 "Call does not support special user #" + userId);
1175 }
1176 if ("system".equals(appPackageName)) {
1177 return mContext.mMainThread.getSystemContext().getResources();
1178 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001179 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001180 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001181 if (ai != null) {
1182 return getResourcesForApplication(ai);
1183 }
1184 } catch (RemoteException e) {
1185 throw new RuntimeException("Package manager has died", e);
1186 }
1187 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1188 }
1189
Jeff Sharkeycd654482016-01-08 17:42:11 -07001190 volatile int mCachedSafeMode = -1;
1191
1192 @Override
1193 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001194 try {
1195 if (mCachedSafeMode < 0) {
1196 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1197 }
1198 return mCachedSafeMode != 0;
1199 } catch (RemoteException e) {
1200 throw new RuntimeException("Package manager has died", e);
1201 }
1202 }
1203
Svetoslavf7c06eb2015-06-10 18:43:22 -07001204 @Override
1205 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1206 synchronized (mPermissionListeners) {
1207 if (mPermissionListeners.get(listener) != null) {
1208 return;
1209 }
1210 OnPermissionsChangeListenerDelegate delegate =
1211 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1212 try {
1213 mPM.addOnPermissionsChangeListener(delegate);
1214 mPermissionListeners.put(listener, delegate);
1215 } catch (RemoteException e) {
1216 throw new RuntimeException("Package manager has died", e);
1217 }
1218 }
1219 }
1220
1221 @Override
1222 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1223 synchronized (mPermissionListeners) {
1224 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1225 if (delegate != null) {
1226 try {
1227 mPM.removeOnPermissionsChangeListener(delegate);
1228 mPermissionListeners.remove(listener);
1229 } catch (RemoteException e) {
1230 throw new RuntimeException("Package manager has died", e);
1231 }
1232 }
1233 }
1234 }
1235
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001236 static void configurationChanged() {
1237 synchronized (sSync) {
1238 sIconCache.clear();
1239 sStringCache.clear();
1240 }
1241 }
1242
1243 ApplicationPackageManager(ContextImpl context,
1244 IPackageManager pm) {
1245 mContext = context;
1246 mPM = pm;
1247 }
1248
Alan Viveretteecd585a2015-04-13 10:32:51 -07001249 @Nullable
1250 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001251 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001252 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001253 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1254 + name + ": " + wr);
1255 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001256 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001257 if (state != null) {
1258 if (DEBUG_ICONS) {
1259 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1260 }
1261 // Note: It's okay here to not use the newDrawable(Resources) variant
1262 // of the API. The ConstantState comes from a drawable that was
1263 // originally created by passing the proper app Resources instance
1264 // which means the state should already contain the proper
1265 // resources specific information (like density.) See
1266 // BitmapDrawable.BitmapState for instance.
1267 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001268 }
1269 // our entry has been purged
1270 sIconCache.remove(name);
1271 }
1272 }
1273 return null;
1274 }
1275
Alan Viveretteecd585a2015-04-13 10:32:51 -07001276 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001277 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001278 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001279 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001280 }
1281 }
1282
Romain Guy39fe17c2011-11-30 10:34:07 -08001283 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001284 boolean immediateGc = false;
1285 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1286 immediateGc = true;
1287 }
1288 if (pkgList != null && (pkgList.length > 0)) {
1289 boolean needCleanup = false;
1290 for (String ssp : pkgList) {
1291 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001292 for (int i=sIconCache.size()-1; i>=0; i--) {
1293 ResourceName nm = sIconCache.keyAt(i);
1294 if (nm.packageName.equals(ssp)) {
1295 //Log.i(TAG, "Removing cached drawable for " + nm);
1296 sIconCache.removeAt(i);
1297 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001298 }
1299 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001300 for (int i=sStringCache.size()-1; i>=0; i--) {
1301 ResourceName nm = sStringCache.keyAt(i);
1302 if (nm.packageName.equals(ssp)) {
1303 //Log.i(TAG, "Removing cached string for " + nm);
1304 sStringCache.removeAt(i);
1305 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001306 }
1307 }
1308 }
1309 }
1310 if (needCleanup || hasPkgInfo) {
1311 if (immediateGc) {
1312 // Schedule an immediate gc.
1313 Runtime.getRuntime().gc();
1314 } else {
1315 ActivityThread.currentActivityThread().scheduleGcIdler();
1316 }
1317 }
1318 }
1319 }
1320
1321 private static final class ResourceName {
1322 final String packageName;
1323 final int iconId;
1324
1325 ResourceName(String _packageName, int _iconId) {
1326 packageName = _packageName;
1327 iconId = _iconId;
1328 }
1329
1330 ResourceName(ApplicationInfo aInfo, int _iconId) {
1331 this(aInfo.packageName, _iconId);
1332 }
1333
1334 ResourceName(ComponentInfo cInfo, int _iconId) {
1335 this(cInfo.applicationInfo.packageName, _iconId);
1336 }
1337
1338 ResourceName(ResolveInfo rInfo, int _iconId) {
1339 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1340 }
1341
1342 @Override
1343 public boolean equals(Object o) {
1344 if (this == o) return true;
1345 if (o == null || getClass() != o.getClass()) return false;
1346
1347 ResourceName that = (ResourceName) o;
1348
1349 if (iconId != that.iconId) return false;
1350 return !(packageName != null ?
1351 !packageName.equals(that.packageName) : that.packageName != null);
1352
1353 }
1354
1355 @Override
1356 public int hashCode() {
1357 int result;
1358 result = packageName.hashCode();
1359 result = 31 * result + iconId;
1360 return result;
1361 }
1362
1363 @Override
1364 public String toString() {
1365 return "{ResourceName " + packageName + " / " + iconId + "}";
1366 }
1367 }
1368
1369 private CharSequence getCachedString(ResourceName name) {
1370 synchronized (sSync) {
1371 WeakReference<CharSequence> wr = sStringCache.get(name);
1372 if (wr != null) { // we have the activity
1373 CharSequence cs = wr.get();
1374 if (cs != null) {
1375 return cs;
1376 }
1377 // our entry has been purged
1378 sStringCache.remove(name);
1379 }
1380 }
1381 return null;
1382 }
1383
1384 private void putCachedString(ResourceName name, CharSequence cs) {
1385 synchronized (sSync) {
1386 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1387 }
1388 }
1389
1390 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001391 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001392 ApplicationInfo appInfo) {
1393 ResourceName name = new ResourceName(packageName, resid);
1394 CharSequence text = getCachedString(name);
1395 if (text != null) {
1396 return text;
1397 }
1398 if (appInfo == null) {
1399 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001400 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001401 } catch (NameNotFoundException e) {
1402 return null;
1403 }
1404 }
1405 try {
1406 Resources r = getResourcesForApplication(appInfo);
1407 text = r.getText(resid);
1408 putCachedString(name, text);
1409 return text;
1410 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001411 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001412 + appInfo.packageName);
1413 } catch (RuntimeException e) {
1414 // If an exception was thrown, fall through to return
1415 // default icon.
1416 Log.w("PackageManager", "Failure retrieving text 0x"
1417 + Integer.toHexString(resid) + " in package "
1418 + packageName, e);
1419 }
1420 return null;
1421 }
1422
1423 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001424 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001425 ApplicationInfo appInfo) {
1426 if (appInfo == null) {
1427 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001428 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001429 } catch (NameNotFoundException e) {
1430 return null;
1431 }
1432 }
1433 try {
1434 Resources r = getResourcesForApplication(appInfo);
1435 return r.getXml(resid);
1436 } catch (RuntimeException e) {
1437 // If an exception was thrown, fall through to return
1438 // default icon.
1439 Log.w("PackageManager", "Failure retrieving xml 0x"
1440 + Integer.toHexString(resid) + " in package "
1441 + packageName, e);
1442 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001443 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001444 + appInfo.packageName);
1445 }
1446 return null;
1447 }
1448
1449 @Override
1450 public CharSequence getApplicationLabel(ApplicationInfo info) {
1451 return info.loadLabel(this);
1452 }
1453
1454 @Override
1455 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1456 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001457 final VerificationParams verificationParams = new VerificationParams(null, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001458 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001459 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001460 installerPackageName, verificationParams, null, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001461 }
1462
1463 @Override
Kenny Root5ab21572011-07-27 11:11:19 -07001464 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1465 int flags, String installerPackageName, Uri verificationURI,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001466 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001467 final VerificationParams verificationParams = new VerificationParams(verificationURI, 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, encryptionParams, mContext.getUserId());
Kenny Root5ab21572011-07-27 11:11:19 -07001471 }
1472
1473 @Override
John Spurlock8a985d22014-02-25 09:40:05 -05001474 public void installPackageWithVerificationAndEncryption(Uri packageURI,
rich cannings706e8ba2012-08-20 13:20:14 -07001475 IPackageInstallObserver observer, int flags, String installerPackageName,
1476 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001477 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001478 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001479 }
1480
Christopher Tatef1977b42014-03-24 16:25:51 -07001481 @Override
1482 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1483 int flags, String installerPackageName) {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001484 installPackageAsUser(packageURI, observer, flags, installerPackageName,
Robin Lee0e27c872015-09-28 14:37:40 +01001485 mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001486 }
1487
1488 @Override
1489 public void installPackageAsUser(Uri packageURI, PackageInstallObserver observer, int flags,
1490 String installerPackageName, int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001491 final VerificationParams verificationParams = new VerificationParams(null, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001492 null, VerificationParams.NO_UID);
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001493 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null,
1494 userId);
Christopher Tatef1977b42014-03-24 16:25:51 -07001495 }
1496
1497 @Override
1498 public void installPackageWithVerification(Uri packageURI,
1499 PackageInstallObserver observer, int flags, String installerPackageName,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001500 Uri verificationURI,
Christopher Tatef1977b42014-03-24 16:25:51 -07001501 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001502 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001503 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001504 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001505 encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001506 }
1507
1508 @Override
1509 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1510 PackageInstallObserver observer, int flags, String installerPackageName,
1511 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001512 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001513 encryptionParams, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001514 }
1515
1516 private void installCommon(Uri packageURI,
1517 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001518 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams,
1519 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001520 if (!"file".equals(packageURI.getScheme())) {
1521 throw new UnsupportedOperationException("Only file:// URIs are supported");
1522 }
1523 if (encryptionParams != null) {
1524 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1525 }
1526
1527 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001528 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001529 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
1530 verificationParams, null, userId);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001531 } catch (RemoteException ignored) {
rich cannings706e8ba2012-08-20 13:20:14 -07001532 }
1533 }
1534
1535 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001536 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001537 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001538 }
1539
1540 @Override
1541 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001542 throws NameNotFoundException {
1543 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001544 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001545 if (res == INSTALL_FAILED_INVALID_URI) {
1546 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1547 }
1548 return res;
1549 } catch (RemoteException e) {
1550 // Should never happen!
1551 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1552 }
1553 }
1554
1555 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001556 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001557 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001558 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001559 } catch (RemoteException e) {
1560 // Should never happen!
1561 }
1562 }
1563
1564 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001565 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1566 long millisecondsToDelay) {
1567 try {
1568 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1569 } catch (RemoteException e) {
1570 // Should never happen!
1571 }
1572 }
1573
1574 @Override
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001575 public void verifyIntentFilter(int id, int verificationCode, List<String> outFailedDomains) {
1576 try {
1577 mPM.verifyIntentFilter(id, verificationCode, outFailedDomains);
1578 } catch (RemoteException e) {
1579 // Should never happen!
1580 }
1581 }
1582
1583 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001584 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001585 try {
1586 return mPM.getIntentVerificationStatus(packageName, userId);
1587 } catch (RemoteException e) {
1588 // Should never happen!
1589 return PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1590 }
1591 }
1592
1593 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001594 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001595 try {
1596 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1597 } catch (RemoteException e) {
1598 // Should never happen!
1599 return false;
1600 }
1601 }
1602
1603 @Override
1604 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1605 try {
1606 return mPM.getIntentFilterVerifications(packageName);
1607 } catch (RemoteException e) {
1608 // Should never happen!
1609 return null;
1610 }
1611 }
1612
1613 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001614 public List<IntentFilter> getAllIntentFilters(String packageName) {
1615 try {
1616 return mPM.getAllIntentFilters(packageName);
1617 } catch (RemoteException e) {
1618 // Should never happen!
1619 return null;
1620 }
1621 }
1622
1623 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001624 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001625 try {
1626 return mPM.getDefaultBrowserPackageName(userId);
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 boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001635 try {
1636 return mPM.setDefaultBrowserPackageName(packageName, userId);
1637 } catch (RemoteException e) {
1638 // Should never happen!
1639 return false;
1640 }
1641 }
1642
1643 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001644 public void setInstallerPackageName(String targetPackage,
1645 String installerPackageName) {
1646 try {
1647 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1648 } catch (RemoteException e) {
1649 // Should never happen!
1650 }
1651 }
1652
1653 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001654 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001655 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001656 return mPM.getInstallerPackageName(packageName);
1657 } catch (RemoteException e) {
1658 // Should never happen!
1659 }
1660 return null;
1661 }
1662
1663 @Override
1664 public int getMoveStatus(int moveId) {
1665 try {
1666 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001667 } catch (RemoteException e) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001668 throw e.rethrowAsRuntimeException();
1669 }
1670 }
1671
1672 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001673 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1674 synchronized (mDelegates) {
1675 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1676 handler.getLooper());
1677 try {
1678 mPM.registerMoveCallback(delegate);
1679 } catch (RemoteException e) {
1680 throw e.rethrowAsRuntimeException();
1681 }
1682 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001683 }
1684 }
1685
1686 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001687 public void unregisterMoveCallback(MoveCallback callback) {
1688 synchronized (mDelegates) {
1689 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1690 final MoveCallbackDelegate delegate = i.next();
1691 if (delegate.mCallback == callback) {
1692 try {
1693 mPM.unregisterMoveCallback(delegate);
1694 } catch (RemoteException e) {
1695 throw e.rethrowAsRuntimeException();
1696 }
1697 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001698 }
1699 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001700 }
1701 }
1702
1703 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001704 public int movePackage(String packageName, VolumeInfo vol) {
1705 try {
1706 final String volumeUuid;
1707 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1708 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1709 } else if (vol.isPrimaryPhysical()) {
1710 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1711 } else {
1712 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1713 }
1714
1715 return mPM.movePackage(packageName, volumeUuid);
1716 } catch (RemoteException e) {
1717 throw e.rethrowAsRuntimeException();
1718 }
1719 }
1720
1721 @Override
1722 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001723 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001724 if (app.isInternal()) {
1725 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1726 } else if (app.isExternalAsec()) {
1727 return storage.getPrimaryPhysicalVolume();
1728 } else {
1729 return storage.findVolumeByUuid(app.volumeUuid);
1730 }
1731 }
1732
1733 @Override
1734 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1735 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1736 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001737 final List<VolumeInfo> vols = storage.getVolumes();
1738 final List<VolumeInfo> candidates = new ArrayList<>();
1739 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001740 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001741 candidates.add(vol);
1742 }
1743 }
1744 return candidates;
1745 }
1746
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001747 private static boolean isPackageCandidateVolume(
1748 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1749 final boolean forceAllowOnExternal = Settings.Global.getInt(
1750 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001751 // Private internal is always an option
1752 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1753 return true;
1754 }
1755
1756 // System apps and apps demanding internal storage can't be moved
1757 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001758 if (app.isSystemApp()) {
1759 return false;
1760 }
1761 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001762 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1763 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001764 return false;
1765 }
1766
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001767 // Gotta be able to write there
1768 if (!vol.isMountedWritable()) {
1769 return false;
1770 }
1771
1772 // Moving into an ASEC on public primary is only option internal
1773 if (vol.isPrimaryPhysical()) {
1774 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001775 }
1776
1777 // Otherwise we can move to any private volume
1778 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1779 }
1780
1781 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001782 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001783 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001784 final String volumeUuid;
1785 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1786 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1787 } else if (vol.isPrimaryPhysical()) {
1788 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1789 } else {
1790 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1791 }
1792
1793 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001794 } catch (RemoteException e) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001795 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001796 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001797 }
1798
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001799 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001800 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1801 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1802 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001803 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001804 }
1805
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001806 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001807 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1808 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1809 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1810 final List<VolumeInfo> vols = storage.getVolumes();
1811 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001812 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1813 storage.getPrimaryStorageUuid()) && currentVol != null) {
1814 // TODO: support moving primary physical to emulated volume
1815 candidates.add(currentVol);
1816 } else {
1817 for (VolumeInfo vol : vols) {
1818 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1819 candidates.add(vol);
1820 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001821 }
1822 }
1823 return candidates;
1824 }
1825
1826 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1827 // Private internal is always an option
1828 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1829 return true;
1830 }
1831
1832 // Gotta be able to write there
1833 if (!vol.isMountedWritable()) {
1834 return false;
1835 }
1836
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001837 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001838 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001839 }
1840
1841 @Override
1842 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001843 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001844 }
1845
1846 @Override
1847 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1848 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001849 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001850 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001851 } catch (RemoteException e) {
1852 // Should never happen!
1853 }
1854 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001855
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001856 @Override
1857 public void clearApplicationUserData(String packageName,
1858 IPackageDataObserver observer) {
1859 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001860 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001861 } catch (RemoteException e) {
1862 // Should never happen!
1863 }
1864 }
1865 @Override
1866 public void deleteApplicationCacheFiles(String packageName,
1867 IPackageDataObserver observer) {
1868 try {
1869 mPM.deleteApplicationCacheFiles(packageName, observer);
1870 } catch (RemoteException e) {
1871 // Should never happen!
1872 }
1873 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001874
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001875 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001876 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1877 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001878 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001879 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001880 } catch (RemoteException e) {
1881 // Should never happen!
1882 }
1883 }
1884
1885 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001886 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001887 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001888 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001889 } catch (RemoteException e) {
1890 // Should never happen!
1891 }
1892 }
1893
1894 @Override
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001895 public boolean setPackageSuspendedAsUser(String packageName, boolean suspended, int userId) {
1896 try {
1897 return mPM.setPackageSuspendedAsUser(packageName, suspended, userId);
1898 } catch (RemoteException e) {
1899 // Should never happen!
1900 }
1901 return false;
1902 }
1903
1904 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001905 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001906 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001907 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001908 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001909 } catch (RemoteException e) {
1910 // Should never happen!
1911 }
1912 }
1913 @Override
1914 public void addPackageToPreferred(String packageName) {
1915 try {
1916 mPM.addPackageToPreferred(packageName);
1917 } catch (RemoteException e) {
1918 // Should never happen!
1919 }
1920 }
1921
1922 @Override
1923 public void removePackageFromPreferred(String packageName) {
1924 try {
1925 mPM.removePackageFromPreferred(packageName);
1926 } catch (RemoteException e) {
1927 // Should never happen!
1928 }
1929 }
1930
1931 @Override
1932 public List<PackageInfo> getPreferredPackages(int flags) {
1933 try {
1934 return mPM.getPreferredPackages(flags);
1935 } catch (RemoteException e) {
1936 // Should never happen!
1937 }
1938 return new ArrayList<PackageInfo>();
1939 }
1940
1941 @Override
1942 public void addPreferredActivity(IntentFilter filter,
1943 int match, ComponentName[] set, ComponentName activity) {
1944 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001945 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001946 } catch (RemoteException e) {
1947 // Should never happen!
1948 }
1949 }
1950
1951 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001952 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07001953 ComponentName[] set, ComponentName activity, int userId) {
1954 try {
1955 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001956 } catch (RemoteException e) {
1957 // Should never happen!
1958 }
1959 }
1960
1961 @Override
1962 public void replacePreferredActivity(IntentFilter filter,
1963 int match, ComponentName[] set, ComponentName activity) {
1964 try {
Robin Lee0e27c872015-09-28 14:37:40 +01001965 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001966 } catch (RemoteException e) {
1967 // Should never happen!
1968 }
1969 }
1970
1971 @Override
1972 public void replacePreferredActivityAsUser(IntentFilter filter,
1973 int match, ComponentName[] set, ComponentName activity,
1974 int userId) {
1975 try {
1976 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001977 } catch (RemoteException e) {
1978 // Should never happen!
1979 }
1980 }
1981
1982 @Override
1983 public void clearPackagePreferredActivities(String packageName) {
1984 try {
1985 mPM.clearPackagePreferredActivities(packageName);
1986 } catch (RemoteException e) {
1987 // Should never happen!
1988 }
1989 }
1990
1991 @Override
1992 public int getPreferredActivities(List<IntentFilter> outFilters,
1993 List<ComponentName> outActivities, String packageName) {
1994 try {
1995 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
1996 } catch (RemoteException e) {
1997 // Should never happen!
1998 }
1999 return 0;
2000 }
2001
2002 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002003 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2004 try {
2005 return mPM.getHomeActivities(outActivities);
2006 } catch (RemoteException e) {
2007 // Should never happen!
2008 }
2009 return null;
2010 }
2011
2012 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002013 public void setComponentEnabledSetting(ComponentName componentName,
2014 int newState, int flags) {
2015 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002016 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002017 } catch (RemoteException e) {
2018 // Should never happen!
2019 }
2020 }
2021
2022 @Override
2023 public int getComponentEnabledSetting(ComponentName componentName) {
2024 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002025 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002026 } catch (RemoteException e) {
2027 // Should never happen!
2028 }
2029 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2030 }
2031
2032 @Override
2033 public void setApplicationEnabledSetting(String packageName,
2034 int newState, int flags) {
2035 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002036 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002037 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002038 } catch (RemoteException e) {
2039 // Should never happen!
2040 }
2041 }
2042
2043 @Override
2044 public int getApplicationEnabledSetting(String packageName) {
2045 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002046 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002047 } catch (RemoteException e) {
2048 // Should never happen!
2049 }
2050 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2051 }
2052
Amith Yamasani655d0e22013-06-12 14:19:10 -07002053 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002054 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002055 UserHandle user) {
2056 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002057 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002058 user.getIdentifier());
2059 } catch (RemoteException re) {
2060 // Should never happen!
2061 }
2062 return false;
2063 }
2064
2065 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002066 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002067 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002068 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Amith Yamasani655d0e22013-06-12 14:19:10 -07002069 } catch (RemoteException re) {
2070 // Should never happen!
2071 }
2072 return false;
2073 }
2074
dcashmanc6f22492014-08-14 09:54:51 -07002075 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002076 @Override
2077 public KeySet getKeySetByAlias(String packageName, String alias) {
2078 Preconditions.checkNotNull(packageName);
2079 Preconditions.checkNotNull(alias);
dcashmanc6f22492014-08-14 09:54:51 -07002080 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002081 try {
dcashmanc6f22492014-08-14 09:54:51 -07002082 ks = mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002083 } catch (RemoteException e) {
2084 return null;
2085 }
dcashmanc6f22492014-08-14 09:54:51 -07002086 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002087 }
2088
dcashmanc6f22492014-08-14 09:54:51 -07002089 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002090 @Override
2091 public KeySet getSigningKeySet(String packageName) {
2092 Preconditions.checkNotNull(packageName);
dcashmanc6f22492014-08-14 09:54:51 -07002093 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002094 try {
dcashmanc6f22492014-08-14 09:54:51 -07002095 ks = mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002096 } catch (RemoteException e) {
2097 return null;
2098 }
dcashmanc6f22492014-08-14 09:54:51 -07002099 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002100 }
2101
dcashmanc6f22492014-08-14 09:54:51 -07002102 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002103 @Override
2104 public boolean isSignedBy(String packageName, KeySet ks) {
2105 Preconditions.checkNotNull(packageName);
2106 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002107 try {
dcashmanc6f22492014-08-14 09:54:51 -07002108 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002109 } catch (RemoteException e) {
2110 return false;
2111 }
2112 }
2113
dcashmanc6f22492014-08-14 09:54:51 -07002114 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002115 @Override
2116 public boolean isSignedByExactly(String packageName, KeySet ks) {
2117 Preconditions.checkNotNull(packageName);
2118 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002119 try {
dcashmanc6f22492014-08-14 09:54:51 -07002120 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002121 } catch (RemoteException e) {
2122 return false;
2123 }
2124 }
2125
Kenny Root0aaa0d92011-09-12 16:42:55 -07002126 /**
2127 * @hide
2128 */
2129 @Override
2130 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2131 try {
2132 return mPM.getVerifierDeviceIdentity();
2133 } catch (RemoteException e) {
2134 // Should never happen!
2135 }
2136 return null;
2137 }
2138
Jeff Hao9f60c082014-10-28 18:51:07 -07002139 /**
2140 * @hide
2141 */
2142 @Override
2143 public boolean isUpgrade() {
2144 try {
2145 return mPM.isUpgrade();
2146 } catch (RemoteException e) {
2147 return false;
2148 }
2149 }
2150
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002151 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002152 public PackageInstaller getPackageInstaller() {
2153 synchronized (mLock) {
2154 if (mInstaller == null) {
2155 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002156 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002157 mContext.getPackageName(), mContext.getUserId());
2158 } catch (RemoteException e) {
2159 throw e.rethrowAsRuntimeException();
2160 }
2161 }
2162 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002163 }
2164 }
2165
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002166 @Override
2167 public boolean isPackageAvailable(String packageName) {
2168 try {
2169 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2170 } catch (RemoteException e) {
2171 throw e.rethrowAsRuntimeException();
2172 }
2173 }
2174
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002175 /**
2176 * @hide
2177 */
2178 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002179 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2180 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002181 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002182 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002183 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002184 } catch (RemoteException e) {
2185 // Should never happen!
2186 }
2187 }
2188
2189 /**
2190 * @hide
2191 */
2192 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002193 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002194 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002195 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002196 } catch (RemoteException e) {
2197 // Should never happen!
2198 }
2199 }
2200
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002201 /**
2202 * @hide
2203 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002204 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002205 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2206 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2207 return dr;
2208 }
2209 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2210 }
2211
2212 /**
2213 * @hide
2214 */
2215 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002216 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002217 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2218 if (bitmap == null) {
2219 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2220 }
2221 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002222 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002223 Drawable dr = null;
2224 if (itemInfo.packageName != null) {
2225 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2226 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002227 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002228 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002229 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002230 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002231 }
2232
2233 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2234 Rect badgeLocation, boolean tryBadgeInPlace) {
2235 final int badgedWidth = drawable.getIntrinsicWidth();
2236 final int badgedHeight = drawable.getIntrinsicHeight();
2237 final boolean canBadgeInPlace = tryBadgeInPlace
2238 && (drawable instanceof BitmapDrawable)
2239 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2240
2241 final Bitmap bitmap;
2242 if (canBadgeInPlace) {
2243 bitmap = ((BitmapDrawable) drawable).getBitmap();
2244 } else {
2245 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2246 }
2247 Canvas canvas = new Canvas(bitmap);
2248
2249 if (!canBadgeInPlace) {
2250 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2251 drawable.draw(canvas);
2252 }
2253
2254 if (badgeLocation != null) {
2255 if (badgeLocation.left < 0 || badgeLocation.top < 0
2256 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2257 throw new IllegalArgumentException("Badge location " + badgeLocation
2258 + " not in badged drawable bounds "
2259 + new Rect(0, 0, badgedWidth, badgedHeight));
2260 }
2261 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2262
2263 canvas.save();
2264 canvas.translate(badgeLocation.left, badgeLocation.top);
2265 badgeDrawable.draw(canvas);
2266 canvas.restore();
2267 } else {
2268 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2269 badgeDrawable.draw(canvas);
2270 }
2271
2272 if (!canBadgeInPlace) {
2273 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2274
2275 if (drawable instanceof BitmapDrawable) {
2276 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2277 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2278 }
2279
2280 return mergedDrawable;
2281 }
2282
2283 return drawable;
2284 }
2285
2286 private int getBadgeResIdForUser(int userHandle) {
2287 // Return the framework-provided badge.
2288 UserInfo userInfo = getUserIfProfile(userHandle);
2289 if (userInfo != null && userInfo.isManagedProfile()) {
2290 return com.android.internal.R.drawable.ic_corp_icon_badge;
2291 }
2292 return 0;
2293 }
2294
2295 private UserInfo getUserIfProfile(int userHandle) {
Robin Lee0e27c872015-09-28 14:37:40 +01002296 List<UserInfo> userProfiles = getUserManager().getProfiles(mContext.getUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07002297 for (UserInfo user : userProfiles) {
2298 if (user.id == userHandle) {
2299 return user;
2300 }
2301 }
2302 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002303 }
2304
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002305 /** {@hide} */
2306 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2307 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002308 private static final int MSG_CREATED = 1;
2309 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002310
2311 final MoveCallback mCallback;
2312 final Handler mHandler;
2313
2314 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2315 mCallback = callback;
2316 mHandler = new Handler(looper, this);
2317 }
2318
2319 @Override
2320 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002321 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002322 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002323 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002324 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002325 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002326 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002327 }
2328 case MSG_STATUS_CHANGED: {
2329 final SomeArgs args = (SomeArgs) msg.obj;
2330 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2331 args.recycle();
2332 return true;
2333 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002334 }
2335 return false;
2336 }
2337
2338 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002339 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002340 final SomeArgs args = SomeArgs.obtain();
2341 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002342 args.arg2 = extras;
2343 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2344 }
2345
2346 @Override
2347 public void onStatusChanged(int moveId, int status, long estMillis) {
2348 final SomeArgs args = SomeArgs.obtain();
2349 args.argi1 = moveId;
2350 args.argi2 = status;
2351 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002352 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002353 }
2354 }
2355
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002356 private final ContextImpl mContext;
2357 private final IPackageManager mPM;
2358
2359 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002360 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2361 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2362 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2363 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002364
2365 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2366 mPermissionListeners = new ArrayMap<>();
2367
2368 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2369 implements Handler.Callback{
2370 private static final int MSG_PERMISSIONS_CHANGED = 1;
2371
2372 private final OnPermissionsChangedListener mListener;
2373 private final Handler mHandler;
2374
2375
2376 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2377 Looper looper) {
2378 mListener = listener;
2379 mHandler = new Handler(looper, this);
2380 }
2381
2382 @Override
2383 public void onPermissionsChanged(int uid) {
2384 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2385 }
2386
2387 @Override
2388 public boolean handleMessage(Message msg) {
2389 switch (msg.what) {
2390 case MSG_PERMISSIONS_CHANGED: {
2391 final int uid = msg.arg1;
2392 mListener.onPermissionsChanged(uid);
2393 return true;
2394 }
2395 }
2396 return false;
2397 }
2398 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002399}