From 3e1776c67c5d3af35cfd553ce2c760d04aa0ab33 Mon Sep 17 00:00:00 2001 From: Felka Chang Date: Tue, 8 Sep 2020 20:58:50 +0800 Subject: [PATCH] Remove PackageManager.getResourcesForApplicationAsUser API To switch another user to do something should create the context of the specified user by using Context.createContextAsUser rather than call *AsUser API directly. Replacing PackageManager.getResourcesForApplicationAsUser with context.createContextAsUser(...) applys in not only ShortcutService but also the related ShortcutManager test. Test: TEST_NAME="FrameworksServicesTests"; \ PACKAGE_NAME="com.android.server.pm.ShortcutManagerTest"; \ MODULE_LIST="CtsShortcutHostTestCases CtsShortcutManagerTestCases"; \ for i in `seq 1 11`; \ do\ MODULE_LIST="${MODULE_LIST} ${TEST_NAME}:${PACKAGE_NAME}${i}"; \ done; \ atest $MODULE_LIST Test: make -j droid cts gts vts Test: make -j docs Test: atest SystemUITests \ FrameworksServicesTests:com.android.server.pm Bug: 170928809 Change-Id: I5a4acce42b0d71e405412f84aaa73c0dfbb610e4 --- core/java/android/content/pm/ShortcutInfo.java | 6 +++--- .../java/com/android/server/pm/ShortcutService.java | 7 ++++--- .../com/android/server/pm/BaseShortcutManagerTest.java | 10 ++++++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/core/java/android/content/pm/ShortcutInfo.java b/core/java/android/content/pm/ShortcutInfo.java index 1b3c46f908519..7ed803f782604 100644 --- a/core/java/android/content/pm/ShortcutInfo.java +++ b/core/java/android/content/pm/ShortcutInfo.java @@ -630,7 +630,7 @@ public final class ShortcutInfo implements Parcelable { * This will set {@link #FLAG_STRINGS_RESOLVED}. * * @param res {@link Resources} for the publisher. Must have been loaded with - * {@link PackageManager#getResourcesForApplicationAsUser}. + * {@link PackageManager#getResourcesForApplication(String)}. * * @hide */ @@ -752,7 +752,7 @@ public final class ShortcutInfo implements Parcelable { * aforementioned method would do internally, but not documented, so doing here explicitly.) * * @param res {@link Resources} for the publisher. Must have been loaded with - * {@link PackageManager#getResourcesForApplicationAsUser}. + * {@link PackageManager#getResourcesForApplication(String)}. * * @hide */ @@ -782,7 +782,7 @@ public final class ShortcutInfo implements Parcelable { * in the resource name fields. * * @param res {@link Resources} for the publisher. Must have been loaded with - * {@link PackageManager#getResourcesForApplicationAsUser}. + * {@link PackageManager#getResourcesForApplication(String)}. * * @hide */ diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java index 96f9982a16a52..e471ac6976790 100644 --- a/services/core/java/com/android/server/pm/ShortcutService.java +++ b/services/core/java/com/android/server/pm/ShortcutService.java @@ -3907,10 +3907,11 @@ public class ShortcutService extends IShortcutService.Stub { final long start = getStatStartTime(); final long token = injectClearCallingIdentity(); try { - return mContext.getPackageManager().getResourcesForApplicationAsUser( - packageName, userId); + return mContext.createContextAsUser(UserHandle.of(userId), /* flags */ 0) + .getPackageManager().getResourcesForApplication(packageName); } catch (NameNotFoundException e) { - Slog.e(TAG, "Resources for package " + packageName + " not found"); + Slog.e(TAG, "Resources of package " + packageName + " for user " + userId + + " not found"); return null; } finally { injectRestoreCallingIdentity(token); diff --git a/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java index e4acdfe93fd4f..f78c01a8f2816 100644 --- a/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java @@ -176,6 +176,12 @@ public abstract class BaseShortcutManagerTest extends InstrumentationTestCase { return null; } + @Override + public Context createContextAsUser(UserHandle user, int flags) { + when(mMockPackageManager.getUserId()).thenReturn(user.getIdentifier()); + return this; + } + @Override public void unregisterReceiver(BroadcastReceiver receiver) { // ignore. @@ -939,7 +945,7 @@ public abstract class BaseShortcutManagerTest extends InstrumentationTestCase { assertEquals(Process.SYSTEM_UID, mInjectedCallingUid); final String packageName = (String) pmInvocation.getArguments()[0]; - final int userId = (Integer) pmInvocation.getArguments()[1]; + final int userId = mMockPackageManager.getUserId(); final Resources res = mock(Resources.class); @@ -971,7 +977,7 @@ public abstract class BaseShortcutManagerTest extends InstrumentationTestCase { return Integer.parseInt(entryName.substring(1)) + ressIdOffset; }).when(res).getIdentifier(anyStringOrNull(), anyStringOrNull(), anyStringOrNull()); return res; - }).when(mMockPackageManager).getResourcesForApplicationAsUser(anyString(), anyInt()); + }).when(mMockPackageManager).getResourcesForApplication(anyString()); } protected static UserInfo withProfileGroupId(UserInfo in, int groupId) {