blob: b199984740f24fc784f66b784dd618706ec11b6d [file] [log] [blame]
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.DrawableRes;
Alan Viveretteecd585a2015-04-13 10:32:51 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.StringRes;
23import android.annotation.XmlRes;
Makoto Onukif34db0a2016-02-17 11:17:15 -080024import android.app.admin.DevicePolicyManager;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080025import android.content.ComponentName;
26import android.content.ContentResolver;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.IntentSender;
30import android.content.pm.ActivityInfo;
31import android.content.pm.ApplicationInfo;
32import android.content.pm.ComponentInfo;
Svet Ganov2acf0632015-11-24 19:10:59 -080033import android.content.pm.EphemeralApplicationInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080034import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070035import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080036import android.content.pm.IPackageDataObserver;
37import android.content.pm.IPackageDeleteObserver;
38import android.content.pm.IPackageInstallObserver;
39import android.content.pm.IPackageManager;
40import android.content.pm.IPackageMoveObserver;
41import android.content.pm.IPackageStatsObserver;
42import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080043import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070044import android.content.pm.KeySet;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080045import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070046import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010047import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080048import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070049import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080050import android.content.pm.PermissionGroupInfo;
51import android.content.pm.PermissionInfo;
52import android.content.pm.ProviderInfo;
53import android.content.pm.ResolveInfo;
54import android.content.pm.ServiceInfo;
Svetoslavc7d62f02014-09-04 15:39:54 -070055import android.content.pm.UserInfo;
Kenny Root0aaa0d92011-09-12 16:42:55 -070056import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080057import android.content.res.Resources;
58import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070059import android.graphics.Bitmap;
60import android.graphics.Canvas;
61import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010062import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080063import android.graphics.drawable.Drawable;
64import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070065import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070066import android.os.Handler;
67import android.os.Looper;
68import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080069import android.os.Process;
70import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070071import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080072import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010073import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070074import android.os.storage.StorageManager;
75import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070076import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070077import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080078import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070079import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070080
81import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070082
83import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070084import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070085import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010086import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080087import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070088
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080089import java.lang.ref.WeakReference;
90import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080091import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070092import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080093import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070094import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070095import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080096
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070097/** @hide */
98public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080099 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800100 private final static boolean DEBUG_ICONS = false;
101
Svet Ganov2acf0632015-11-24 19:10:59 -0800102 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
103
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700104 // Default flags to use with PackageManager when no flags are given.
105 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
106
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700107 private final Object mLock = new Object();
108
109 @GuardedBy("mLock")
110 private UserManager mUserManager;
111 @GuardedBy("mLock")
112 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100113
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700114 @GuardedBy("mDelegates")
115 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
116
Svet Ganovf1b7f202015-07-29 08:33:42 -0700117 @GuardedBy("mLock")
118 private String mPermissionsControllerPackageName;
119
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100120 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700121 synchronized (mLock) {
122 if (mUserManager == null) {
123 mUserManager = UserManager.get(mContext);
124 }
125 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100126 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100127 }
128
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800129 @Override
130 public PackageInfo getPackageInfo(String packageName, int flags)
131 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100132 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
133 }
134
135 @Override
136 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
137 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800138 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100139 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800140 if (pi != null) {
141 return pi;
142 }
143 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700144 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800145 }
146
147 throw new NameNotFoundException(packageName);
148 }
149
150 @Override
151 public String[] currentToCanonicalPackageNames(String[] names) {
152 try {
153 return mPM.currentToCanonicalPackageNames(names);
154 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700155 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800156 }
157 }
158
159 @Override
160 public String[] canonicalToCurrentPackageNames(String[] names) {
161 try {
162 return mPM.canonicalToCurrentPackageNames(names);
163 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700164 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800165 }
166 }
167
168 @Override
169 public Intent getLaunchIntentForPackage(String packageName) {
170 // First see if the package has an INFO activity; the existence of
171 // such an activity is implied to be the desired front-door for the
172 // overall package (such as if it has multiple launcher entries).
173 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
174 intentToResolve.addCategory(Intent.CATEGORY_INFO);
175 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800176 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800177
178 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800179 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800180 // reuse the intent instance
181 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
182 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
183 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800184 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800185 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800186 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800187 return null;
188 }
189 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800190 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800191 intent.setClassName(ris.get(0).activityInfo.packageName,
192 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800193 return intent;
194 }
195
196 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700197 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
198 // Try to find a main leanback_launcher activity.
199 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
200 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
201 intentToResolve.setPackage(packageName);
202 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
203
204 if (ris == null || ris.size() <= 0) {
205 return null;
206 }
207 Intent intent = new Intent(intentToResolve);
208 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
209 intent.setClassName(ris.get(0).activityInfo.packageName,
210 ris.get(0).activityInfo.name);
211 return intent;
212 }
213
214 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700215 public int[] getPackageGids(String packageName) throws NameNotFoundException {
216 return getPackageGids(packageName, 0);
217 }
218
219 @Override
220 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800221 throws NameNotFoundException {
222 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700223 int[] gids = mPM.getPackageGids(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800224 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800225 return gids;
226 }
227 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700228 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800229 }
230
231 throw new NameNotFoundException(packageName);
232 }
233
234 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700235 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
236 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
237 }
238
239 @Override
240 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
241 return getPackageUidAsUser(packageName, 0, userId);
242 }
243
244 @Override
245 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800246 throws NameNotFoundException {
247 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700248 int uid = mPM.getPackageUid(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800249 if (uid >= 0) {
250 return uid;
251 }
252 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700253 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800254 }
255
256 throw new NameNotFoundException(packageName);
257 }
258
259 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800260 public PermissionInfo getPermissionInfo(String name, int flags)
261 throws NameNotFoundException {
262 try {
263 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
264 if (pi != null) {
265 return pi;
266 }
267 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700268 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800269 }
270
271 throw new NameNotFoundException(name);
272 }
273
274 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700275 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800276 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
277 throws NameNotFoundException {
278 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700279 ParceledListSlice<PermissionInfo> parceledList =
280 mPM.queryPermissionsByGroup(group, flags);
281 if (parceledList != null) {
282 List<PermissionInfo> pi = parceledList.getList();
283 if (pi != null) {
284 return pi;
285 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800286 }
287 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700288 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800289 }
290
291 throw new NameNotFoundException(group);
292 }
293
294 @Override
295 public PermissionGroupInfo getPermissionGroupInfo(String name,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700296 int flags) throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800297 try {
298 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
299 if (pgi != null) {
300 return pgi;
301 }
302 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700303 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800304 }
305
306 throw new NameNotFoundException(name);
307 }
308
309 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700310 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800311 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
312 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700313 ParceledListSlice<PermissionGroupInfo> parceledList =
314 mPM.getAllPermissionGroups(flags);
315 if (parceledList == null) {
316 return Collections.emptyList();
317 }
318 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800319 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700320 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800321 }
322 }
323
324 @Override
325 public ApplicationInfo getApplicationInfo(String packageName, int flags)
326 throws NameNotFoundException {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700327 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
328 }
329
330 @Override
331 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
332 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800333 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700334 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800335 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100336 // This is a temporary hack. Callers must use
337 // createPackageContext(packageName).getApplicationInfo() to
338 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800339 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800340 }
341 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700342 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800343 }
344
345 throw new NameNotFoundException(packageName);
346 }
347
Tao Baic9a02372016-01-12 15:02:24 -0800348 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100349 // If we're dealing with a multi-arch application that has both
350 // 32 and 64 bit shared libraries, we might need to choose the secondary
351 // depending on what the current runtime's instruction set is.
352 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
353 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500354
355 // Get the instruction set that the libraries of secondary Abi is supported.
356 // In presence of a native bridge this might be different than the one secondary Abi used.
357 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
358 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
359 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100360
361 // If the runtimeIsa is the same as the primary isa, then we do nothing.
362 // Everything will be set up correctly because info.nativeLibraryDir will
363 // correspond to the right ISA.
364 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800365 ApplicationInfo modified = new ApplicationInfo(info);
366 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
367 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100368 }
369 }
Tao Baic9a02372016-01-12 15:02:24 -0800370 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100371 }
372
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800373 @Override
374 public ActivityInfo getActivityInfo(ComponentName className, int flags)
375 throws NameNotFoundException {
376 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700377 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800378 if (ai != null) {
379 return ai;
380 }
381 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700382 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800383 }
384
385 throw new NameNotFoundException(className.toString());
386 }
387
388 @Override
389 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
390 throws NameNotFoundException {
391 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700392 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800393 if (ai != null) {
394 return ai;
395 }
396 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700397 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800398 }
399
400 throw new NameNotFoundException(className.toString());
401 }
402
403 @Override
404 public ServiceInfo getServiceInfo(ComponentName className, int flags)
405 throws NameNotFoundException {
406 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700407 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800408 if (si != null) {
409 return si;
410 }
411 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700412 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800413 }
414
415 throw new NameNotFoundException(className.toString());
416 }
417
418 @Override
419 public ProviderInfo getProviderInfo(ComponentName className, int flags)
420 throws NameNotFoundException {
421 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700422 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800423 if (pi != null) {
424 return pi;
425 }
426 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700427 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800428 }
429
430 throw new NameNotFoundException(className.toString());
431 }
432
433 @Override
434 public String[] getSystemSharedLibraryNames() {
435 try {
436 return mPM.getSystemSharedLibraryNames();
437 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700438 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800439 }
440 }
441
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800442 /** @hide */
443 @Override
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700444 public @NonNull String getServicesSystemSharedLibraryPackageName() {
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800445 try {
446 return mPM.getServicesSystemSharedLibraryPackageName();
447 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700448 throw e.rethrowFromSystemServer();
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800449 }
450 }
451
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700452 /**
453 * @hide
454 */
455 public @NonNull String getSharedSystemSharedLibraryPackageName() {
456 try {
457 return mPM.getSharedSystemSharedLibraryPackageName();
458 } catch (RemoteException e) {
459 throw e.rethrowFromSystemServer();
460 }
461 }
462
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800463 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700464 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800465 public FeatureInfo[] getSystemAvailableFeatures() {
466 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700467 ParceledListSlice<FeatureInfo> parceledList =
468 mPM.getSystemAvailableFeatures();
469 if (parceledList == null) {
470 return new FeatureInfo[0];
471 }
472 final List<FeatureInfo> list = parceledList.getList();
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700473 final FeatureInfo[] res = new FeatureInfo[list.size()];
474 for (int i = 0; i < res.length; i++) {
475 res[i] = list.get(i);
476 }
477 return res;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800478 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700479 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800480 }
481 }
482
483 @Override
484 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700485 return hasSystemFeature(name, 0);
486 }
487
488 @Override
489 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800490 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700491 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800492 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700493 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800494 }
495 }
496
497 @Override
498 public int checkPermission(String permName, String pkgName) {
499 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800500 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800501 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700502 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800503 }
504 }
505
506 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700507 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
508 try {
509 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
510 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700511 throw e.rethrowFromSystemServer();
Svet Ganovad3b2972015-07-07 22:49:17 -0700512 }
513 }
514
Svet Ganovf1b7f202015-07-29 08:33:42 -0700515 /**
516 * @hide
517 */
518 @Override
519 public String getPermissionControllerPackageName() {
520 synchronized (mLock) {
521 if (mPermissionsControllerPackageName == null) {
522 try {
523 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
524 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700525 throw e.rethrowFromSystemServer();
Svet Ganovf1b7f202015-07-29 08:33:42 -0700526 }
527 }
528 return mPermissionsControllerPackageName;
529 }
530 }
531
Svet Ganovad3b2972015-07-07 22:49:17 -0700532 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800533 public boolean addPermission(PermissionInfo info) {
534 try {
535 return mPM.addPermission(info);
536 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700537 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800538 }
539 }
540
541 @Override
542 public boolean addPermissionAsync(PermissionInfo info) {
543 try {
544 return mPM.addPermissionAsync(info);
545 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700546 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800547 }
548 }
549
550 @Override
551 public void removePermission(String name) {
552 try {
553 mPM.removePermission(name);
554 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700555 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800556 }
557 }
558
559 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700560 public void grantRuntimePermission(String packageName, String permissionName,
561 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800562 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700563 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800564 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700565 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800566 }
567 }
568
569 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700570 public void revokeRuntimePermission(String packageName, String permissionName,
571 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800572 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700573 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
574 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700575 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700576 }
577 }
578
579 @Override
580 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
581 try {
582 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
583 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700584 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700585 }
586 }
587
588 @Override
589 public void updatePermissionFlags(String permissionName, String packageName,
590 int flagMask, int flagValues, UserHandle user) {
591 try {
592 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
593 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800594 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700595 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800596 }
597 }
598
599 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700600 public boolean shouldShowRequestPermissionRationale(String permission) {
601 try {
602 return mPM.shouldShowRequestPermissionRationale(permission,
603 mContext.getPackageName(), mContext.getUserId());
604 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700605 throw e.rethrowFromSystemServer();
Svetoslav20770dd2015-05-29 15:43:04 -0700606 }
607 }
608
609 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800610 public int checkSignatures(String pkg1, String pkg2) {
611 try {
612 return mPM.checkSignatures(pkg1, pkg2);
613 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700614 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800615 }
616 }
617
618 @Override
619 public int checkSignatures(int uid1, int uid2) {
620 try {
621 return mPM.checkUidSignatures(uid1, uid2);
622 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700623 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800624 }
625 }
626
627 @Override
628 public String[] getPackagesForUid(int uid) {
629 try {
630 return mPM.getPackagesForUid(uid);
631 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700632 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800633 }
634 }
635
636 @Override
637 public String getNameForUid(int uid) {
638 try {
639 return mPM.getNameForUid(uid);
640 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700641 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800642 }
643 }
644
645 @Override
646 public int getUidForSharedUser(String sharedUserName)
647 throws NameNotFoundException {
648 try {
649 int uid = mPM.getUidForSharedUser(sharedUserName);
650 if(uid != -1) {
651 return uid;
652 }
653 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700654 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800655 }
656 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
657 }
658
Kenny Roote6cd0c72011-05-19 12:48:14 -0700659 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800660 @Override
661 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700662 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700663 }
664
665 /** @hide */
666 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700667 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700668 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800669 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700670 ParceledListSlice<PackageInfo> parceledList =
671 mPM.getInstalledPackages(flags, userId);
672 if (parceledList == null) {
673 return Collections.emptyList();
674 }
675 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800676 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700677 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800678 }
679 }
680
Kenny Roote6cd0c72011-05-19 12:48:14 -0700681 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800682 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800683 public List<PackageInfo> getPackagesHoldingPermissions(
684 String[] permissions, int flags) {
685 final int userId = mContext.getUserId();
686 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700687 ParceledListSlice<PackageInfo> parceledList =
688 mPM.getPackagesHoldingPermissions(permissions, flags, userId);
689 if (parceledList == null) {
690 return Collections.emptyList();
691 }
692 return parceledList.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800693 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700694 throw e.rethrowFromSystemServer();
Dianne Hackborne7991752013-01-16 17:56:46 -0800695 }
696 }
697
698 @SuppressWarnings("unchecked")
699 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800700 public List<ApplicationInfo> getInstalledApplications(int flags) {
Bartosz Fabianowski11334242016-11-17 20:49:16 +0100701 return getInstalledApplicationsAsUser(flags, mContext.getUserId());
702 }
703
704 /** @hide */
705 @SuppressWarnings("unchecked")
706 @Override
707 public List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800708 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700709 ParceledListSlice<ApplicationInfo> parceledList =
710 mPM.getInstalledApplications(flags, userId);
711 if (parceledList == null) {
712 return Collections.emptyList();
713 }
714 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800715 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700716 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800717 }
718 }
719
Svet Ganov2acf0632015-11-24 19:10:59 -0800720 /** @hide */
721 @SuppressWarnings("unchecked")
722 @Override
723 public List<EphemeralApplicationInfo> getEphemeralApplications() {
724 try {
725 ParceledListSlice<EphemeralApplicationInfo> slice =
726 mPM.getEphemeralApplications(mContext.getUserId());
727 if (slice != null) {
728 return slice.getList();
729 }
730 return Collections.emptyList();
731 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700732 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800733 }
734 }
735
736 /** @hide */
737 @Override
738 public Drawable getEphemeralApplicationIcon(String packageName) {
739 try {
740 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
741 packageName, mContext.getUserId());
742 if (bitmap != null) {
743 return new BitmapDrawable(null, bitmap);
744 }
745 return null;
746 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700747 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800748 }
749 }
750
751 @Override
752 public boolean isEphemeralApplication() {
753 try {
754 return mPM.isEphemeralApplication(
755 mContext.getPackageName(), mContext.getUserId());
756 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700757 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800758 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800759 }
760
761 @Override
762 public int getEphemeralCookieMaxSizeBytes() {
763 return Settings.Global.getInt(mContext.getContentResolver(),
764 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
765 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
766 }
767
768 @Override
769 public @NonNull byte[] getEphemeralCookie() {
770 try {
771 final byte[] cookie = mPM.getEphemeralApplicationCookie(
772 mContext.getPackageName(), mContext.getUserId());
773 if (cookie != null) {
774 return cookie;
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700775 } else {
776 return EmptyArray.BYTE;
Svet Ganov2acf0632015-11-24 19:10:59 -0800777 }
778 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700779 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800780 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800781 }
782
783 @Override
784 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
785 try {
786 return mPM.setEphemeralApplicationCookie(
787 mContext.getPackageName(), cookie, mContext.getUserId());
788 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700789 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800790 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800791 }
792
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800793 @Override
794 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700795 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700796 }
797
798 @Override
799 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800800 try {
801 return mPM.resolveIntent(
802 intent,
803 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700804 flags,
805 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800806 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700807 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800808 }
809 }
810
811 @Override
812 public List<ResolveInfo> queryIntentActivities(Intent intent,
813 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700814 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700815 }
816
817 /** @hide Same as above but for a specific user */
818 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700819 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700820 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700821 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800822 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700823 ParceledListSlice<ResolveInfo> parceledList =
824 mPM.queryIntentActivities(intent,
825 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
826 flags, userId);
827 if (parceledList == null) {
828 return Collections.emptyList();
829 }
830 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800831 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700832 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800833 }
834 }
835
836 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700837 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800838 public List<ResolveInfo> queryIntentActivityOptions(
839 ComponentName caller, Intent[] specifics, Intent intent,
840 int flags) {
841 final ContentResolver resolver = mContext.getContentResolver();
842
843 String[] specificTypes = null;
844 if (specifics != null) {
845 final int N = specifics.length;
846 for (int i=0; i<N; i++) {
847 Intent sp = specifics[i];
848 if (sp != null) {
849 String t = sp.resolveTypeIfNeeded(resolver);
850 if (t != null) {
851 if (specificTypes == null) {
852 specificTypes = new String[N];
853 }
854 specificTypes[i] = t;
855 }
856 }
857 }
858 }
859
860 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700861 ParceledListSlice<ResolveInfo> parceledList =
862 mPM.queryIntentActivityOptions(caller, specifics, specificTypes, intent,
863 intent.resolveTypeIfNeeded(resolver), flags, mContext.getUserId());
864 if (parceledList == null) {
865 return Collections.emptyList();
866 }
867 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800868 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700869 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800870 }
871 }
872
Amith Yamasanif203aee2012-08-29 18:41:53 -0700873 /**
874 * @hide
875 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800876 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700877 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700878 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800879 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700880 ParceledListSlice<ResolveInfo> parceledList =
881 mPM.queryIntentReceivers(intent,
882 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
883 flags, userId);
884 if (parceledList == null) {
885 return Collections.emptyList();
886 }
887 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800888 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700889 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800890 }
891 }
892
893 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700894 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700895 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700896 }
897
898 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800899 public ResolveInfo resolveService(Intent intent, int flags) {
900 try {
901 return mPM.resolveService(
902 intent,
903 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700904 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700905 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800906 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700907 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800908 }
909 }
910
911 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700912 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700913 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800914 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700915 ParceledListSlice<ResolveInfo> parceledList =
916 mPM.queryIntentServices(intent,
917 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
918 flags, userId);
919 if (parceledList == null) {
920 return Collections.emptyList();
921 }
922 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800923 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700924 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800925 }
926 }
927
928 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700929 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700930 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700931 }
932
933 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700934 @SuppressWarnings("unchecked")
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700935 public List<ResolveInfo> queryIntentContentProvidersAsUser(
936 Intent intent, int flags, int userId) {
937 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700938 ParceledListSlice<ResolveInfo> parceledList =
939 mPM.queryIntentContentProviders(intent,
940 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
941 flags, userId);
942 if (parceledList == null) {
943 return Collections.emptyList();
944 }
945 return parceledList.getList();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700946 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700947 throw e.rethrowFromSystemServer();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700948 }
949 }
950
951 @Override
952 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
953 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
954 }
955
956 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100957 public ProviderInfo resolveContentProvider(String name, int flags) {
958 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
959 }
960
961 /** @hide **/
962 @Override
963 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800964 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100965 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800966 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700967 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800968 }
969 }
970
971 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700972 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800973 public List<ProviderInfo> queryContentProviders(String processName,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700974 int uid, int flags) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800975 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700976 ParceledListSlice<ProviderInfo> slice =
977 mPM.queryContentProviders(processName, uid, flags);
978 return slice != null ? slice.getList() : Collections.<ProviderInfo>emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800979 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700980 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800981 }
982 }
983
984 @Override
985 public InstrumentationInfo getInstrumentationInfo(
986 ComponentName className, int flags)
987 throws NameNotFoundException {
988 try {
989 InstrumentationInfo ii = mPM.getInstrumentationInfo(
990 className, flags);
991 if (ii != null) {
992 return ii;
993 }
994 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700995 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800996 }
997
998 throw new NameNotFoundException(className.toString());
999 }
1000
1001 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001002 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001003 public List<InstrumentationInfo> queryInstrumentation(
1004 String targetPackage, int flags) {
1005 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001006 ParceledListSlice<InstrumentationInfo> parceledList =
1007 mPM.queryInstrumentation(targetPackage, flags);
1008 if (parceledList == null) {
1009 return Collections.emptyList();
1010 }
1011 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001012 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001013 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001014 }
1015 }
1016
Alan Viveretteecd585a2015-04-13 10:32:51 -07001017 @Nullable
1018 @Override
1019 public Drawable getDrawable(String packageName, @DrawableRes int resId,
1020 @Nullable ApplicationInfo appInfo) {
1021 final ResourceName name = new ResourceName(packageName, resId);
1022 final Drawable cachedIcon = getCachedIcon(name);
1023 if (cachedIcon != null) {
1024 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001025 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001026
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001027 if (appInfo == null) {
1028 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001029 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001030 } catch (NameNotFoundException e) {
1031 return null;
1032 }
1033 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001034
1035 if (resId != 0) {
1036 try {
1037 final Resources r = getResourcesForApplication(appInfo);
1038 final Drawable dr = r.getDrawable(resId, null);
1039 if (dr != null) {
1040 putCachedIcon(name, dr);
1041 }
1042
1043 if (false) {
1044 RuntimeException e = new RuntimeException("here");
1045 e.fillInStackTrace();
1046 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
1047 + " from package " + packageName
1048 + ": app scale=" + r.getCompatibilityInfo().applicationScale
1049 + ", caller scale=" + mContext.getResources()
1050 .getCompatibilityInfo().applicationScale,
1051 e);
1052 }
Ricky Wai3ce46252015-04-15 16:12:22 +01001053 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001054 Log.v(TAG, "Getting drawable 0x"
1055 + Integer.toHexString(resId) + " from " + r
1056 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +01001057 }
1058 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -07001059 } catch (NameNotFoundException e) {
1060 Log.w("PackageManager", "Failure retrieving resources for "
1061 + appInfo.packageName);
1062 } catch (Resources.NotFoundException e) {
1063 Log.w("PackageManager", "Failure retrieving resources for "
1064 + appInfo.packageName + ": " + e.getMessage());
1065 } catch (Exception e) {
1066 // If an exception was thrown, fall through to return
1067 // default icon.
1068 Log.w("PackageManager", "Failure retrieving icon 0x"
1069 + Integer.toHexString(resId) + " in package "
1070 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001071 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001072 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001073
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001074 return null;
1075 }
1076
1077 @Override public Drawable getActivityIcon(ComponentName activityName)
1078 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001079 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001080 }
1081
1082 @Override public Drawable getActivityIcon(Intent intent)
1083 throws NameNotFoundException {
1084 if (intent.getComponent() != null) {
1085 return getActivityIcon(intent.getComponent());
1086 }
1087
1088 ResolveInfo info = resolveActivity(
1089 intent, PackageManager.MATCH_DEFAULT_ONLY);
1090 if (info != null) {
1091 return info.activityInfo.loadIcon(this);
1092 }
1093
Romain Guy39fe17c2011-11-30 10:34:07 -08001094 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001095 }
1096
1097 @Override public Drawable getDefaultActivityIcon() {
1098 return Resources.getSystem().getDrawable(
1099 com.android.internal.R.drawable.sym_def_app_icon);
1100 }
1101
1102 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1103 return info.loadIcon(this);
1104 }
1105
1106 @Override public Drawable getApplicationIcon(String packageName)
1107 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001108 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001109 }
1110
1111 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001112 public Drawable getActivityBanner(ComponentName activityName)
1113 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001114 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001115 }
1116
1117 @Override
1118 public Drawable getActivityBanner(Intent intent)
1119 throws NameNotFoundException {
1120 if (intent.getComponent() != null) {
1121 return getActivityBanner(intent.getComponent());
1122 }
1123
1124 ResolveInfo info = resolveActivity(
1125 intent, PackageManager.MATCH_DEFAULT_ONLY);
1126 if (info != null) {
1127 return info.activityInfo.loadBanner(this);
1128 }
1129
1130 throw new NameNotFoundException(intent.toUri(0));
1131 }
1132
1133 @Override
1134 public Drawable getApplicationBanner(ApplicationInfo info) {
1135 return info.loadBanner(this);
1136 }
1137
1138 @Override
1139 public Drawable getApplicationBanner(String packageName)
1140 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001141 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001142 }
1143
1144 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001145 public Drawable getActivityLogo(ComponentName activityName)
1146 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001147 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001148 }
1149
1150 @Override
1151 public Drawable getActivityLogo(Intent intent)
1152 throws NameNotFoundException {
1153 if (intent.getComponent() != null) {
1154 return getActivityLogo(intent.getComponent());
1155 }
1156
1157 ResolveInfo info = resolveActivity(
1158 intent, PackageManager.MATCH_DEFAULT_ONLY);
1159 if (info != null) {
1160 return info.activityInfo.loadLogo(this);
1161 }
1162
1163 throw new NameNotFoundException(intent.toUri(0));
1164 }
1165
1166 @Override
1167 public Drawable getApplicationLogo(ApplicationInfo info) {
1168 return info.loadLogo(this);
1169 }
1170
1171 @Override
1172 public Drawable getApplicationLogo(String packageName)
1173 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001174 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001175 }
1176
Svetoslavc7d62f02014-09-04 15:39:54 -07001177 @Override
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001178 public Drawable getManagedUserBadgedDrawable(Drawable drawable, Rect badgeLocation,
1179 int badgeDensity) {
1180 Drawable badgeDrawable = getDrawableForDensity(
1181 com.android.internal.R.drawable.ic_corp_badge, badgeDensity);
1182 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1183 }
1184
1185 @Override
Svetoslavc7d62f02014-09-04 15:39:54 -07001186 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1187 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1188 if (badgeResId == 0) {
1189 return icon;
1190 }
1191 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1192 return getBadgedDrawable(icon, badgeIcon, null, true);
1193 }
1194
1195 @Override
1196 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1197 Rect badgeLocation, int badgeDensity) {
1198 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1199 if (badgeDrawable == null) {
1200 return drawable;
1201 }
1202 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1203 }
1204
1205 @Override
1206 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001207 return getManagedProfileIconForDensity(user, com.android.internal.R.drawable.ic_corp_badge,
1208 density);
Selim Cineke6ff9462016-01-15 15:07:06 -08001209 }
1210
1211 @Override
1212 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001213 return getManagedProfileIconForDensity(user,
1214 com.android.internal.R.drawable.ic_corp_badge_no_background, density);
Selim Cineke6ff9462016-01-15 15:07:06 -08001215 }
1216
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001217 private Drawable getDrawableForDensity(int drawableId, int density) {
1218 if (density <= 0) {
1219 density = mContext.getResources().getDisplayMetrics().densityDpi;
1220 }
1221 return Resources.getSystem().getDrawableForDensity(drawableId, density);
1222 }
1223
1224 private Drawable getManagedProfileIconForDensity(UserHandle user, int drawableId, int density) {
Tony Mak8673b282016-03-21 21:10:59 +00001225 if (isManagedProfile(user.getIdentifier())) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001226 return getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001227 }
1228 return null;
1229 }
1230
1231 @Override
1232 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
Tony Mak8673b282016-03-21 21:10:59 +00001233 if (isManagedProfile(user.getIdentifier())) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001234 return Resources.getSystem().getString(
1235 com.android.internal.R.string.managed_profile_label_badge, label);
1236 }
1237 return label;
1238 }
1239
Alan Viveretteecd585a2015-04-13 10:32:51 -07001240 @Override
1241 public Resources getResourcesForActivity(ComponentName activityName)
1242 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001243 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001244 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001245 }
1246
Alan Viveretteecd585a2015-04-13 10:32:51 -07001247 @Override
1248 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1249 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001250 if (app.packageName.equals("system")) {
1251 return mContext.mMainThread.getSystemContext().getResources();
1252 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001253 final boolean sameUid = (app.uid == Process.myUid());
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001254 final Resources r = mContext.mMainThread.getTopLevelResources(
Adam Lesinskic82f28a2016-06-08 17:19:09 -07001255 sameUid ? app.sourceDir : app.publicSourceDir,
1256 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
1257 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1258 mContext.mPackageInfo);
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001259 if (r != null) {
1260 return r;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001261 }
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001262 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1263
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001264 }
1265
Alan Viveretteecd585a2015-04-13 10:32:51 -07001266 @Override
1267 public Resources getResourcesForApplication(String appPackageName)
1268 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001269 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001270 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001271 }
1272
Amith Yamasani98edc952012-09-25 14:09:27 -07001273 /** @hide */
1274 @Override
1275 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1276 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001277 if (userId < 0) {
1278 throw new IllegalArgumentException(
1279 "Call does not support special user #" + userId);
1280 }
1281 if ("system".equals(appPackageName)) {
1282 return mContext.mMainThread.getSystemContext().getResources();
1283 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001284 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001285 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001286 if (ai != null) {
1287 return getResourcesForApplication(ai);
1288 }
1289 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001290 throw e.rethrowFromSystemServer();
Amith Yamasani98edc952012-09-25 14:09:27 -07001291 }
1292 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1293 }
1294
Jeff Sharkeycd654482016-01-08 17:42:11 -07001295 volatile int mCachedSafeMode = -1;
1296
1297 @Override
1298 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001299 try {
1300 if (mCachedSafeMode < 0) {
1301 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1302 }
1303 return mCachedSafeMode != 0;
1304 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001305 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001306 }
1307 }
1308
Svetoslavf7c06eb2015-06-10 18:43:22 -07001309 @Override
1310 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1311 synchronized (mPermissionListeners) {
1312 if (mPermissionListeners.get(listener) != null) {
1313 return;
1314 }
1315 OnPermissionsChangeListenerDelegate delegate =
1316 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1317 try {
1318 mPM.addOnPermissionsChangeListener(delegate);
1319 mPermissionListeners.put(listener, delegate);
1320 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001321 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001322 }
1323 }
1324 }
1325
1326 @Override
1327 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1328 synchronized (mPermissionListeners) {
1329 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1330 if (delegate != null) {
1331 try {
1332 mPM.removeOnPermissionsChangeListener(delegate);
1333 mPermissionListeners.remove(listener);
1334 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001335 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001336 }
1337 }
1338 }
1339 }
1340
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001341 static void configurationChanged() {
1342 synchronized (sSync) {
1343 sIconCache.clear();
1344 sStringCache.clear();
1345 }
1346 }
1347
1348 ApplicationPackageManager(ContextImpl context,
1349 IPackageManager pm) {
1350 mContext = context;
1351 mPM = pm;
1352 }
1353
Alan Viveretteecd585a2015-04-13 10:32:51 -07001354 @Nullable
1355 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001356 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001357 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001358 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1359 + name + ": " + wr);
1360 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001361 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001362 if (state != null) {
1363 if (DEBUG_ICONS) {
1364 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1365 }
1366 // Note: It's okay here to not use the newDrawable(Resources) variant
1367 // of the API. The ConstantState comes from a drawable that was
1368 // originally created by passing the proper app Resources instance
1369 // which means the state should already contain the proper
1370 // resources specific information (like density.) See
1371 // BitmapDrawable.BitmapState for instance.
1372 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001373 }
1374 // our entry has been purged
1375 sIconCache.remove(name);
1376 }
1377 }
1378 return null;
1379 }
1380
Alan Viveretteecd585a2015-04-13 10:32:51 -07001381 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001382 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001383 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001384 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001385 }
1386 }
1387
Romain Guy39fe17c2011-11-30 10:34:07 -08001388 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001389 boolean immediateGc = false;
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001390 if (cmd == ApplicationThreadConstants.EXTERNAL_STORAGE_UNAVAILABLE) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001391 immediateGc = true;
1392 }
1393 if (pkgList != null && (pkgList.length > 0)) {
1394 boolean needCleanup = false;
1395 for (String ssp : pkgList) {
1396 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001397 for (int i=sIconCache.size()-1; i>=0; i--) {
1398 ResourceName nm = sIconCache.keyAt(i);
1399 if (nm.packageName.equals(ssp)) {
1400 //Log.i(TAG, "Removing cached drawable for " + nm);
1401 sIconCache.removeAt(i);
1402 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001403 }
1404 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001405 for (int i=sStringCache.size()-1; i>=0; i--) {
1406 ResourceName nm = sStringCache.keyAt(i);
1407 if (nm.packageName.equals(ssp)) {
1408 //Log.i(TAG, "Removing cached string for " + nm);
1409 sStringCache.removeAt(i);
1410 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001411 }
1412 }
1413 }
1414 }
1415 if (needCleanup || hasPkgInfo) {
1416 if (immediateGc) {
1417 // Schedule an immediate gc.
1418 Runtime.getRuntime().gc();
1419 } else {
1420 ActivityThread.currentActivityThread().scheduleGcIdler();
1421 }
1422 }
1423 }
1424 }
1425
1426 private static final class ResourceName {
1427 final String packageName;
1428 final int iconId;
1429
1430 ResourceName(String _packageName, int _iconId) {
1431 packageName = _packageName;
1432 iconId = _iconId;
1433 }
1434
1435 ResourceName(ApplicationInfo aInfo, int _iconId) {
1436 this(aInfo.packageName, _iconId);
1437 }
1438
1439 ResourceName(ComponentInfo cInfo, int _iconId) {
1440 this(cInfo.applicationInfo.packageName, _iconId);
1441 }
1442
1443 ResourceName(ResolveInfo rInfo, int _iconId) {
1444 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1445 }
1446
1447 @Override
1448 public boolean equals(Object o) {
1449 if (this == o) return true;
1450 if (o == null || getClass() != o.getClass()) return false;
1451
1452 ResourceName that = (ResourceName) o;
1453
1454 if (iconId != that.iconId) return false;
1455 return !(packageName != null ?
1456 !packageName.equals(that.packageName) : that.packageName != null);
1457
1458 }
1459
1460 @Override
1461 public int hashCode() {
1462 int result;
1463 result = packageName.hashCode();
1464 result = 31 * result + iconId;
1465 return result;
1466 }
1467
1468 @Override
1469 public String toString() {
1470 return "{ResourceName " + packageName + " / " + iconId + "}";
1471 }
1472 }
1473
1474 private CharSequence getCachedString(ResourceName name) {
1475 synchronized (sSync) {
1476 WeakReference<CharSequence> wr = sStringCache.get(name);
1477 if (wr != null) { // we have the activity
1478 CharSequence cs = wr.get();
1479 if (cs != null) {
1480 return cs;
1481 }
1482 // our entry has been purged
1483 sStringCache.remove(name);
1484 }
1485 }
1486 return null;
1487 }
1488
1489 private void putCachedString(ResourceName name, CharSequence cs) {
1490 synchronized (sSync) {
1491 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1492 }
1493 }
1494
1495 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001496 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001497 ApplicationInfo appInfo) {
1498 ResourceName name = new ResourceName(packageName, resid);
1499 CharSequence text = getCachedString(name);
1500 if (text != null) {
1501 return text;
1502 }
1503 if (appInfo == null) {
1504 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001505 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001506 } catch (NameNotFoundException e) {
1507 return null;
1508 }
1509 }
1510 try {
1511 Resources r = getResourcesForApplication(appInfo);
1512 text = r.getText(resid);
1513 putCachedString(name, text);
1514 return text;
1515 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001516 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001517 + appInfo.packageName);
1518 } catch (RuntimeException e) {
1519 // If an exception was thrown, fall through to return
1520 // default icon.
1521 Log.w("PackageManager", "Failure retrieving text 0x"
1522 + Integer.toHexString(resid) + " in package "
1523 + packageName, e);
1524 }
1525 return null;
1526 }
1527
1528 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001529 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001530 ApplicationInfo appInfo) {
1531 if (appInfo == null) {
1532 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001533 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001534 } catch (NameNotFoundException e) {
1535 return null;
1536 }
1537 }
1538 try {
1539 Resources r = getResourcesForApplication(appInfo);
1540 return r.getXml(resid);
1541 } catch (RuntimeException e) {
1542 // If an exception was thrown, fall through to return
1543 // default icon.
1544 Log.w("PackageManager", "Failure retrieving xml 0x"
1545 + Integer.toHexString(resid) + " in package "
1546 + packageName, e);
1547 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001548 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001549 + appInfo.packageName);
1550 }
1551 return null;
1552 }
1553
1554 @Override
1555 public CharSequence getApplicationLabel(ApplicationInfo info) {
1556 return info.loadLabel(this);
1557 }
1558
1559 @Override
1560 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1561 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001562 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Todd Kennedya6793232016-02-24 22:46:00 +00001563 installerPackageName, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001564 }
1565
Christopher Tatef1977b42014-03-24 16:25:51 -07001566 @Override
1567 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1568 int flags, String installerPackageName) {
Todd Kennedya6793232016-02-24 22:46:00 +00001569 installCommon(packageURI, observer, flags, installerPackageName, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001570 }
1571
1572 private void installCommon(Uri packageURI,
1573 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001574 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001575 if (!"file".equals(packageURI.getScheme())) {
1576 throw new UnsupportedOperationException("Only file:// URIs are supported");
1577 }
Jeff Sharkey513a0742014-07-08 17:10:32 -07001578
1579 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001580 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001581 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
Todd Kennedya6793232016-02-24 22:46:00 +00001582 userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001583 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001584 throw e.rethrowFromSystemServer();
rich cannings706e8ba2012-08-20 13:20:14 -07001585 }
1586 }
1587
1588 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001589 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001590 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001591 }
1592
1593 @Override
1594 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001595 throws NameNotFoundException {
1596 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001597 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001598 if (res == INSTALL_FAILED_INVALID_URI) {
1599 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1600 }
1601 return res;
1602 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001603 throw e.rethrowFromSystemServer();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001604 }
1605 }
1606
1607 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001608 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001609 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001610 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001611 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001612 throw e.rethrowFromSystemServer();
Kenny Root5ab21572011-07-27 11:11:19 -07001613 }
1614 }
1615
1616 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001617 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1618 long millisecondsToDelay) {
1619 try {
1620 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1621 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001622 throw e.rethrowFromSystemServer();
rich canningsd9ef3e52012-08-22 14:28:05 -07001623 }
1624 }
1625
1626 @Override
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001627 public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001628 try {
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001629 mPM.verifyIntentFilter(id, verificationCode, failedDomains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001630 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001631 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001632 }
1633 }
1634
1635 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001636 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001637 try {
1638 return mPM.getIntentVerificationStatus(packageName, userId);
1639 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001640 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001641 }
1642 }
1643
1644 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001645 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001646 try {
1647 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1648 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001649 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001650 }
1651 }
1652
1653 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001654 @SuppressWarnings("unchecked")
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001655 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1656 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001657 ParceledListSlice<IntentFilterVerificationInfo> parceledList =
1658 mPM.getIntentFilterVerifications(packageName);
1659 if (parceledList == null) {
1660 return Collections.emptyList();
1661 }
1662 return parceledList.getList();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001663 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001664 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001665 }
1666 }
1667
1668 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001669 @SuppressWarnings("unchecked")
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001670 public List<IntentFilter> getAllIntentFilters(String packageName) {
1671 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001672 ParceledListSlice<IntentFilter> parceledList =
1673 mPM.getAllIntentFilters(packageName);
1674 if (parceledList == null) {
1675 return Collections.emptyList();
1676 }
1677 return parceledList.getList();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001678 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001679 throw e.rethrowFromSystemServer();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001680 }
1681 }
1682
1683 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001684 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001685 try {
1686 return mPM.getDefaultBrowserPackageName(userId);
1687 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001688 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001689 }
1690 }
1691
1692 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001693 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001694 try {
1695 return mPM.setDefaultBrowserPackageName(packageName, userId);
1696 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001697 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001698 }
1699 }
1700
1701 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001702 public void setInstallerPackageName(String targetPackage,
1703 String installerPackageName) {
1704 try {
1705 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1706 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001707 throw e.rethrowFromSystemServer();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001708 }
1709 }
1710
1711 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001712 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001713 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001714 return mPM.getInstallerPackageName(packageName);
1715 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001716 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001717 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001718 }
1719
1720 @Override
1721 public int getMoveStatus(int moveId) {
1722 try {
1723 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001724 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001725 throw e.rethrowFromSystemServer();
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001726 }
1727 }
1728
1729 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001730 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1731 synchronized (mDelegates) {
1732 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1733 handler.getLooper());
1734 try {
1735 mPM.registerMoveCallback(delegate);
1736 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001737 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001738 }
1739 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001740 }
1741 }
1742
1743 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001744 public void unregisterMoveCallback(MoveCallback callback) {
1745 synchronized (mDelegates) {
1746 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1747 final MoveCallbackDelegate delegate = i.next();
1748 if (delegate.mCallback == callback) {
1749 try {
1750 mPM.unregisterMoveCallback(delegate);
1751 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001752 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001753 }
1754 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001755 }
1756 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001757 }
1758 }
1759
1760 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001761 public int movePackage(String packageName, VolumeInfo vol) {
1762 try {
1763 final String volumeUuid;
1764 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1765 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1766 } else if (vol.isPrimaryPhysical()) {
1767 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1768 } else {
1769 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1770 }
1771
1772 return mPM.movePackage(packageName, volumeUuid);
1773 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001774 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001775 }
1776 }
1777
1778 @Override
1779 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001780 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001781 if (app.isInternal()) {
1782 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1783 } else if (app.isExternalAsec()) {
1784 return storage.getPrimaryPhysicalVolume();
1785 } else {
1786 return storage.findVolumeByUuid(app.volumeUuid);
1787 }
1788 }
1789
1790 @Override
1791 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1792 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1793 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001794 final List<VolumeInfo> vols = storage.getVolumes();
1795 final List<VolumeInfo> candidates = new ArrayList<>();
1796 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001797 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001798 candidates.add(vol);
1799 }
1800 }
1801 return candidates;
1802 }
1803
Makoto Onukif34db0a2016-02-17 11:17:15 -08001804 private boolean isPackageCandidateVolume(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001805 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1806 final boolean forceAllowOnExternal = Settings.Global.getInt(
1807 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001808 // Private internal is always an option
1809 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1810 return true;
1811 }
1812
1813 // System apps and apps demanding internal storage can't be moved
1814 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001815 if (app.isSystemApp()) {
1816 return false;
1817 }
1818 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001819 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1820 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001821 return false;
1822 }
1823
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001824 // Gotta be able to write there
1825 if (!vol.isMountedWritable()) {
1826 return false;
1827 }
1828
1829 // Moving into an ASEC on public primary is only option internal
1830 if (vol.isPrimaryPhysical()) {
1831 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001832 }
1833
Makoto Onukif34db0a2016-02-17 11:17:15 -08001834 // Some apps can't be moved. (e.g. device admins)
1835 try {
1836 if (mPM.isPackageDeviceAdminOnAnyUser(app.packageName)) {
1837 return false;
1838 }
1839 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001840 throw e.rethrowFromSystemServer();
Makoto Onukif34db0a2016-02-17 11:17:15 -08001841 }
1842
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001843 // Otherwise we can move to any private volume
1844 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1845 }
1846
1847 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001848 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001849 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001850 final String volumeUuid;
1851 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1852 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1853 } else if (vol.isPrimaryPhysical()) {
1854 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1855 } else {
1856 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1857 }
1858
1859 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001860 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001861 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001862 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001863 }
1864
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001865 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001866 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1867 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1868 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001869 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001870 }
1871
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001872 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001873 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1874 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1875 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1876 final List<VolumeInfo> vols = storage.getVolumes();
1877 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001878 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1879 storage.getPrimaryStorageUuid()) && currentVol != null) {
1880 // TODO: support moving primary physical to emulated volume
1881 candidates.add(currentVol);
1882 } else {
1883 for (VolumeInfo vol : vols) {
1884 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1885 candidates.add(vol);
1886 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001887 }
1888 }
1889 return candidates;
1890 }
1891
1892 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1893 // Private internal is always an option
1894 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1895 return true;
1896 }
1897
1898 // Gotta be able to write there
1899 if (!vol.isMountedWritable()) {
1900 return false;
1901 }
1902
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001903 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001904 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001905 }
1906
1907 @Override
1908 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001909 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001910 }
1911
1912 @Override
1913 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1914 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001915 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001916 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001917 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001918 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001919 }
1920 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001921
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001922 @Override
1923 public void clearApplicationUserData(String packageName,
1924 IPackageDataObserver observer) {
1925 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001926 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001927 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001928 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001929 }
1930 }
1931 @Override
1932 public void deleteApplicationCacheFiles(String packageName,
1933 IPackageDataObserver observer) {
1934 try {
1935 mPM.deleteApplicationCacheFiles(packageName, observer);
1936 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001937 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001938 }
1939 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001940
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001941 @Override
Suprabh Shukla78c9eb82016-04-12 15:51:35 -07001942 public void deleteApplicationCacheFilesAsUser(String packageName, int userId,
1943 IPackageDataObserver observer) {
1944 try {
1945 mPM.deleteApplicationCacheFilesAsUser(packageName, userId, observer);
1946 } catch (RemoteException e) {
1947 throw e.rethrowFromSystemServer();
1948 }
1949 }
1950
1951 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001952 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1953 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001954 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001955 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001956 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001957 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001958 }
1959 }
1960
1961 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001962 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001963 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001964 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001965 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001966 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001967 }
1968 }
1969
1970 @Override
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001971 public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended,
1972 int userId) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001973 try {
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001974 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, userId);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001975 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001976 throw e.rethrowFromSystemServer();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001977 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001978 }
1979
1980 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001981 public boolean isPackageSuspendedForUser(String packageName, int userId) {
1982 try {
1983 return mPM.isPackageSuspendedForUser(packageName, userId);
1984 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001985 throw e.rethrowFromSystemServer();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001986 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001987 }
1988
1989 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001990 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001991 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001992 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001993 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001994 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001995 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001996 }
1997 }
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001998
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001999 @Override
2000 public void addPackageToPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002001 Log.w(TAG, "addPackageToPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002002 }
2003
2004 @Override
2005 public void removePackageFromPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002006 Log.w(TAG, "removePackageFromPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002007 }
2008
2009 @Override
2010 public List<PackageInfo> getPreferredPackages(int flags) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002011 Log.w(TAG, "getPreferredPackages() is a no-op");
2012 return Collections.emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002013 }
2014
2015 @Override
2016 public void addPreferredActivity(IntentFilter filter,
2017 int match, ComponentName[] set, ComponentName activity) {
2018 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002019 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07002020 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002021 throw e.rethrowFromSystemServer();
Amith Yamasania3f133a2012-08-09 17:11:28 -07002022 }
2023 }
2024
2025 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002026 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07002027 ComponentName[] set, ComponentName activity, int userId) {
2028 try {
2029 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002030 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002031 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002032 }
2033 }
2034
2035 @Override
2036 public void replacePreferredActivity(IntentFilter filter,
2037 int match, ComponentName[] set, ComponentName activity) {
2038 try {
Robin Lee0e27c872015-09-28 14:37:40 +01002039 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002040 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002041 throw e.rethrowFromSystemServer();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002042 }
2043 }
2044
2045 @Override
2046 public void replacePreferredActivityAsUser(IntentFilter filter,
2047 int match, ComponentName[] set, ComponentName activity,
2048 int userId) {
2049 try {
2050 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002051 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002052 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002053 }
2054 }
2055
2056 @Override
2057 public void clearPackagePreferredActivities(String packageName) {
2058 try {
2059 mPM.clearPackagePreferredActivities(packageName);
2060 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002061 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002062 }
2063 }
2064
2065 @Override
2066 public int getPreferredActivities(List<IntentFilter> outFilters,
2067 List<ComponentName> outActivities, String packageName) {
2068 try {
2069 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2070 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002071 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002072 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002073 }
2074
2075 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002076 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2077 try {
2078 return mPM.getHomeActivities(outActivities);
2079 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002080 throw e.rethrowFromSystemServer();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002081 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002082 }
2083
2084 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002085 public void setComponentEnabledSetting(ComponentName componentName,
2086 int newState, int flags) {
2087 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002088 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002089 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002090 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002091 }
2092 }
2093
2094 @Override
2095 public int getComponentEnabledSetting(ComponentName componentName) {
2096 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002097 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002098 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002099 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002100 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002101 }
2102
2103 @Override
2104 public void setApplicationEnabledSetting(String packageName,
2105 int newState, int flags) {
2106 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002107 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002108 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002109 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002110 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002111 }
2112 }
2113
2114 @Override
2115 public int getApplicationEnabledSetting(String packageName) {
2116 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002117 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002118 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002119 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002120 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002121 }
2122
Amith Yamasani655d0e22013-06-12 14:19:10 -07002123 @Override
Sudheer Shankabbb3ff22015-07-09 15:39:23 +01002124 public void flushPackageRestrictionsAsUser(int userId) {
2125 try {
2126 mPM.flushPackageRestrictionsAsUser(userId);
2127 } catch (RemoteException e) {
2128 throw e.rethrowFromSystemServer();
2129 }
2130 }
2131
2132 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002133 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002134 UserHandle user) {
2135 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002136 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002137 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002138 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002139 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002140 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002141 }
2142
2143 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002144 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002145 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002146 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002147 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002148 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002149 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002150 }
2151
dcashmanc6f22492014-08-14 09:54:51 -07002152 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002153 @Override
2154 public KeySet getKeySetByAlias(String packageName, String alias) {
2155 Preconditions.checkNotNull(packageName);
2156 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002157 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002158 return mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002159 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002160 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002161 }
dcashman9d2f4412014-06-09 09:27:54 -07002162 }
2163
dcashmanc6f22492014-08-14 09:54:51 -07002164 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002165 @Override
2166 public KeySet getSigningKeySet(String packageName) {
2167 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002168 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002169 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002170 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002171 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002172 }
dcashman9d2f4412014-06-09 09:27:54 -07002173 }
2174
dcashmanc6f22492014-08-14 09:54:51 -07002175 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002176 @Override
2177 public boolean isSignedBy(String packageName, KeySet ks) {
2178 Preconditions.checkNotNull(packageName);
2179 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002180 try {
dcashmanc6f22492014-08-14 09:54:51 -07002181 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002182 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002183 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002184 }
2185 }
2186
dcashmanc6f22492014-08-14 09:54:51 -07002187 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002188 @Override
2189 public boolean isSignedByExactly(String packageName, KeySet ks) {
2190 Preconditions.checkNotNull(packageName);
2191 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002192 try {
dcashmanc6f22492014-08-14 09:54:51 -07002193 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002194 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002195 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002196 }
2197 }
2198
Kenny Root0aaa0d92011-09-12 16:42:55 -07002199 /**
2200 * @hide
2201 */
2202 @Override
2203 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2204 try {
2205 return mPM.getVerifierDeviceIdentity();
2206 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002207 throw e.rethrowFromSystemServer();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002208 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002209 }
2210
Jeff Hao9f60c082014-10-28 18:51:07 -07002211 /**
2212 * @hide
2213 */
2214 @Override
2215 public boolean isUpgrade() {
2216 try {
2217 return mPM.isUpgrade();
2218 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002219 throw e.rethrowFromSystemServer();
Jeff Hao9f60c082014-10-28 18:51:07 -07002220 }
2221 }
2222
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002223 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002224 public PackageInstaller getPackageInstaller() {
2225 synchronized (mLock) {
2226 if (mInstaller == null) {
2227 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002228 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002229 mContext.getPackageName(), mContext.getUserId());
2230 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002231 throw e.rethrowFromSystemServer();
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002232 }
2233 }
2234 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002235 }
2236 }
2237
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002238 @Override
2239 public boolean isPackageAvailable(String packageName) {
2240 try {
2241 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2242 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002243 throw e.rethrowFromSystemServer();
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002244 }
2245 }
2246
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002247 /**
2248 * @hide
2249 */
2250 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002251 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2252 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002253 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002254 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002255 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002256 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002257 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002258 }
2259 }
2260
2261 /**
2262 * @hide
2263 */
2264 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002265 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002266 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002267 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002268 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002269 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002270 }
2271 }
2272
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002273 /**
2274 * @hide
2275 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002276 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002277 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2278 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2279 return dr;
2280 }
2281 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2282 }
2283
2284 /**
2285 * @hide
2286 */
2287 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002288 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002289 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2290 if (bitmap == null) {
2291 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2292 }
2293 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002294 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002295 Drawable dr = null;
2296 if (itemInfo.packageName != null) {
2297 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2298 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002299 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002300 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002301 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002302 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002303 }
2304
2305 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2306 Rect badgeLocation, boolean tryBadgeInPlace) {
2307 final int badgedWidth = drawable.getIntrinsicWidth();
2308 final int badgedHeight = drawable.getIntrinsicHeight();
2309 final boolean canBadgeInPlace = tryBadgeInPlace
2310 && (drawable instanceof BitmapDrawable)
2311 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2312
2313 final Bitmap bitmap;
2314 if (canBadgeInPlace) {
2315 bitmap = ((BitmapDrawable) drawable).getBitmap();
2316 } else {
2317 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2318 }
2319 Canvas canvas = new Canvas(bitmap);
2320
2321 if (!canBadgeInPlace) {
2322 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2323 drawable.draw(canvas);
2324 }
2325
2326 if (badgeLocation != null) {
2327 if (badgeLocation.left < 0 || badgeLocation.top < 0
2328 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2329 throw new IllegalArgumentException("Badge location " + badgeLocation
2330 + " not in badged drawable bounds "
2331 + new Rect(0, 0, badgedWidth, badgedHeight));
2332 }
2333 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2334
2335 canvas.save();
2336 canvas.translate(badgeLocation.left, badgeLocation.top);
2337 badgeDrawable.draw(canvas);
2338 canvas.restore();
2339 } else {
2340 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2341 badgeDrawable.draw(canvas);
2342 }
2343
2344 if (!canBadgeInPlace) {
2345 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2346
2347 if (drawable instanceof BitmapDrawable) {
2348 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2349 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2350 }
2351
2352 return mergedDrawable;
2353 }
2354
2355 return drawable;
2356 }
2357
Tony Mak8673b282016-03-21 21:10:59 +00002358 private int getBadgeResIdForUser(int userId) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002359 // Return the framework-provided badge.
Tony Mak8673b282016-03-21 21:10:59 +00002360 if (isManagedProfile(userId)) {
Svetoslavc7d62f02014-09-04 15:39:54 -07002361 return com.android.internal.R.drawable.ic_corp_icon_badge;
2362 }
2363 return 0;
2364 }
2365
Tony Mak8673b282016-03-21 21:10:59 +00002366 private boolean isManagedProfile(int userId) {
2367 return getUserManager().isManagedProfile(userId);
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002368 }
2369
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002370 /** {@hide} */
2371 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2372 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002373 private static final int MSG_CREATED = 1;
2374 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002375
2376 final MoveCallback mCallback;
2377 final Handler mHandler;
2378
2379 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2380 mCallback = callback;
2381 mHandler = new Handler(looper, this);
2382 }
2383
2384 @Override
2385 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002386 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002387 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002388 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002389 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002390 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002391 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002392 }
2393 case MSG_STATUS_CHANGED: {
2394 final SomeArgs args = (SomeArgs) msg.obj;
2395 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2396 args.recycle();
2397 return true;
2398 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002399 }
2400 return false;
2401 }
2402
2403 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002404 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002405 final SomeArgs args = SomeArgs.obtain();
2406 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002407 args.arg2 = extras;
2408 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2409 }
2410
2411 @Override
2412 public void onStatusChanged(int moveId, int status, long estMillis) {
2413 final SomeArgs args = SomeArgs.obtain();
2414 args.argi1 = moveId;
2415 args.argi2 = status;
2416 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002417 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002418 }
2419 }
2420
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002421 private final ContextImpl mContext;
2422 private final IPackageManager mPM;
2423
2424 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002425 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2426 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2427 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2428 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002429
2430 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2431 mPermissionListeners = new ArrayMap<>();
2432
2433 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2434 implements Handler.Callback{
2435 private static final int MSG_PERMISSIONS_CHANGED = 1;
2436
2437 private final OnPermissionsChangedListener mListener;
2438 private final Handler mHandler;
2439
2440
2441 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2442 Looper looper) {
2443 mListener = listener;
2444 mHandler = new Handler(looper, this);
2445 }
2446
2447 @Override
2448 public void onPermissionsChanged(int uid) {
2449 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2450 }
2451
2452 @Override
2453 public boolean handleMessage(Message msg) {
2454 switch (msg.what) {
2455 case MSG_PERMISSIONS_CHANGED: {
2456 final int uid = msg.arg1;
2457 mListener.onPermissionsChanged(uid);
2458 return true;
2459 }
2460 }
2461 return false;
2462 }
2463 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002464}