blob: 4048e6596cfcd8d0c4f90a46a08c2e4be1043cae [file] [log] [blame]
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.DrawableRes;
Alan Viveretteecd585a2015-04-13 10:32:51 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.StringRes;
23import android.annotation.XmlRes;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080024import android.content.ComponentName;
25import android.content.ContentResolver;
Yao Chen022b8ea2016-12-16 11:03:28 -080026import android.content.Context;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080027import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.IntentSender;
30import android.content.pm.ActivityInfo;
31import android.content.pm.ApplicationInfo;
Todd Kennedy9106c642017-02-08 14:16:53 -080032import android.content.pm.ChangedPackages;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080033import android.content.pm.ComponentInfo;
34import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070035import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080036import android.content.pm.IPackageDataObserver;
37import android.content.pm.IPackageDeleteObserver;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080038import android.content.pm.IPackageManager;
39import android.content.pm.IPackageMoveObserver;
40import android.content.pm.IPackageStatsObserver;
Todd Kennedy9da8b8a72017-07-25 15:38:39 -070041import android.content.pm.InstantAppInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080042import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080043import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070044import android.content.pm.KeySet;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080045import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070046import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010047import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080048import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070049import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080050import android.content.pm.PermissionGroupInfo;
51import android.content.pm.PermissionInfo;
52import android.content.pm.ProviderInfo;
53import android.content.pm.ResolveInfo;
54import android.content.pm.ServiceInfo;
Svet Ganov67882122016-12-11 16:36:34 -080055import android.content.pm.SharedLibraryInfo;
Kenny Root0aaa0d92011-09-12 16:42:55 -070056import android.content.pm.VerifierDeviceIdentity;
Svet Ganov67882122016-12-11 16:36:34 -080057import android.content.pm.VersionedPackage;
Calin Juravle45f8b292017-11-07 18:49:43 -080058import android.content.pm.dex.ArtManager;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080059import android.content.res.Resources;
60import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070061import android.graphics.Bitmap;
62import android.graphics.Canvas;
63import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010064import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080065import android.graphics.drawable.Drawable;
Kenny Guy02c89902016-11-15 19:36:38 +000066import android.graphics.drawable.LayerDrawable;
Jeff Sharkeye6306c42017-03-07 21:03:18 -070067import android.os.Build;
Jeff Sharkey50a05452015-04-29 11:24:52 -070068import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070069import android.os.Handler;
70import android.os.Looper;
71import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080072import android.os.Process;
73import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070074import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080075import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010076import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070077import android.os.storage.StorageManager;
78import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070079import android.provider.Settings;
Calin Juravle3d2af7f2017-04-19 19:56:21 -070080import android.system.ErrnoException;
81import android.system.Os;
82import android.system.OsConstants;
83import android.system.StructStat;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070084import android.util.ArrayMap;
Sunny Goyalbab30752017-04-12 15:36:42 -070085import android.util.IconDrawableFactory;
86import android.util.LauncherIcons;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080087import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070088import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070089
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070090import com.android.internal.annotations.GuardedBy;
Kenny Guy02c89902016-11-15 19:36:38 +000091import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070092import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070093import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010094import com.android.internal.util.UserIcons;
Todd Kennedy9da8b8a72017-07-25 15:38:39 -070095
96import dalvik.system.VMRuntime;
97
Svet Ganov2acf0632015-11-24 19:10:59 -080098import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070099
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800100import java.lang.ref.WeakReference;
101import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -0800102import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700103import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800104import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -0700105import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700106import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800107
Makoto Onukicc4bbeb2015-09-17 10:28:24 -0700108/** @hide */
109public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800110 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800111 private final static boolean DEBUG_ICONS = false;
112
Svet Ganov2acf0632015-11-24 19:10:59 -0800113 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
114
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700115 // Default flags to use with PackageManager when no flags are given.
116 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
117
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700118 private final Object mLock = new Object();
119
120 @GuardedBy("mLock")
121 private UserManager mUserManager;
122 @GuardedBy("mLock")
123 private PackageInstaller mInstaller;
Calin Juravle45f8b292017-11-07 18:49:43 -0800124 @GuardedBy("mLock")
125 private ArtManager mArtManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100126
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700127 @GuardedBy("mDelegates")
128 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
129
Svet Ganovf1b7f202015-07-29 08:33:42 -0700130 @GuardedBy("mLock")
131 private String mPermissionsControllerPackageName;
132
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100133 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700134 synchronized (mLock) {
135 if (mUserManager == null) {
136 mUserManager = UserManager.get(mContext);
137 }
138 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100139 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100140 }
141
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800142 @Override
143 public PackageInfo getPackageInfo(String packageName, int flags)
144 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100145 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
146 }
147
148 @Override
Svet Ganov67882122016-12-11 16:36:34 -0800149 public PackageInfo getPackageInfo(VersionedPackage versionedPackage, int flags)
150 throws NameNotFoundException {
151 try {
152 PackageInfo pi = mPM.getPackageInfoVersioned(versionedPackage, flags,
153 mContext.getUserId());
154 if (pi != null) {
155 return pi;
156 }
157 } catch (RemoteException e) {
158 throw e.rethrowFromSystemServer();
159 }
160 throw new NameNotFoundException(versionedPackage.toString());
161 }
162
163 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100164 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
165 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800166 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100167 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800168 if (pi != null) {
169 return pi;
170 }
171 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700172 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800173 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800174 throw new NameNotFoundException(packageName);
175 }
176
177 @Override
178 public String[] currentToCanonicalPackageNames(String[] names) {
179 try {
180 return mPM.currentToCanonicalPackageNames(names);
181 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700182 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800183 }
184 }
185
186 @Override
187 public String[] canonicalToCurrentPackageNames(String[] names) {
188 try {
189 return mPM.canonicalToCurrentPackageNames(names);
190 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700191 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800192 }
193 }
194
195 @Override
196 public Intent getLaunchIntentForPackage(String packageName) {
197 // First see if the package has an INFO activity; the existence of
198 // such an activity is implied to be the desired front-door for the
199 // overall package (such as if it has multiple launcher entries).
200 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
201 intentToResolve.addCategory(Intent.CATEGORY_INFO);
202 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800203 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800204
205 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800206 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800207 // reuse the intent instance
208 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
209 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
210 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800211 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800212 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800213 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800214 return null;
215 }
216 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800217 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800218 intent.setClassName(ris.get(0).activityInfo.packageName,
219 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800220 return intent;
221 }
222
223 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700224 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
225 // Try to find a main leanback_launcher activity.
226 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
227 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
228 intentToResolve.setPackage(packageName);
229 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
230
231 if (ris == null || ris.size() <= 0) {
232 return null;
233 }
234 Intent intent = new Intent(intentToResolve);
235 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
236 intent.setClassName(ris.get(0).activityInfo.packageName,
237 ris.get(0).activityInfo.name);
238 return intent;
239 }
240
241 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700242 public int[] getPackageGids(String packageName) throws NameNotFoundException {
243 return getPackageGids(packageName, 0);
244 }
245
246 @Override
247 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800248 throws NameNotFoundException {
249 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700250 int[] gids = mPM.getPackageGids(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800251 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800252 return gids;
253 }
254 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700255 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800256 }
257
258 throw new NameNotFoundException(packageName);
259 }
260
261 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700262 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
263 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
264 }
265
266 @Override
267 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
268 return getPackageUidAsUser(packageName, 0, userId);
269 }
270
271 @Override
272 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800273 throws NameNotFoundException {
274 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700275 int uid = mPM.getPackageUid(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800276 if (uid >= 0) {
277 return uid;
278 }
279 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700280 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800281 }
282
283 throw new NameNotFoundException(packageName);
284 }
285
286 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800287 public PermissionInfo getPermissionInfo(String name, int flags)
288 throws NameNotFoundException {
289 try {
Svetoslav Ganovadb8c522017-07-28 05:46:53 +0000290 PermissionInfo pi = mPM.getPermissionInfo(name,
291 mContext.getOpPackageName(), flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800292 if (pi != null) {
293 return pi;
294 }
295 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700296 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800297 }
298
299 throw new NameNotFoundException(name);
300 }
301
302 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700303 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800304 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
305 throws NameNotFoundException {
306 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700307 ParceledListSlice<PermissionInfo> parceledList =
308 mPM.queryPermissionsByGroup(group, flags);
309 if (parceledList != null) {
310 List<PermissionInfo> pi = parceledList.getList();
311 if (pi != null) {
312 return pi;
313 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800314 }
315 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700316 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800317 }
318
319 throw new NameNotFoundException(group);
320 }
321
322 @Override
Paul Navin7b89a7b2017-01-26 23:56:08 +0000323 public boolean isPermissionReviewModeEnabled() {
324 return mContext.getResources().getBoolean(
325 com.android.internal.R.bool.config_permissionReviewRequired);
326 }
327
328 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800329 public PermissionGroupInfo getPermissionGroupInfo(String name,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700330 int flags) throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800331 try {
332 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
333 if (pgi != null) {
334 return pgi;
335 }
336 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700337 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800338 }
339
340 throw new NameNotFoundException(name);
341 }
342
343 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700344 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800345 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
346 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700347 ParceledListSlice<PermissionGroupInfo> parceledList =
348 mPM.getAllPermissionGroups(flags);
349 if (parceledList == null) {
350 return Collections.emptyList();
351 }
352 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800353 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700354 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800355 }
356 }
357
358 @Override
359 public ApplicationInfo getApplicationInfo(String packageName, int flags)
360 throws NameNotFoundException {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700361 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
362 }
363
364 @Override
365 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
366 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800367 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700368 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800369 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100370 // This is a temporary hack. Callers must use
371 // createPackageContext(packageName).getApplicationInfo() to
372 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800373 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800374 }
375 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700376 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800377 }
378
379 throw new NameNotFoundException(packageName);
380 }
381
Tao Baic9a02372016-01-12 15:02:24 -0800382 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100383 // If we're dealing with a multi-arch application that has both
384 // 32 and 64 bit shared libraries, we might need to choose the secondary
385 // depending on what the current runtime's instruction set is.
386 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
387 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500388
389 // Get the instruction set that the libraries of secondary Abi is supported.
390 // In presence of a native bridge this might be different than the one secondary Abi used.
391 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
392 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
393 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100394
395 // If the runtimeIsa is the same as the primary isa, then we do nothing.
396 // Everything will be set up correctly because info.nativeLibraryDir will
397 // correspond to the right ISA.
398 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800399 ApplicationInfo modified = new ApplicationInfo(info);
400 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
401 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100402 }
403 }
Tao Baic9a02372016-01-12 15:02:24 -0800404 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100405 }
406
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800407 @Override
408 public ActivityInfo getActivityInfo(ComponentName className, int flags)
409 throws NameNotFoundException {
410 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700411 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800412 if (ai != null) {
413 return ai;
414 }
415 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700416 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800417 }
418
419 throw new NameNotFoundException(className.toString());
420 }
421
422 @Override
423 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
424 throws NameNotFoundException {
425 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700426 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800427 if (ai != null) {
428 return ai;
429 }
430 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700431 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800432 }
433
434 throw new NameNotFoundException(className.toString());
435 }
436
437 @Override
438 public ServiceInfo getServiceInfo(ComponentName className, int flags)
439 throws NameNotFoundException {
440 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700441 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800442 if (si != null) {
443 return si;
444 }
445 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700446 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800447 }
448
449 throw new NameNotFoundException(className.toString());
450 }
451
452 @Override
453 public ProviderInfo getProviderInfo(ComponentName className, int flags)
454 throws NameNotFoundException {
455 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700456 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800457 if (pi != null) {
458 return pi;
459 }
460 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700461 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800462 }
463
464 throw new NameNotFoundException(className.toString());
465 }
466
467 @Override
468 public String[] getSystemSharedLibraryNames() {
469 try {
470 return mPM.getSystemSharedLibraryNames();
471 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700472 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800473 }
474 }
475
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800476 /** @hide */
477 @Override
Svet Ganov67882122016-12-11 16:36:34 -0800478 public @NonNull List<SharedLibraryInfo> getSharedLibraries(int flags) {
479 return getSharedLibrariesAsUser(flags, mContext.getUserId());
480 }
481
482 /** @hide */
483 @Override
484 @SuppressWarnings("unchecked")
485 public @NonNull List<SharedLibraryInfo> getSharedLibrariesAsUser(int flags, int userId) {
486 try {
487 ParceledListSlice<SharedLibraryInfo> sharedLibs = mPM.getSharedLibraries(
Svet Ganov58c99f02017-05-15 14:07:17 -0700488 mContext.getOpPackageName(), flags, userId);
Svet Ganov67882122016-12-11 16:36:34 -0800489 if (sharedLibs == null) {
490 return Collections.emptyList();
491 }
492 return sharedLibs.getList();
493 } catch (RemoteException e) {
494 throw e.rethrowFromSystemServer();
495 }
496 }
497
498 /** @hide */
499 @Override
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700500 public @NonNull String getServicesSystemSharedLibraryPackageName() {
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800501 try {
502 return mPM.getServicesSystemSharedLibraryPackageName();
503 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700504 throw e.rethrowFromSystemServer();
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800505 }
506 }
507
Svetoslav Ganova9c25002016-04-13 19:25:56 -0700508 /**
509 * @hide
510 */
511 public @NonNull String getSharedSystemSharedLibraryPackageName() {
512 try {
513 return mPM.getSharedSystemSharedLibraryPackageName();
514 } catch (RemoteException e) {
515 throw e.rethrowFromSystemServer();
516 }
517 }
518
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800519 @Override
Todd Kennedy9106c642017-02-08 14:16:53 -0800520 public ChangedPackages getChangedPackages(int sequenceNumber) {
521 try {
522 return mPM.getChangedPackages(sequenceNumber, mContext.getUserId());
523 } catch (RemoteException e) {
524 throw e.rethrowFromSystemServer();
525 }
526 }
527
528 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700529 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800530 public FeatureInfo[] getSystemAvailableFeatures() {
531 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700532 ParceledListSlice<FeatureInfo> parceledList =
533 mPM.getSystemAvailableFeatures();
534 if (parceledList == null) {
535 return new FeatureInfo[0];
536 }
537 final List<FeatureInfo> list = parceledList.getList();
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700538 final FeatureInfo[] res = new FeatureInfo[list.size()];
539 for (int i = 0; i < res.length; i++) {
540 res[i] = list.get(i);
541 }
542 return res;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800543 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700544 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800545 }
546 }
547
548 @Override
549 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700550 return hasSystemFeature(name, 0);
551 }
552
553 @Override
554 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800555 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700556 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800557 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700558 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800559 }
560 }
561
562 @Override
563 public int checkPermission(String permName, String pkgName) {
564 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800565 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800566 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700567 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800568 }
569 }
570
571 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700572 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
573 try {
574 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
575 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700576 throw e.rethrowFromSystemServer();
Svet Ganovad3b2972015-07-07 22:49:17 -0700577 }
578 }
579
Svet Ganovf1b7f202015-07-29 08:33:42 -0700580 /**
581 * @hide
582 */
583 @Override
584 public String getPermissionControllerPackageName() {
585 synchronized (mLock) {
586 if (mPermissionsControllerPackageName == null) {
587 try {
588 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
589 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700590 throw e.rethrowFromSystemServer();
Svet Ganovf1b7f202015-07-29 08:33:42 -0700591 }
592 }
593 return mPermissionsControllerPackageName;
594 }
595 }
596
Svet Ganovad3b2972015-07-07 22:49:17 -0700597 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800598 public boolean addPermission(PermissionInfo info) {
599 try {
600 return mPM.addPermission(info);
601 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700602 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800603 }
604 }
605
606 @Override
607 public boolean addPermissionAsync(PermissionInfo info) {
608 try {
609 return mPM.addPermissionAsync(info);
610 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700611 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800612 }
613 }
614
615 @Override
616 public void removePermission(String name) {
617 try {
618 mPM.removePermission(name);
619 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700620 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800621 }
622 }
623
624 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700625 public void grantRuntimePermission(String packageName, String permissionName,
626 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800627 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700628 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800629 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700630 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800631 }
632 }
633
634 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700635 public void revokeRuntimePermission(String packageName, String permissionName,
636 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800637 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700638 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
639 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700640 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700641 }
642 }
643
644 @Override
645 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
646 try {
647 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
648 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700649 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700650 }
651 }
652
653 @Override
654 public void updatePermissionFlags(String permissionName, String packageName,
655 int flagMask, int flagValues, UserHandle user) {
656 try {
657 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
658 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800659 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700660 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800661 }
662 }
663
664 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700665 public boolean shouldShowRequestPermissionRationale(String permission) {
666 try {
667 return mPM.shouldShowRequestPermissionRationale(permission,
668 mContext.getPackageName(), mContext.getUserId());
669 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700670 throw e.rethrowFromSystemServer();
Svetoslav20770dd2015-05-29 15:43:04 -0700671 }
672 }
673
674 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800675 public int checkSignatures(String pkg1, String pkg2) {
676 try {
677 return mPM.checkSignatures(pkg1, pkg2);
678 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700679 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800680 }
681 }
682
683 @Override
684 public int checkSignatures(int uid1, int uid2) {
685 try {
686 return mPM.checkUidSignatures(uid1, uid2);
687 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700688 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800689 }
690 }
691
692 @Override
693 public String[] getPackagesForUid(int uid) {
694 try {
695 return mPM.getPackagesForUid(uid);
696 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700697 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800698 }
699 }
700
701 @Override
702 public String getNameForUid(int uid) {
703 try {
704 return mPM.getNameForUid(uid);
705 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700706 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800707 }
708 }
709
710 @Override
Todd Kennedy9da8b8a72017-07-25 15:38:39 -0700711 public String[] getNamesForUids(int[] uids) {
712 try {
713 return mPM.getNamesForUids(uids);
714 } catch (RemoteException e) {
715 throw e.rethrowFromSystemServer();
716 }
717 }
718
719 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800720 public int getUidForSharedUser(String sharedUserName)
721 throws NameNotFoundException {
722 try {
723 int uid = mPM.getUidForSharedUser(sharedUserName);
724 if(uid != -1) {
725 return uid;
726 }
727 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700728 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800729 }
730 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
731 }
732
Kenny Roote6cd0c72011-05-19 12:48:14 -0700733 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800734 @Override
735 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700736 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700737 }
738
739 /** @hide */
740 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700741 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700742 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800743 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700744 ParceledListSlice<PackageInfo> parceledList =
745 mPM.getInstalledPackages(flags, userId);
746 if (parceledList == null) {
747 return Collections.emptyList();
748 }
749 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800750 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700751 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800752 }
753 }
754
Kenny Roote6cd0c72011-05-19 12:48:14 -0700755 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800756 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800757 public List<PackageInfo> getPackagesHoldingPermissions(
758 String[] permissions, int flags) {
759 final int userId = mContext.getUserId();
760 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700761 ParceledListSlice<PackageInfo> parceledList =
762 mPM.getPackagesHoldingPermissions(permissions, flags, userId);
763 if (parceledList == null) {
764 return Collections.emptyList();
765 }
766 return parceledList.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800767 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700768 throw e.rethrowFromSystemServer();
Dianne Hackborne7991752013-01-16 17:56:46 -0800769 }
770 }
771
772 @SuppressWarnings("unchecked")
773 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800774 public List<ApplicationInfo> getInstalledApplications(int flags) {
Bartosz Fabianowski11334242016-11-17 20:49:16 +0100775 return getInstalledApplicationsAsUser(flags, mContext.getUserId());
776 }
777
778 /** @hide */
779 @SuppressWarnings("unchecked")
780 @Override
781 public List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800782 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700783 ParceledListSlice<ApplicationInfo> parceledList =
784 mPM.getInstalledApplications(flags, userId);
785 if (parceledList == null) {
786 return Collections.emptyList();
787 }
788 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800789 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700790 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800791 }
792 }
793
Svet Ganov2acf0632015-11-24 19:10:59 -0800794 /** @hide */
795 @SuppressWarnings("unchecked")
796 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800797 public List<InstantAppInfo> getInstantApps() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800798 try {
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800799 ParceledListSlice<InstantAppInfo> slice =
800 mPM.getInstantApps(mContext.getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800801 if (slice != null) {
802 return slice.getList();
803 }
804 return Collections.emptyList();
805 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700806 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800807 }
808 }
809
810 /** @hide */
811 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800812 public Drawable getInstantAppIcon(String packageName) {
Svet Ganov2acf0632015-11-24 19:10:59 -0800813 try {
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800814 Bitmap bitmap = mPM.getInstantAppIcon(
Svet Ganov2acf0632015-11-24 19:10:59 -0800815 packageName, mContext.getUserId());
816 if (bitmap != null) {
817 return new BitmapDrawable(null, bitmap);
818 }
819 return null;
820 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700821 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800822 }
823 }
824
825 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800826 public boolean isInstantApp() {
David Christie31a16552017-03-01 15:08:45 -0800827 return isInstantApp(mContext.getPackageName());
828 }
829
830 @Override
831 public boolean isInstantApp(String packageName) {
Svet Ganov2acf0632015-11-24 19:10:59 -0800832 try {
David Christie31a16552017-03-01 15:08:45 -0800833 return mPM.isInstantApp(packageName, mContext.getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800834 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700835 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800836 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800837 }
838
Svetoslav Ganov345ffa52017-04-18 16:08:41 -0700839 public int getInstantAppCookieMaxBytes() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800840 return Settings.Global.getInt(mContext.getContentResolver(),
841 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
842 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
843 }
844
845 @Override
Svetoslav Ganov345ffa52017-04-18 16:08:41 -0700846 public int getInstantAppCookieMaxSize() {
847 return getInstantAppCookieMaxBytes();
848 }
849
850 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800851 public @NonNull byte[] getInstantAppCookie() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800852 try {
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800853 final byte[] cookie = mPM.getInstantAppCookie(
Svet Ganov2acf0632015-11-24 19:10:59 -0800854 mContext.getPackageName(), mContext.getUserId());
855 if (cookie != null) {
856 return cookie;
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700857 } else {
858 return EmptyArray.BYTE;
Svet Ganov2acf0632015-11-24 19:10:59 -0800859 }
860 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700861 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800862 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800863 }
864
865 @Override
Svetoslav Ganov345ffa52017-04-18 16:08:41 -0700866 public void clearInstantAppCookie() {
867 updateInstantAppCookie(null);
868 }
869
870 @Override
871 public void updateInstantAppCookie(@NonNull byte[] cookie) {
872 if (cookie != null && cookie.length > getInstantAppCookieMaxBytes()) {
873 throw new IllegalArgumentException("instant cookie longer than "
874 + getInstantAppCookieMaxBytes());
875 }
876 try {
877 mPM.setInstantAppCookie(mContext.getPackageName(),
878 cookie, mContext.getUserId());
879 } catch (RemoteException e) {
880 throw e.rethrowFromSystemServer();
881 }
882 }
883
884 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800885 public boolean setInstantAppCookie(@NonNull byte[] cookie) {
Svet Ganov2acf0632015-11-24 19:10:59 -0800886 try {
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800887 return mPM.setInstantAppCookie(mContext.getPackageName(),
888 cookie, mContext.getUserId());
Svet Ganov2acf0632015-11-24 19:10:59 -0800889 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700890 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800891 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800892 }
893
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800894 @Override
895 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700896 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700897 }
898
899 @Override
900 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800901 try {
902 return mPM.resolveIntent(
903 intent,
904 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700905 flags,
906 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800907 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700908 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800909 }
910 }
911
912 @Override
913 public List<ResolveInfo> queryIntentActivities(Intent intent,
914 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700915 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700916 }
917
918 /** @hide Same as above but for a specific user */
919 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700920 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700921 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700922 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800923 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700924 ParceledListSlice<ResolveInfo> parceledList =
925 mPM.queryIntentActivities(intent,
926 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
927 flags, userId);
928 if (parceledList == null) {
929 return Collections.emptyList();
930 }
931 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800932 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700933 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800934 }
935 }
936
937 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700938 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800939 public List<ResolveInfo> queryIntentActivityOptions(
940 ComponentName caller, Intent[] specifics, Intent intent,
941 int flags) {
942 final ContentResolver resolver = mContext.getContentResolver();
943
944 String[] specificTypes = null;
945 if (specifics != null) {
946 final int N = specifics.length;
947 for (int i=0; i<N; i++) {
948 Intent sp = specifics[i];
949 if (sp != null) {
950 String t = sp.resolveTypeIfNeeded(resolver);
951 if (t != null) {
952 if (specificTypes == null) {
953 specificTypes = new String[N];
954 }
955 specificTypes[i] = t;
956 }
957 }
958 }
959 }
960
961 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700962 ParceledListSlice<ResolveInfo> parceledList =
963 mPM.queryIntentActivityOptions(caller, specifics, specificTypes, intent,
964 intent.resolveTypeIfNeeded(resolver), flags, mContext.getUserId());
965 if (parceledList == null) {
966 return Collections.emptyList();
967 }
968 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800969 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700970 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800971 }
972 }
973
Amith Yamasanif203aee2012-08-29 18:41:53 -0700974 /**
975 * @hide
976 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800977 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700978 @SuppressWarnings("unchecked")
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700979 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800980 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -0700981 ParceledListSlice<ResolveInfo> parceledList =
982 mPM.queryIntentReceivers(intent,
983 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
984 flags, userId);
985 if (parceledList == null) {
986 return Collections.emptyList();
987 }
988 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800989 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700990 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800991 }
992 }
993
994 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700995 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700996 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700997 }
998
999 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001000 public ResolveInfo resolveService(Intent intent, int flags) {
1001 try {
1002 return mPM.resolveService(
1003 intent,
1004 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -07001005 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001006 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001007 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001008 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001009 }
1010 }
1011
1012 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001013 @SuppressWarnings("unchecked")
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001014 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001015 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001016 ParceledListSlice<ResolveInfo> parceledList =
1017 mPM.queryIntentServices(intent,
1018 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
1019 flags, userId);
1020 if (parceledList == null) {
1021 return Collections.emptyList();
1022 }
1023 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001024 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001025 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001026 }
1027 }
1028
1029 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001030 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001031 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -07001032 }
1033
1034 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001035 @SuppressWarnings("unchecked")
Jeff Sharkey85f5f812013-10-07 10:16:12 -07001036 public List<ResolveInfo> queryIntentContentProvidersAsUser(
1037 Intent intent, int flags, int userId) {
1038 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001039 ParceledListSlice<ResolveInfo> parceledList =
1040 mPM.queryIntentContentProviders(intent,
1041 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
1042 flags, userId);
1043 if (parceledList == null) {
1044 return Collections.emptyList();
1045 }
1046 return parceledList.getList();
Jeff Sharkey85f5f812013-10-07 10:16:12 -07001047 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001048 throw e.rethrowFromSystemServer();
Jeff Sharkey85f5f812013-10-07 10:16:12 -07001049 }
1050 }
1051
1052 @Override
1053 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
1054 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
1055 }
1056
1057 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01001058 public ProviderInfo resolveContentProvider(String name, int flags) {
1059 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
1060 }
1061
1062 /** @hide **/
1063 @Override
1064 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001065 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +01001066 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001067 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001068 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001069 }
1070 }
1071
1072 @Override
1073 public List<ProviderInfo> queryContentProviders(String processName,
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001074 int uid, int flags) {
Makoto Onuki32757292017-02-22 14:36:59 -08001075 return queryContentProviders(processName, uid, flags, null);
1076 }
1077
1078 @Override
1079 @SuppressWarnings("unchecked")
1080 public List<ProviderInfo> queryContentProviders(String processName,
1081 int uid, int flags, String metaDataKey) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001082 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001083 ParceledListSlice<ProviderInfo> slice =
Makoto Onuki32757292017-02-22 14:36:59 -08001084 mPM.queryContentProviders(processName, uid, flags, metaDataKey);
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001085 return slice != null ? slice.getList() : Collections.<ProviderInfo>emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001086 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001087 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001088 }
1089 }
1090
1091 @Override
1092 public InstrumentationInfo getInstrumentationInfo(
1093 ComponentName className, int flags)
1094 throws NameNotFoundException {
1095 try {
1096 InstrumentationInfo ii = mPM.getInstrumentationInfo(
1097 className, flags);
1098 if (ii != null) {
1099 return ii;
1100 }
1101 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001102 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001103 }
1104
1105 throw new NameNotFoundException(className.toString());
1106 }
1107
1108 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001109 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001110 public List<InstrumentationInfo> queryInstrumentation(
1111 String targetPackage, int flags) {
1112 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001113 ParceledListSlice<InstrumentationInfo> parceledList =
1114 mPM.queryInstrumentation(targetPackage, flags);
1115 if (parceledList == null) {
1116 return Collections.emptyList();
1117 }
1118 return parceledList.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001119 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001120 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001121 }
1122 }
1123
Alan Viveretteecd585a2015-04-13 10:32:51 -07001124 @Nullable
1125 @Override
1126 public Drawable getDrawable(String packageName, @DrawableRes int resId,
1127 @Nullable ApplicationInfo appInfo) {
1128 final ResourceName name = new ResourceName(packageName, resId);
1129 final Drawable cachedIcon = getCachedIcon(name);
1130 if (cachedIcon != null) {
1131 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001132 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001133
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001134 if (appInfo == null) {
1135 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001136 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001137 } catch (NameNotFoundException e) {
1138 return null;
1139 }
1140 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001141
1142 if (resId != 0) {
1143 try {
1144 final Resources r = getResourcesForApplication(appInfo);
1145 final Drawable dr = r.getDrawable(resId, null);
1146 if (dr != null) {
1147 putCachedIcon(name, dr);
1148 }
1149
1150 if (false) {
1151 RuntimeException e = new RuntimeException("here");
1152 e.fillInStackTrace();
1153 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
1154 + " from package " + packageName
1155 + ": app scale=" + r.getCompatibilityInfo().applicationScale
1156 + ", caller scale=" + mContext.getResources()
1157 .getCompatibilityInfo().applicationScale,
1158 e);
1159 }
Ricky Wai3ce46252015-04-15 16:12:22 +01001160 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001161 Log.v(TAG, "Getting drawable 0x"
1162 + Integer.toHexString(resId) + " from " + r
1163 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +01001164 }
1165 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -07001166 } catch (NameNotFoundException e) {
1167 Log.w("PackageManager", "Failure retrieving resources for "
1168 + appInfo.packageName);
1169 } catch (Resources.NotFoundException e) {
1170 Log.w("PackageManager", "Failure retrieving resources for "
1171 + appInfo.packageName + ": " + e.getMessage());
1172 } catch (Exception e) {
1173 // If an exception was thrown, fall through to return
1174 // default icon.
1175 Log.w("PackageManager", "Failure retrieving icon 0x"
1176 + Integer.toHexString(resId) + " in package "
1177 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001178 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001179 }
Alan Viveretteecd585a2015-04-13 10:32:51 -07001180
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001181 return null;
1182 }
1183
1184 @Override public Drawable getActivityIcon(ComponentName activityName)
1185 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001186 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001187 }
1188
1189 @Override public Drawable getActivityIcon(Intent intent)
1190 throws NameNotFoundException {
1191 if (intent.getComponent() != null) {
1192 return getActivityIcon(intent.getComponent());
1193 }
1194
1195 ResolveInfo info = resolveActivity(
1196 intent, PackageManager.MATCH_DEFAULT_ONLY);
1197 if (info != null) {
1198 return info.activityInfo.loadIcon(this);
1199 }
1200
Romain Guy39fe17c2011-11-30 10:34:07 -08001201 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001202 }
1203
1204 @Override public Drawable getDefaultActivityIcon() {
1205 return Resources.getSystem().getDrawable(
1206 com.android.internal.R.drawable.sym_def_app_icon);
1207 }
1208
1209 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1210 return info.loadIcon(this);
1211 }
1212
1213 @Override public Drawable getApplicationIcon(String packageName)
1214 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001215 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001216 }
1217
1218 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001219 public Drawable getActivityBanner(ComponentName activityName)
1220 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001221 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001222 }
1223
1224 @Override
1225 public Drawable getActivityBanner(Intent intent)
1226 throws NameNotFoundException {
1227 if (intent.getComponent() != null) {
1228 return getActivityBanner(intent.getComponent());
1229 }
1230
1231 ResolveInfo info = resolveActivity(
1232 intent, PackageManager.MATCH_DEFAULT_ONLY);
1233 if (info != null) {
1234 return info.activityInfo.loadBanner(this);
1235 }
1236
1237 throw new NameNotFoundException(intent.toUri(0));
1238 }
1239
1240 @Override
1241 public Drawable getApplicationBanner(ApplicationInfo info) {
1242 return info.loadBanner(this);
1243 }
1244
1245 @Override
1246 public Drawable getApplicationBanner(String packageName)
1247 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001248 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001249 }
1250
1251 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001252 public Drawable getActivityLogo(ComponentName activityName)
1253 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001254 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001255 }
1256
1257 @Override
1258 public Drawable getActivityLogo(Intent intent)
1259 throws NameNotFoundException {
1260 if (intent.getComponent() != null) {
1261 return getActivityLogo(intent.getComponent());
1262 }
1263
1264 ResolveInfo info = resolveActivity(
1265 intent, PackageManager.MATCH_DEFAULT_ONLY);
1266 if (info != null) {
1267 return info.activityInfo.loadLogo(this);
1268 }
1269
1270 throw new NameNotFoundException(intent.toUri(0));
1271 }
1272
1273 @Override
1274 public Drawable getApplicationLogo(ApplicationInfo info) {
1275 return info.loadLogo(this);
1276 }
1277
1278 @Override
1279 public Drawable getApplicationLogo(String packageName)
1280 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001281 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001282 }
1283
Svetoslavc7d62f02014-09-04 15:39:54 -07001284 @Override
1285 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
Kenny Guy02c89902016-11-15 19:36:38 +00001286 if (!isManagedProfile(user.getIdentifier())) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001287 return icon;
1288 }
Sunny Goyalbab30752017-04-12 15:36:42 -07001289 Drawable badge = new LauncherIcons(mContext).getBadgeDrawable(
1290 com.android.internal.R.drawable.ic_corp_icon_badge_case,
1291 getUserBadgeColor(user));
Kenny Guy02c89902016-11-15 19:36:38 +00001292 return getBadgedDrawable(icon, badge, null, true);
Svetoslavc7d62f02014-09-04 15:39:54 -07001293 }
1294
1295 @Override
1296 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1297 Rect badgeLocation, int badgeDensity) {
1298 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1299 if (badgeDrawable == null) {
1300 return drawable;
1301 }
1302 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1303 }
1304
Kenny Guy02c89902016-11-15 19:36:38 +00001305 @VisibleForTesting
1306 public static final int[] CORP_BADGE_LABEL_RES_ID = new int[] {
1307 com.android.internal.R.string.managed_profile_label_badge,
1308 com.android.internal.R.string.managed_profile_label_badge_2,
1309 com.android.internal.R.string.managed_profile_label_badge_3
1310 };
1311
1312 private int getUserBadgeColor(UserHandle user) {
Sunny Goyalbab30752017-04-12 15:36:42 -07001313 return IconDrawableFactory.getUserBadgeColor(getUserManager(), user.getIdentifier());
Kenny Guy02c89902016-11-15 19:36:38 +00001314 }
1315
Svetoslavc7d62f02014-09-04 15:39:54 -07001316 @Override
1317 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Kenny Guy02c89902016-11-15 19:36:38 +00001318 Drawable badgeColor = getManagedProfileIconForDensity(user,
1319 com.android.internal.R.drawable.ic_corp_badge_color, density);
1320 if (badgeColor == null) {
1321 return null;
1322 }
1323 badgeColor.setTint(getUserBadgeColor(user));
1324 Drawable badgeForeground = getDrawableForDensity(
1325 com.android.internal.R.drawable.ic_corp_badge_case, density);
1326 Drawable badge = new LayerDrawable(
1327 new Drawable[] {badgeColor, badgeForeground });
1328 return badge;
Selim Cineke6ff9462016-01-15 15:07:06 -08001329 }
1330
1331 @Override
1332 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
Kenny Guy02c89902016-11-15 19:36:38 +00001333 Drawable badge = getManagedProfileIconForDensity(user,
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001334 com.android.internal.R.drawable.ic_corp_badge_no_background, density);
Kenny Guy02c89902016-11-15 19:36:38 +00001335 if (badge != null) {
1336 badge.setTint(getUserBadgeColor(user));
1337 }
1338 return badge;
Selim Cineke6ff9462016-01-15 15:07:06 -08001339 }
1340
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001341 private Drawable getDrawableForDensity(int drawableId, int density) {
1342 if (density <= 0) {
1343 density = mContext.getResources().getDisplayMetrics().densityDpi;
1344 }
1345 return Resources.getSystem().getDrawableForDensity(drawableId, density);
1346 }
1347
1348 private Drawable getManagedProfileIconForDensity(UserHandle user, int drawableId, int density) {
Tony Mak8673b282016-03-21 21:10:59 +00001349 if (isManagedProfile(user.getIdentifier())) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001350 return getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001351 }
1352 return null;
1353 }
1354
1355 @Override
1356 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
Tony Mak8673b282016-03-21 21:10:59 +00001357 if (isManagedProfile(user.getIdentifier())) {
Kenny Guy02c89902016-11-15 19:36:38 +00001358 int badge = getUserManager().getManagedProfileBadge(user.getIdentifier());
1359 int resourceId = CORP_BADGE_LABEL_RES_ID[badge % CORP_BADGE_LABEL_RES_ID.length];
1360 return Resources.getSystem().getString(resourceId, label);
Svetoslavc7d62f02014-09-04 15:39:54 -07001361 }
1362 return label;
1363 }
1364
Alan Viveretteecd585a2015-04-13 10:32:51 -07001365 @Override
1366 public Resources getResourcesForActivity(ComponentName activityName)
1367 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001368 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001369 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001370 }
1371
Alan Viveretteecd585a2015-04-13 10:32:51 -07001372 @Override
1373 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1374 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001375 if (app.packageName.equals("system")) {
Adam Lesinskia82b6262017-03-21 16:56:17 -07001376 return mContext.mMainThread.getSystemUiContext().getResources();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001377 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001378 final boolean sameUid = (app.uid == Process.myUid());
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001379 final Resources r = mContext.mMainThread.getTopLevelResources(
Adam Lesinskic82f28a2016-06-08 17:19:09 -07001380 sameUid ? app.sourceDir : app.publicSourceDir,
1381 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
1382 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
Jeff Hamilton7541ca4d2017-10-24 15:20:43 -04001383 mContext.mLoadedApk);
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001384 if (r != null) {
1385 return r;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001386 }
Adam Lesinski53fafdf2016-08-03 13:36:39 -07001387 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1388
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001389 }
1390
Alan Viveretteecd585a2015-04-13 10:32:51 -07001391 @Override
1392 public Resources getResourcesForApplication(String appPackageName)
1393 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001394 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001395 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001396 }
1397
Amith Yamasani98edc952012-09-25 14:09:27 -07001398 /** @hide */
1399 @Override
1400 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1401 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001402 if (userId < 0) {
1403 throw new IllegalArgumentException(
1404 "Call does not support special user #" + userId);
1405 }
1406 if ("system".equals(appPackageName)) {
Adam Lesinskia82b6262017-03-21 16:56:17 -07001407 return mContext.mMainThread.getSystemUiContext().getResources();
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001408 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001409 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001410 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001411 if (ai != null) {
1412 return getResourcesForApplication(ai);
1413 }
1414 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001415 throw e.rethrowFromSystemServer();
Amith Yamasani98edc952012-09-25 14:09:27 -07001416 }
1417 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1418 }
1419
Jeff Sharkeycd654482016-01-08 17:42:11 -07001420 volatile int mCachedSafeMode = -1;
1421
1422 @Override
1423 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001424 try {
1425 if (mCachedSafeMode < 0) {
1426 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1427 }
1428 return mCachedSafeMode != 0;
1429 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001430 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001431 }
1432 }
1433
Svetoslavf7c06eb2015-06-10 18:43:22 -07001434 @Override
1435 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1436 synchronized (mPermissionListeners) {
1437 if (mPermissionListeners.get(listener) != null) {
1438 return;
1439 }
1440 OnPermissionsChangeListenerDelegate delegate =
1441 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1442 try {
1443 mPM.addOnPermissionsChangeListener(delegate);
1444 mPermissionListeners.put(listener, delegate);
1445 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001446 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001447 }
1448 }
1449 }
1450
1451 @Override
1452 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1453 synchronized (mPermissionListeners) {
1454 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1455 if (delegate != null) {
1456 try {
1457 mPM.removeOnPermissionsChangeListener(delegate);
1458 mPermissionListeners.remove(listener);
1459 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001460 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001461 }
1462 }
1463 }
1464 }
1465
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001466 static void configurationChanged() {
1467 synchronized (sSync) {
1468 sIconCache.clear();
1469 sStringCache.clear();
1470 }
1471 }
1472
Yao Chen022b8ea2016-12-16 11:03:28 -08001473 protected ApplicationPackageManager(ContextImpl context,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001474 IPackageManager pm) {
1475 mContext = context;
1476 mPM = pm;
1477 }
1478
Alan Viveretteecd585a2015-04-13 10:32:51 -07001479 @Nullable
1480 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001481 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001482 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001483 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1484 + name + ": " + wr);
1485 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001486 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001487 if (state != null) {
1488 if (DEBUG_ICONS) {
1489 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1490 }
1491 // Note: It's okay here to not use the newDrawable(Resources) variant
1492 // of the API. The ConstantState comes from a drawable that was
1493 // originally created by passing the proper app Resources instance
1494 // which means the state should already contain the proper
1495 // resources specific information (like density.) See
1496 // BitmapDrawable.BitmapState for instance.
1497 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001498 }
1499 // our entry has been purged
1500 sIconCache.remove(name);
1501 }
1502 }
1503 return null;
1504 }
1505
Alan Viveretteecd585a2015-04-13 10:32:51 -07001506 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001507 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001508 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001509 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001510 }
1511 }
1512
Romain Guy39fe17c2011-11-30 10:34:07 -08001513 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001514 boolean immediateGc = false;
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001515 if (cmd == ApplicationThreadConstants.EXTERNAL_STORAGE_UNAVAILABLE) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001516 immediateGc = true;
1517 }
1518 if (pkgList != null && (pkgList.length > 0)) {
1519 boolean needCleanup = false;
1520 for (String ssp : pkgList) {
1521 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001522 for (int i=sIconCache.size()-1; i>=0; i--) {
1523 ResourceName nm = sIconCache.keyAt(i);
1524 if (nm.packageName.equals(ssp)) {
1525 //Log.i(TAG, "Removing cached drawable for " + nm);
1526 sIconCache.removeAt(i);
1527 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001528 }
1529 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001530 for (int i=sStringCache.size()-1; i>=0; i--) {
1531 ResourceName nm = sStringCache.keyAt(i);
1532 if (nm.packageName.equals(ssp)) {
1533 //Log.i(TAG, "Removing cached string for " + nm);
1534 sStringCache.removeAt(i);
1535 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001536 }
1537 }
1538 }
1539 }
1540 if (needCleanup || hasPkgInfo) {
1541 if (immediateGc) {
1542 // Schedule an immediate gc.
1543 Runtime.getRuntime().gc();
1544 } else {
1545 ActivityThread.currentActivityThread().scheduleGcIdler();
1546 }
1547 }
1548 }
1549 }
1550
1551 private static final class ResourceName {
1552 final String packageName;
1553 final int iconId;
1554
1555 ResourceName(String _packageName, int _iconId) {
1556 packageName = _packageName;
1557 iconId = _iconId;
1558 }
1559
1560 ResourceName(ApplicationInfo aInfo, int _iconId) {
1561 this(aInfo.packageName, _iconId);
1562 }
1563
1564 ResourceName(ComponentInfo cInfo, int _iconId) {
1565 this(cInfo.applicationInfo.packageName, _iconId);
1566 }
1567
1568 ResourceName(ResolveInfo rInfo, int _iconId) {
1569 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1570 }
1571
1572 @Override
1573 public boolean equals(Object o) {
1574 if (this == o) return true;
1575 if (o == null || getClass() != o.getClass()) return false;
1576
1577 ResourceName that = (ResourceName) o;
1578
1579 if (iconId != that.iconId) return false;
1580 return !(packageName != null ?
1581 !packageName.equals(that.packageName) : that.packageName != null);
1582
1583 }
1584
1585 @Override
1586 public int hashCode() {
1587 int result;
1588 result = packageName.hashCode();
1589 result = 31 * result + iconId;
1590 return result;
1591 }
1592
1593 @Override
1594 public String toString() {
1595 return "{ResourceName " + packageName + " / " + iconId + "}";
1596 }
1597 }
1598
1599 private CharSequence getCachedString(ResourceName name) {
1600 synchronized (sSync) {
1601 WeakReference<CharSequence> wr = sStringCache.get(name);
1602 if (wr != null) { // we have the activity
1603 CharSequence cs = wr.get();
1604 if (cs != null) {
1605 return cs;
1606 }
1607 // our entry has been purged
1608 sStringCache.remove(name);
1609 }
1610 }
1611 return null;
1612 }
1613
1614 private void putCachedString(ResourceName name, CharSequence cs) {
1615 synchronized (sSync) {
1616 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1617 }
1618 }
1619
1620 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001621 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001622 ApplicationInfo appInfo) {
1623 ResourceName name = new ResourceName(packageName, resid);
1624 CharSequence text = getCachedString(name);
1625 if (text != null) {
1626 return text;
1627 }
1628 if (appInfo == null) {
1629 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001630 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001631 } catch (NameNotFoundException e) {
1632 return null;
1633 }
1634 }
1635 try {
1636 Resources r = getResourcesForApplication(appInfo);
1637 text = r.getText(resid);
1638 putCachedString(name, text);
1639 return text;
1640 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001641 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001642 + appInfo.packageName);
1643 } catch (RuntimeException e) {
1644 // If an exception was thrown, fall through to return
1645 // default icon.
1646 Log.w("PackageManager", "Failure retrieving text 0x"
1647 + Integer.toHexString(resid) + " in package "
1648 + packageName, e);
1649 }
1650 return null;
1651 }
1652
1653 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001654 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001655 ApplicationInfo appInfo) {
1656 if (appInfo == null) {
1657 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001658 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001659 } catch (NameNotFoundException e) {
1660 return null;
1661 }
1662 }
1663 try {
1664 Resources r = getResourcesForApplication(appInfo);
1665 return r.getXml(resid);
1666 } catch (RuntimeException e) {
1667 // If an exception was thrown, fall through to return
1668 // default icon.
1669 Log.w("PackageManager", "Failure retrieving xml 0x"
1670 + Integer.toHexString(resid) + " in package "
1671 + packageName, e);
1672 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001673 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001674 + appInfo.packageName);
1675 }
1676 return null;
1677 }
1678
1679 @Override
1680 public CharSequence getApplicationLabel(ApplicationInfo info) {
1681 return info.loadLabel(this);
1682 }
1683
1684 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001685 public int installExistingPackage(String packageName) throws NameNotFoundException {
Sunny Goyala31a74b2017-05-11 15:59:19 -07001686 return installExistingPackage(packageName, PackageManager.INSTALL_REASON_UNKNOWN);
1687 }
1688
1689 @Override
1690 public int installExistingPackage(String packageName, int installReason)
1691 throws NameNotFoundException {
1692 return installExistingPackageAsUser(packageName, installReason, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001693 }
1694
1695 @Override
1696 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001697 throws NameNotFoundException {
Sunny Goyala31a74b2017-05-11 15:59:19 -07001698 return installExistingPackageAsUser(packageName, PackageManager.INSTALL_REASON_UNKNOWN,
1699 userId);
1700 }
1701
1702 private int installExistingPackageAsUser(String packageName, int installReason, int userId)
1703 throws NameNotFoundException {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001704 try {
Todd Kennedybe0b8892017-02-15 14:13:52 -08001705 int res = mPM.installExistingPackageAsUser(packageName, userId, 0 /*installFlags*/,
Sunny Goyala31a74b2017-05-11 15:59:19 -07001706 installReason);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001707 if (res == INSTALL_FAILED_INVALID_URI) {
1708 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1709 }
1710 return res;
1711 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001712 throw e.rethrowFromSystemServer();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001713 }
1714 }
1715
1716 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001717 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001718 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001719 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001720 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001721 throw e.rethrowFromSystemServer();
Kenny Root5ab21572011-07-27 11:11:19 -07001722 }
1723 }
1724
1725 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001726 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1727 long millisecondsToDelay) {
1728 try {
1729 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1730 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001731 throw e.rethrowFromSystemServer();
rich canningsd9ef3e52012-08-22 14:28:05 -07001732 }
1733 }
1734
1735 @Override
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001736 public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001737 try {
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001738 mPM.verifyIntentFilter(id, verificationCode, failedDomains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001739 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001740 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001741 }
1742 }
1743
1744 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001745 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001746 try {
1747 return mPM.getIntentVerificationStatus(packageName, userId);
1748 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001749 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001750 }
1751 }
1752
1753 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001754 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001755 try {
1756 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1757 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001758 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001759 }
1760 }
1761
1762 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001763 @SuppressWarnings("unchecked")
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001764 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1765 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001766 ParceledListSlice<IntentFilterVerificationInfo> parceledList =
1767 mPM.getIntentFilterVerifications(packageName);
1768 if (parceledList == null) {
1769 return Collections.emptyList();
1770 }
1771 return parceledList.getList();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001772 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001773 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001774 }
1775 }
1776
1777 @Override
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001778 @SuppressWarnings("unchecked")
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001779 public List<IntentFilter> getAllIntentFilters(String packageName) {
1780 try {
Svetoslav Ganovb2c2e452016-03-24 18:01:17 -07001781 ParceledListSlice<IntentFilter> parceledList =
1782 mPM.getAllIntentFilters(packageName);
1783 if (parceledList == null) {
1784 return Collections.emptyList();
1785 }
1786 return parceledList.getList();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001787 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001788 throw e.rethrowFromSystemServer();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001789 }
1790 }
1791
1792 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001793 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001794 try {
1795 return mPM.getDefaultBrowserPackageName(userId);
1796 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001797 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001798 }
1799 }
1800
1801 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001802 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001803 try {
1804 return mPM.setDefaultBrowserPackageName(packageName, userId);
1805 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001806 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001807 }
1808 }
1809
1810 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001811 public void setInstallerPackageName(String targetPackage,
1812 String installerPackageName) {
1813 try {
1814 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1815 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001816 throw e.rethrowFromSystemServer();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001817 }
1818 }
1819
1820 @Override
Todd Kennedyab532892017-03-08 14:19:49 -08001821 public void setUpdateAvailable(String packageName, boolean updateAvailable) {
1822 try {
1823 mPM.setUpdateAvailable(packageName, updateAvailable);
1824 } catch (RemoteException e) {
1825 throw e.rethrowFromSystemServer();
1826 }
1827 }
1828
1829 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001830 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001831 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001832 return mPM.getInstallerPackageName(packageName);
1833 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001834 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001835 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001836 }
1837
1838 @Override
1839 public int getMoveStatus(int moveId) {
1840 try {
1841 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001842 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001843 throw e.rethrowFromSystemServer();
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001844 }
1845 }
1846
1847 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001848 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1849 synchronized (mDelegates) {
1850 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1851 handler.getLooper());
1852 try {
1853 mPM.registerMoveCallback(delegate);
1854 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001855 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001856 }
1857 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001858 }
1859 }
1860
1861 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001862 public void unregisterMoveCallback(MoveCallback callback) {
1863 synchronized (mDelegates) {
1864 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1865 final MoveCallbackDelegate delegate = i.next();
1866 if (delegate.mCallback == callback) {
1867 try {
1868 mPM.unregisterMoveCallback(delegate);
1869 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001870 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001871 }
1872 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001873 }
1874 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001875 }
1876 }
1877
1878 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001879 public int movePackage(String packageName, VolumeInfo vol) {
1880 try {
1881 final String volumeUuid;
1882 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1883 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1884 } else if (vol.isPrimaryPhysical()) {
1885 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1886 } else {
1887 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1888 }
1889
1890 return mPM.movePackage(packageName, volumeUuid);
1891 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001892 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001893 }
1894 }
1895
1896 @Override
1897 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001898 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Yao Chen022b8ea2016-12-16 11:03:28 -08001899 return getPackageCurrentVolume(app, storage);
1900 }
1901
1902 @VisibleForTesting
1903 protected @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app,
1904 StorageManager storage) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001905 if (app.isInternal()) {
1906 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1907 } else if (app.isExternalAsec()) {
1908 return storage.getPrimaryPhysicalVolume();
1909 } else {
1910 return storage.findVolumeByUuid(app.volumeUuid);
1911 }
1912 }
1913
1914 @Override
1915 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
Yao Chen022b8ea2016-12-16 11:03:28 -08001916 final StorageManager storageManager = mContext.getSystemService(StorageManager.class);
1917 return getPackageCandidateVolumes(app, storageManager, mPM);
1918 }
1919
1920 @VisibleForTesting
1921 protected @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app,
1922 StorageManager storageManager, IPackageManager pm) {
1923 final VolumeInfo currentVol = getPackageCurrentVolume(app, storageManager);
1924 final List<VolumeInfo> vols = storageManager.getVolumes();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001925 final List<VolumeInfo> candidates = new ArrayList<>();
1926 for (VolumeInfo vol : vols) {
Yao Chen022b8ea2016-12-16 11:03:28 -08001927 if (Objects.equals(vol, currentVol)
1928 || isPackageCandidateVolume(mContext, app, vol, pm)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001929 candidates.add(vol);
1930 }
1931 }
1932 return candidates;
1933 }
1934
Yao Chen022b8ea2016-12-16 11:03:28 -08001935 @VisibleForTesting
1936 protected boolean isForceAllowOnExternal(Context context) {
1937 return Settings.Global.getInt(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001938 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Yao Chen022b8ea2016-12-16 11:03:28 -08001939 }
1940
1941 @VisibleForTesting
1942 protected boolean isAllow3rdPartyOnInternal(Context context) {
1943 return context.getResources().getBoolean(
1944 com.android.internal.R.bool.config_allow3rdPartyAppOnInternal);
1945 }
1946
1947 private boolean isPackageCandidateVolume(
1948 ContextImpl context, ApplicationInfo app, VolumeInfo vol, IPackageManager pm) {
1949 final boolean forceAllowOnExternal = isForceAllowOnExternal(context);
1950
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001951 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
Yao Chen022b8ea2016-12-16 11:03:28 -08001952 return app.isSystemApp() || isAllow3rdPartyOnInternal(context);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001953 }
1954
1955 // System apps and apps demanding internal storage can't be moved
1956 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001957 if (app.isSystemApp()) {
1958 return false;
1959 }
1960 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001961 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1962 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001963 return false;
1964 }
1965
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001966 // Gotta be able to write there
1967 if (!vol.isMountedWritable()) {
1968 return false;
1969 }
1970
1971 // Moving into an ASEC on public primary is only option internal
1972 if (vol.isPrimaryPhysical()) {
1973 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001974 }
1975
Makoto Onukif34db0a2016-02-17 11:17:15 -08001976 // Some apps can't be moved. (e.g. device admins)
1977 try {
Yao Chen022b8ea2016-12-16 11:03:28 -08001978 if (pm.isPackageDeviceAdminOnAnyUser(app.packageName)) {
Makoto Onukif34db0a2016-02-17 11:17:15 -08001979 return false;
1980 }
1981 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001982 throw e.rethrowFromSystemServer();
Makoto Onukif34db0a2016-02-17 11:17:15 -08001983 }
1984
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001985 // Otherwise we can move to any private volume
1986 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1987 }
1988
1989 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001990 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001991 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001992 final String volumeUuid;
1993 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1994 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1995 } else if (vol.isPrimaryPhysical()) {
1996 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1997 } else {
1998 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1999 }
2000
2001 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002002 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002003 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002004 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002005 }
2006
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002007 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002008 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
2009 final StorageManager storage = mContext.getSystemService(StorageManager.class);
2010 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07002011 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002012 }
2013
Jeff Sharkey275e3e42015-04-24 16:10:32 -07002014 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002015 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
2016 final StorageManager storage = mContext.getSystemService(StorageManager.class);
2017 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
2018 final List<VolumeInfo> vols = storage.getVolumes();
2019 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002020 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
2021 storage.getPrimaryStorageUuid()) && currentVol != null) {
2022 // TODO: support moving primary physical to emulated volume
2023 candidates.add(currentVol);
2024 } else {
2025 for (VolumeInfo vol : vols) {
2026 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
2027 candidates.add(vol);
2028 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002029 }
2030 }
2031 return candidates;
2032 }
2033
2034 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
2035 // Private internal is always an option
2036 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
2037 return true;
2038 }
2039
2040 // Gotta be able to write there
2041 if (!vol.isMountedWritable()) {
2042 return false;
2043 }
2044
Jeff Sharkeyfced5342015-05-10 14:53:34 -07002045 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002046 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002047 }
2048
2049 @Override
2050 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01002051 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01002052 }
2053
2054 @Override
Svet Ganov67882122016-12-11 16:36:34 -08002055 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer,
2056 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002057 try {
Svet Ganov67882122016-12-11 16:36:34 -08002058 mPM.deletePackageAsUser(packageName, PackageManager.VERSION_CODE_HIGHEST,
2059 observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002060 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002061 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002062 }
2063 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07002064
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002065 @Override
2066 public void clearApplicationUserData(String packageName,
2067 IPackageDataObserver observer) {
2068 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002069 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002070 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002071 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002072 }
2073 }
2074 @Override
2075 public void deleteApplicationCacheFiles(String packageName,
2076 IPackageDataObserver observer) {
2077 try {
2078 mPM.deleteApplicationCacheFiles(packageName, observer);
2079 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002080 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002081 }
2082 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002083
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002084 @Override
Suprabh Shukla78c9eb82016-04-12 15:51:35 -07002085 public void deleteApplicationCacheFilesAsUser(String packageName, int userId,
2086 IPackageDataObserver observer) {
2087 try {
2088 mPM.deleteApplicationCacheFilesAsUser(packageName, userId, observer);
2089 } catch (RemoteException e) {
2090 throw e.rethrowFromSystemServer();
2091 }
2092 }
2093
2094 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002095 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
2096 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002097 try {
Jeff Sharkeyddff8072017-05-26 13:10:46 -06002098 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, 0, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002099 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002100 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002101 }
2102 }
2103
2104 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07002105 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002106 try {
Jeff Sharkeyddff8072017-05-26 13:10:46 -06002107 mPM.freeStorage(volumeUuid, freeStorageSize, 0, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002108 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002109 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002110 }
2111 }
2112
2113 @Override
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00002114 public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended,
2115 int userId) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002116 try {
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00002117 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, userId);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002118 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002119 throw e.rethrowFromSystemServer();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002120 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00002121 }
2122
2123 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002124 public boolean isPackageSuspendedForUser(String packageName, int userId) {
2125 try {
2126 return mPM.isPackageSuspendedForUser(packageName, userId);
2127 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002128 throw e.rethrowFromSystemServer();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002129 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002130 }
2131
Jeff Sharkey9bc89af2017-01-11 11:25:50 -07002132 /** @hide */
2133 @Override
2134 public void setApplicationCategoryHint(String packageName, int categoryHint) {
2135 try {
2136 mPM.setApplicationCategoryHint(packageName, categoryHint,
2137 mContext.getOpPackageName());
2138 } catch (RemoteException e) {
2139 throw e.rethrowFromSystemServer();
2140 }
2141 }
2142
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00002143 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002144 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07002145 IPackageStatsObserver observer) {
Jeff Sharkey6f4b2a32017-03-21 14:13:41 -06002146 final String msg = "Shame on you for calling the hidden API "
2147 + "getPackageSizeInfoAsUser(). Shame!";
Jeff Sharkeye6306c42017-03-07 21:03:18 -07002148 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
Jeff Sharkey6f4b2a32017-03-21 14:13:41 -06002149 throw new UnsupportedOperationException(msg);
Jeff Sharkeye6306c42017-03-07 21:03:18 -07002150 } else if (observer != null) {
Jeff Sharkey6f4b2a32017-03-21 14:13:41 -06002151 Log.d(TAG, msg);
Jeff Sharkeye6306c42017-03-07 21:03:18 -07002152 try {
2153 observer.onGetStatsCompleted(null, false);
2154 } catch (RemoteException ignored) {
2155 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002156 }
2157 }
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002158
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002159 @Override
2160 public void addPackageToPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002161 Log.w(TAG, "addPackageToPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002162 }
2163
2164 @Override
2165 public void removePackageFromPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002166 Log.w(TAG, "removePackageFromPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002167 }
2168
2169 @Override
2170 public List<PackageInfo> getPreferredPackages(int flags) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002171 Log.w(TAG, "getPreferredPackages() is a no-op");
2172 return Collections.emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002173 }
2174
2175 @Override
2176 public void addPreferredActivity(IntentFilter filter,
2177 int match, ComponentName[] set, ComponentName activity) {
2178 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002179 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07002180 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002181 throw e.rethrowFromSystemServer();
Amith Yamasania3f133a2012-08-09 17:11:28 -07002182 }
2183 }
2184
2185 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07002186 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07002187 ComponentName[] set, ComponentName activity, int userId) {
2188 try {
2189 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002190 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002191 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002192 }
2193 }
2194
2195 @Override
2196 public void replacePreferredActivity(IntentFilter filter,
2197 int match, ComponentName[] set, ComponentName activity) {
2198 try {
Robin Lee0e27c872015-09-28 14:37:40 +01002199 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002200 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002201 throw e.rethrowFromSystemServer();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07002202 }
2203 }
2204
2205 @Override
2206 public void replacePreferredActivityAsUser(IntentFilter filter,
2207 int match, ComponentName[] set, ComponentName activity,
2208 int userId) {
2209 try {
2210 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002211 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002212 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002213 }
2214 }
2215
2216 @Override
2217 public void clearPackagePreferredActivities(String packageName) {
2218 try {
2219 mPM.clearPackagePreferredActivities(packageName);
2220 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002221 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002222 }
2223 }
2224
2225 @Override
2226 public int getPreferredActivities(List<IntentFilter> outFilters,
2227 List<ComponentName> outActivities, String packageName) {
2228 try {
2229 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
2230 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002231 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002232 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002233 }
2234
2235 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002236 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
2237 try {
2238 return mPM.getHomeActivities(outActivities);
2239 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002240 throw e.rethrowFromSystemServer();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002241 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07002242 }
2243
2244 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002245 public void setComponentEnabledSetting(ComponentName componentName,
2246 int newState, int flags) {
2247 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002248 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002249 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002250 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002251 }
2252 }
2253
2254 @Override
2255 public int getComponentEnabledSetting(ComponentName componentName) {
2256 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002257 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002258 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002259 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002260 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002261 }
2262
2263 @Override
2264 public void setApplicationEnabledSetting(String packageName,
2265 int newState, int flags) {
2266 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002267 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002268 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002269 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002270 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002271 }
2272 }
2273
2274 @Override
2275 public int getApplicationEnabledSetting(String packageName) {
2276 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002277 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002278 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002279 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002280 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002281 }
2282
Amith Yamasani655d0e22013-06-12 14:19:10 -07002283 @Override
Sudheer Shankabbb3ff22015-07-09 15:39:23 +01002284 public void flushPackageRestrictionsAsUser(int userId) {
2285 try {
2286 mPM.flushPackageRestrictionsAsUser(userId);
2287 } catch (RemoteException e) {
2288 throw e.rethrowFromSystemServer();
2289 }
2290 }
2291
2292 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002293 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002294 UserHandle user) {
2295 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002296 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002297 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002298 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002299 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002300 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002301 }
2302
2303 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002304 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002305 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002306 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002307 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002308 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002309 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002310 }
2311
dcashmanc6f22492014-08-14 09:54:51 -07002312 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002313 @Override
2314 public KeySet getKeySetByAlias(String packageName, String alias) {
2315 Preconditions.checkNotNull(packageName);
2316 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002317 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002318 return mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002319 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002320 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002321 }
dcashman9d2f4412014-06-09 09:27:54 -07002322 }
2323
dcashmanc6f22492014-08-14 09:54:51 -07002324 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002325 @Override
2326 public KeySet getSigningKeySet(String packageName) {
2327 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002328 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002329 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002330 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002331 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002332 }
dcashman9d2f4412014-06-09 09:27:54 -07002333 }
2334
dcashmanc6f22492014-08-14 09:54:51 -07002335 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002336 @Override
2337 public boolean isSignedBy(String packageName, KeySet ks) {
2338 Preconditions.checkNotNull(packageName);
2339 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002340 try {
dcashmanc6f22492014-08-14 09:54:51 -07002341 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002342 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002343 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002344 }
2345 }
2346
dcashmanc6f22492014-08-14 09:54:51 -07002347 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002348 @Override
2349 public boolean isSignedByExactly(String packageName, KeySet ks) {
2350 Preconditions.checkNotNull(packageName);
2351 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002352 try {
dcashmanc6f22492014-08-14 09:54:51 -07002353 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002354 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002355 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002356 }
2357 }
2358
Kenny Root0aaa0d92011-09-12 16:42:55 -07002359 /**
2360 * @hide
2361 */
2362 @Override
2363 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2364 try {
2365 return mPM.getVerifierDeviceIdentity();
2366 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002367 throw e.rethrowFromSystemServer();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002368 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002369 }
2370
Jeff Hao9f60c082014-10-28 18:51:07 -07002371 /**
2372 * @hide
2373 */
2374 @Override
2375 public boolean isUpgrade() {
2376 try {
2377 return mPM.isUpgrade();
2378 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002379 throw e.rethrowFromSystemServer();
Jeff Hao9f60c082014-10-28 18:51:07 -07002380 }
2381 }
2382
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002383 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002384 public PackageInstaller getPackageInstaller() {
2385 synchronized (mLock) {
2386 if (mInstaller == null) {
2387 try {
Svet Ganov67882122016-12-11 16:36:34 -08002388 mInstaller = new PackageInstaller(mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002389 mContext.getPackageName(), mContext.getUserId());
2390 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002391 throw e.rethrowFromSystemServer();
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002392 }
2393 }
2394 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002395 }
2396 }
2397
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002398 @Override
2399 public boolean isPackageAvailable(String packageName) {
2400 try {
2401 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2402 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002403 throw e.rethrowFromSystemServer();
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002404 }
2405 }
2406
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002407 /**
2408 * @hide
2409 */
2410 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002411 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2412 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002413 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002414 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002415 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002416 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002417 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002418 }
2419 }
2420
2421 /**
2422 * @hide
2423 */
2424 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002425 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002426 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002427 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002428 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002429 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002430 }
2431 }
2432
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002433 /**
2434 * @hide
2435 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002436 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002437 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2438 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2439 return dr;
2440 }
2441 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2442 }
2443
2444 /**
2445 * @hide
2446 */
2447 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002448 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002449 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2450 if (bitmap == null) {
Tony Mak213955e2017-11-23 16:57:08 +08002451 return UserIcons.getDefaultUserIcon(
2452 mContext.getResources(), itemInfo.showUserIcon, /* light= */ false);
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002453 }
2454 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002455 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002456 Drawable dr = null;
2457 if (itemInfo.packageName != null) {
2458 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2459 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002460 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002461 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002462 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002463 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002464 }
2465
2466 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2467 Rect badgeLocation, boolean tryBadgeInPlace) {
2468 final int badgedWidth = drawable.getIntrinsicWidth();
2469 final int badgedHeight = drawable.getIntrinsicHeight();
2470 final boolean canBadgeInPlace = tryBadgeInPlace
2471 && (drawable instanceof BitmapDrawable)
2472 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2473
2474 final Bitmap bitmap;
2475 if (canBadgeInPlace) {
2476 bitmap = ((BitmapDrawable) drawable).getBitmap();
2477 } else {
2478 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2479 }
2480 Canvas canvas = new Canvas(bitmap);
2481
2482 if (!canBadgeInPlace) {
2483 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2484 drawable.draw(canvas);
2485 }
2486
2487 if (badgeLocation != null) {
2488 if (badgeLocation.left < 0 || badgeLocation.top < 0
2489 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2490 throw new IllegalArgumentException("Badge location " + badgeLocation
2491 + " not in badged drawable bounds "
2492 + new Rect(0, 0, badgedWidth, badgedHeight));
2493 }
2494 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2495
2496 canvas.save();
2497 canvas.translate(badgeLocation.left, badgeLocation.top);
2498 badgeDrawable.draw(canvas);
2499 canvas.restore();
2500 } else {
2501 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2502 badgeDrawable.draw(canvas);
2503 }
2504
2505 if (!canBadgeInPlace) {
2506 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2507
2508 if (drawable instanceof BitmapDrawable) {
2509 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2510 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2511 }
2512
2513 return mergedDrawable;
2514 }
2515
2516 return drawable;
2517 }
2518
Tony Mak8673b282016-03-21 21:10:59 +00002519 private boolean isManagedProfile(int userId) {
2520 return getUserManager().isManagedProfile(userId);
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002521 }
2522
Bartosz Fabianowskia34f53f2017-01-11 18:08:47 +01002523 /**
2524 * @hide
2525 */
2526 @Override
2527 public int getInstallReason(String packageName, UserHandle user) {
2528 try {
2529 return mPM.getInstallReason(packageName, user.getIdentifier());
2530 } catch (RemoteException e) {
2531 throw e.rethrowFromSystemServer();
2532 }
2533 }
2534
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002535 /** {@hide} */
2536 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2537 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002538 private static final int MSG_CREATED = 1;
2539 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002540
2541 final MoveCallback mCallback;
2542 final Handler mHandler;
2543
2544 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2545 mCallback = callback;
2546 mHandler = new Handler(looper, this);
2547 }
2548
2549 @Override
2550 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002551 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002552 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002553 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002554 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002555 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002556 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002557 }
2558 case MSG_STATUS_CHANGED: {
2559 final SomeArgs args = (SomeArgs) msg.obj;
2560 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2561 args.recycle();
2562 return true;
2563 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002564 }
2565 return false;
2566 }
2567
2568 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002569 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002570 final SomeArgs args = SomeArgs.obtain();
2571 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002572 args.arg2 = extras;
2573 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2574 }
2575
2576 @Override
2577 public void onStatusChanged(int moveId, int status, long estMillis) {
2578 final SomeArgs args = SomeArgs.obtain();
2579 args.argi1 = moveId;
2580 args.argi2 = status;
2581 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002582 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002583 }
2584 }
2585
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002586 private final ContextImpl mContext;
2587 private final IPackageManager mPM;
2588
2589 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002590 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2591 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2592 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2593 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002594
2595 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2596 mPermissionListeners = new ArrayMap<>();
2597
2598 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2599 implements Handler.Callback{
2600 private static final int MSG_PERMISSIONS_CHANGED = 1;
2601
2602 private final OnPermissionsChangedListener mListener;
2603 private final Handler mHandler;
2604
2605
2606 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2607 Looper looper) {
2608 mListener = listener;
2609 mHandler = new Handler(looper, this);
2610 }
2611
2612 @Override
2613 public void onPermissionsChanged(int uid) {
2614 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2615 }
2616
2617 @Override
2618 public boolean handleMessage(Message msg) {
2619 switch (msg.what) {
2620 case MSG_PERMISSIONS_CHANGED: {
2621 final int uid = msg.arg1;
2622 mListener.onPermissionsChanged(uid);
2623 return true;
2624 }
2625 }
2626 return false;
2627 }
2628 }
Suprabh Shuklaaef25132017-01-23 18:09:03 -08002629
2630 @Override
2631 public boolean canRequestPackageInstalls() {
2632 try {
2633 return mPM.canRequestPackageInstalls(mContext.getPackageName(), mContext.getUserId());
2634 } catch (RemoteException e) {
2635 throw e.rethrowAsRuntimeException();
2636 }
2637 }
Chad Brubaker336ae5b2017-03-24 15:53:09 -07002638
2639 @Override
2640 public ComponentName getInstantAppResolverSettingsComponent() {
2641 try {
2642 return mPM.getInstantAppResolverSettingsComponent();
2643 } catch (RemoteException e) {
2644 throw e.rethrowAsRuntimeException();
2645 }
2646 }
Todd Kennedy801e6592017-04-12 14:10:12 -07002647
2648 @Override
2649 public ComponentName getInstantAppInstallerComponent() {
2650 try {
2651 return mPM.getInstantAppInstallerComponent();
2652 } catch (RemoteException e) {
2653 throw e.rethrowAsRuntimeException();
2654 }
2655 }
Chad Brubaker0d277a72017-04-12 16:56:53 -07002656
2657 @Override
2658 public String getInstantAppAndroidId(String packageName, UserHandle user) {
2659 try {
2660 return mPM.getInstantAppAndroidId(packageName, user.getIdentifier());
2661 } catch (RemoteException e) {
2662 throw e.rethrowAsRuntimeException();
2663 }
2664 }
Calin Juravle3d2af7f2017-04-19 19:56:21 -07002665
2666 private static class DexModuleRegisterResult {
2667 final String dexModulePath;
2668 final boolean success;
2669 final String message;
2670
2671 private DexModuleRegisterResult(String dexModulePath, boolean success, String message) {
2672 this.dexModulePath = dexModulePath;
2673 this.success = success;
2674 this.message = message;
2675 }
2676 }
2677
2678 private static class DexModuleRegisterCallbackDelegate
2679 extends android.content.pm.IDexModuleRegisterCallback.Stub
2680 implements Handler.Callback {
2681 private static final int MSG_DEX_MODULE_REGISTERED = 1;
2682 private final DexModuleRegisterCallback callback;
2683 private final Handler mHandler;
2684
2685 DexModuleRegisterCallbackDelegate(@NonNull DexModuleRegisterCallback callback) {
2686 this.callback = callback;
2687 mHandler = new Handler(Looper.getMainLooper(), this);
2688 }
2689
2690 @Override
2691 public void onDexModuleRegistered(@NonNull String dexModulePath, boolean success,
2692 @Nullable String message)throws RemoteException {
2693 mHandler.obtainMessage(MSG_DEX_MODULE_REGISTERED,
2694 new DexModuleRegisterResult(dexModulePath, success, message)).sendToTarget();
2695 }
2696
2697 @Override
2698 public boolean handleMessage(Message msg) {
2699 if (msg.what != MSG_DEX_MODULE_REGISTERED) {
2700 return false;
2701 }
2702 DexModuleRegisterResult result = (DexModuleRegisterResult)msg.obj;
2703 callback.onDexModuleRegistered(result.dexModulePath, result.success, result.message);
2704 return true;
2705 }
2706 }
2707
2708 @Override
2709 public void registerDexModule(@NonNull String dexModule,
2710 @Nullable DexModuleRegisterCallback callback) {
2711 // Check if this is a shared module by looking if the others can read it.
2712 boolean isSharedModule = false;
2713 try {
2714 StructStat stat = Os.stat(dexModule);
2715 if ((OsConstants.S_IROTH & stat.st_mode) != 0) {
2716 isSharedModule = true;
2717 }
2718 } catch (ErrnoException e) {
2719 callback.onDexModuleRegistered(dexModule, false,
2720 "Could not get stat the module file: " + e.getMessage());
2721 return;
2722 }
2723
2724 // Module path is ok.
2725 // Create the callback delegate to be passed to package manager service.
2726 DexModuleRegisterCallbackDelegate callbackDelegate = null;
2727 if (callback != null) {
2728 callbackDelegate = new DexModuleRegisterCallbackDelegate(callback);
2729 }
2730
2731 // Invoke the package manager service.
2732 try {
2733 mPM.registerDexModule(mContext.getPackageName(), dexModule,
2734 isSharedModule, callbackDelegate);
2735 } catch (RemoteException e) {
2736 throw e.rethrowAsRuntimeException();
2737 }
2738 }
Calin Juravle45f8b292017-11-07 18:49:43 -08002739
2740 @Override
Ben Gruver1ab3d6e2017-12-07 13:45:08 -08002741 public CharSequence getHarmfulAppWarning(String packageName) {
2742 try {
2743 return mPM.getHarmfulAppWarning(packageName, mContext.getUserId());
2744 } catch (RemoteException e) {
2745 throw e.rethrowAsRuntimeException();
2746 }
2747 }
2748
2749 @Override
2750 public void setHarmfulAppWarning(String packageName, CharSequence warning) {
2751 try {
2752 mPM.setHarmfulAppWarning(packageName, warning, mContext.getUserId());
2753 } catch (RemoteException e) {
2754 throw e.rethrowAsRuntimeException();
2755 }
2756 }
2757
2758 @Override
Calin Juravle45f8b292017-11-07 18:49:43 -08002759 public ArtManager getArtManager() {
2760 synchronized (mLock) {
2761 if (mArtManager == null) {
2762 try {
2763 mArtManager = new ArtManager(mPM.getArtManager());
2764 } catch (RemoteException e) {
2765 throw e.rethrowFromSystemServer();
2766 }
2767 }
2768 return mArtManager;
2769 }
2770 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002771}