blob: bf56f251325749f3e5d332d137dd1676f8041999 [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;
Makoto Onukif34db0a2016-02-17 11:17:15 -080024import android.app.admin.DevicePolicyManager;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080025import android.content.ComponentName;
26import android.content.ContentResolver;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.IntentSender;
30import android.content.pm.ActivityInfo;
31import android.content.pm.ApplicationInfo;
32import android.content.pm.ComponentInfo;
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;
Kenny Root0aaa0d92011-09-12 16:42:55 -070056import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080057import android.content.res.Resources;
58import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070059import android.graphics.Bitmap;
60import android.graphics.Canvas;
61import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010062import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080063import android.graphics.drawable.Drawable;
64import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070065import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070066import android.os.Handler;
67import android.os.Looper;
68import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080069import android.os.Process;
70import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070071import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080072import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010073import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070074import android.os.storage.StorageManager;
75import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070076import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070077import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080078import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070079import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070080
81import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070082
83import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070084import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070085import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010086import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080087import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070088
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080089import java.lang.ref.WeakReference;
90import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080091import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070092import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080093import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070094import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070095import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080096
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070097/** @hide */
98public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080099 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800100 private final static boolean DEBUG_ICONS = false;
101
Svet Ganov2acf0632015-11-24 19:10:59 -0800102 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
103
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700104 // Default flags to use with PackageManager when no flags are given.
105 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
106
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700107 private final Object mLock = new Object();
108
109 @GuardedBy("mLock")
110 private UserManager mUserManager;
111 @GuardedBy("mLock")
112 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100113
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700114 @GuardedBy("mDelegates")
115 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
116
Svet Ganovf1b7f202015-07-29 08:33:42 -0700117 @GuardedBy("mLock")
118 private String mPermissionsControllerPackageName;
119
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100120 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700121 synchronized (mLock) {
122 if (mUserManager == null) {
123 mUserManager = UserManager.get(mContext);
124 }
125 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100126 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100127 }
128
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800129 @Override
130 public PackageInfo getPackageInfo(String packageName, int flags)
131 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100132 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
133 }
134
135 @Override
136 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
137 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800138 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100139 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800140 if (pi != null) {
141 return pi;
142 }
143 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700144 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800145 }
146
147 throw new NameNotFoundException(packageName);
148 }
149
150 @Override
151 public String[] currentToCanonicalPackageNames(String[] names) {
152 try {
153 return mPM.currentToCanonicalPackageNames(names);
154 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700155 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800156 }
157 }
158
159 @Override
160 public String[] canonicalToCurrentPackageNames(String[] names) {
161 try {
162 return mPM.canonicalToCurrentPackageNames(names);
163 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700164 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800165 }
166 }
167
168 @Override
169 public Intent getLaunchIntentForPackage(String packageName) {
170 // First see if the package has an INFO activity; the existence of
171 // such an activity is implied to be the desired front-door for the
172 // overall package (such as if it has multiple launcher entries).
173 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
174 intentToResolve.addCategory(Intent.CATEGORY_INFO);
175 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800176 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800177
178 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800179 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800180 // reuse the intent instance
181 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
182 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
183 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800184 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800185 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800186 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800187 return null;
188 }
189 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800190 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800191 intent.setClassName(ris.get(0).activityInfo.packageName,
192 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800193 return intent;
194 }
195
196 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700197 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
198 // Try to find a main leanback_launcher activity.
199 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
200 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
201 intentToResolve.setPackage(packageName);
202 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
203
204 if (ris == null || ris.size() <= 0) {
205 return null;
206 }
207 Intent intent = new Intent(intentToResolve);
208 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
209 intent.setClassName(ris.get(0).activityInfo.packageName,
210 ris.get(0).activityInfo.name);
211 return intent;
212 }
213
214 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700215 public int[] getPackageGids(String packageName) throws NameNotFoundException {
216 return getPackageGids(packageName, 0);
217 }
218
219 @Override
220 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800221 throws NameNotFoundException {
222 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700223 int[] gids = mPM.getPackageGids(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800224 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800225 return gids;
226 }
227 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700228 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800229 }
230
231 throw new NameNotFoundException(packageName);
232 }
233
234 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700235 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
236 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
237 }
238
239 @Override
240 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
241 return getPackageUidAsUser(packageName, 0, userId);
242 }
243
244 @Override
245 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800246 throws NameNotFoundException {
247 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700248 int uid = mPM.getPackageUid(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800249 if (uid >= 0) {
250 return uid;
251 }
252 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700253 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800254 }
255
256 throw new NameNotFoundException(packageName);
257 }
258
259 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800260 public PermissionInfo getPermissionInfo(String name, int flags)
261 throws NameNotFoundException {
262 try {
263 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
264 if (pi != null) {
265 return pi;
266 }
267 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700268 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800269 }
270
271 throw new NameNotFoundException(name);
272 }
273
274 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700275 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800276 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
277 throws NameNotFoundException {
278 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700279 ParceledListSlice<PermissionInfo> parceledList =
280 mPM.queryPermissionsByGroup(group, flags);
281 if (parceledList != null) {
282 List<PermissionInfo> pi = parceledList.getList();
283 if (pi != null) {
284 return pi;
285 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800286 }
287 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700288 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800289 }
290
291 throw new NameNotFoundException(group);
292 }
293
294 @Override
295 public PermissionGroupInfo getPermissionGroupInfo(String name,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700296 int flags) throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800297 try {
298 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
299 if (pgi != null) {
300 return pgi;
301 }
302 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700303 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800304 }
305
306 throw new NameNotFoundException(name);
307 }
308
309 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700310 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800311 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
312 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700313 ParceledListSlice<PermissionGroupInfo> parceledList =
314 mPM.getAllPermissionGroups(flags);
315 if (parceledList == null) {
316 return Collections.emptyList();
317 }
318 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800319 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700320 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800321 }
322 }
323
324 @Override
325 public ApplicationInfo getApplicationInfo(String packageName, int flags)
326 throws NameNotFoundException {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700327 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
328 }
329
330 @Override
331 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
332 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800333 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700334 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800335 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100336 // This is a temporary hack. Callers must use
337 // createPackageContext(packageName).getApplicationInfo() to
338 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800339 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800340 }
341 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700342 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800343 }
344
345 throw new NameNotFoundException(packageName);
346 }
347
Tao Baic9a02372016-01-12 15:02:24 -0800348 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100349 // If we're dealing with a multi-arch application that has both
350 // 32 and 64 bit shared libraries, we might need to choose the secondary
351 // depending on what the current runtime's instruction set is.
352 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
353 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500354
355 // Get the instruction set that the libraries of secondary Abi is supported.
356 // In presence of a native bridge this might be different than the one secondary Abi used.
357 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
358 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
359 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100360
361 // If the runtimeIsa is the same as the primary isa, then we do nothing.
362 // Everything will be set up correctly because info.nativeLibraryDir will
363 // correspond to the right ISA.
364 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800365 ApplicationInfo modified = new ApplicationInfo(info);
366 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
367 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100368 }
369 }
Tao Baic9a02372016-01-12 15:02:24 -0800370 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100371 }
372
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800373 @Override
374 public ActivityInfo getActivityInfo(ComponentName className, int flags)
375 throws NameNotFoundException {
376 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700377 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800378 if (ai != null) {
379 return ai;
380 }
381 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700382 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800383 }
384
385 throw new NameNotFoundException(className.toString());
386 }
387
388 @Override
389 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
390 throws NameNotFoundException {
391 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700392 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800393 if (ai != null) {
394 return ai;
395 }
396 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700397 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800398 }
399
400 throw new NameNotFoundException(className.toString());
401 }
402
403 @Override
404 public ServiceInfo getServiceInfo(ComponentName className, int flags)
405 throws NameNotFoundException {
406 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700407 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800408 if (si != null) {
409 return si;
410 }
411 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700412 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800413 }
414
415 throw new NameNotFoundException(className.toString());
416 }
417
418 @Override
419 public ProviderInfo getProviderInfo(ComponentName className, int flags)
420 throws NameNotFoundException {
421 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700422 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800423 if (pi != null) {
424 return pi;
425 }
426 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700427 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800428 }
429
430 throw new NameNotFoundException(className.toString());
431 }
432
433 @Override
434 public String[] getSystemSharedLibraryNames() {
435 try {
436 return mPM.getSystemSharedLibraryNames();
437 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700438 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800439 }
440 }
441
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800442 /** @hide */
443 @Override
444 public @Nullable String getServicesSystemSharedLibraryPackageName() {
445 try {
446 return mPM.getServicesSystemSharedLibraryPackageName();
447 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700448 throw e.rethrowFromSystemServer();
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800449 }
450 }
451
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800452 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700453 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800454 public FeatureInfo[] getSystemAvailableFeatures() {
455 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700456 ParceledListSlice<FeatureInfo> parceledList =
457 mPM.getSystemAvailableFeatures();
458 if (parceledList == null) {
459 return new FeatureInfo[0];
460 }
461 final List<FeatureInfo> list = parceledList.getList();
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700462 final FeatureInfo[] res = new FeatureInfo[list.size()];
463 for (int i = 0; i < res.length; i++) {
464 res[i] = list.get(i);
465 }
466 return res;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800467 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700468 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800469 }
470 }
471
472 @Override
473 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700474 return hasSystemFeature(name, 0);
475 }
476
477 @Override
478 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800479 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700480 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800481 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700482 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800483 }
484 }
485
486 @Override
487 public int checkPermission(String permName, String pkgName) {
488 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800489 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800490 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700491 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800492 }
493 }
494
495 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700496 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
497 try {
498 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
499 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700500 throw e.rethrowFromSystemServer();
Svet Ganovad3b2972015-07-07 22:49:17 -0700501 }
502 }
503
Svet Ganovf1b7f202015-07-29 08:33:42 -0700504 /**
505 * @hide
506 */
507 @Override
508 public String getPermissionControllerPackageName() {
509 synchronized (mLock) {
510 if (mPermissionsControllerPackageName == null) {
511 try {
512 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
513 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700514 throw e.rethrowFromSystemServer();
Svet Ganovf1b7f202015-07-29 08:33:42 -0700515 }
516 }
517 return mPermissionsControllerPackageName;
518 }
519 }
520
Svet Ganovad3b2972015-07-07 22:49:17 -0700521 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800522 public boolean addPermission(PermissionInfo info) {
523 try {
524 return mPM.addPermission(info);
525 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700526 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800527 }
528 }
529
530 @Override
531 public boolean addPermissionAsync(PermissionInfo info) {
532 try {
533 return mPM.addPermissionAsync(info);
534 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700535 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800536 }
537 }
538
539 @Override
540 public void removePermission(String name) {
541 try {
542 mPM.removePermission(name);
543 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700544 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800545 }
546 }
547
548 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700549 public void grantRuntimePermission(String packageName, String permissionName,
550 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800551 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700552 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800553 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700554 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800555 }
556 }
557
558 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700559 public void revokeRuntimePermission(String packageName, String permissionName,
560 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800561 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700562 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
563 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700564 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700565 }
566 }
567
568 @Override
569 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
570 try {
571 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
572 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700573 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700574 }
575 }
576
577 @Override
578 public void updatePermissionFlags(String permissionName, String packageName,
579 int flagMask, int flagValues, UserHandle user) {
580 try {
581 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
582 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800583 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700584 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800585 }
586 }
587
588 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700589 public boolean shouldShowRequestPermissionRationale(String permission) {
590 try {
591 return mPM.shouldShowRequestPermissionRationale(permission,
592 mContext.getPackageName(), mContext.getUserId());
593 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700594 throw e.rethrowFromSystemServer();
Svetoslav20770dd2015-05-29 15:43:04 -0700595 }
596 }
597
598 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800599 public int checkSignatures(String pkg1, String pkg2) {
600 try {
601 return mPM.checkSignatures(pkg1, pkg2);
602 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700603 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800604 }
605 }
606
607 @Override
608 public int checkSignatures(int uid1, int uid2) {
609 try {
610 return mPM.checkUidSignatures(uid1, uid2);
611 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700612 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800613 }
614 }
615
616 @Override
617 public String[] getPackagesForUid(int uid) {
618 try {
619 return mPM.getPackagesForUid(uid);
620 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700621 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800622 }
623 }
624
625 @Override
626 public String getNameForUid(int uid) {
627 try {
628 return mPM.getNameForUid(uid);
629 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700630 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800631 }
632 }
633
634 @Override
635 public int getUidForSharedUser(String sharedUserName)
636 throws NameNotFoundException {
637 try {
638 int uid = mPM.getUidForSharedUser(sharedUserName);
639 if(uid != -1) {
640 return uid;
641 }
642 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700643 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800644 }
645 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
646 }
647
Kenny Roote6cd0c72011-05-19 12:48:14 -0700648 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800649 @Override
650 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700651 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700652 }
653
654 /** @hide */
655 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700656 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700657 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800658 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700659 ParceledListSlice<PackageInfo> parceledList =
660 mPM.getInstalledPackages(flags, userId);
661 if (parceledList == null) {
662 return Collections.emptyList();
663 }
664 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800665 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700666 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800667 }
668 }
669
Kenny Roote6cd0c72011-05-19 12:48:14 -0700670 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800671 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800672 public List<PackageInfo> getPackagesHoldingPermissions(
673 String[] permissions, int flags) {
674 final int userId = mContext.getUserId();
675 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700676 ParceledListSlice<PackageInfo> parceledList =
677 mPM.getPackagesHoldingPermissions(permissions, flags, userId);
678 if (parceledList == null) {
679 return Collections.emptyList();
680 }
681 return parceledList.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800682 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700683 throw e.rethrowFromSystemServer();
Dianne Hackborne7991752013-01-16 17:56:46 -0800684 }
685 }
686
687 @SuppressWarnings("unchecked")
688 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800689 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700690 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800691 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700692 ParceledListSlice<ApplicationInfo> parceledList =
693 mPM.getInstalledApplications(flags, userId);
694 if (parceledList == null) {
695 return Collections.emptyList();
696 }
697 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800698 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700699 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800700 }
701 }
702
Svet Ganov2acf0632015-11-24 19:10:59 -0800703 /** @hide */
704 @SuppressWarnings("unchecked")
705 @Override
706 public List<EphemeralApplicationInfo> getEphemeralApplications() {
707 try {
708 ParceledListSlice<EphemeralApplicationInfo> slice =
709 mPM.getEphemeralApplications(mContext.getUserId());
710 if (slice != null) {
711 return slice.getList();
712 }
713 return Collections.emptyList();
714 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700715 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800716 }
717 }
718
719 /** @hide */
720 @Override
721 public Drawable getEphemeralApplicationIcon(String packageName) {
722 try {
723 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
724 packageName, mContext.getUserId());
725 if (bitmap != null) {
726 return new BitmapDrawable(null, bitmap);
727 }
728 return null;
729 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700730 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800731 }
732 }
733
734 @Override
735 public boolean isEphemeralApplication() {
736 try {
737 return mPM.isEphemeralApplication(
738 mContext.getPackageName(), mContext.getUserId());
739 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700740 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800741 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800742 }
743
744 @Override
745 public int getEphemeralCookieMaxSizeBytes() {
746 return Settings.Global.getInt(mContext.getContentResolver(),
747 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
748 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
749 }
750
751 @Override
752 public @NonNull byte[] getEphemeralCookie() {
753 try {
754 final byte[] cookie = mPM.getEphemeralApplicationCookie(
755 mContext.getPackageName(), mContext.getUserId());
756 if (cookie != null) {
757 return cookie;
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700758 } else {
759 return EmptyArray.BYTE;
Svet Ganov2acf0632015-11-24 19:10:59 -0800760 }
761 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700762 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800763 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800764 }
765
766 @Override
767 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
768 try {
769 return mPM.setEphemeralApplicationCookie(
770 mContext.getPackageName(), cookie, mContext.getUserId());
771 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700772 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800773 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800774 }
775
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800776 @Override
777 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700778 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700779 }
780
781 @Override
782 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800783 try {
784 return mPM.resolveIntent(
785 intent,
786 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700787 flags,
788 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800789 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700790 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800791 }
792 }
793
794 @Override
795 public List<ResolveInfo> queryIntentActivities(Intent intent,
796 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700797 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700798 }
799
800 /** @hide Same as above but for a specific user */
801 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700802 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700803 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700804 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800805 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700806 ParceledListSlice<ResolveInfo> parceledList =
807 mPM.queryIntentActivities(intent,
808 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
809 flags, userId);
810 if (parceledList == null) {
811 return Collections.emptyList();
812 }
813 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800814 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700815 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800816 }
817 }
818
819 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700820 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800821 public List<ResolveInfo> queryIntentActivityOptions(
822 ComponentName caller, Intent[] specifics, Intent intent,
823 int flags) {
824 final ContentResolver resolver = mContext.getContentResolver();
825
826 String[] specificTypes = null;
827 if (specifics != null) {
828 final int N = specifics.length;
829 for (int i=0; i<N; i++) {
830 Intent sp = specifics[i];
831 if (sp != null) {
832 String t = sp.resolveTypeIfNeeded(resolver);
833 if (t != null) {
834 if (specificTypes == null) {
835 specificTypes = new String[N];
836 }
837 specificTypes[i] = t;
838 }
839 }
840 }
841 }
842
843 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700844 ParceledListSlice<ResolveInfo> parceledList =
845 mPM.queryIntentActivityOptions(caller, specifics, specificTypes, intent,
846 intent.resolveTypeIfNeeded(resolver), flags, mContext.getUserId());
847 if (parceledList == null) {
848 return Collections.emptyList();
849 }
850 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800851 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700852 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800853 }
854 }
855
Amith Yamasanif203aee2012-08-29 18:41:53 -0700856 /**
857 * @hide
858 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800859 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700860 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700861 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800862 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700863 ParceledListSlice<ResolveInfo> parceledList =
864 mPM.queryIntentReceivers(intent,
865 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
866 flags, userId);
867 if (parceledList == null) {
868 return Collections.emptyList();
869 }
870 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800871 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700872 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800873 }
874 }
875
876 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700877 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700878 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700879 }
880
881 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800882 public ResolveInfo resolveService(Intent intent, int flags) {
883 try {
884 return mPM.resolveService(
885 intent,
886 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700887 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700888 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800889 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700890 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800891 }
892 }
893
894 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700895 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700896 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800897 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700898 ParceledListSlice<ResolveInfo> parceledList =
899 mPM.queryIntentServices(intent,
900 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
901 flags, userId);
902 if (parceledList == null) {
903 return Collections.emptyList();
904 }
905 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800906 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700907 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800908 }
909 }
910
911 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700912 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700913 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700914 }
915
916 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700917 @SuppressWarnings("unchecked")
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700918 public List<ResolveInfo> queryIntentContentProvidersAsUser(
919 Intent intent, int flags, int userId) {
920 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700921 ParceledListSlice<ResolveInfo> parceledList =
922 mPM.queryIntentContentProviders(intent,
923 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
924 flags, userId);
925 if (parceledList == null) {
926 return Collections.emptyList();
927 }
928 return parceledList.getList();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700929 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700930 throw e.rethrowFromSystemServer();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700931 }
932 }
933
934 @Override
935 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
936 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
937 }
938
939 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100940 public ProviderInfo resolveContentProvider(String name, int flags) {
941 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
942 }
943
944 /** @hide **/
945 @Override
946 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800947 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100948 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800949 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700950 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800951 }
952 }
953
954 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700955 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800956 public List<ProviderInfo> queryContentProviders(String processName,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700957 int uid, int flags) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800958 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700959 ParceledListSlice<ProviderInfo> slice =
960 mPM.queryContentProviders(processName, uid, flags);
961 return slice != null ? slice.getList() : Collections.<ProviderInfo>emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800962 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700963 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800964 }
965 }
966
967 @Override
968 public InstrumentationInfo getInstrumentationInfo(
969 ComponentName className, int flags)
970 throws NameNotFoundException {
971 try {
972 InstrumentationInfo ii = mPM.getInstrumentationInfo(
973 className, flags);
974 if (ii != null) {
975 return ii;
976 }
977 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700978 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800979 }
980
981 throw new NameNotFoundException(className.toString());
982 }
983
984 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700985 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800986 public List<InstrumentationInfo> queryInstrumentation(
987 String targetPackage, int flags) {
988 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700989 ParceledListSlice<InstrumentationInfo> parceledList =
990 mPM.queryInstrumentation(targetPackage, flags);
991 if (parceledList == null) {
992 return Collections.emptyList();
993 }
994 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800995 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700996 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800997 }
998 }
999
Alan Viveretteecd585a2015-04-13 10:32:51 -07001000 @Nullable
1001 @Override
1002 public Drawable getDrawable(String packageName, @DrawableRes int resId,
1003 @Nullable ApplicationInfo appInfo) {
1004 final ResourceName name = new ResourceName(packageName, resId);
1005 final Drawable cachedIcon = getCachedIcon(name);
1006 if (cachedIcon != null) {
1007 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001008 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001009
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001010 if (appInfo == null) {
1011 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001012 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001013 } catch (NameNotFoundException e) {
1014 return null;
1015 }
1016 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001017
1018 if (resId != 0) {
1019 try {
1020 final Resources r = getResourcesForApplication(appInfo);
1021 final Drawable dr = r.getDrawable(resId, null);
1022 if (dr != null) {
1023 putCachedIcon(name, dr);
1024 }
1025
1026 if (false) {
1027 RuntimeException e = new RuntimeException("here");
1028 e.fillInStackTrace();
1029 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
1030 + " from package " + packageName
1031 + ": app scale=" + r.getCompatibilityInfo().applicationScale
1032 + ", caller scale=" + mContext.getResources()
1033 .getCompatibilityInfo().applicationScale,
1034 e);
1035 }
Ricky Wai3ce46252015-04-15 16:12:22 +01001036 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001037 Log.v(TAG, "Getting drawable 0x"
1038 + Integer.toHexString(resId) + " from " + r
1039 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +01001040 }
1041 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -07001042 } catch (NameNotFoundException e) {
1043 Log.w("PackageManager", "Failure retrieving resources for "
1044 + appInfo.packageName);
1045 } catch (Resources.NotFoundException e) {
1046 Log.w("PackageManager", "Failure retrieving resources for "
1047 + appInfo.packageName + ": " + e.getMessage());
1048 } catch (Exception e) {
1049 // If an exception was thrown, fall through to return
1050 // default icon.
1051 Log.w("PackageManager", "Failure retrieving icon 0x"
1052 + Integer.toHexString(resId) + " in package "
1053 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001054 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001055 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001056
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001057 return null;
1058 }
1059
1060 @Override public Drawable getActivityIcon(ComponentName activityName)
1061 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001062 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001063 }
1064
1065 @Override public Drawable getActivityIcon(Intent intent)
1066 throws NameNotFoundException {
1067 if (intent.getComponent() != null) {
1068 return getActivityIcon(intent.getComponent());
1069 }
1070
1071 ResolveInfo info = resolveActivity(
1072 intent, PackageManager.MATCH_DEFAULT_ONLY);
1073 if (info != null) {
1074 return info.activityInfo.loadIcon(this);
1075 }
1076
Romain Guy39fe17c2011-11-30 10:34:07 -08001077 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001078 }
1079
1080 @Override public Drawable getDefaultActivityIcon() {
1081 return Resources.getSystem().getDrawable(
1082 com.android.internal.R.drawable.sym_def_app_icon);
1083 }
1084
1085 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1086 return info.loadIcon(this);
1087 }
1088
1089 @Override public Drawable getApplicationIcon(String packageName)
1090 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001091 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001092 }
1093
1094 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001095 public Drawable getActivityBanner(ComponentName activityName)
1096 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001097 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001098 }
1099
1100 @Override
1101 public Drawable getActivityBanner(Intent intent)
1102 throws NameNotFoundException {
1103 if (intent.getComponent() != null) {
1104 return getActivityBanner(intent.getComponent());
1105 }
1106
1107 ResolveInfo info = resolveActivity(
1108 intent, PackageManager.MATCH_DEFAULT_ONLY);
1109 if (info != null) {
1110 return info.activityInfo.loadBanner(this);
1111 }
1112
1113 throw new NameNotFoundException(intent.toUri(0));
1114 }
1115
1116 @Override
1117 public Drawable getApplicationBanner(ApplicationInfo info) {
1118 return info.loadBanner(this);
1119 }
1120
1121 @Override
1122 public Drawable getApplicationBanner(String packageName)
1123 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001124 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001125 }
1126
1127 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001128 public Drawable getActivityLogo(ComponentName activityName)
1129 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001130 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001131 }
1132
1133 @Override
1134 public Drawable getActivityLogo(Intent intent)
1135 throws NameNotFoundException {
1136 if (intent.getComponent() != null) {
1137 return getActivityLogo(intent.getComponent());
1138 }
1139
1140 ResolveInfo info = resolveActivity(
1141 intent, PackageManager.MATCH_DEFAULT_ONLY);
1142 if (info != null) {
1143 return info.activityInfo.loadLogo(this);
1144 }
1145
1146 throw new NameNotFoundException(intent.toUri(0));
1147 }
1148
1149 @Override
1150 public Drawable getApplicationLogo(ApplicationInfo info) {
1151 return info.loadLogo(this);
1152 }
1153
1154 @Override
1155 public Drawable getApplicationLogo(String packageName)
1156 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001157 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001158 }
1159
Svetoslavc7d62f02014-09-04 15:39:54 -07001160 @Override
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001161 public Drawable getManagedUserBadgedDrawable(Drawable drawable, Rect badgeLocation,
1162 int badgeDensity) {
1163 Drawable badgeDrawable = getDrawableForDensity(
1164 com.android.internal.R.drawable.ic_corp_badge, badgeDensity);
1165 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1166 }
1167
1168 @Override
Svetoslavc7d62f02014-09-04 15:39:54 -07001169 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1170 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1171 if (badgeResId == 0) {
1172 return icon;
1173 }
1174 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1175 return getBadgedDrawable(icon, badgeIcon, null, true);
1176 }
1177
1178 @Override
1179 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1180 Rect badgeLocation, int badgeDensity) {
1181 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1182 if (badgeDrawable == null) {
1183 return drawable;
1184 }
1185 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1186 }
1187
1188 @Override
1189 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001190 return getManagedProfileIconForDensity(user, com.android.internal.R.drawable.ic_corp_badge,
1191 density);
Selim Cineke6ff9462016-01-15 15:07:06 -08001192 }
1193
1194 @Override
1195 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001196 return getManagedProfileIconForDensity(user,
1197 com.android.internal.R.drawable.ic_corp_badge_no_background, density);
Selim Cineke6ff9462016-01-15 15:07:06 -08001198 }
1199
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001200 private Drawable getDrawableForDensity(int drawableId, int density) {
1201 if (density <= 0) {
1202 density = mContext.getResources().getDisplayMetrics().densityDpi;
1203 }
1204 return Resources.getSystem().getDrawableForDensity(drawableId, density);
1205 }
1206
1207 private Drawable getManagedProfileIconForDensity(UserHandle user, int drawableId, int density) {
Tony Mak8673b282016-03-21 21:10:59 +00001208 if (isManagedProfile(user.getIdentifier())) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001209 return getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001210 }
1211 return null;
1212 }
1213
1214 @Override
1215 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
Tony Mak8673b282016-03-21 21:10:59 +00001216 if (isManagedProfile(user.getIdentifier())) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001217 return Resources.getSystem().getString(
1218 com.android.internal.R.string.managed_profile_label_badge, label);
1219 }
1220 return label;
1221 }
1222
Alan Viveretteecd585a2015-04-13 10:32:51 -07001223 @Override
1224 public Resources getResourcesForActivity(ComponentName activityName)
1225 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001226 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001227 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001228 }
1229
Alan Viveretteecd585a2015-04-13 10:32:51 -07001230 @Override
1231 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1232 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001233 if (app.packageName.equals("system")) {
1234 return mContext.mMainThread.getSystemContext().getResources();
1235 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001236 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001237 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001238 sameUid ? app.sourceDir : app.publicSourceDir,
1239 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001240 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
Adam Lesinski082614c2016-03-04 14:33:47 -08001241 mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001242 if (r != null) {
1243 return r;
1244 }
1245 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1246 }
1247
Alan Viveretteecd585a2015-04-13 10:32:51 -07001248 @Override
1249 public Resources getResourcesForApplication(String appPackageName)
1250 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001251 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001252 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001253 }
1254
Amith Yamasani98edc952012-09-25 14:09:27 -07001255 /** @hide */
1256 @Override
1257 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1258 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001259 if (userId < 0) {
1260 throw new IllegalArgumentException(
1261 "Call does not support special user #" + userId);
1262 }
1263 if ("system".equals(appPackageName)) {
1264 return mContext.mMainThread.getSystemContext().getResources();
1265 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001266 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001267 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001268 if (ai != null) {
1269 return getResourcesForApplication(ai);
1270 }
1271 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001272 throw e.rethrowFromSystemServer();
Amith Yamasani98edc952012-09-25 14:09:27 -07001273 }
1274 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1275 }
1276
Jeff Sharkeycd654482016-01-08 17:42:11 -07001277 volatile int mCachedSafeMode = -1;
1278
1279 @Override
1280 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001281 try {
1282 if (mCachedSafeMode < 0) {
1283 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1284 }
1285 return mCachedSafeMode != 0;
1286 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001287 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001288 }
1289 }
1290
Svetoslavf7c06eb2015-06-10 18:43:22 -07001291 @Override
1292 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1293 synchronized (mPermissionListeners) {
1294 if (mPermissionListeners.get(listener) != null) {
1295 return;
1296 }
1297 OnPermissionsChangeListenerDelegate delegate =
1298 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1299 try {
1300 mPM.addOnPermissionsChangeListener(delegate);
1301 mPermissionListeners.put(listener, delegate);
1302 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001303 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001304 }
1305 }
1306 }
1307
1308 @Override
1309 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1310 synchronized (mPermissionListeners) {
1311 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1312 if (delegate != null) {
1313 try {
1314 mPM.removeOnPermissionsChangeListener(delegate);
1315 mPermissionListeners.remove(listener);
1316 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001317 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001318 }
1319 }
1320 }
1321 }
1322
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001323 static void configurationChanged() {
1324 synchronized (sSync) {
1325 sIconCache.clear();
1326 sStringCache.clear();
1327 }
1328 }
1329
1330 ApplicationPackageManager(ContextImpl context,
1331 IPackageManager pm) {
1332 mContext = context;
1333 mPM = pm;
1334 }
1335
Alan Viveretteecd585a2015-04-13 10:32:51 -07001336 @Nullable
1337 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001338 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001339 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001340 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1341 + name + ": " + wr);
1342 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001343 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001344 if (state != null) {
1345 if (DEBUG_ICONS) {
1346 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1347 }
1348 // Note: It's okay here to not use the newDrawable(Resources) variant
1349 // of the API. The ConstantState comes from a drawable that was
1350 // originally created by passing the proper app Resources instance
1351 // which means the state should already contain the proper
1352 // resources specific information (like density.) See
1353 // BitmapDrawable.BitmapState for instance.
1354 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001355 }
1356 // our entry has been purged
1357 sIconCache.remove(name);
1358 }
1359 }
1360 return null;
1361 }
1362
Alan Viveretteecd585a2015-04-13 10:32:51 -07001363 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001364 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001365 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001366 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001367 }
1368 }
1369
Romain Guy39fe17c2011-11-30 10:34:07 -08001370 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001371 boolean immediateGc = false;
1372 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1373 immediateGc = true;
1374 }
1375 if (pkgList != null && (pkgList.length > 0)) {
1376 boolean needCleanup = false;
1377 for (String ssp : pkgList) {
1378 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001379 for (int i=sIconCache.size()-1; i>=0; i--) {
1380 ResourceName nm = sIconCache.keyAt(i);
1381 if (nm.packageName.equals(ssp)) {
1382 //Log.i(TAG, "Removing cached drawable for " + nm);
1383 sIconCache.removeAt(i);
1384 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001385 }
1386 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001387 for (int i=sStringCache.size()-1; i>=0; i--) {
1388 ResourceName nm = sStringCache.keyAt(i);
1389 if (nm.packageName.equals(ssp)) {
1390 //Log.i(TAG, "Removing cached string for " + nm);
1391 sStringCache.removeAt(i);
1392 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001393 }
1394 }
1395 }
1396 }
1397 if (needCleanup || hasPkgInfo) {
1398 if (immediateGc) {
1399 // Schedule an immediate gc.
1400 Runtime.getRuntime().gc();
1401 } else {
1402 ActivityThread.currentActivityThread().scheduleGcIdler();
1403 }
1404 }
1405 }
1406 }
1407
1408 private static final class ResourceName {
1409 final String packageName;
1410 final int iconId;
1411
1412 ResourceName(String _packageName, int _iconId) {
1413 packageName = _packageName;
1414 iconId = _iconId;
1415 }
1416
1417 ResourceName(ApplicationInfo aInfo, int _iconId) {
1418 this(aInfo.packageName, _iconId);
1419 }
1420
1421 ResourceName(ComponentInfo cInfo, int _iconId) {
1422 this(cInfo.applicationInfo.packageName, _iconId);
1423 }
1424
1425 ResourceName(ResolveInfo rInfo, int _iconId) {
1426 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1427 }
1428
1429 @Override
1430 public boolean equals(Object o) {
1431 if (this == o) return true;
1432 if (o == null || getClass() != o.getClass()) return false;
1433
1434 ResourceName that = (ResourceName) o;
1435
1436 if (iconId != that.iconId) return false;
1437 return !(packageName != null ?
1438 !packageName.equals(that.packageName) : that.packageName != null);
1439
1440 }
1441
1442 @Override
1443 public int hashCode() {
1444 int result;
1445 result = packageName.hashCode();
1446 result = 31 * result + iconId;
1447 return result;
1448 }
1449
1450 @Override
1451 public String toString() {
1452 return "{ResourceName " + packageName + " / " + iconId + "}";
1453 }
1454 }
1455
1456 private CharSequence getCachedString(ResourceName name) {
1457 synchronized (sSync) {
1458 WeakReference<CharSequence> wr = sStringCache.get(name);
1459 if (wr != null) { // we have the activity
1460 CharSequence cs = wr.get();
1461 if (cs != null) {
1462 return cs;
1463 }
1464 // our entry has been purged
1465 sStringCache.remove(name);
1466 }
1467 }
1468 return null;
1469 }
1470
1471 private void putCachedString(ResourceName name, CharSequence cs) {
1472 synchronized (sSync) {
1473 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1474 }
1475 }
1476
1477 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001478 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001479 ApplicationInfo appInfo) {
1480 ResourceName name = new ResourceName(packageName, resid);
1481 CharSequence text = getCachedString(name);
1482 if (text != null) {
1483 return text;
1484 }
1485 if (appInfo == null) {
1486 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001487 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001488 } catch (NameNotFoundException e) {
1489 return null;
1490 }
1491 }
1492 try {
1493 Resources r = getResourcesForApplication(appInfo);
1494 text = r.getText(resid);
1495 putCachedString(name, text);
1496 return text;
1497 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001498 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001499 + appInfo.packageName);
1500 } catch (RuntimeException e) {
1501 // If an exception was thrown, fall through to return
1502 // default icon.
1503 Log.w("PackageManager", "Failure retrieving text 0x"
1504 + Integer.toHexString(resid) + " in package "
1505 + packageName, e);
1506 }
1507 return null;
1508 }
1509
1510 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001511 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001512 ApplicationInfo appInfo) {
1513 if (appInfo == null) {
1514 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001515 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001516 } catch (NameNotFoundException e) {
1517 return null;
1518 }
1519 }
1520 try {
1521 Resources r = getResourcesForApplication(appInfo);
1522 return r.getXml(resid);
1523 } catch (RuntimeException e) {
1524 // If an exception was thrown, fall through to return
1525 // default icon.
1526 Log.w("PackageManager", "Failure retrieving xml 0x"
1527 + Integer.toHexString(resid) + " in package "
1528 + packageName, e);
1529 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001530 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001531 + appInfo.packageName);
1532 }
1533 return null;
1534 }
1535
1536 @Override
1537 public CharSequence getApplicationLabel(ApplicationInfo info) {
1538 return info.loadLabel(this);
1539 }
1540
1541 @Override
1542 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1543 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001544 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Todd Kennedya6793232016-02-24 22:46:00 +00001545 installerPackageName, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001546 }
1547
Christopher Tatef1977b42014-03-24 16:25:51 -07001548 @Override
1549 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1550 int flags, String installerPackageName) {
Todd Kennedya6793232016-02-24 22:46:00 +00001551 installCommon(packageURI, observer, flags, installerPackageName, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001552 }
1553
1554 private void installCommon(Uri packageURI,
1555 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001556 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001557 if (!"file".equals(packageURI.getScheme())) {
1558 throw new UnsupportedOperationException("Only file:// URIs are supported");
1559 }
Jeff Sharkey513a0742014-07-08 17:10:32 -07001560
1561 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001562 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001563 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
Todd Kennedya6793232016-02-24 22:46:00 +00001564 userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001565 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001566 throw e.rethrowFromSystemServer();
rich cannings706e8ba2012-08-20 13:20:14 -07001567 }
1568 }
1569
1570 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001571 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001572 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001573 }
1574
1575 @Override
1576 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001577 throws NameNotFoundException {
1578 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001579 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001580 if (res == INSTALL_FAILED_INVALID_URI) {
1581 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1582 }
1583 return res;
1584 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001585 throw e.rethrowFromSystemServer();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001586 }
1587 }
1588
1589 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001590 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001591 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001592 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001593 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001594 throw e.rethrowFromSystemServer();
Kenny Root5ab21572011-07-27 11:11:19 -07001595 }
1596 }
1597
1598 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001599 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1600 long millisecondsToDelay) {
1601 try {
1602 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1603 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001604 throw e.rethrowFromSystemServer();
rich canningsd9ef3e52012-08-22 14:28:05 -07001605 }
1606 }
1607
1608 @Override
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001609 public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001610 try {
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001611 mPM.verifyIntentFilter(id, verificationCode, failedDomains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001612 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001613 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001614 }
1615 }
1616
1617 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001618 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001619 try {
1620 return mPM.getIntentVerificationStatus(packageName, userId);
1621 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001622 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001623 }
1624 }
1625
1626 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001627 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001628 try {
1629 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1630 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001631 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001632 }
1633 }
1634
1635 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001636 @SuppressWarnings("unchecked")
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001637 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1638 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001639 ParceledListSlice<IntentFilterVerificationInfo> parceledList =
1640 mPM.getIntentFilterVerifications(packageName);
1641 if (parceledList == null) {
1642 return Collections.emptyList();
1643 }
1644 return parceledList.getList();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001645 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001646 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001647 }
1648 }
1649
1650 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001651 @SuppressWarnings("unchecked")
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001652 public List<IntentFilter> getAllIntentFilters(String packageName) {
1653 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001654 ParceledListSlice<IntentFilter> parceledList =
1655 mPM.getAllIntentFilters(packageName);
1656 if (parceledList == null) {
1657 return Collections.emptyList();
1658 }
1659 return parceledList.getList();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001660 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001661 throw e.rethrowFromSystemServer();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001662 }
1663 }
1664
1665 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001666 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001667 try {
1668 return mPM.getDefaultBrowserPackageName(userId);
1669 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001670 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001671 }
1672 }
1673
1674 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001675 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001676 try {
1677 return mPM.setDefaultBrowserPackageName(packageName, userId);
1678 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001679 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001680 }
1681 }
1682
1683 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001684 public void setInstallerPackageName(String targetPackage,
1685 String installerPackageName) {
1686 try {
1687 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1688 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001689 throw e.rethrowFromSystemServer();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001690 }
1691 }
1692
1693 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001694 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001695 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001696 return mPM.getInstallerPackageName(packageName);
1697 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001698 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001699 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001700 }
1701
1702 @Override
1703 public int getMoveStatus(int moveId) {
1704 try {
1705 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001706 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001707 throw e.rethrowFromSystemServer();
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001708 }
1709 }
1710
1711 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001712 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1713 synchronized (mDelegates) {
1714 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1715 handler.getLooper());
1716 try {
1717 mPM.registerMoveCallback(delegate);
1718 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001719 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001720 }
1721 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001722 }
1723 }
1724
1725 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001726 public void unregisterMoveCallback(MoveCallback callback) {
1727 synchronized (mDelegates) {
1728 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1729 final MoveCallbackDelegate delegate = i.next();
1730 if (delegate.mCallback == callback) {
1731 try {
1732 mPM.unregisterMoveCallback(delegate);
1733 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001734 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001735 }
1736 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001737 }
1738 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001739 }
1740 }
1741
1742 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001743 public int movePackage(String packageName, VolumeInfo vol) {
1744 try {
1745 final String volumeUuid;
1746 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1747 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1748 } else if (vol.isPrimaryPhysical()) {
1749 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1750 } else {
1751 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1752 }
1753
1754 return mPM.movePackage(packageName, volumeUuid);
1755 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001756 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001757 }
1758 }
1759
1760 @Override
1761 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001762 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001763 if (app.isInternal()) {
1764 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1765 } else if (app.isExternalAsec()) {
1766 return storage.getPrimaryPhysicalVolume();
1767 } else {
1768 return storage.findVolumeByUuid(app.volumeUuid);
1769 }
1770 }
1771
1772 @Override
1773 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1774 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1775 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001776 final List<VolumeInfo> vols = storage.getVolumes();
1777 final List<VolumeInfo> candidates = new ArrayList<>();
1778 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001779 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001780 candidates.add(vol);
1781 }
1782 }
1783 return candidates;
1784 }
1785
Makoto Onukif34db0a2016-02-17 11:17:15 -08001786 private boolean isPackageCandidateVolume(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001787 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1788 final boolean forceAllowOnExternal = Settings.Global.getInt(
1789 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001790 // Private internal is always an option
1791 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1792 return true;
1793 }
1794
1795 // System apps and apps demanding internal storage can't be moved
1796 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001797 if (app.isSystemApp()) {
1798 return false;
1799 }
1800 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001801 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1802 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001803 return false;
1804 }
1805
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001806 // Gotta be able to write there
1807 if (!vol.isMountedWritable()) {
1808 return false;
1809 }
1810
1811 // Moving into an ASEC on public primary is only option internal
1812 if (vol.isPrimaryPhysical()) {
1813 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001814 }
1815
Makoto Onukif34db0a2016-02-17 11:17:15 -08001816 // Some apps can't be moved. (e.g. device admins)
1817 try {
1818 if (mPM.isPackageDeviceAdminOnAnyUser(app.packageName)) {
1819 return false;
1820 }
1821 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001822 throw e.rethrowFromSystemServer();
Makoto Onukif34db0a2016-02-17 11:17:15 -08001823 }
1824
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001825 // Otherwise we can move to any private volume
1826 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1827 }
1828
1829 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001830 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001831 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001832 final String volumeUuid;
1833 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1834 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1835 } else if (vol.isPrimaryPhysical()) {
1836 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1837 } else {
1838 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1839 }
1840
1841 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001842 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001843 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001844 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001845 }
1846
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001847 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001848 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1849 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1850 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001851 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001852 }
1853
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001854 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001855 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1856 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1857 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1858 final List<VolumeInfo> vols = storage.getVolumes();
1859 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001860 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1861 storage.getPrimaryStorageUuid()) && currentVol != null) {
1862 // TODO: support moving primary physical to emulated volume
1863 candidates.add(currentVol);
1864 } else {
1865 for (VolumeInfo vol : vols) {
1866 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1867 candidates.add(vol);
1868 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001869 }
1870 }
1871 return candidates;
1872 }
1873
1874 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1875 // Private internal is always an option
1876 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1877 return true;
1878 }
1879
1880 // Gotta be able to write there
1881 if (!vol.isMountedWritable()) {
1882 return false;
1883 }
1884
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001885 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001886 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001887 }
1888
1889 @Override
1890 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001891 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001892 }
1893
1894 @Override
1895 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1896 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001897 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001898 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001899 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001900 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001901 }
1902 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001903
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001904 @Override
1905 public void clearApplicationUserData(String packageName,
1906 IPackageDataObserver observer) {
1907 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001908 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001909 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001910 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001911 }
1912 }
1913 @Override
1914 public void deleteApplicationCacheFiles(String packageName,
1915 IPackageDataObserver observer) {
1916 try {
1917 mPM.deleteApplicationCacheFiles(packageName, observer);
1918 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001919 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001920 }
1921 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001922
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001923 @Override
Suprabh Shukla78c9eb82016-04-12 15:51:35 -07001924 public void deleteApplicationCacheFilesAsUser(String packageName, int userId,
1925 IPackageDataObserver observer) {
1926 try {
1927 mPM.deleteApplicationCacheFilesAsUser(packageName, userId, observer);
1928 } catch (RemoteException e) {
1929 throw e.rethrowFromSystemServer();
1930 }
1931 }
1932
1933 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001934 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1935 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001936 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001937 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001938 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001939 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001940 }
1941 }
1942
1943 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001944 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001945 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001946 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001947 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001948 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001949 }
1950 }
1951
1952 @Override
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001953 public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended,
1954 int userId) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001955 try {
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001956 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, userId);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001957 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001958 throw e.rethrowFromSystemServer();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001959 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001960 }
1961
1962 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001963 public boolean isPackageSuspendedForUser(String packageName, int userId) {
1964 try {
1965 return mPM.isPackageSuspendedForUser(packageName, userId);
1966 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001967 throw e.rethrowFromSystemServer();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001968 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001969 }
1970
1971 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001972 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001973 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001974 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001975 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001976 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001977 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001978 }
1979 }
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001980
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001981 @Override
1982 public void addPackageToPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001983 Log.w(TAG, "addPackageToPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001984 }
1985
1986 @Override
1987 public void removePackageFromPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001988 Log.w(TAG, "removePackageFromPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001989 }
1990
1991 @Override
1992 public List<PackageInfo> getPreferredPackages(int flags) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001993 Log.w(TAG, "getPreferredPackages() is a no-op");
1994 return Collections.emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001995 }
1996
1997 @Override
1998 public void addPreferredActivity(IntentFilter filter,
1999 int match, ComponentName[] set, ComponentName activity) {
2000 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002001 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07002002 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002003 throw e.rethrowFromSystemServer();
Amith Yamasania3f133a2012-08-09 17:11:28 -07002004 }
2005 }
2006
2007 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002008 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07002009 ComponentName[] set, ComponentName activity, int userId) {
2010 try {
2011 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002012 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002013 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002014 }
2015 }
2016
2017 @Override
2018 public void replacePreferredActivity(IntentFilter filter,
2019 int match, ComponentName[] set, ComponentName activity) {
2020 try {
Robin Lee0e27c872015-09-28 14:37:40 +01002021 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002022 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002023 throw e.rethrowFromSystemServer();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002024 }
2025 }
2026
2027 @Override
2028 public void replacePreferredActivityAsUser(IntentFilter filter,
2029 int match, ComponentName[] set, ComponentName activity,
2030 int userId) {
2031 try {
2032 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002033 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002034 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002035 }
2036 }
2037
2038 @Override
2039 public void clearPackagePreferredActivities(String packageName) {
2040 try {
2041 mPM.clearPackagePreferredActivities(packageName);
2042 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002043 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002044 }
2045 }
2046
2047 @Override
2048 public int getPreferredActivities(List<IntentFilter> outFilters,
2049 List<ComponentName> outActivities, String packageName) {
2050 try {
2051 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2052 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002053 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002054 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002055 }
2056
2057 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002058 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2059 try {
2060 return mPM.getHomeActivities(outActivities);
2061 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002062 throw e.rethrowFromSystemServer();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002063 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002064 }
2065
2066 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002067 public void setComponentEnabledSetting(ComponentName componentName,
2068 int newState, int flags) {
2069 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002070 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002071 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002072 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002073 }
2074 }
2075
2076 @Override
2077 public int getComponentEnabledSetting(ComponentName componentName) {
2078 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002079 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002080 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002081 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002082 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002083 }
2084
2085 @Override
2086 public void setApplicationEnabledSetting(String packageName,
2087 int newState, int flags) {
2088 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002089 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002090 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002091 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002092 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002093 }
2094 }
2095
2096 @Override
2097 public int getApplicationEnabledSetting(String packageName) {
2098 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002099 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002100 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002101 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002102 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002103 }
2104
Amith Yamasani655d0e22013-06-12 14:19:10 -07002105 @Override
Sudheer Shankabbb3ff22015-07-09 15:39:23 +01002106 public void flushPackageRestrictionsAsUser(int userId) {
2107 try {
2108 mPM.flushPackageRestrictionsAsUser(userId);
2109 } catch (RemoteException e) {
2110 throw e.rethrowFromSystemServer();
2111 }
2112 }
2113
2114 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002115 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002116 UserHandle user) {
2117 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002118 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002119 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002120 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002121 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002122 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002123 }
2124
2125 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002126 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002127 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002128 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002129 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002130 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002131 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002132 }
2133
dcashmanc6f22492014-08-14 09:54:51 -07002134 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002135 @Override
2136 public KeySet getKeySetByAlias(String packageName, String alias) {
2137 Preconditions.checkNotNull(packageName);
2138 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002139 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002140 return mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002141 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002142 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002143 }
dcashman9d2f4412014-06-09 09:27:54 -07002144 }
2145
dcashmanc6f22492014-08-14 09:54:51 -07002146 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002147 @Override
2148 public KeySet getSigningKeySet(String packageName) {
2149 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002150 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002151 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002152 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002153 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002154 }
dcashman9d2f4412014-06-09 09:27:54 -07002155 }
2156
dcashmanc6f22492014-08-14 09:54:51 -07002157 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002158 @Override
2159 public boolean isSignedBy(String packageName, KeySet ks) {
2160 Preconditions.checkNotNull(packageName);
2161 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002162 try {
dcashmanc6f22492014-08-14 09:54:51 -07002163 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002164 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002165 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002166 }
2167 }
2168
dcashmanc6f22492014-08-14 09:54:51 -07002169 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002170 @Override
2171 public boolean isSignedByExactly(String packageName, KeySet ks) {
2172 Preconditions.checkNotNull(packageName);
2173 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002174 try {
dcashmanc6f22492014-08-14 09:54:51 -07002175 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002176 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002177 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002178 }
2179 }
2180
Kenny Root0aaa0d92011-09-12 16:42:55 -07002181 /**
2182 * @hide
2183 */
2184 @Override
2185 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2186 try {
2187 return mPM.getVerifierDeviceIdentity();
2188 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002189 throw e.rethrowFromSystemServer();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002190 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002191 }
2192
Jeff Hao9f60c082014-10-28 18:51:07 -07002193 /**
2194 * @hide
2195 */
2196 @Override
2197 public boolean isUpgrade() {
2198 try {
2199 return mPM.isUpgrade();
2200 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002201 throw e.rethrowFromSystemServer();
Jeff Hao9f60c082014-10-28 18:51:07 -07002202 }
2203 }
2204
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002205 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002206 public PackageInstaller getPackageInstaller() {
2207 synchronized (mLock) {
2208 if (mInstaller == null) {
2209 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002210 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002211 mContext.getPackageName(), mContext.getUserId());
2212 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002213 throw e.rethrowFromSystemServer();
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002214 }
2215 }
2216 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002217 }
2218 }
2219
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002220 @Override
2221 public boolean isPackageAvailable(String packageName) {
2222 try {
2223 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2224 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002225 throw e.rethrowFromSystemServer();
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002226 }
2227 }
2228
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002229 /**
2230 * @hide
2231 */
2232 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002233 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2234 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002235 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002236 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002237 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002238 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002239 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002240 }
2241 }
2242
2243 /**
2244 * @hide
2245 */
2246 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002247 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002248 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002249 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002250 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002251 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002252 }
2253 }
2254
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002255 /**
2256 * @hide
2257 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002258 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002259 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2260 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2261 return dr;
2262 }
2263 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2264 }
2265
2266 /**
2267 * @hide
2268 */
2269 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002270 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002271 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2272 if (bitmap == null) {
2273 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2274 }
2275 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002276 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002277 Drawable dr = null;
2278 if (itemInfo.packageName != null) {
2279 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2280 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002281 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002282 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002283 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002284 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002285 }
2286
2287 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2288 Rect badgeLocation, boolean tryBadgeInPlace) {
2289 final int badgedWidth = drawable.getIntrinsicWidth();
2290 final int badgedHeight = drawable.getIntrinsicHeight();
2291 final boolean canBadgeInPlace = tryBadgeInPlace
2292 && (drawable instanceof BitmapDrawable)
2293 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2294
2295 final Bitmap bitmap;
2296 if (canBadgeInPlace) {
2297 bitmap = ((BitmapDrawable) drawable).getBitmap();
2298 } else {
2299 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2300 }
2301 Canvas canvas = new Canvas(bitmap);
2302
2303 if (!canBadgeInPlace) {
2304 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2305 drawable.draw(canvas);
2306 }
2307
2308 if (badgeLocation != null) {
2309 if (badgeLocation.left < 0 || badgeLocation.top < 0
2310 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2311 throw new IllegalArgumentException("Badge location " + badgeLocation
2312 + " not in badged drawable bounds "
2313 + new Rect(0, 0, badgedWidth, badgedHeight));
2314 }
2315 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2316
2317 canvas.save();
2318 canvas.translate(badgeLocation.left, badgeLocation.top);
2319 badgeDrawable.draw(canvas);
2320 canvas.restore();
2321 } else {
2322 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2323 badgeDrawable.draw(canvas);
2324 }
2325
2326 if (!canBadgeInPlace) {
2327 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2328
2329 if (drawable instanceof BitmapDrawable) {
2330 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2331 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2332 }
2333
2334 return mergedDrawable;
2335 }
2336
2337 return drawable;
2338 }
2339
Tony Mak8673b282016-03-21 21:10:59 +00002340 private int getBadgeResIdForUser(int userId) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002341 // Return the framework-provided badge.
Tony Mak8673b282016-03-21 21:10:59 +00002342 if (isManagedProfile(userId)) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002343 return com.android.internal.R.drawable.ic_corp_icon_badge;
2344 }
2345 return 0;
2346 }
2347
Tony Mak8673b282016-03-21 21:10:59 +00002348 private boolean isManagedProfile(int userId) {
2349 return getUserManager().isManagedProfile(userId);
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002350 }
2351
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002352 /** {@hide} */
2353 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2354 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002355 private static final int MSG_CREATED = 1;
2356 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002357
2358 final MoveCallback mCallback;
2359 final Handler mHandler;
2360
2361 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2362 mCallback = callback;
2363 mHandler = new Handler(looper, this);
2364 }
2365
2366 @Override
2367 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002368 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002369 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002370 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002371 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002372 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002373 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002374 }
2375 case MSG_STATUS_CHANGED: {
2376 final SomeArgs args = (SomeArgs) msg.obj;
2377 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2378 args.recycle();
2379 return true;
2380 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002381 }
2382 return false;
2383 }
2384
2385 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002386 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002387 final SomeArgs args = SomeArgs.obtain();
2388 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002389 args.arg2 = extras;
2390 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2391 }
2392
2393 @Override
2394 public void onStatusChanged(int moveId, int status, long estMillis) {
2395 final SomeArgs args = SomeArgs.obtain();
2396 args.argi1 = moveId;
2397 args.argi2 = status;
2398 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002399 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002400 }
2401 }
2402
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002403 private final ContextImpl mContext;
2404 private final IPackageManager mPM;
2405
2406 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002407 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2408 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2409 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2410 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002411
2412 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2413 mPermissionListeners = new ArrayMap<>();
2414
2415 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2416 implements Handler.Callback{
2417 private static final int MSG_PERMISSIONS_CHANGED = 1;
2418
2419 private final OnPermissionsChangedListener mListener;
2420 private final Handler mHandler;
2421
2422
2423 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2424 Looper looper) {
2425 mListener = listener;
2426 mHandler = new Handler(looper, this);
2427 }
2428
2429 @Override
2430 public void onPermissionsChanged(int uid) {
2431 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2432 }
2433
2434 @Override
2435 public boolean handleMessage(Message msg) {
2436 switch (msg.what) {
2437 case MSG_PERMISSIONS_CHANGED: {
2438 final int uid = msg.arg1;
2439 mListener.onPermissionsChanged(uid);
2440 return true;
2441 }
2442 }
2443 return false;
2444 }
2445 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002446}