blob: 0c6c4ba33ee312b8a908bc46c36270814b66bff6 [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;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080024import android.content.ComponentName;
25import android.content.ContentResolver;
Yao Chen022b8ea2016-12-16 11:03:28 -080026import android.content.Context;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080027import 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;
Svetoslav Ganov096d3042017-01-30 16:34:13 -080033import android.content.pm.InstantAppInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080034import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070035import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080036import android.content.pm.IPackageDataObserver;
37import android.content.pm.IPackageDeleteObserver;
38import android.content.pm.IPackageInstallObserver;
39import android.content.pm.IPackageManager;
40import android.content.pm.IPackageMoveObserver;
41import android.content.pm.IPackageStatsObserver;
42import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080043import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070044import android.content.pm.KeySet;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080045import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070046import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010047import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080048import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070049import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080050import android.content.pm.PermissionGroupInfo;
51import android.content.pm.PermissionInfo;
52import android.content.pm.ProviderInfo;
53import android.content.pm.ResolveInfo;
54import android.content.pm.ServiceInfo;
Svet Ganov67882122016-12-11 16:36:34 -080055import android.content.pm.SharedLibraryInfo;
Kenny Root0aaa0d92011-09-12 16:42:55 -070056import android.content.pm.VerifierDeviceIdentity;
Svet Ganov67882122016-12-11 16:36:34 -080057import android.content.pm.VersionedPackage;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080058import android.content.res.Resources;
59import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070060import android.graphics.Bitmap;
61import android.graphics.Canvas;
62import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010063import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080064import android.graphics.drawable.Drawable;
Kenny Guy02c89902016-11-15 19:36:38 +000065import android.graphics.drawable.LayerDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080066import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070067import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070068import android.os.Handler;
69import android.os.Looper;
70import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080071import android.os.Process;
72import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070073import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080074import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010075import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070076import android.os.storage.StorageManager;
77import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070078import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070079import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080080import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070081import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070082
83import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070084
85import com.android.internal.annotations.GuardedBy;
Kenny Guy02c89902016-11-15 19:36:38 +000086import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070087import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070088import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010089import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080090import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070091
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080092import java.lang.ref.WeakReference;
93import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080094import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070095import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080096import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070097import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070098import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080099
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700100/** @hide */
101public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800102 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800103 private final static boolean DEBUG_ICONS = false;
104
Svet Ganov2acf0632015-11-24 19:10:59 -0800105 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
106
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700107 // Default flags to use with PackageManager when no flags are given.
108 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
109
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700110 private final Object mLock = new Object();
111
112 @GuardedBy("mLock")
113 private UserManager mUserManager;
114 @GuardedBy("mLock")
115 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100116
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700117 @GuardedBy("mDelegates")
118 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
119
Svet Ganovf1b7f202015-07-29 08:33:42 -0700120 @GuardedBy("mLock")
121 private String mPermissionsControllerPackageName;
122
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100123 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700124 synchronized (mLock) {
125 if (mUserManager == null) {
126 mUserManager = UserManager.get(mContext);
127 }
128 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100129 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100130 }
131
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800132 @Override
133 public PackageInfo getPackageInfo(String packageName, int flags)
134 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100135 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
136 }
137
138 @Override
Svet Ganov67882122016-12-11 16:36:34 -0800139 public PackageInfo getPackageInfo(VersionedPackage versionedPackage, int flags)
140 throws NameNotFoundException {
141 try {
142 PackageInfo pi = mPM.getPackageInfoVersioned(versionedPackage, flags,
143 mContext.getUserId());
144 if (pi != null) {
145 return pi;
146 }
147 } catch (RemoteException e) {
148 throw e.rethrowFromSystemServer();
149 }
150 throw new NameNotFoundException(versionedPackage.toString());
151 }
152
153 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100154 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
155 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800156 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100157 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800158 if (pi != null) {
159 return pi;
160 }
161 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700162 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800163 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800164 throw new NameNotFoundException(packageName);
165 }
166
167 @Override
168 public String[] currentToCanonicalPackageNames(String[] names) {
169 try {
170 return mPM.currentToCanonicalPackageNames(names);
171 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700172 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800173 }
174 }
175
176 @Override
177 public String[] canonicalToCurrentPackageNames(String[] names) {
178 try {
179 return mPM.canonicalToCurrentPackageNames(names);
180 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700181 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800182 }
183 }
184
185 @Override
186 public Intent getLaunchIntentForPackage(String packageName) {
187 // First see if the package has an INFO activity; the existence of
188 // such an activity is implied to be the desired front-door for the
189 // overall package (such as if it has multiple launcher entries).
190 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
191 intentToResolve.addCategory(Intent.CATEGORY_INFO);
192 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800193 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800194
195 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800196 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800197 // reuse the intent instance
198 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
199 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
200 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800201 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800202 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800203 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800204 return null;
205 }
206 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800207 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800208 intent.setClassName(ris.get(0).activityInfo.packageName,
209 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800210 return intent;
211 }
212
213 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700214 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
215 // Try to find a main leanback_launcher activity.
216 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
217 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
218 intentToResolve.setPackage(packageName);
219 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
220
221 if (ris == null || ris.size() <= 0) {
222 return null;
223 }
224 Intent intent = new Intent(intentToResolve);
225 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
226 intent.setClassName(ris.get(0).activityInfo.packageName,
227 ris.get(0).activityInfo.name);
228 return intent;
229 }
230
231 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700232 public int[] getPackageGids(String packageName) throws NameNotFoundException {
233 return getPackageGids(packageName, 0);
234 }
235
236 @Override
237 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800238 throws NameNotFoundException {
239 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700240 int[] gids = mPM.getPackageGids(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800241 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800242 return gids;
243 }
244 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700245 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800246 }
247
248 throw new NameNotFoundException(packageName);
249 }
250
251 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700252 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
253 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
254 }
255
256 @Override
257 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
258 return getPackageUidAsUser(packageName, 0, userId);
259 }
260
261 @Override
262 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800263 throws NameNotFoundException {
264 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700265 int uid = mPM.getPackageUid(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800266 if (uid >= 0) {
267 return uid;
268 }
269 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700270 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800271 }
272
273 throw new NameNotFoundException(packageName);
274 }
275
276 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800277 public PermissionInfo getPermissionInfo(String name, int flags)
278 throws NameNotFoundException {
279 try {
280 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
281 if (pi != null) {
282 return pi;
283 }
284 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700285 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800286 }
287
288 throw new NameNotFoundException(name);
289 }
290
291 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700292 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800293 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
294 throws NameNotFoundException {
295 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700296 ParceledListSlice<PermissionInfo> parceledList =
297 mPM.queryPermissionsByGroup(group, flags);
298 if (parceledList != null) {
299 List<PermissionInfo> pi = parceledList.getList();
300 if (pi != null) {
301 return pi;
302 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800303 }
304 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700305 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800306 }
307
308 throw new NameNotFoundException(group);
309 }
310
311 @Override
Paul Navin7b89a7b2017-01-26 23:56:08 +0000312 public boolean isPermissionReviewModeEnabled() {
313 return mContext.getResources().getBoolean(
314 com.android.internal.R.bool.config_permissionReviewRequired);
315 }
316
317 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800318 public PermissionGroupInfo getPermissionGroupInfo(String name,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700319 int flags) throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800320 try {
321 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
322 if (pgi != null) {
323 return pgi;
324 }
325 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700326 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800327 }
328
329 throw new NameNotFoundException(name);
330 }
331
332 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700333 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800334 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
335 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700336 ParceledListSlice<PermissionGroupInfo> parceledList =
337 mPM.getAllPermissionGroups(flags);
338 if (parceledList == null) {
339 return Collections.emptyList();
340 }
341 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800342 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700343 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800344 }
345 }
346
347 @Override
348 public ApplicationInfo getApplicationInfo(String packageName, int flags)
349 throws NameNotFoundException {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700350 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
351 }
352
353 @Override
354 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
355 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800356 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700357 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800358 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100359 // This is a temporary hack. Callers must use
360 // createPackageContext(packageName).getApplicationInfo() to
361 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800362 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800363 }
364 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700365 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800366 }
367
368 throw new NameNotFoundException(packageName);
369 }
370
Tao Baic9a02372016-01-12 15:02:24 -0800371 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100372 // If we're dealing with a multi-arch application that has both
373 // 32 and 64 bit shared libraries, we might need to choose the secondary
374 // depending on what the current runtime's instruction set is.
375 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
376 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500377
378 // Get the instruction set that the libraries of secondary Abi is supported.
379 // In presence of a native bridge this might be different than the one secondary Abi used.
380 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
381 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
382 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100383
384 // If the runtimeIsa is the same as the primary isa, then we do nothing.
385 // Everything will be set up correctly because info.nativeLibraryDir will
386 // correspond to the right ISA.
387 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800388 ApplicationInfo modified = new ApplicationInfo(info);
389 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
390 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100391 }
392 }
Tao Baic9a02372016-01-12 15:02:24 -0800393 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100394 }
395
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800396 @Override
397 public ActivityInfo getActivityInfo(ComponentName className, int flags)
398 throws NameNotFoundException {
399 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700400 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800401 if (ai != null) {
402 return ai;
403 }
404 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700405 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800406 }
407
408 throw new NameNotFoundException(className.toString());
409 }
410
411 @Override
412 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
413 throws NameNotFoundException {
414 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700415 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800416 if (ai != null) {
417 return ai;
418 }
419 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700420 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800421 }
422
423 throw new NameNotFoundException(className.toString());
424 }
425
426 @Override
427 public ServiceInfo getServiceInfo(ComponentName className, int flags)
428 throws NameNotFoundException {
429 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700430 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800431 if (si != null) {
432 return si;
433 }
434 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700435 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800436 }
437
438 throw new NameNotFoundException(className.toString());
439 }
440
441 @Override
442 public ProviderInfo getProviderInfo(ComponentName className, int flags)
443 throws NameNotFoundException {
444 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700445 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800446 if (pi != null) {
447 return pi;
448 }
449 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700450 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800451 }
452
453 throw new NameNotFoundException(className.toString());
454 }
455
456 @Override
457 public String[] getSystemSharedLibraryNames() {
458 try {
459 return mPM.getSystemSharedLibraryNames();
460 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700461 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800462 }
463 }
464
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800465 /** @hide */
466 @Override
Svet Ganov67882122016-12-11 16:36:34 -0800467 public @NonNull List<SharedLibraryInfo> getSharedLibraries(int flags) {
468 return getSharedLibrariesAsUser(flags, mContext.getUserId());
469 }
470
471 /** @hide */
472 @Override
473 @SuppressWarnings("unchecked")
474 public @NonNull List<SharedLibraryInfo> getSharedLibrariesAsUser(int flags, int userId) {
475 try {
476 ParceledListSlice<SharedLibraryInfo> sharedLibs = mPM.getSharedLibraries(
477 flags, userId);
478 if (sharedLibs == null) {
479 return Collections.emptyList();
480 }
481 return sharedLibs.getList();
482 } catch (RemoteException e) {
483 throw e.rethrowFromSystemServer();
484 }
485 }
486
487 /** @hide */
488 @Override
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700489 public @NonNull String getServicesSystemSharedLibraryPackageName() {
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800490 try {
491 return mPM.getServicesSystemSharedLibraryPackageName();
492 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700493 throw e.rethrowFromSystemServer();
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800494 }
495 }
496
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700497 /**
498 * @hide
499 */
500 public @NonNull String getSharedSystemSharedLibraryPackageName() {
501 try {
502 return mPM.getSharedSystemSharedLibraryPackageName();
503 } catch (RemoteException e) {
504 throw e.rethrowFromSystemServer();
505 }
506 }
507
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800508 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700509 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800510 public FeatureInfo[] getSystemAvailableFeatures() {
511 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700512 ParceledListSlice<FeatureInfo> parceledList =
513 mPM.getSystemAvailableFeatures();
514 if (parceledList == null) {
515 return new FeatureInfo[0];
516 }
517 final List<FeatureInfo> list = parceledList.getList();
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700518 final FeatureInfo[] res = new FeatureInfo[list.size()];
519 for (int i = 0; i < res.length; i++) {
520 res[i] = list.get(i);
521 }
522 return res;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800523 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700524 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800525 }
526 }
527
528 @Override
529 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700530 return hasSystemFeature(name, 0);
531 }
532
533 @Override
534 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800535 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700536 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800537 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700538 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800539 }
540 }
541
542 @Override
543 public int checkPermission(String permName, String pkgName) {
544 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800545 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800546 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700547 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800548 }
549 }
550
551 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700552 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
553 try {
554 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
555 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700556 throw e.rethrowFromSystemServer();
Svet Ganovad3b2972015-07-07 22:49:17 -0700557 }
558 }
559
Svet Ganovf1b7f202015-07-29 08:33:42 -0700560 /**
561 * @hide
562 */
563 @Override
564 public String getPermissionControllerPackageName() {
565 synchronized (mLock) {
566 if (mPermissionsControllerPackageName == null) {
567 try {
568 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
569 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700570 throw e.rethrowFromSystemServer();
Svet Ganovf1b7f202015-07-29 08:33:42 -0700571 }
572 }
573 return mPermissionsControllerPackageName;
574 }
575 }
576
Svet Ganovad3b2972015-07-07 22:49:17 -0700577 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800578 public boolean addPermission(PermissionInfo info) {
579 try {
580 return mPM.addPermission(info);
581 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700582 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800583 }
584 }
585
586 @Override
587 public boolean addPermissionAsync(PermissionInfo info) {
588 try {
589 return mPM.addPermissionAsync(info);
590 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700591 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800592 }
593 }
594
595 @Override
596 public void removePermission(String name) {
597 try {
598 mPM.removePermission(name);
599 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700600 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800601 }
602 }
603
604 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700605 public void grantRuntimePermission(String packageName, String permissionName,
606 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800607 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700608 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800609 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700610 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800611 }
612 }
613
614 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700615 public void revokeRuntimePermission(String packageName, String permissionName,
616 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800617 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700618 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
619 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700620 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700621 }
622 }
623
624 @Override
625 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
626 try {
627 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
628 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700629 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700630 }
631 }
632
633 @Override
634 public void updatePermissionFlags(String permissionName, String packageName,
635 int flagMask, int flagValues, UserHandle user) {
636 try {
637 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
638 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800639 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700640 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800641 }
642 }
643
644 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700645 public boolean shouldShowRequestPermissionRationale(String permission) {
646 try {
647 return mPM.shouldShowRequestPermissionRationale(permission,
648 mContext.getPackageName(), mContext.getUserId());
649 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700650 throw e.rethrowFromSystemServer();
Svetoslav20770dd2015-05-29 15:43:04 -0700651 }
652 }
653
654 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800655 public int checkSignatures(String pkg1, String pkg2) {
656 try {
657 return mPM.checkSignatures(pkg1, pkg2);
658 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700659 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800660 }
661 }
662
663 @Override
664 public int checkSignatures(int uid1, int uid2) {
665 try {
666 return mPM.checkUidSignatures(uid1, uid2);
667 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700668 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800669 }
670 }
671
672 @Override
673 public String[] getPackagesForUid(int uid) {
674 try {
675 return mPM.getPackagesForUid(uid);
676 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700677 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800678 }
679 }
680
681 @Override
682 public String getNameForUid(int uid) {
683 try {
684 return mPM.getNameForUid(uid);
685 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700686 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800687 }
688 }
689
690 @Override
691 public int getUidForSharedUser(String sharedUserName)
692 throws NameNotFoundException {
693 try {
694 int uid = mPM.getUidForSharedUser(sharedUserName);
695 if(uid != -1) {
696 return uid;
697 }
698 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700699 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800700 }
701 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
702 }
703
Kenny Roote6cd0c72011-05-19 12:48:14 -0700704 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800705 @Override
706 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700707 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700708 }
709
710 /** @hide */
711 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700712 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700713 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800714 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700715 ParceledListSlice<PackageInfo> parceledList =
716 mPM.getInstalledPackages(flags, userId);
717 if (parceledList == null) {
718 return Collections.emptyList();
719 }
720 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800721 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700722 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800723 }
724 }
725
Kenny Roote6cd0c72011-05-19 12:48:14 -0700726 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800727 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800728 public List<PackageInfo> getPackagesHoldingPermissions(
729 String[] permissions, int flags) {
730 final int userId = mContext.getUserId();
731 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700732 ParceledListSlice<PackageInfo> parceledList =
733 mPM.getPackagesHoldingPermissions(permissions, flags, userId);
734 if (parceledList == null) {
735 return Collections.emptyList();
736 }
737 return parceledList.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800738 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700739 throw e.rethrowFromSystemServer();
Dianne Hackborne7991752013-01-16 17:56:46 -0800740 }
741 }
742
743 @SuppressWarnings("unchecked")
744 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800745 public List<ApplicationInfo> getInstalledApplications(int flags) {
Bartosz Fabianowski11334242016-11-17 20:49:16 +0100746 return getInstalledApplicationsAsUser(flags, mContext.getUserId());
747 }
748
749 /** @hide */
750 @SuppressWarnings("unchecked")
751 @Override
752 public List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800753 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700754 ParceledListSlice<ApplicationInfo> parceledList =
755 mPM.getInstalledApplications(flags, userId);
756 if (parceledList == null) {
757 return Collections.emptyList();
758 }
759 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800760 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700761 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800762 }
763 }
764
Svet Ganov2acf0632015-11-24 19:10:59 -0800765 /** @hide */
766 @SuppressWarnings("unchecked")
767 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800768 public List<InstantAppInfo> getInstantApps() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800769 try {
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800770 ParceledListSlice<InstantAppInfo> slice =
771 mPM.getInstantApps(mContext.getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800772 if (slice != null) {
773 return slice.getList();
774 }
775 return Collections.emptyList();
776 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700777 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800778 }
779 }
780
781 /** @hide */
782 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800783 public Drawable getInstantAppIcon(String packageName) {
Svet Ganov2acf0632015-11-24 19:10:59 -0800784 try {
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800785 Bitmap bitmap = mPM.getInstantAppIcon(
Svet Ganov2acf0632015-11-24 19:10:59 -0800786 packageName, mContext.getUserId());
787 if (bitmap != null) {
788 return new BitmapDrawable(null, bitmap);
789 }
790 return null;
791 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700792 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800793 }
794 }
795
796 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800797 public boolean isInstantApp() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800798 try {
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800799 return mPM.isInstantApp(mContext.getPackageName(),
800 mContext.getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800801 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700802 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800803 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800804 }
805
806 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800807 public int getInstantAppCookieMaxSize() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800808 return Settings.Global.getInt(mContext.getContentResolver(),
809 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
810 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
811 }
812
813 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800814 public @NonNull byte[] getInstantAppCookie() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800815 try {
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800816 final byte[] cookie = mPM.getInstantAppCookie(
Svet Ganov2acf0632015-11-24 19:10:59 -0800817 mContext.getPackageName(), mContext.getUserId());
818 if (cookie != null) {
819 return cookie;
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700820 } else {
821 return EmptyArray.BYTE;
Svet Ganov2acf0632015-11-24 19:10:59 -0800822 }
823 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700824 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800825 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800826 }
827
828 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800829 public boolean setInstantAppCookie(@NonNull byte[] cookie) {
Svet Ganov2acf0632015-11-24 19:10:59 -0800830 try {
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800831 return mPM.setInstantAppCookie(mContext.getPackageName(),
832 cookie, mContext.getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800833 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700834 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800835 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800836 }
837
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800838 @Override
839 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700840 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700841 }
842
843 @Override
844 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800845 try {
846 return mPM.resolveIntent(
847 intent,
848 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700849 flags,
850 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800851 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700852 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800853 }
854 }
855
856 @Override
857 public List<ResolveInfo> queryIntentActivities(Intent intent,
858 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700859 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700860 }
861
862 /** @hide Same as above but for a specific user */
863 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700864 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700865 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700866 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800867 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700868 ParceledListSlice<ResolveInfo> parceledList =
869 mPM.queryIntentActivities(intent,
870 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
871 flags, userId);
872 if (parceledList == null) {
873 return Collections.emptyList();
874 }
875 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800876 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700877 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800878 }
879 }
880
881 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700882 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800883 public List<ResolveInfo> queryIntentActivityOptions(
884 ComponentName caller, Intent[] specifics, Intent intent,
885 int flags) {
886 final ContentResolver resolver = mContext.getContentResolver();
887
888 String[] specificTypes = null;
889 if (specifics != null) {
890 final int N = specifics.length;
891 for (int i=0; i<N; i++) {
892 Intent sp = specifics[i];
893 if (sp != null) {
894 String t = sp.resolveTypeIfNeeded(resolver);
895 if (t != null) {
896 if (specificTypes == null) {
897 specificTypes = new String[N];
898 }
899 specificTypes[i] = t;
900 }
901 }
902 }
903 }
904
905 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700906 ParceledListSlice<ResolveInfo> parceledList =
907 mPM.queryIntentActivityOptions(caller, specifics, specificTypes, intent,
908 intent.resolveTypeIfNeeded(resolver), flags, mContext.getUserId());
909 if (parceledList == null) {
910 return Collections.emptyList();
911 }
912 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800913 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700914 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800915 }
916 }
917
Amith Yamasanif203aee2012-08-29 18:41:53 -0700918 /**
919 * @hide
920 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800921 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700922 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700923 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800924 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700925 ParceledListSlice<ResolveInfo> parceledList =
926 mPM.queryIntentReceivers(intent,
927 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
928 flags, userId);
929 if (parceledList == null) {
930 return Collections.emptyList();
931 }
932 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800933 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700934 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800935 }
936 }
937
938 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700939 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700940 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700941 }
942
943 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800944 public ResolveInfo resolveService(Intent intent, int flags) {
945 try {
946 return mPM.resolveService(
947 intent,
948 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700949 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700950 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800951 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700952 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800953 }
954 }
955
956 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700957 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700958 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800959 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700960 ParceledListSlice<ResolveInfo> parceledList =
961 mPM.queryIntentServices(intent,
962 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
963 flags, userId);
964 if (parceledList == null) {
965 return Collections.emptyList();
966 }
967 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800968 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700969 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800970 }
971 }
972
973 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700974 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700975 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700976 }
977
978 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700979 @SuppressWarnings("unchecked")
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700980 public List<ResolveInfo> queryIntentContentProvidersAsUser(
981 Intent intent, int flags, int userId) {
982 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700983 ParceledListSlice<ResolveInfo> parceledList =
984 mPM.queryIntentContentProviders(intent,
985 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
986 flags, userId);
987 if (parceledList == null) {
988 return Collections.emptyList();
989 }
990 return parceledList.getList();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700991 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700992 throw e.rethrowFromSystemServer();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700993 }
994 }
995
996 @Override
997 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
998 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
999 }
1000
1001 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01001002 public ProviderInfo resolveContentProvider(String name, int flags) {
1003 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
1004 }
1005
1006 /** @hide **/
1007 @Override
1008 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001009 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01001010 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001011 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001012 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001013 }
1014 }
1015
1016 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001017 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001018 public List<ProviderInfo> queryContentProviders(String processName,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001019 int uid, int flags) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001020 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001021 ParceledListSlice<ProviderInfo> slice =
1022 mPM.queryContentProviders(processName, uid, flags);
1023 return slice != null ? slice.getList() : Collections.<ProviderInfo>emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001024 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001025 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001026 }
1027 }
1028
1029 @Override
1030 public InstrumentationInfo getInstrumentationInfo(
1031 ComponentName className, int flags)
1032 throws NameNotFoundException {
1033 try {
1034 InstrumentationInfo ii = mPM.getInstrumentationInfo(
1035 className, flags);
1036 if (ii != null) {
1037 return ii;
1038 }
1039 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001040 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001041 }
1042
1043 throw new NameNotFoundException(className.toString());
1044 }
1045
1046 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001047 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001048 public List<InstrumentationInfo> queryInstrumentation(
1049 String targetPackage, int flags) {
1050 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001051 ParceledListSlice<InstrumentationInfo> parceledList =
1052 mPM.queryInstrumentation(targetPackage, flags);
1053 if (parceledList == null) {
1054 return Collections.emptyList();
1055 }
1056 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001057 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001058 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001059 }
1060 }
1061
Alan Viveretteecd585a2015-04-13 10:32:51 -07001062 @Nullable
1063 @Override
1064 public Drawable getDrawable(String packageName, @DrawableRes int resId,
1065 @Nullable ApplicationInfo appInfo) {
1066 final ResourceName name = new ResourceName(packageName, resId);
1067 final Drawable cachedIcon = getCachedIcon(name);
1068 if (cachedIcon != null) {
1069 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001070 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001071
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001072 if (appInfo == null) {
1073 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001074 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001075 } catch (NameNotFoundException e) {
1076 return null;
1077 }
1078 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001079
1080 if (resId != 0) {
1081 try {
1082 final Resources r = getResourcesForApplication(appInfo);
1083 final Drawable dr = r.getDrawable(resId, null);
1084 if (dr != null) {
1085 putCachedIcon(name, dr);
1086 }
1087
1088 if (false) {
1089 RuntimeException e = new RuntimeException("here");
1090 e.fillInStackTrace();
1091 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
1092 + " from package " + packageName
1093 + ": app scale=" + r.getCompatibilityInfo().applicationScale
1094 + ", caller scale=" + mContext.getResources()
1095 .getCompatibilityInfo().applicationScale,
1096 e);
1097 }
Ricky Wai3ce46252015-04-15 16:12:22 +01001098 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001099 Log.v(TAG, "Getting drawable 0x"
1100 + Integer.toHexString(resId) + " from " + r
1101 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +01001102 }
1103 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -07001104 } catch (NameNotFoundException e) {
1105 Log.w("PackageManager", "Failure retrieving resources for "
1106 + appInfo.packageName);
1107 } catch (Resources.NotFoundException e) {
1108 Log.w("PackageManager", "Failure retrieving resources for "
1109 + appInfo.packageName + ": " + e.getMessage());
1110 } catch (Exception e) {
1111 // If an exception was thrown, fall through to return
1112 // default icon.
1113 Log.w("PackageManager", "Failure retrieving icon 0x"
1114 + Integer.toHexString(resId) + " in package "
1115 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001116 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001117 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001118
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001119 return null;
1120 }
1121
1122 @Override public Drawable getActivityIcon(ComponentName activityName)
1123 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001124 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001125 }
1126
1127 @Override public Drawable getActivityIcon(Intent intent)
1128 throws NameNotFoundException {
1129 if (intent.getComponent() != null) {
1130 return getActivityIcon(intent.getComponent());
1131 }
1132
1133 ResolveInfo info = resolveActivity(
1134 intent, PackageManager.MATCH_DEFAULT_ONLY);
1135 if (info != null) {
1136 return info.activityInfo.loadIcon(this);
1137 }
1138
Romain Guy39fe17c2011-11-30 10:34:07 -08001139 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001140 }
1141
1142 @Override public Drawable getDefaultActivityIcon() {
1143 return Resources.getSystem().getDrawable(
1144 com.android.internal.R.drawable.sym_def_app_icon);
1145 }
1146
1147 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1148 return info.loadIcon(this);
1149 }
1150
1151 @Override public Drawable getApplicationIcon(String packageName)
1152 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001153 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001154 }
1155
1156 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001157 public Drawable getActivityBanner(ComponentName activityName)
1158 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001159 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001160 }
1161
1162 @Override
1163 public Drawable getActivityBanner(Intent intent)
1164 throws NameNotFoundException {
1165 if (intent.getComponent() != null) {
1166 return getActivityBanner(intent.getComponent());
1167 }
1168
1169 ResolveInfo info = resolveActivity(
1170 intent, PackageManager.MATCH_DEFAULT_ONLY);
1171 if (info != null) {
1172 return info.activityInfo.loadBanner(this);
1173 }
1174
1175 throw new NameNotFoundException(intent.toUri(0));
1176 }
1177
1178 @Override
1179 public Drawable getApplicationBanner(ApplicationInfo info) {
1180 return info.loadBanner(this);
1181 }
1182
1183 @Override
1184 public Drawable getApplicationBanner(String packageName)
1185 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001186 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001187 }
1188
1189 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001190 public Drawable getActivityLogo(ComponentName activityName)
1191 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001192 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001193 }
1194
1195 @Override
1196 public Drawable getActivityLogo(Intent intent)
1197 throws NameNotFoundException {
1198 if (intent.getComponent() != null) {
1199 return getActivityLogo(intent.getComponent());
1200 }
1201
1202 ResolveInfo info = resolveActivity(
1203 intent, PackageManager.MATCH_DEFAULT_ONLY);
1204 if (info != null) {
1205 return info.activityInfo.loadLogo(this);
1206 }
1207
1208 throw new NameNotFoundException(intent.toUri(0));
1209 }
1210
1211 @Override
1212 public Drawable getApplicationLogo(ApplicationInfo info) {
1213 return info.loadLogo(this);
1214 }
1215
1216 @Override
1217 public Drawable getApplicationLogo(String packageName)
1218 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001219 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001220 }
1221
Svetoslavc7d62f02014-09-04 15:39:54 -07001222 @Override
1223 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
Kenny Guy02c89902016-11-15 19:36:38 +00001224 if (!isManagedProfile(user.getIdentifier())) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001225 return icon;
1226 }
Kenny Guy02c89902016-11-15 19:36:38 +00001227 Drawable badgeShadow = getDrawable("system",
1228 com.android.internal.R.drawable.ic_corp_icon_badge_shadow, null);
1229 Drawable badgeColor = getDrawable("system",
1230 com.android.internal.R.drawable.ic_corp_icon_badge_color, null);
1231 badgeColor.setTint(getUserBadgeColor(user));
1232 Drawable badgeForeground = getDrawable("system",
1233 com.android.internal.R.drawable.ic_corp_icon_badge_case, null);
1234
1235 Drawable badge = new LayerDrawable(
1236 new Drawable[] {badgeShadow, badgeColor, badgeForeground });
1237 return getBadgedDrawable(icon, badge, null, true);
Svetoslavc7d62f02014-09-04 15:39:54 -07001238 }
1239
1240 @Override
1241 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1242 Rect badgeLocation, int badgeDensity) {
1243 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1244 if (badgeDrawable == null) {
1245 return drawable;
1246 }
1247 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1248 }
1249
Kenny Guy02c89902016-11-15 19:36:38 +00001250 // Should have enough colors to cope with UserManagerService.getMaxManagedProfiles()
1251 @VisibleForTesting
1252 public static final int[] CORP_BADGE_COLORS = new int[] {
1253 com.android.internal.R.color.profile_badge_1,
1254 com.android.internal.R.color.profile_badge_2,
1255 com.android.internal.R.color.profile_badge_3
1256 };
1257
1258 @VisibleForTesting
1259 public static final int[] CORP_BADGE_LABEL_RES_ID = new int[] {
1260 com.android.internal.R.string.managed_profile_label_badge,
1261 com.android.internal.R.string.managed_profile_label_badge_2,
1262 com.android.internal.R.string.managed_profile_label_badge_3
1263 };
1264
1265 private int getUserBadgeColor(UserHandle user) {
1266 int badge = getUserManager().getManagedProfileBadge(user.getIdentifier());
1267 if (badge < 0) {
1268 badge = 0;
1269 }
1270 int resourceId = CORP_BADGE_COLORS[badge % CORP_BADGE_COLORS.length];
1271 return Resources.getSystem().getColor(resourceId, null);
1272 }
1273
Svetoslavc7d62f02014-09-04 15:39:54 -07001274 @Override
1275 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Kenny Guy02c89902016-11-15 19:36:38 +00001276 Drawable badgeColor = getManagedProfileIconForDensity(user,
1277 com.android.internal.R.drawable.ic_corp_badge_color, density);
1278 if (badgeColor == null) {
1279 return null;
1280 }
1281 badgeColor.setTint(getUserBadgeColor(user));
1282 Drawable badgeForeground = getDrawableForDensity(
1283 com.android.internal.R.drawable.ic_corp_badge_case, density);
1284 Drawable badge = new LayerDrawable(
1285 new Drawable[] {badgeColor, badgeForeground });
1286 return badge;
Selim Cineke6ff9462016-01-15 15:07:06 -08001287 }
1288
1289 @Override
1290 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
Kenny Guy02c89902016-11-15 19:36:38 +00001291 Drawable badge = getManagedProfileIconForDensity(user,
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001292 com.android.internal.R.drawable.ic_corp_badge_no_background, density);
Kenny Guy02c89902016-11-15 19:36:38 +00001293 if (badge != null) {
1294 badge.setTint(getUserBadgeColor(user));
1295 }
1296 return badge;
Selim Cineke6ff9462016-01-15 15:07:06 -08001297 }
1298
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001299 private Drawable getDrawableForDensity(int drawableId, int density) {
1300 if (density <= 0) {
1301 density = mContext.getResources().getDisplayMetrics().densityDpi;
1302 }
1303 return Resources.getSystem().getDrawableForDensity(drawableId, density);
1304 }
1305
1306 private Drawable getManagedProfileIconForDensity(UserHandle user, int drawableId, int density) {
Tony Mak8673b282016-03-21 21:10:59 +00001307 if (isManagedProfile(user.getIdentifier())) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001308 return getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001309 }
1310 return null;
1311 }
1312
1313 @Override
1314 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
Tony Mak8673b282016-03-21 21:10:59 +00001315 if (isManagedProfile(user.getIdentifier())) {
Kenny Guy02c89902016-11-15 19:36:38 +00001316 int badge = getUserManager().getManagedProfileBadge(user.getIdentifier());
1317 int resourceId = CORP_BADGE_LABEL_RES_ID[badge % CORP_BADGE_LABEL_RES_ID.length];
1318 return Resources.getSystem().getString(resourceId, label);
Svetoslavc7d62f02014-09-04 15:39:54 -07001319 }
1320 return label;
1321 }
1322
Alan Viveretteecd585a2015-04-13 10:32:51 -07001323 @Override
1324 public Resources getResourcesForActivity(ComponentName activityName)
1325 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001326 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001327 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001328 }
1329
Alan Viveretteecd585a2015-04-13 10:32:51 -07001330 @Override
1331 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1332 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001333 if (app.packageName.equals("system")) {
1334 return mContext.mMainThread.getSystemContext().getResources();
1335 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001336 final boolean sameUid = (app.uid == Process.myUid());
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001337 final Resources r = mContext.mMainThread.getTopLevelResources(
Adam Lesinskic82f28a2016-06-08 17:19:09 -07001338 sameUid ? app.sourceDir : app.publicSourceDir,
1339 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
1340 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1341 mContext.mPackageInfo);
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001342 if (r != null) {
1343 return r;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001344 }
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001345 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1346
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001347 }
1348
Alan Viveretteecd585a2015-04-13 10:32:51 -07001349 @Override
1350 public Resources getResourcesForApplication(String appPackageName)
1351 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001352 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001353 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001354 }
1355
Amith Yamasani98edc952012-09-25 14:09:27 -07001356 /** @hide */
1357 @Override
1358 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1359 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001360 if (userId < 0) {
1361 throw new IllegalArgumentException(
1362 "Call does not support special user #" + userId);
1363 }
1364 if ("system".equals(appPackageName)) {
1365 return mContext.mMainThread.getSystemContext().getResources();
1366 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001367 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001368 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001369 if (ai != null) {
1370 return getResourcesForApplication(ai);
1371 }
1372 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001373 throw e.rethrowFromSystemServer();
Amith Yamasani98edc952012-09-25 14:09:27 -07001374 }
1375 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1376 }
1377
Jeff Sharkeycd654482016-01-08 17:42:11 -07001378 volatile int mCachedSafeMode = -1;
1379
1380 @Override
1381 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001382 try {
1383 if (mCachedSafeMode < 0) {
1384 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1385 }
1386 return mCachedSafeMode != 0;
1387 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001388 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001389 }
1390 }
1391
Svetoslavf7c06eb2015-06-10 18:43:22 -07001392 @Override
1393 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1394 synchronized (mPermissionListeners) {
1395 if (mPermissionListeners.get(listener) != null) {
1396 return;
1397 }
1398 OnPermissionsChangeListenerDelegate delegate =
1399 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1400 try {
1401 mPM.addOnPermissionsChangeListener(delegate);
1402 mPermissionListeners.put(listener, delegate);
1403 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001404 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001405 }
1406 }
1407 }
1408
1409 @Override
1410 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1411 synchronized (mPermissionListeners) {
1412 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1413 if (delegate != null) {
1414 try {
1415 mPM.removeOnPermissionsChangeListener(delegate);
1416 mPermissionListeners.remove(listener);
1417 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001418 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001419 }
1420 }
1421 }
1422 }
1423
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001424 static void configurationChanged() {
1425 synchronized (sSync) {
1426 sIconCache.clear();
1427 sStringCache.clear();
1428 }
1429 }
1430
Yao Chen022b8ea2016-12-16 11:03:28 -08001431 protected ApplicationPackageManager(ContextImpl context,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001432 IPackageManager pm) {
1433 mContext = context;
1434 mPM = pm;
1435 }
1436
Alan Viveretteecd585a2015-04-13 10:32:51 -07001437 @Nullable
1438 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001439 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001440 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001441 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1442 + name + ": " + wr);
1443 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001444 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001445 if (state != null) {
1446 if (DEBUG_ICONS) {
1447 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1448 }
1449 // Note: It's okay here to not use the newDrawable(Resources) variant
1450 // of the API. The ConstantState comes from a drawable that was
1451 // originally created by passing the proper app Resources instance
1452 // which means the state should already contain the proper
1453 // resources specific information (like density.) See
1454 // BitmapDrawable.BitmapState for instance.
1455 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001456 }
1457 // our entry has been purged
1458 sIconCache.remove(name);
1459 }
1460 }
1461 return null;
1462 }
1463
Alan Viveretteecd585a2015-04-13 10:32:51 -07001464 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001465 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001466 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001467 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001468 }
1469 }
1470
Romain Guy39fe17c2011-11-30 10:34:07 -08001471 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001472 boolean immediateGc = false;
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001473 if (cmd == ApplicationThreadConstants.EXTERNAL_STORAGE_UNAVAILABLE) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001474 immediateGc = true;
1475 }
1476 if (pkgList != null && (pkgList.length > 0)) {
1477 boolean needCleanup = false;
1478 for (String ssp : pkgList) {
1479 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001480 for (int i=sIconCache.size()-1; i>=0; i--) {
1481 ResourceName nm = sIconCache.keyAt(i);
1482 if (nm.packageName.equals(ssp)) {
1483 //Log.i(TAG, "Removing cached drawable for " + nm);
1484 sIconCache.removeAt(i);
1485 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001486 }
1487 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001488 for (int i=sStringCache.size()-1; i>=0; i--) {
1489 ResourceName nm = sStringCache.keyAt(i);
1490 if (nm.packageName.equals(ssp)) {
1491 //Log.i(TAG, "Removing cached string for " + nm);
1492 sStringCache.removeAt(i);
1493 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001494 }
1495 }
1496 }
1497 }
1498 if (needCleanup || hasPkgInfo) {
1499 if (immediateGc) {
1500 // Schedule an immediate gc.
1501 Runtime.getRuntime().gc();
1502 } else {
1503 ActivityThread.currentActivityThread().scheduleGcIdler();
1504 }
1505 }
1506 }
1507 }
1508
1509 private static final class ResourceName {
1510 final String packageName;
1511 final int iconId;
1512
1513 ResourceName(String _packageName, int _iconId) {
1514 packageName = _packageName;
1515 iconId = _iconId;
1516 }
1517
1518 ResourceName(ApplicationInfo aInfo, int _iconId) {
1519 this(aInfo.packageName, _iconId);
1520 }
1521
1522 ResourceName(ComponentInfo cInfo, int _iconId) {
1523 this(cInfo.applicationInfo.packageName, _iconId);
1524 }
1525
1526 ResourceName(ResolveInfo rInfo, int _iconId) {
1527 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1528 }
1529
1530 @Override
1531 public boolean equals(Object o) {
1532 if (this == o) return true;
1533 if (o == null || getClass() != o.getClass()) return false;
1534
1535 ResourceName that = (ResourceName) o;
1536
1537 if (iconId != that.iconId) return false;
1538 return !(packageName != null ?
1539 !packageName.equals(that.packageName) : that.packageName != null);
1540
1541 }
1542
1543 @Override
1544 public int hashCode() {
1545 int result;
1546 result = packageName.hashCode();
1547 result = 31 * result + iconId;
1548 return result;
1549 }
1550
1551 @Override
1552 public String toString() {
1553 return "{ResourceName " + packageName + " / " + iconId + "}";
1554 }
1555 }
1556
1557 private CharSequence getCachedString(ResourceName name) {
1558 synchronized (sSync) {
1559 WeakReference<CharSequence> wr = sStringCache.get(name);
1560 if (wr != null) { // we have the activity
1561 CharSequence cs = wr.get();
1562 if (cs != null) {
1563 return cs;
1564 }
1565 // our entry has been purged
1566 sStringCache.remove(name);
1567 }
1568 }
1569 return null;
1570 }
1571
1572 private void putCachedString(ResourceName name, CharSequence cs) {
1573 synchronized (sSync) {
1574 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1575 }
1576 }
1577
1578 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001579 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001580 ApplicationInfo appInfo) {
1581 ResourceName name = new ResourceName(packageName, resid);
1582 CharSequence text = getCachedString(name);
1583 if (text != null) {
1584 return text;
1585 }
1586 if (appInfo == null) {
1587 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001588 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001589 } catch (NameNotFoundException e) {
1590 return null;
1591 }
1592 }
1593 try {
1594 Resources r = getResourcesForApplication(appInfo);
1595 text = r.getText(resid);
1596 putCachedString(name, text);
1597 return text;
1598 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001599 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001600 + appInfo.packageName);
1601 } catch (RuntimeException e) {
1602 // If an exception was thrown, fall through to return
1603 // default icon.
1604 Log.w("PackageManager", "Failure retrieving text 0x"
1605 + Integer.toHexString(resid) + " in package "
1606 + packageName, e);
1607 }
1608 return null;
1609 }
1610
1611 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001612 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001613 ApplicationInfo appInfo) {
1614 if (appInfo == null) {
1615 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001616 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001617 } catch (NameNotFoundException e) {
1618 return null;
1619 }
1620 }
1621 try {
1622 Resources r = getResourcesForApplication(appInfo);
1623 return r.getXml(resid);
1624 } catch (RuntimeException e) {
1625 // If an exception was thrown, fall through to return
1626 // default icon.
1627 Log.w("PackageManager", "Failure retrieving xml 0x"
1628 + Integer.toHexString(resid) + " in package "
1629 + packageName, e);
1630 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001631 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001632 + appInfo.packageName);
1633 }
1634 return null;
1635 }
1636
1637 @Override
1638 public CharSequence getApplicationLabel(ApplicationInfo info) {
1639 return info.loadLabel(this);
1640 }
1641
1642 @Override
1643 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1644 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001645 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Todd Kennedya6793232016-02-24 22:46:00 +00001646 installerPackageName, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001647 }
1648
Christopher Tatef1977b42014-03-24 16:25:51 -07001649 @Override
1650 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1651 int flags, String installerPackageName) {
Todd Kennedya6793232016-02-24 22:46:00 +00001652 installCommon(packageURI, observer, flags, installerPackageName, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001653 }
1654
1655 private void installCommon(Uri packageURI,
1656 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001657 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001658 if (!"file".equals(packageURI.getScheme())) {
1659 throw new UnsupportedOperationException("Only file:// URIs are supported");
1660 }
Jeff Sharkey513a0742014-07-08 17:10:32 -07001661
1662 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001663 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001664 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
Todd Kennedya6793232016-02-24 22:46:00 +00001665 userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001666 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001667 throw e.rethrowFromSystemServer();
rich cannings706e8ba2012-08-20 13:20:14 -07001668 }
1669 }
1670
1671 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001672 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001673 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001674 }
1675
1676 @Override
1677 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001678 throws NameNotFoundException {
1679 try {
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +01001680 int res = mPM.installExistingPackageAsUser(packageName, userId,
1681 PackageManager.INSTALL_REASON_UNKNOWN);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001682 if (res == INSTALL_FAILED_INVALID_URI) {
1683 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1684 }
1685 return res;
1686 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001687 throw e.rethrowFromSystemServer();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001688 }
1689 }
1690
1691 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001692 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001693 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001694 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001695 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001696 throw e.rethrowFromSystemServer();
Kenny Root5ab21572011-07-27 11:11:19 -07001697 }
1698 }
1699
1700 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001701 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1702 long millisecondsToDelay) {
1703 try {
1704 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1705 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001706 throw e.rethrowFromSystemServer();
rich canningsd9ef3e52012-08-22 14:28:05 -07001707 }
1708 }
1709
1710 @Override
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001711 public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001712 try {
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001713 mPM.verifyIntentFilter(id, verificationCode, failedDomains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001714 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001715 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001716 }
1717 }
1718
1719 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001720 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001721 try {
1722 return mPM.getIntentVerificationStatus(packageName, userId);
1723 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001724 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001725 }
1726 }
1727
1728 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001729 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001730 try {
1731 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1732 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001733 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001734 }
1735 }
1736
1737 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001738 @SuppressWarnings("unchecked")
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001739 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1740 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001741 ParceledListSlice<IntentFilterVerificationInfo> parceledList =
1742 mPM.getIntentFilterVerifications(packageName);
1743 if (parceledList == null) {
1744 return Collections.emptyList();
1745 }
1746 return parceledList.getList();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001747 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001748 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001749 }
1750 }
1751
1752 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001753 @SuppressWarnings("unchecked")
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001754 public List<IntentFilter> getAllIntentFilters(String packageName) {
1755 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001756 ParceledListSlice<IntentFilter> parceledList =
1757 mPM.getAllIntentFilters(packageName);
1758 if (parceledList == null) {
1759 return Collections.emptyList();
1760 }
1761 return parceledList.getList();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001762 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001763 throw e.rethrowFromSystemServer();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001764 }
1765 }
1766
1767 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001768 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001769 try {
1770 return mPM.getDefaultBrowserPackageName(userId);
1771 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001772 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001773 }
1774 }
1775
1776 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001777 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001778 try {
1779 return mPM.setDefaultBrowserPackageName(packageName, userId);
1780 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001781 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001782 }
1783 }
1784
1785 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001786 public void setInstallerPackageName(String targetPackage,
1787 String installerPackageName) {
1788 try {
1789 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1790 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001791 throw e.rethrowFromSystemServer();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001792 }
1793 }
1794
1795 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001796 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001797 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001798 return mPM.getInstallerPackageName(packageName);
1799 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001800 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001801 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001802 }
1803
1804 @Override
1805 public int getMoveStatus(int moveId) {
1806 try {
1807 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001808 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001809 throw e.rethrowFromSystemServer();
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001810 }
1811 }
1812
1813 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001814 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1815 synchronized (mDelegates) {
1816 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1817 handler.getLooper());
1818 try {
1819 mPM.registerMoveCallback(delegate);
1820 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001821 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001822 }
1823 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001824 }
1825 }
1826
1827 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001828 public void unregisterMoveCallback(MoveCallback callback) {
1829 synchronized (mDelegates) {
1830 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1831 final MoveCallbackDelegate delegate = i.next();
1832 if (delegate.mCallback == callback) {
1833 try {
1834 mPM.unregisterMoveCallback(delegate);
1835 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001836 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001837 }
1838 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001839 }
1840 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001841 }
1842 }
1843
1844 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001845 public int movePackage(String packageName, VolumeInfo vol) {
1846 try {
1847 final String volumeUuid;
1848 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1849 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1850 } else if (vol.isPrimaryPhysical()) {
1851 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1852 } else {
1853 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1854 }
1855
1856 return mPM.movePackage(packageName, volumeUuid);
1857 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001858 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001859 }
1860 }
1861
1862 @Override
1863 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001864 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Yao Chen022b8ea2016-12-16 11:03:28 -08001865 return getPackageCurrentVolume(app, storage);
1866 }
1867
1868 @VisibleForTesting
1869 protected @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app,
1870 StorageManager storage) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001871 if (app.isInternal()) {
1872 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1873 } else if (app.isExternalAsec()) {
1874 return storage.getPrimaryPhysicalVolume();
1875 } else {
1876 return storage.findVolumeByUuid(app.volumeUuid);
1877 }
1878 }
1879
1880 @Override
1881 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
Yao Chen022b8ea2016-12-16 11:03:28 -08001882 final StorageManager storageManager = mContext.getSystemService(StorageManager.class);
1883 return getPackageCandidateVolumes(app, storageManager, mPM);
1884 }
1885
1886 @VisibleForTesting
1887 protected @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app,
1888 StorageManager storageManager, IPackageManager pm) {
1889 final VolumeInfo currentVol = getPackageCurrentVolume(app, storageManager);
1890 final List<VolumeInfo> vols = storageManager.getVolumes();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001891 final List<VolumeInfo> candidates = new ArrayList<>();
1892 for (VolumeInfo vol : vols) {
Yao Chen022b8ea2016-12-16 11:03:28 -08001893 if (Objects.equals(vol, currentVol)
1894 || isPackageCandidateVolume(mContext, app, vol, pm)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001895 candidates.add(vol);
1896 }
1897 }
1898 return candidates;
1899 }
1900
Yao Chen022b8ea2016-12-16 11:03:28 -08001901 @VisibleForTesting
1902 protected boolean isForceAllowOnExternal(Context context) {
1903 return Settings.Global.getInt(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001904 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Yao Chen022b8ea2016-12-16 11:03:28 -08001905 }
1906
1907 @VisibleForTesting
1908 protected boolean isAllow3rdPartyOnInternal(Context context) {
1909 return context.getResources().getBoolean(
1910 com.android.internal.R.bool.config_allow3rdPartyAppOnInternal);
1911 }
1912
1913 private boolean isPackageCandidateVolume(
1914 ContextImpl context, ApplicationInfo app, VolumeInfo vol, IPackageManager pm) {
1915 final boolean forceAllowOnExternal = isForceAllowOnExternal(context);
1916
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001917 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
Yao Chen022b8ea2016-12-16 11:03:28 -08001918 return app.isSystemApp() || isAllow3rdPartyOnInternal(context);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001919 }
1920
1921 // System apps and apps demanding internal storage can't be moved
1922 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001923 if (app.isSystemApp()) {
1924 return false;
1925 }
1926 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001927 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1928 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001929 return false;
1930 }
1931
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001932 // Gotta be able to write there
1933 if (!vol.isMountedWritable()) {
1934 return false;
1935 }
1936
1937 // Moving into an ASEC on public primary is only option internal
1938 if (vol.isPrimaryPhysical()) {
1939 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001940 }
1941
Makoto Onukif34db0a2016-02-17 11:17:15 -08001942 // Some apps can't be moved. (e.g. device admins)
1943 try {
Yao Chen022b8ea2016-12-16 11:03:28 -08001944 if (pm.isPackageDeviceAdminOnAnyUser(app.packageName)) {
Makoto Onukif34db0a2016-02-17 11:17:15 -08001945 return false;
1946 }
1947 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001948 throw e.rethrowFromSystemServer();
Makoto Onukif34db0a2016-02-17 11:17:15 -08001949 }
1950
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001951 // Otherwise we can move to any private volume
1952 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1953 }
1954
1955 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001956 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001957 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001958 final String volumeUuid;
1959 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1960 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1961 } else if (vol.isPrimaryPhysical()) {
1962 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1963 } else {
1964 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1965 }
1966
1967 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001968 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001969 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001970 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001971 }
1972
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001973 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001974 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1975 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1976 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001977 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001978 }
1979
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001980 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001981 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1982 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1983 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1984 final List<VolumeInfo> vols = storage.getVolumes();
1985 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001986 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1987 storage.getPrimaryStorageUuid()) && currentVol != null) {
1988 // TODO: support moving primary physical to emulated volume
1989 candidates.add(currentVol);
1990 } else {
1991 for (VolumeInfo vol : vols) {
1992 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1993 candidates.add(vol);
1994 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001995 }
1996 }
1997 return candidates;
1998 }
1999
2000 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
2001 // Private internal is always an option
2002 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
2003 return true;
2004 }
2005
2006 // Gotta be able to write there
2007 if (!vol.isMountedWritable()) {
2008 return false;
2009 }
2010
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002011 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002012 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002013 }
2014
2015 @Override
2016 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01002017 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01002018 }
2019
2020 @Override
Svet Ganov67882122016-12-11 16:36:34 -08002021 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer,
2022 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002023 try {
Svet Ganov67882122016-12-11 16:36:34 -08002024 mPM.deletePackageAsUser(packageName, PackageManager.VERSION_CODE_HIGHEST,
2025 observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002026 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002027 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002028 }
2029 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07002030
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002031 @Override
2032 public void clearApplicationUserData(String packageName,
2033 IPackageDataObserver observer) {
2034 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002035 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002036 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002037 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002038 }
2039 }
2040 @Override
2041 public void deleteApplicationCacheFiles(String packageName,
2042 IPackageDataObserver observer) {
2043 try {
2044 mPM.deleteApplicationCacheFiles(packageName, observer);
2045 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002046 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002047 }
2048 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002049
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002050 @Override
Suprabh Shukla78c9eb82016-04-12 15:51:35 -07002051 public void deleteApplicationCacheFilesAsUser(String packageName, int userId,
2052 IPackageDataObserver observer) {
2053 try {
2054 mPM.deleteApplicationCacheFilesAsUser(packageName, userId, observer);
2055 } catch (RemoteException e) {
2056 throw e.rethrowFromSystemServer();
2057 }
2058 }
2059
2060 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002061 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
2062 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002063 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002064 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002065 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002066 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002067 }
2068 }
2069
2070 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002071 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002072 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002073 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002074 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002075 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002076 }
2077 }
2078
2079 @Override
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00002080 public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended,
2081 int userId) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002082 try {
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00002083 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, userId);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002084 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002085 throw e.rethrowFromSystemServer();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002086 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002087 }
2088
2089 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002090 public boolean isPackageSuspendedForUser(String packageName, int userId) {
2091 try {
2092 return mPM.isPackageSuspendedForUser(packageName, userId);
2093 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002094 throw e.rethrowFromSystemServer();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002095 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002096 }
2097
Jeff Sharkey9bc89af2017-01-11 11:25:50 -07002098 /** @hide */
2099 @Override
2100 public void setApplicationCategoryHint(String packageName, int categoryHint) {
2101 try {
2102 mPM.setApplicationCategoryHint(packageName, categoryHint,
2103 mContext.getOpPackageName());
2104 } catch (RemoteException e) {
2105 throw e.rethrowFromSystemServer();
2106 }
2107 }
2108
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002109 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002110 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07002111 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002112 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07002113 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002114 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002115 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002116 }
2117 }
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002118
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002119 @Override
2120 public void addPackageToPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002121 Log.w(TAG, "addPackageToPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002122 }
2123
2124 @Override
2125 public void removePackageFromPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002126 Log.w(TAG, "removePackageFromPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002127 }
2128
2129 @Override
2130 public List<PackageInfo> getPreferredPackages(int flags) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002131 Log.w(TAG, "getPreferredPackages() is a no-op");
2132 return Collections.emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002133 }
2134
2135 @Override
2136 public void addPreferredActivity(IntentFilter filter,
2137 int match, ComponentName[] set, ComponentName activity) {
2138 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002139 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07002140 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002141 throw e.rethrowFromSystemServer();
Amith Yamasania3f133a2012-08-09 17:11:28 -07002142 }
2143 }
2144
2145 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002146 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07002147 ComponentName[] set, ComponentName activity, int userId) {
2148 try {
2149 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002150 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002151 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002152 }
2153 }
2154
2155 @Override
2156 public void replacePreferredActivity(IntentFilter filter,
2157 int match, ComponentName[] set, ComponentName activity) {
2158 try {
Robin Lee0e27c872015-09-28 14:37:40 +01002159 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002160 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002161 throw e.rethrowFromSystemServer();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002162 }
2163 }
2164
2165 @Override
2166 public void replacePreferredActivityAsUser(IntentFilter filter,
2167 int match, ComponentName[] set, ComponentName activity,
2168 int userId) {
2169 try {
2170 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002171 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002172 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002173 }
2174 }
2175
2176 @Override
2177 public void clearPackagePreferredActivities(String packageName) {
2178 try {
2179 mPM.clearPackagePreferredActivities(packageName);
2180 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002181 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002182 }
2183 }
2184
2185 @Override
2186 public int getPreferredActivities(List<IntentFilter> outFilters,
2187 List<ComponentName> outActivities, String packageName) {
2188 try {
2189 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2190 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002191 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002192 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002193 }
2194
2195 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002196 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2197 try {
2198 return mPM.getHomeActivities(outActivities);
2199 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002200 throw e.rethrowFromSystemServer();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002201 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002202 }
2203
2204 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002205 public void setComponentEnabledSetting(ComponentName componentName,
2206 int newState, int flags) {
2207 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002208 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002209 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002210 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002211 }
2212 }
2213
2214 @Override
2215 public int getComponentEnabledSetting(ComponentName componentName) {
2216 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002217 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002218 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002219 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002220 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002221 }
2222
2223 @Override
2224 public void setApplicationEnabledSetting(String packageName,
2225 int newState, int flags) {
2226 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002227 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002228 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002229 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002230 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002231 }
2232 }
2233
2234 @Override
2235 public int getApplicationEnabledSetting(String packageName) {
2236 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002237 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002238 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002239 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002240 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002241 }
2242
Amith Yamasani655d0e22013-06-12 14:19:10 -07002243 @Override
Sudheer Shankabbb3ff22015-07-09 15:39:23 +01002244 public void flushPackageRestrictionsAsUser(int userId) {
2245 try {
2246 mPM.flushPackageRestrictionsAsUser(userId);
2247 } catch (RemoteException e) {
2248 throw e.rethrowFromSystemServer();
2249 }
2250 }
2251
2252 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002253 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002254 UserHandle user) {
2255 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002256 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002257 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002258 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002259 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002260 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002261 }
2262
2263 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002264 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002265 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002266 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002267 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002268 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002269 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002270 }
2271
dcashmanc6f22492014-08-14 09:54:51 -07002272 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002273 @Override
2274 public KeySet getKeySetByAlias(String packageName, String alias) {
2275 Preconditions.checkNotNull(packageName);
2276 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002277 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002278 return mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002279 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002280 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002281 }
dcashman9d2f4412014-06-09 09:27:54 -07002282 }
2283
dcashmanc6f22492014-08-14 09:54:51 -07002284 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002285 @Override
2286 public KeySet getSigningKeySet(String packageName) {
2287 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002288 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002289 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002290 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002291 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002292 }
dcashman9d2f4412014-06-09 09:27:54 -07002293 }
2294
dcashmanc6f22492014-08-14 09:54:51 -07002295 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002296 @Override
2297 public boolean isSignedBy(String packageName, KeySet ks) {
2298 Preconditions.checkNotNull(packageName);
2299 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002300 try {
dcashmanc6f22492014-08-14 09:54:51 -07002301 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002302 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002303 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002304 }
2305 }
2306
dcashmanc6f22492014-08-14 09:54:51 -07002307 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002308 @Override
2309 public boolean isSignedByExactly(String packageName, KeySet ks) {
2310 Preconditions.checkNotNull(packageName);
2311 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002312 try {
dcashmanc6f22492014-08-14 09:54:51 -07002313 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002314 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002315 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002316 }
2317 }
2318
Kenny Root0aaa0d92011-09-12 16:42:55 -07002319 /**
2320 * @hide
2321 */
2322 @Override
2323 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2324 try {
2325 return mPM.getVerifierDeviceIdentity();
2326 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002327 throw e.rethrowFromSystemServer();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002328 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002329 }
2330
Jeff Hao9f60c082014-10-28 18:51:07 -07002331 /**
2332 * @hide
2333 */
2334 @Override
2335 public boolean isUpgrade() {
2336 try {
2337 return mPM.isUpgrade();
2338 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002339 throw e.rethrowFromSystemServer();
Jeff Hao9f60c082014-10-28 18:51:07 -07002340 }
2341 }
2342
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002343 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002344 public PackageInstaller getPackageInstaller() {
2345 synchronized (mLock) {
2346 if (mInstaller == null) {
2347 try {
Svet Ganov67882122016-12-11 16:36:34 -08002348 mInstaller = new PackageInstaller(mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002349 mContext.getPackageName(), mContext.getUserId());
2350 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002351 throw e.rethrowFromSystemServer();
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002352 }
2353 }
2354 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002355 }
2356 }
2357
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002358 @Override
2359 public boolean isPackageAvailable(String packageName) {
2360 try {
2361 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2362 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002363 throw e.rethrowFromSystemServer();
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002364 }
2365 }
2366
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002367 /**
2368 * @hide
2369 */
2370 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002371 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2372 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002373 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002374 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002375 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002376 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002377 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002378 }
2379 }
2380
2381 /**
2382 * @hide
2383 */
2384 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002385 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002386 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002387 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002388 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002389 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002390 }
2391 }
2392
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002393 /**
2394 * @hide
2395 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002396 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002397 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2398 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2399 return dr;
2400 }
2401 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2402 }
2403
2404 /**
2405 * @hide
2406 */
2407 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002408 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002409 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2410 if (bitmap == null) {
2411 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2412 }
2413 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002414 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002415 Drawable dr = null;
2416 if (itemInfo.packageName != null) {
2417 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2418 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002419 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002420 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002421 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002422 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002423 }
2424
2425 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2426 Rect badgeLocation, boolean tryBadgeInPlace) {
2427 final int badgedWidth = drawable.getIntrinsicWidth();
2428 final int badgedHeight = drawable.getIntrinsicHeight();
2429 final boolean canBadgeInPlace = tryBadgeInPlace
2430 && (drawable instanceof BitmapDrawable)
2431 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2432
2433 final Bitmap bitmap;
2434 if (canBadgeInPlace) {
2435 bitmap = ((BitmapDrawable) drawable).getBitmap();
2436 } else {
2437 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2438 }
2439 Canvas canvas = new Canvas(bitmap);
2440
2441 if (!canBadgeInPlace) {
2442 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2443 drawable.draw(canvas);
2444 }
2445
2446 if (badgeLocation != null) {
2447 if (badgeLocation.left < 0 || badgeLocation.top < 0
2448 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2449 throw new IllegalArgumentException("Badge location " + badgeLocation
2450 + " not in badged drawable bounds "
2451 + new Rect(0, 0, badgedWidth, badgedHeight));
2452 }
2453 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2454
2455 canvas.save();
2456 canvas.translate(badgeLocation.left, badgeLocation.top);
2457 badgeDrawable.draw(canvas);
2458 canvas.restore();
2459 } else {
2460 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2461 badgeDrawable.draw(canvas);
2462 }
2463
2464 if (!canBadgeInPlace) {
2465 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2466
2467 if (drawable instanceof BitmapDrawable) {
2468 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2469 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2470 }
2471
2472 return mergedDrawable;
2473 }
2474
2475 return drawable;
2476 }
2477
Tony Mak8673b282016-03-21 21:10:59 +00002478 private boolean isManagedProfile(int userId) {
2479 return getUserManager().isManagedProfile(userId);
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002480 }
2481
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +01002482 /**
2483 * @hide
2484 */
2485 @Override
2486 public int getInstallReason(String packageName, UserHandle user) {
2487 try {
2488 return mPM.getInstallReason(packageName, user.getIdentifier());
2489 } catch (RemoteException e) {
2490 throw e.rethrowFromSystemServer();
2491 }
2492 }
2493
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002494 /** {@hide} */
2495 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2496 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002497 private static final int MSG_CREATED = 1;
2498 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002499
2500 final MoveCallback mCallback;
2501 final Handler mHandler;
2502
2503 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2504 mCallback = callback;
2505 mHandler = new Handler(looper, this);
2506 }
2507
2508 @Override
2509 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002510 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002511 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002512 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002513 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002514 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002515 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002516 }
2517 case MSG_STATUS_CHANGED: {
2518 final SomeArgs args = (SomeArgs) msg.obj;
2519 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2520 args.recycle();
2521 return true;
2522 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002523 }
2524 return false;
2525 }
2526
2527 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002528 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002529 final SomeArgs args = SomeArgs.obtain();
2530 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002531 args.arg2 = extras;
2532 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2533 }
2534
2535 @Override
2536 public void onStatusChanged(int moveId, int status, long estMillis) {
2537 final SomeArgs args = SomeArgs.obtain();
2538 args.argi1 = moveId;
2539 args.argi2 = status;
2540 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002541 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002542 }
2543 }
2544
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002545 private final ContextImpl mContext;
2546 private final IPackageManager mPM;
2547
2548 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002549 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2550 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2551 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2552 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002553
2554 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2555 mPermissionListeners = new ArrayMap<>();
2556
2557 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2558 implements Handler.Callback{
2559 private static final int MSG_PERMISSIONS_CHANGED = 1;
2560
2561 private final OnPermissionsChangedListener mListener;
2562 private final Handler mHandler;
2563
2564
2565 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2566 Looper looper) {
2567 mListener = listener;
2568 mHandler = new Handler(looper, this);
2569 }
2570
2571 @Override
2572 public void onPermissionsChanged(int uid) {
2573 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2574 }
2575
2576 @Override
2577 public boolean handleMessage(Message msg) {
2578 switch (msg.what) {
2579 case MSG_PERMISSIONS_CHANGED: {
2580 final int uid = msg.arg1;
2581 mListener.onPermissionsChanged(uid);
2582 return true;
2583 }
2584 }
2585 return false;
2586 }
2587 }
Suprabh Shuklaaef25132017-01-23 18:09:03 -08002588
2589 @Override
2590 public boolean canRequestPackageInstalls() {
2591 try {
2592 return mPM.canRequestPackageInstalls(mContext.getPackageName(), mContext.getUserId());
2593 } catch (RemoteException e) {
2594 throw e.rethrowAsRuntimeException();
2595 }
2596 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002597}