Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3e1776c6 authored by Felka Chang's avatar Felka Chang
Browse files

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
parent faf7e4a0
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -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
     */
+4 −3
Original line number Diff line number Diff line
@@ -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);
+8 −2
Original line number Diff line number Diff line
@@ -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) {