blob: 7e50518cb3e125b87c0db4c1dd16b228441ca370 [file] [log] [blame]
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.DrawableRes;
Alan Viveretteecd585a2015-04-13 10:32:51 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.StringRes;
23import android.annotation.XmlRes;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080024import android.content.ComponentName;
25import android.content.ContentResolver;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.IntentSender;
29import android.content.pm.ActivityInfo;
30import android.content.pm.ApplicationInfo;
31import android.content.pm.ComponentInfo;
Anonymous Cowardceb1b0b2012-04-24 10:35:16 -070032import android.content.pm.ContainerEncryptionParams;
Svet Ganov2acf0632015-11-24 19:10:59 -080033import android.content.pm.EphemeralApplicationInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080034import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070035import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080036import android.content.pm.IPackageDataObserver;
37import android.content.pm.IPackageDeleteObserver;
38import android.content.pm.IPackageInstallObserver;
39import android.content.pm.IPackageManager;
40import android.content.pm.IPackageMoveObserver;
41import android.content.pm.IPackageStatsObserver;
42import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080043import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070044import android.content.pm.KeySet;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080045import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070046import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010047import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080048import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070049import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080050import android.content.pm.PermissionGroupInfo;
51import android.content.pm.PermissionInfo;
52import android.content.pm.ProviderInfo;
53import android.content.pm.ResolveInfo;
54import android.content.pm.ServiceInfo;
Svetoslavc7d62f02014-09-04 15:39:54 -070055import android.content.pm.UserInfo;
rich cannings706e8ba2012-08-20 13:20:14 -070056import android.content.pm.VerificationParams;
Kenny Root0aaa0d92011-09-12 16:42:55 -070057import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080058import android.content.res.Resources;
59import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070060import android.graphics.Bitmap;
61import android.graphics.Canvas;
62import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010063import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080064import android.graphics.drawable.Drawable;
65import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070066import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070067import android.os.Handler;
68import android.os.Looper;
69import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080070import android.os.Process;
71import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070072import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080073import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010074import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070075import android.os.storage.StorageManager;
76import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070077import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070078import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080079import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070080import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070081
82import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070083
84import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070085import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070086import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010087import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080088import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070089
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080090import java.lang.ref.WeakReference;
91import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080092import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070093import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080094import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070095import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070096import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080097
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070098/** @hide */
99public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800100 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800101 private final static boolean DEBUG_ICONS = false;
102
Svet Ganov2acf0632015-11-24 19:10:59 -0800103 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
104
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700105 // Default flags to use with PackageManager when no flags are given.
106 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
107
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700108 private final Object mLock = new Object();
109
110 @GuardedBy("mLock")
111 private UserManager mUserManager;
112 @GuardedBy("mLock")
113 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100114
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700115 @GuardedBy("mDelegates")
116 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
117
Svet Ganovf1b7f202015-07-29 08:33:42 -0700118 @GuardedBy("mLock")
119 private String mPermissionsControllerPackageName;
120
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100121 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700122 synchronized (mLock) {
123 if (mUserManager == null) {
124 mUserManager = UserManager.get(mContext);
125 }
126 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100127 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100128 }
129
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800130 @Override
131 public PackageInfo getPackageInfo(String packageName, int flags)
132 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100133 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
134 }
135
136 @Override
137 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
138 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800139 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100140 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800141 if (pi != null) {
142 return pi;
143 }
144 } catch (RemoteException e) {
145 throw new RuntimeException("Package manager has died", e);
146 }
147
148 throw new NameNotFoundException(packageName);
149 }
150
151 @Override
152 public String[] currentToCanonicalPackageNames(String[] names) {
153 try {
154 return mPM.currentToCanonicalPackageNames(names);
155 } catch (RemoteException e) {
156 throw new RuntimeException("Package manager has died", e);
157 }
158 }
159
160 @Override
161 public String[] canonicalToCurrentPackageNames(String[] names) {
162 try {
163 return mPM.canonicalToCurrentPackageNames(names);
164 } catch (RemoteException e) {
165 throw new RuntimeException("Package manager has died", e);
166 }
167 }
168
169 @Override
170 public Intent getLaunchIntentForPackage(String packageName) {
171 // First see if the package has an INFO activity; the existence of
172 // such an activity is implied to be the desired front-door for the
173 // overall package (such as if it has multiple launcher entries).
174 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
175 intentToResolve.addCategory(Intent.CATEGORY_INFO);
176 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800177 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800178
179 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800180 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800181 // reuse the intent instance
182 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
183 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
184 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800185 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800186 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800187 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800188 return null;
189 }
190 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800191 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800192 intent.setClassName(ris.get(0).activityInfo.packageName,
193 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800194 return intent;
195 }
196
197 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700198 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
199 // Try to find a main leanback_launcher activity.
200 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
201 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
202 intentToResolve.setPackage(packageName);
203 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
204
205 if (ris == null || ris.size() <= 0) {
206 return null;
207 }
208 Intent intent = new Intent(intentToResolve);
209 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
210 intent.setClassName(ris.get(0).activityInfo.packageName,
211 ris.get(0).activityInfo.name);
212 return intent;
213 }
214
215 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700216 public int[] getPackageGids(String packageName) throws NameNotFoundException {
217 return getPackageGids(packageName, 0);
218 }
219
220 @Override
221 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800222 throws NameNotFoundException {
223 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700224 int[] gids = mPM.getPackageGids(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800225 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800226 return gids;
227 }
228 } catch (RemoteException e) {
229 throw new RuntimeException("Package manager has died", e);
230 }
231
232 throw new NameNotFoundException(packageName);
233 }
234
235 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700236 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
237 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
238 }
239
240 @Override
241 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
242 return getPackageUidAsUser(packageName, 0, userId);
243 }
244
245 @Override
246 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800247 throws NameNotFoundException {
248 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700249 int uid = mPM.getPackageUid(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800250 if (uid >= 0) {
251 return uid;
252 }
253 } catch (RemoteException e) {
254 throw new RuntimeException("Package manager has died", e);
255 }
256
257 throw new NameNotFoundException(packageName);
258 }
259
260 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800261 public PermissionInfo getPermissionInfo(String name, int flags)
262 throws NameNotFoundException {
263 try {
264 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
265 if (pi != null) {
266 return pi;
267 }
268 } catch (RemoteException e) {
269 throw new RuntimeException("Package manager has died", e);
270 }
271
272 throw new NameNotFoundException(name);
273 }
274
275 @Override
276 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
277 throws NameNotFoundException {
278 try {
279 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags);
280 if (pi != null) {
281 return pi;
282 }
283 } catch (RemoteException e) {
284 throw new RuntimeException("Package manager has died", e);
285 }
286
287 throw new NameNotFoundException(group);
288 }
289
290 @Override
291 public PermissionGroupInfo getPermissionGroupInfo(String name,
292 int flags) throws NameNotFoundException {
293 try {
294 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
295 if (pgi != null) {
296 return pgi;
297 }
298 } catch (RemoteException e) {
299 throw new RuntimeException("Package manager has died", e);
300 }
301
302 throw new NameNotFoundException(name);
303 }
304
305 @Override
306 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
307 try {
308 return mPM.getAllPermissionGroups(flags);
309 } catch (RemoteException e) {
310 throw new RuntimeException("Package manager has died", e);
311 }
312 }
313
314 @Override
315 public ApplicationInfo getApplicationInfo(String packageName, int flags)
316 throws NameNotFoundException {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700317 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
318 }
319
320 @Override
321 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
322 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800323 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700324 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800325 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100326 // This is a temporary hack. Callers must use
327 // createPackageContext(packageName).getApplicationInfo() to
328 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800329 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800330 }
331 } catch (RemoteException e) {
332 throw new RuntimeException("Package manager has died", e);
333 }
334
335 throw new NameNotFoundException(packageName);
336 }
337
Tao Baic9a02372016-01-12 15:02:24 -0800338 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100339 // If we're dealing with a multi-arch application that has both
340 // 32 and 64 bit shared libraries, we might need to choose the secondary
341 // depending on what the current runtime's instruction set is.
342 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
343 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500344
345 // Get the instruction set that the libraries of secondary Abi is supported.
346 // In presence of a native bridge this might be different than the one secondary Abi used.
347 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
348 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
349 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100350
351 // If the runtimeIsa is the same as the primary isa, then we do nothing.
352 // Everything will be set up correctly because info.nativeLibraryDir will
353 // correspond to the right ISA.
354 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800355 ApplicationInfo modified = new ApplicationInfo(info);
356 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
357 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100358 }
359 }
Tao Baic9a02372016-01-12 15:02:24 -0800360 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100361 }
362
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800363 @Override
364 public ActivityInfo getActivityInfo(ComponentName className, int flags)
365 throws NameNotFoundException {
366 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700367 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800368 if (ai != null) {
369 return ai;
370 }
371 } catch (RemoteException e) {
372 throw new RuntimeException("Package manager has died", e);
373 }
374
375 throw new NameNotFoundException(className.toString());
376 }
377
378 @Override
379 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
380 throws NameNotFoundException {
381 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700382 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800383 if (ai != null) {
384 return ai;
385 }
386 } catch (RemoteException e) {
387 throw new RuntimeException("Package manager has died", e);
388 }
389
390 throw new NameNotFoundException(className.toString());
391 }
392
393 @Override
394 public ServiceInfo getServiceInfo(ComponentName className, int flags)
395 throws NameNotFoundException {
396 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700397 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800398 if (si != null) {
399 return si;
400 }
401 } catch (RemoteException e) {
402 throw new RuntimeException("Package manager has died", e);
403 }
404
405 throw new NameNotFoundException(className.toString());
406 }
407
408 @Override
409 public ProviderInfo getProviderInfo(ComponentName className, int flags)
410 throws NameNotFoundException {
411 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700412 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800413 if (pi != null) {
414 return pi;
415 }
416 } catch (RemoteException e) {
417 throw new RuntimeException("Package manager has died", e);
418 }
419
420 throw new NameNotFoundException(className.toString());
421 }
422
423 @Override
424 public String[] getSystemSharedLibraryNames() {
425 try {
426 return mPM.getSystemSharedLibraryNames();
427 } catch (RemoteException e) {
428 throw new RuntimeException("Package manager has died", e);
429 }
430 }
431
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800432 /** @hide */
433 @Override
434 public @Nullable String getServicesSystemSharedLibraryPackageName() {
435 try {
436 return mPM.getServicesSystemSharedLibraryPackageName();
437 } catch (RemoteException e) {
438 throw new RuntimeException("Package manager has died", e);
439 }
440 }
441
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800442 @Override
443 public FeatureInfo[] getSystemAvailableFeatures() {
444 try {
445 return mPM.getSystemAvailableFeatures();
446 } catch (RemoteException e) {
447 throw new RuntimeException("Package manager has died", e);
448 }
449 }
450
451 @Override
452 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700453 return hasSystemFeature(name, 0);
454 }
455
456 @Override
457 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800458 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700459 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800460 } catch (RemoteException e) {
461 throw new RuntimeException("Package manager has died", e);
462 }
463 }
464
465 @Override
466 public int checkPermission(String permName, String pkgName) {
467 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800468 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800469 } catch (RemoteException e) {
470 throw new RuntimeException("Package manager has died", e);
471 }
472 }
473
474 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700475 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
476 try {
477 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
478 } catch (RemoteException e) {
479 throw new RuntimeException("Package manager has died", e);
480 }
481 }
482
Svet Ganovf1b7f202015-07-29 08:33:42 -0700483 /**
484 * @hide
485 */
486 @Override
487 public String getPermissionControllerPackageName() {
488 synchronized (mLock) {
489 if (mPermissionsControllerPackageName == null) {
490 try {
491 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
492 } catch (RemoteException e) {
493 throw new RuntimeException("Package manager has died", e);
494 }
495 }
496 return mPermissionsControllerPackageName;
497 }
498 }
499
Svet Ganovad3b2972015-07-07 22:49:17 -0700500 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800501 public boolean addPermission(PermissionInfo info) {
502 try {
503 return mPM.addPermission(info);
504 } catch (RemoteException e) {
505 throw new RuntimeException("Package manager has died", e);
506 }
507 }
508
509 @Override
510 public boolean addPermissionAsync(PermissionInfo info) {
511 try {
512 return mPM.addPermissionAsync(info);
513 } catch (RemoteException e) {
514 throw new RuntimeException("Package manager has died", e);
515 }
516 }
517
518 @Override
519 public void removePermission(String name) {
520 try {
521 mPM.removePermission(name);
522 } catch (RemoteException e) {
523 throw new RuntimeException("Package manager has died", e);
524 }
525 }
526
527 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700528 public void grantRuntimePermission(String packageName, String permissionName,
529 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800530 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700531 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800532 } catch (RemoteException e) {
533 throw new RuntimeException("Package manager has died", e);
534 }
535 }
536
537 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700538 public void revokeRuntimePermission(String packageName, String permissionName,
539 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800540 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700541 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
542 } catch (RemoteException e) {
543 throw new RuntimeException("Package manager has died", e);
544 }
545 }
546
547 @Override
548 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
549 try {
550 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
551 } catch (RemoteException e) {
552 throw new RuntimeException("Package manager has died", e);
553 }
554 }
555
556 @Override
557 public void updatePermissionFlags(String permissionName, String packageName,
558 int flagMask, int flagValues, UserHandle user) {
559 try {
560 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
561 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800562 } catch (RemoteException e) {
563 throw new RuntimeException("Package manager has died", e);
564 }
565 }
566
567 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700568 public boolean shouldShowRequestPermissionRationale(String permission) {
569 try {
570 return mPM.shouldShowRequestPermissionRationale(permission,
571 mContext.getPackageName(), mContext.getUserId());
572 } catch (RemoteException e) {
573 throw new RuntimeException("Package manager has died", e);
574 }
575 }
576
577 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800578 public int checkSignatures(String pkg1, String pkg2) {
579 try {
580 return mPM.checkSignatures(pkg1, pkg2);
581 } catch (RemoteException e) {
582 throw new RuntimeException("Package manager has died", e);
583 }
584 }
585
586 @Override
587 public int checkSignatures(int uid1, int uid2) {
588 try {
589 return mPM.checkUidSignatures(uid1, uid2);
590 } catch (RemoteException e) {
591 throw new RuntimeException("Package manager has died", e);
592 }
593 }
594
595 @Override
596 public String[] getPackagesForUid(int uid) {
597 try {
598 return mPM.getPackagesForUid(uid);
599 } catch (RemoteException e) {
600 throw new RuntimeException("Package manager has died", e);
601 }
602 }
603
604 @Override
605 public String getNameForUid(int uid) {
606 try {
607 return mPM.getNameForUid(uid);
608 } catch (RemoteException e) {
609 throw new RuntimeException("Package manager has died", e);
610 }
611 }
612
613 @Override
614 public int getUidForSharedUser(String sharedUserName)
615 throws NameNotFoundException {
616 try {
617 int uid = mPM.getUidForSharedUser(sharedUserName);
618 if(uid != -1) {
619 return uid;
620 }
621 } catch (RemoteException e) {
622 throw new RuntimeException("Package manager has died", e);
623 }
624 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
625 }
626
Kenny Roote6cd0c72011-05-19 12:48:14 -0700627 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800628 @Override
629 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700630 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700631 }
632
633 /** @hide */
634 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700635 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800636 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800637 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
638 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800639 } catch (RemoteException e) {
640 throw new RuntimeException("Package manager has died", e);
641 }
642 }
643
Kenny Roote6cd0c72011-05-19 12:48:14 -0700644 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800645 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800646 public List<PackageInfo> getPackagesHoldingPermissions(
647 String[] permissions, int flags) {
648 final int userId = mContext.getUserId();
649 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800650 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
651 permissions, flags, userId);
652 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800653 } catch (RemoteException e) {
654 throw new RuntimeException("Package manager has died", e);
655 }
656 }
657
658 @SuppressWarnings("unchecked")
659 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800660 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700661 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800662 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800663 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
664 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800665 } catch (RemoteException e) {
666 throw new RuntimeException("Package manager has died", e);
667 }
668 }
669
Svet Ganov2acf0632015-11-24 19:10:59 -0800670 /** @hide */
671 @SuppressWarnings("unchecked")
672 @Override
673 public List<EphemeralApplicationInfo> getEphemeralApplications() {
674 try {
675 ParceledListSlice<EphemeralApplicationInfo> slice =
676 mPM.getEphemeralApplications(mContext.getUserId());
677 if (slice != null) {
678 return slice.getList();
679 }
680 return Collections.emptyList();
681 } catch (RemoteException e) {
682 throw new RuntimeException("Package manager has died", e);
683 }
684 }
685
686 /** @hide */
687 @Override
688 public Drawable getEphemeralApplicationIcon(String packageName) {
689 try {
690 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
691 packageName, mContext.getUserId());
692 if (bitmap != null) {
693 return new BitmapDrawable(null, bitmap);
694 }
695 return null;
696 } catch (RemoteException e) {
697 throw new RuntimeException("Package manager has died", e);
698 }
699 }
700
701 @Override
702 public boolean isEphemeralApplication() {
703 try {
704 return mPM.isEphemeralApplication(
705 mContext.getPackageName(), mContext.getUserId());
706 } catch (RemoteException e) {
707 Log.e(TAG, "System server is dead", e);
708 }
709 return false;
710 }
711
712 @Override
713 public int getEphemeralCookieMaxSizeBytes() {
714 return Settings.Global.getInt(mContext.getContentResolver(),
715 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
716 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
717 }
718
719 @Override
720 public @NonNull byte[] getEphemeralCookie() {
721 try {
722 final byte[] cookie = mPM.getEphemeralApplicationCookie(
723 mContext.getPackageName(), mContext.getUserId());
724 if (cookie != null) {
725 return cookie;
726 }
727 } catch (RemoteException e) {
728 Log.e(TAG, "System server is dead", e);
729 }
730 return EmptyArray.BYTE;
731 }
732
733 @Override
734 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
735 try {
736 return mPM.setEphemeralApplicationCookie(
737 mContext.getPackageName(), cookie, mContext.getUserId());
738 } catch (RemoteException e) {
739 Log.e(TAG, "System server is dead", e);
740 }
741 return false;
742 }
743
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800744 @Override
745 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700746 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700747 }
748
749 @Override
750 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800751 try {
752 return mPM.resolveIntent(
753 intent,
754 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700755 flags,
756 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800757 } catch (RemoteException e) {
758 throw new RuntimeException("Package manager has died", e);
759 }
760 }
761
762 @Override
763 public List<ResolveInfo> queryIntentActivities(Intent intent,
764 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700765 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700766 }
767
768 /** @hide Same as above but for a specific user */
769 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700770 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700771 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800772 try {
773 return mPM.queryIntentActivities(
774 intent,
775 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700776 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700777 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800778 } catch (RemoteException e) {
779 throw new RuntimeException("Package manager has died", e);
780 }
781 }
782
783 @Override
784 public List<ResolveInfo> queryIntentActivityOptions(
785 ComponentName caller, Intent[] specifics, Intent intent,
786 int flags) {
787 final ContentResolver resolver = mContext.getContentResolver();
788
789 String[] specificTypes = null;
790 if (specifics != null) {
791 final int N = specifics.length;
792 for (int i=0; i<N; i++) {
793 Intent sp = specifics[i];
794 if (sp != null) {
795 String t = sp.resolveTypeIfNeeded(resolver);
796 if (t != null) {
797 if (specificTypes == null) {
798 specificTypes = new String[N];
799 }
800 specificTypes[i] = t;
801 }
802 }
803 }
804 }
805
806 try {
807 return mPM.queryIntentActivityOptions(caller, specifics,
808 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700809 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800810 } catch (RemoteException e) {
811 throw new RuntimeException("Package manager has died", e);
812 }
813 }
814
Amith Yamasanif203aee2012-08-29 18:41:53 -0700815 /**
816 * @hide
817 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800818 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700819 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800820 try {
821 return mPM.queryIntentReceivers(
822 intent,
823 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700824 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -0700825 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800826 } catch (RemoteException e) {
827 throw new RuntimeException("Package manager has died", e);
828 }
829 }
830
831 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700832 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700833 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700834 }
835
836 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800837 public ResolveInfo resolveService(Intent intent, int flags) {
838 try {
839 return mPM.resolveService(
840 intent,
841 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700842 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700843 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800844 } catch (RemoteException e) {
845 throw new RuntimeException("Package manager has died", e);
846 }
847 }
848
849 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700850 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800851 try {
852 return mPM.queryIntentServices(
853 intent,
854 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700855 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700856 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800857 } catch (RemoteException e) {
858 throw new RuntimeException("Package manager has died", e);
859 }
860 }
861
862 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700863 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700864 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700865 }
866
867 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700868 public List<ResolveInfo> queryIntentContentProvidersAsUser(
869 Intent intent, int flags, int userId) {
870 try {
871 return mPM.queryIntentContentProviders(intent,
872 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
873 } catch (RemoteException e) {
874 throw new RuntimeException("Package manager has died", e);
875 }
876 }
877
878 @Override
879 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
880 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
881 }
882
883 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100884 public ProviderInfo resolveContentProvider(String name, int flags) {
885 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
886 }
887
888 /** @hide **/
889 @Override
890 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800891 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100892 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800893 } catch (RemoteException e) {
894 throw new RuntimeException("Package manager has died", e);
895 }
896 }
897
898 @Override
899 public List<ProviderInfo> queryContentProviders(String processName,
900 int uid, int flags) {
901 try {
Dianne Hackborn28ec27c2015-08-03 15:28:28 -0700902 ParceledListSlice<ProviderInfo> slice
903 = mPM.queryContentProviders(processName, uid, flags);
904 return slice != null ? slice.getList() : null;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800905 } catch (RemoteException e) {
906 throw new RuntimeException("Package manager has died", e);
907 }
908 }
909
910 @Override
911 public InstrumentationInfo getInstrumentationInfo(
912 ComponentName className, int flags)
913 throws NameNotFoundException {
914 try {
915 InstrumentationInfo ii = mPM.getInstrumentationInfo(
916 className, flags);
917 if (ii != null) {
918 return ii;
919 }
920 } catch (RemoteException e) {
921 throw new RuntimeException("Package manager has died", e);
922 }
923
924 throw new NameNotFoundException(className.toString());
925 }
926
927 @Override
928 public List<InstrumentationInfo> queryInstrumentation(
929 String targetPackage, int flags) {
930 try {
931 return mPM.queryInstrumentation(targetPackage, flags);
932 } catch (RemoteException e) {
933 throw new RuntimeException("Package manager has died", e);
934 }
935 }
936
Alan Viveretteecd585a2015-04-13 10:32:51 -0700937 @Nullable
938 @Override
939 public Drawable getDrawable(String packageName, @DrawableRes int resId,
940 @Nullable ApplicationInfo appInfo) {
941 final ResourceName name = new ResourceName(packageName, resId);
942 final Drawable cachedIcon = getCachedIcon(name);
943 if (cachedIcon != null) {
944 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800945 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700946
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800947 if (appInfo == null) {
948 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700949 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800950 } catch (NameNotFoundException e) {
951 return null;
952 }
953 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700954
955 if (resId != 0) {
956 try {
957 final Resources r = getResourcesForApplication(appInfo);
958 final Drawable dr = r.getDrawable(resId, null);
959 if (dr != null) {
960 putCachedIcon(name, dr);
961 }
962
963 if (false) {
964 RuntimeException e = new RuntimeException("here");
965 e.fillInStackTrace();
966 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
967 + " from package " + packageName
968 + ": app scale=" + r.getCompatibilityInfo().applicationScale
969 + ", caller scale=" + mContext.getResources()
970 .getCompatibilityInfo().applicationScale,
971 e);
972 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100973 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700974 Log.v(TAG, "Getting drawable 0x"
975 + Integer.toHexString(resId) + " from " + r
976 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100977 }
978 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700979 } catch (NameNotFoundException e) {
980 Log.w("PackageManager", "Failure retrieving resources for "
981 + appInfo.packageName);
982 } catch (Resources.NotFoundException e) {
983 Log.w("PackageManager", "Failure retrieving resources for "
984 + appInfo.packageName + ": " + e.getMessage());
985 } catch (Exception e) {
986 // If an exception was thrown, fall through to return
987 // default icon.
988 Log.w("PackageManager", "Failure retrieving icon 0x"
989 + Integer.toHexString(resId) + " in package "
990 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800991 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800992 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700993
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800994 return null;
995 }
996
997 @Override public Drawable getActivityIcon(ComponentName activityName)
998 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700999 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001000 }
1001
1002 @Override public Drawable getActivityIcon(Intent intent)
1003 throws NameNotFoundException {
1004 if (intent.getComponent() != null) {
1005 return getActivityIcon(intent.getComponent());
1006 }
1007
1008 ResolveInfo info = resolveActivity(
1009 intent, PackageManager.MATCH_DEFAULT_ONLY);
1010 if (info != null) {
1011 return info.activityInfo.loadIcon(this);
1012 }
1013
Romain Guy39fe17c2011-11-30 10:34:07 -08001014 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001015 }
1016
1017 @Override public Drawable getDefaultActivityIcon() {
1018 return Resources.getSystem().getDrawable(
1019 com.android.internal.R.drawable.sym_def_app_icon);
1020 }
1021
1022 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1023 return info.loadIcon(this);
1024 }
1025
1026 @Override public Drawable getApplicationIcon(String packageName)
1027 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001028 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001029 }
1030
1031 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001032 public Drawable getActivityBanner(ComponentName activityName)
1033 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001034 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001035 }
1036
1037 @Override
1038 public Drawable getActivityBanner(Intent intent)
1039 throws NameNotFoundException {
1040 if (intent.getComponent() != null) {
1041 return getActivityBanner(intent.getComponent());
1042 }
1043
1044 ResolveInfo info = resolveActivity(
1045 intent, PackageManager.MATCH_DEFAULT_ONLY);
1046 if (info != null) {
1047 return info.activityInfo.loadBanner(this);
1048 }
1049
1050 throw new NameNotFoundException(intent.toUri(0));
1051 }
1052
1053 @Override
1054 public Drawable getApplicationBanner(ApplicationInfo info) {
1055 return info.loadBanner(this);
1056 }
1057
1058 @Override
1059 public Drawable getApplicationBanner(String packageName)
1060 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001061 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001062 }
1063
1064 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001065 public Drawable getActivityLogo(ComponentName activityName)
1066 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001067 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001068 }
1069
1070 @Override
1071 public Drawable getActivityLogo(Intent intent)
1072 throws NameNotFoundException {
1073 if (intent.getComponent() != null) {
1074 return getActivityLogo(intent.getComponent());
1075 }
1076
1077 ResolveInfo info = resolveActivity(
1078 intent, PackageManager.MATCH_DEFAULT_ONLY);
1079 if (info != null) {
1080 return info.activityInfo.loadLogo(this);
1081 }
1082
1083 throw new NameNotFoundException(intent.toUri(0));
1084 }
1085
1086 @Override
1087 public Drawable getApplicationLogo(ApplicationInfo info) {
1088 return info.loadLogo(this);
1089 }
1090
1091 @Override
1092 public Drawable getApplicationLogo(String packageName)
1093 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001094 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001095 }
1096
Svetoslavc7d62f02014-09-04 15:39:54 -07001097 @Override
1098 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1099 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1100 if (badgeResId == 0) {
1101 return icon;
1102 }
1103 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1104 return getBadgedDrawable(icon, badgeIcon, null, true);
1105 }
1106
1107 @Override
1108 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1109 Rect badgeLocation, int badgeDensity) {
1110 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1111 if (badgeDrawable == null) {
1112 return drawable;
1113 }
1114 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1115 }
1116
1117 @Override
1118 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Selim Cineke6ff9462016-01-15 15:07:06 -08001119 return getManagedProfileIconForDensity(user, density,
1120 com.android.internal.R.drawable.ic_corp_badge);
1121 }
1122
1123 @Override
1124 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
1125 return getManagedProfileIconForDensity(user, density,
1126 com.android.internal.R.drawable.ic_corp_badge_no_background);
1127 }
1128
1129 private Drawable getManagedProfileIconForDensity(UserHandle user, int density,
1130 int drawableId) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001131 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1132 if (userInfo != null && userInfo.isManagedProfile()) {
1133 if (density <= 0) {
1134 density = mContext.getResources().getDisplayMetrics().densityDpi;
1135 }
Selim Cineke6ff9462016-01-15 15:07:06 -08001136 return Resources.getSystem().getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001137 }
1138 return null;
1139 }
1140
1141 @Override
1142 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
1143 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1144 if (userInfo != null && userInfo.isManagedProfile()) {
1145 return Resources.getSystem().getString(
1146 com.android.internal.R.string.managed_profile_label_badge, label);
1147 }
1148 return label;
1149 }
1150
Alan Viveretteecd585a2015-04-13 10:32:51 -07001151 @Override
1152 public Resources getResourcesForActivity(ComponentName activityName)
1153 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001154 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001155 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001156 }
1157
Alan Viveretteecd585a2015-04-13 10:32:51 -07001158 @Override
1159 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1160 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001161 if (app.packageName.equals("system")) {
1162 return mContext.mMainThread.getSystemContext().getResources();
1163 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001164 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001165 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001166 sameUid ? app.sourceDir : app.publicSourceDir,
1167 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001168 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1169 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001170 if (r != null) {
1171 return r;
1172 }
1173 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1174 }
1175
Alan Viveretteecd585a2015-04-13 10:32:51 -07001176 @Override
1177 public Resources getResourcesForApplication(String appPackageName)
1178 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001179 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001180 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001181 }
1182
Amith Yamasani98edc952012-09-25 14:09:27 -07001183 /** @hide */
1184 @Override
1185 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1186 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001187 if (userId < 0) {
1188 throw new IllegalArgumentException(
1189 "Call does not support special user #" + userId);
1190 }
1191 if ("system".equals(appPackageName)) {
1192 return mContext.mMainThread.getSystemContext().getResources();
1193 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001194 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001195 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001196 if (ai != null) {
1197 return getResourcesForApplication(ai);
1198 }
1199 } catch (RemoteException e) {
1200 throw new RuntimeException("Package manager has died", e);
1201 }
1202 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1203 }
1204
Jeff Sharkeycd654482016-01-08 17:42:11 -07001205 volatile int mCachedSafeMode = -1;
1206
1207 @Override
1208 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001209 try {
1210 if (mCachedSafeMode < 0) {
1211 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1212 }
1213 return mCachedSafeMode != 0;
1214 } catch (RemoteException e) {
1215 throw new RuntimeException("Package manager has died", e);
1216 }
1217 }
1218
Svetoslavf7c06eb2015-06-10 18:43:22 -07001219 @Override
1220 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1221 synchronized (mPermissionListeners) {
1222 if (mPermissionListeners.get(listener) != null) {
1223 return;
1224 }
1225 OnPermissionsChangeListenerDelegate delegate =
1226 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1227 try {
1228 mPM.addOnPermissionsChangeListener(delegate);
1229 mPermissionListeners.put(listener, delegate);
1230 } catch (RemoteException e) {
1231 throw new RuntimeException("Package manager has died", e);
1232 }
1233 }
1234 }
1235
1236 @Override
1237 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1238 synchronized (mPermissionListeners) {
1239 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1240 if (delegate != null) {
1241 try {
1242 mPM.removeOnPermissionsChangeListener(delegate);
1243 mPermissionListeners.remove(listener);
1244 } catch (RemoteException e) {
1245 throw new RuntimeException("Package manager has died", e);
1246 }
1247 }
1248 }
1249 }
1250
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001251 static void configurationChanged() {
1252 synchronized (sSync) {
1253 sIconCache.clear();
1254 sStringCache.clear();
1255 }
1256 }
1257
1258 ApplicationPackageManager(ContextImpl context,
1259 IPackageManager pm) {
1260 mContext = context;
1261 mPM = pm;
1262 }
1263
Alan Viveretteecd585a2015-04-13 10:32:51 -07001264 @Nullable
1265 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001266 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001267 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001268 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1269 + name + ": " + wr);
1270 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001271 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001272 if (state != null) {
1273 if (DEBUG_ICONS) {
1274 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1275 }
1276 // Note: It's okay here to not use the newDrawable(Resources) variant
1277 // of the API. The ConstantState comes from a drawable that was
1278 // originally created by passing the proper app Resources instance
1279 // which means the state should already contain the proper
1280 // resources specific information (like density.) See
1281 // BitmapDrawable.BitmapState for instance.
1282 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001283 }
1284 // our entry has been purged
1285 sIconCache.remove(name);
1286 }
1287 }
1288 return null;
1289 }
1290
Alan Viveretteecd585a2015-04-13 10:32:51 -07001291 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001292 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001293 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001294 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001295 }
1296 }
1297
Romain Guy39fe17c2011-11-30 10:34:07 -08001298 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001299 boolean immediateGc = false;
1300 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1301 immediateGc = true;
1302 }
1303 if (pkgList != null && (pkgList.length > 0)) {
1304 boolean needCleanup = false;
1305 for (String ssp : pkgList) {
1306 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001307 for (int i=sIconCache.size()-1; i>=0; i--) {
1308 ResourceName nm = sIconCache.keyAt(i);
1309 if (nm.packageName.equals(ssp)) {
1310 //Log.i(TAG, "Removing cached drawable for " + nm);
1311 sIconCache.removeAt(i);
1312 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001313 }
1314 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001315 for (int i=sStringCache.size()-1; i>=0; i--) {
1316 ResourceName nm = sStringCache.keyAt(i);
1317 if (nm.packageName.equals(ssp)) {
1318 //Log.i(TAG, "Removing cached string for " + nm);
1319 sStringCache.removeAt(i);
1320 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001321 }
1322 }
1323 }
1324 }
1325 if (needCleanup || hasPkgInfo) {
1326 if (immediateGc) {
1327 // Schedule an immediate gc.
1328 Runtime.getRuntime().gc();
1329 } else {
1330 ActivityThread.currentActivityThread().scheduleGcIdler();
1331 }
1332 }
1333 }
1334 }
1335
1336 private static final class ResourceName {
1337 final String packageName;
1338 final int iconId;
1339
1340 ResourceName(String _packageName, int _iconId) {
1341 packageName = _packageName;
1342 iconId = _iconId;
1343 }
1344
1345 ResourceName(ApplicationInfo aInfo, int _iconId) {
1346 this(aInfo.packageName, _iconId);
1347 }
1348
1349 ResourceName(ComponentInfo cInfo, int _iconId) {
1350 this(cInfo.applicationInfo.packageName, _iconId);
1351 }
1352
1353 ResourceName(ResolveInfo rInfo, int _iconId) {
1354 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1355 }
1356
1357 @Override
1358 public boolean equals(Object o) {
1359 if (this == o) return true;
1360 if (o == null || getClass() != o.getClass()) return false;
1361
1362 ResourceName that = (ResourceName) o;
1363
1364 if (iconId != that.iconId) return false;
1365 return !(packageName != null ?
1366 !packageName.equals(that.packageName) : that.packageName != null);
1367
1368 }
1369
1370 @Override
1371 public int hashCode() {
1372 int result;
1373 result = packageName.hashCode();
1374 result = 31 * result + iconId;
1375 return result;
1376 }
1377
1378 @Override
1379 public String toString() {
1380 return "{ResourceName " + packageName + " / " + iconId + "}";
1381 }
1382 }
1383
1384 private CharSequence getCachedString(ResourceName name) {
1385 synchronized (sSync) {
1386 WeakReference<CharSequence> wr = sStringCache.get(name);
1387 if (wr != null) { // we have the activity
1388 CharSequence cs = wr.get();
1389 if (cs != null) {
1390 return cs;
1391 }
1392 // our entry has been purged
1393 sStringCache.remove(name);
1394 }
1395 }
1396 return null;
1397 }
1398
1399 private void putCachedString(ResourceName name, CharSequence cs) {
1400 synchronized (sSync) {
1401 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1402 }
1403 }
1404
1405 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001406 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001407 ApplicationInfo appInfo) {
1408 ResourceName name = new ResourceName(packageName, resid);
1409 CharSequence text = getCachedString(name);
1410 if (text != null) {
1411 return text;
1412 }
1413 if (appInfo == null) {
1414 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001415 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001416 } catch (NameNotFoundException e) {
1417 return null;
1418 }
1419 }
1420 try {
1421 Resources r = getResourcesForApplication(appInfo);
1422 text = r.getText(resid);
1423 putCachedString(name, text);
1424 return text;
1425 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001426 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001427 + appInfo.packageName);
1428 } catch (RuntimeException e) {
1429 // If an exception was thrown, fall through to return
1430 // default icon.
1431 Log.w("PackageManager", "Failure retrieving text 0x"
1432 + Integer.toHexString(resid) + " in package "
1433 + packageName, e);
1434 }
1435 return null;
1436 }
1437
1438 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001439 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001440 ApplicationInfo appInfo) {
1441 if (appInfo == null) {
1442 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001443 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001444 } catch (NameNotFoundException e) {
1445 return null;
1446 }
1447 }
1448 try {
1449 Resources r = getResourcesForApplication(appInfo);
1450 return r.getXml(resid);
1451 } catch (RuntimeException e) {
1452 // If an exception was thrown, fall through to return
1453 // default icon.
1454 Log.w("PackageManager", "Failure retrieving xml 0x"
1455 + Integer.toHexString(resid) + " in package "
1456 + packageName, e);
1457 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001458 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001459 + appInfo.packageName);
1460 }
1461 return null;
1462 }
1463
1464 @Override
1465 public CharSequence getApplicationLabel(ApplicationInfo info) {
1466 return info.loadLabel(this);
1467 }
1468
1469 @Override
1470 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1471 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001472 final VerificationParams verificationParams = new VerificationParams(null, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001473 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001474 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001475 installerPackageName, verificationParams, null, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001476 }
1477
1478 @Override
Kenny Root5ab21572011-07-27 11:11:19 -07001479 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1480 int flags, String installerPackageName, Uri verificationURI,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001481 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001482 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001483 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001484 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001485 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Kenny Root5ab21572011-07-27 11:11:19 -07001486 }
1487
1488 @Override
John Spurlock8a985d22014-02-25 09:40:05 -05001489 public void installPackageWithVerificationAndEncryption(Uri packageURI,
rich cannings706e8ba2012-08-20 13:20:14 -07001490 IPackageInstallObserver observer, int flags, String installerPackageName,
1491 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001492 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001493 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001494 }
1495
Christopher Tatef1977b42014-03-24 16:25:51 -07001496 @Override
1497 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1498 int flags, String installerPackageName) {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001499 installPackageAsUser(packageURI, observer, flags, installerPackageName,
Robin Lee0e27c872015-09-28 14:37:40 +01001500 mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001501 }
1502
1503 @Override
1504 public void installPackageAsUser(Uri packageURI, PackageInstallObserver observer, int flags,
1505 String installerPackageName, int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001506 final VerificationParams verificationParams = new VerificationParams(null, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001507 null, VerificationParams.NO_UID);
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001508 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null,
1509 userId);
Christopher Tatef1977b42014-03-24 16:25:51 -07001510 }
1511
1512 @Override
1513 public void installPackageWithVerification(Uri packageURI,
1514 PackageInstallObserver observer, int flags, String installerPackageName,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001515 Uri verificationURI,
Christopher Tatef1977b42014-03-24 16:25:51 -07001516 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001517 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
Alex Klyubin31ffb442015-12-21 11:32:53 -08001518 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001519 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001520 encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001521 }
1522
1523 @Override
1524 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1525 PackageInstallObserver observer, int flags, String installerPackageName,
1526 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001527 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001528 encryptionParams, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001529 }
1530
1531 private void installCommon(Uri packageURI,
1532 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001533 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams,
1534 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001535 if (!"file".equals(packageURI.getScheme())) {
1536 throw new UnsupportedOperationException("Only file:// URIs are supported");
1537 }
1538 if (encryptionParams != null) {
1539 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1540 }
1541
1542 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001543 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001544 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
1545 verificationParams, null, userId);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001546 } catch (RemoteException ignored) {
rich cannings706e8ba2012-08-20 13:20:14 -07001547 }
1548 }
1549
1550 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001551 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001552 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001553 }
1554
1555 @Override
1556 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001557 throws NameNotFoundException {
1558 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001559 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001560 if (res == INSTALL_FAILED_INVALID_URI) {
1561 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1562 }
1563 return res;
1564 } catch (RemoteException e) {
1565 // Should never happen!
1566 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1567 }
1568 }
1569
1570 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001571 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001572 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001573 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001574 } catch (RemoteException e) {
1575 // Should never happen!
1576 }
1577 }
1578
1579 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001580 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1581 long millisecondsToDelay) {
1582 try {
1583 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1584 } catch (RemoteException e) {
1585 // Should never happen!
1586 }
1587 }
1588
1589 @Override
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001590 public void verifyIntentFilter(int id, int verificationCode, List<String> outFailedDomains) {
1591 try {
1592 mPM.verifyIntentFilter(id, verificationCode, outFailedDomains);
1593 } catch (RemoteException e) {
1594 // Should never happen!
1595 }
1596 }
1597
1598 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001599 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001600 try {
1601 return mPM.getIntentVerificationStatus(packageName, userId);
1602 } catch (RemoteException e) {
1603 // Should never happen!
1604 return PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1605 }
1606 }
1607
1608 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001609 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001610 try {
1611 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1612 } catch (RemoteException e) {
1613 // Should never happen!
1614 return false;
1615 }
1616 }
1617
1618 @Override
1619 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1620 try {
1621 return mPM.getIntentFilterVerifications(packageName);
1622 } catch (RemoteException e) {
1623 // Should never happen!
1624 return null;
1625 }
1626 }
1627
1628 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001629 public List<IntentFilter> getAllIntentFilters(String packageName) {
1630 try {
1631 return mPM.getAllIntentFilters(packageName);
1632 } catch (RemoteException e) {
1633 // Should never happen!
1634 return null;
1635 }
1636 }
1637
1638 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001639 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001640 try {
1641 return mPM.getDefaultBrowserPackageName(userId);
1642 } catch (RemoteException e) {
1643 // Should never happen!
1644 return null;
1645 }
1646 }
1647
1648 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001649 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001650 try {
1651 return mPM.setDefaultBrowserPackageName(packageName, userId);
1652 } catch (RemoteException e) {
1653 // Should never happen!
1654 return false;
1655 }
1656 }
1657
1658 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001659 public void setInstallerPackageName(String targetPackage,
1660 String installerPackageName) {
1661 try {
1662 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1663 } catch (RemoteException e) {
1664 // Should never happen!
1665 }
1666 }
1667
1668 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001669 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001670 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001671 return mPM.getInstallerPackageName(packageName);
1672 } catch (RemoteException e) {
1673 // Should never happen!
1674 }
1675 return null;
1676 }
1677
1678 @Override
1679 public int getMoveStatus(int moveId) {
1680 try {
1681 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001682 } catch (RemoteException e) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001683 throw e.rethrowAsRuntimeException();
1684 }
1685 }
1686
1687 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001688 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1689 synchronized (mDelegates) {
1690 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1691 handler.getLooper());
1692 try {
1693 mPM.registerMoveCallback(delegate);
1694 } catch (RemoteException e) {
1695 throw e.rethrowAsRuntimeException();
1696 }
1697 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001698 }
1699 }
1700
1701 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001702 public void unregisterMoveCallback(MoveCallback callback) {
1703 synchronized (mDelegates) {
1704 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1705 final MoveCallbackDelegate delegate = i.next();
1706 if (delegate.mCallback == callback) {
1707 try {
1708 mPM.unregisterMoveCallback(delegate);
1709 } catch (RemoteException e) {
1710 throw e.rethrowAsRuntimeException();
1711 }
1712 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001713 }
1714 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001715 }
1716 }
1717
1718 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001719 public int movePackage(String packageName, VolumeInfo vol) {
1720 try {
1721 final String volumeUuid;
1722 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1723 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1724 } else if (vol.isPrimaryPhysical()) {
1725 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1726 } else {
1727 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1728 }
1729
1730 return mPM.movePackage(packageName, volumeUuid);
1731 } catch (RemoteException e) {
1732 throw e.rethrowAsRuntimeException();
1733 }
1734 }
1735
1736 @Override
1737 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001738 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001739 if (app.isInternal()) {
1740 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1741 } else if (app.isExternalAsec()) {
1742 return storage.getPrimaryPhysicalVolume();
1743 } else {
1744 return storage.findVolumeByUuid(app.volumeUuid);
1745 }
1746 }
1747
1748 @Override
1749 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1750 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1751 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001752 final List<VolumeInfo> vols = storage.getVolumes();
1753 final List<VolumeInfo> candidates = new ArrayList<>();
1754 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001755 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001756 candidates.add(vol);
1757 }
1758 }
1759 return candidates;
1760 }
1761
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001762 private static boolean isPackageCandidateVolume(
1763 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1764 final boolean forceAllowOnExternal = Settings.Global.getInt(
1765 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001766 // Private internal is always an option
1767 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1768 return true;
1769 }
1770
1771 // System apps and apps demanding internal storage can't be moved
1772 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001773 if (app.isSystemApp()) {
1774 return false;
1775 }
1776 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001777 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1778 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001779 return false;
1780 }
1781
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001782 // Gotta be able to write there
1783 if (!vol.isMountedWritable()) {
1784 return false;
1785 }
1786
1787 // Moving into an ASEC on public primary is only option internal
1788 if (vol.isPrimaryPhysical()) {
1789 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001790 }
1791
1792 // Otherwise we can move to any private volume
1793 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1794 }
1795
1796 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001797 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001798 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001799 final String volumeUuid;
1800 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1801 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1802 } else if (vol.isPrimaryPhysical()) {
1803 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1804 } else {
1805 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1806 }
1807
1808 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001809 } catch (RemoteException e) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001810 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001811 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001812 }
1813
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001814 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001815 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1816 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1817 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001818 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001819 }
1820
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001821 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001822 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1823 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1824 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1825 final List<VolumeInfo> vols = storage.getVolumes();
1826 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001827 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1828 storage.getPrimaryStorageUuid()) && currentVol != null) {
1829 // TODO: support moving primary physical to emulated volume
1830 candidates.add(currentVol);
1831 } else {
1832 for (VolumeInfo vol : vols) {
1833 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1834 candidates.add(vol);
1835 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001836 }
1837 }
1838 return candidates;
1839 }
1840
1841 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1842 // Private internal is always an option
1843 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1844 return true;
1845 }
1846
1847 // Gotta be able to write there
1848 if (!vol.isMountedWritable()) {
1849 return false;
1850 }
1851
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001852 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001853 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001854 }
1855
1856 @Override
1857 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001858 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001859 }
1860
1861 @Override
1862 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1863 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001864 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001865 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001866 } catch (RemoteException e) {
1867 // Should never happen!
1868 }
1869 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001870
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001871 @Override
1872 public void clearApplicationUserData(String packageName,
1873 IPackageDataObserver observer) {
1874 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001875 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001876 } catch (RemoteException e) {
1877 // Should never happen!
1878 }
1879 }
1880 @Override
1881 public void deleteApplicationCacheFiles(String packageName,
1882 IPackageDataObserver observer) {
1883 try {
1884 mPM.deleteApplicationCacheFiles(packageName, observer);
1885 } catch (RemoteException e) {
1886 // Should never happen!
1887 }
1888 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001889
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001890 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001891 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1892 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001893 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001894 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001895 } catch (RemoteException e) {
1896 // Should never happen!
1897 }
1898 }
1899
1900 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001901 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001902 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001903 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001904 } catch (RemoteException e) {
1905 // Should never happen!
1906 }
1907 }
1908
1909 @Override
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001910 public boolean setPackageSuspendedAsUser(String packageName, boolean suspended, int userId) {
1911 try {
1912 return mPM.setPackageSuspendedAsUser(packageName, suspended, userId);
1913 } catch (RemoteException e) {
1914 // Should never happen!
1915 }
1916 return false;
1917 }
1918
1919 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001920 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001921 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001922 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001923 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001924 } catch (RemoteException e) {
1925 // Should never happen!
1926 }
1927 }
1928 @Override
1929 public void addPackageToPreferred(String packageName) {
1930 try {
1931 mPM.addPackageToPreferred(packageName);
1932 } catch (RemoteException e) {
1933 // Should never happen!
1934 }
1935 }
1936
1937 @Override
1938 public void removePackageFromPreferred(String packageName) {
1939 try {
1940 mPM.removePackageFromPreferred(packageName);
1941 } catch (RemoteException e) {
1942 // Should never happen!
1943 }
1944 }
1945
1946 @Override
1947 public List<PackageInfo> getPreferredPackages(int flags) {
1948 try {
1949 return mPM.getPreferredPackages(flags);
1950 } catch (RemoteException e) {
1951 // Should never happen!
1952 }
1953 return new ArrayList<PackageInfo>();
1954 }
1955
1956 @Override
1957 public void addPreferredActivity(IntentFilter filter,
1958 int match, ComponentName[] set, ComponentName activity) {
1959 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001960 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001961 } catch (RemoteException e) {
1962 // Should never happen!
1963 }
1964 }
1965
1966 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001967 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07001968 ComponentName[] set, ComponentName activity, int userId) {
1969 try {
1970 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001971 } catch (RemoteException e) {
1972 // Should never happen!
1973 }
1974 }
1975
1976 @Override
1977 public void replacePreferredActivity(IntentFilter filter,
1978 int match, ComponentName[] set, ComponentName activity) {
1979 try {
Robin Lee0e27c872015-09-28 14:37:40 +01001980 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001981 } catch (RemoteException e) {
1982 // Should never happen!
1983 }
1984 }
1985
1986 @Override
1987 public void replacePreferredActivityAsUser(IntentFilter filter,
1988 int match, ComponentName[] set, ComponentName activity,
1989 int userId) {
1990 try {
1991 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001992 } catch (RemoteException e) {
1993 // Should never happen!
1994 }
1995 }
1996
1997 @Override
1998 public void clearPackagePreferredActivities(String packageName) {
1999 try {
2000 mPM.clearPackagePreferredActivities(packageName);
2001 } catch (RemoteException e) {
2002 // Should never happen!
2003 }
2004 }
2005
2006 @Override
2007 public int getPreferredActivities(List<IntentFilter> outFilters,
2008 List<ComponentName> outActivities, String packageName) {
2009 try {
2010 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2011 } catch (RemoteException e) {
2012 // Should never happen!
2013 }
2014 return 0;
2015 }
2016
2017 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002018 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2019 try {
2020 return mPM.getHomeActivities(outActivities);
2021 } catch (RemoteException e) {
2022 // Should never happen!
2023 }
2024 return null;
2025 }
2026
2027 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002028 public void setComponentEnabledSetting(ComponentName componentName,
2029 int newState, int flags) {
2030 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002031 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002032 } catch (RemoteException e) {
2033 // Should never happen!
2034 }
2035 }
2036
2037 @Override
2038 public int getComponentEnabledSetting(ComponentName componentName) {
2039 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002040 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002041 } catch (RemoteException e) {
2042 // Should never happen!
2043 }
2044 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2045 }
2046
2047 @Override
2048 public void setApplicationEnabledSetting(String packageName,
2049 int newState, int flags) {
2050 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002051 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002052 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002053 } catch (RemoteException e) {
2054 // Should never happen!
2055 }
2056 }
2057
2058 @Override
2059 public int getApplicationEnabledSetting(String packageName) {
2060 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002061 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002062 } catch (RemoteException e) {
2063 // Should never happen!
2064 }
2065 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
2066 }
2067
Amith Yamasani655d0e22013-06-12 14:19:10 -07002068 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002069 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002070 UserHandle user) {
2071 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002072 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002073 user.getIdentifier());
2074 } catch (RemoteException re) {
2075 // Should never happen!
2076 }
2077 return false;
2078 }
2079
2080 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002081 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002082 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002083 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Amith Yamasani655d0e22013-06-12 14:19:10 -07002084 } catch (RemoteException re) {
2085 // Should never happen!
2086 }
2087 return false;
2088 }
2089
dcashmanc6f22492014-08-14 09:54:51 -07002090 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002091 @Override
2092 public KeySet getKeySetByAlias(String packageName, String alias) {
2093 Preconditions.checkNotNull(packageName);
2094 Preconditions.checkNotNull(alias);
dcashmanc6f22492014-08-14 09:54:51 -07002095 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002096 try {
dcashmanc6f22492014-08-14 09:54:51 -07002097 ks = mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002098 } catch (RemoteException e) {
2099 return null;
2100 }
dcashmanc6f22492014-08-14 09:54:51 -07002101 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002102 }
2103
dcashmanc6f22492014-08-14 09:54:51 -07002104 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002105 @Override
2106 public KeySet getSigningKeySet(String packageName) {
2107 Preconditions.checkNotNull(packageName);
dcashmanc6f22492014-08-14 09:54:51 -07002108 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07002109 try {
dcashmanc6f22492014-08-14 09:54:51 -07002110 ks = mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002111 } catch (RemoteException e) {
2112 return null;
2113 }
dcashmanc6f22492014-08-14 09:54:51 -07002114 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07002115 }
2116
dcashmanc6f22492014-08-14 09:54:51 -07002117 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002118 @Override
2119 public boolean isSignedBy(String packageName, KeySet ks) {
2120 Preconditions.checkNotNull(packageName);
2121 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002122 try {
dcashmanc6f22492014-08-14 09:54:51 -07002123 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002124 } catch (RemoteException e) {
2125 return false;
2126 }
2127 }
2128
dcashmanc6f22492014-08-14 09:54:51 -07002129 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002130 @Override
2131 public boolean isSignedByExactly(String packageName, KeySet ks) {
2132 Preconditions.checkNotNull(packageName);
2133 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002134 try {
dcashmanc6f22492014-08-14 09:54:51 -07002135 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002136 } catch (RemoteException e) {
2137 return false;
2138 }
2139 }
2140
Kenny Root0aaa0d92011-09-12 16:42:55 -07002141 /**
2142 * @hide
2143 */
2144 @Override
2145 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2146 try {
2147 return mPM.getVerifierDeviceIdentity();
2148 } catch (RemoteException e) {
2149 // Should never happen!
2150 }
2151 return null;
2152 }
2153
Jeff Hao9f60c082014-10-28 18:51:07 -07002154 /**
2155 * @hide
2156 */
2157 @Override
2158 public boolean isUpgrade() {
2159 try {
2160 return mPM.isUpgrade();
2161 } catch (RemoteException e) {
2162 return false;
2163 }
2164 }
2165
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002166 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002167 public PackageInstaller getPackageInstaller() {
2168 synchronized (mLock) {
2169 if (mInstaller == null) {
2170 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002171 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002172 mContext.getPackageName(), mContext.getUserId());
2173 } catch (RemoteException e) {
2174 throw e.rethrowAsRuntimeException();
2175 }
2176 }
2177 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002178 }
2179 }
2180
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002181 @Override
2182 public boolean isPackageAvailable(String packageName) {
2183 try {
2184 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2185 } catch (RemoteException e) {
2186 throw e.rethrowAsRuntimeException();
2187 }
2188 }
2189
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002190 /**
2191 * @hide
2192 */
2193 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002194 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2195 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002196 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002197 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002198 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002199 } catch (RemoteException e) {
2200 // Should never happen!
2201 }
2202 }
2203
2204 /**
2205 * @hide
2206 */
2207 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002208 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002209 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002210 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002211 } catch (RemoteException e) {
2212 // Should never happen!
2213 }
2214 }
2215
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002216 /**
2217 * @hide
2218 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002219 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002220 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2221 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2222 return dr;
2223 }
2224 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2225 }
2226
2227 /**
2228 * @hide
2229 */
2230 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002231 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002232 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2233 if (bitmap == null) {
2234 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2235 }
2236 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002237 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002238 Drawable dr = null;
2239 if (itemInfo.packageName != null) {
2240 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2241 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002242 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002243 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002244 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002245 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002246 }
2247
2248 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2249 Rect badgeLocation, boolean tryBadgeInPlace) {
2250 final int badgedWidth = drawable.getIntrinsicWidth();
2251 final int badgedHeight = drawable.getIntrinsicHeight();
2252 final boolean canBadgeInPlace = tryBadgeInPlace
2253 && (drawable instanceof BitmapDrawable)
2254 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2255
2256 final Bitmap bitmap;
2257 if (canBadgeInPlace) {
2258 bitmap = ((BitmapDrawable) drawable).getBitmap();
2259 } else {
2260 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2261 }
2262 Canvas canvas = new Canvas(bitmap);
2263
2264 if (!canBadgeInPlace) {
2265 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2266 drawable.draw(canvas);
2267 }
2268
2269 if (badgeLocation != null) {
2270 if (badgeLocation.left < 0 || badgeLocation.top < 0
2271 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2272 throw new IllegalArgumentException("Badge location " + badgeLocation
2273 + " not in badged drawable bounds "
2274 + new Rect(0, 0, badgedWidth, badgedHeight));
2275 }
2276 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2277
2278 canvas.save();
2279 canvas.translate(badgeLocation.left, badgeLocation.top);
2280 badgeDrawable.draw(canvas);
2281 canvas.restore();
2282 } else {
2283 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2284 badgeDrawable.draw(canvas);
2285 }
2286
2287 if (!canBadgeInPlace) {
2288 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2289
2290 if (drawable instanceof BitmapDrawable) {
2291 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2292 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2293 }
2294
2295 return mergedDrawable;
2296 }
2297
2298 return drawable;
2299 }
2300
2301 private int getBadgeResIdForUser(int userHandle) {
2302 // Return the framework-provided badge.
2303 UserInfo userInfo = getUserIfProfile(userHandle);
2304 if (userInfo != null && userInfo.isManagedProfile()) {
2305 return com.android.internal.R.drawable.ic_corp_icon_badge;
2306 }
2307 return 0;
2308 }
2309
2310 private UserInfo getUserIfProfile(int userHandle) {
Robin Lee0e27c872015-09-28 14:37:40 +01002311 List<UserInfo> userProfiles = getUserManager().getProfiles(mContext.getUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07002312 for (UserInfo user : userProfiles) {
2313 if (user.id == userHandle) {
2314 return user;
2315 }
2316 }
2317 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002318 }
2319
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002320 /** {@hide} */
2321 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2322 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002323 private static final int MSG_CREATED = 1;
2324 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002325
2326 final MoveCallback mCallback;
2327 final Handler mHandler;
2328
2329 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2330 mCallback = callback;
2331 mHandler = new Handler(looper, this);
2332 }
2333
2334 @Override
2335 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002336 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002337 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002338 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002339 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002340 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002341 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002342 }
2343 case MSG_STATUS_CHANGED: {
2344 final SomeArgs args = (SomeArgs) msg.obj;
2345 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2346 args.recycle();
2347 return true;
2348 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002349 }
2350 return false;
2351 }
2352
2353 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002354 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002355 final SomeArgs args = SomeArgs.obtain();
2356 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002357 args.arg2 = extras;
2358 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2359 }
2360
2361 @Override
2362 public void onStatusChanged(int moveId, int status, long estMillis) {
2363 final SomeArgs args = SomeArgs.obtain();
2364 args.argi1 = moveId;
2365 args.argi2 = status;
2366 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002367 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002368 }
2369 }
2370
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002371 private final ContextImpl mContext;
2372 private final IPackageManager mPM;
2373
2374 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002375 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2376 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2377 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2378 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002379
2380 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2381 mPermissionListeners = new ArrayMap<>();
2382
2383 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2384 implements Handler.Callback{
2385 private static final int MSG_PERMISSIONS_CHANGED = 1;
2386
2387 private final OnPermissionsChangedListener mListener;
2388 private final Handler mHandler;
2389
2390
2391 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2392 Looper looper) {
2393 mListener = listener;
2394 mHandler = new Handler(looper, this);
2395 }
2396
2397 @Override
2398 public void onPermissionsChanged(int uid) {
2399 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2400 }
2401
2402 @Override
2403 public boolean handleMessage(Message msg) {
2404 switch (msg.what) {
2405 case MSG_PERMISSIONS_CHANGED: {
2406 final int uid = msg.arg1;
2407 mListener.onPermissionsChanged(uid);
2408 return true;
2409 }
2410 }
2411 return false;
2412 }
2413 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002414}