Skip to content

Commit ded653b

Browse files
committed
Fix "system" resources; more context user work.
Special-case "system" resources, since it doesn't have an ApplicationInfo. Also switch Context and PackageManager to using the wrapped UserHandle for outgoing calls. Bug: 7242361 Change-Id: I1ecd2a4752399a6dce2de97c59bd5545996c9a99
1 parent 5e08af0 commit ded653b

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-35
lines changed

core/java/android/app/ApplicationPackageManager.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import android.net.Uri;
5151
import android.os.Process;
5252
import android.os.RemoteException;
53-
import android.os.UserHandle;
5453
import android.util.Log;
5554
import android.view.Display;
5655

@@ -70,7 +69,7 @@ final class ApplicationPackageManager extends PackageManager {
7069
public PackageInfo getPackageInfo(String packageName, int flags)
7170
throws NameNotFoundException {
7271
try {
73-
PackageInfo pi = mPM.getPackageInfo(packageName, flags, UserHandle.myUserId());
72+
PackageInfo pi = mPM.getPackageInfo(packageName, flags, mContext.getUserId());
7473
if (pi != null) {
7574
return pi;
7675
}
@@ -200,7 +199,7 @@ public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
200199
public ApplicationInfo getApplicationInfo(String packageName, int flags)
201200
throws NameNotFoundException {
202201
try {
203-
ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, UserHandle.myUserId());
202+
ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, mContext.getUserId());
204203
if (ai != null) {
205204
return ai;
206205
}
@@ -215,7 +214,7 @@ public ApplicationInfo getApplicationInfo(String packageName, int flags)
215214
public ActivityInfo getActivityInfo(ComponentName className, int flags)
216215
throws NameNotFoundException {
217216
try {
218-
ActivityInfo ai = mPM.getActivityInfo(className, flags, UserHandle.myUserId());
217+
ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
219218
if (ai != null) {
220219
return ai;
221220
}
@@ -230,7 +229,7 @@ public ActivityInfo getActivityInfo(ComponentName className, int flags)
230229
public ActivityInfo getReceiverInfo(ComponentName className, int flags)
231230
throws NameNotFoundException {
232231
try {
233-
ActivityInfo ai = mPM.getReceiverInfo(className, flags, UserHandle.myUserId());
232+
ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
234233
if (ai != null) {
235234
return ai;
236235
}
@@ -245,7 +244,7 @@ public ActivityInfo getReceiverInfo(ComponentName className, int flags)
245244
public ServiceInfo getServiceInfo(ComponentName className, int flags)
246245
throws NameNotFoundException {
247246
try {
248-
ServiceInfo si = mPM.getServiceInfo(className, flags, UserHandle.myUserId());
247+
ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
249248
if (si != null) {
250249
return si;
251250
}
@@ -260,7 +259,7 @@ public ServiceInfo getServiceInfo(ComponentName className, int flags)
260259
public ProviderInfo getProviderInfo(ComponentName className, int flags)
261260
throws NameNotFoundException {
262261
try {
263-
ProviderInfo pi = mPM.getProviderInfo(className, flags, UserHandle.myUserId());
262+
ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
264263
if (pi != null) {
265264
return pi;
266265
}
@@ -405,7 +404,7 @@ public int getUidForSharedUser(String sharedUserName)
405404
@SuppressWarnings("unchecked")
406405
@Override
407406
public List<PackageInfo> getInstalledPackages(int flags) {
408-
return getInstalledPackages(flags, UserHandle.myUserId());
407+
return getInstalledPackages(flags, mContext.getUserId());
409408
}
410409

411410
/** @hide */
@@ -431,7 +430,7 @@ public List<PackageInfo> getInstalledPackages(int flags, int userId) {
431430
@SuppressWarnings("unchecked")
432431
@Override
433432
public List<ApplicationInfo> getInstalledApplications(int flags) {
434-
int userId = UserHandle.getUserId(Process.myUid());
433+
final int userId = mContext.getUserId();
435434
try {
436435
final List<ApplicationInfo> applicationInfos = new ArrayList<ApplicationInfo>();
437436
ApplicationInfo lastItem = null;
@@ -451,7 +450,7 @@ public List<ApplicationInfo> getInstalledApplications(int flags) {
451450

452451
@Override
453452
public ResolveInfo resolveActivity(Intent intent, int flags) {
454-
return resolveActivityAsUser(intent, flags, UserHandle.myUserId());
453+
return resolveActivityAsUser(intent, flags, mContext.getUserId());
455454
}
456455

457456
@Override
@@ -470,7 +469,7 @@ public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
470469
@Override
471470
public List<ResolveInfo> queryIntentActivities(Intent intent,
472471
int flags) {
473-
return queryIntentActivitiesAsUser(intent, flags, UserHandle.myUserId());
472+
return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
474473
}
475474

476475
/** @hide Same as above but for a specific user */
@@ -514,7 +513,7 @@ public List<ResolveInfo> queryIntentActivityOptions(
514513
try {
515514
return mPM.queryIntentActivityOptions(caller, specifics,
516515
specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
517-
flags, UserHandle.myUserId());
516+
flags, mContext.getUserId());
518517
} catch (RemoteException e) {
519518
throw new RuntimeException("Package manager has died", e);
520519
}
@@ -538,7 +537,7 @@ public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags, int u
538537

539538
@Override
540539
public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
541-
return queryBroadcastReceivers(intent, flags, UserHandle.myUserId());
540+
return queryBroadcastReceivers(intent, flags, mContext.getUserId());
542541
}
543542

544543
@Override
@@ -548,7 +547,7 @@ public ResolveInfo resolveService(Intent intent, int flags) {
548547
intent,
549548
intent.resolveTypeIfNeeded(mContext.getContentResolver()),
550549
flags,
551-
UserHandle.myUserId());
550+
mContext.getUserId());
552551
} catch (RemoteException e) {
553552
throw new RuntimeException("Package manager has died", e);
554553
}
@@ -569,14 +568,14 @@ public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int
569568

570569
@Override
571570
public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
572-
return queryIntentServicesAsUser(intent, flags, UserHandle.myUserId());
571+
return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
573572
}
574573

575574
@Override
576575
public ProviderInfo resolveContentProvider(String name,
577576
int flags) {
578577
try {
579-
return mPM.resolveContentProvider(name, flags, UserHandle.myUserId());
578+
return mPM.resolveContentProvider(name, flags, mContext.getUserId());
580579
} catch (RemoteException e) {
581580
throw new RuntimeException("Package manager has died", e);
582581
}
@@ -763,6 +762,13 @@ public Drawable getApplicationLogo(String packageName)
763762
@Override
764763
public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
765764
throws NameNotFoundException {
765+
if (userId < 0) {
766+
throw new IllegalArgumentException(
767+
"Call does not support special user #" + userId);
768+
}
769+
if ("system".equals(appPackageName)) {
770+
return mContext.mMainThread.getSystemContext().getResources();
771+
}
766772
try {
767773
ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, 0, userId);
768774
if (ai != null) {
@@ -1118,7 +1124,7 @@ public void deletePackage(String packageName, IPackageDeleteObserver observer, i
11181124
public void clearApplicationUserData(String packageName,
11191125
IPackageDataObserver observer) {
11201126
try {
1121-
mPM.clearApplicationUserData(packageName, observer, UserHandle.myUserId());
1127+
mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
11221128
} catch (RemoteException e) {
11231129
// Should never happen!
11241130
}
@@ -1191,7 +1197,7 @@ public List<PackageInfo> getPreferredPackages(int flags) {
11911197
public void addPreferredActivity(IntentFilter filter,
11921198
int match, ComponentName[] set, ComponentName activity) {
11931199
try {
1194-
mPM.addPreferredActivity(filter, match, set, activity, UserHandle.myUserId());
1200+
mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
11951201
} catch (RemoteException e) {
11961202
// Should never happen!
11971203
}
@@ -1241,7 +1247,7 @@ public int getPreferredActivities(List<IntentFilter> outFilters,
12411247
public void setComponentEnabledSetting(ComponentName componentName,
12421248
int newState, int flags) {
12431249
try {
1244-
mPM.setComponentEnabledSetting(componentName, newState, flags, UserHandle.myUserId());
1250+
mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
12451251
} catch (RemoteException e) {
12461252
// Should never happen!
12471253
}
@@ -1250,7 +1256,7 @@ public void setComponentEnabledSetting(ComponentName componentName,
12501256
@Override
12511257
public int getComponentEnabledSetting(ComponentName componentName) {
12521258
try {
1253-
return mPM.getComponentEnabledSetting(componentName, UserHandle.myUserId());
1259+
return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
12541260
} catch (RemoteException e) {
12551261
// Should never happen!
12561262
}
@@ -1261,7 +1267,7 @@ public int getComponentEnabledSetting(ComponentName componentName) {
12611267
public void setApplicationEnabledSetting(String packageName,
12621268
int newState, int flags) {
12631269
try {
1264-
mPM.setApplicationEnabledSetting(packageName, newState, flags, UserHandle.myUserId());
1270+
mPM.setApplicationEnabledSetting(packageName, newState, flags, mContext.getUserId());
12651271
} catch (RemoteException e) {
12661272
// Should never happen!
12671273
}
@@ -1270,7 +1276,7 @@ public void setApplicationEnabledSetting(String packageName,
12701276
@Override
12711277
public int getApplicationEnabledSetting(String packageName) {
12721278
try {
1273-
return mPM.getApplicationEnabledSetting(packageName, UserHandle.myUserId());
1279+
return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
12741280
} catch (RemoteException e) {
12751281
// Should never happen!
12761282
}

core/java/android/app/ContextImpl.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ public void sendBroadcast(Intent intent) {
10291029
ActivityManagerNative.getDefault().broadcastIntent(
10301030
mMainThread.getApplicationThread(), intent, resolvedType, null,
10311031
Activity.RESULT_OK, null, null, null, false, false,
1032-
UserHandle.myUserId());
1032+
getUserId());
10331033
} catch (RemoteException e) {
10341034
}
10351035
}
@@ -1042,7 +1042,7 @@ public void sendBroadcast(Intent intent, String receiverPermission) {
10421042
ActivityManagerNative.getDefault().broadcastIntent(
10431043
mMainThread.getApplicationThread(), intent, resolvedType, null,
10441044
Activity.RESULT_OK, null, null, receiverPermission, false, false,
1045-
UserHandle.myUserId());
1045+
getUserId());
10461046
} catch (RemoteException e) {
10471047
}
10481048
}
@@ -1056,7 +1056,7 @@ public void sendOrderedBroadcast(Intent intent,
10561056
ActivityManagerNative.getDefault().broadcastIntent(
10571057
mMainThread.getApplicationThread(), intent, resolvedType, null,
10581058
Activity.RESULT_OK, null, null, receiverPermission, true, false,
1059-
UserHandle.myUserId());
1059+
getUserId());
10601060
} catch (RemoteException e) {
10611061
}
10621062
}
@@ -1089,7 +1089,7 @@ resultReceiver, getOuterContext(), scheduler,
10891089
ActivityManagerNative.getDefault().broadcastIntent(
10901090
mMainThread.getApplicationThread(), intent, resolvedType, rd,
10911091
initialCode, initialData, initialExtras, receiverPermission,
1092-
true, false, UserHandle.myUserId());
1092+
true, false, getUserId());
10931093
} catch (RemoteException e) {
10941094
}
10951095
}
@@ -1160,7 +1160,7 @@ public void sendStickyBroadcast(Intent intent) {
11601160
ActivityManagerNative.getDefault().broadcastIntent(
11611161
mMainThread.getApplicationThread(), intent, resolvedType, null,
11621162
Activity.RESULT_OK, null, null, null, false, true,
1163-
UserHandle.myUserId());
1163+
getUserId());
11641164
} catch (RemoteException e) {
11651165
}
11661166
}
@@ -1193,7 +1193,7 @@ resultReceiver, getOuterContext(), scheduler,
11931193
ActivityManagerNative.getDefault().broadcastIntent(
11941194
mMainThread.getApplicationThread(), intent, resolvedType, rd,
11951195
initialCode, initialData, initialExtras, null,
1196-
true, true, UserHandle.myUserId());
1196+
true, true, getUserId());
11971197
} catch (RemoteException e) {
11981198
}
11991199
}
@@ -1208,7 +1208,7 @@ public void removeStickyBroadcast(Intent intent) {
12081208
try {
12091209
intent.setAllowFds(false);
12101210
ActivityManagerNative.getDefault().unbroadcastIntent(
1211-
mMainThread.getApplicationThread(), intent, UserHandle.myUserId());
1211+
mMainThread.getApplicationThread(), intent, getUserId());
12121212
} catch (RemoteException e) {
12131213
}
12141214
}
@@ -1281,7 +1281,7 @@ public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
12811281
@Override
12821282
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
12831283
String broadcastPermission, Handler scheduler) {
1284-
return registerReceiverInternal(receiver, UserHandle.myUserId(),
1284+
return registerReceiverInternal(receiver, getUserId(),
12851285
filter, broadcastPermission, scheduler, getOuterContext());
12861286
}
12871287

@@ -1337,12 +1337,12 @@ public void unregisterReceiver(BroadcastReceiver receiver) {
13371337

13381338
@Override
13391339
public ComponentName startService(Intent service) {
1340-
return startServiceAsUser(service, Process.myUserHandle());
1340+
return startServiceAsUser(service, mUser);
13411341
}
13421342

13431343
@Override
13441344
public boolean stopService(Intent service) {
1345-
return stopServiceAsUser(service, Process.myUserHandle());
1345+
return stopServiceAsUser(service, mUser);
13461346
}
13471347

13481348
@Override
@@ -1446,7 +1446,7 @@ public boolean startInstrumentation(ComponentName className,
14461446
arguments.setAllowFds(false);
14471447
}
14481448
return ActivityManagerNative.getDefault().startInstrumentation(
1449-
className, profileFile, 0, arguments, null, UserHandle.myUserId());
1449+
className, profileFile, 0, arguments, null, getUserId());
14501450
} catch (RemoteException e) {
14511451
// System has crashed, nothing we can do.
14521452
}
@@ -1792,6 +1792,11 @@ public File getDir(String name, int mode) {
17921792
return file;
17931793
}
17941794

1795+
/** {@hide} */
1796+
public int getUserId() {
1797+
return mUser.getIdentifier();
1798+
}
1799+
17951800
static ContextImpl createSystemContext(ActivityThread mainThread) {
17961801
final ContextImpl context = new ContextImpl();
17971802
context.init(Resources.getSystem(), mainThread, Process.myUserHandle());

packages/SystemUI/src/com/android/systemui/statusbar/StatusBarIconView.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.graphics.Canvas;
2525
import android.graphics.Paint;
2626
import android.graphics.Rect;
27+
import android.os.UserHandle;
2728
import android.text.TextUtils;
2829
import android.util.AttributeSet;
2930
import android.util.Slog;
@@ -165,8 +166,12 @@ public static Drawable getIcon(Context context, StatusBarIcon icon) {
165166

166167
if (icon.iconPackage != null) {
167168
try {
168-
r = context.getPackageManager().getResourcesForApplicationAsUser(icon.iconPackage,
169-
icon.user.getIdentifier());
169+
int userId = icon.user.getIdentifier();
170+
if (userId == UserHandle.USER_ALL) {
171+
userId = UserHandle.USER_OWNER;
172+
}
173+
r = context.getPackageManager()
174+
.getResourcesForApplicationAsUser(icon.iconPackage, userId);
170175
} catch (PackageManager.NameNotFoundException ex) {
171176
Slog.e(TAG, "Icon package not found: " + icon.iconPackage);
172177
return null;

0 commit comments

Comments
 (0)