blob: 87511eed93e6f85e063242f6cb9eede5359b4246 [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
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700444 public @NonNull String getServicesSystemSharedLibraryPackageName() {
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800445 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
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700452 /**
453 * @hide
454 */
455 public @NonNull String getSharedSystemSharedLibraryPackageName() {
456 try {
457 return mPM.getSharedSystemSharedLibraryPackageName();
458 } catch (RemoteException e) {
459 throw e.rethrowFromSystemServer();
460 }
461 }
462
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800463 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700464 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800465 public FeatureInfo[] getSystemAvailableFeatures() {
466 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700467 ParceledListSlice<FeatureInfo> parceledList =
468 mPM.getSystemAvailableFeatures();
469 if (parceledList == null) {
470 return new FeatureInfo[0];
471 }
472 final List<FeatureInfo> list = parceledList.getList();
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700473 final FeatureInfo[] res = new FeatureInfo[list.size()];
474 for (int i = 0; i < res.length; i++) {
475 res[i] = list.get(i);
476 }
477 return res;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800478 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700479 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800480 }
481 }
482
483 @Override
484 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700485 return hasSystemFeature(name, 0);
486 }
487
488 @Override
489 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800490 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700491 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800492 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700493 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800494 }
495 }
496
497 @Override
498 public int checkPermission(String permName, String pkgName) {
499 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800500 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800501 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700502 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800503 }
504 }
505
506 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700507 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
508 try {
509 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
510 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700511 throw e.rethrowFromSystemServer();
Svet Ganovad3b2972015-07-07 22:49:17 -0700512 }
513 }
514
Svet Ganovf1b7f202015-07-29 08:33:42 -0700515 /**
516 * @hide
517 */
518 @Override
519 public String getPermissionControllerPackageName() {
520 synchronized (mLock) {
521 if (mPermissionsControllerPackageName == null) {
522 try {
523 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
524 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700525 throw e.rethrowFromSystemServer();
Svet Ganovf1b7f202015-07-29 08:33:42 -0700526 }
527 }
528 return mPermissionsControllerPackageName;
529 }
530 }
531
Svet Ganovad3b2972015-07-07 22:49:17 -0700532 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800533 public boolean addPermission(PermissionInfo info) {
534 try {
535 return mPM.addPermission(info);
536 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700537 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800538 }
539 }
540
541 @Override
542 public boolean addPermissionAsync(PermissionInfo info) {
543 try {
544 return mPM.addPermissionAsync(info);
545 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700546 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800547 }
548 }
549
550 @Override
551 public void removePermission(String name) {
552 try {
553 mPM.removePermission(name);
554 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700555 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800556 }
557 }
558
559 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700560 public void grantRuntimePermission(String packageName, String permissionName,
561 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800562 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700563 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800564 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700565 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800566 }
567 }
568
569 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700570 public void revokeRuntimePermission(String packageName, String permissionName,
571 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800572 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700573 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
574 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700575 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700576 }
577 }
578
579 @Override
580 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
581 try {
582 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
583 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700584 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700585 }
586 }
587
588 @Override
589 public void updatePermissionFlags(String permissionName, String packageName,
590 int flagMask, int flagValues, UserHandle user) {
591 try {
592 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
593 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800594 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700595 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800596 }
597 }
598
599 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700600 public boolean shouldShowRequestPermissionRationale(String permission) {
601 try {
602 return mPM.shouldShowRequestPermissionRationale(permission,
603 mContext.getPackageName(), mContext.getUserId());
604 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700605 throw e.rethrowFromSystemServer();
Svetoslav20770dd2015-05-29 15:43:04 -0700606 }
607 }
608
609 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800610 public int checkSignatures(String pkg1, String pkg2) {
611 try {
612 return mPM.checkSignatures(pkg1, pkg2);
613 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700614 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800615 }
616 }
617
618 @Override
619 public int checkSignatures(int uid1, int uid2) {
620 try {
621 return mPM.checkUidSignatures(uid1, uid2);
622 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700623 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800624 }
625 }
626
627 @Override
628 public String[] getPackagesForUid(int uid) {
629 try {
630 return mPM.getPackagesForUid(uid);
631 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700632 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800633 }
634 }
635
636 @Override
637 public String getNameForUid(int uid) {
638 try {
639 return mPM.getNameForUid(uid);
640 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700641 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800642 }
643 }
644
645 @Override
646 public int getUidForSharedUser(String sharedUserName)
647 throws NameNotFoundException {
648 try {
649 int uid = mPM.getUidForSharedUser(sharedUserName);
650 if(uid != -1) {
651 return uid;
652 }
653 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700654 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800655 }
656 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
657 }
658
Kenny Roote6cd0c72011-05-19 12:48:14 -0700659 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800660 @Override
661 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700662 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700663 }
664
665 /** @hide */
666 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700667 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700668 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800669 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700670 ParceledListSlice<PackageInfo> parceledList =
671 mPM.getInstalledPackages(flags, userId);
672 if (parceledList == null) {
673 return Collections.emptyList();
674 }
675 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800676 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700677 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800678 }
679 }
680
Kenny Roote6cd0c72011-05-19 12:48:14 -0700681 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800682 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800683 public List<PackageInfo> getPackagesHoldingPermissions(
684 String[] permissions, int flags) {
685 final int userId = mContext.getUserId();
686 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700687 ParceledListSlice<PackageInfo> parceledList =
688 mPM.getPackagesHoldingPermissions(permissions, flags, userId);
689 if (parceledList == null) {
690 return Collections.emptyList();
691 }
692 return parceledList.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800693 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700694 throw e.rethrowFromSystemServer();
Dianne Hackborne7991752013-01-16 17:56:46 -0800695 }
696 }
697
698 @SuppressWarnings("unchecked")
699 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800700 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700701 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800702 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700703 ParceledListSlice<ApplicationInfo> parceledList =
704 mPM.getInstalledApplications(flags, userId);
705 if (parceledList == null) {
706 return Collections.emptyList();
707 }
708 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800709 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700710 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800711 }
712 }
713
Svet Ganov2acf0632015-11-24 19:10:59 -0800714 /** @hide */
715 @SuppressWarnings("unchecked")
716 @Override
717 public List<EphemeralApplicationInfo> getEphemeralApplications() {
718 try {
719 ParceledListSlice<EphemeralApplicationInfo> slice =
720 mPM.getEphemeralApplications(mContext.getUserId());
721 if (slice != null) {
722 return slice.getList();
723 }
724 return Collections.emptyList();
725 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700726 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800727 }
728 }
729
730 /** @hide */
731 @Override
732 public Drawable getEphemeralApplicationIcon(String packageName) {
733 try {
734 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
735 packageName, mContext.getUserId());
736 if (bitmap != null) {
737 return new BitmapDrawable(null, bitmap);
738 }
739 return null;
740 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700741 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800742 }
743 }
744
745 @Override
746 public boolean isEphemeralApplication() {
747 try {
748 return mPM.isEphemeralApplication(
749 mContext.getPackageName(), mContext.getUserId());
750 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700751 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800752 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800753 }
754
755 @Override
756 public int getEphemeralCookieMaxSizeBytes() {
757 return Settings.Global.getInt(mContext.getContentResolver(),
758 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
759 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
760 }
761
762 @Override
763 public @NonNull byte[] getEphemeralCookie() {
764 try {
765 final byte[] cookie = mPM.getEphemeralApplicationCookie(
766 mContext.getPackageName(), mContext.getUserId());
767 if (cookie != null) {
768 return cookie;
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700769 } else {
770 return EmptyArray.BYTE;
Svet Ganov2acf0632015-11-24 19:10:59 -0800771 }
772 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700773 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800774 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800775 }
776
777 @Override
778 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
779 try {
780 return mPM.setEphemeralApplicationCookie(
781 mContext.getPackageName(), cookie, mContext.getUserId());
782 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700783 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800784 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800785 }
786
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800787 @Override
788 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700789 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700790 }
791
792 @Override
793 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800794 try {
795 return mPM.resolveIntent(
796 intent,
797 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700798 flags,
799 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800800 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700801 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800802 }
803 }
804
805 @Override
806 public List<ResolveInfo> queryIntentActivities(Intent intent,
807 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700808 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700809 }
810
811 /** @hide Same as above but for a specific user */
812 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700813 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700814 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700815 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800816 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700817 ParceledListSlice<ResolveInfo> parceledList =
818 mPM.queryIntentActivities(intent,
819 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
820 flags, userId);
821 if (parceledList == null) {
822 return Collections.emptyList();
823 }
824 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800825 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700826 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800827 }
828 }
829
830 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700831 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800832 public List<ResolveInfo> queryIntentActivityOptions(
833 ComponentName caller, Intent[] specifics, Intent intent,
834 int flags) {
835 final ContentResolver resolver = mContext.getContentResolver();
836
837 String[] specificTypes = null;
838 if (specifics != null) {
839 final int N = specifics.length;
840 for (int i=0; i<N; i++) {
841 Intent sp = specifics[i];
842 if (sp != null) {
843 String t = sp.resolveTypeIfNeeded(resolver);
844 if (t != null) {
845 if (specificTypes == null) {
846 specificTypes = new String[N];
847 }
848 specificTypes[i] = t;
849 }
850 }
851 }
852 }
853
854 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700855 ParceledListSlice<ResolveInfo> parceledList =
856 mPM.queryIntentActivityOptions(caller, specifics, specificTypes, intent,
857 intent.resolveTypeIfNeeded(resolver), flags, mContext.getUserId());
858 if (parceledList == null) {
859 return Collections.emptyList();
860 }
861 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800862 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700863 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800864 }
865 }
866
Amith Yamasanif203aee2012-08-29 18:41:53 -0700867 /**
868 * @hide
869 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800870 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700871 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700872 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800873 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700874 ParceledListSlice<ResolveInfo> parceledList =
875 mPM.queryIntentReceivers(intent,
876 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
877 flags, userId);
878 if (parceledList == null) {
879 return Collections.emptyList();
880 }
881 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800882 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700883 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800884 }
885 }
886
887 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700888 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700889 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700890 }
891
892 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800893 public ResolveInfo resolveService(Intent intent, int flags) {
894 try {
895 return mPM.resolveService(
896 intent,
897 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700898 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700899 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800900 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700901 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800902 }
903 }
904
905 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700906 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700907 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800908 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700909 ParceledListSlice<ResolveInfo> parceledList =
910 mPM.queryIntentServices(intent,
911 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
912 flags, userId);
913 if (parceledList == null) {
914 return Collections.emptyList();
915 }
916 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800917 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700918 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800919 }
920 }
921
922 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700923 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700924 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700925 }
926
927 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700928 @SuppressWarnings("unchecked")
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700929 public List<ResolveInfo> queryIntentContentProvidersAsUser(
930 Intent intent, int flags, int userId) {
931 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700932 ParceledListSlice<ResolveInfo> parceledList =
933 mPM.queryIntentContentProviders(intent,
934 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
935 flags, userId);
936 if (parceledList == null) {
937 return Collections.emptyList();
938 }
939 return parceledList.getList();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700940 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700941 throw e.rethrowFromSystemServer();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700942 }
943 }
944
945 @Override
946 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
947 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
948 }
949
950 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100951 public ProviderInfo resolveContentProvider(String name, int flags) {
952 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
953 }
954
955 /** @hide **/
956 @Override
957 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800958 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100959 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800960 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700961 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800962 }
963 }
964
965 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700966 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800967 public List<ProviderInfo> queryContentProviders(String processName,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700968 int uid, int flags) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800969 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700970 ParceledListSlice<ProviderInfo> slice =
971 mPM.queryContentProviders(processName, uid, flags);
972 return slice != null ? slice.getList() : Collections.<ProviderInfo>emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800973 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700974 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800975 }
976 }
977
978 @Override
979 public InstrumentationInfo getInstrumentationInfo(
980 ComponentName className, int flags)
981 throws NameNotFoundException {
982 try {
983 InstrumentationInfo ii = mPM.getInstrumentationInfo(
984 className, flags);
985 if (ii != null) {
986 return ii;
987 }
988 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700989 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800990 }
991
992 throw new NameNotFoundException(className.toString());
993 }
994
995 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700996 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800997 public List<InstrumentationInfo> queryInstrumentation(
998 String targetPackage, int flags) {
999 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001000 ParceledListSlice<InstrumentationInfo> parceledList =
1001 mPM.queryInstrumentation(targetPackage, flags);
1002 if (parceledList == null) {
1003 return Collections.emptyList();
1004 }
1005 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001006 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001007 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001008 }
1009 }
1010
Alan Viveretteecd585a2015-04-13 10:32:51 -07001011 @Nullable
1012 @Override
1013 public Drawable getDrawable(String packageName, @DrawableRes int resId,
1014 @Nullable ApplicationInfo appInfo) {
1015 final ResourceName name = new ResourceName(packageName, resId);
1016 final Drawable cachedIcon = getCachedIcon(name);
1017 if (cachedIcon != null) {
1018 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001019 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001020
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001021 if (appInfo == null) {
1022 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001023 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001024 } catch (NameNotFoundException e) {
1025 return null;
1026 }
1027 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001028
1029 if (resId != 0) {
1030 try {
1031 final Resources r = getResourcesForApplication(appInfo);
1032 final Drawable dr = r.getDrawable(resId, null);
1033 if (dr != null) {
1034 putCachedIcon(name, dr);
1035 }
1036
1037 if (false) {
1038 RuntimeException e = new RuntimeException("here");
1039 e.fillInStackTrace();
1040 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
1041 + " from package " + packageName
1042 + ": app scale=" + r.getCompatibilityInfo().applicationScale
1043 + ", caller scale=" + mContext.getResources()
1044 .getCompatibilityInfo().applicationScale,
1045 e);
1046 }
Ricky Wai3ce46252015-04-15 16:12:22 +01001047 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001048 Log.v(TAG, "Getting drawable 0x"
1049 + Integer.toHexString(resId) + " from " + r
1050 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +01001051 }
1052 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -07001053 } catch (NameNotFoundException e) {
1054 Log.w("PackageManager", "Failure retrieving resources for "
1055 + appInfo.packageName);
1056 } catch (Resources.NotFoundException e) {
1057 Log.w("PackageManager", "Failure retrieving resources for "
1058 + appInfo.packageName + ": " + e.getMessage());
1059 } catch (Exception e) {
1060 // If an exception was thrown, fall through to return
1061 // default icon.
1062 Log.w("PackageManager", "Failure retrieving icon 0x"
1063 + Integer.toHexString(resId) + " in package "
1064 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001065 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001066 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001067
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001068 return null;
1069 }
1070
1071 @Override public Drawable getActivityIcon(ComponentName activityName)
1072 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001073 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001074 }
1075
1076 @Override public Drawable getActivityIcon(Intent intent)
1077 throws NameNotFoundException {
1078 if (intent.getComponent() != null) {
1079 return getActivityIcon(intent.getComponent());
1080 }
1081
1082 ResolveInfo info = resolveActivity(
1083 intent, PackageManager.MATCH_DEFAULT_ONLY);
1084 if (info != null) {
1085 return info.activityInfo.loadIcon(this);
1086 }
1087
Romain Guy39fe17c2011-11-30 10:34:07 -08001088 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001089 }
1090
1091 @Override public Drawable getDefaultActivityIcon() {
1092 return Resources.getSystem().getDrawable(
1093 com.android.internal.R.drawable.sym_def_app_icon);
1094 }
1095
1096 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1097 return info.loadIcon(this);
1098 }
1099
1100 @Override public Drawable getApplicationIcon(String packageName)
1101 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001102 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001103 }
1104
1105 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001106 public Drawable getActivityBanner(ComponentName activityName)
1107 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001108 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001109 }
1110
1111 @Override
1112 public Drawable getActivityBanner(Intent intent)
1113 throws NameNotFoundException {
1114 if (intent.getComponent() != null) {
1115 return getActivityBanner(intent.getComponent());
1116 }
1117
1118 ResolveInfo info = resolveActivity(
1119 intent, PackageManager.MATCH_DEFAULT_ONLY);
1120 if (info != null) {
1121 return info.activityInfo.loadBanner(this);
1122 }
1123
1124 throw new NameNotFoundException(intent.toUri(0));
1125 }
1126
1127 @Override
1128 public Drawable getApplicationBanner(ApplicationInfo info) {
1129 return info.loadBanner(this);
1130 }
1131
1132 @Override
1133 public Drawable getApplicationBanner(String packageName)
1134 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001135 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001136 }
1137
1138 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001139 public Drawable getActivityLogo(ComponentName activityName)
1140 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001141 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001142 }
1143
1144 @Override
1145 public Drawable getActivityLogo(Intent intent)
1146 throws NameNotFoundException {
1147 if (intent.getComponent() != null) {
1148 return getActivityLogo(intent.getComponent());
1149 }
1150
1151 ResolveInfo info = resolveActivity(
1152 intent, PackageManager.MATCH_DEFAULT_ONLY);
1153 if (info != null) {
1154 return info.activityInfo.loadLogo(this);
1155 }
1156
1157 throw new NameNotFoundException(intent.toUri(0));
1158 }
1159
1160 @Override
1161 public Drawable getApplicationLogo(ApplicationInfo info) {
1162 return info.loadLogo(this);
1163 }
1164
1165 @Override
1166 public Drawable getApplicationLogo(String packageName)
1167 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001168 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001169 }
1170
Svetoslavc7d62f02014-09-04 15:39:54 -07001171 @Override
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001172 public Drawable getManagedUserBadgedDrawable(Drawable drawable, Rect badgeLocation,
1173 int badgeDensity) {
1174 Drawable badgeDrawable = getDrawableForDensity(
1175 com.android.internal.R.drawable.ic_corp_badge, badgeDensity);
1176 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1177 }
1178
1179 @Override
Svetoslavc7d62f02014-09-04 15:39:54 -07001180 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1181 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1182 if (badgeResId == 0) {
1183 return icon;
1184 }
1185 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1186 return getBadgedDrawable(icon, badgeIcon, null, true);
1187 }
1188
1189 @Override
1190 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1191 Rect badgeLocation, int badgeDensity) {
1192 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1193 if (badgeDrawable == null) {
1194 return drawable;
1195 }
1196 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1197 }
1198
1199 @Override
1200 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001201 return getManagedProfileIconForDensity(user, com.android.internal.R.drawable.ic_corp_badge,
1202 density);
Selim Cineke6ff9462016-01-15 15:07:06 -08001203 }
1204
1205 @Override
1206 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001207 return getManagedProfileIconForDensity(user,
1208 com.android.internal.R.drawable.ic_corp_badge_no_background, density);
Selim Cineke6ff9462016-01-15 15:07:06 -08001209 }
1210
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001211 private Drawable getDrawableForDensity(int drawableId, int density) {
1212 if (density <= 0) {
1213 density = mContext.getResources().getDisplayMetrics().densityDpi;
1214 }
1215 return Resources.getSystem().getDrawableForDensity(drawableId, density);
1216 }
1217
1218 private Drawable getManagedProfileIconForDensity(UserHandle user, int drawableId, int density) {
Tony Mak8673b282016-03-21 21:10:59 +00001219 if (isManagedProfile(user.getIdentifier())) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001220 return getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001221 }
1222 return null;
1223 }
1224
1225 @Override
1226 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
Tony Mak8673b282016-03-21 21:10:59 +00001227 if (isManagedProfile(user.getIdentifier())) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001228 return Resources.getSystem().getString(
1229 com.android.internal.R.string.managed_profile_label_badge, label);
1230 }
1231 return label;
1232 }
1233
Alan Viveretteecd585a2015-04-13 10:32:51 -07001234 @Override
1235 public Resources getResourcesForActivity(ComponentName activityName)
1236 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001237 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001238 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001239 }
1240
Alan Viveretteecd585a2015-04-13 10:32:51 -07001241 @Override
1242 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1243 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001244 if (app.packageName.equals("system")) {
1245 return mContext.mMainThread.getSystemContext().getResources();
1246 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001247 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001248 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001249 sameUid ? app.sourceDir : app.publicSourceDir,
1250 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001251 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
Adam Lesinski082614c2016-03-04 14:33:47 -08001252 mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001253 if (r != null) {
1254 return r;
1255 }
1256 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1257 }
1258
Alan Viveretteecd585a2015-04-13 10:32:51 -07001259 @Override
1260 public Resources getResourcesForApplication(String appPackageName)
1261 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001262 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001263 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001264 }
1265
Amith Yamasani98edc952012-09-25 14:09:27 -07001266 /** @hide */
1267 @Override
1268 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1269 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001270 if (userId < 0) {
1271 throw new IllegalArgumentException(
1272 "Call does not support special user #" + userId);
1273 }
1274 if ("system".equals(appPackageName)) {
1275 return mContext.mMainThread.getSystemContext().getResources();
1276 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001277 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001278 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001279 if (ai != null) {
1280 return getResourcesForApplication(ai);
1281 }
1282 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001283 throw e.rethrowFromSystemServer();
Amith Yamasani98edc952012-09-25 14:09:27 -07001284 }
1285 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1286 }
1287
Jeff Sharkeycd654482016-01-08 17:42:11 -07001288 volatile int mCachedSafeMode = -1;
1289
1290 @Override
1291 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001292 try {
1293 if (mCachedSafeMode < 0) {
1294 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1295 }
1296 return mCachedSafeMode != 0;
1297 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001298 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001299 }
1300 }
1301
Svetoslavf7c06eb2015-06-10 18:43:22 -07001302 @Override
1303 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1304 synchronized (mPermissionListeners) {
1305 if (mPermissionListeners.get(listener) != null) {
1306 return;
1307 }
1308 OnPermissionsChangeListenerDelegate delegate =
1309 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1310 try {
1311 mPM.addOnPermissionsChangeListener(delegate);
1312 mPermissionListeners.put(listener, delegate);
1313 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001314 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001315 }
1316 }
1317 }
1318
1319 @Override
1320 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1321 synchronized (mPermissionListeners) {
1322 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1323 if (delegate != null) {
1324 try {
1325 mPM.removeOnPermissionsChangeListener(delegate);
1326 mPermissionListeners.remove(listener);
1327 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001328 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001329 }
1330 }
1331 }
1332 }
1333
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001334 static void configurationChanged() {
1335 synchronized (sSync) {
1336 sIconCache.clear();
1337 sStringCache.clear();
1338 }
1339 }
1340
1341 ApplicationPackageManager(ContextImpl context,
1342 IPackageManager pm) {
1343 mContext = context;
1344 mPM = pm;
1345 }
1346
Alan Viveretteecd585a2015-04-13 10:32:51 -07001347 @Nullable
1348 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001349 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001350 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001351 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1352 + name + ": " + wr);
1353 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001354 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001355 if (state != null) {
1356 if (DEBUG_ICONS) {
1357 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1358 }
1359 // Note: It's okay here to not use the newDrawable(Resources) variant
1360 // of the API. The ConstantState comes from a drawable that was
1361 // originally created by passing the proper app Resources instance
1362 // which means the state should already contain the proper
1363 // resources specific information (like density.) See
1364 // BitmapDrawable.BitmapState for instance.
1365 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001366 }
1367 // our entry has been purged
1368 sIconCache.remove(name);
1369 }
1370 }
1371 return null;
1372 }
1373
Alan Viveretteecd585a2015-04-13 10:32:51 -07001374 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001375 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001376 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001377 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001378 }
1379 }
1380
Romain Guy39fe17c2011-11-30 10:34:07 -08001381 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001382 boolean immediateGc = false;
1383 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1384 immediateGc = true;
1385 }
1386 if (pkgList != null && (pkgList.length > 0)) {
1387 boolean needCleanup = false;
1388 for (String ssp : pkgList) {
1389 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001390 for (int i=sIconCache.size()-1; i>=0; i--) {
1391 ResourceName nm = sIconCache.keyAt(i);
1392 if (nm.packageName.equals(ssp)) {
1393 //Log.i(TAG, "Removing cached drawable for " + nm);
1394 sIconCache.removeAt(i);
1395 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001396 }
1397 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001398 for (int i=sStringCache.size()-1; i>=0; i--) {
1399 ResourceName nm = sStringCache.keyAt(i);
1400 if (nm.packageName.equals(ssp)) {
1401 //Log.i(TAG, "Removing cached string for " + nm);
1402 sStringCache.removeAt(i);
1403 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001404 }
1405 }
1406 }
1407 }
1408 if (needCleanup || hasPkgInfo) {
1409 if (immediateGc) {
1410 // Schedule an immediate gc.
1411 Runtime.getRuntime().gc();
1412 } else {
1413 ActivityThread.currentActivityThread().scheduleGcIdler();
1414 }
1415 }
1416 }
1417 }
1418
1419 private static final class ResourceName {
1420 final String packageName;
1421 final int iconId;
1422
1423 ResourceName(String _packageName, int _iconId) {
1424 packageName = _packageName;
1425 iconId = _iconId;
1426 }
1427
1428 ResourceName(ApplicationInfo aInfo, int _iconId) {
1429 this(aInfo.packageName, _iconId);
1430 }
1431
1432 ResourceName(ComponentInfo cInfo, int _iconId) {
1433 this(cInfo.applicationInfo.packageName, _iconId);
1434 }
1435
1436 ResourceName(ResolveInfo rInfo, int _iconId) {
1437 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1438 }
1439
1440 @Override
1441 public boolean equals(Object o) {
1442 if (this == o) return true;
1443 if (o == null || getClass() != o.getClass()) return false;
1444
1445 ResourceName that = (ResourceName) o;
1446
1447 if (iconId != that.iconId) return false;
1448 return !(packageName != null ?
1449 !packageName.equals(that.packageName) : that.packageName != null);
1450
1451 }
1452
1453 @Override
1454 public int hashCode() {
1455 int result;
1456 result = packageName.hashCode();
1457 result = 31 * result + iconId;
1458 return result;
1459 }
1460
1461 @Override
1462 public String toString() {
1463 return "{ResourceName " + packageName + " / " + iconId + "}";
1464 }
1465 }
1466
1467 private CharSequence getCachedString(ResourceName name) {
1468 synchronized (sSync) {
1469 WeakReference<CharSequence> wr = sStringCache.get(name);
1470 if (wr != null) { // we have the activity
1471 CharSequence cs = wr.get();
1472 if (cs != null) {
1473 return cs;
1474 }
1475 // our entry has been purged
1476 sStringCache.remove(name);
1477 }
1478 }
1479 return null;
1480 }
1481
1482 private void putCachedString(ResourceName name, CharSequence cs) {
1483 synchronized (sSync) {
1484 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1485 }
1486 }
1487
1488 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001489 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001490 ApplicationInfo appInfo) {
1491 ResourceName name = new ResourceName(packageName, resid);
1492 CharSequence text = getCachedString(name);
1493 if (text != null) {
1494 return text;
1495 }
1496 if (appInfo == null) {
1497 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001498 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001499 } catch (NameNotFoundException e) {
1500 return null;
1501 }
1502 }
1503 try {
1504 Resources r = getResourcesForApplication(appInfo);
1505 text = r.getText(resid);
1506 putCachedString(name, text);
1507 return text;
1508 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001509 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001510 + appInfo.packageName);
1511 } catch (RuntimeException e) {
1512 // If an exception was thrown, fall through to return
1513 // default icon.
1514 Log.w("PackageManager", "Failure retrieving text 0x"
1515 + Integer.toHexString(resid) + " in package "
1516 + packageName, e);
1517 }
1518 return null;
1519 }
1520
1521 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001522 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001523 ApplicationInfo appInfo) {
1524 if (appInfo == null) {
1525 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001526 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001527 } catch (NameNotFoundException e) {
1528 return null;
1529 }
1530 }
1531 try {
1532 Resources r = getResourcesForApplication(appInfo);
1533 return r.getXml(resid);
1534 } catch (RuntimeException e) {
1535 // If an exception was thrown, fall through to return
1536 // default icon.
1537 Log.w("PackageManager", "Failure retrieving xml 0x"
1538 + Integer.toHexString(resid) + " in package "
1539 + packageName, e);
1540 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001541 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001542 + appInfo.packageName);
1543 }
1544 return null;
1545 }
1546
1547 @Override
1548 public CharSequence getApplicationLabel(ApplicationInfo info) {
1549 return info.loadLabel(this);
1550 }
1551
1552 @Override
1553 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1554 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001555 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Todd Kennedya6793232016-02-24 22:46:00 +00001556 installerPackageName, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001557 }
1558
Christopher Tatef1977b42014-03-24 16:25:51 -07001559 @Override
1560 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1561 int flags, String installerPackageName) {
Todd Kennedya6793232016-02-24 22:46:00 +00001562 installCommon(packageURI, observer, flags, installerPackageName, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001563 }
1564
1565 private void installCommon(Uri packageURI,
1566 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001567 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001568 if (!"file".equals(packageURI.getScheme())) {
1569 throw new UnsupportedOperationException("Only file:// URIs are supported");
1570 }
Jeff Sharkey513a0742014-07-08 17:10:32 -07001571
1572 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001573 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001574 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
Todd Kennedya6793232016-02-24 22:46:00 +00001575 userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001576 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001577 throw e.rethrowFromSystemServer();
rich cannings706e8ba2012-08-20 13:20:14 -07001578 }
1579 }
1580
1581 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001582 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001583 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001584 }
1585
1586 @Override
1587 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001588 throws NameNotFoundException {
1589 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001590 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001591 if (res == INSTALL_FAILED_INVALID_URI) {
1592 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1593 }
1594 return res;
1595 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001596 throw e.rethrowFromSystemServer();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001597 }
1598 }
1599
1600 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001601 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001602 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001603 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001604 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001605 throw e.rethrowFromSystemServer();
Kenny Root5ab21572011-07-27 11:11:19 -07001606 }
1607 }
1608
1609 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001610 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1611 long millisecondsToDelay) {
1612 try {
1613 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1614 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001615 throw e.rethrowFromSystemServer();
rich canningsd9ef3e52012-08-22 14:28:05 -07001616 }
1617 }
1618
1619 @Override
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001620 public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001621 try {
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001622 mPM.verifyIntentFilter(id, verificationCode, failedDomains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001623 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001624 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001625 }
1626 }
1627
1628 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001629 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001630 try {
1631 return mPM.getIntentVerificationStatus(packageName, userId);
1632 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001633 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001634 }
1635 }
1636
1637 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001638 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001639 try {
1640 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1641 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001642 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001643 }
1644 }
1645
1646 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001647 @SuppressWarnings("unchecked")
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001648 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1649 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001650 ParceledListSlice<IntentFilterVerificationInfo> parceledList =
1651 mPM.getIntentFilterVerifications(packageName);
1652 if (parceledList == null) {
1653 return Collections.emptyList();
1654 }
1655 return parceledList.getList();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001656 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001657 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001658 }
1659 }
1660
1661 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001662 @SuppressWarnings("unchecked")
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001663 public List<IntentFilter> getAllIntentFilters(String packageName) {
1664 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001665 ParceledListSlice<IntentFilter> parceledList =
1666 mPM.getAllIntentFilters(packageName);
1667 if (parceledList == null) {
1668 return Collections.emptyList();
1669 }
1670 return parceledList.getList();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001671 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001672 throw e.rethrowFromSystemServer();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001673 }
1674 }
1675
1676 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001677 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001678 try {
1679 return mPM.getDefaultBrowserPackageName(userId);
1680 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001681 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001682 }
1683 }
1684
1685 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001686 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001687 try {
1688 return mPM.setDefaultBrowserPackageName(packageName, userId);
1689 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001690 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001691 }
1692 }
1693
1694 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001695 public void setInstallerPackageName(String targetPackage,
1696 String installerPackageName) {
1697 try {
1698 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1699 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001700 throw e.rethrowFromSystemServer();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001701 }
1702 }
1703
1704 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001705 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001706 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001707 return mPM.getInstallerPackageName(packageName);
1708 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001709 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001710 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001711 }
1712
1713 @Override
1714 public int getMoveStatus(int moveId) {
1715 try {
1716 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001717 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001718 throw e.rethrowFromSystemServer();
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001719 }
1720 }
1721
1722 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001723 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1724 synchronized (mDelegates) {
1725 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1726 handler.getLooper());
1727 try {
1728 mPM.registerMoveCallback(delegate);
1729 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001730 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001731 }
1732 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001733 }
1734 }
1735
1736 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001737 public void unregisterMoveCallback(MoveCallback callback) {
1738 synchronized (mDelegates) {
1739 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1740 final MoveCallbackDelegate delegate = i.next();
1741 if (delegate.mCallback == callback) {
1742 try {
1743 mPM.unregisterMoveCallback(delegate);
1744 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001745 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001746 }
1747 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001748 }
1749 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001750 }
1751 }
1752
1753 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001754 public int movePackage(String packageName, VolumeInfo vol) {
1755 try {
1756 final String volumeUuid;
1757 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1758 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1759 } else if (vol.isPrimaryPhysical()) {
1760 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1761 } else {
1762 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1763 }
1764
1765 return mPM.movePackage(packageName, volumeUuid);
1766 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001767 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001768 }
1769 }
1770
1771 @Override
1772 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001773 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001774 if (app.isInternal()) {
1775 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1776 } else if (app.isExternalAsec()) {
1777 return storage.getPrimaryPhysicalVolume();
1778 } else {
1779 return storage.findVolumeByUuid(app.volumeUuid);
1780 }
1781 }
1782
1783 @Override
1784 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1785 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1786 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001787 final List<VolumeInfo> vols = storage.getVolumes();
1788 final List<VolumeInfo> candidates = new ArrayList<>();
1789 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001790 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001791 candidates.add(vol);
1792 }
1793 }
1794 return candidates;
1795 }
1796
Makoto Onukif34db0a2016-02-17 11:17:15 -08001797 private boolean isPackageCandidateVolume(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001798 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1799 final boolean forceAllowOnExternal = Settings.Global.getInt(
1800 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001801 // Private internal is always an option
1802 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1803 return true;
1804 }
1805
1806 // System apps and apps demanding internal storage can't be moved
1807 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001808 if (app.isSystemApp()) {
1809 return false;
1810 }
1811 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001812 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1813 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001814 return false;
1815 }
1816
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001817 // Gotta be able to write there
1818 if (!vol.isMountedWritable()) {
1819 return false;
1820 }
1821
1822 // Moving into an ASEC on public primary is only option internal
1823 if (vol.isPrimaryPhysical()) {
1824 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001825 }
1826
Makoto Onukif34db0a2016-02-17 11:17:15 -08001827 // Some apps can't be moved. (e.g. device admins)
1828 try {
1829 if (mPM.isPackageDeviceAdminOnAnyUser(app.packageName)) {
1830 return false;
1831 }
1832 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001833 throw e.rethrowFromSystemServer();
Makoto Onukif34db0a2016-02-17 11:17:15 -08001834 }
1835
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001836 // Otherwise we can move to any private volume
1837 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1838 }
1839
1840 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001841 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001842 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001843 final String volumeUuid;
1844 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1845 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1846 } else if (vol.isPrimaryPhysical()) {
1847 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1848 } else {
1849 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1850 }
1851
1852 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001853 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001854 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001855 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001856 }
1857
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001858 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001859 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1860 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1861 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001862 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001863 }
1864
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001865 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001866 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1867 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1868 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1869 final List<VolumeInfo> vols = storage.getVolumes();
1870 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001871 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1872 storage.getPrimaryStorageUuid()) && currentVol != null) {
1873 // TODO: support moving primary physical to emulated volume
1874 candidates.add(currentVol);
1875 } else {
1876 for (VolumeInfo vol : vols) {
1877 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1878 candidates.add(vol);
1879 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001880 }
1881 }
1882 return candidates;
1883 }
1884
1885 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1886 // Private internal is always an option
1887 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1888 return true;
1889 }
1890
1891 // Gotta be able to write there
1892 if (!vol.isMountedWritable()) {
1893 return false;
1894 }
1895
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001896 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001897 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001898 }
1899
1900 @Override
1901 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001902 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001903 }
1904
1905 @Override
1906 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1907 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001908 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001909 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001910 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001911 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001912 }
1913 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001914
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001915 @Override
1916 public void clearApplicationUserData(String packageName,
1917 IPackageDataObserver observer) {
1918 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001919 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001920 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001921 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001922 }
1923 }
1924 @Override
1925 public void deleteApplicationCacheFiles(String packageName,
1926 IPackageDataObserver observer) {
1927 try {
1928 mPM.deleteApplicationCacheFiles(packageName, observer);
1929 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001930 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001931 }
1932 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001933
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001934 @Override
Suprabh Shukla78c9eb82016-04-12 15:51:35 -07001935 public void deleteApplicationCacheFilesAsUser(String packageName, int userId,
1936 IPackageDataObserver observer) {
1937 try {
1938 mPM.deleteApplicationCacheFilesAsUser(packageName, userId, observer);
1939 } catch (RemoteException e) {
1940 throw e.rethrowFromSystemServer();
1941 }
1942 }
1943
1944 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001945 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1946 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001947 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001948 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001949 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001950 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001951 }
1952 }
1953
1954 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001955 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001956 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001957 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001958 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001959 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001960 }
1961 }
1962
1963 @Override
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001964 public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended,
1965 int userId) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001966 try {
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001967 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, userId);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001968 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001969 throw e.rethrowFromSystemServer();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001970 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001971 }
1972
1973 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001974 public boolean isPackageSuspendedForUser(String packageName, int userId) {
1975 try {
1976 return mPM.isPackageSuspendedForUser(packageName, userId);
1977 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001978 throw e.rethrowFromSystemServer();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001979 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001980 }
1981
1982 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001983 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001984 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001985 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001986 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001987 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001988 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001989 }
1990 }
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001991
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001992 @Override
1993 public void addPackageToPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001994 Log.w(TAG, "addPackageToPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001995 }
1996
1997 @Override
1998 public void removePackageFromPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001999 Log.w(TAG, "removePackageFromPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002000 }
2001
2002 @Override
2003 public List<PackageInfo> getPreferredPackages(int flags) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002004 Log.w(TAG, "getPreferredPackages() is a no-op");
2005 return Collections.emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002006 }
2007
2008 @Override
2009 public void addPreferredActivity(IntentFilter filter,
2010 int match, ComponentName[] set, ComponentName activity) {
2011 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002012 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07002013 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002014 throw e.rethrowFromSystemServer();
Amith Yamasania3f133a2012-08-09 17:11:28 -07002015 }
2016 }
2017
2018 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002019 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07002020 ComponentName[] set, ComponentName activity, int userId) {
2021 try {
2022 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002023 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002024 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002025 }
2026 }
2027
2028 @Override
2029 public void replacePreferredActivity(IntentFilter filter,
2030 int match, ComponentName[] set, ComponentName activity) {
2031 try {
Robin Lee0e27c872015-09-28 14:37:40 +01002032 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002033 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002034 throw e.rethrowFromSystemServer();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002035 }
2036 }
2037
2038 @Override
2039 public void replacePreferredActivityAsUser(IntentFilter filter,
2040 int match, ComponentName[] set, ComponentName activity,
2041 int userId) {
2042 try {
2043 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002044 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002045 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002046 }
2047 }
2048
2049 @Override
2050 public void clearPackagePreferredActivities(String packageName) {
2051 try {
2052 mPM.clearPackagePreferredActivities(packageName);
2053 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002054 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002055 }
2056 }
2057
2058 @Override
2059 public int getPreferredActivities(List<IntentFilter> outFilters,
2060 List<ComponentName> outActivities, String packageName) {
2061 try {
2062 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2063 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002064 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002065 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002066 }
2067
2068 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002069 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2070 try {
2071 return mPM.getHomeActivities(outActivities);
2072 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002073 throw e.rethrowFromSystemServer();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002074 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002075 }
2076
2077 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002078 public void setComponentEnabledSetting(ComponentName componentName,
2079 int newState, int flags) {
2080 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002081 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002082 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002083 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002084 }
2085 }
2086
2087 @Override
2088 public int getComponentEnabledSetting(ComponentName componentName) {
2089 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002090 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
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 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002094 }
2095
2096 @Override
2097 public void setApplicationEnabledSetting(String packageName,
2098 int newState, int flags) {
2099 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002100 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002101 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002102 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002103 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002104 }
2105 }
2106
2107 @Override
2108 public int getApplicationEnabledSetting(String packageName) {
2109 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002110 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002111 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002112 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002113 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002114 }
2115
Amith Yamasani655d0e22013-06-12 14:19:10 -07002116 @Override
Sudheer Shankabbb3ff22015-07-09 15:39:23 +01002117 public void flushPackageRestrictionsAsUser(int userId) {
2118 try {
2119 mPM.flushPackageRestrictionsAsUser(userId);
2120 } catch (RemoteException e) {
2121 throw e.rethrowFromSystemServer();
2122 }
2123 }
2124
2125 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002126 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002127 UserHandle user) {
2128 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002129 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002130 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002131 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002132 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002133 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002134 }
2135
2136 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002137 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002138 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002139 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002140 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002141 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002142 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002143 }
2144
dcashmanc6f22492014-08-14 09:54:51 -07002145 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002146 @Override
2147 public KeySet getKeySetByAlias(String packageName, String alias) {
2148 Preconditions.checkNotNull(packageName);
2149 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002150 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002151 return mPM.getKeySetByAlias(packageName, alias);
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 KeySet getSigningKeySet(String packageName) {
2160 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002161 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002162 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002163 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002164 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002165 }
dcashman9d2f4412014-06-09 09:27:54 -07002166 }
2167
dcashmanc6f22492014-08-14 09:54:51 -07002168 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002169 @Override
2170 public boolean isSignedBy(String packageName, KeySet ks) {
2171 Preconditions.checkNotNull(packageName);
2172 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002173 try {
dcashmanc6f22492014-08-14 09:54:51 -07002174 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002175 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002176 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002177 }
2178 }
2179
dcashmanc6f22492014-08-14 09:54:51 -07002180 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002181 @Override
2182 public boolean isSignedByExactly(String packageName, KeySet ks) {
2183 Preconditions.checkNotNull(packageName);
2184 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002185 try {
dcashmanc6f22492014-08-14 09:54:51 -07002186 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002187 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002188 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002189 }
2190 }
2191
Kenny Root0aaa0d92011-09-12 16:42:55 -07002192 /**
2193 * @hide
2194 */
2195 @Override
2196 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2197 try {
2198 return mPM.getVerifierDeviceIdentity();
2199 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002200 throw e.rethrowFromSystemServer();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002201 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002202 }
2203
Jeff Hao9f60c082014-10-28 18:51:07 -07002204 /**
2205 * @hide
2206 */
2207 @Override
2208 public boolean isUpgrade() {
2209 try {
2210 return mPM.isUpgrade();
2211 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002212 throw e.rethrowFromSystemServer();
Jeff Hao9f60c082014-10-28 18:51:07 -07002213 }
2214 }
2215
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002216 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002217 public PackageInstaller getPackageInstaller() {
2218 synchronized (mLock) {
2219 if (mInstaller == null) {
2220 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002221 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002222 mContext.getPackageName(), mContext.getUserId());
2223 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002224 throw e.rethrowFromSystemServer();
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002225 }
2226 }
2227 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002228 }
2229 }
2230
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002231 @Override
2232 public boolean isPackageAvailable(String packageName) {
2233 try {
2234 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2235 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002236 throw e.rethrowFromSystemServer();
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002237 }
2238 }
2239
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002240 /**
2241 * @hide
2242 */
2243 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002244 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2245 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002246 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002247 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002248 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002249 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002250 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002251 }
2252 }
2253
2254 /**
2255 * @hide
2256 */
2257 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002258 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002259 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002260 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002261 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002262 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002263 }
2264 }
2265
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002266 /**
2267 * @hide
2268 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002269 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002270 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2271 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2272 return dr;
2273 }
2274 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2275 }
2276
2277 /**
2278 * @hide
2279 */
2280 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002281 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002282 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2283 if (bitmap == null) {
2284 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2285 }
2286 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002287 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002288 Drawable dr = null;
2289 if (itemInfo.packageName != null) {
2290 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2291 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002292 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002293 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002294 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002295 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002296 }
2297
2298 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2299 Rect badgeLocation, boolean tryBadgeInPlace) {
2300 final int badgedWidth = drawable.getIntrinsicWidth();
2301 final int badgedHeight = drawable.getIntrinsicHeight();
2302 final boolean canBadgeInPlace = tryBadgeInPlace
2303 && (drawable instanceof BitmapDrawable)
2304 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2305
2306 final Bitmap bitmap;
2307 if (canBadgeInPlace) {
2308 bitmap = ((BitmapDrawable) drawable).getBitmap();
2309 } else {
2310 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2311 }
2312 Canvas canvas = new Canvas(bitmap);
2313
2314 if (!canBadgeInPlace) {
2315 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2316 drawable.draw(canvas);
2317 }
2318
2319 if (badgeLocation != null) {
2320 if (badgeLocation.left < 0 || badgeLocation.top < 0
2321 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2322 throw new IllegalArgumentException("Badge location " + badgeLocation
2323 + " not in badged drawable bounds "
2324 + new Rect(0, 0, badgedWidth, badgedHeight));
2325 }
2326 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2327
2328 canvas.save();
2329 canvas.translate(badgeLocation.left, badgeLocation.top);
2330 badgeDrawable.draw(canvas);
2331 canvas.restore();
2332 } else {
2333 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2334 badgeDrawable.draw(canvas);
2335 }
2336
2337 if (!canBadgeInPlace) {
2338 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2339
2340 if (drawable instanceof BitmapDrawable) {
2341 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2342 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2343 }
2344
2345 return mergedDrawable;
2346 }
2347
2348 return drawable;
2349 }
2350
Tony Mak8673b282016-03-21 21:10:59 +00002351 private int getBadgeResIdForUser(int userId) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002352 // Return the framework-provided badge.
Tony Mak8673b282016-03-21 21:10:59 +00002353 if (isManagedProfile(userId)) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002354 return com.android.internal.R.drawable.ic_corp_icon_badge;
2355 }
2356 return 0;
2357 }
2358
Tony Mak8673b282016-03-21 21:10:59 +00002359 private boolean isManagedProfile(int userId) {
2360 return getUserManager().isManagedProfile(userId);
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002361 }
2362
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002363 /** {@hide} */
2364 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2365 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002366 private static final int MSG_CREATED = 1;
2367 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002368
2369 final MoveCallback mCallback;
2370 final Handler mHandler;
2371
2372 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2373 mCallback = callback;
2374 mHandler = new Handler(looper, this);
2375 }
2376
2377 @Override
2378 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002379 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002380 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002381 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002382 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002383 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002384 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002385 }
2386 case MSG_STATUS_CHANGED: {
2387 final SomeArgs args = (SomeArgs) msg.obj;
2388 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2389 args.recycle();
2390 return true;
2391 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002392 }
2393 return false;
2394 }
2395
2396 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002397 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002398 final SomeArgs args = SomeArgs.obtain();
2399 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002400 args.arg2 = extras;
2401 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2402 }
2403
2404 @Override
2405 public void onStatusChanged(int moveId, int status, long estMillis) {
2406 final SomeArgs args = SomeArgs.obtain();
2407 args.argi1 = moveId;
2408 args.argi2 = status;
2409 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002410 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002411 }
2412 }
2413
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002414 private final ContextImpl mContext;
2415 private final IPackageManager mPM;
2416
2417 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002418 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2419 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2420 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2421 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002422
2423 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2424 mPermissionListeners = new ArrayMap<>();
2425
2426 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2427 implements Handler.Callback{
2428 private static final int MSG_PERMISSIONS_CHANGED = 1;
2429
2430 private final OnPermissionsChangedListener mListener;
2431 private final Handler mHandler;
2432
2433
2434 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2435 Looper looper) {
2436 mListener = listener;
2437 mHandler = new Handler(looper, this);
2438 }
2439
2440 @Override
2441 public void onPermissionsChanged(int uid) {
2442 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2443 }
2444
2445 @Override
2446 public boolean handleMessage(Message msg) {
2447 switch (msg.what) {
2448 case MSG_PERMISSIONS_CHANGED: {
2449 final int uid = msg.arg1;
2450 mListener.onPermissionsChanged(uid);
2451 return true;
2452 }
2453 }
2454 return false;
2455 }
2456 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002457}