blob: 6d716cc869eb80a56fb955350bf38fd90c64f418 [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;
Todd Kennedy5cbeb592016-02-24 22:45:00 +000033import android.content.pm.ContainerEncryptionParams;
Svet Ganov2acf0632015-11-24 19:10:59 -080034import android.content.pm.EphemeralApplicationInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080035import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070036import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080037import android.content.pm.IPackageDataObserver;
38import android.content.pm.IPackageDeleteObserver;
39import android.content.pm.IPackageInstallObserver;
40import android.content.pm.IPackageManager;
41import android.content.pm.IPackageMoveObserver;
42import android.content.pm.IPackageStatsObserver;
43import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080044import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070045import android.content.pm.KeySet;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080046import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070047import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010048import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080049import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070050import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080051import android.content.pm.PermissionGroupInfo;
52import android.content.pm.PermissionInfo;
53import android.content.pm.ProviderInfo;
54import android.content.pm.ResolveInfo;
55import android.content.pm.ServiceInfo;
Svetoslavc7d62f02014-09-04 15:39:54 -070056import android.content.pm.UserInfo;
Todd Kennedy5cbeb592016-02-24 22:45:00 +000057import android.content.pm.VerificationParams;
Kenny Root0aaa0d92011-09-12 16:42:55 -070058import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080059import android.content.res.Resources;
60import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070061import android.graphics.Bitmap;
62import android.graphics.Canvas;
63import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010064import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080065import android.graphics.drawable.Drawable;
66import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070067import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070068import android.os.Handler;
69import android.os.Looper;
70import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080071import android.os.Process;
72import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070073import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080074import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010075import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070076import android.os.storage.StorageManager;
77import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070078import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070079import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080080import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070081import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070082
83import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070084
85import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070086import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070087import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010088import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080089import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070090
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080091import java.lang.ref.WeakReference;
92import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080093import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070094import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080095import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070096import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070097import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080098
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070099/** @hide */
100public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800101 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800102 private final static boolean DEBUG_ICONS = false;
103
Svet Ganov2acf0632015-11-24 19:10:59 -0800104 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
105
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700106 // Default flags to use with PackageManager when no flags are given.
107 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
108
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700109 private final Object mLock = new Object();
110
111 @GuardedBy("mLock")
112 private UserManager mUserManager;
113 @GuardedBy("mLock")
114 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100115
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700116 @GuardedBy("mDelegates")
117 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
118
Svet Ganovf1b7f202015-07-29 08:33:42 -0700119 @GuardedBy("mLock")
120 private String mPermissionsControllerPackageName;
121
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100122 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700123 synchronized (mLock) {
124 if (mUserManager == null) {
125 mUserManager = UserManager.get(mContext);
126 }
127 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100128 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100129 }
130
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800131 @Override
132 public PackageInfo getPackageInfo(String packageName, int flags)
133 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100134 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
135 }
136
137 @Override
138 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
139 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800140 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100141 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800142 if (pi != null) {
143 return pi;
144 }
145 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700146 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800147 }
148
149 throw new NameNotFoundException(packageName);
150 }
151
152 @Override
153 public String[] currentToCanonicalPackageNames(String[] names) {
154 try {
155 return mPM.currentToCanonicalPackageNames(names);
156 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700157 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800158 }
159 }
160
161 @Override
162 public String[] canonicalToCurrentPackageNames(String[] names) {
163 try {
164 return mPM.canonicalToCurrentPackageNames(names);
165 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700166 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800167 }
168 }
169
170 @Override
171 public Intent getLaunchIntentForPackage(String packageName) {
172 // First see if the package has an INFO activity; the existence of
173 // such an activity is implied to be the desired front-door for the
174 // overall package (such as if it has multiple launcher entries).
175 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
176 intentToResolve.addCategory(Intent.CATEGORY_INFO);
177 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800178 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800179
180 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800181 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800182 // reuse the intent instance
183 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
184 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
185 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800186 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800187 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800188 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800189 return null;
190 }
191 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800192 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800193 intent.setClassName(ris.get(0).activityInfo.packageName,
194 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800195 return intent;
196 }
197
198 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700199 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
200 // Try to find a main leanback_launcher activity.
201 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
202 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
203 intentToResolve.setPackage(packageName);
204 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
205
206 if (ris == null || ris.size() <= 0) {
207 return null;
208 }
209 Intent intent = new Intent(intentToResolve);
210 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
211 intent.setClassName(ris.get(0).activityInfo.packageName,
212 ris.get(0).activityInfo.name);
213 return intent;
214 }
215
216 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700217 public int[] getPackageGids(String packageName) throws NameNotFoundException {
218 return getPackageGids(packageName, 0);
219 }
220
221 @Override
222 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800223 throws NameNotFoundException {
224 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700225 int[] gids = mPM.getPackageGids(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800226 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800227 return gids;
228 }
229 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700230 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800231 }
232
233 throw new NameNotFoundException(packageName);
234 }
235
236 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700237 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
238 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
239 }
240
241 @Override
242 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
243 return getPackageUidAsUser(packageName, 0, userId);
244 }
245
246 @Override
247 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800248 throws NameNotFoundException {
249 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700250 int uid = mPM.getPackageUid(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800251 if (uid >= 0) {
252 return uid;
253 }
254 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700255 throw e.rethrowAsRuntimeException();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800256 }
257
258 throw new NameNotFoundException(packageName);
259 }
260
261 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800262 public PermissionInfo getPermissionInfo(String name, int flags)
263 throws NameNotFoundException {
264 try {
265 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
266 if (pi != null) {
267 return pi;
268 }
269 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700270 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800271 }
272
273 throw new NameNotFoundException(name);
274 }
275
276 @Override
277 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
278 throws NameNotFoundException {
279 try {
280 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags);
281 if (pi != null) {
282 return pi;
283 }
284 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700285 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800286 }
287
288 throw new NameNotFoundException(group);
289 }
290
291 @Override
292 public PermissionGroupInfo getPermissionGroupInfo(String name,
293 int flags) throws NameNotFoundException {
294 try {
295 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
296 if (pgi != null) {
297 return pgi;
298 }
299 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700300 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800301 }
302
303 throw new NameNotFoundException(name);
304 }
305
306 @Override
307 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
308 try {
309 return mPM.getAllPermissionGroups(flags);
310 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700311 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800312 }
313 }
314
315 @Override
316 public ApplicationInfo getApplicationInfo(String packageName, int flags)
317 throws NameNotFoundException {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700318 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
319 }
320
321 @Override
322 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
323 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800324 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700325 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800326 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100327 // This is a temporary hack. Callers must use
328 // createPackageContext(packageName).getApplicationInfo() to
329 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800330 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800331 }
332 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700333 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800334 }
335
336 throw new NameNotFoundException(packageName);
337 }
338
Tao Baic9a02372016-01-12 15:02:24 -0800339 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100340 // If we're dealing with a multi-arch application that has both
341 // 32 and 64 bit shared libraries, we might need to choose the secondary
342 // depending on what the current runtime's instruction set is.
343 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
344 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500345
346 // Get the instruction set that the libraries of secondary Abi is supported.
347 // In presence of a native bridge this might be different than the one secondary Abi used.
348 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
349 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
350 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100351
352 // If the runtimeIsa is the same as the primary isa, then we do nothing.
353 // Everything will be set up correctly because info.nativeLibraryDir will
354 // correspond to the right ISA.
355 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800356 ApplicationInfo modified = new ApplicationInfo(info);
357 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
358 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100359 }
360 }
Tao Baic9a02372016-01-12 15:02:24 -0800361 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100362 }
363
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800364 @Override
365 public ActivityInfo getActivityInfo(ComponentName className, int flags)
366 throws NameNotFoundException {
367 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700368 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800369 if (ai != null) {
370 return ai;
371 }
372 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700373 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800374 }
375
376 throw new NameNotFoundException(className.toString());
377 }
378
379 @Override
380 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
381 throws NameNotFoundException {
382 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700383 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800384 if (ai != null) {
385 return ai;
386 }
387 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700388 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800389 }
390
391 throw new NameNotFoundException(className.toString());
392 }
393
394 @Override
395 public ServiceInfo getServiceInfo(ComponentName className, int flags)
396 throws NameNotFoundException {
397 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700398 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800399 if (si != null) {
400 return si;
401 }
402 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700403 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800404 }
405
406 throw new NameNotFoundException(className.toString());
407 }
408
409 @Override
410 public ProviderInfo getProviderInfo(ComponentName className, int flags)
411 throws NameNotFoundException {
412 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700413 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800414 if (pi != null) {
415 return pi;
416 }
417 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700418 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800419 }
420
421 throw new NameNotFoundException(className.toString());
422 }
423
424 @Override
425 public String[] getSystemSharedLibraryNames() {
426 try {
427 return mPM.getSystemSharedLibraryNames();
428 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700429 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800430 }
431 }
432
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800433 /** @hide */
434 @Override
435 public @Nullable String getServicesSystemSharedLibraryPackageName() {
436 try {
437 return mPM.getServicesSystemSharedLibraryPackageName();
438 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700439 throw e.rethrowAsRuntimeException();
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800440 }
441 }
442
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800443 @Override
444 public FeatureInfo[] getSystemAvailableFeatures() {
445 try {
446 return mPM.getSystemAvailableFeatures();
447 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700448 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800449 }
450 }
451
452 @Override
453 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700454 return hasSystemFeature(name, 0);
455 }
456
457 @Override
458 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800459 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700460 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800461 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700462 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800463 }
464 }
465
466 @Override
467 public int checkPermission(String permName, String pkgName) {
468 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800469 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800470 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700471 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800472 }
473 }
474
475 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700476 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
477 try {
478 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
479 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700480 throw e.rethrowAsRuntimeException();
Svet Ganovad3b2972015-07-07 22:49:17 -0700481 }
482 }
483
Svet Ganovf1b7f202015-07-29 08:33:42 -0700484 /**
485 * @hide
486 */
487 @Override
488 public String getPermissionControllerPackageName() {
489 synchronized (mLock) {
490 if (mPermissionsControllerPackageName == null) {
491 try {
492 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
493 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700494 throw e.rethrowAsRuntimeException();
Svet Ganovf1b7f202015-07-29 08:33:42 -0700495 }
496 }
497 return mPermissionsControllerPackageName;
498 }
499 }
500
Svet Ganovad3b2972015-07-07 22:49:17 -0700501 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800502 public boolean addPermission(PermissionInfo info) {
503 try {
504 return mPM.addPermission(info);
505 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700506 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800507 }
508 }
509
510 @Override
511 public boolean addPermissionAsync(PermissionInfo info) {
512 try {
513 return mPM.addPermissionAsync(info);
514 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700515 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800516 }
517 }
518
519 @Override
520 public void removePermission(String name) {
521 try {
522 mPM.removePermission(name);
523 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700524 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800525 }
526 }
527
528 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700529 public void grantRuntimePermission(String packageName, String permissionName,
530 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800531 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700532 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800533 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700534 throw e.rethrowAsRuntimeException();
Dianne Hackborne639da72012-02-21 15:11:13 -0800535 }
536 }
537
538 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700539 public void revokeRuntimePermission(String packageName, String permissionName,
540 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800541 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700542 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
543 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700544 throw e.rethrowAsRuntimeException();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700545 }
546 }
547
548 @Override
549 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
550 try {
551 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
552 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700553 throw e.rethrowAsRuntimeException();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700554 }
555 }
556
557 @Override
558 public void updatePermissionFlags(String permissionName, String packageName,
559 int flagMask, int flagValues, UserHandle user) {
560 try {
561 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
562 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800563 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700564 throw e.rethrowAsRuntimeException();
Dianne Hackborne639da72012-02-21 15:11:13 -0800565 }
566 }
567
568 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700569 public boolean shouldShowRequestPermissionRationale(String permission) {
570 try {
571 return mPM.shouldShowRequestPermissionRationale(permission,
572 mContext.getPackageName(), mContext.getUserId());
573 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700574 throw e.rethrowAsRuntimeException();
Svetoslav20770dd2015-05-29 15:43:04 -0700575 }
576 }
577
578 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800579 public int checkSignatures(String pkg1, String pkg2) {
580 try {
581 return mPM.checkSignatures(pkg1, pkg2);
582 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700583 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800584 }
585 }
586
587 @Override
588 public int checkSignatures(int uid1, int uid2) {
589 try {
590 return mPM.checkUidSignatures(uid1, uid2);
591 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700592 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800593 }
594 }
595
596 @Override
597 public String[] getPackagesForUid(int uid) {
598 try {
599 return mPM.getPackagesForUid(uid);
600 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700601 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800602 }
603 }
604
605 @Override
606 public String getNameForUid(int uid) {
607 try {
608 return mPM.getNameForUid(uid);
609 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700610 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800611 }
612 }
613
614 @Override
615 public int getUidForSharedUser(String sharedUserName)
616 throws NameNotFoundException {
617 try {
618 int uid = mPM.getUidForSharedUser(sharedUserName);
619 if(uid != -1) {
620 return uid;
621 }
622 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700623 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800624 }
625 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
626 }
627
Kenny Roote6cd0c72011-05-19 12:48:14 -0700628 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800629 @Override
630 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700631 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700632 }
633
634 /** @hide */
635 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700636 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800637 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800638 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
639 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800640 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700641 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800642 }
643 }
644
Kenny Roote6cd0c72011-05-19 12:48:14 -0700645 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800646 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800647 public List<PackageInfo> getPackagesHoldingPermissions(
648 String[] permissions, int flags) {
649 final int userId = mContext.getUserId();
650 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800651 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
652 permissions, flags, userId);
653 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800654 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700655 throw e.rethrowAsRuntimeException();
Dianne Hackborne7991752013-01-16 17:56:46 -0800656 }
657 }
658
659 @SuppressWarnings("unchecked")
660 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800661 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700662 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800663 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800664 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
665 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800666 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700667 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800668 }
669 }
670
Svet Ganov2acf0632015-11-24 19:10:59 -0800671 /** @hide */
672 @SuppressWarnings("unchecked")
673 @Override
674 public List<EphemeralApplicationInfo> getEphemeralApplications() {
675 try {
676 ParceledListSlice<EphemeralApplicationInfo> slice =
677 mPM.getEphemeralApplications(mContext.getUserId());
678 if (slice != null) {
679 return slice.getList();
680 }
681 return Collections.emptyList();
682 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700683 throw e.rethrowAsRuntimeException();
Svet Ganov2acf0632015-11-24 19:10:59 -0800684 }
685 }
686
687 /** @hide */
688 @Override
689 public Drawable getEphemeralApplicationIcon(String packageName) {
690 try {
691 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
692 packageName, mContext.getUserId());
693 if (bitmap != null) {
694 return new BitmapDrawable(null, bitmap);
695 }
696 return null;
697 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700698 throw e.rethrowAsRuntimeException();
Svet Ganov2acf0632015-11-24 19:10:59 -0800699 }
700 }
701
702 @Override
703 public boolean isEphemeralApplication() {
704 try {
705 return mPM.isEphemeralApplication(
706 mContext.getPackageName(), mContext.getUserId());
707 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700708 throw e.rethrowAsRuntimeException();
Svet Ganov2acf0632015-11-24 19:10:59 -0800709 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800710 }
711
712 @Override
713 public int getEphemeralCookieMaxSizeBytes() {
714 return Settings.Global.getInt(mContext.getContentResolver(),
715 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
716 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
717 }
718
719 @Override
720 public @NonNull byte[] getEphemeralCookie() {
721 try {
722 final byte[] cookie = mPM.getEphemeralApplicationCookie(
723 mContext.getPackageName(), mContext.getUserId());
724 if (cookie != null) {
725 return cookie;
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700726 } else {
727 return EmptyArray.BYTE;
Svet Ganov2acf0632015-11-24 19:10:59 -0800728 }
729 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700730 throw e.rethrowAsRuntimeException();
Svet Ganov2acf0632015-11-24 19:10:59 -0800731 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800732 }
733
734 @Override
735 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
736 try {
737 return mPM.setEphemeralApplicationCookie(
738 mContext.getPackageName(), cookie, mContext.getUserId());
739 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700740 throw e.rethrowAsRuntimeException();
Svet Ganov2acf0632015-11-24 19:10:59 -0800741 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800742 }
743
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800744 @Override
745 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700746 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700747 }
748
749 @Override
750 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800751 try {
752 return mPM.resolveIntent(
753 intent,
754 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700755 flags,
756 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800757 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700758 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800759 }
760 }
761
762 @Override
763 public List<ResolveInfo> queryIntentActivities(Intent intent,
764 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700765 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700766 }
767
768 /** @hide Same as above but for a specific user */
769 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700770 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700771 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800772 try {
773 return mPM.queryIntentActivities(
774 intent,
775 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700776 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700777 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800778 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700779 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800780 }
781 }
782
783 @Override
784 public List<ResolveInfo> queryIntentActivityOptions(
785 ComponentName caller, Intent[] specifics, Intent intent,
786 int flags) {
787 final ContentResolver resolver = mContext.getContentResolver();
788
789 String[] specificTypes = null;
790 if (specifics != null) {
791 final int N = specifics.length;
792 for (int i=0; i<N; i++) {
793 Intent sp = specifics[i];
794 if (sp != null) {
795 String t = sp.resolveTypeIfNeeded(resolver);
796 if (t != null) {
797 if (specificTypes == null) {
798 specificTypes = new String[N];
799 }
800 specificTypes[i] = t;
801 }
802 }
803 }
804 }
805
806 try {
807 return mPM.queryIntentActivityOptions(caller, specifics,
808 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700809 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800810 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700811 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800812 }
813 }
814
Amith Yamasanif203aee2012-08-29 18:41:53 -0700815 /**
816 * @hide
817 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800818 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700819 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800820 try {
821 return mPM.queryIntentReceivers(
822 intent,
823 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700824 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -0700825 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800826 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700827 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800828 }
829 }
830
831 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700832 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700833 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700834 }
835
836 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800837 public ResolveInfo resolveService(Intent intent, int flags) {
838 try {
839 return mPM.resolveService(
840 intent,
841 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700842 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700843 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800844 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700845 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800846 }
847 }
848
849 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700850 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800851 try {
852 return mPM.queryIntentServices(
853 intent,
854 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700855 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700856 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800857 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700858 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800859 }
860 }
861
862 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700863 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700864 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700865 }
866
867 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700868 public List<ResolveInfo> queryIntentContentProvidersAsUser(
869 Intent intent, int flags, int userId) {
870 try {
871 return mPM.queryIntentContentProviders(intent,
872 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
873 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700874 throw e.rethrowAsRuntimeException();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700875 }
876 }
877
878 @Override
879 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
880 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
881 }
882
883 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100884 public ProviderInfo resolveContentProvider(String name, int flags) {
885 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
886 }
887
888 /** @hide **/
889 @Override
890 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800891 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100892 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800893 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700894 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800895 }
896 }
897
898 @Override
899 public List<ProviderInfo> queryContentProviders(String processName,
900 int uid, int flags) {
901 try {
Dianne Hackborn28ec27c2015-08-03 15:28:28 -0700902 ParceledListSlice<ProviderInfo> slice
903 = mPM.queryContentProviders(processName, uid, flags);
904 return slice != null ? slice.getList() : null;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800905 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700906 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800907 }
908 }
909
910 @Override
911 public InstrumentationInfo getInstrumentationInfo(
912 ComponentName className, int flags)
913 throws NameNotFoundException {
914 try {
915 InstrumentationInfo ii = mPM.getInstrumentationInfo(
916 className, flags);
917 if (ii != null) {
918 return ii;
919 }
920 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700921 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800922 }
923
924 throw new NameNotFoundException(className.toString());
925 }
926
927 @Override
928 public List<InstrumentationInfo> queryInstrumentation(
929 String targetPackage, int flags) {
930 try {
931 return mPM.queryInstrumentation(targetPackage, flags);
932 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700933 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800934 }
935 }
936
Alan Viveretteecd585a2015-04-13 10:32:51 -0700937 @Nullable
938 @Override
939 public Drawable getDrawable(String packageName, @DrawableRes int resId,
940 @Nullable ApplicationInfo appInfo) {
941 final ResourceName name = new ResourceName(packageName, resId);
942 final Drawable cachedIcon = getCachedIcon(name);
943 if (cachedIcon != null) {
944 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800945 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700946
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800947 if (appInfo == null) {
948 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700949 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800950 } catch (NameNotFoundException e) {
951 return null;
952 }
953 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700954
955 if (resId != 0) {
956 try {
957 final Resources r = getResourcesForApplication(appInfo);
958 final Drawable dr = r.getDrawable(resId, null);
959 if (dr != null) {
960 putCachedIcon(name, dr);
961 }
962
963 if (false) {
964 RuntimeException e = new RuntimeException("here");
965 e.fillInStackTrace();
966 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
967 + " from package " + packageName
968 + ": app scale=" + r.getCompatibilityInfo().applicationScale
969 + ", caller scale=" + mContext.getResources()
970 .getCompatibilityInfo().applicationScale,
971 e);
972 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100973 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700974 Log.v(TAG, "Getting drawable 0x"
975 + Integer.toHexString(resId) + " from " + r
976 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100977 }
978 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700979 } catch (NameNotFoundException e) {
980 Log.w("PackageManager", "Failure retrieving resources for "
981 + appInfo.packageName);
982 } catch (Resources.NotFoundException e) {
983 Log.w("PackageManager", "Failure retrieving resources for "
984 + appInfo.packageName + ": " + e.getMessage());
985 } catch (Exception e) {
986 // If an exception was thrown, fall through to return
987 // default icon.
988 Log.w("PackageManager", "Failure retrieving icon 0x"
989 + Integer.toHexString(resId) + " in package "
990 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800991 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800992 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700993
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800994 return null;
995 }
996
997 @Override public Drawable getActivityIcon(ComponentName activityName)
998 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700999 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001000 }
1001
1002 @Override public Drawable getActivityIcon(Intent intent)
1003 throws NameNotFoundException {
1004 if (intent.getComponent() != null) {
1005 return getActivityIcon(intent.getComponent());
1006 }
1007
1008 ResolveInfo info = resolveActivity(
1009 intent, PackageManager.MATCH_DEFAULT_ONLY);
1010 if (info != null) {
1011 return info.activityInfo.loadIcon(this);
1012 }
1013
Romain Guy39fe17c2011-11-30 10:34:07 -08001014 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001015 }
1016
1017 @Override public Drawable getDefaultActivityIcon() {
1018 return Resources.getSystem().getDrawable(
1019 com.android.internal.R.drawable.sym_def_app_icon);
1020 }
1021
1022 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1023 return info.loadIcon(this);
1024 }
1025
1026 @Override public Drawable getApplicationIcon(String packageName)
1027 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001028 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001029 }
1030
1031 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001032 public Drawable getActivityBanner(ComponentName activityName)
1033 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001034 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001035 }
1036
1037 @Override
1038 public Drawable getActivityBanner(Intent intent)
1039 throws NameNotFoundException {
1040 if (intent.getComponent() != null) {
1041 return getActivityBanner(intent.getComponent());
1042 }
1043
1044 ResolveInfo info = resolveActivity(
1045 intent, PackageManager.MATCH_DEFAULT_ONLY);
1046 if (info != null) {
1047 return info.activityInfo.loadBanner(this);
1048 }
1049
1050 throw new NameNotFoundException(intent.toUri(0));
1051 }
1052
1053 @Override
1054 public Drawable getApplicationBanner(ApplicationInfo info) {
1055 return info.loadBanner(this);
1056 }
1057
1058 @Override
1059 public Drawable getApplicationBanner(String packageName)
1060 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001061 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001062 }
1063
1064 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001065 public Drawable getActivityLogo(ComponentName activityName)
1066 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001067 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001068 }
1069
1070 @Override
1071 public Drawable getActivityLogo(Intent intent)
1072 throws NameNotFoundException {
1073 if (intent.getComponent() != null) {
1074 return getActivityLogo(intent.getComponent());
1075 }
1076
1077 ResolveInfo info = resolveActivity(
1078 intent, PackageManager.MATCH_DEFAULT_ONLY);
1079 if (info != null) {
1080 return info.activityInfo.loadLogo(this);
1081 }
1082
1083 throw new NameNotFoundException(intent.toUri(0));
1084 }
1085
1086 @Override
1087 public Drawable getApplicationLogo(ApplicationInfo info) {
1088 return info.loadLogo(this);
1089 }
1090
1091 @Override
1092 public Drawable getApplicationLogo(String packageName)
1093 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001094 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001095 }
1096
Svetoslavc7d62f02014-09-04 15:39:54 -07001097 @Override
1098 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1099 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1100 if (badgeResId == 0) {
1101 return icon;
1102 }
1103 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1104 return getBadgedDrawable(icon, badgeIcon, null, true);
1105 }
1106
1107 @Override
1108 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1109 Rect badgeLocation, int badgeDensity) {
1110 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1111 if (badgeDrawable == null) {
1112 return drawable;
1113 }
1114 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1115 }
1116
1117 @Override
1118 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Selim Cineke6ff9462016-01-15 15:07:06 -08001119 return getManagedProfileIconForDensity(user, density,
1120 com.android.internal.R.drawable.ic_corp_badge);
1121 }
1122
1123 @Override
1124 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
1125 return getManagedProfileIconForDensity(user, density,
1126 com.android.internal.R.drawable.ic_corp_badge_no_background);
1127 }
1128
1129 private Drawable getManagedProfileIconForDensity(UserHandle user, int density,
1130 int drawableId) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001131 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1132 if (userInfo != null && userInfo.isManagedProfile()) {
1133 if (density <= 0) {
1134 density = mContext.getResources().getDisplayMetrics().densityDpi;
1135 }
Selim Cineke6ff9462016-01-15 15:07:06 -08001136 return Resources.getSystem().getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001137 }
1138 return null;
1139 }
1140
1141 @Override
1142 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
1143 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1144 if (userInfo != null && userInfo.isManagedProfile()) {
1145 return Resources.getSystem().getString(
1146 com.android.internal.R.string.managed_profile_label_badge, label);
1147 }
1148 return label;
1149 }
1150
Alan Viveretteecd585a2015-04-13 10:32:51 -07001151 @Override
1152 public Resources getResourcesForActivity(ComponentName activityName)
1153 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001154 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001155 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001156 }
1157
Alan Viveretteecd585a2015-04-13 10:32:51 -07001158 @Override
1159 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1160 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001161 if (app.packageName.equals("system")) {
1162 return mContext.mMainThread.getSystemContext().getResources();
1163 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001164 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001165 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001166 sameUid ? app.sourceDir : app.publicSourceDir,
1167 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001168 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1169 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001170 if (r != null) {
1171 return r;
1172 }
1173 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1174 }
1175
Alan Viveretteecd585a2015-04-13 10:32:51 -07001176 @Override
1177 public Resources getResourcesForApplication(String appPackageName)
1178 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001179 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001180 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001181 }
1182
Amith Yamasani98edc952012-09-25 14:09:27 -07001183 /** @hide */
1184 @Override
1185 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1186 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001187 if (userId < 0) {
1188 throw new IllegalArgumentException(
1189 "Call does not support special user #" + userId);
1190 }
1191 if ("system".equals(appPackageName)) {
1192 return mContext.mMainThread.getSystemContext().getResources();
1193 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001194 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001195 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001196 if (ai != null) {
1197 return getResourcesForApplication(ai);
1198 }
1199 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001200 throw e.rethrowAsRuntimeException();
Amith Yamasani98edc952012-09-25 14:09:27 -07001201 }
1202 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1203 }
1204
Jeff Sharkeycd654482016-01-08 17:42:11 -07001205 volatile int mCachedSafeMode = -1;
1206
1207 @Override
1208 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001209 try {
1210 if (mCachedSafeMode < 0) {
1211 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1212 }
1213 return mCachedSafeMode != 0;
1214 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001215 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001216 }
1217 }
1218
Svetoslavf7c06eb2015-06-10 18:43:22 -07001219 @Override
1220 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1221 synchronized (mPermissionListeners) {
1222 if (mPermissionListeners.get(listener) != null) {
1223 return;
1224 }
1225 OnPermissionsChangeListenerDelegate delegate =
1226 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1227 try {
1228 mPM.addOnPermissionsChangeListener(delegate);
1229 mPermissionListeners.put(listener, delegate);
1230 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001231 throw e.rethrowAsRuntimeException();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001232 }
1233 }
1234 }
1235
1236 @Override
1237 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1238 synchronized (mPermissionListeners) {
1239 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1240 if (delegate != null) {
1241 try {
1242 mPM.removeOnPermissionsChangeListener(delegate);
1243 mPermissionListeners.remove(listener);
1244 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001245 throw e.rethrowAsRuntimeException();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001246 }
1247 }
1248 }
1249 }
1250
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001251 static void configurationChanged() {
1252 synchronized (sSync) {
1253 sIconCache.clear();
1254 sStringCache.clear();
1255 }
1256 }
1257
1258 ApplicationPackageManager(ContextImpl context,
1259 IPackageManager pm) {
1260 mContext = context;
1261 mPM = pm;
1262 }
1263
Alan Viveretteecd585a2015-04-13 10:32:51 -07001264 @Nullable
1265 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001266 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001267 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001268 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1269 + name + ": " + wr);
1270 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001271 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001272 if (state != null) {
1273 if (DEBUG_ICONS) {
1274 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1275 }
1276 // Note: It's okay here to not use the newDrawable(Resources) variant
1277 // of the API. The ConstantState comes from a drawable that was
1278 // originally created by passing the proper app Resources instance
1279 // which means the state should already contain the proper
1280 // resources specific information (like density.) See
1281 // BitmapDrawable.BitmapState for instance.
1282 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001283 }
1284 // our entry has been purged
1285 sIconCache.remove(name);
1286 }
1287 }
1288 return null;
1289 }
1290
Alan Viveretteecd585a2015-04-13 10:32:51 -07001291 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001292 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001293 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001294 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001295 }
1296 }
1297
Romain Guy39fe17c2011-11-30 10:34:07 -08001298 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001299 boolean immediateGc = false;
1300 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1301 immediateGc = true;
1302 }
1303 if (pkgList != null && (pkgList.length > 0)) {
1304 boolean needCleanup = false;
1305 for (String ssp : pkgList) {
1306 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001307 for (int i=sIconCache.size()-1; i>=0; i--) {
1308 ResourceName nm = sIconCache.keyAt(i);
1309 if (nm.packageName.equals(ssp)) {
1310 //Log.i(TAG, "Removing cached drawable for " + nm);
1311 sIconCache.removeAt(i);
1312 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001313 }
1314 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001315 for (int i=sStringCache.size()-1; i>=0; i--) {
1316 ResourceName nm = sStringCache.keyAt(i);
1317 if (nm.packageName.equals(ssp)) {
1318 //Log.i(TAG, "Removing cached string for " + nm);
1319 sStringCache.removeAt(i);
1320 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001321 }
1322 }
1323 }
1324 }
1325 if (needCleanup || hasPkgInfo) {
1326 if (immediateGc) {
1327 // Schedule an immediate gc.
1328 Runtime.getRuntime().gc();
1329 } else {
1330 ActivityThread.currentActivityThread().scheduleGcIdler();
1331 }
1332 }
1333 }
1334 }
1335
1336 private static final class ResourceName {
1337 final String packageName;
1338 final int iconId;
1339
1340 ResourceName(String _packageName, int _iconId) {
1341 packageName = _packageName;
1342 iconId = _iconId;
1343 }
1344
1345 ResourceName(ApplicationInfo aInfo, int _iconId) {
1346 this(aInfo.packageName, _iconId);
1347 }
1348
1349 ResourceName(ComponentInfo cInfo, int _iconId) {
1350 this(cInfo.applicationInfo.packageName, _iconId);
1351 }
1352
1353 ResourceName(ResolveInfo rInfo, int _iconId) {
1354 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1355 }
1356
1357 @Override
1358 public boolean equals(Object o) {
1359 if (this == o) return true;
1360 if (o == null || getClass() != o.getClass()) return false;
1361
1362 ResourceName that = (ResourceName) o;
1363
1364 if (iconId != that.iconId) return false;
1365 return !(packageName != null ?
1366 !packageName.equals(that.packageName) : that.packageName != null);
1367
1368 }
1369
1370 @Override
1371 public int hashCode() {
1372 int result;
1373 result = packageName.hashCode();
1374 result = 31 * result + iconId;
1375 return result;
1376 }
1377
1378 @Override
1379 public String toString() {
1380 return "{ResourceName " + packageName + " / " + iconId + "}";
1381 }
1382 }
1383
1384 private CharSequence getCachedString(ResourceName name) {
1385 synchronized (sSync) {
1386 WeakReference<CharSequence> wr = sStringCache.get(name);
1387 if (wr != null) { // we have the activity
1388 CharSequence cs = wr.get();
1389 if (cs != null) {
1390 return cs;
1391 }
1392 // our entry has been purged
1393 sStringCache.remove(name);
1394 }
1395 }
1396 return null;
1397 }
1398
1399 private void putCachedString(ResourceName name, CharSequence cs) {
1400 synchronized (sSync) {
1401 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1402 }
1403 }
1404
1405 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001406 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001407 ApplicationInfo appInfo) {
1408 ResourceName name = new ResourceName(packageName, resid);
1409 CharSequence text = getCachedString(name);
1410 if (text != null) {
1411 return text;
1412 }
1413 if (appInfo == null) {
1414 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001415 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001416 } catch (NameNotFoundException e) {
1417 return null;
1418 }
1419 }
1420 try {
1421 Resources r = getResourcesForApplication(appInfo);
1422 text = r.getText(resid);
1423 putCachedString(name, text);
1424 return text;
1425 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001426 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001427 + appInfo.packageName);
1428 } catch (RuntimeException e) {
1429 // If an exception was thrown, fall through to return
1430 // default icon.
1431 Log.w("PackageManager", "Failure retrieving text 0x"
1432 + Integer.toHexString(resid) + " in package "
1433 + packageName, e);
1434 }
1435 return null;
1436 }
1437
1438 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001439 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001440 ApplicationInfo appInfo) {
1441 if (appInfo == null) {
1442 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001443 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001444 } catch (NameNotFoundException e) {
1445 return null;
1446 }
1447 }
1448 try {
1449 Resources r = getResourcesForApplication(appInfo);
1450 return r.getXml(resid);
1451 } catch (RuntimeException e) {
1452 // If an exception was thrown, fall through to return
1453 // default icon.
1454 Log.w("PackageManager", "Failure retrieving xml 0x"
1455 + Integer.toHexString(resid) + " in package "
1456 + packageName, e);
1457 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001458 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001459 + appInfo.packageName);
1460 }
1461 return null;
1462 }
1463
1464 @Override
1465 public CharSequence getApplicationLabel(ApplicationInfo info) {
1466 return info.loadLabel(this);
1467 }
1468
1469 @Override
1470 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1471 String installerPackageName) {
Todd Kennedy5cbeb592016-02-24 22:45:00 +00001472 final VerificationParams verificationParams = new VerificationParams(null, null,
1473 null, VerificationParams.NO_UID);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001474 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Todd Kennedy5cbeb592016-02-24 22:45:00 +00001475 installerPackageName, verificationParams, null, mContext.getUserId());
1476 }
1477
1478 @Override
1479 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1480 int flags, String installerPackageName, Uri verificationURI,
1481 ContainerEncryptionParams encryptionParams) {
1482 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1483 null, VerificationParams.NO_UID);
1484 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1485 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
1486 }
1487
1488 @Override
1489 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1490 IPackageInstallObserver observer, int flags, String installerPackageName,
1491 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
1492 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1493 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001494 }
1495
Christopher Tatef1977b42014-03-24 16:25:51 -07001496 @Override
1497 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1498 int flags, String installerPackageName) {
Todd Kennedy5cbeb592016-02-24 22:45:00 +00001499 installPackageAsUser(packageURI, observer, flags, installerPackageName,
1500 mContext.getUserId());
1501 }
1502
1503 @Override
1504 public void installPackageAsUser(Uri packageURI, PackageInstallObserver observer, int flags,
1505 String installerPackageName, int userId) {
1506 final VerificationParams verificationParams = new VerificationParams(null, null,
1507 null, VerificationParams.NO_UID);
1508 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null,
1509 userId);
1510 }
1511
1512 @Override
1513 public void installPackageWithVerification(Uri packageURI,
1514 PackageInstallObserver observer, int flags, String installerPackageName,
1515 Uri verificationURI,
1516 ContainerEncryptionParams encryptionParams) {
1517 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1518 null, VerificationParams.NO_UID);
1519 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
1520 encryptionParams, mContext.getUserId());
1521 }
1522
1523 @Override
1524 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1525 PackageInstallObserver observer, int flags, String installerPackageName,
1526 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
1527 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
1528 encryptionParams, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001529 }
1530
1531 private void installCommon(Uri packageURI,
1532 PackageInstallObserver observer, int flags, String installerPackageName,
Todd Kennedy5cbeb592016-02-24 22:45:00 +00001533 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001534 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001535 if (!"file".equals(packageURI.getScheme())) {
1536 throw new UnsupportedOperationException("Only file:// URIs are supported");
1537 }
Todd Kennedy5cbeb592016-02-24 22:45:00 +00001538 if (encryptionParams != null) {
1539 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1540 }
Jeff Sharkey513a0742014-07-08 17:10:32 -07001541
1542 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001543 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001544 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
Todd Kennedy5cbeb592016-02-24 22:45:00 +00001545 verificationParams, null, userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001546 } catch (RemoteException e) {
1547 throw e.rethrowAsRuntimeException();
rich cannings706e8ba2012-08-20 13:20:14 -07001548 }
1549 }
1550
1551 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001552 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001553 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001554 }
1555
1556 @Override
1557 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001558 throws NameNotFoundException {
1559 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001560 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001561 if (res == INSTALL_FAILED_INVALID_URI) {
1562 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1563 }
1564 return res;
1565 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001566 throw e.rethrowAsRuntimeException();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001567 }
1568 }
1569
1570 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001571 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001572 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001573 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001574 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001575 throw e.rethrowAsRuntimeException();
Kenny Root5ab21572011-07-27 11:11:19 -07001576 }
1577 }
1578
1579 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001580 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1581 long millisecondsToDelay) {
1582 try {
1583 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1584 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001585 throw e.rethrowAsRuntimeException();
rich canningsd9ef3e52012-08-22 14:28:05 -07001586 }
1587 }
1588
1589 @Override
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001590 public void verifyIntentFilter(int id, int verificationCode, List<String> outFailedDomains) {
1591 try {
1592 mPM.verifyIntentFilter(id, verificationCode, outFailedDomains);
1593 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001594 throw e.rethrowAsRuntimeException();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001595 }
1596 }
1597
1598 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001599 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001600 try {
1601 return mPM.getIntentVerificationStatus(packageName, userId);
1602 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001603 throw e.rethrowAsRuntimeException();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001604 }
1605 }
1606
1607 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001608 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001609 try {
1610 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1611 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001612 throw e.rethrowAsRuntimeException();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001613 }
1614 }
1615
1616 @Override
1617 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1618 try {
1619 return mPM.getIntentFilterVerifications(packageName);
1620 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001621 throw e.rethrowAsRuntimeException();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001622 }
1623 }
1624
1625 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001626 public List<IntentFilter> getAllIntentFilters(String packageName) {
1627 try {
1628 return mPM.getAllIntentFilters(packageName);
1629 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001630 throw e.rethrowAsRuntimeException();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001631 }
1632 }
1633
1634 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001635 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001636 try {
1637 return mPM.getDefaultBrowserPackageName(userId);
1638 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001639 throw e.rethrowAsRuntimeException();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001640 }
1641 }
1642
1643 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001644 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001645 try {
1646 return mPM.setDefaultBrowserPackageName(packageName, userId);
1647 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001648 throw e.rethrowAsRuntimeException();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001649 }
1650 }
1651
1652 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001653 public void setInstallerPackageName(String targetPackage,
1654 String installerPackageName) {
1655 try {
1656 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1657 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001658 throw e.rethrowAsRuntimeException();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001659 }
1660 }
1661
1662 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001663 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001664 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001665 return mPM.getInstallerPackageName(packageName);
1666 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001667 throw e.rethrowAsRuntimeException();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001668 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001669 }
1670
1671 @Override
1672 public int getMoveStatus(int moveId) {
1673 try {
1674 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001675 } catch (RemoteException e) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001676 throw e.rethrowAsRuntimeException();
1677 }
1678 }
1679
1680 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001681 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1682 synchronized (mDelegates) {
1683 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1684 handler.getLooper());
1685 try {
1686 mPM.registerMoveCallback(delegate);
1687 } catch (RemoteException e) {
1688 throw e.rethrowAsRuntimeException();
1689 }
1690 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001691 }
1692 }
1693
1694 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001695 public void unregisterMoveCallback(MoveCallback callback) {
1696 synchronized (mDelegates) {
1697 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1698 final MoveCallbackDelegate delegate = i.next();
1699 if (delegate.mCallback == callback) {
1700 try {
1701 mPM.unregisterMoveCallback(delegate);
1702 } catch (RemoteException e) {
1703 throw e.rethrowAsRuntimeException();
1704 }
1705 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001706 }
1707 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001708 }
1709 }
1710
1711 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001712 public int movePackage(String packageName, VolumeInfo vol) {
1713 try {
1714 final String volumeUuid;
1715 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1716 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1717 } else if (vol.isPrimaryPhysical()) {
1718 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1719 } else {
1720 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1721 }
1722
1723 return mPM.movePackage(packageName, volumeUuid);
1724 } catch (RemoteException e) {
1725 throw e.rethrowAsRuntimeException();
1726 }
1727 }
1728
1729 @Override
1730 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001731 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001732 if (app.isInternal()) {
1733 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1734 } else if (app.isExternalAsec()) {
1735 return storage.getPrimaryPhysicalVolume();
1736 } else {
1737 return storage.findVolumeByUuid(app.volumeUuid);
1738 }
1739 }
1740
1741 @Override
1742 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1743 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1744 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001745 final List<VolumeInfo> vols = storage.getVolumes();
1746 final List<VolumeInfo> candidates = new ArrayList<>();
1747 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001748 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001749 candidates.add(vol);
1750 }
1751 }
1752 return candidates;
1753 }
1754
Makoto Onukif34db0a2016-02-17 11:17:15 -08001755 private boolean isPackageCandidateVolume(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001756 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1757 final boolean forceAllowOnExternal = Settings.Global.getInt(
1758 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001759 // Private internal is always an option
1760 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1761 return true;
1762 }
1763
1764 // System apps and apps demanding internal storage can't be moved
1765 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001766 if (app.isSystemApp()) {
1767 return false;
1768 }
1769 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001770 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1771 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001772 return false;
1773 }
1774
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001775 // Gotta be able to write there
1776 if (!vol.isMountedWritable()) {
1777 return false;
1778 }
1779
1780 // Moving into an ASEC on public primary is only option internal
1781 if (vol.isPrimaryPhysical()) {
1782 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001783 }
1784
Makoto Onukif34db0a2016-02-17 11:17:15 -08001785 // Some apps can't be moved. (e.g. device admins)
1786 try {
1787 if (mPM.isPackageDeviceAdminOnAnyUser(app.packageName)) {
1788 return false;
1789 }
1790 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001791 throw e.rethrowAsRuntimeException();
Makoto Onukif34db0a2016-02-17 11:17:15 -08001792 }
1793
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001794 // Otherwise we can move to any private volume
1795 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1796 }
1797
1798 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001799 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001800 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001801 final String volumeUuid;
1802 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1803 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1804 } else if (vol.isPrimaryPhysical()) {
1805 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1806 } else {
1807 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1808 }
1809
1810 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001811 } catch (RemoteException e) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001812 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001813 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001814 }
1815
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001816 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001817 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1818 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1819 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001820 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001821 }
1822
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001823 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001824 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1825 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1826 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1827 final List<VolumeInfo> vols = storage.getVolumes();
1828 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001829 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1830 storage.getPrimaryStorageUuid()) && currentVol != null) {
1831 // TODO: support moving primary physical to emulated volume
1832 candidates.add(currentVol);
1833 } else {
1834 for (VolumeInfo vol : vols) {
1835 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1836 candidates.add(vol);
1837 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001838 }
1839 }
1840 return candidates;
1841 }
1842
1843 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1844 // Private internal is always an option
1845 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1846 return true;
1847 }
1848
1849 // Gotta be able to write there
1850 if (!vol.isMountedWritable()) {
1851 return false;
1852 }
1853
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001854 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001855 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001856 }
1857
1858 @Override
1859 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001860 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001861 }
1862
1863 @Override
1864 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1865 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001866 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001867 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001868 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001869 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001870 }
1871 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001872
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001873 @Override
1874 public void clearApplicationUserData(String packageName,
1875 IPackageDataObserver observer) {
1876 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001877 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001878 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001879 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001880 }
1881 }
1882 @Override
1883 public void deleteApplicationCacheFiles(String packageName,
1884 IPackageDataObserver observer) {
1885 try {
1886 mPM.deleteApplicationCacheFiles(packageName, observer);
1887 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001888 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001889 }
1890 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001891
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001892 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001893 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1894 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001895 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001896 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001897 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001898 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001899 }
1900 }
1901
1902 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001903 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001904 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001905 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001906 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001907 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001908 }
1909 }
1910
1911 @Override
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001912 public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended,
1913 int userId) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001914 try {
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001915 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, userId);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001916 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001917 throw e.rethrowAsRuntimeException();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001918 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001919 }
1920
1921 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001922 public boolean isPackageSuspendedForUser(String packageName, int userId) {
1923 try {
1924 return mPM.isPackageSuspendedForUser(packageName, userId);
1925 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001926 throw e.rethrowAsRuntimeException();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001927 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001928 }
1929
1930 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001931 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001932 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001933 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001934 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001935 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001936 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001937 }
1938 }
1939 @Override
1940 public void addPackageToPreferred(String packageName) {
1941 try {
1942 mPM.addPackageToPreferred(packageName);
1943 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001944 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001945 }
1946 }
1947
1948 @Override
1949 public void removePackageFromPreferred(String packageName) {
1950 try {
1951 mPM.removePackageFromPreferred(packageName);
1952 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001953 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001954 }
1955 }
1956
1957 @Override
1958 public List<PackageInfo> getPreferredPackages(int flags) {
1959 try {
1960 return mPM.getPreferredPackages(flags);
1961 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001962 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001963 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001964 }
1965
1966 @Override
1967 public void addPreferredActivity(IntentFilter filter,
1968 int match, ComponentName[] set, ComponentName activity) {
1969 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001970 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001971 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001972 throw e.rethrowAsRuntimeException();
Amith Yamasania3f133a2012-08-09 17:11:28 -07001973 }
1974 }
1975
1976 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001977 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07001978 ComponentName[] set, ComponentName activity, int userId) {
1979 try {
1980 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001981 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001982 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001983 }
1984 }
1985
1986 @Override
1987 public void replacePreferredActivity(IntentFilter filter,
1988 int match, ComponentName[] set, ComponentName activity) {
1989 try {
Robin Lee0e27c872015-09-28 14:37:40 +01001990 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001991 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001992 throw e.rethrowAsRuntimeException();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001993 }
1994 }
1995
1996 @Override
1997 public void replacePreferredActivityAsUser(IntentFilter filter,
1998 int match, ComponentName[] set, ComponentName activity,
1999 int userId) {
2000 try {
2001 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002002 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002003 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002004 }
2005 }
2006
2007 @Override
2008 public void clearPackagePreferredActivities(String packageName) {
2009 try {
2010 mPM.clearPackagePreferredActivities(packageName);
2011 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002012 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002013 }
2014 }
2015
2016 @Override
2017 public int getPreferredActivities(List<IntentFilter> outFilters,
2018 List<ComponentName> outActivities, String packageName) {
2019 try {
2020 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2021 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002022 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002023 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002024 }
2025
2026 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002027 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2028 try {
2029 return mPM.getHomeActivities(outActivities);
2030 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002031 throw e.rethrowAsRuntimeException();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002032 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002033 }
2034
2035 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002036 public void setComponentEnabledSetting(ComponentName componentName,
2037 int newState, int flags) {
2038 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002039 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002040 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002041 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002042 }
2043 }
2044
2045 @Override
2046 public int getComponentEnabledSetting(ComponentName componentName) {
2047 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002048 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002049 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002050 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002051 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002052 }
2053
2054 @Override
2055 public void setApplicationEnabledSetting(String packageName,
2056 int newState, int flags) {
2057 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002058 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002059 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002060 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002061 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002062 }
2063 }
2064
2065 @Override
2066 public int getApplicationEnabledSetting(String packageName) {
2067 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002068 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002069 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002070 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002071 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002072 }
2073
Amith Yamasani655d0e22013-06-12 14:19:10 -07002074 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002075 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002076 UserHandle user) {
2077 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002078 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002079 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002080 } catch (RemoteException e) {
2081 throw e.rethrowAsRuntimeException();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002082 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002083 }
2084
2085 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002086 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002087 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002088 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002089 } catch (RemoteException e) {
2090 throw e.rethrowAsRuntimeException();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002091 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002092 }
2093
dcashmanc6f22492014-08-14 09:54:51 -07002094 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002095 @Override
2096 public KeySet getKeySetByAlias(String packageName, String alias) {
2097 Preconditions.checkNotNull(packageName);
2098 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002099 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002100 return mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002101 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002102 throw e.rethrowAsRuntimeException();
dcashman9d2f4412014-06-09 09:27:54 -07002103 }
dcashman9d2f4412014-06-09 09:27:54 -07002104 }
2105
dcashmanc6f22492014-08-14 09:54:51 -07002106 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002107 @Override
2108 public KeySet getSigningKeySet(String packageName) {
2109 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002110 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002111 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002112 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002113 throw e.rethrowAsRuntimeException();
dcashman9d2f4412014-06-09 09:27:54 -07002114 }
dcashman9d2f4412014-06-09 09:27:54 -07002115 }
2116
dcashmanc6f22492014-08-14 09:54:51 -07002117 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002118 @Override
2119 public boolean isSignedBy(String packageName, KeySet ks) {
2120 Preconditions.checkNotNull(packageName);
2121 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002122 try {
dcashmanc6f22492014-08-14 09:54:51 -07002123 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002124 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002125 throw e.rethrowAsRuntimeException();
dcashman9d2f4412014-06-09 09:27:54 -07002126 }
2127 }
2128
dcashmanc6f22492014-08-14 09:54:51 -07002129 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002130 @Override
2131 public boolean isSignedByExactly(String packageName, KeySet ks) {
2132 Preconditions.checkNotNull(packageName);
2133 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002134 try {
dcashmanc6f22492014-08-14 09:54:51 -07002135 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002136 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002137 throw e.rethrowAsRuntimeException();
dcashman9d2f4412014-06-09 09:27:54 -07002138 }
2139 }
2140
Kenny Root0aaa0d92011-09-12 16:42:55 -07002141 /**
2142 * @hide
2143 */
2144 @Override
2145 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2146 try {
2147 return mPM.getVerifierDeviceIdentity();
2148 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002149 throw e.rethrowAsRuntimeException();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002150 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002151 }
2152
Jeff Hao9f60c082014-10-28 18:51:07 -07002153 /**
2154 * @hide
2155 */
2156 @Override
2157 public boolean isUpgrade() {
2158 try {
2159 return mPM.isUpgrade();
2160 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002161 throw e.rethrowAsRuntimeException();
Jeff Hao9f60c082014-10-28 18:51:07 -07002162 }
2163 }
2164
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002165 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002166 public PackageInstaller getPackageInstaller() {
2167 synchronized (mLock) {
2168 if (mInstaller == null) {
2169 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002170 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002171 mContext.getPackageName(), mContext.getUserId());
2172 } catch (RemoteException e) {
2173 throw e.rethrowAsRuntimeException();
2174 }
2175 }
2176 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002177 }
2178 }
2179
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002180 @Override
2181 public boolean isPackageAvailable(String packageName) {
2182 try {
2183 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2184 } catch (RemoteException e) {
2185 throw e.rethrowAsRuntimeException();
2186 }
2187 }
2188
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002189 /**
2190 * @hide
2191 */
2192 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002193 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2194 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002195 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002196 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002197 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002198 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002199 throw e.rethrowAsRuntimeException();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002200 }
2201 }
2202
2203 /**
2204 * @hide
2205 */
2206 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002207 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002208 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002209 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002210 } catch (RemoteException e) {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002211 throw e.rethrowAsRuntimeException();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002212 }
2213 }
2214
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002215 /**
2216 * @hide
2217 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002218 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002219 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2220 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2221 return dr;
2222 }
2223 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2224 }
2225
2226 /**
2227 * @hide
2228 */
2229 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002230 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002231 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2232 if (bitmap == null) {
2233 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2234 }
2235 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002236 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002237 Drawable dr = null;
2238 if (itemInfo.packageName != null) {
2239 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2240 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002241 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002242 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002243 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002244 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002245 }
2246
2247 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2248 Rect badgeLocation, boolean tryBadgeInPlace) {
2249 final int badgedWidth = drawable.getIntrinsicWidth();
2250 final int badgedHeight = drawable.getIntrinsicHeight();
2251 final boolean canBadgeInPlace = tryBadgeInPlace
2252 && (drawable instanceof BitmapDrawable)
2253 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2254
2255 final Bitmap bitmap;
2256 if (canBadgeInPlace) {
2257 bitmap = ((BitmapDrawable) drawable).getBitmap();
2258 } else {
2259 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2260 }
2261 Canvas canvas = new Canvas(bitmap);
2262
2263 if (!canBadgeInPlace) {
2264 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2265 drawable.draw(canvas);
2266 }
2267
2268 if (badgeLocation != null) {
2269 if (badgeLocation.left < 0 || badgeLocation.top < 0
2270 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2271 throw new IllegalArgumentException("Badge location " + badgeLocation
2272 + " not in badged drawable bounds "
2273 + new Rect(0, 0, badgedWidth, badgedHeight));
2274 }
2275 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2276
2277 canvas.save();
2278 canvas.translate(badgeLocation.left, badgeLocation.top);
2279 badgeDrawable.draw(canvas);
2280 canvas.restore();
2281 } else {
2282 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2283 badgeDrawable.draw(canvas);
2284 }
2285
2286 if (!canBadgeInPlace) {
2287 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2288
2289 if (drawable instanceof BitmapDrawable) {
2290 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2291 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2292 }
2293
2294 return mergedDrawable;
2295 }
2296
2297 return drawable;
2298 }
2299
2300 private int getBadgeResIdForUser(int userHandle) {
2301 // Return the framework-provided badge.
2302 UserInfo userInfo = getUserIfProfile(userHandle);
2303 if (userInfo != null && userInfo.isManagedProfile()) {
2304 return com.android.internal.R.drawable.ic_corp_icon_badge;
2305 }
2306 return 0;
2307 }
2308
2309 private UserInfo getUserIfProfile(int userHandle) {
Robin Lee0e27c872015-09-28 14:37:40 +01002310 List<UserInfo> userProfiles = getUserManager().getProfiles(mContext.getUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07002311 for (UserInfo user : userProfiles) {
2312 if (user.id == userHandle) {
2313 return user;
2314 }
2315 }
2316 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002317 }
2318
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002319 /** {@hide} */
2320 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2321 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002322 private static final int MSG_CREATED = 1;
2323 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002324
2325 final MoveCallback mCallback;
2326 final Handler mHandler;
2327
2328 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2329 mCallback = callback;
2330 mHandler = new Handler(looper, this);
2331 }
2332
2333 @Override
2334 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002335 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002336 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002337 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002338 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002339 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002340 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002341 }
2342 case MSG_STATUS_CHANGED: {
2343 final SomeArgs args = (SomeArgs) msg.obj;
2344 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2345 args.recycle();
2346 return true;
2347 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002348 }
2349 return false;
2350 }
2351
2352 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002353 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002354 final SomeArgs args = SomeArgs.obtain();
2355 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002356 args.arg2 = extras;
2357 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2358 }
2359
2360 @Override
2361 public void onStatusChanged(int moveId, int status, long estMillis) {
2362 final SomeArgs args = SomeArgs.obtain();
2363 args.argi1 = moveId;
2364 args.argi2 = status;
2365 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002366 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002367 }
2368 }
2369
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002370 private final ContextImpl mContext;
2371 private final IPackageManager mPM;
2372
2373 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002374 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2375 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2376 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2377 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002378
2379 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2380 mPermissionListeners = new ArrayMap<>();
2381
2382 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2383 implements Handler.Callback{
2384 private static final int MSG_PERMISSIONS_CHANGED = 1;
2385
2386 private final OnPermissionsChangedListener mListener;
2387 private final Handler mHandler;
2388
2389
2390 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2391 Looper looper) {
2392 mListener = listener;
2393 mHandler = new Handler(looper, this);
2394 }
2395
2396 @Override
2397 public void onPermissionsChanged(int uid) {
2398 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2399 }
2400
2401 @Override
2402 public boolean handleMessage(Message msg) {
2403 switch (msg.what) {
2404 case MSG_PERMISSIONS_CHANGED: {
2405 final int uid = msg.arg1;
2406 mListener.onPermissionsChanged(uid);
2407 return true;
2408 }
2409 }
2410 return false;
2411 }
2412 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002413}