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
This commit is contained in:
Felka Chang
2020-09-08 20:58:50 +08:00
parent faf7e4a05b
commit 3e1776c67c
3 changed files with 15 additions and 8 deletions

View File

@@ -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
*/

View File

@@ -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);

View File

@@ -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) {