blob: 344610a5375fb6620663442f5b316170f669e221 [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;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080023import android.annotation.UserIdInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070024import android.annotation.XmlRes;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080025import android.content.ComponentName;
26import android.content.ContentResolver;
Yao Chen022b8ea2016-12-16 11:03:28 -080027import android.content.Context;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080028import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.IntentSender;
31import android.content.pm.ActivityInfo;
32import android.content.pm.ApplicationInfo;
Todd Kennedy9106c642017-02-08 14:16:53 -080033import android.content.pm.ChangedPackages;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080034import android.content.pm.ComponentInfo;
35import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070036import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080037import android.content.pm.IPackageDataObserver;
38import android.content.pm.IPackageDeleteObserver;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080039import android.content.pm.IPackageManager;
40import android.content.pm.IPackageMoveObserver;
41import android.content.pm.IPackageStatsObserver;
Todd Kennedy9da8b8a72017-07-25 15:38:39 -070042import android.content.pm.InstantAppInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080043import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080044import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070045import android.content.pm.KeySet;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080046import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070047import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010048import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080049import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070050import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080051import android.content.pm.PermissionGroupInfo;
52import android.content.pm.PermissionInfo;
53import android.content.pm.ProviderInfo;
54import android.content.pm.ResolveInfo;
55import android.content.pm.ServiceInfo;
Svet Ganov67882122016-12-11 16:36:34 -080056import android.content.pm.SharedLibraryInfo;
Kenny Root0aaa0d92011-09-12 16:42:55 -070057import android.content.pm.VerifierDeviceIdentity;
Svet Ganov67882122016-12-11 16:36:34 -080058import android.content.pm.VersionedPackage;
Calin Juravle45f8b292017-11-07 18:49:43 -080059import android.content.pm.dex.ArtManager;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080060import android.content.res.Resources;
61import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070062import android.graphics.Bitmap;
63import android.graphics.Canvas;
64import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010065import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080066import android.graphics.drawable.Drawable;
Kenny Guy02c89902016-11-15 19:36:38 +000067import android.graphics.drawable.LayerDrawable;
Jeff Sharkeye6306c42017-03-07 21:03:18 -070068import android.os.Build;
Jeff Sharkey50a05452015-04-29 11:24:52 -070069import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070070import android.os.Handler;
71import android.os.Looper;
72import android.os.Message;
Suprabh Shukla021b57a2018-03-08 18:21:50 -080073import android.os.PersistableBundle;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080074import android.os.Process;
75import android.os.RemoteException;
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -060076import android.os.StrictMode;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070077import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080078import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010079import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070080import android.os.storage.StorageManager;
81import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070082import android.provider.Settings;
Calin Juravle3d2af7f2017-04-19 19:56:21 -070083import android.system.ErrnoException;
84import android.system.Os;
85import android.system.OsConstants;
86import android.system.StructStat;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070087import android.util.ArrayMap;
Sunny Goyalbab30752017-04-12 15:36:42 -070088import android.util.IconDrawableFactory;
89import android.util.LauncherIcons;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080090import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070091import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070092
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070093import com.android.internal.annotations.GuardedBy;
Kenny Guy02c89902016-11-15 19:36:38 +000094import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070095import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070096import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010097import com.android.internal.util.UserIcons;
Todd Kennedy9da8b8a72017-07-25 15:38:39 -070098
99import dalvik.system.VMRuntime;
100
Svet Ganov2acf0632015-11-24 19:10:59 -0800101import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700102
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800103import java.lang.ref.WeakReference;
104import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -0800105import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700106import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800107import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -0700108import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700109import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800110
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700111/** @hide */
112public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800113 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800114 private final static boolean DEBUG_ICONS = false;
115
Svet Ganov2acf0632015-11-24 19:10:59 -0800116 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
117
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700118 // Default flags to use with PackageManager when no flags are given.
119 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
120
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700121 private final Object mLock = new Object();
122
123 @GuardedBy("mLock")
124 private UserManager mUserManager;
125 @GuardedBy("mLock")
126 private PackageInstaller mInstaller;
Calin Juravle45f8b292017-11-07 18:49:43 -0800127 @GuardedBy("mLock")
128 private ArtManager mArtManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100129
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700130 @GuardedBy("mDelegates")
131 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
132
Svet Ganovf1b7f202015-07-29 08:33:42 -0700133 @GuardedBy("mLock")
134 private String mPermissionsControllerPackageName;
135
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100136 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700137 synchronized (mLock) {
138 if (mUserManager == null) {
139 mUserManager = UserManager.get(mContext);
140 }
141 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100142 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100143 }
144
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800145 @Override
Jeff Sharkeyad357d12018-02-02 13:25:31 -0700146 public int getUserId() {
147 return mContext.getUserId();
148 }
149
150 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800151 public PackageInfo getPackageInfo(String packageName, int flags)
152 throws NameNotFoundException {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600153 return getPackageInfoAsUser(packageName, flags, getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100154 }
155
156 @Override
Svet Ganov67882122016-12-11 16:36:34 -0800157 public PackageInfo getPackageInfo(VersionedPackage versionedPackage, int flags)
158 throws NameNotFoundException {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600159 final int userId = getUserId();
Svet Ganov67882122016-12-11 16:36:34 -0800160 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600161 PackageInfo pi = mPM.getPackageInfoVersioned(versionedPackage,
162 updateFlagsForPackage(flags, userId), userId);
Svet Ganov67882122016-12-11 16:36:34 -0800163 if (pi != null) {
164 return pi;
165 }
166 } catch (RemoteException e) {
167 throw e.rethrowFromSystemServer();
168 }
169 throw new NameNotFoundException(versionedPackage.toString());
170 }
171
172 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100173 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
174 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800175 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600176 PackageInfo pi = mPM.getPackageInfo(packageName,
177 updateFlagsForPackage(flags, userId), userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800178 if (pi != null) {
179 return pi;
180 }
181 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700182 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800183 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800184 throw new NameNotFoundException(packageName);
185 }
186
187 @Override
188 public String[] currentToCanonicalPackageNames(String[] names) {
189 try {
190 return mPM.currentToCanonicalPackageNames(names);
191 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700192 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800193 }
194 }
195
196 @Override
197 public String[] canonicalToCurrentPackageNames(String[] names) {
198 try {
199 return mPM.canonicalToCurrentPackageNames(names);
200 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700201 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800202 }
203 }
204
205 @Override
206 public Intent getLaunchIntentForPackage(String packageName) {
207 // First see if the package has an INFO activity; the existence of
208 // such an activity is implied to be the desired front-door for the
209 // overall package (such as if it has multiple launcher entries).
210 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
211 intentToResolve.addCategory(Intent.CATEGORY_INFO);
212 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800213 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800214
215 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800216 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800217 // reuse the intent instance
218 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
219 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
220 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800221 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800222 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800223 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800224 return null;
225 }
226 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800227 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800228 intent.setClassName(ris.get(0).activityInfo.packageName,
229 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800230 return intent;
231 }
232
233 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700234 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
Roberto Perez7ee1a7a2017-12-04 17:14:33 -0800235 return getLaunchIntentForPackageAndCategory(packageName, Intent.CATEGORY_LEANBACK_LAUNCHER);
236 }
237
238 @Override
239 public Intent getCarLaunchIntentForPackage(String packageName) {
240 return getLaunchIntentForPackageAndCategory(packageName, Intent.CATEGORY_CAR_LAUNCHER);
241 }
242
243 private Intent getLaunchIntentForPackageAndCategory(String packageName, String category) {
244 // Try to find a main launcher activity for the given categories.
Jose Lima970417c2014-04-10 10:42:19 -0700245 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
Roberto Perez7ee1a7a2017-12-04 17:14:33 -0800246 intentToResolve.addCategory(category);
Jose Lima970417c2014-04-10 10:42:19 -0700247 intentToResolve.setPackage(packageName);
248 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
249
250 if (ris == null || ris.size() <= 0) {
251 return null;
252 }
253 Intent intent = new Intent(intentToResolve);
254 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
255 intent.setClassName(ris.get(0).activityInfo.packageName,
256 ris.get(0).activityInfo.name);
257 return intent;
258 }
259
260 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700261 public int[] getPackageGids(String packageName) throws NameNotFoundException {
262 return getPackageGids(packageName, 0);
263 }
264
265 @Override
266 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800267 throws NameNotFoundException {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600268 final int userId = getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800269 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600270 int[] gids = mPM.getPackageGids(packageName,
271 updateFlagsForPackage(flags, userId), userId);
Svetoslavc6d1c342015-02-26 14:44:43 -0800272 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800273 return gids;
274 }
275 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700276 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800277 }
278
279 throw new NameNotFoundException(packageName);
280 }
281
282 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700283 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600284 return getPackageUidAsUser(packageName, flags, getUserId());
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700285 }
286
287 @Override
288 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
289 return getPackageUidAsUser(packageName, 0, userId);
290 }
291
292 @Override
293 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800294 throws NameNotFoundException {
295 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600296 int uid = mPM.getPackageUid(packageName,
297 updateFlagsForPackage(flags, userId), userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800298 if (uid >= 0) {
299 return uid;
300 }
301 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700302 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800303 }
304
305 throw new NameNotFoundException(packageName);
306 }
307
308 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800309 public PermissionInfo getPermissionInfo(String name, int flags)
310 throws NameNotFoundException {
311 try {
Svetoslav Ganovadb8c522017-07-28 05:46:53 +0000312 PermissionInfo pi = mPM.getPermissionInfo(name,
313 mContext.getOpPackageName(), flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800314 if (pi != null) {
315 return pi;
316 }
317 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700318 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800319 }
320
321 throw new NameNotFoundException(name);
322 }
323
324 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700325 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800326 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
327 throws NameNotFoundException {
328 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700329 ParceledListSlice<PermissionInfo> parceledList =
330 mPM.queryPermissionsByGroup(group, flags);
331 if (parceledList != null) {
332 List<PermissionInfo> pi = parceledList.getList();
333 if (pi != null) {
334 return pi;
335 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800336 }
337 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700338 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800339 }
340
341 throw new NameNotFoundException(group);
342 }
343
344 @Override
Paul Navin7b89a7b2017-01-26 23:56:08 +0000345 public boolean isPermissionReviewModeEnabled() {
346 return mContext.getResources().getBoolean(
347 com.android.internal.R.bool.config_permissionReviewRequired);
348 }
349
350 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800351 public PermissionGroupInfo getPermissionGroupInfo(String name,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700352 int flags) throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800353 try {
354 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
355 if (pgi != null) {
356 return pgi;
357 }
358 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700359 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800360 }
361
362 throw new NameNotFoundException(name);
363 }
364
365 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700366 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800367 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
368 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700369 ParceledListSlice<PermissionGroupInfo> parceledList =
370 mPM.getAllPermissionGroups(flags);
371 if (parceledList == null) {
372 return Collections.emptyList();
373 }
374 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800375 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700376 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800377 }
378 }
379
380 @Override
381 public ApplicationInfo getApplicationInfo(String packageName, int flags)
382 throws NameNotFoundException {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600383 return getApplicationInfoAsUser(packageName, flags, getUserId());
Jeff Sharkeycd654482016-01-08 17:42:11 -0700384 }
385
386 @Override
387 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
388 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800389 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600390 ApplicationInfo ai = mPM.getApplicationInfo(packageName,
391 updateFlagsForApplication(flags, userId), userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800392 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100393 // This is a temporary hack. Callers must use
394 // createPackageContext(packageName).getApplicationInfo() to
395 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800396 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800397 }
398 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700399 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800400 }
401
402 throw new NameNotFoundException(packageName);
403 }
404
Tao Baic9a02372016-01-12 15:02:24 -0800405 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100406 // If we're dealing with a multi-arch application that has both
407 // 32 and 64 bit shared libraries, we might need to choose the secondary
408 // depending on what the current runtime's instruction set is.
409 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
410 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500411
412 // Get the instruction set that the libraries of secondary Abi is supported.
413 // In presence of a native bridge this might be different than the one secondary Abi used.
414 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
415 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
416 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100417
418 // If the runtimeIsa is the same as the primary isa, then we do nothing.
419 // Everything will be set up correctly because info.nativeLibraryDir will
420 // correspond to the right ISA.
421 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800422 ApplicationInfo modified = new ApplicationInfo(info);
423 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
424 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100425 }
426 }
Tao Baic9a02372016-01-12 15:02:24 -0800427 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100428 }
429
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800430 @Override
431 public ActivityInfo getActivityInfo(ComponentName className, int flags)
432 throws NameNotFoundException {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600433 final int userId = getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800434 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600435 ActivityInfo ai = mPM.getActivityInfo(className,
436 updateFlagsForComponent(flags, userId, null), userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800437 if (ai != null) {
438 return ai;
439 }
440 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700441 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800442 }
443
444 throw new NameNotFoundException(className.toString());
445 }
446
447 @Override
448 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
449 throws NameNotFoundException {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600450 final int userId = getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800451 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600452 ActivityInfo ai = mPM.getReceiverInfo(className,
453 updateFlagsForComponent(flags, userId, null), userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800454 if (ai != null) {
455 return ai;
456 }
457 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700458 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800459 }
460
461 throw new NameNotFoundException(className.toString());
462 }
463
464 @Override
465 public ServiceInfo getServiceInfo(ComponentName className, int flags)
466 throws NameNotFoundException {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600467 final int userId = getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800468 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600469 ServiceInfo si = mPM.getServiceInfo(className,
470 updateFlagsForComponent(flags, userId, null), userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800471 if (si != null) {
472 return si;
473 }
474 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700475 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800476 }
477
478 throw new NameNotFoundException(className.toString());
479 }
480
481 @Override
482 public ProviderInfo getProviderInfo(ComponentName className, int flags)
483 throws NameNotFoundException {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600484 final int userId = getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800485 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600486 ProviderInfo pi = mPM.getProviderInfo(className,
487 updateFlagsForComponent(flags, userId, null), userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800488 if (pi != null) {
489 return pi;
490 }
491 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700492 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800493 }
494
495 throw new NameNotFoundException(className.toString());
496 }
497
498 @Override
499 public String[] getSystemSharedLibraryNames() {
500 try {
501 return mPM.getSystemSharedLibraryNames();
502 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700503 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800504 }
505 }
506
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800507 /** @hide */
508 @Override
Svet Ganov67882122016-12-11 16:36:34 -0800509 public @NonNull List<SharedLibraryInfo> getSharedLibraries(int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600510 return getSharedLibrariesAsUser(flags, getUserId());
Svet Ganov67882122016-12-11 16:36:34 -0800511 }
512
513 /** @hide */
514 @Override
515 @SuppressWarnings("unchecked")
516 public @NonNull List<SharedLibraryInfo> getSharedLibrariesAsUser(int flags, int userId) {
517 try {
518 ParceledListSlice<SharedLibraryInfo> sharedLibs = mPM.getSharedLibraries(
Svet Ganov58c99f02017-05-15 14:07:17 -0700519 mContext.getOpPackageName(), flags, userId);
Svet Ganov67882122016-12-11 16:36:34 -0800520 if (sharedLibs == null) {
521 return Collections.emptyList();
522 }
523 return sharedLibs.getList();
524 } catch (RemoteException e) {
525 throw e.rethrowFromSystemServer();
526 }
527 }
528
529 /** @hide */
530 @Override
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700531 public @NonNull String getServicesSystemSharedLibraryPackageName() {
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800532 try {
533 return mPM.getServicesSystemSharedLibraryPackageName();
534 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700535 throw e.rethrowFromSystemServer();
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800536 }
537 }
538
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700539 /**
540 * @hide
541 */
542 public @NonNull String getSharedSystemSharedLibraryPackageName() {
543 try {
544 return mPM.getSharedSystemSharedLibraryPackageName();
545 } catch (RemoteException e) {
546 throw e.rethrowFromSystemServer();
547 }
548 }
549
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800550 @Override
Todd Kennedy9106c642017-02-08 14:16:53 -0800551 public ChangedPackages getChangedPackages(int sequenceNumber) {
552 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600553 return mPM.getChangedPackages(sequenceNumber, getUserId());
Todd Kennedy9106c642017-02-08 14:16:53 -0800554 } catch (RemoteException e) {
555 throw e.rethrowFromSystemServer();
556 }
557 }
558
559 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700560 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800561 public FeatureInfo[] getSystemAvailableFeatures() {
562 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700563 ParceledListSlice<FeatureInfo> parceledList =
564 mPM.getSystemAvailableFeatures();
565 if (parceledList == null) {
566 return new FeatureInfo[0];
567 }
568 final List<FeatureInfo> list = parceledList.getList();
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700569 final FeatureInfo[] res = new FeatureInfo[list.size()];
570 for (int i = 0; i < res.length; i++) {
571 res[i] = list.get(i);
572 }
573 return res;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800574 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700575 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800576 }
577 }
578
579 @Override
580 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700581 return hasSystemFeature(name, 0);
582 }
583
584 @Override
585 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800586 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700587 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800588 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700589 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800590 }
591 }
592
593 @Override
594 public int checkPermission(String permName, String pkgName) {
595 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600596 return mPM.checkPermission(permName, pkgName, getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800597 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700598 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800599 }
600 }
601
602 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700603 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
604 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600605 return mPM.isPermissionRevokedByPolicy(permName, pkgName, getUserId());
Svet Ganovad3b2972015-07-07 22:49:17 -0700606 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700607 throw e.rethrowFromSystemServer();
Svet Ganovad3b2972015-07-07 22:49:17 -0700608 }
609 }
610
Svet Ganovf1b7f202015-07-29 08:33:42 -0700611 /**
612 * @hide
613 */
614 @Override
615 public String getPermissionControllerPackageName() {
616 synchronized (mLock) {
617 if (mPermissionsControllerPackageName == null) {
618 try {
619 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
620 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700621 throw e.rethrowFromSystemServer();
Svet Ganovf1b7f202015-07-29 08:33:42 -0700622 }
623 }
624 return mPermissionsControllerPackageName;
625 }
626 }
627
Svet Ganovad3b2972015-07-07 22:49:17 -0700628 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800629 public boolean addPermission(PermissionInfo info) {
630 try {
631 return mPM.addPermission(info);
632 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700633 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800634 }
635 }
636
637 @Override
638 public boolean addPermissionAsync(PermissionInfo info) {
639 try {
640 return mPM.addPermissionAsync(info);
641 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700642 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800643 }
644 }
645
646 @Override
647 public void removePermission(String name) {
648 try {
649 mPM.removePermission(name);
650 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700651 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800652 }
653 }
654
655 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700656 public void grantRuntimePermission(String packageName, String permissionName,
657 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800658 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700659 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800660 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700661 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800662 }
663 }
664
665 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700666 public void revokeRuntimePermission(String packageName, String permissionName,
667 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800668 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700669 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
670 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700671 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700672 }
673 }
674
675 @Override
676 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
677 try {
678 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
679 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700680 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700681 }
682 }
683
684 @Override
685 public void updatePermissionFlags(String permissionName, String packageName,
686 int flagMask, int flagValues, UserHandle user) {
687 try {
688 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
689 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800690 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700691 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800692 }
693 }
694
695 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700696 public boolean shouldShowRequestPermissionRationale(String permission) {
697 try {
698 return mPM.shouldShowRequestPermissionRationale(permission,
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600699 mContext.getPackageName(), getUserId());
Svetoslav20770dd2015-05-29 15:43:04 -0700700 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700701 throw e.rethrowFromSystemServer();
Svetoslav20770dd2015-05-29 15:43:04 -0700702 }
703 }
704
705 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800706 public int checkSignatures(String pkg1, String pkg2) {
707 try {
708 return mPM.checkSignatures(pkg1, pkg2);
709 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700710 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800711 }
712 }
713
714 @Override
715 public int checkSignatures(int uid1, int uid2) {
716 try {
717 return mPM.checkUidSignatures(uid1, uid2);
718 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700719 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800720 }
721 }
722
723 @Override
Daniel Cashman5cdda342018-01-19 07:22:52 -0800724 public boolean hasSigningCertificate(
725 String packageName, byte[] certificate, @PackageManager.CertificateInputType int type) {
726 try {
727 return mPM.hasSigningCertificate(packageName, certificate, type);
728 } catch (RemoteException e) {
729 throw e.rethrowFromSystemServer();
730 }
731 }
732
733 @Override
734 public boolean hasSigningCertificate(
735 int uid, byte[] certificate, @PackageManager.CertificateInputType int type) {
736 try {
737 return mPM.hasUidSigningCertificate(uid, certificate, type);
738 } catch (RemoteException e) {
739 throw e.rethrowFromSystemServer();
740 }
741 }
742
743 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800744 public String[] getPackagesForUid(int uid) {
745 try {
746 return mPM.getPackagesForUid(uid);
747 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700748 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800749 }
750 }
751
752 @Override
753 public String getNameForUid(int uid) {
754 try {
755 return mPM.getNameForUid(uid);
756 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700757 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800758 }
759 }
760
761 @Override
Todd Kennedy9da8b8a72017-07-25 15:38:39 -0700762 public String[] getNamesForUids(int[] uids) {
763 try {
764 return mPM.getNamesForUids(uids);
765 } catch (RemoteException e) {
766 throw e.rethrowFromSystemServer();
767 }
768 }
769
770 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800771 public int getUidForSharedUser(String sharedUserName)
772 throws NameNotFoundException {
773 try {
774 int uid = mPM.getUidForSharedUser(sharedUserName);
775 if(uid != -1) {
776 return uid;
777 }
778 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700779 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800780 }
781 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
782 }
783
Kenny Roote6cd0c72011-05-19 12:48:14 -0700784 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800785 @Override
786 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600787 return getInstalledPackagesAsUser(flags, getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700788 }
789
790 /** @hide */
791 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700792 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700793 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800794 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700795 ParceledListSlice<PackageInfo> parceledList =
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600796 mPM.getInstalledPackages(updateFlagsForPackage(flags, userId), userId);
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700797 if (parceledList == null) {
798 return Collections.emptyList();
799 }
800 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800801 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700802 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800803 }
804 }
805
Kenny Roote6cd0c72011-05-19 12:48:14 -0700806 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800807 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800808 public List<PackageInfo> getPackagesHoldingPermissions(
809 String[] permissions, int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600810 final int userId = getUserId();
Dianne Hackborne7991752013-01-16 17:56:46 -0800811 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700812 ParceledListSlice<PackageInfo> parceledList =
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600813 mPM.getPackagesHoldingPermissions(permissions,
814 updateFlagsForPackage(flags, userId), userId);
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700815 if (parceledList == null) {
816 return Collections.emptyList();
817 }
818 return parceledList.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800819 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700820 throw e.rethrowFromSystemServer();
Dianne Hackborne7991752013-01-16 17:56:46 -0800821 }
822 }
823
824 @SuppressWarnings("unchecked")
825 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800826 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600827 return getInstalledApplicationsAsUser(flags, getUserId());
Bartosz Fabianowski11334242016-11-17 20:49:16 +0100828 }
829
830 /** @hide */
831 @SuppressWarnings("unchecked")
832 @Override
833 public List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800834 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700835 ParceledListSlice<ApplicationInfo> parceledList =
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600836 mPM.getInstalledApplications(updateFlagsForApplication(flags, userId), userId);
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700837 if (parceledList == null) {
838 return Collections.emptyList();
839 }
840 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800841 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700842 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800843 }
844 }
845
Svet Ganov2acf0632015-11-24 19:10:59 -0800846 /** @hide */
847 @SuppressWarnings("unchecked")
848 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800849 public List<InstantAppInfo> getInstantApps() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800850 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600851 ParceledListSlice<InstantAppInfo> slice = mPM.getInstantApps(getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800852 if (slice != null) {
853 return slice.getList();
854 }
855 return Collections.emptyList();
856 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700857 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800858 }
859 }
860
861 /** @hide */
862 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800863 public Drawable getInstantAppIcon(String packageName) {
Svet Ganov2acf0632015-11-24 19:10:59 -0800864 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600865 Bitmap bitmap = mPM.getInstantAppIcon(packageName, getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800866 if (bitmap != null) {
867 return new BitmapDrawable(null, bitmap);
868 }
869 return null;
870 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700871 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800872 }
873 }
874
875 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800876 public boolean isInstantApp() {
David Christie31a16552017-03-01 15:08:45 -0800877 return isInstantApp(mContext.getPackageName());
878 }
879
880 @Override
881 public boolean isInstantApp(String packageName) {
Svet Ganov2acf0632015-11-24 19:10:59 -0800882 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600883 return mPM.isInstantApp(packageName, getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800884 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700885 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800886 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800887 }
888
Svetoslav Ganov345ffa52017-04-18 16:08:41 -0700889 public int getInstantAppCookieMaxBytes() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800890 return Settings.Global.getInt(mContext.getContentResolver(),
891 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
892 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
893 }
894
895 @Override
Svetoslav Ganov345ffa52017-04-18 16:08:41 -0700896 public int getInstantAppCookieMaxSize() {
897 return getInstantAppCookieMaxBytes();
898 }
899
900 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800901 public @NonNull byte[] getInstantAppCookie() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800902 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600903 final byte[] cookie = mPM.getInstantAppCookie(mContext.getPackageName(), getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800904 if (cookie != null) {
905 return cookie;
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700906 } else {
907 return EmptyArray.BYTE;
Svet Ganov2acf0632015-11-24 19:10:59 -0800908 }
909 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700910 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800911 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800912 }
913
914 @Override
Svetoslav Ganov345ffa52017-04-18 16:08:41 -0700915 public void clearInstantAppCookie() {
916 updateInstantAppCookie(null);
917 }
918
919 @Override
920 public void updateInstantAppCookie(@NonNull byte[] cookie) {
921 if (cookie != null && cookie.length > getInstantAppCookieMaxBytes()) {
922 throw new IllegalArgumentException("instant cookie longer than "
923 + getInstantAppCookieMaxBytes());
924 }
925 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600926 mPM.setInstantAppCookie(mContext.getPackageName(), cookie, getUserId());
Svetoslav Ganov345ffa52017-04-18 16:08:41 -0700927 } catch (RemoteException e) {
928 throw e.rethrowFromSystemServer();
929 }
930 }
931
932 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800933 public boolean setInstantAppCookie(@NonNull byte[] cookie) {
Svet Ganov2acf0632015-11-24 19:10:59 -0800934 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600935 return mPM.setInstantAppCookie(mContext.getPackageName(), cookie, getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800936 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700937 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800938 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800939 }
940
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800941 @Override
942 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600943 return resolveActivityAsUser(intent, flags, getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700944 }
945
946 @Override
947 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800948 try {
949 return mPM.resolveIntent(
950 intent,
951 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600952 updateFlagsForComponent(flags, userId, intent),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700953 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800954 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700955 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800956 }
957 }
958
959 @Override
960 public List<ResolveInfo> queryIntentActivities(Intent intent,
961 int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600962 return queryIntentActivitiesAsUser(intent, flags, getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700963 }
964
965 /** @hide Same as above but for a specific user */
966 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700967 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700968 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700969 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800970 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600971 ParceledListSlice<ResolveInfo> parceledList = mPM.queryIntentActivities(
972 intent,
973 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
974 updateFlagsForComponent(flags, userId, intent),
975 userId);
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700976 if (parceledList == null) {
977 return Collections.emptyList();
978 }
979 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800980 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700981 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800982 }
983 }
984
985 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700986 @SuppressWarnings("unchecked")
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -0600987 public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller, Intent[] specifics,
988 Intent intent, int flags) {
989 final int userId = getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800990 final ContentResolver resolver = mContext.getContentResolver();
991
992 String[] specificTypes = null;
993 if (specifics != null) {
994 final int N = specifics.length;
995 for (int i=0; i<N; i++) {
996 Intent sp = specifics[i];
997 if (sp != null) {
998 String t = sp.resolveTypeIfNeeded(resolver);
999 if (t != null) {
1000 if (specificTypes == null) {
1001 specificTypes = new String[N];
1002 }
1003 specificTypes[i] = t;
1004 }
1005 }
1006 }
1007 }
1008
1009 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001010 ParceledListSlice<ResolveInfo> parceledList = mPM.queryIntentActivityOptions(
1011 caller,
1012 specifics,
1013 specificTypes,
1014 intent,
1015 intent.resolveTypeIfNeeded(resolver),
1016 updateFlagsForComponent(flags, userId, intent),
1017 userId);
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001018 if (parceledList == null) {
1019 return Collections.emptyList();
1020 }
1021 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001022 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001023 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001024 }
1025 }
1026
Amith Yamasanif203aee2012-08-29 18:41:53 -07001027 /**
1028 * @hide
1029 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001030 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001031 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001032 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001033 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001034 ParceledListSlice<ResolveInfo> parceledList = mPM.queryIntentReceivers(
1035 intent,
1036 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
1037 updateFlagsForComponent(flags, userId, intent),
1038 userId);
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001039 if (parceledList == null) {
1040 return Collections.emptyList();
1041 }
1042 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001043 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001044 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001045 }
1046 }
1047
1048 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -07001049 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001050 return queryBroadcastReceiversAsUser(intent, flags, getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -07001051 }
1052
1053 @Override
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001054 public ResolveInfo resolveServiceAsUser(Intent intent, @ResolveInfoFlags int flags,
1055 @UserIdInt int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001056 try {
1057 return mPM.resolveService(
1058 intent,
1059 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001060 updateFlagsForComponent(flags, userId, intent),
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001061 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001062 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001063 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001064 }
1065 }
1066
1067 @Override
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001068 public ResolveInfo resolveService(Intent intent, int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001069 return resolveServiceAsUser(intent, flags, getUserId());
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001070 }
1071
1072 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001073 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001074 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001075 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001076 ParceledListSlice<ResolveInfo> parceledList = mPM.queryIntentServices(
1077 intent,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001078 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001079 updateFlagsForComponent(flags, userId, intent),
1080 userId);
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001081 if (parceledList == null) {
1082 return Collections.emptyList();
1083 }
1084 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001085 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001086 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001087 }
1088 }
1089
1090 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001091 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001092 return queryIntentServicesAsUser(intent, flags, getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001093 }
1094
1095 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001096 @SuppressWarnings("unchecked")
Jeff Sharkey85f5f812013-10-07 10:16:12 -07001097 public List<ResolveInfo> queryIntentContentProvidersAsUser(
1098 Intent intent, int flags, int userId) {
1099 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001100 ParceledListSlice<ResolveInfo> parceledList = mPM.queryIntentContentProviders(
1101 intent,
1102 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
1103 updateFlagsForComponent(flags, userId, intent),
1104 userId);
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001105 if (parceledList == null) {
1106 return Collections.emptyList();
1107 }
1108 return parceledList.getList();
Jeff Sharkey85f5f812013-10-07 10:16:12 -07001109 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001110 throw e.rethrowFromSystemServer();
Jeff Sharkey85f5f812013-10-07 10:16:12 -07001111 }
1112 }
1113
1114 @Override
1115 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001116 return queryIntentContentProvidersAsUser(intent, flags, getUserId());
Jeff Sharkey85f5f812013-10-07 10:16:12 -07001117 }
1118
1119 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01001120 public ProviderInfo resolveContentProvider(String name, int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001121 return resolveContentProviderAsUser(name, flags, getUserId());
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01001122 }
1123
1124 /** @hide **/
1125 @Override
1126 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001127 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001128 return mPM.resolveContentProvider(name,
1129 updateFlagsForComponent(flags, userId, null), userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001130 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001131 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001132 }
1133 }
1134
1135 @Override
1136 public List<ProviderInfo> queryContentProviders(String processName,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001137 int uid, int flags) {
Makoto Onuki32757292017-02-22 14:36:59 -08001138 return queryContentProviders(processName, uid, flags, null);
1139 }
1140
1141 @Override
1142 @SuppressWarnings("unchecked")
1143 public List<ProviderInfo> queryContentProviders(String processName,
1144 int uid, int flags, String metaDataKey) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001145 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001146 ParceledListSlice<ProviderInfo> slice = mPM.queryContentProviders(processName, uid,
1147 updateFlagsForComponent(flags, UserHandle.getUserId(uid), null), metaDataKey);
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001148 return slice != null ? slice.getList() : Collections.<ProviderInfo>emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001149 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001150 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001151 }
1152 }
1153
1154 @Override
1155 public InstrumentationInfo getInstrumentationInfo(
1156 ComponentName className, int flags)
1157 throws NameNotFoundException {
1158 try {
1159 InstrumentationInfo ii = mPM.getInstrumentationInfo(
1160 className, flags);
1161 if (ii != null) {
1162 return ii;
1163 }
1164 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001165 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001166 }
1167
1168 throw new NameNotFoundException(className.toString());
1169 }
1170
1171 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001172 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001173 public List<InstrumentationInfo> queryInstrumentation(
1174 String targetPackage, int flags) {
1175 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001176 ParceledListSlice<InstrumentationInfo> parceledList =
1177 mPM.queryInstrumentation(targetPackage, flags);
1178 if (parceledList == null) {
1179 return Collections.emptyList();
1180 }
1181 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001182 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001183 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001184 }
1185 }
1186
Alan Viveretteecd585a2015-04-13 10:32:51 -07001187 @Nullable
1188 @Override
1189 public Drawable getDrawable(String packageName, @DrawableRes int resId,
1190 @Nullable ApplicationInfo appInfo) {
1191 final ResourceName name = new ResourceName(packageName, resId);
1192 final Drawable cachedIcon = getCachedIcon(name);
1193 if (cachedIcon != null) {
1194 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001195 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001196
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001197 if (appInfo == null) {
1198 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001199 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001200 } catch (NameNotFoundException e) {
1201 return null;
1202 }
1203 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001204
1205 if (resId != 0) {
1206 try {
1207 final Resources r = getResourcesForApplication(appInfo);
1208 final Drawable dr = r.getDrawable(resId, null);
1209 if (dr != null) {
1210 putCachedIcon(name, dr);
1211 }
1212
1213 if (false) {
1214 RuntimeException e = new RuntimeException("here");
1215 e.fillInStackTrace();
1216 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
1217 + " from package " + packageName
1218 + ": app scale=" + r.getCompatibilityInfo().applicationScale
1219 + ", caller scale=" + mContext.getResources()
1220 .getCompatibilityInfo().applicationScale,
1221 e);
1222 }
Ricky Wai3ce46252015-04-15 16:12:22 +01001223 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001224 Log.v(TAG, "Getting drawable 0x"
1225 + Integer.toHexString(resId) + " from " + r
1226 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +01001227 }
1228 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -07001229 } catch (NameNotFoundException e) {
1230 Log.w("PackageManager", "Failure retrieving resources for "
1231 + appInfo.packageName);
1232 } catch (Resources.NotFoundException e) {
1233 Log.w("PackageManager", "Failure retrieving resources for "
1234 + appInfo.packageName + ": " + e.getMessage());
1235 } catch (Exception e) {
1236 // If an exception was thrown, fall through to return
1237 // default icon.
1238 Log.w("PackageManager", "Failure retrieving icon 0x"
1239 + Integer.toHexString(resId) + " in package "
1240 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001241 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001242 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001243
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001244 return null;
1245 }
1246
1247 @Override public Drawable getActivityIcon(ComponentName activityName)
1248 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001249 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001250 }
1251
1252 @Override public Drawable getActivityIcon(Intent intent)
1253 throws NameNotFoundException {
1254 if (intent.getComponent() != null) {
1255 return getActivityIcon(intent.getComponent());
1256 }
1257
1258 ResolveInfo info = resolveActivity(
1259 intent, PackageManager.MATCH_DEFAULT_ONLY);
1260 if (info != null) {
1261 return info.activityInfo.loadIcon(this);
1262 }
1263
Romain Guy39fe17c2011-11-30 10:34:07 -08001264 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001265 }
1266
1267 @Override public Drawable getDefaultActivityIcon() {
1268 return Resources.getSystem().getDrawable(
1269 com.android.internal.R.drawable.sym_def_app_icon);
1270 }
1271
1272 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1273 return info.loadIcon(this);
1274 }
1275
1276 @Override public Drawable getApplicationIcon(String packageName)
1277 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001278 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001279 }
1280
1281 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001282 public Drawable getActivityBanner(ComponentName activityName)
1283 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001284 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001285 }
1286
1287 @Override
1288 public Drawable getActivityBanner(Intent intent)
1289 throws NameNotFoundException {
1290 if (intent.getComponent() != null) {
1291 return getActivityBanner(intent.getComponent());
1292 }
1293
1294 ResolveInfo info = resolveActivity(
1295 intent, PackageManager.MATCH_DEFAULT_ONLY);
1296 if (info != null) {
1297 return info.activityInfo.loadBanner(this);
1298 }
1299
1300 throw new NameNotFoundException(intent.toUri(0));
1301 }
1302
1303 @Override
1304 public Drawable getApplicationBanner(ApplicationInfo info) {
1305 return info.loadBanner(this);
1306 }
1307
1308 @Override
1309 public Drawable getApplicationBanner(String packageName)
1310 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001311 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001312 }
1313
1314 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001315 public Drawable getActivityLogo(ComponentName activityName)
1316 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001317 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001318 }
1319
1320 @Override
1321 public Drawable getActivityLogo(Intent intent)
1322 throws NameNotFoundException {
1323 if (intent.getComponent() != null) {
1324 return getActivityLogo(intent.getComponent());
1325 }
1326
1327 ResolveInfo info = resolveActivity(
1328 intent, PackageManager.MATCH_DEFAULT_ONLY);
1329 if (info != null) {
1330 return info.activityInfo.loadLogo(this);
1331 }
1332
1333 throw new NameNotFoundException(intent.toUri(0));
1334 }
1335
1336 @Override
1337 public Drawable getApplicationLogo(ApplicationInfo info) {
1338 return info.loadLogo(this);
1339 }
1340
1341 @Override
1342 public Drawable getApplicationLogo(String packageName)
1343 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001344 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001345 }
1346
Svetoslavc7d62f02014-09-04 15:39:54 -07001347 @Override
1348 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
Kenny Guy02c89902016-11-15 19:36:38 +00001349 if (!isManagedProfile(user.getIdentifier())) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001350 return icon;
1351 }
Sunny Goyalbab30752017-04-12 15:36:42 -07001352 Drawable badge = new LauncherIcons(mContext).getBadgeDrawable(
1353 com.android.internal.R.drawable.ic_corp_icon_badge_case,
1354 getUserBadgeColor(user));
Kenny Guy02c89902016-11-15 19:36:38 +00001355 return getBadgedDrawable(icon, badge, null, true);
Svetoslavc7d62f02014-09-04 15:39:54 -07001356 }
1357
1358 @Override
1359 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1360 Rect badgeLocation, int badgeDensity) {
1361 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1362 if (badgeDrawable == null) {
1363 return drawable;
1364 }
1365 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1366 }
1367
Kenny Guy02c89902016-11-15 19:36:38 +00001368 @VisibleForTesting
1369 public static final int[] CORP_BADGE_LABEL_RES_ID = new int[] {
1370 com.android.internal.R.string.managed_profile_label_badge,
1371 com.android.internal.R.string.managed_profile_label_badge_2,
1372 com.android.internal.R.string.managed_profile_label_badge_3
1373 };
1374
1375 private int getUserBadgeColor(UserHandle user) {
Sunny Goyalbab30752017-04-12 15:36:42 -07001376 return IconDrawableFactory.getUserBadgeColor(getUserManager(), user.getIdentifier());
Kenny Guy02c89902016-11-15 19:36:38 +00001377 }
1378
Svetoslavc7d62f02014-09-04 15:39:54 -07001379 @Override
1380 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Kenny Guy02c89902016-11-15 19:36:38 +00001381 Drawable badgeColor = getManagedProfileIconForDensity(user,
1382 com.android.internal.R.drawable.ic_corp_badge_color, density);
1383 if (badgeColor == null) {
1384 return null;
1385 }
Kenny Guy02c89902016-11-15 19:36:38 +00001386 Drawable badgeForeground = getDrawableForDensity(
1387 com.android.internal.R.drawable.ic_corp_badge_case, density);
Tony Make21a1d02018-02-16 11:46:58 +00001388 badgeForeground.setTint(getUserBadgeColor(user));
1389 Drawable badge = new LayerDrawable(new Drawable[] {badgeColor, badgeForeground });
Kenny Guy02c89902016-11-15 19:36:38 +00001390 return badge;
Selim Cineke6ff9462016-01-15 15:07:06 -08001391 }
1392
1393 @Override
1394 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
Kenny Guy02c89902016-11-15 19:36:38 +00001395 Drawable badge = getManagedProfileIconForDensity(user,
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001396 com.android.internal.R.drawable.ic_corp_badge_no_background, density);
Kenny Guy02c89902016-11-15 19:36:38 +00001397 if (badge != null) {
1398 badge.setTint(getUserBadgeColor(user));
1399 }
1400 return badge;
Selim Cineke6ff9462016-01-15 15:07:06 -08001401 }
1402
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001403 private Drawable getDrawableForDensity(int drawableId, int density) {
1404 if (density <= 0) {
1405 density = mContext.getResources().getDisplayMetrics().densityDpi;
1406 }
1407 return Resources.getSystem().getDrawableForDensity(drawableId, density);
1408 }
1409
1410 private Drawable getManagedProfileIconForDensity(UserHandle user, int drawableId, int density) {
Tony Mak8673b282016-03-21 21:10:59 +00001411 if (isManagedProfile(user.getIdentifier())) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001412 return getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001413 }
1414 return null;
1415 }
1416
1417 @Override
1418 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
Tony Mak8673b282016-03-21 21:10:59 +00001419 if (isManagedProfile(user.getIdentifier())) {
Kenny Guy02c89902016-11-15 19:36:38 +00001420 int badge = getUserManager().getManagedProfileBadge(user.getIdentifier());
1421 int resourceId = CORP_BADGE_LABEL_RES_ID[badge % CORP_BADGE_LABEL_RES_ID.length];
1422 return Resources.getSystem().getString(resourceId, label);
Svetoslavc7d62f02014-09-04 15:39:54 -07001423 }
1424 return label;
1425 }
1426
Alan Viveretteecd585a2015-04-13 10:32:51 -07001427 @Override
1428 public Resources getResourcesForActivity(ComponentName activityName)
1429 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001430 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001431 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001432 }
1433
Alan Viveretteecd585a2015-04-13 10:32:51 -07001434 @Override
1435 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1436 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001437 if (app.packageName.equals("system")) {
Adam Lesinskia82b6262017-03-21 16:56:17 -07001438 return mContext.mMainThread.getSystemUiContext().getResources();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001439 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001440 final boolean sameUid = (app.uid == Process.myUid());
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001441 final Resources r = mContext.mMainThread.getTopLevelResources(
Adam Lesinskic82f28a2016-06-08 17:19:09 -07001442 sameUid ? app.sourceDir : app.publicSourceDir,
1443 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
1444 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
Todd Kennedy233a0b12018-01-29 20:30:24 +00001445 mContext.mPackageInfo);
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001446 if (r != null) {
1447 return r;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001448 }
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001449 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1450
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001451 }
1452
Alan Viveretteecd585a2015-04-13 10:32:51 -07001453 @Override
1454 public Resources getResourcesForApplication(String appPackageName)
1455 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001456 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001457 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001458 }
1459
Amith Yamasani98edc952012-09-25 14:09:27 -07001460 /** @hide */
1461 @Override
1462 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1463 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001464 if (userId < 0) {
1465 throw new IllegalArgumentException(
1466 "Call does not support special user #" + userId);
1467 }
1468 if ("system".equals(appPackageName)) {
Adam Lesinskia82b6262017-03-21 16:56:17 -07001469 return mContext.mMainThread.getSystemUiContext().getResources();
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001470 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001471 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001472 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001473 if (ai != null) {
1474 return getResourcesForApplication(ai);
1475 }
1476 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001477 throw e.rethrowFromSystemServer();
Amith Yamasani98edc952012-09-25 14:09:27 -07001478 }
1479 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1480 }
1481
Jeff Sharkeycd654482016-01-08 17:42:11 -07001482 volatile int mCachedSafeMode = -1;
1483
1484 @Override
1485 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001486 try {
1487 if (mCachedSafeMode < 0) {
1488 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1489 }
1490 return mCachedSafeMode != 0;
1491 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001492 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001493 }
1494 }
1495
Svetoslavf7c06eb2015-06-10 18:43:22 -07001496 @Override
1497 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1498 synchronized (mPermissionListeners) {
1499 if (mPermissionListeners.get(listener) != null) {
1500 return;
1501 }
1502 OnPermissionsChangeListenerDelegate delegate =
1503 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1504 try {
1505 mPM.addOnPermissionsChangeListener(delegate);
1506 mPermissionListeners.put(listener, delegate);
1507 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001508 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001509 }
1510 }
1511 }
1512
1513 @Override
1514 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1515 synchronized (mPermissionListeners) {
1516 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1517 if (delegate != null) {
1518 try {
1519 mPM.removeOnPermissionsChangeListener(delegate);
1520 mPermissionListeners.remove(listener);
1521 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001522 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001523 }
1524 }
1525 }
1526 }
1527
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001528 static void configurationChanged() {
1529 synchronized (sSync) {
1530 sIconCache.clear();
1531 sStringCache.clear();
1532 }
1533 }
1534
Yao Chen022b8ea2016-12-16 11:03:28 -08001535 protected ApplicationPackageManager(ContextImpl context,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001536 IPackageManager pm) {
1537 mContext = context;
1538 mPM = pm;
1539 }
1540
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001541 /**
1542 * Update given flags when being used to request {@link PackageInfo}.
1543 */
1544 private int updateFlagsForPackage(int flags, int userId) {
1545 if ((flags & (GET_ACTIVITIES | GET_RECEIVERS | GET_SERVICES | GET_PROVIDERS)) != 0) {
1546 // Caller is asking for component details, so they'd better be
1547 // asking for specific Direct Boot matching behavior
1548 if ((flags & (MATCH_DIRECT_BOOT_UNAWARE
1549 | MATCH_DIRECT_BOOT_AWARE
1550 | MATCH_DIRECT_BOOT_AUTO)) == 0) {
1551 onImplicitDirectBoot(userId);
1552 }
1553 }
1554 return flags;
1555 }
1556
1557 /**
1558 * Update given flags when being used to request {@link ApplicationInfo}.
1559 */
1560 private int updateFlagsForApplication(int flags, int userId) {
1561 return updateFlagsForPackage(flags, userId);
1562 }
1563
1564 /**
1565 * Update given flags when being used to request {@link ComponentInfo}.
1566 */
1567 private int updateFlagsForComponent(int flags, int userId, Intent intent) {
1568 if (intent != null) {
1569 if ((intent.getFlags() & Intent.FLAG_DIRECT_BOOT_AUTO) != 0) {
1570 flags |= MATCH_DIRECT_BOOT_AUTO;
1571 }
1572 }
1573
1574 // Caller is asking for component details, so they'd better be
1575 // asking for specific Direct Boot matching behavior
1576 if ((flags & (MATCH_DIRECT_BOOT_UNAWARE
1577 | MATCH_DIRECT_BOOT_AWARE
1578 | MATCH_DIRECT_BOOT_AUTO)) == 0) {
1579 onImplicitDirectBoot(userId);
1580 }
1581 return flags;
1582 }
1583
1584 private void onImplicitDirectBoot(int userId) {
1585 // Only report if someone is relying on implicit behavior while the user
1586 // is locked; code running when unlocked is going to see both aware and
1587 // unaware components.
1588 if (StrictMode.vmImplicitDirectBootEnabled()) {
1589 // We can cache the unlocked state for the userId we're running as,
1590 // since any relocking of that user will always result in our
1591 // process being killed to release any CE FDs we're holding onto.
1592 if (userId == UserHandle.myUserId()) {
1593 if (mUserUnlocked) {
1594 return;
1595 } else if (mContext.getSystemService(UserManager.class)
1596 .isUserUnlockingOrUnlocked(userId)) {
1597 mUserUnlocked = true;
1598 } else {
1599 StrictMode.onImplicitDirectBoot();
1600 }
1601 } else if (!mContext.getSystemService(UserManager.class)
1602 .isUserUnlockingOrUnlocked(userId)) {
1603 StrictMode.onImplicitDirectBoot();
1604 }
1605 }
1606 }
1607
Alan Viveretteecd585a2015-04-13 10:32:51 -07001608 @Nullable
1609 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001610 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001611 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001612 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1613 + name + ": " + wr);
1614 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001615 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001616 if (state != null) {
1617 if (DEBUG_ICONS) {
1618 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1619 }
1620 // Note: It's okay here to not use the newDrawable(Resources) variant
1621 // of the API. The ConstantState comes from a drawable that was
1622 // originally created by passing the proper app Resources instance
1623 // which means the state should already contain the proper
1624 // resources specific information (like density.) See
1625 // BitmapDrawable.BitmapState for instance.
1626 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001627 }
1628 // our entry has been purged
1629 sIconCache.remove(name);
1630 }
1631 }
1632 return null;
1633 }
1634
Alan Viveretteecd585a2015-04-13 10:32:51 -07001635 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001636 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001637 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001638 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001639 }
1640 }
1641
Romain Guy39fe17c2011-11-30 10:34:07 -08001642 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001643 boolean immediateGc = false;
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001644 if (cmd == ApplicationThreadConstants.EXTERNAL_STORAGE_UNAVAILABLE) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001645 immediateGc = true;
1646 }
1647 if (pkgList != null && (pkgList.length > 0)) {
1648 boolean needCleanup = false;
1649 for (String ssp : pkgList) {
1650 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001651 for (int i=sIconCache.size()-1; i>=0; i--) {
1652 ResourceName nm = sIconCache.keyAt(i);
1653 if (nm.packageName.equals(ssp)) {
1654 //Log.i(TAG, "Removing cached drawable for " + nm);
1655 sIconCache.removeAt(i);
1656 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001657 }
1658 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001659 for (int i=sStringCache.size()-1; i>=0; i--) {
1660 ResourceName nm = sStringCache.keyAt(i);
1661 if (nm.packageName.equals(ssp)) {
1662 //Log.i(TAG, "Removing cached string for " + nm);
1663 sStringCache.removeAt(i);
1664 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001665 }
1666 }
1667 }
1668 }
1669 if (needCleanup || hasPkgInfo) {
1670 if (immediateGc) {
1671 // Schedule an immediate gc.
1672 Runtime.getRuntime().gc();
1673 } else {
1674 ActivityThread.currentActivityThread().scheduleGcIdler();
1675 }
1676 }
1677 }
1678 }
1679
1680 private static final class ResourceName {
1681 final String packageName;
1682 final int iconId;
1683
1684 ResourceName(String _packageName, int _iconId) {
1685 packageName = _packageName;
1686 iconId = _iconId;
1687 }
1688
1689 ResourceName(ApplicationInfo aInfo, int _iconId) {
1690 this(aInfo.packageName, _iconId);
1691 }
1692
1693 ResourceName(ComponentInfo cInfo, int _iconId) {
1694 this(cInfo.applicationInfo.packageName, _iconId);
1695 }
1696
1697 ResourceName(ResolveInfo rInfo, int _iconId) {
1698 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1699 }
1700
1701 @Override
1702 public boolean equals(Object o) {
1703 if (this == o) return true;
1704 if (o == null || getClass() != o.getClass()) return false;
1705
1706 ResourceName that = (ResourceName) o;
1707
1708 if (iconId != that.iconId) return false;
1709 return !(packageName != null ?
1710 !packageName.equals(that.packageName) : that.packageName != null);
1711
1712 }
1713
1714 @Override
1715 public int hashCode() {
1716 int result;
1717 result = packageName.hashCode();
1718 result = 31 * result + iconId;
1719 return result;
1720 }
1721
1722 @Override
1723 public String toString() {
1724 return "{ResourceName " + packageName + " / " + iconId + "}";
1725 }
1726 }
1727
1728 private CharSequence getCachedString(ResourceName name) {
1729 synchronized (sSync) {
1730 WeakReference<CharSequence> wr = sStringCache.get(name);
1731 if (wr != null) { // we have the activity
1732 CharSequence cs = wr.get();
1733 if (cs != null) {
1734 return cs;
1735 }
1736 // our entry has been purged
1737 sStringCache.remove(name);
1738 }
1739 }
1740 return null;
1741 }
1742
1743 private void putCachedString(ResourceName name, CharSequence cs) {
1744 synchronized (sSync) {
1745 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1746 }
1747 }
1748
1749 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001750 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001751 ApplicationInfo appInfo) {
1752 ResourceName name = new ResourceName(packageName, resid);
1753 CharSequence text = getCachedString(name);
1754 if (text != null) {
1755 return text;
1756 }
1757 if (appInfo == null) {
1758 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001759 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001760 } catch (NameNotFoundException e) {
1761 return null;
1762 }
1763 }
1764 try {
1765 Resources r = getResourcesForApplication(appInfo);
1766 text = r.getText(resid);
1767 putCachedString(name, text);
1768 return text;
1769 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001770 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001771 + appInfo.packageName);
1772 } catch (RuntimeException e) {
1773 // If an exception was thrown, fall through to return
1774 // default icon.
1775 Log.w("PackageManager", "Failure retrieving text 0x"
1776 + Integer.toHexString(resid) + " in package "
1777 + packageName, e);
1778 }
1779 return null;
1780 }
1781
1782 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001783 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001784 ApplicationInfo appInfo) {
1785 if (appInfo == null) {
1786 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001787 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001788 } catch (NameNotFoundException e) {
1789 return null;
1790 }
1791 }
1792 try {
1793 Resources r = getResourcesForApplication(appInfo);
1794 return r.getXml(resid);
1795 } catch (RuntimeException e) {
1796 // If an exception was thrown, fall through to return
1797 // default icon.
1798 Log.w("PackageManager", "Failure retrieving xml 0x"
1799 + Integer.toHexString(resid) + " in package "
1800 + packageName, e);
1801 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001802 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001803 + appInfo.packageName);
1804 }
1805 return null;
1806 }
1807
1808 @Override
1809 public CharSequence getApplicationLabel(ApplicationInfo info) {
1810 return info.loadLabel(this);
1811 }
1812
1813 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001814 public int installExistingPackage(String packageName) throws NameNotFoundException {
Sunny Goyala31a74b2017-05-11 15:59:19 -07001815 return installExistingPackage(packageName, PackageManager.INSTALL_REASON_UNKNOWN);
1816 }
1817
1818 @Override
1819 public int installExistingPackage(String packageName, int installReason)
1820 throws NameNotFoundException {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06001821 return installExistingPackageAsUser(packageName, installReason, getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001822 }
1823
1824 @Override
1825 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001826 throws NameNotFoundException {
Sunny Goyala31a74b2017-05-11 15:59:19 -07001827 return installExistingPackageAsUser(packageName, PackageManager.INSTALL_REASON_UNKNOWN,
1828 userId);
1829 }
1830
1831 private int installExistingPackageAsUser(String packageName, int installReason, int userId)
1832 throws NameNotFoundException {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001833 try {
Todd Kennedybe0b8892017-02-15 14:13:52 -08001834 int res = mPM.installExistingPackageAsUser(packageName, userId, 0 /*installFlags*/,
Sunny Goyala31a74b2017-05-11 15:59:19 -07001835 installReason);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001836 if (res == INSTALL_FAILED_INVALID_URI) {
1837 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1838 }
1839 return res;
1840 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001841 throw e.rethrowFromSystemServer();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001842 }
1843 }
1844
1845 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001846 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001847 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001848 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001849 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001850 throw e.rethrowFromSystemServer();
Kenny Root5ab21572011-07-27 11:11:19 -07001851 }
1852 }
1853
1854 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001855 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1856 long millisecondsToDelay) {
1857 try {
1858 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1859 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001860 throw e.rethrowFromSystemServer();
rich canningsd9ef3e52012-08-22 14:28:05 -07001861 }
1862 }
1863
1864 @Override
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001865 public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001866 try {
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001867 mPM.verifyIntentFilter(id, verificationCode, failedDomains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001868 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001869 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001870 }
1871 }
1872
1873 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001874 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001875 try {
1876 return mPM.getIntentVerificationStatus(packageName, userId);
1877 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001878 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001879 }
1880 }
1881
1882 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001883 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001884 try {
1885 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1886 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001887 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001888 }
1889 }
1890
1891 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001892 @SuppressWarnings("unchecked")
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001893 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1894 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001895 ParceledListSlice<IntentFilterVerificationInfo> parceledList =
1896 mPM.getIntentFilterVerifications(packageName);
1897 if (parceledList == null) {
1898 return Collections.emptyList();
1899 }
1900 return parceledList.getList();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001901 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001902 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001903 }
1904 }
1905
1906 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001907 @SuppressWarnings("unchecked")
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001908 public List<IntentFilter> getAllIntentFilters(String packageName) {
1909 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001910 ParceledListSlice<IntentFilter> parceledList =
1911 mPM.getAllIntentFilters(packageName);
1912 if (parceledList == null) {
1913 return Collections.emptyList();
1914 }
1915 return parceledList.getList();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001916 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001917 throw e.rethrowFromSystemServer();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001918 }
1919 }
1920
1921 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001922 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001923 try {
1924 return mPM.getDefaultBrowserPackageName(userId);
1925 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001926 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001927 }
1928 }
1929
1930 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001931 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001932 try {
1933 return mPM.setDefaultBrowserPackageName(packageName, userId);
1934 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001935 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001936 }
1937 }
1938
1939 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001940 public void setInstallerPackageName(String targetPackage,
1941 String installerPackageName) {
1942 try {
1943 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1944 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001945 throw e.rethrowFromSystemServer();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001946 }
1947 }
1948
1949 @Override
Todd Kennedyab532892017-03-08 14:19:49 -08001950 public void setUpdateAvailable(String packageName, boolean updateAvailable) {
1951 try {
1952 mPM.setUpdateAvailable(packageName, updateAvailable);
1953 } catch (RemoteException e) {
1954 throw e.rethrowFromSystemServer();
1955 }
1956 }
1957
1958 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001959 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001960 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001961 return mPM.getInstallerPackageName(packageName);
1962 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001963 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001964 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001965 }
1966
1967 @Override
1968 public int getMoveStatus(int moveId) {
1969 try {
1970 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001971 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001972 throw e.rethrowFromSystemServer();
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001973 }
1974 }
1975
1976 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001977 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1978 synchronized (mDelegates) {
1979 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1980 handler.getLooper());
1981 try {
1982 mPM.registerMoveCallback(delegate);
1983 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001984 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001985 }
1986 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001987 }
1988 }
1989
1990 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001991 public void unregisterMoveCallback(MoveCallback callback) {
1992 synchronized (mDelegates) {
1993 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1994 final MoveCallbackDelegate delegate = i.next();
1995 if (delegate.mCallback == callback) {
1996 try {
1997 mPM.unregisterMoveCallback(delegate);
1998 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001999 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002000 }
2001 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07002002 }
2003 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07002004 }
2005 }
2006
2007 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002008 public int movePackage(String packageName, VolumeInfo vol) {
2009 try {
2010 final String volumeUuid;
2011 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
2012 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
2013 } else if (vol.isPrimaryPhysical()) {
2014 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
2015 } else {
2016 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
2017 }
2018
2019 return mPM.movePackage(packageName, volumeUuid);
2020 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002021 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002022 }
2023 }
2024
2025 @Override
2026 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07002027 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Yao Chen022b8ea2016-12-16 11:03:28 -08002028 return getPackageCurrentVolume(app, storage);
2029 }
2030
2031 @VisibleForTesting
2032 protected @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app,
2033 StorageManager storage) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002034 if (app.isInternal()) {
2035 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
2036 } else if (app.isExternalAsec()) {
2037 return storage.getPrimaryPhysicalVolume();
2038 } else {
2039 return storage.findVolumeByUuid(app.volumeUuid);
2040 }
2041 }
2042
2043 @Override
2044 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
Yao Chen022b8ea2016-12-16 11:03:28 -08002045 final StorageManager storageManager = mContext.getSystemService(StorageManager.class);
2046 return getPackageCandidateVolumes(app, storageManager, mPM);
2047 }
2048
2049 @VisibleForTesting
2050 protected @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app,
2051 StorageManager storageManager, IPackageManager pm) {
2052 final VolumeInfo currentVol = getPackageCurrentVolume(app, storageManager);
2053 final List<VolumeInfo> vols = storageManager.getVolumes();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07002054 final List<VolumeInfo> candidates = new ArrayList<>();
2055 for (VolumeInfo vol : vols) {
Yao Chen022b8ea2016-12-16 11:03:28 -08002056 if (Objects.equals(vol, currentVol)
2057 || isPackageCandidateVolume(mContext, app, vol, pm)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07002058 candidates.add(vol);
2059 }
2060 }
2061 return candidates;
2062 }
2063
Yao Chen022b8ea2016-12-16 11:03:28 -08002064 @VisibleForTesting
2065 protected boolean isForceAllowOnExternal(Context context) {
2066 return Settings.Global.getInt(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07002067 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Yao Chen022b8ea2016-12-16 11:03:28 -08002068 }
2069
2070 @VisibleForTesting
2071 protected boolean isAllow3rdPartyOnInternal(Context context) {
2072 return context.getResources().getBoolean(
2073 com.android.internal.R.bool.config_allow3rdPartyAppOnInternal);
2074 }
2075
2076 private boolean isPackageCandidateVolume(
2077 ContextImpl context, ApplicationInfo app, VolumeInfo vol, IPackageManager pm) {
2078 final boolean forceAllowOnExternal = isForceAllowOnExternal(context);
2079
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07002080 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
Yao Chen022b8ea2016-12-16 11:03:28 -08002081 return app.isSystemApp() || isAllow3rdPartyOnInternal(context);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07002082 }
2083
2084 // System apps and apps demanding internal storage can't be moved
2085 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07002086 if (app.isSystemApp()) {
2087 return false;
2088 }
2089 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07002090 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
2091 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07002092 return false;
2093 }
2094
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002095 // Gotta be able to write there
2096 if (!vol.isMountedWritable()) {
2097 return false;
2098 }
2099
2100 // Moving into an ASEC on public primary is only option internal
2101 if (vol.isPrimaryPhysical()) {
2102 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07002103 }
2104
Makoto Onukif34db0a2016-02-17 11:17:15 -08002105 // Some apps can't be moved. (e.g. device admins)
2106 try {
Yao Chen022b8ea2016-12-16 11:03:28 -08002107 if (pm.isPackageDeviceAdminOnAnyUser(app.packageName)) {
Makoto Onukif34db0a2016-02-17 11:17:15 -08002108 return false;
2109 }
2110 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002111 throw e.rethrowFromSystemServer();
Makoto Onukif34db0a2016-02-17 11:17:15 -08002112 }
2113
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07002114 // Otherwise we can move to any private volume
2115 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
2116 }
2117
2118 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002119 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002120 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002121 final String volumeUuid;
2122 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
2123 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
2124 } else if (vol.isPrimaryPhysical()) {
2125 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
2126 } else {
2127 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
2128 }
2129
2130 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002131 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002132 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002133 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002134 }
2135
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002136 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002137 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
2138 final StorageManager storage = mContext.getSystemService(StorageManager.class);
2139 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07002140 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002141 }
2142
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002143 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002144 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
2145 final StorageManager storage = mContext.getSystemService(StorageManager.class);
2146 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
2147 final List<VolumeInfo> vols = storage.getVolumes();
2148 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002149 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
2150 storage.getPrimaryStorageUuid()) && currentVol != null) {
2151 // TODO: support moving primary physical to emulated volume
2152 candidates.add(currentVol);
2153 } else {
2154 for (VolumeInfo vol : vols) {
2155 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
2156 candidates.add(vol);
2157 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002158 }
2159 }
2160 return candidates;
2161 }
2162
2163 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
2164 // Private internal is always an option
2165 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
2166 return true;
2167 }
2168
2169 // Gotta be able to write there
2170 if (!vol.isMountedWritable()) {
2171 return false;
2172 }
2173
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002174 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002175 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002176 }
2177
2178 @Override
2179 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002180 deletePackageAsUser(packageName, observer, flags, getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01002181 }
2182
2183 @Override
Svet Ganov67882122016-12-11 16:36:34 -08002184 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer,
2185 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002186 try {
Svet Ganov67882122016-12-11 16:36:34 -08002187 mPM.deletePackageAsUser(packageName, PackageManager.VERSION_CODE_HIGHEST,
2188 observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002189 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002190 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002191 }
2192 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07002193
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002194 @Override
2195 public void clearApplicationUserData(String packageName,
2196 IPackageDataObserver observer) {
2197 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002198 mPM.clearApplicationUserData(packageName, observer, getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002199 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002200 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002201 }
2202 }
2203 @Override
2204 public void deleteApplicationCacheFiles(String packageName,
2205 IPackageDataObserver observer) {
2206 try {
2207 mPM.deleteApplicationCacheFiles(packageName, observer);
2208 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002209 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002210 }
2211 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002212
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002213 @Override
Suprabh Shukla78c9eb82016-04-12 15:51:35 -07002214 public void deleteApplicationCacheFilesAsUser(String packageName, int userId,
2215 IPackageDataObserver observer) {
2216 try {
2217 mPM.deleteApplicationCacheFilesAsUser(packageName, userId, observer);
2218 } catch (RemoteException e) {
2219 throw e.rethrowFromSystemServer();
2220 }
2221 }
2222
2223 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002224 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
2225 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002226 try {
Jeff Sharkeyddff8072017-05-26 13:10:46 -06002227 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, 0, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002228 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002229 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002230 }
2231 }
2232
2233 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002234 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002235 try {
Jeff Sharkeyddff8072017-05-26 13:10:46 -06002236 mPM.freeStorage(volumeUuid, freeStorageSize, 0, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002237 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002238 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002239 }
2240 }
2241
2242 @Override
Suprabh Shukla021b57a2018-03-08 18:21:50 -08002243 public String[] setPackagesSuspended(String[] packageNames, boolean suspended,
2244 PersistableBundle appExtras, PersistableBundle launcherExtras,
2245 String dialogMessage) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002246 try {
Suprabh Shukla021b57a2018-03-08 18:21:50 -08002247 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, appExtras,
Suprabh Shukla3c3af142018-03-30 00:28:37 -07002248 launcherExtras, dialogMessage, mContext.getOpPackageName(),
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002249 getUserId());
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002250 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002251 throw e.rethrowFromSystemServer();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002252 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002253 }
2254
2255 @Override
Suprabh Shukla45b9e402018-04-09 17:33:53 -07002256 public Bundle getSuspendedPackageAppExtras() {
2257 final PersistableBundle extras;
Suprabh Shukla021b57a2018-03-08 18:21:50 -08002258 try {
Suprabh Shukla45b9e402018-04-09 17:33:53 -07002259 extras = mPM.getSuspendedPackageAppExtras(mContext.getOpPackageName(),
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002260 getUserId());
Suprabh Shukla021b57a2018-03-08 18:21:50 -08002261 } catch (RemoteException e) {
2262 throw e.rethrowFromSystemServer();
2263 }
Suprabh Shukla2fd43ba2018-03-22 16:57:49 -07002264 return extras != null ? new Bundle(extras.deepCopy()) : null;
Suprabh Shukla021b57a2018-03-08 18:21:50 -08002265 }
2266
2267 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002268 public boolean isPackageSuspendedForUser(String packageName, int userId) {
2269 try {
2270 return mPM.isPackageSuspendedForUser(packageName, userId);
2271 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002272 throw e.rethrowFromSystemServer();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002273 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002274 }
2275
Jeff Sharkey9bc89af2017-01-11 11:25:50 -07002276 /** @hide */
2277 @Override
Suprabh Shukla96212bc2018-04-10 15:04:51 -07002278 public boolean isPackageSuspended(String packageName) throws NameNotFoundException {
2279 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002280 return isPackageSuspendedForUser(packageName, getUserId());
Suprabh Shukla96212bc2018-04-10 15:04:51 -07002281 } catch (IllegalArgumentException ie) {
2282 throw new NameNotFoundException(packageName);
2283 }
Suprabh Shukla021b57a2018-03-08 18:21:50 -08002284 }
2285
2286 @Override
2287 public boolean isPackageSuspended() {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002288 return isPackageSuspendedForUser(mContext.getOpPackageName(), getUserId());
Suprabh Shukla021b57a2018-03-08 18:21:50 -08002289 }
2290
2291 /** @hide */
2292 @Override
Jeff Sharkey9bc89af2017-01-11 11:25:50 -07002293 public void setApplicationCategoryHint(String packageName, int categoryHint) {
2294 try {
2295 mPM.setApplicationCategoryHint(packageName, categoryHint,
2296 mContext.getOpPackageName());
2297 } catch (RemoteException e) {
2298 throw e.rethrowFromSystemServer();
2299 }
2300 }
2301
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002302 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002303 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07002304 IPackageStatsObserver observer) {
Jeff Sharkey6f4b2a32017-03-21 14:13:41 -06002305 final String msg = "Shame on you for calling the hidden API "
2306 + "getPackageSizeInfoAsUser(). Shame!";
Jeff Sharkeye6306c42017-03-07 21:03:18 -07002307 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
Jeff Sharkey6f4b2a32017-03-21 14:13:41 -06002308 throw new UnsupportedOperationException(msg);
Jeff Sharkeye6306c42017-03-07 21:03:18 -07002309 } else if (observer != null) {
Jeff Sharkey6f4b2a32017-03-21 14:13:41 -06002310 Log.d(TAG, msg);
Jeff Sharkeye6306c42017-03-07 21:03:18 -07002311 try {
2312 observer.onGetStatsCompleted(null, false);
2313 } catch (RemoteException ignored) {
2314 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002315 }
2316 }
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002317
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002318 @Override
2319 public void addPackageToPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002320 Log.w(TAG, "addPackageToPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002321 }
2322
2323 @Override
2324 public void removePackageFromPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002325 Log.w(TAG, "removePackageFromPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002326 }
2327
2328 @Override
2329 public List<PackageInfo> getPreferredPackages(int flags) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002330 Log.w(TAG, "getPreferredPackages() is a no-op");
2331 return Collections.emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002332 }
2333
2334 @Override
2335 public void addPreferredActivity(IntentFilter filter,
2336 int match, ComponentName[] set, ComponentName activity) {
2337 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002338 mPM.addPreferredActivity(filter, match, set, activity, getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07002339 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002340 throw e.rethrowFromSystemServer();
Amith Yamasania3f133a2012-08-09 17:11:28 -07002341 }
2342 }
2343
2344 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002345 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07002346 ComponentName[] set, ComponentName activity, int userId) {
2347 try {
2348 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002349 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002350 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002351 }
2352 }
2353
2354 @Override
2355 public void replacePreferredActivity(IntentFilter filter,
2356 int match, ComponentName[] set, ComponentName activity) {
2357 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002358 mPM.replacePreferredActivity(filter, match, set, activity, getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002359 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002360 throw e.rethrowFromSystemServer();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002361 }
2362 }
2363
2364 @Override
2365 public void replacePreferredActivityAsUser(IntentFilter filter,
2366 int match, ComponentName[] set, ComponentName activity,
2367 int userId) {
2368 try {
2369 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002370 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002371 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002372 }
2373 }
2374
2375 @Override
2376 public void clearPackagePreferredActivities(String packageName) {
2377 try {
2378 mPM.clearPackagePreferredActivities(packageName);
2379 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002380 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002381 }
2382 }
2383
2384 @Override
2385 public int getPreferredActivities(List<IntentFilter> outFilters,
2386 List<ComponentName> outActivities, String packageName) {
2387 try {
2388 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2389 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002390 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002391 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002392 }
2393
2394 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002395 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2396 try {
2397 return mPM.getHomeActivities(outActivities);
2398 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002399 throw e.rethrowFromSystemServer();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002400 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002401 }
2402
2403 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002404 public void setComponentEnabledSetting(ComponentName componentName,
2405 int newState, int flags) {
2406 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002407 mPM.setComponentEnabledSetting(componentName, newState, flags, getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002408 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002409 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002410 }
2411 }
2412
2413 @Override
2414 public int getComponentEnabledSetting(ComponentName componentName) {
2415 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002416 return mPM.getComponentEnabledSetting(componentName, getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002417 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002418 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002419 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002420 }
2421
2422 @Override
2423 public void setApplicationEnabledSetting(String packageName,
2424 int newState, int flags) {
2425 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002426 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002427 getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002428 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002429 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002430 }
2431 }
2432
2433 @Override
2434 public int getApplicationEnabledSetting(String packageName) {
2435 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002436 return mPM.getApplicationEnabledSetting(packageName, getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002437 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002438 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002439 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002440 }
2441
Amith Yamasani655d0e22013-06-12 14:19:10 -07002442 @Override
Sudheer Shankabbb3ff22015-07-09 15:39:23 +01002443 public void flushPackageRestrictionsAsUser(int userId) {
2444 try {
2445 mPM.flushPackageRestrictionsAsUser(userId);
2446 } catch (RemoteException e) {
2447 throw e.rethrowFromSystemServer();
2448 }
2449 }
2450
2451 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002452 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002453 UserHandle user) {
2454 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002455 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002456 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002457 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002458 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002459 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002460 }
2461
2462 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002463 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002464 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002465 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002466 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002467 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002468 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002469 }
2470
dcashmanc6f22492014-08-14 09:54:51 -07002471 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002472 @Override
2473 public KeySet getKeySetByAlias(String packageName, String alias) {
2474 Preconditions.checkNotNull(packageName);
2475 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002476 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002477 return mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002478 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002479 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002480 }
dcashman9d2f4412014-06-09 09:27:54 -07002481 }
2482
dcashmanc6f22492014-08-14 09:54:51 -07002483 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002484 @Override
2485 public KeySet getSigningKeySet(String packageName) {
2486 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002487 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002488 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002489 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002490 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002491 }
dcashman9d2f4412014-06-09 09:27:54 -07002492 }
2493
dcashmanc6f22492014-08-14 09:54:51 -07002494 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002495 @Override
2496 public boolean isSignedBy(String packageName, KeySet ks) {
2497 Preconditions.checkNotNull(packageName);
2498 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002499 try {
dcashmanc6f22492014-08-14 09:54:51 -07002500 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002501 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002502 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002503 }
2504 }
2505
dcashmanc6f22492014-08-14 09:54:51 -07002506 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002507 @Override
2508 public boolean isSignedByExactly(String packageName, KeySet ks) {
2509 Preconditions.checkNotNull(packageName);
2510 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002511 try {
dcashmanc6f22492014-08-14 09:54:51 -07002512 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002513 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002514 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002515 }
2516 }
2517
Kenny Root0aaa0d92011-09-12 16:42:55 -07002518 /**
2519 * @hide
2520 */
2521 @Override
2522 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2523 try {
2524 return mPM.getVerifierDeviceIdentity();
2525 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002526 throw e.rethrowFromSystemServer();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002527 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002528 }
2529
Jeff Hao9f60c082014-10-28 18:51:07 -07002530 /**
2531 * @hide
2532 */
2533 @Override
2534 public boolean isUpgrade() {
2535 try {
2536 return mPM.isUpgrade();
2537 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002538 throw e.rethrowFromSystemServer();
Jeff Hao9f60c082014-10-28 18:51:07 -07002539 }
2540 }
2541
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002542 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002543 public PackageInstaller getPackageInstaller() {
2544 synchronized (mLock) {
2545 if (mInstaller == null) {
2546 try {
Svet Ganov67882122016-12-11 16:36:34 -08002547 mInstaller = new PackageInstaller(mPM.getPackageInstaller(),
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002548 mContext.getPackageName(), getUserId());
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002549 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002550 throw e.rethrowFromSystemServer();
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002551 }
2552 }
2553 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002554 }
2555 }
2556
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002557 @Override
2558 public boolean isPackageAvailable(String packageName) {
2559 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002560 return mPM.isPackageAvailable(packageName, getUserId());
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002561 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002562 throw e.rethrowFromSystemServer();
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002563 }
2564 }
2565
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002566 /**
2567 * @hide
2568 */
2569 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002570 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2571 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002572 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002573 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002574 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002575 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002576 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002577 }
2578 }
2579
2580 /**
2581 * @hide
2582 */
2583 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002584 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002585 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002586 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002587 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002588 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002589 }
2590 }
2591
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002592 /**
2593 * @hide
2594 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002595 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002596 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2597 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2598 return dr;
2599 }
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002600 return getUserBadgedIcon(dr, new UserHandle(getUserId()));
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002601 }
2602
2603 /**
2604 * @hide
2605 */
2606 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002607 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002608 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2609 if (bitmap == null) {
Tony Mak213955e2017-11-23 16:57:08 +08002610 return UserIcons.getDefaultUserIcon(
2611 mContext.getResources(), itemInfo.showUserIcon, /* light= */ false);
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002612 }
2613 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002614 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002615 Drawable dr = null;
2616 if (itemInfo.packageName != null) {
2617 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2618 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002619 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002620 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002621 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002622 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002623 }
2624
2625 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2626 Rect badgeLocation, boolean tryBadgeInPlace) {
2627 final int badgedWidth = drawable.getIntrinsicWidth();
2628 final int badgedHeight = drawable.getIntrinsicHeight();
2629 final boolean canBadgeInPlace = tryBadgeInPlace
2630 && (drawable instanceof BitmapDrawable)
2631 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2632
2633 final Bitmap bitmap;
2634 if (canBadgeInPlace) {
2635 bitmap = ((BitmapDrawable) drawable).getBitmap();
2636 } else {
2637 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2638 }
2639 Canvas canvas = new Canvas(bitmap);
2640
2641 if (!canBadgeInPlace) {
2642 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2643 drawable.draw(canvas);
2644 }
2645
2646 if (badgeLocation != null) {
2647 if (badgeLocation.left < 0 || badgeLocation.top < 0
2648 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2649 throw new IllegalArgumentException("Badge location " + badgeLocation
2650 + " not in badged drawable bounds "
2651 + new Rect(0, 0, badgedWidth, badgedHeight));
2652 }
2653 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2654
2655 canvas.save();
2656 canvas.translate(badgeLocation.left, badgeLocation.top);
2657 badgeDrawable.draw(canvas);
2658 canvas.restore();
2659 } else {
2660 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2661 badgeDrawable.draw(canvas);
2662 }
2663
2664 if (!canBadgeInPlace) {
2665 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2666
2667 if (drawable instanceof BitmapDrawable) {
2668 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2669 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2670 }
2671
2672 return mergedDrawable;
2673 }
2674
2675 return drawable;
2676 }
2677
Tony Mak8673b282016-03-21 21:10:59 +00002678 private boolean isManagedProfile(int userId) {
2679 return getUserManager().isManagedProfile(userId);
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002680 }
2681
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +01002682 /**
2683 * @hide
2684 */
2685 @Override
2686 public int getInstallReason(String packageName, UserHandle user) {
2687 try {
2688 return mPM.getInstallReason(packageName, user.getIdentifier());
2689 } catch (RemoteException e) {
2690 throw e.rethrowFromSystemServer();
2691 }
2692 }
2693
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002694 /** {@hide} */
2695 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2696 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002697 private static final int MSG_CREATED = 1;
2698 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002699
2700 final MoveCallback mCallback;
2701 final Handler mHandler;
2702
2703 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2704 mCallback = callback;
2705 mHandler = new Handler(looper, this);
2706 }
2707
2708 @Override
2709 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002710 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002711 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002712 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002713 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002714 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002715 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002716 }
2717 case MSG_STATUS_CHANGED: {
2718 final SomeArgs args = (SomeArgs) msg.obj;
2719 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2720 args.recycle();
2721 return true;
2722 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002723 }
2724 return false;
2725 }
2726
2727 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002728 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002729 final SomeArgs args = SomeArgs.obtain();
2730 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002731 args.arg2 = extras;
2732 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2733 }
2734
2735 @Override
2736 public void onStatusChanged(int moveId, int status, long estMillis) {
2737 final SomeArgs args = SomeArgs.obtain();
2738 args.argi1 = moveId;
2739 args.argi2 = status;
2740 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002741 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002742 }
2743 }
2744
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002745 private final ContextImpl mContext;
2746 private final IPackageManager mPM;
2747
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002748 /** Assume locked until we hear otherwise */
2749 private volatile boolean mUserUnlocked = false;
2750
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002751 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002752 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2753 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2754 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2755 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002756
2757 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2758 mPermissionListeners = new ArrayMap<>();
2759
2760 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2761 implements Handler.Callback{
2762 private static final int MSG_PERMISSIONS_CHANGED = 1;
2763
2764 private final OnPermissionsChangedListener mListener;
2765 private final Handler mHandler;
2766
2767
2768 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2769 Looper looper) {
2770 mListener = listener;
2771 mHandler = new Handler(looper, this);
2772 }
2773
2774 @Override
2775 public void onPermissionsChanged(int uid) {
2776 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2777 }
2778
2779 @Override
2780 public boolean handleMessage(Message msg) {
2781 switch (msg.what) {
2782 case MSG_PERMISSIONS_CHANGED: {
2783 final int uid = msg.arg1;
2784 mListener.onPermissionsChanged(uid);
2785 return true;
2786 }
2787 }
2788 return false;
2789 }
2790 }
Suprabh Shuklaaef25132017-01-23 18:09:03 -08002791
2792 @Override
2793 public boolean canRequestPackageInstalls() {
2794 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002795 return mPM.canRequestPackageInstalls(mContext.getPackageName(), getUserId());
Suprabh Shuklaaef25132017-01-23 18:09:03 -08002796 } catch (RemoteException e) {
2797 throw e.rethrowAsRuntimeException();
2798 }
2799 }
Chad Brubaker336ae5b2017-03-24 15:53:09 -07002800
2801 @Override
2802 public ComponentName getInstantAppResolverSettingsComponent() {
2803 try {
2804 return mPM.getInstantAppResolverSettingsComponent();
2805 } catch (RemoteException e) {
2806 throw e.rethrowAsRuntimeException();
2807 }
2808 }
Todd Kennedy801e6592017-04-12 14:10:12 -07002809
2810 @Override
2811 public ComponentName getInstantAppInstallerComponent() {
2812 try {
2813 return mPM.getInstantAppInstallerComponent();
2814 } catch (RemoteException e) {
2815 throw e.rethrowAsRuntimeException();
2816 }
2817 }
Chad Brubaker0d277a72017-04-12 16:56:53 -07002818
2819 @Override
2820 public String getInstantAppAndroidId(String packageName, UserHandle user) {
2821 try {
2822 return mPM.getInstantAppAndroidId(packageName, user.getIdentifier());
2823 } catch (RemoteException e) {
2824 throw e.rethrowAsRuntimeException();
2825 }
2826 }
Calin Juravle3d2af7f2017-04-19 19:56:21 -07002827
2828 private static class DexModuleRegisterResult {
2829 final String dexModulePath;
2830 final boolean success;
2831 final String message;
2832
2833 private DexModuleRegisterResult(String dexModulePath, boolean success, String message) {
2834 this.dexModulePath = dexModulePath;
2835 this.success = success;
2836 this.message = message;
2837 }
2838 }
2839
2840 private static class DexModuleRegisterCallbackDelegate
2841 extends android.content.pm.IDexModuleRegisterCallback.Stub
2842 implements Handler.Callback {
2843 private static final int MSG_DEX_MODULE_REGISTERED = 1;
2844 private final DexModuleRegisterCallback callback;
2845 private final Handler mHandler;
2846
2847 DexModuleRegisterCallbackDelegate(@NonNull DexModuleRegisterCallback callback) {
2848 this.callback = callback;
2849 mHandler = new Handler(Looper.getMainLooper(), this);
2850 }
2851
2852 @Override
2853 public void onDexModuleRegistered(@NonNull String dexModulePath, boolean success,
2854 @Nullable String message)throws RemoteException {
2855 mHandler.obtainMessage(MSG_DEX_MODULE_REGISTERED,
2856 new DexModuleRegisterResult(dexModulePath, success, message)).sendToTarget();
2857 }
2858
2859 @Override
2860 public boolean handleMessage(Message msg) {
2861 if (msg.what != MSG_DEX_MODULE_REGISTERED) {
2862 return false;
2863 }
2864 DexModuleRegisterResult result = (DexModuleRegisterResult)msg.obj;
2865 callback.onDexModuleRegistered(result.dexModulePath, result.success, result.message);
2866 return true;
2867 }
2868 }
2869
2870 @Override
2871 public void registerDexModule(@NonNull String dexModule,
2872 @Nullable DexModuleRegisterCallback callback) {
2873 // Check if this is a shared module by looking if the others can read it.
2874 boolean isSharedModule = false;
2875 try {
2876 StructStat stat = Os.stat(dexModule);
2877 if ((OsConstants.S_IROTH & stat.st_mode) != 0) {
2878 isSharedModule = true;
2879 }
2880 } catch (ErrnoException e) {
2881 callback.onDexModuleRegistered(dexModule, false,
2882 "Could not get stat the module file: " + e.getMessage());
2883 return;
2884 }
2885
2886 // Module path is ok.
2887 // Create the callback delegate to be passed to package manager service.
2888 DexModuleRegisterCallbackDelegate callbackDelegate = null;
2889 if (callback != null) {
2890 callbackDelegate = new DexModuleRegisterCallbackDelegate(callback);
2891 }
2892
2893 // Invoke the package manager service.
2894 try {
2895 mPM.registerDexModule(mContext.getPackageName(), dexModule,
2896 isSharedModule, callbackDelegate);
2897 } catch (RemoteException e) {
2898 throw e.rethrowAsRuntimeException();
2899 }
2900 }
Calin Juravle45f8b292017-11-07 18:49:43 -08002901
2902 @Override
Ben Gruver1ab3d6e2017-12-07 13:45:08 -08002903 public CharSequence getHarmfulAppWarning(String packageName) {
2904 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002905 return mPM.getHarmfulAppWarning(packageName, getUserId());
Ben Gruver1ab3d6e2017-12-07 13:45:08 -08002906 } catch (RemoteException e) {
2907 throw e.rethrowAsRuntimeException();
2908 }
2909 }
2910
2911 @Override
2912 public void setHarmfulAppWarning(String packageName, CharSequence warning) {
2913 try {
Jeff Sharkeyc59a5e72018-06-21 19:14:58 -06002914 mPM.setHarmfulAppWarning(packageName, warning, getUserId());
Ben Gruver1ab3d6e2017-12-07 13:45:08 -08002915 } catch (RemoteException e) {
2916 throw e.rethrowAsRuntimeException();
2917 }
2918 }
2919
2920 @Override
Calin Juravle45f8b292017-11-07 18:49:43 -08002921 public ArtManager getArtManager() {
2922 synchronized (mLock) {
2923 if (mArtManager == null) {
2924 try {
Jeff Sharkey0ad62832018-04-19 11:20:57 -06002925 mArtManager = new ArtManager(mContext, mPM.getArtManager());
Calin Juravle45f8b292017-11-07 18:49:43 -08002926 } catch (RemoteException e) {
2927 throw e.rethrowFromSystemServer();
2928 }
2929 }
2930 return mArtManager;
2931 }
2932 }
Makoto Onuki700feef2018-02-15 10:59:41 -08002933
2934 @Override
2935 public String getSystemTextClassifierPackageName() {
2936 try {
2937 return mPM.getSystemTextClassifierPackageName();
2938 } catch (RemoteException e) {
2939 throw e.rethrowAsRuntimeException();
2940 }
2941 }
Benjamin Franz7cc9a812018-03-22 10:11:29 +00002942
2943 @Override
2944 public boolean isPackageStateProtected(String packageName, int userId) {
2945 try {
2946 return mPM.isPackageStateProtected(packageName, userId);
2947 } catch (RemoteException e) {
2948 throw e.rethrowAsRuntimeException();
2949 }
2950 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002951}