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.

To change Icon not to use PackageManager.getResourcesForApplicationAsUser
lets StatusBarIconViewTest no need to mock
getResourcesForApplicationAsUser in mock PackageManager instance.

Test: make -j droid cts gts vts
Test: make -j docs
Test: atest SystemUITests \
    FrameworksServicesTests:com.android.server.pm \
    CtsGraphicsTestCases:android.graphics.drawable.cts.IconTest \
    FrameworksCoreTests:android.graphics.drawable.IconTest
Bug: 170928809
Change-Id: I4a2ec54b6e137a9c9efcfba0589c08ea8cac86ce
This commit is contained in:
Felka Chang
2020-09-08 20:58:50 +08:00
parent faf7e4a05b
commit f922ae3179
2 changed files with 4 additions and 4 deletions

View File

@@ -39,6 +39,7 @@ import android.os.Handler;
import android.os.Message;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Log;
@@ -441,10 +442,11 @@ public final class Icon implements Parcelable {
resPackage = context.getPackageName();
}
if (getResources() == null && !(getResPackage().equals("android"))) {
final PackageManager pm = context.getPackageManager();
final PackageManager pm = context.createContextAsUser(
UserHandle.of(userId), /* flags */ 0).getPackageManager();
try {
// assign getResources() as the correct user
mObj1 = pm.getResourcesForApplicationAsUser(resPackage, userId);
mObj1 = pm.getResourcesForApplication(resPackage);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, String.format("Unable to find pkg=%s user=%d",
getResPackage(),

View File

@@ -73,8 +73,6 @@ public class StatusBarIconViewTest extends SysuiTestCase {
// Set up context such that asking for "mockPackage" resources returns mMockResources.
mMockResources = mock(Resources.class);
mPackageManagerSpy = spy(getContext().getPackageManager());
doReturn(mMockResources).when(mPackageManagerSpy)
.getResourcesForApplicationAsUser(eq("mockPackage"), anyInt());
doReturn(mMockResources).when(mPackageManagerSpy)
.getResourcesForApplication(eq("mockPackage"));
doReturn(mMockResources).when(mPackageManagerSpy).getResourcesForApplication(argThat(