blob: f3185a8d3a6b370be8d4f60a6de6b790dcc45381 [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;
Kenny Guy02c89902016-11-15 19:36:38 +000061import android.graphics.Color;
Svetoslavc7d62f02014-09-04 15:39:54 -070062import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010063import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080064import android.graphics.drawable.Drawable;
Kenny Guy02c89902016-11-15 19:36:38 +000065import android.graphics.drawable.LayerDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080066import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070067import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070068import android.os.Handler;
69import android.os.Looper;
70import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080071import android.os.Process;
72import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070073import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080074import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010075import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070076import android.os.storage.StorageManager;
77import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070078import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070079import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080080import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070081import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070082
83import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070084
85import com.android.internal.annotations.GuardedBy;
Kenny Guy02c89902016-11-15 19:36:38 +000086import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070087import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070088import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010089import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080090import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070091
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080092import java.lang.ref.WeakReference;
93import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080094import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070095import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080096import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070097import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070098import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080099
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700100/** @hide */
101public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800102 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800103 private final static boolean DEBUG_ICONS = false;
104
Svet Ganov2acf0632015-11-24 19:10:59 -0800105 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
106
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700107 // Default flags to use with PackageManager when no flags are given.
108 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
109
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700110 private final Object mLock = new Object();
111
112 @GuardedBy("mLock")
113 private UserManager mUserManager;
114 @GuardedBy("mLock")
115 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100116
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700117 @GuardedBy("mDelegates")
118 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
119
Svet Ganovf1b7f202015-07-29 08:33:42 -0700120 @GuardedBy("mLock")
121 private String mPermissionsControllerPackageName;
122
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100123 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700124 synchronized (mLock) {
125 if (mUserManager == null) {
126 mUserManager = UserManager.get(mContext);
127 }
128 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100129 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100130 }
131
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800132 @Override
133 public PackageInfo getPackageInfo(String packageName, int flags)
134 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100135 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
136 }
137
138 @Override
139 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
140 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800141 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100142 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800143 if (pi != null) {
144 return pi;
145 }
146 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700147 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800148 }
149
150 throw new NameNotFoundException(packageName);
151 }
152
153 @Override
154 public String[] currentToCanonicalPackageNames(String[] names) {
155 try {
156 return mPM.currentToCanonicalPackageNames(names);
157 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700158 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800159 }
160 }
161
162 @Override
163 public String[] canonicalToCurrentPackageNames(String[] names) {
164 try {
165 return mPM.canonicalToCurrentPackageNames(names);
166 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700167 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800168 }
169 }
170
171 @Override
172 public Intent getLaunchIntentForPackage(String packageName) {
173 // First see if the package has an INFO activity; the existence of
174 // such an activity is implied to be the desired front-door for the
175 // overall package (such as if it has multiple launcher entries).
176 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
177 intentToResolve.addCategory(Intent.CATEGORY_INFO);
178 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800179 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800180
181 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800182 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800183 // reuse the intent instance
184 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
185 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
186 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800187 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800188 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800189 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800190 return null;
191 }
192 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800193 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800194 intent.setClassName(ris.get(0).activityInfo.packageName,
195 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800196 return intent;
197 }
198
199 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700200 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
201 // Try to find a main leanback_launcher activity.
202 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
203 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
204 intentToResolve.setPackage(packageName);
205 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
206
207 if (ris == null || ris.size() <= 0) {
208 return null;
209 }
210 Intent intent = new Intent(intentToResolve);
211 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
212 intent.setClassName(ris.get(0).activityInfo.packageName,
213 ris.get(0).activityInfo.name);
214 return intent;
215 }
216
217 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700218 public int[] getPackageGids(String packageName) throws NameNotFoundException {
219 return getPackageGids(packageName, 0);
220 }
221
222 @Override
223 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800224 throws NameNotFoundException {
225 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700226 int[] gids = mPM.getPackageGids(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800227 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800228 return gids;
229 }
230 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700231 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800232 }
233
234 throw new NameNotFoundException(packageName);
235 }
236
237 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700238 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
239 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
240 }
241
242 @Override
243 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
244 return getPackageUidAsUser(packageName, 0, userId);
245 }
246
247 @Override
248 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800249 throws NameNotFoundException {
250 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700251 int uid = mPM.getPackageUid(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800252 if (uid >= 0) {
253 return uid;
254 }
255 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700256 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800257 }
258
259 throw new NameNotFoundException(packageName);
260 }
261
262 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800263 public PermissionInfo getPermissionInfo(String name, int flags)
264 throws NameNotFoundException {
265 try {
266 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
267 if (pi != null) {
268 return pi;
269 }
270 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700271 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800272 }
273
274 throw new NameNotFoundException(name);
275 }
276
277 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700278 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800279 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
280 throws NameNotFoundException {
281 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700282 ParceledListSlice<PermissionInfo> parceledList =
283 mPM.queryPermissionsByGroup(group, flags);
284 if (parceledList != null) {
285 List<PermissionInfo> pi = parceledList.getList();
286 if (pi != null) {
287 return pi;
288 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800289 }
290 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700291 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800292 }
293
294 throw new NameNotFoundException(group);
295 }
296
297 @Override
298 public PermissionGroupInfo getPermissionGroupInfo(String name,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700299 int flags) throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800300 try {
301 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
302 if (pgi != null) {
303 return pgi;
304 }
305 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700306 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800307 }
308
309 throw new NameNotFoundException(name);
310 }
311
312 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700313 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800314 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
315 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700316 ParceledListSlice<PermissionGroupInfo> parceledList =
317 mPM.getAllPermissionGroups(flags);
318 if (parceledList == null) {
319 return Collections.emptyList();
320 }
321 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800322 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700323 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800324 }
325 }
326
327 @Override
328 public ApplicationInfo getApplicationInfo(String packageName, int flags)
329 throws NameNotFoundException {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700330 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
331 }
332
333 @Override
334 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
335 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800336 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700337 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800338 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100339 // This is a temporary hack. Callers must use
340 // createPackageContext(packageName).getApplicationInfo() to
341 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800342 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800343 }
344 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700345 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800346 }
347
348 throw new NameNotFoundException(packageName);
349 }
350
Tao Baic9a02372016-01-12 15:02:24 -0800351 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100352 // If we're dealing with a multi-arch application that has both
353 // 32 and 64 bit shared libraries, we might need to choose the secondary
354 // depending on what the current runtime's instruction set is.
355 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
356 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500357
358 // Get the instruction set that the libraries of secondary Abi is supported.
359 // In presence of a native bridge this might be different than the one secondary Abi used.
360 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
361 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
362 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100363
364 // If the runtimeIsa is the same as the primary isa, then we do nothing.
365 // Everything will be set up correctly because info.nativeLibraryDir will
366 // correspond to the right ISA.
367 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800368 ApplicationInfo modified = new ApplicationInfo(info);
369 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
370 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100371 }
372 }
Tao Baic9a02372016-01-12 15:02:24 -0800373 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100374 }
375
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800376 @Override
377 public ActivityInfo getActivityInfo(ComponentName className, int flags)
378 throws NameNotFoundException {
379 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700380 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800381 if (ai != null) {
382 return ai;
383 }
384 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700385 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800386 }
387
388 throw new NameNotFoundException(className.toString());
389 }
390
391 @Override
392 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
393 throws NameNotFoundException {
394 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700395 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800396 if (ai != null) {
397 return ai;
398 }
399 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700400 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800401 }
402
403 throw new NameNotFoundException(className.toString());
404 }
405
406 @Override
407 public ServiceInfo getServiceInfo(ComponentName className, int flags)
408 throws NameNotFoundException {
409 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700410 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800411 if (si != null) {
412 return si;
413 }
414 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700415 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800416 }
417
418 throw new NameNotFoundException(className.toString());
419 }
420
421 @Override
422 public ProviderInfo getProviderInfo(ComponentName className, int flags)
423 throws NameNotFoundException {
424 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700425 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800426 if (pi != null) {
427 return pi;
428 }
429 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700430 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800431 }
432
433 throw new NameNotFoundException(className.toString());
434 }
435
436 @Override
437 public String[] getSystemSharedLibraryNames() {
438 try {
439 return mPM.getSystemSharedLibraryNames();
440 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700441 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800442 }
443 }
444
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800445 /** @hide */
446 @Override
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700447 public @NonNull String getServicesSystemSharedLibraryPackageName() {
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800448 try {
449 return mPM.getServicesSystemSharedLibraryPackageName();
450 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700451 throw e.rethrowFromSystemServer();
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800452 }
453 }
454
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700455 /**
456 * @hide
457 */
458 public @NonNull String getSharedSystemSharedLibraryPackageName() {
459 try {
460 return mPM.getSharedSystemSharedLibraryPackageName();
461 } catch (RemoteException e) {
462 throw e.rethrowFromSystemServer();
463 }
464 }
465
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800466 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700467 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800468 public FeatureInfo[] getSystemAvailableFeatures() {
469 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700470 ParceledListSlice<FeatureInfo> parceledList =
471 mPM.getSystemAvailableFeatures();
472 if (parceledList == null) {
473 return new FeatureInfo[0];
474 }
475 final List<FeatureInfo> list = parceledList.getList();
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700476 final FeatureInfo[] res = new FeatureInfo[list.size()];
477 for (int i = 0; i < res.length; i++) {
478 res[i] = list.get(i);
479 }
480 return res;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800481 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700482 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800483 }
484 }
485
486 @Override
487 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700488 return hasSystemFeature(name, 0);
489 }
490
491 @Override
492 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800493 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700494 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800495 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700496 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800497 }
498 }
499
500 @Override
501 public int checkPermission(String permName, String pkgName) {
502 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800503 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800504 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700505 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800506 }
507 }
508
509 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700510 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
511 try {
512 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
513 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700514 throw e.rethrowFromSystemServer();
Svet Ganovad3b2972015-07-07 22:49:17 -0700515 }
516 }
517
Svet Ganovf1b7f202015-07-29 08:33:42 -0700518 /**
519 * @hide
520 */
521 @Override
522 public String getPermissionControllerPackageName() {
523 synchronized (mLock) {
524 if (mPermissionsControllerPackageName == null) {
525 try {
526 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
527 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700528 throw e.rethrowFromSystemServer();
Svet Ganovf1b7f202015-07-29 08:33:42 -0700529 }
530 }
531 return mPermissionsControllerPackageName;
532 }
533 }
534
Svet Ganovad3b2972015-07-07 22:49:17 -0700535 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800536 public boolean addPermission(PermissionInfo info) {
537 try {
538 return mPM.addPermission(info);
539 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700540 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800541 }
542 }
543
544 @Override
545 public boolean addPermissionAsync(PermissionInfo info) {
546 try {
547 return mPM.addPermissionAsync(info);
548 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700549 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800550 }
551 }
552
553 @Override
554 public void removePermission(String name) {
555 try {
556 mPM.removePermission(name);
557 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700558 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800559 }
560 }
561
562 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700563 public void grantRuntimePermission(String packageName, String permissionName,
564 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800565 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700566 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800567 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700568 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800569 }
570 }
571
572 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700573 public void revokeRuntimePermission(String packageName, String permissionName,
574 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800575 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700576 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
577 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700578 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700579 }
580 }
581
582 @Override
583 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
584 try {
585 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
586 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700587 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700588 }
589 }
590
591 @Override
592 public void updatePermissionFlags(String permissionName, String packageName,
593 int flagMask, int flagValues, UserHandle user) {
594 try {
595 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
596 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800597 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700598 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800599 }
600 }
601
602 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700603 public boolean shouldShowRequestPermissionRationale(String permission) {
604 try {
605 return mPM.shouldShowRequestPermissionRationale(permission,
606 mContext.getPackageName(), mContext.getUserId());
607 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700608 throw e.rethrowFromSystemServer();
Svetoslav20770dd2015-05-29 15:43:04 -0700609 }
610 }
611
612 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800613 public int checkSignatures(String pkg1, String pkg2) {
614 try {
615 return mPM.checkSignatures(pkg1, pkg2);
616 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700617 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800618 }
619 }
620
621 @Override
622 public int checkSignatures(int uid1, int uid2) {
623 try {
624 return mPM.checkUidSignatures(uid1, uid2);
625 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700626 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800627 }
628 }
629
630 @Override
631 public String[] getPackagesForUid(int uid) {
632 try {
633 return mPM.getPackagesForUid(uid);
634 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700635 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800636 }
637 }
638
639 @Override
640 public String getNameForUid(int uid) {
641 try {
642 return mPM.getNameForUid(uid);
643 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700644 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800645 }
646 }
647
648 @Override
649 public int getUidForSharedUser(String sharedUserName)
650 throws NameNotFoundException {
651 try {
652 int uid = mPM.getUidForSharedUser(sharedUserName);
653 if(uid != -1) {
654 return uid;
655 }
656 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700657 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800658 }
659 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
660 }
661
Kenny Roote6cd0c72011-05-19 12:48:14 -0700662 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800663 @Override
664 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700665 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700666 }
667
668 /** @hide */
669 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700670 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700671 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800672 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700673 ParceledListSlice<PackageInfo> parceledList =
674 mPM.getInstalledPackages(flags, userId);
675 if (parceledList == null) {
676 return Collections.emptyList();
677 }
678 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800679 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700680 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800681 }
682 }
683
Kenny Roote6cd0c72011-05-19 12:48:14 -0700684 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800685 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800686 public List<PackageInfo> getPackagesHoldingPermissions(
687 String[] permissions, int flags) {
688 final int userId = mContext.getUserId();
689 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700690 ParceledListSlice<PackageInfo> parceledList =
691 mPM.getPackagesHoldingPermissions(permissions, flags, userId);
692 if (parceledList == null) {
693 return Collections.emptyList();
694 }
695 return parceledList.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800696 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700697 throw e.rethrowFromSystemServer();
Dianne Hackborne7991752013-01-16 17:56:46 -0800698 }
699 }
700
701 @SuppressWarnings("unchecked")
702 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800703 public List<ApplicationInfo> getInstalledApplications(int flags) {
Bartosz Fabianowski11334242016-11-17 20:49:16 +0100704 return getInstalledApplicationsAsUser(flags, mContext.getUserId());
705 }
706
707 /** @hide */
708 @SuppressWarnings("unchecked")
709 @Override
710 public List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800711 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700712 ParceledListSlice<ApplicationInfo> parceledList =
713 mPM.getInstalledApplications(flags, userId);
714 if (parceledList == null) {
715 return Collections.emptyList();
716 }
717 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800718 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700719 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800720 }
721 }
722
Svet Ganov2acf0632015-11-24 19:10:59 -0800723 /** @hide */
724 @SuppressWarnings("unchecked")
725 @Override
726 public List<EphemeralApplicationInfo> getEphemeralApplications() {
727 try {
728 ParceledListSlice<EphemeralApplicationInfo> slice =
729 mPM.getEphemeralApplications(mContext.getUserId());
730 if (slice != null) {
731 return slice.getList();
732 }
733 return Collections.emptyList();
734 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700735 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800736 }
737 }
738
739 /** @hide */
740 @Override
741 public Drawable getEphemeralApplicationIcon(String packageName) {
742 try {
743 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
744 packageName, mContext.getUserId());
745 if (bitmap != null) {
746 return new BitmapDrawable(null, bitmap);
747 }
748 return null;
749 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700750 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800751 }
752 }
753
754 @Override
755 public boolean isEphemeralApplication() {
756 try {
757 return mPM.isEphemeralApplication(
758 mContext.getPackageName(), mContext.getUserId());
759 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700760 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800761 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800762 }
763
764 @Override
765 public int getEphemeralCookieMaxSizeBytes() {
766 return Settings.Global.getInt(mContext.getContentResolver(),
767 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
768 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
769 }
770
771 @Override
772 public @NonNull byte[] getEphemeralCookie() {
773 try {
774 final byte[] cookie = mPM.getEphemeralApplicationCookie(
775 mContext.getPackageName(), mContext.getUserId());
776 if (cookie != null) {
777 return cookie;
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700778 } else {
779 return EmptyArray.BYTE;
Svet Ganov2acf0632015-11-24 19:10:59 -0800780 }
781 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700782 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800783 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800784 }
785
786 @Override
787 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
788 try {
789 return mPM.setEphemeralApplicationCookie(
790 mContext.getPackageName(), cookie, mContext.getUserId());
791 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700792 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800793 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800794 }
795
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800796 @Override
797 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700798 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700799 }
800
801 @Override
802 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800803 try {
804 return mPM.resolveIntent(
805 intent,
806 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700807 flags,
808 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800809 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700810 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800811 }
812 }
813
814 @Override
815 public List<ResolveInfo> queryIntentActivities(Intent intent,
816 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700817 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700818 }
819
820 /** @hide Same as above but for a specific user */
821 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700822 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700823 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700824 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800825 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700826 ParceledListSlice<ResolveInfo> parceledList =
827 mPM.queryIntentActivities(intent,
828 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
829 flags, userId);
830 if (parceledList == null) {
831 return Collections.emptyList();
832 }
833 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800834 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700835 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800836 }
837 }
838
839 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700840 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800841 public List<ResolveInfo> queryIntentActivityOptions(
842 ComponentName caller, Intent[] specifics, Intent intent,
843 int flags) {
844 final ContentResolver resolver = mContext.getContentResolver();
845
846 String[] specificTypes = null;
847 if (specifics != null) {
848 final int N = specifics.length;
849 for (int i=0; i<N; i++) {
850 Intent sp = specifics[i];
851 if (sp != null) {
852 String t = sp.resolveTypeIfNeeded(resolver);
853 if (t != null) {
854 if (specificTypes == null) {
855 specificTypes = new String[N];
856 }
857 specificTypes[i] = t;
858 }
859 }
860 }
861 }
862
863 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700864 ParceledListSlice<ResolveInfo> parceledList =
865 mPM.queryIntentActivityOptions(caller, specifics, specificTypes, intent,
866 intent.resolveTypeIfNeeded(resolver), flags, mContext.getUserId());
867 if (parceledList == null) {
868 return Collections.emptyList();
869 }
870 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800871 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700872 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800873 }
874 }
875
Amith Yamasanif203aee2012-08-29 18:41:53 -0700876 /**
877 * @hide
878 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800879 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700880 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700881 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800882 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700883 ParceledListSlice<ResolveInfo> parceledList =
884 mPM.queryIntentReceivers(intent,
885 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
886 flags, userId);
887 if (parceledList == null) {
888 return Collections.emptyList();
889 }
890 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800891 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700892 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800893 }
894 }
895
896 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700897 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700898 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700899 }
900
901 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800902 public ResolveInfo resolveService(Intent intent, int flags) {
903 try {
904 return mPM.resolveService(
905 intent,
906 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700907 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700908 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800909 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700910 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800911 }
912 }
913
914 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700915 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700916 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800917 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700918 ParceledListSlice<ResolveInfo> parceledList =
919 mPM.queryIntentServices(intent,
920 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
921 flags, userId);
922 if (parceledList == null) {
923 return Collections.emptyList();
924 }
925 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800926 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700927 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800928 }
929 }
930
931 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700932 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700933 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700934 }
935
936 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700937 @SuppressWarnings("unchecked")
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700938 public List<ResolveInfo> queryIntentContentProvidersAsUser(
939 Intent intent, int flags, int userId) {
940 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700941 ParceledListSlice<ResolveInfo> parceledList =
942 mPM.queryIntentContentProviders(intent,
943 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
944 flags, userId);
945 if (parceledList == null) {
946 return Collections.emptyList();
947 }
948 return parceledList.getList();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700949 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700950 throw e.rethrowFromSystemServer();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700951 }
952 }
953
954 @Override
955 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
956 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
957 }
958
959 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100960 public ProviderInfo resolveContentProvider(String name, int flags) {
961 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
962 }
963
964 /** @hide **/
965 @Override
966 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800967 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100968 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800969 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700970 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800971 }
972 }
973
974 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700975 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800976 public List<ProviderInfo> queryContentProviders(String processName,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700977 int uid, int flags) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800978 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700979 ParceledListSlice<ProviderInfo> slice =
980 mPM.queryContentProviders(processName, uid, flags);
981 return slice != null ? slice.getList() : Collections.<ProviderInfo>emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800982 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700983 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800984 }
985 }
986
987 @Override
988 public InstrumentationInfo getInstrumentationInfo(
989 ComponentName className, int flags)
990 throws NameNotFoundException {
991 try {
992 InstrumentationInfo ii = mPM.getInstrumentationInfo(
993 className, flags);
994 if (ii != null) {
995 return ii;
996 }
997 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700998 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800999 }
1000
1001 throw new NameNotFoundException(className.toString());
1002 }
1003
1004 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001005 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001006 public List<InstrumentationInfo> queryInstrumentation(
1007 String targetPackage, int flags) {
1008 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001009 ParceledListSlice<InstrumentationInfo> parceledList =
1010 mPM.queryInstrumentation(targetPackage, flags);
1011 if (parceledList == null) {
1012 return Collections.emptyList();
1013 }
1014 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001015 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001016 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001017 }
1018 }
1019
Alan Viveretteecd585a2015-04-13 10:32:51 -07001020 @Nullable
1021 @Override
1022 public Drawable getDrawable(String packageName, @DrawableRes int resId,
1023 @Nullable ApplicationInfo appInfo) {
1024 final ResourceName name = new ResourceName(packageName, resId);
1025 final Drawable cachedIcon = getCachedIcon(name);
1026 if (cachedIcon != null) {
1027 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001028 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001029
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001030 if (appInfo == null) {
1031 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001032 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001033 } catch (NameNotFoundException e) {
1034 return null;
1035 }
1036 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001037
1038 if (resId != 0) {
1039 try {
1040 final Resources r = getResourcesForApplication(appInfo);
1041 final Drawable dr = r.getDrawable(resId, null);
1042 if (dr != null) {
1043 putCachedIcon(name, dr);
1044 }
1045
1046 if (false) {
1047 RuntimeException e = new RuntimeException("here");
1048 e.fillInStackTrace();
1049 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
1050 + " from package " + packageName
1051 + ": app scale=" + r.getCompatibilityInfo().applicationScale
1052 + ", caller scale=" + mContext.getResources()
1053 .getCompatibilityInfo().applicationScale,
1054 e);
1055 }
Ricky Wai3ce46252015-04-15 16:12:22 +01001056 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001057 Log.v(TAG, "Getting drawable 0x"
1058 + Integer.toHexString(resId) + " from " + r
1059 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +01001060 }
1061 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -07001062 } catch (NameNotFoundException e) {
1063 Log.w("PackageManager", "Failure retrieving resources for "
1064 + appInfo.packageName);
1065 } catch (Resources.NotFoundException e) {
1066 Log.w("PackageManager", "Failure retrieving resources for "
1067 + appInfo.packageName + ": " + e.getMessage());
1068 } catch (Exception e) {
1069 // If an exception was thrown, fall through to return
1070 // default icon.
1071 Log.w("PackageManager", "Failure retrieving icon 0x"
1072 + Integer.toHexString(resId) + " in package "
1073 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001074 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001075 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001076
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001077 return null;
1078 }
1079
1080 @Override public Drawable getActivityIcon(ComponentName activityName)
1081 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001082 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001083 }
1084
1085 @Override public Drawable getActivityIcon(Intent intent)
1086 throws NameNotFoundException {
1087 if (intent.getComponent() != null) {
1088 return getActivityIcon(intent.getComponent());
1089 }
1090
1091 ResolveInfo info = resolveActivity(
1092 intent, PackageManager.MATCH_DEFAULT_ONLY);
1093 if (info != null) {
1094 return info.activityInfo.loadIcon(this);
1095 }
1096
Romain Guy39fe17c2011-11-30 10:34:07 -08001097 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001098 }
1099
1100 @Override public Drawable getDefaultActivityIcon() {
1101 return Resources.getSystem().getDrawable(
1102 com.android.internal.R.drawable.sym_def_app_icon);
1103 }
1104
1105 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1106 return info.loadIcon(this);
1107 }
1108
1109 @Override public Drawable getApplicationIcon(String packageName)
1110 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001111 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001112 }
1113
1114 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001115 public Drawable getActivityBanner(ComponentName activityName)
1116 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001117 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001118 }
1119
1120 @Override
1121 public Drawable getActivityBanner(Intent intent)
1122 throws NameNotFoundException {
1123 if (intent.getComponent() != null) {
1124 return getActivityBanner(intent.getComponent());
1125 }
1126
1127 ResolveInfo info = resolveActivity(
1128 intent, PackageManager.MATCH_DEFAULT_ONLY);
1129 if (info != null) {
1130 return info.activityInfo.loadBanner(this);
1131 }
1132
1133 throw new NameNotFoundException(intent.toUri(0));
1134 }
1135
1136 @Override
1137 public Drawable getApplicationBanner(ApplicationInfo info) {
1138 return info.loadBanner(this);
1139 }
1140
1141 @Override
1142 public Drawable getApplicationBanner(String packageName)
1143 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001144 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001145 }
1146
1147 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001148 public Drawable getActivityLogo(ComponentName activityName)
1149 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001150 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001151 }
1152
1153 @Override
1154 public Drawable getActivityLogo(Intent intent)
1155 throws NameNotFoundException {
1156 if (intent.getComponent() != null) {
1157 return getActivityLogo(intent.getComponent());
1158 }
1159
1160 ResolveInfo info = resolveActivity(
1161 intent, PackageManager.MATCH_DEFAULT_ONLY);
1162 if (info != null) {
1163 return info.activityInfo.loadLogo(this);
1164 }
1165
1166 throw new NameNotFoundException(intent.toUri(0));
1167 }
1168
1169 @Override
1170 public Drawable getApplicationLogo(ApplicationInfo info) {
1171 return info.loadLogo(this);
1172 }
1173
1174 @Override
1175 public Drawable getApplicationLogo(String packageName)
1176 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001177 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001178 }
1179
Svetoslavc7d62f02014-09-04 15:39:54 -07001180 @Override
1181 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
Kenny Guy02c89902016-11-15 19:36:38 +00001182 if (!isManagedProfile(user.getIdentifier())) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001183 return icon;
1184 }
Kenny Guy02c89902016-11-15 19:36:38 +00001185 Drawable badgeShadow = getDrawable("system",
1186 com.android.internal.R.drawable.ic_corp_icon_badge_shadow, null);
1187 Drawable badgeColor = getDrawable("system",
1188 com.android.internal.R.drawable.ic_corp_icon_badge_color, null);
1189 badgeColor.setTint(getUserBadgeColor(user));
1190 Drawable badgeForeground = getDrawable("system",
1191 com.android.internal.R.drawable.ic_corp_icon_badge_case, null);
1192
1193 Drawable badge = new LayerDrawable(
1194 new Drawable[] {badgeShadow, badgeColor, badgeForeground });
1195 return getBadgedDrawable(icon, badge, null, true);
Svetoslavc7d62f02014-09-04 15:39:54 -07001196 }
1197
1198 @Override
1199 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1200 Rect badgeLocation, int badgeDensity) {
1201 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1202 if (badgeDrawable == null) {
1203 return drawable;
1204 }
1205 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1206 }
1207
Kenny Guy02c89902016-11-15 19:36:38 +00001208 // Should have enough colors to cope with UserManagerService.getMaxManagedProfiles()
1209 @VisibleForTesting
1210 public static final int[] CORP_BADGE_COLORS = new int[] {
1211 com.android.internal.R.color.profile_badge_1,
1212 com.android.internal.R.color.profile_badge_2,
1213 com.android.internal.R.color.profile_badge_3
1214 };
1215
1216 @VisibleForTesting
1217 public static final int[] CORP_BADGE_LABEL_RES_ID = new int[] {
1218 com.android.internal.R.string.managed_profile_label_badge,
1219 com.android.internal.R.string.managed_profile_label_badge_2,
1220 com.android.internal.R.string.managed_profile_label_badge_3
1221 };
1222
1223 private int getUserBadgeColor(UserHandle user) {
1224 int badge = getUserManager().getManagedProfileBadge(user.getIdentifier());
1225 if (badge < 0) {
1226 badge = 0;
1227 }
1228 int resourceId = CORP_BADGE_COLORS[badge % CORP_BADGE_COLORS.length];
1229 return Resources.getSystem().getColor(resourceId, null);
1230 }
1231
Svetoslavc7d62f02014-09-04 15:39:54 -07001232 @Override
1233 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Kenny Guy02c89902016-11-15 19:36:38 +00001234 Drawable badgeColor = getManagedProfileIconForDensity(user,
1235 com.android.internal.R.drawable.ic_corp_badge_color, density);
1236 if (badgeColor == null) {
1237 return null;
1238 }
1239 badgeColor.setTint(getUserBadgeColor(user));
1240 Drawable badgeForeground = getDrawableForDensity(
1241 com.android.internal.R.drawable.ic_corp_badge_case, density);
1242 Drawable badge = new LayerDrawable(
1243 new Drawable[] {badgeColor, badgeForeground });
1244 return badge;
Selim Cineke6ff9462016-01-15 15:07:06 -08001245 }
1246
1247 @Override
1248 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
Kenny Guy02c89902016-11-15 19:36:38 +00001249 Drawable badge = getManagedProfileIconForDensity(user,
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001250 com.android.internal.R.drawable.ic_corp_badge_no_background, density);
Kenny Guy02c89902016-11-15 19:36:38 +00001251 if (badge != null) {
1252 badge.setTint(getUserBadgeColor(user));
1253 }
1254 return badge;
Selim Cineke6ff9462016-01-15 15:07:06 -08001255 }
1256
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001257 private Drawable getDrawableForDensity(int drawableId, int density) {
1258 if (density <= 0) {
1259 density = mContext.getResources().getDisplayMetrics().densityDpi;
1260 }
1261 return Resources.getSystem().getDrawableForDensity(drawableId, density);
1262 }
1263
1264 private Drawable getManagedProfileIconForDensity(UserHandle user, int drawableId, int density) {
Tony Mak8673b282016-03-21 21:10:59 +00001265 if (isManagedProfile(user.getIdentifier())) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001266 return getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001267 }
1268 return null;
1269 }
1270
1271 @Override
1272 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
Tony Mak8673b282016-03-21 21:10:59 +00001273 if (isManagedProfile(user.getIdentifier())) {
Kenny Guy02c89902016-11-15 19:36:38 +00001274 int badge = getUserManager().getManagedProfileBadge(user.getIdentifier());
1275 int resourceId = CORP_BADGE_LABEL_RES_ID[badge % CORP_BADGE_LABEL_RES_ID.length];
1276 return Resources.getSystem().getString(resourceId, label);
Svetoslavc7d62f02014-09-04 15:39:54 -07001277 }
1278 return label;
1279 }
1280
Alan Viveretteecd585a2015-04-13 10:32:51 -07001281 @Override
1282 public Resources getResourcesForActivity(ComponentName activityName)
1283 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001284 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001285 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001286 }
1287
Alan Viveretteecd585a2015-04-13 10:32:51 -07001288 @Override
1289 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1290 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001291 if (app.packageName.equals("system")) {
1292 return mContext.mMainThread.getSystemContext().getResources();
1293 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001294 final boolean sameUid = (app.uid == Process.myUid());
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001295 final Resources r = mContext.mMainThread.getTopLevelResources(
Adam Lesinskic82f28a2016-06-08 17:19:09 -07001296 sameUid ? app.sourceDir : app.publicSourceDir,
1297 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
1298 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1299 mContext.mPackageInfo);
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001300 if (r != null) {
1301 return r;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001302 }
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001303 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1304
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001305 }
1306
Alan Viveretteecd585a2015-04-13 10:32:51 -07001307 @Override
1308 public Resources getResourcesForApplication(String appPackageName)
1309 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001310 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001311 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001312 }
1313
Amith Yamasani98edc952012-09-25 14:09:27 -07001314 /** @hide */
1315 @Override
1316 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1317 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001318 if (userId < 0) {
1319 throw new IllegalArgumentException(
1320 "Call does not support special user #" + userId);
1321 }
1322 if ("system".equals(appPackageName)) {
1323 return mContext.mMainThread.getSystemContext().getResources();
1324 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001325 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001326 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001327 if (ai != null) {
1328 return getResourcesForApplication(ai);
1329 }
1330 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001331 throw e.rethrowFromSystemServer();
Amith Yamasani98edc952012-09-25 14:09:27 -07001332 }
1333 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1334 }
1335
Jeff Sharkeycd654482016-01-08 17:42:11 -07001336 volatile int mCachedSafeMode = -1;
1337
1338 @Override
1339 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001340 try {
1341 if (mCachedSafeMode < 0) {
1342 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1343 }
1344 return mCachedSafeMode != 0;
1345 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001346 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001347 }
1348 }
1349
Svetoslavf7c06eb2015-06-10 18:43:22 -07001350 @Override
1351 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1352 synchronized (mPermissionListeners) {
1353 if (mPermissionListeners.get(listener) != null) {
1354 return;
1355 }
1356 OnPermissionsChangeListenerDelegate delegate =
1357 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1358 try {
1359 mPM.addOnPermissionsChangeListener(delegate);
1360 mPermissionListeners.put(listener, delegate);
1361 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001362 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001363 }
1364 }
1365 }
1366
1367 @Override
1368 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1369 synchronized (mPermissionListeners) {
1370 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1371 if (delegate != null) {
1372 try {
1373 mPM.removeOnPermissionsChangeListener(delegate);
1374 mPermissionListeners.remove(listener);
1375 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001376 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001377 }
1378 }
1379 }
1380 }
1381
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001382 static void configurationChanged() {
1383 synchronized (sSync) {
1384 sIconCache.clear();
1385 sStringCache.clear();
1386 }
1387 }
1388
1389 ApplicationPackageManager(ContextImpl context,
1390 IPackageManager pm) {
1391 mContext = context;
1392 mPM = pm;
1393 }
1394
Alan Viveretteecd585a2015-04-13 10:32:51 -07001395 @Nullable
1396 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001397 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001398 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001399 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1400 + name + ": " + wr);
1401 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001402 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001403 if (state != null) {
1404 if (DEBUG_ICONS) {
1405 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1406 }
1407 // Note: It's okay here to not use the newDrawable(Resources) variant
1408 // of the API. The ConstantState comes from a drawable that was
1409 // originally created by passing the proper app Resources instance
1410 // which means the state should already contain the proper
1411 // resources specific information (like density.) See
1412 // BitmapDrawable.BitmapState for instance.
1413 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001414 }
1415 // our entry has been purged
1416 sIconCache.remove(name);
1417 }
1418 }
1419 return null;
1420 }
1421
Alan Viveretteecd585a2015-04-13 10:32:51 -07001422 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001423 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001424 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001425 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001426 }
1427 }
1428
Romain Guy39fe17c2011-11-30 10:34:07 -08001429 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001430 boolean immediateGc = false;
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001431 if (cmd == ApplicationThreadConstants.EXTERNAL_STORAGE_UNAVAILABLE) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001432 immediateGc = true;
1433 }
1434 if (pkgList != null && (pkgList.length > 0)) {
1435 boolean needCleanup = false;
1436 for (String ssp : pkgList) {
1437 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001438 for (int i=sIconCache.size()-1; i>=0; i--) {
1439 ResourceName nm = sIconCache.keyAt(i);
1440 if (nm.packageName.equals(ssp)) {
1441 //Log.i(TAG, "Removing cached drawable for " + nm);
1442 sIconCache.removeAt(i);
1443 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001444 }
1445 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001446 for (int i=sStringCache.size()-1; i>=0; i--) {
1447 ResourceName nm = sStringCache.keyAt(i);
1448 if (nm.packageName.equals(ssp)) {
1449 //Log.i(TAG, "Removing cached string for " + nm);
1450 sStringCache.removeAt(i);
1451 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001452 }
1453 }
1454 }
1455 }
1456 if (needCleanup || hasPkgInfo) {
1457 if (immediateGc) {
1458 // Schedule an immediate gc.
1459 Runtime.getRuntime().gc();
1460 } else {
1461 ActivityThread.currentActivityThread().scheduleGcIdler();
1462 }
1463 }
1464 }
1465 }
1466
1467 private static final class ResourceName {
1468 final String packageName;
1469 final int iconId;
1470
1471 ResourceName(String _packageName, int _iconId) {
1472 packageName = _packageName;
1473 iconId = _iconId;
1474 }
1475
1476 ResourceName(ApplicationInfo aInfo, int _iconId) {
1477 this(aInfo.packageName, _iconId);
1478 }
1479
1480 ResourceName(ComponentInfo cInfo, int _iconId) {
1481 this(cInfo.applicationInfo.packageName, _iconId);
1482 }
1483
1484 ResourceName(ResolveInfo rInfo, int _iconId) {
1485 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1486 }
1487
1488 @Override
1489 public boolean equals(Object o) {
1490 if (this == o) return true;
1491 if (o == null || getClass() != o.getClass()) return false;
1492
1493 ResourceName that = (ResourceName) o;
1494
1495 if (iconId != that.iconId) return false;
1496 return !(packageName != null ?
1497 !packageName.equals(that.packageName) : that.packageName != null);
1498
1499 }
1500
1501 @Override
1502 public int hashCode() {
1503 int result;
1504 result = packageName.hashCode();
1505 result = 31 * result + iconId;
1506 return result;
1507 }
1508
1509 @Override
1510 public String toString() {
1511 return "{ResourceName " + packageName + " / " + iconId + "}";
1512 }
1513 }
1514
1515 private CharSequence getCachedString(ResourceName name) {
1516 synchronized (sSync) {
1517 WeakReference<CharSequence> wr = sStringCache.get(name);
1518 if (wr != null) { // we have the activity
1519 CharSequence cs = wr.get();
1520 if (cs != null) {
1521 return cs;
1522 }
1523 // our entry has been purged
1524 sStringCache.remove(name);
1525 }
1526 }
1527 return null;
1528 }
1529
1530 private void putCachedString(ResourceName name, CharSequence cs) {
1531 synchronized (sSync) {
1532 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1533 }
1534 }
1535
1536 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001537 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001538 ApplicationInfo appInfo) {
1539 ResourceName name = new ResourceName(packageName, resid);
1540 CharSequence text = getCachedString(name);
1541 if (text != null) {
1542 return text;
1543 }
1544 if (appInfo == null) {
1545 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001546 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001547 } catch (NameNotFoundException e) {
1548 return null;
1549 }
1550 }
1551 try {
1552 Resources r = getResourcesForApplication(appInfo);
1553 text = r.getText(resid);
1554 putCachedString(name, text);
1555 return text;
1556 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001557 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001558 + appInfo.packageName);
1559 } catch (RuntimeException e) {
1560 // If an exception was thrown, fall through to return
1561 // default icon.
1562 Log.w("PackageManager", "Failure retrieving text 0x"
1563 + Integer.toHexString(resid) + " in package "
1564 + packageName, e);
1565 }
1566 return null;
1567 }
1568
1569 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001570 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001571 ApplicationInfo appInfo) {
1572 if (appInfo == null) {
1573 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001574 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001575 } catch (NameNotFoundException e) {
1576 return null;
1577 }
1578 }
1579 try {
1580 Resources r = getResourcesForApplication(appInfo);
1581 return r.getXml(resid);
1582 } catch (RuntimeException e) {
1583 // If an exception was thrown, fall through to return
1584 // default icon.
1585 Log.w("PackageManager", "Failure retrieving xml 0x"
1586 + Integer.toHexString(resid) + " in package "
1587 + packageName, e);
1588 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001589 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001590 + appInfo.packageName);
1591 }
1592 return null;
1593 }
1594
1595 @Override
1596 public CharSequence getApplicationLabel(ApplicationInfo info) {
1597 return info.loadLabel(this);
1598 }
1599
1600 @Override
1601 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1602 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001603 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Todd Kennedya6793232016-02-24 22:46:00 +00001604 installerPackageName, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001605 }
1606
Christopher Tatef1977b42014-03-24 16:25:51 -07001607 @Override
1608 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1609 int flags, String installerPackageName) {
Todd Kennedya6793232016-02-24 22:46:00 +00001610 installCommon(packageURI, observer, flags, installerPackageName, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001611 }
1612
1613 private void installCommon(Uri packageURI,
1614 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001615 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001616 if (!"file".equals(packageURI.getScheme())) {
1617 throw new UnsupportedOperationException("Only file:// URIs are supported");
1618 }
Jeff Sharkey513a0742014-07-08 17:10:32 -07001619
1620 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001621 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001622 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
Todd Kennedya6793232016-02-24 22:46:00 +00001623 userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001624 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001625 throw e.rethrowFromSystemServer();
rich cannings706e8ba2012-08-20 13:20:14 -07001626 }
1627 }
1628
1629 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001630 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001631 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001632 }
1633
1634 @Override
1635 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001636 throws NameNotFoundException {
1637 try {
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +01001638 int res = mPM.installExistingPackageAsUser(packageName, userId,
1639 PackageManager.INSTALL_REASON_UNKNOWN);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001640 if (res == INSTALL_FAILED_INVALID_URI) {
1641 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1642 }
1643 return res;
1644 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001645 throw e.rethrowFromSystemServer();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001646 }
1647 }
1648
1649 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001650 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001651 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001652 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001653 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001654 throw e.rethrowFromSystemServer();
Kenny Root5ab21572011-07-27 11:11:19 -07001655 }
1656 }
1657
1658 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001659 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1660 long millisecondsToDelay) {
1661 try {
1662 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1663 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001664 throw e.rethrowFromSystemServer();
rich canningsd9ef3e52012-08-22 14:28:05 -07001665 }
1666 }
1667
1668 @Override
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001669 public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001670 try {
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001671 mPM.verifyIntentFilter(id, verificationCode, failedDomains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001672 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001673 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001674 }
1675 }
1676
1677 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001678 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001679 try {
1680 return mPM.getIntentVerificationStatus(packageName, userId);
1681 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001682 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001683 }
1684 }
1685
1686 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001687 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001688 try {
1689 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1690 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001691 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001692 }
1693 }
1694
1695 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001696 @SuppressWarnings("unchecked")
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001697 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1698 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001699 ParceledListSlice<IntentFilterVerificationInfo> parceledList =
1700 mPM.getIntentFilterVerifications(packageName);
1701 if (parceledList == null) {
1702 return Collections.emptyList();
1703 }
1704 return parceledList.getList();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001705 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001706 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001707 }
1708 }
1709
1710 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001711 @SuppressWarnings("unchecked")
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001712 public List<IntentFilter> getAllIntentFilters(String packageName) {
1713 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001714 ParceledListSlice<IntentFilter> parceledList =
1715 mPM.getAllIntentFilters(packageName);
1716 if (parceledList == null) {
1717 return Collections.emptyList();
1718 }
1719 return parceledList.getList();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001720 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001721 throw e.rethrowFromSystemServer();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001722 }
1723 }
1724
1725 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001726 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001727 try {
1728 return mPM.getDefaultBrowserPackageName(userId);
1729 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001730 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001731 }
1732 }
1733
1734 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001735 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001736 try {
1737 return mPM.setDefaultBrowserPackageName(packageName, userId);
1738 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001739 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001740 }
1741 }
1742
1743 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001744 public void setInstallerPackageName(String targetPackage,
1745 String installerPackageName) {
1746 try {
1747 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1748 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001749 throw e.rethrowFromSystemServer();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001750 }
1751 }
1752
1753 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001754 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001755 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001756 return mPM.getInstallerPackageName(packageName);
1757 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001758 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001759 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001760 }
1761
1762 @Override
1763 public int getMoveStatus(int moveId) {
1764 try {
1765 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001766 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001767 throw e.rethrowFromSystemServer();
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001768 }
1769 }
1770
1771 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001772 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1773 synchronized (mDelegates) {
1774 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1775 handler.getLooper());
1776 try {
1777 mPM.registerMoveCallback(delegate);
1778 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001779 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001780 }
1781 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001782 }
1783 }
1784
1785 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001786 public void unregisterMoveCallback(MoveCallback callback) {
1787 synchronized (mDelegates) {
1788 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1789 final MoveCallbackDelegate delegate = i.next();
1790 if (delegate.mCallback == callback) {
1791 try {
1792 mPM.unregisterMoveCallback(delegate);
1793 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001794 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001795 }
1796 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001797 }
1798 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001799 }
1800 }
1801
1802 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001803 public int movePackage(String packageName, VolumeInfo vol) {
1804 try {
1805 final String volumeUuid;
1806 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1807 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1808 } else if (vol.isPrimaryPhysical()) {
1809 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1810 } else {
1811 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1812 }
1813
1814 return mPM.movePackage(packageName, volumeUuid);
1815 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001816 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001817 }
1818 }
1819
1820 @Override
1821 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001822 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001823 if (app.isInternal()) {
1824 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1825 } else if (app.isExternalAsec()) {
1826 return storage.getPrimaryPhysicalVolume();
1827 } else {
1828 return storage.findVolumeByUuid(app.volumeUuid);
1829 }
1830 }
1831
1832 @Override
1833 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1834 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1835 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001836 final List<VolumeInfo> vols = storage.getVolumes();
1837 final List<VolumeInfo> candidates = new ArrayList<>();
1838 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001839 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001840 candidates.add(vol);
1841 }
1842 }
1843 return candidates;
1844 }
1845
Makoto Onukif34db0a2016-02-17 11:17:15 -08001846 private boolean isPackageCandidateVolume(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001847 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1848 final boolean forceAllowOnExternal = Settings.Global.getInt(
1849 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001850 // Private internal is always an option
1851 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1852 return true;
1853 }
1854
1855 // System apps and apps demanding internal storage can't be moved
1856 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001857 if (app.isSystemApp()) {
1858 return false;
1859 }
1860 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001861 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1862 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001863 return false;
1864 }
1865
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001866 // Gotta be able to write there
1867 if (!vol.isMountedWritable()) {
1868 return false;
1869 }
1870
1871 // Moving into an ASEC on public primary is only option internal
1872 if (vol.isPrimaryPhysical()) {
1873 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001874 }
1875
Makoto Onukif34db0a2016-02-17 11:17:15 -08001876 // Some apps can't be moved. (e.g. device admins)
1877 try {
1878 if (mPM.isPackageDeviceAdminOnAnyUser(app.packageName)) {
1879 return false;
1880 }
1881 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001882 throw e.rethrowFromSystemServer();
Makoto Onukif34db0a2016-02-17 11:17:15 -08001883 }
1884
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001885 // Otherwise we can move to any private volume
1886 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1887 }
1888
1889 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001890 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001891 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001892 final String volumeUuid;
1893 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1894 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1895 } else if (vol.isPrimaryPhysical()) {
1896 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1897 } else {
1898 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1899 }
1900
1901 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001902 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001903 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001904 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001905 }
1906
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001907 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001908 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1909 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1910 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001911 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001912 }
1913
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001914 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001915 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1916 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1917 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1918 final List<VolumeInfo> vols = storage.getVolumes();
1919 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001920 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1921 storage.getPrimaryStorageUuid()) && currentVol != null) {
1922 // TODO: support moving primary physical to emulated volume
1923 candidates.add(currentVol);
1924 } else {
1925 for (VolumeInfo vol : vols) {
1926 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1927 candidates.add(vol);
1928 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001929 }
1930 }
1931 return candidates;
1932 }
1933
1934 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1935 // Private internal is always an option
1936 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1937 return true;
1938 }
1939
1940 // Gotta be able to write there
1941 if (!vol.isMountedWritable()) {
1942 return false;
1943 }
1944
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001945 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001946 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001947 }
1948
1949 @Override
1950 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001951 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001952 }
1953
1954 @Override
1955 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1956 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001957 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001958 mPM.deletePackageAsUser(packageName, observer, userId, flags);
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 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001963
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001964 @Override
1965 public void clearApplicationUserData(String packageName,
1966 IPackageDataObserver observer) {
1967 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001968 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001969 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001970 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001971 }
1972 }
1973 @Override
1974 public void deleteApplicationCacheFiles(String packageName,
1975 IPackageDataObserver observer) {
1976 try {
1977 mPM.deleteApplicationCacheFiles(packageName, observer);
1978 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001979 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001980 }
1981 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001982
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001983 @Override
Suprabh Shukla78c9eb82016-04-12 15:51:35 -07001984 public void deleteApplicationCacheFilesAsUser(String packageName, int userId,
1985 IPackageDataObserver observer) {
1986 try {
1987 mPM.deleteApplicationCacheFilesAsUser(packageName, userId, observer);
1988 } catch (RemoteException e) {
1989 throw e.rethrowFromSystemServer();
1990 }
1991 }
1992
1993 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001994 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1995 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001996 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001997 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001998 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001999 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002000 }
2001 }
2002
2003 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002004 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002005 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002006 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002007 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002008 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002009 }
2010 }
2011
2012 @Override
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00002013 public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended,
2014 int userId) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002015 try {
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00002016 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, userId);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002017 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002018 throw e.rethrowFromSystemServer();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002019 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002020 }
2021
2022 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002023 public boolean isPackageSuspendedForUser(String packageName, int userId) {
2024 try {
2025 return mPM.isPackageSuspendedForUser(packageName, userId);
2026 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002027 throw e.rethrowFromSystemServer();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002028 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002029 }
2030
Jeff Sharkey9bc89af2017-01-11 11:25:50 -07002031 /** @hide */
2032 @Override
2033 public void setApplicationCategoryHint(String packageName, int categoryHint) {
2034 try {
2035 mPM.setApplicationCategoryHint(packageName, categoryHint,
2036 mContext.getOpPackageName());
2037 } catch (RemoteException e) {
2038 throw e.rethrowFromSystemServer();
2039 }
2040 }
2041
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002042 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002043 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07002044 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002045 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07002046 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002047 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002048 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002049 }
2050 }
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002051
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002052 @Override
2053 public void addPackageToPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002054 Log.w(TAG, "addPackageToPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002055 }
2056
2057 @Override
2058 public void removePackageFromPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002059 Log.w(TAG, "removePackageFromPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002060 }
2061
2062 @Override
2063 public List<PackageInfo> getPreferredPackages(int flags) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002064 Log.w(TAG, "getPreferredPackages() is a no-op");
2065 return Collections.emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002066 }
2067
2068 @Override
2069 public void addPreferredActivity(IntentFilter filter,
2070 int match, ComponentName[] set, ComponentName activity) {
2071 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002072 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07002073 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002074 throw e.rethrowFromSystemServer();
Amith Yamasania3f133a2012-08-09 17:11:28 -07002075 }
2076 }
2077
2078 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002079 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07002080 ComponentName[] set, ComponentName activity, int userId) {
2081 try {
2082 mPM.addPreferredActivity(filter, match, set, activity, userId);
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 void replacePreferredActivity(IntentFilter filter,
2090 int match, ComponentName[] set, ComponentName activity) {
2091 try {
Robin Lee0e27c872015-09-28 14:37:40 +01002092 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002093 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002094 throw e.rethrowFromSystemServer();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002095 }
2096 }
2097
2098 @Override
2099 public void replacePreferredActivityAsUser(IntentFilter filter,
2100 int match, ComponentName[] set, ComponentName activity,
2101 int userId) {
2102 try {
2103 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002104 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002105 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002106 }
2107 }
2108
2109 @Override
2110 public void clearPackagePreferredActivities(String packageName) {
2111 try {
2112 mPM.clearPackagePreferredActivities(packageName);
2113 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002114 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002115 }
2116 }
2117
2118 @Override
2119 public int getPreferredActivities(List<IntentFilter> outFilters,
2120 List<ComponentName> outActivities, String packageName) {
2121 try {
2122 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2123 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002124 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002125 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002126 }
2127
2128 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002129 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2130 try {
2131 return mPM.getHomeActivities(outActivities);
2132 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002133 throw e.rethrowFromSystemServer();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002134 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002135 }
2136
2137 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002138 public void setComponentEnabledSetting(ComponentName componentName,
2139 int newState, int flags) {
2140 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002141 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002142 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002143 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002144 }
2145 }
2146
2147 @Override
2148 public int getComponentEnabledSetting(ComponentName componentName) {
2149 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002150 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002151 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002152 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002153 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002154 }
2155
2156 @Override
2157 public void setApplicationEnabledSetting(String packageName,
2158 int newState, int flags) {
2159 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002160 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002161 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002162 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002163 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002164 }
2165 }
2166
2167 @Override
2168 public int getApplicationEnabledSetting(String packageName) {
2169 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002170 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002171 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002172 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002173 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002174 }
2175
Amith Yamasani655d0e22013-06-12 14:19:10 -07002176 @Override
Sudheer Shankabbb3ff22015-07-09 15:39:23 +01002177 public void flushPackageRestrictionsAsUser(int userId) {
2178 try {
2179 mPM.flushPackageRestrictionsAsUser(userId);
2180 } catch (RemoteException e) {
2181 throw e.rethrowFromSystemServer();
2182 }
2183 }
2184
2185 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002186 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002187 UserHandle user) {
2188 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002189 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002190 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002191 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002192 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002193 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002194 }
2195
2196 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002197 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002198 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002199 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002200 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002201 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002202 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002203 }
2204
dcashmanc6f22492014-08-14 09:54:51 -07002205 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002206 @Override
2207 public KeySet getKeySetByAlias(String packageName, String alias) {
2208 Preconditions.checkNotNull(packageName);
2209 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002210 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002211 return mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002212 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002213 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002214 }
dcashman9d2f4412014-06-09 09:27:54 -07002215 }
2216
dcashmanc6f22492014-08-14 09:54:51 -07002217 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002218 @Override
2219 public KeySet getSigningKeySet(String packageName) {
2220 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002221 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002222 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002223 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002224 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002225 }
dcashman9d2f4412014-06-09 09:27:54 -07002226 }
2227
dcashmanc6f22492014-08-14 09:54:51 -07002228 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002229 @Override
2230 public boolean isSignedBy(String packageName, KeySet ks) {
2231 Preconditions.checkNotNull(packageName);
2232 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002233 try {
dcashmanc6f22492014-08-14 09:54:51 -07002234 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002235 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002236 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002237 }
2238 }
2239
dcashmanc6f22492014-08-14 09:54:51 -07002240 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002241 @Override
2242 public boolean isSignedByExactly(String packageName, KeySet ks) {
2243 Preconditions.checkNotNull(packageName);
2244 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002245 try {
dcashmanc6f22492014-08-14 09:54:51 -07002246 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002247 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002248 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002249 }
2250 }
2251
Kenny Root0aaa0d92011-09-12 16:42:55 -07002252 /**
2253 * @hide
2254 */
2255 @Override
2256 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2257 try {
2258 return mPM.getVerifierDeviceIdentity();
2259 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002260 throw e.rethrowFromSystemServer();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002261 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002262 }
2263
Jeff Hao9f60c082014-10-28 18:51:07 -07002264 /**
2265 * @hide
2266 */
2267 @Override
2268 public boolean isUpgrade() {
2269 try {
2270 return mPM.isUpgrade();
2271 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002272 throw e.rethrowFromSystemServer();
Jeff Hao9f60c082014-10-28 18:51:07 -07002273 }
2274 }
2275
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002276 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002277 public PackageInstaller getPackageInstaller() {
2278 synchronized (mLock) {
2279 if (mInstaller == null) {
2280 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002281 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002282 mContext.getPackageName(), mContext.getUserId());
2283 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002284 throw e.rethrowFromSystemServer();
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002285 }
2286 }
2287 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002288 }
2289 }
2290
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002291 @Override
2292 public boolean isPackageAvailable(String packageName) {
2293 try {
2294 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2295 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002296 throw e.rethrowFromSystemServer();
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002297 }
2298 }
2299
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002300 /**
2301 * @hide
2302 */
2303 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002304 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2305 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002306 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002307 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002308 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002309 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002310 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002311 }
2312 }
2313
2314 /**
2315 * @hide
2316 */
2317 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002318 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002319 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002320 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002321 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002322 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002323 }
2324 }
2325
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002326 /**
2327 * @hide
2328 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002329 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002330 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2331 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2332 return dr;
2333 }
2334 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2335 }
2336
2337 /**
2338 * @hide
2339 */
2340 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002341 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002342 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2343 if (bitmap == null) {
2344 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2345 }
2346 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002347 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002348 Drawable dr = null;
2349 if (itemInfo.packageName != null) {
2350 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2351 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002352 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002353 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002354 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002355 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002356 }
2357
2358 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2359 Rect badgeLocation, boolean tryBadgeInPlace) {
2360 final int badgedWidth = drawable.getIntrinsicWidth();
2361 final int badgedHeight = drawable.getIntrinsicHeight();
2362 final boolean canBadgeInPlace = tryBadgeInPlace
2363 && (drawable instanceof BitmapDrawable)
2364 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2365
2366 final Bitmap bitmap;
2367 if (canBadgeInPlace) {
2368 bitmap = ((BitmapDrawable) drawable).getBitmap();
2369 } else {
2370 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2371 }
2372 Canvas canvas = new Canvas(bitmap);
2373
2374 if (!canBadgeInPlace) {
2375 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2376 drawable.draw(canvas);
2377 }
2378
2379 if (badgeLocation != null) {
2380 if (badgeLocation.left < 0 || badgeLocation.top < 0
2381 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2382 throw new IllegalArgumentException("Badge location " + badgeLocation
2383 + " not in badged drawable bounds "
2384 + new Rect(0, 0, badgedWidth, badgedHeight));
2385 }
2386 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2387
2388 canvas.save();
2389 canvas.translate(badgeLocation.left, badgeLocation.top);
2390 badgeDrawable.draw(canvas);
2391 canvas.restore();
2392 } else {
2393 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2394 badgeDrawable.draw(canvas);
2395 }
2396
2397 if (!canBadgeInPlace) {
2398 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2399
2400 if (drawable instanceof BitmapDrawable) {
2401 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2402 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2403 }
2404
2405 return mergedDrawable;
2406 }
2407
2408 return drawable;
2409 }
2410
Tony Mak8673b282016-03-21 21:10:59 +00002411 private boolean isManagedProfile(int userId) {
2412 return getUserManager().isManagedProfile(userId);
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002413 }
2414
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +01002415 /**
2416 * @hide
2417 */
2418 @Override
2419 public int getInstallReason(String packageName, UserHandle user) {
2420 try {
2421 return mPM.getInstallReason(packageName, user.getIdentifier());
2422 } catch (RemoteException e) {
2423 throw e.rethrowFromSystemServer();
2424 }
2425 }
2426
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002427 /** {@hide} */
2428 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2429 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002430 private static final int MSG_CREATED = 1;
2431 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002432
2433 final MoveCallback mCallback;
2434 final Handler mHandler;
2435
2436 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2437 mCallback = callback;
2438 mHandler = new Handler(looper, this);
2439 }
2440
2441 @Override
2442 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002443 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002444 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002445 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002446 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002447 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002448 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002449 }
2450 case MSG_STATUS_CHANGED: {
2451 final SomeArgs args = (SomeArgs) msg.obj;
2452 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2453 args.recycle();
2454 return true;
2455 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002456 }
2457 return false;
2458 }
2459
2460 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002461 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002462 final SomeArgs args = SomeArgs.obtain();
2463 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002464 args.arg2 = extras;
2465 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2466 }
2467
2468 @Override
2469 public void onStatusChanged(int moveId, int status, long estMillis) {
2470 final SomeArgs args = SomeArgs.obtain();
2471 args.argi1 = moveId;
2472 args.argi2 = status;
2473 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002474 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002475 }
2476 }
2477
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002478 private final ContextImpl mContext;
2479 private final IPackageManager mPM;
2480
2481 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002482 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2483 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2484 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2485 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002486
2487 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2488 mPermissionListeners = new ArrayMap<>();
2489
2490 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2491 implements Handler.Callback{
2492 private static final int MSG_PERMISSIONS_CHANGED = 1;
2493
2494 private final OnPermissionsChangedListener mListener;
2495 private final Handler mHandler;
2496
2497
2498 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2499 Looper looper) {
2500 mListener = listener;
2501 mHandler = new Handler(looper, this);
2502 }
2503
2504 @Override
2505 public void onPermissionsChanged(int uid) {
2506 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2507 }
2508
2509 @Override
2510 public boolean handleMessage(Message msg) {
2511 switch (msg.what) {
2512 case MSG_PERMISSIONS_CHANGED: {
2513 final int uid = msg.arg1;
2514 mListener.onPermissionsChanged(uid);
2515 return true;
2516 }
2517 }
2518 return false;
2519 }
2520 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002521}