blob: 3b3e0704606e95f414f2348e722603a206ac2eff [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());
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001248 final Resources r = mContext.mMainThread.getTopLevelResources(
Adam Lesinskic82f28a2016-06-08 17:19:09 -07001249 sameUid ? app.sourceDir : app.publicSourceDir,
1250 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
1251 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1252 mContext.mPackageInfo);
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001253 if (r != null) {
1254 return r;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001255 }
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001256 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1257
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001258 }
1259
Alan Viveretteecd585a2015-04-13 10:32:51 -07001260 @Override
1261 public Resources getResourcesForApplication(String appPackageName)
1262 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001263 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001264 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001265 }
1266
Amith Yamasani98edc952012-09-25 14:09:27 -07001267 /** @hide */
1268 @Override
1269 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1270 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001271 if (userId < 0) {
1272 throw new IllegalArgumentException(
1273 "Call does not support special user #" + userId);
1274 }
1275 if ("system".equals(appPackageName)) {
1276 return mContext.mMainThread.getSystemContext().getResources();
1277 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001278 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001279 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001280 if (ai != null) {
1281 return getResourcesForApplication(ai);
1282 }
1283 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001284 throw e.rethrowFromSystemServer();
Amith Yamasani98edc952012-09-25 14:09:27 -07001285 }
1286 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1287 }
1288
Jeff Sharkeycd654482016-01-08 17:42:11 -07001289 volatile int mCachedSafeMode = -1;
1290
1291 @Override
1292 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001293 try {
1294 if (mCachedSafeMode < 0) {
1295 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1296 }
1297 return mCachedSafeMode != 0;
1298 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001299 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001300 }
1301 }
1302
Svetoslavf7c06eb2015-06-10 18:43:22 -07001303 @Override
1304 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1305 synchronized (mPermissionListeners) {
1306 if (mPermissionListeners.get(listener) != null) {
1307 return;
1308 }
1309 OnPermissionsChangeListenerDelegate delegate =
1310 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1311 try {
1312 mPM.addOnPermissionsChangeListener(delegate);
1313 mPermissionListeners.put(listener, delegate);
1314 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001315 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001316 }
1317 }
1318 }
1319
1320 @Override
1321 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1322 synchronized (mPermissionListeners) {
1323 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1324 if (delegate != null) {
1325 try {
1326 mPM.removeOnPermissionsChangeListener(delegate);
1327 mPermissionListeners.remove(listener);
1328 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001329 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001330 }
1331 }
1332 }
1333 }
1334
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001335 static void configurationChanged() {
1336 synchronized (sSync) {
1337 sIconCache.clear();
1338 sStringCache.clear();
1339 }
1340 }
1341
1342 ApplicationPackageManager(ContextImpl context,
1343 IPackageManager pm) {
1344 mContext = context;
1345 mPM = pm;
1346 }
1347
Alan Viveretteecd585a2015-04-13 10:32:51 -07001348 @Nullable
1349 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001350 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001351 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001352 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1353 + name + ": " + wr);
1354 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001355 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001356 if (state != null) {
1357 if (DEBUG_ICONS) {
1358 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1359 }
1360 // Note: It's okay here to not use the newDrawable(Resources) variant
1361 // of the API. The ConstantState comes from a drawable that was
1362 // originally created by passing the proper app Resources instance
1363 // which means the state should already contain the proper
1364 // resources specific information (like density.) See
1365 // BitmapDrawable.BitmapState for instance.
1366 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001367 }
1368 // our entry has been purged
1369 sIconCache.remove(name);
1370 }
1371 }
1372 return null;
1373 }
1374
Alan Viveretteecd585a2015-04-13 10:32:51 -07001375 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001376 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001377 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001378 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001379 }
1380 }
1381
Romain Guy39fe17c2011-11-30 10:34:07 -08001382 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001383 boolean immediateGc = false;
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001384 if (cmd == ApplicationThreadConstants.EXTERNAL_STORAGE_UNAVAILABLE) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001385 immediateGc = true;
1386 }
1387 if (pkgList != null && (pkgList.length > 0)) {
1388 boolean needCleanup = false;
1389 for (String ssp : pkgList) {
1390 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001391 for (int i=sIconCache.size()-1; i>=0; i--) {
1392 ResourceName nm = sIconCache.keyAt(i);
1393 if (nm.packageName.equals(ssp)) {
1394 //Log.i(TAG, "Removing cached drawable for " + nm);
1395 sIconCache.removeAt(i);
1396 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001397 }
1398 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001399 for (int i=sStringCache.size()-1; i>=0; i--) {
1400 ResourceName nm = sStringCache.keyAt(i);
1401 if (nm.packageName.equals(ssp)) {
1402 //Log.i(TAG, "Removing cached string for " + nm);
1403 sStringCache.removeAt(i);
1404 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001405 }
1406 }
1407 }
1408 }
1409 if (needCleanup || hasPkgInfo) {
1410 if (immediateGc) {
1411 // Schedule an immediate gc.
1412 Runtime.getRuntime().gc();
1413 } else {
1414 ActivityThread.currentActivityThread().scheduleGcIdler();
1415 }
1416 }
1417 }
1418 }
1419
1420 private static final class ResourceName {
1421 final String packageName;
1422 final int iconId;
1423
1424 ResourceName(String _packageName, int _iconId) {
1425 packageName = _packageName;
1426 iconId = _iconId;
1427 }
1428
1429 ResourceName(ApplicationInfo aInfo, int _iconId) {
1430 this(aInfo.packageName, _iconId);
1431 }
1432
1433 ResourceName(ComponentInfo cInfo, int _iconId) {
1434 this(cInfo.applicationInfo.packageName, _iconId);
1435 }
1436
1437 ResourceName(ResolveInfo rInfo, int _iconId) {
1438 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1439 }
1440
1441 @Override
1442 public boolean equals(Object o) {
1443 if (this == o) return true;
1444 if (o == null || getClass() != o.getClass()) return false;
1445
1446 ResourceName that = (ResourceName) o;
1447
1448 if (iconId != that.iconId) return false;
1449 return !(packageName != null ?
1450 !packageName.equals(that.packageName) : that.packageName != null);
1451
1452 }
1453
1454 @Override
1455 public int hashCode() {
1456 int result;
1457 result = packageName.hashCode();
1458 result = 31 * result + iconId;
1459 return result;
1460 }
1461
1462 @Override
1463 public String toString() {
1464 return "{ResourceName " + packageName + " / " + iconId + "}";
1465 }
1466 }
1467
1468 private CharSequence getCachedString(ResourceName name) {
1469 synchronized (sSync) {
1470 WeakReference<CharSequence> wr = sStringCache.get(name);
1471 if (wr != null) { // we have the activity
1472 CharSequence cs = wr.get();
1473 if (cs != null) {
1474 return cs;
1475 }
1476 // our entry has been purged
1477 sStringCache.remove(name);
1478 }
1479 }
1480 return null;
1481 }
1482
1483 private void putCachedString(ResourceName name, CharSequence cs) {
1484 synchronized (sSync) {
1485 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1486 }
1487 }
1488
1489 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001490 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001491 ApplicationInfo appInfo) {
1492 ResourceName name = new ResourceName(packageName, resid);
1493 CharSequence text = getCachedString(name);
1494 if (text != null) {
1495 return text;
1496 }
1497 if (appInfo == null) {
1498 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001499 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001500 } catch (NameNotFoundException e) {
1501 return null;
1502 }
1503 }
1504 try {
1505 Resources r = getResourcesForApplication(appInfo);
1506 text = r.getText(resid);
1507 putCachedString(name, text);
1508 return text;
1509 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001510 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001511 + appInfo.packageName);
1512 } catch (RuntimeException e) {
1513 // If an exception was thrown, fall through to return
1514 // default icon.
1515 Log.w("PackageManager", "Failure retrieving text 0x"
1516 + Integer.toHexString(resid) + " in package "
1517 + packageName, e);
1518 }
1519 return null;
1520 }
1521
1522 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001523 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001524 ApplicationInfo appInfo) {
1525 if (appInfo == null) {
1526 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001527 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001528 } catch (NameNotFoundException e) {
1529 return null;
1530 }
1531 }
1532 try {
1533 Resources r = getResourcesForApplication(appInfo);
1534 return r.getXml(resid);
1535 } catch (RuntimeException e) {
1536 // If an exception was thrown, fall through to return
1537 // default icon.
1538 Log.w("PackageManager", "Failure retrieving xml 0x"
1539 + Integer.toHexString(resid) + " in package "
1540 + packageName, e);
1541 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001542 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001543 + appInfo.packageName);
1544 }
1545 return null;
1546 }
1547
1548 @Override
1549 public CharSequence getApplicationLabel(ApplicationInfo info) {
1550 return info.loadLabel(this);
1551 }
1552
1553 @Override
1554 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1555 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001556 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Todd Kennedya6793232016-02-24 22:46:00 +00001557 installerPackageName, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001558 }
1559
Christopher Tatef1977b42014-03-24 16:25:51 -07001560 @Override
1561 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1562 int flags, String installerPackageName) {
Todd Kennedya6793232016-02-24 22:46:00 +00001563 installCommon(packageURI, observer, flags, installerPackageName, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001564 }
1565
1566 private void installCommon(Uri packageURI,
1567 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001568 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001569 if (!"file".equals(packageURI.getScheme())) {
1570 throw new UnsupportedOperationException("Only file:// URIs are supported");
1571 }
Jeff Sharkey513a0742014-07-08 17:10:32 -07001572
1573 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001574 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001575 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
Todd Kennedya6793232016-02-24 22:46:00 +00001576 userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001577 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001578 throw e.rethrowFromSystemServer();
rich cannings706e8ba2012-08-20 13:20:14 -07001579 }
1580 }
1581
1582 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001583 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001584 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001585 }
1586
1587 @Override
1588 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001589 throws NameNotFoundException {
1590 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001591 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001592 if (res == INSTALL_FAILED_INVALID_URI) {
1593 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1594 }
1595 return res;
1596 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001597 throw e.rethrowFromSystemServer();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001598 }
1599 }
1600
1601 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001602 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001603 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001604 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001605 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001606 throw e.rethrowFromSystemServer();
Kenny Root5ab21572011-07-27 11:11:19 -07001607 }
1608 }
1609
1610 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001611 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1612 long millisecondsToDelay) {
1613 try {
1614 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1615 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001616 throw e.rethrowFromSystemServer();
rich canningsd9ef3e52012-08-22 14:28:05 -07001617 }
1618 }
1619
1620 @Override
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001621 public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001622 try {
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001623 mPM.verifyIntentFilter(id, verificationCode, failedDomains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001624 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001625 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001626 }
1627 }
1628
1629 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001630 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001631 try {
1632 return mPM.getIntentVerificationStatus(packageName, userId);
1633 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001634 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001635 }
1636 }
1637
1638 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001639 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001640 try {
1641 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1642 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001643 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001644 }
1645 }
1646
1647 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001648 @SuppressWarnings("unchecked")
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001649 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1650 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001651 ParceledListSlice<IntentFilterVerificationInfo> parceledList =
1652 mPM.getIntentFilterVerifications(packageName);
1653 if (parceledList == null) {
1654 return Collections.emptyList();
1655 }
1656 return parceledList.getList();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001657 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001658 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001659 }
1660 }
1661
1662 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001663 @SuppressWarnings("unchecked")
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001664 public List<IntentFilter> getAllIntentFilters(String packageName) {
1665 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001666 ParceledListSlice<IntentFilter> parceledList =
1667 mPM.getAllIntentFilters(packageName);
1668 if (parceledList == null) {
1669 return Collections.emptyList();
1670 }
1671 return parceledList.getList();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001672 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001673 throw e.rethrowFromSystemServer();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001674 }
1675 }
1676
1677 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001678 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001679 try {
1680 return mPM.getDefaultBrowserPackageName(userId);
1681 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001682 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001683 }
1684 }
1685
1686 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001687 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001688 try {
1689 return mPM.setDefaultBrowserPackageName(packageName, userId);
1690 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001691 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001692 }
1693 }
1694
1695 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001696 public void setInstallerPackageName(String targetPackage,
1697 String installerPackageName) {
1698 try {
1699 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1700 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001701 throw e.rethrowFromSystemServer();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001702 }
1703 }
1704
1705 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001706 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001707 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001708 return mPM.getInstallerPackageName(packageName);
1709 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001710 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001711 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001712 }
1713
1714 @Override
1715 public int getMoveStatus(int moveId) {
1716 try {
1717 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001718 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001719 throw e.rethrowFromSystemServer();
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001720 }
1721 }
1722
1723 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001724 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1725 synchronized (mDelegates) {
1726 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1727 handler.getLooper());
1728 try {
1729 mPM.registerMoveCallback(delegate);
1730 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001731 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001732 }
1733 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001734 }
1735 }
1736
1737 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001738 public void unregisterMoveCallback(MoveCallback callback) {
1739 synchronized (mDelegates) {
1740 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1741 final MoveCallbackDelegate delegate = i.next();
1742 if (delegate.mCallback == callback) {
1743 try {
1744 mPM.unregisterMoveCallback(delegate);
1745 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001746 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001747 }
1748 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001749 }
1750 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001751 }
1752 }
1753
1754 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001755 public int movePackage(String packageName, VolumeInfo vol) {
1756 try {
1757 final String volumeUuid;
1758 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1759 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1760 } else if (vol.isPrimaryPhysical()) {
1761 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1762 } else {
1763 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1764 }
1765
1766 return mPM.movePackage(packageName, volumeUuid);
1767 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001768 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001769 }
1770 }
1771
1772 @Override
1773 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001774 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001775 if (app.isInternal()) {
1776 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1777 } else if (app.isExternalAsec()) {
1778 return storage.getPrimaryPhysicalVolume();
1779 } else {
1780 return storage.findVolumeByUuid(app.volumeUuid);
1781 }
1782 }
1783
1784 @Override
1785 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1786 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1787 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001788 final List<VolumeInfo> vols = storage.getVolumes();
1789 final List<VolumeInfo> candidates = new ArrayList<>();
1790 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001791 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001792 candidates.add(vol);
1793 }
1794 }
1795 return candidates;
1796 }
1797
Makoto Onukif34db0a2016-02-17 11:17:15 -08001798 private boolean isPackageCandidateVolume(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001799 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1800 final boolean forceAllowOnExternal = Settings.Global.getInt(
1801 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001802 // Private internal is always an option
1803 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1804 return true;
1805 }
1806
1807 // System apps and apps demanding internal storage can't be moved
1808 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001809 if (app.isSystemApp()) {
1810 return false;
1811 }
1812 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001813 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1814 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001815 return false;
1816 }
1817
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001818 // Gotta be able to write there
1819 if (!vol.isMountedWritable()) {
1820 return false;
1821 }
1822
1823 // Moving into an ASEC on public primary is only option internal
1824 if (vol.isPrimaryPhysical()) {
1825 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001826 }
1827
Makoto Onukif34db0a2016-02-17 11:17:15 -08001828 // Some apps can't be moved. (e.g. device admins)
1829 try {
1830 if (mPM.isPackageDeviceAdminOnAnyUser(app.packageName)) {
1831 return false;
1832 }
1833 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001834 throw e.rethrowFromSystemServer();
Makoto Onukif34db0a2016-02-17 11:17:15 -08001835 }
1836
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001837 // Otherwise we can move to any private volume
1838 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1839 }
1840
1841 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001842 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001843 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001844 final String volumeUuid;
1845 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1846 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1847 } else if (vol.isPrimaryPhysical()) {
1848 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1849 } else {
1850 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1851 }
1852
1853 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001854 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001855 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001856 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001857 }
1858
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001859 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001860 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1861 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1862 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001863 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001864 }
1865
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001866 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001867 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1868 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1869 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1870 final List<VolumeInfo> vols = storage.getVolumes();
1871 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001872 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1873 storage.getPrimaryStorageUuid()) && currentVol != null) {
1874 // TODO: support moving primary physical to emulated volume
1875 candidates.add(currentVol);
1876 } else {
1877 for (VolumeInfo vol : vols) {
1878 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1879 candidates.add(vol);
1880 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001881 }
1882 }
1883 return candidates;
1884 }
1885
1886 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1887 // Private internal is always an option
1888 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1889 return true;
1890 }
1891
1892 // Gotta be able to write there
1893 if (!vol.isMountedWritable()) {
1894 return false;
1895 }
1896
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001897 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001898 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001899 }
1900
1901 @Override
1902 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001903 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001904 }
1905
1906 @Override
1907 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1908 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001909 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001910 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001911 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001912 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001913 }
1914 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001915
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001916 @Override
1917 public void clearApplicationUserData(String packageName,
1918 IPackageDataObserver observer) {
1919 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001920 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001921 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001922 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001923 }
1924 }
1925 @Override
1926 public void deleteApplicationCacheFiles(String packageName,
1927 IPackageDataObserver observer) {
1928 try {
1929 mPM.deleteApplicationCacheFiles(packageName, observer);
1930 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001931 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001932 }
1933 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001934
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001935 @Override
Suprabh Shukla78c9eb82016-04-12 15:51:35 -07001936 public void deleteApplicationCacheFilesAsUser(String packageName, int userId,
1937 IPackageDataObserver observer) {
1938 try {
1939 mPM.deleteApplicationCacheFilesAsUser(packageName, userId, observer);
1940 } catch (RemoteException e) {
1941 throw e.rethrowFromSystemServer();
1942 }
1943 }
1944
1945 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001946 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1947 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001948 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001949 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001950 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001951 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001952 }
1953 }
1954
1955 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001956 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001957 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001958 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001959 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001960 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001961 }
1962 }
1963
1964 @Override
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001965 public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended,
1966 int userId) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001967 try {
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001968 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, userId);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001969 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001970 throw e.rethrowFromSystemServer();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001971 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001972 }
1973
1974 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001975 public boolean isPackageSuspendedForUser(String packageName, int userId) {
1976 try {
1977 return mPM.isPackageSuspendedForUser(packageName, userId);
1978 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001979 throw e.rethrowFromSystemServer();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001980 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001981 }
1982
1983 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001984 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001985 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001986 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001987 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001988 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001989 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001990 }
1991 }
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001992
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001993 @Override
1994 public void addPackageToPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001995 Log.w(TAG, "addPackageToPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001996 }
1997
1998 @Override
1999 public void removePackageFromPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002000 Log.w(TAG, "removePackageFromPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002001 }
2002
2003 @Override
2004 public List<PackageInfo> getPreferredPackages(int flags) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002005 Log.w(TAG, "getPreferredPackages() is a no-op");
2006 return Collections.emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002007 }
2008
2009 @Override
2010 public void addPreferredActivity(IntentFilter filter,
2011 int match, ComponentName[] set, ComponentName activity) {
2012 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002013 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07002014 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002015 throw e.rethrowFromSystemServer();
Amith Yamasania3f133a2012-08-09 17:11:28 -07002016 }
2017 }
2018
2019 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002020 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07002021 ComponentName[] set, ComponentName activity, int userId) {
2022 try {
2023 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002024 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002025 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002026 }
2027 }
2028
2029 @Override
2030 public void replacePreferredActivity(IntentFilter filter,
2031 int match, ComponentName[] set, ComponentName activity) {
2032 try {
Robin Lee0e27c872015-09-28 14:37:40 +01002033 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002034 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002035 throw e.rethrowFromSystemServer();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002036 }
2037 }
2038
2039 @Override
2040 public void replacePreferredActivityAsUser(IntentFilter filter,
2041 int match, ComponentName[] set, ComponentName activity,
2042 int userId) {
2043 try {
2044 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002045 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002046 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002047 }
2048 }
2049
2050 @Override
2051 public void clearPackagePreferredActivities(String packageName) {
2052 try {
2053 mPM.clearPackagePreferredActivities(packageName);
2054 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002055 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002056 }
2057 }
2058
2059 @Override
2060 public int getPreferredActivities(List<IntentFilter> outFilters,
2061 List<ComponentName> outActivities, String packageName) {
2062 try {
2063 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2064 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002065 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002066 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002067 }
2068
2069 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002070 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2071 try {
2072 return mPM.getHomeActivities(outActivities);
2073 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002074 throw e.rethrowFromSystemServer();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002075 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002076 }
2077
2078 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002079 public void setComponentEnabledSetting(ComponentName componentName,
2080 int newState, int flags) {
2081 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002082 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002083 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002084 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002085 }
2086 }
2087
2088 @Override
2089 public int getComponentEnabledSetting(ComponentName componentName) {
2090 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002091 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002092 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002093 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002094 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002095 }
2096
2097 @Override
2098 public void setApplicationEnabledSetting(String packageName,
2099 int newState, int flags) {
2100 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002101 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002102 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002103 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002104 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002105 }
2106 }
2107
2108 @Override
2109 public int getApplicationEnabledSetting(String packageName) {
2110 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002111 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002112 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002113 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002114 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002115 }
2116
Amith Yamasani655d0e22013-06-12 14:19:10 -07002117 @Override
Sudheer Shankabbb3ff22015-07-09 15:39:23 +01002118 public void flushPackageRestrictionsAsUser(int userId) {
2119 try {
2120 mPM.flushPackageRestrictionsAsUser(userId);
2121 } catch (RemoteException e) {
2122 throw e.rethrowFromSystemServer();
2123 }
2124 }
2125
2126 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002127 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002128 UserHandle user) {
2129 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002130 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002131 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002132 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002133 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002134 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002135 }
2136
2137 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002138 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002139 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002140 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002141 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002142 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002143 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002144 }
2145
dcashmanc6f22492014-08-14 09:54:51 -07002146 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002147 @Override
2148 public KeySet getKeySetByAlias(String packageName, String alias) {
2149 Preconditions.checkNotNull(packageName);
2150 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002151 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002152 return mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002153 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002154 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002155 }
dcashman9d2f4412014-06-09 09:27:54 -07002156 }
2157
dcashmanc6f22492014-08-14 09:54:51 -07002158 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002159 @Override
2160 public KeySet getSigningKeySet(String packageName) {
2161 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002162 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002163 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002164 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002165 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002166 }
dcashman9d2f4412014-06-09 09:27:54 -07002167 }
2168
dcashmanc6f22492014-08-14 09:54:51 -07002169 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002170 @Override
2171 public boolean isSignedBy(String packageName, KeySet ks) {
2172 Preconditions.checkNotNull(packageName);
2173 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002174 try {
dcashmanc6f22492014-08-14 09:54:51 -07002175 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002176 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002177 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002178 }
2179 }
2180
dcashmanc6f22492014-08-14 09:54:51 -07002181 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002182 @Override
2183 public boolean isSignedByExactly(String packageName, KeySet ks) {
2184 Preconditions.checkNotNull(packageName);
2185 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002186 try {
dcashmanc6f22492014-08-14 09:54:51 -07002187 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002188 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002189 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002190 }
2191 }
2192
Kenny Root0aaa0d92011-09-12 16:42:55 -07002193 /**
2194 * @hide
2195 */
2196 @Override
2197 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2198 try {
2199 return mPM.getVerifierDeviceIdentity();
2200 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002201 throw e.rethrowFromSystemServer();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002202 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002203 }
2204
Jeff Hao9f60c082014-10-28 18:51:07 -07002205 /**
2206 * @hide
2207 */
2208 @Override
2209 public boolean isUpgrade() {
2210 try {
2211 return mPM.isUpgrade();
2212 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002213 throw e.rethrowFromSystemServer();
Jeff Hao9f60c082014-10-28 18:51:07 -07002214 }
2215 }
2216
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002217 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002218 public PackageInstaller getPackageInstaller() {
2219 synchronized (mLock) {
2220 if (mInstaller == null) {
2221 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002222 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002223 mContext.getPackageName(), mContext.getUserId());
2224 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002225 throw e.rethrowFromSystemServer();
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002226 }
2227 }
2228 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002229 }
2230 }
2231
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002232 @Override
2233 public boolean isPackageAvailable(String packageName) {
2234 try {
2235 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2236 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002237 throw e.rethrowFromSystemServer();
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002238 }
2239 }
2240
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002241 /**
2242 * @hide
2243 */
2244 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002245 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2246 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002247 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002248 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002249 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002250 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002251 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002252 }
2253 }
2254
2255 /**
2256 * @hide
2257 */
2258 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002259 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002260 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002261 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002262 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002263 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002264 }
2265 }
2266
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002267 /**
2268 * @hide
2269 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002270 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002271 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2272 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2273 return dr;
2274 }
2275 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2276 }
2277
2278 /**
2279 * @hide
2280 */
2281 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002282 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002283 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2284 if (bitmap == null) {
2285 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2286 }
2287 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002288 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002289 Drawable dr = null;
2290 if (itemInfo.packageName != null) {
2291 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2292 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002293 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002294 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002295 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002296 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002297 }
2298
2299 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2300 Rect badgeLocation, boolean tryBadgeInPlace) {
2301 final int badgedWidth = drawable.getIntrinsicWidth();
2302 final int badgedHeight = drawable.getIntrinsicHeight();
2303 final boolean canBadgeInPlace = tryBadgeInPlace
2304 && (drawable instanceof BitmapDrawable)
2305 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2306
2307 final Bitmap bitmap;
2308 if (canBadgeInPlace) {
2309 bitmap = ((BitmapDrawable) drawable).getBitmap();
2310 } else {
2311 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2312 }
2313 Canvas canvas = new Canvas(bitmap);
2314
2315 if (!canBadgeInPlace) {
2316 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2317 drawable.draw(canvas);
2318 }
2319
2320 if (badgeLocation != null) {
2321 if (badgeLocation.left < 0 || badgeLocation.top < 0
2322 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2323 throw new IllegalArgumentException("Badge location " + badgeLocation
2324 + " not in badged drawable bounds "
2325 + new Rect(0, 0, badgedWidth, badgedHeight));
2326 }
2327 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2328
2329 canvas.save();
2330 canvas.translate(badgeLocation.left, badgeLocation.top);
2331 badgeDrawable.draw(canvas);
2332 canvas.restore();
2333 } else {
2334 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2335 badgeDrawable.draw(canvas);
2336 }
2337
2338 if (!canBadgeInPlace) {
2339 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2340
2341 if (drawable instanceof BitmapDrawable) {
2342 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2343 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2344 }
2345
2346 return mergedDrawable;
2347 }
2348
2349 return drawable;
2350 }
2351
Tony Mak8673b282016-03-21 21:10:59 +00002352 private int getBadgeResIdForUser(int userId) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002353 // Return the framework-provided badge.
Tony Mak8673b282016-03-21 21:10:59 +00002354 if (isManagedProfile(userId)) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002355 return com.android.internal.R.drawable.ic_corp_icon_badge;
2356 }
2357 return 0;
2358 }
2359
Tony Mak8673b282016-03-21 21:10:59 +00002360 private boolean isManagedProfile(int userId) {
2361 return getUserManager().isManagedProfile(userId);
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002362 }
2363
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002364 /** {@hide} */
2365 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2366 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002367 private static final int MSG_CREATED = 1;
2368 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002369
2370 final MoveCallback mCallback;
2371 final Handler mHandler;
2372
2373 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2374 mCallback = callback;
2375 mHandler = new Handler(looper, this);
2376 }
2377
2378 @Override
2379 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002380 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002381 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002382 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002383 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002384 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002385 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002386 }
2387 case MSG_STATUS_CHANGED: {
2388 final SomeArgs args = (SomeArgs) msg.obj;
2389 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2390 args.recycle();
2391 return true;
2392 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002393 }
2394 return false;
2395 }
2396
2397 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002398 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002399 final SomeArgs args = SomeArgs.obtain();
2400 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002401 args.arg2 = extras;
2402 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2403 }
2404
2405 @Override
2406 public void onStatusChanged(int moveId, int status, long estMillis) {
2407 final SomeArgs args = SomeArgs.obtain();
2408 args.argi1 = moveId;
2409 args.argi2 = status;
2410 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002411 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002412 }
2413 }
2414
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002415 private final ContextImpl mContext;
2416 private final IPackageManager mPM;
2417
2418 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002419 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2420 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2421 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2422 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002423
2424 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2425 mPermissionListeners = new ArrayMap<>();
2426
2427 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2428 implements Handler.Callback{
2429 private static final int MSG_PERMISSIONS_CHANGED = 1;
2430
2431 private final OnPermissionsChangedListener mListener;
2432 private final Handler mHandler;
2433
2434
2435 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2436 Looper looper) {
2437 mListener = listener;
2438 mHandler = new Handler(looper, this);
2439 }
2440
2441 @Override
2442 public void onPermissionsChanged(int uid) {
2443 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2444 }
2445
2446 @Override
2447 public boolean handleMessage(Message msg) {
2448 switch (msg.what) {
2449 case MSG_PERMISSIONS_CHANGED: {
2450 final int uid = msg.arg1;
2451 mListener.onPermissionsChanged(uid);
2452 return true;
2453 }
2454 }
2455 return false;
2456 }
2457 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002458}