Skip to content

Commit bb4ca52

Browse files
author
Dianne Hackborn
committed
Fix issue #7649590: Background windows sometimes not being hidden for secondary users
There are two things going on here: (1) In secondary users, some times theme information such as whether the window is full screen opaque was not being retrieved, so the window manager didn't know that it could hide the windows behind the app. This would just be a performance problem, except that: (2) There appear to be a number of applications that declare that they are full screen opaque, when in fact they are not. Instead they are using window surfaces with an alpha channel, and setting some pixels in their window to a non-opaque alpha level. This will allow you to see whatever is behind the app. If the system happens to completely remove the windows behind the app, and somebody is filling the frame buffer with black, then you will see what the app intends -- those parts of its UI blended with black. If one of those cases doesn't hold (and though we have never guaranteed they would, in practice this is generally what happens), then you will see something else. At any rate, if nothing else than for performance reasons, we need to fix issue #1. It turns out what is happening here is that the AttributeCache used by the activity manager and window manager to retreive theme and other information about applications has not yet been updated for multi-user. One of the things we retrieve from this is the theme information telling the window manager whether an application's window should be treated as full screen opaque, allowing it to hide any windows behind it. In the current implementation, the AttributeCache always retrieves this information about the application as the primary user (user 0). So, if you have an application that is installed on a secondary user but not installed on the primary user, when the AttributeCache tries to retrieve the requested information for it, then from the perspective of the primary user it considers the application not installed, and is not able to retrieve that info. The change here makes AttributeCache multi-user aware, keeping all of its data separately per-user, and requiring that callers now provide the user they want to retrieve information for. Activity manager and window manager are updated to be able to pass in the user when needed. This required some fiddling of the window manager to have that information available -- in particular it needs to be associated with the AppWindowToken. Change-Id: I4b50b4b3a41bab9d4689e61f3584778e451343c8
1 parent f874880 commit bb4ca52

File tree

10 files changed

+70
-45
lines changed

10 files changed

+70
-45
lines changed

core/java/android/view/IWindowManager.aidl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ interface IWindowManager
7474
void setEventDispatching(boolean enabled);
7575
void addWindowToken(IBinder token, int type);
7676
void removeWindowToken(IBinder token);
77-
void addAppToken(int addPos, IApplicationToken token,
77+
void addAppToken(int addPos, int userId, IApplicationToken token,
7878
int groupId, int requestedOrientation, boolean fullscreen, boolean showWhenLocked);
7979
void setAppGroupId(IBinder token, int groupId);
8080
void setAppOrientation(IApplicationToken token, int requestedOrientation);

services/java/com/android/server/AttributeCache.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.content.res.Configuration;
2424
import android.content.res.Resources;
2525
import android.content.res.TypedArray;
26+
import android.os.UserHandle;
2627
import android.util.SparseArray;
2728

2829
import java.util.HashMap;
@@ -34,52 +35,54 @@
3435
*/
3536
public final class AttributeCache {
3637
private static AttributeCache sInstance = null;
37-
38+
3839
private final Context mContext;
39-
private final WeakHashMap<String, Package> mPackages =
40-
new WeakHashMap<String, Package>();
40+
private final SparseArray<WeakHashMap<String, Package>> mPackages =
41+
new SparseArray<WeakHashMap<String, Package>>();
4142
private final Configuration mConfiguration = new Configuration();
42-
43+
4344
public final static class Package {
4445
public final Context context;
4546
private final SparseArray<HashMap<int[], Entry>> mMap
4647
= new SparseArray<HashMap<int[], Entry>>();
47-
48+
4849
public Package(Context c) {
4950
context = c;
5051
}
5152
}
52-
53+
5354
public final static class Entry {
5455
public final Context context;
5556
public final TypedArray array;
56-
57+
5758
public Entry(Context c, TypedArray ta) {
5859
context = c;
5960
array = ta;
6061
}
6162
}
62-
63+
6364
public static void init(Context context) {
6465
if (sInstance == null) {
6566
sInstance = new AttributeCache(context);
6667
}
6768
}
68-
69+
6970
public static AttributeCache instance() {
7071
return sInstance;
7172
}
72-
73+
7374
public AttributeCache(Context context) {
7475
mContext = context;
7576
}
76-
77+
7778
public void removePackage(String packageName) {
7879
synchronized (this) {
79-
mPackages.remove(packageName);
80+
for (int i=0; i<mPackages.size(); i++) {
81+
mPackages.valueAt(i).remove(packageName);
82+
}
8083
}
8184
}
82-
85+
8386
public void updateConfiguration(Configuration config) {
8487
synchronized (this) {
8588
int changes = mConfiguration.updateFrom(config);
@@ -93,10 +96,21 @@ public void updateConfiguration(Configuration config) {
9396
}
9497
}
9598
}
96-
97-
public Entry get(String packageName, int resId, int[] styleable) {
99+
100+
public void removeUser(int userId) {
101+
synchronized (this) {
102+
mPackages.remove(userId);
103+
}
104+
}
105+
106+
public Entry get(int userId, String packageName, int resId, int[] styleable) {
98107
synchronized (this) {
99-
Package pkg = mPackages.get(packageName);
108+
WeakHashMap<String, Package> packages = mPackages.get(userId);
109+
if (packages == null) {
110+
packages = new WeakHashMap<String, Package>();
111+
mPackages.put(userId, packages);
112+
}
113+
Package pkg = packages.get(packageName);
100114
HashMap<int[], Entry> map = null;
101115
Entry ent = null;
102116
if (pkg != null) {
@@ -110,15 +124,16 @@ public Entry get(String packageName, int resId, int[] styleable) {
110124
} else {
111125
Context context;
112126
try {
113-
context = mContext.createPackageContext(packageName, 0);
127+
context = mContext.createPackageContextAsUser(packageName, 0,
128+
new UserHandle(userId));
114129
if (context == null) {
115130
return null;
116131
}
117132
} catch (PackageManager.NameNotFoundException e) {
118133
return null;
119134
}
120135
pkg = new Package(context);
121-
mPackages.put(packageName, pkg);
136+
packages.put(packageName, pkg);
122137
}
123138

124139
if (map == null) {

services/java/com/android/server/am/ActivityManagerService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14551,6 +14551,10 @@ void finishUserStop(UserStartedState uss) {
1455114551
// Clean up all state and processes associated with the user.
1455214552
// Kill all the processes for the user.
1455314553
forceStopUserLocked(userId);
14554+
AttributeCache ac = AttributeCache.instance();
14555+
if (ac != null) {
14556+
ac.removeUser(userId);
14557+
}
1455414558
}
1455514559
}
1455614560

services/java/com/android/server/am/ActivityRecord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static ActivityRecord forToken(IBinder token) {
407407
packageName = aInfo.applicationInfo.packageName;
408408
launchMode = aInfo.launchMode;
409409

410-
AttributeCache.Entry ent = AttributeCache.instance().get(packageName,
410+
AttributeCache.Entry ent = AttributeCache.instance().get(userId, packageName,
411411
realTheme, com.android.internal.R.styleable.Window);
412412
fullscreen = ent != null && !ent.array.getBoolean(
413413
com.android.internal.R.styleable.Window_windowIsFloating, false)

services/java/com/android/server/am/ActivityStack.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,8 +1810,8 @@ private final void startActivityLocked(ActivityRecord r, boolean newTask,
18101810
}
18111811
mHistory.add(addPos, r);
18121812
r.putInHistory();
1813-
mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId,
1814-
r.info.screenOrientation, r.fullscreen,
1813+
mService.mWindowManager.addAppToken(addPos, r.userId, r.appToken,
1814+
r.task.taskId, r.info.screenOrientation, r.fullscreen,
18151815
(r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
18161816
if (VALIDATE_TOKENS) {
18171817
validateAppTokensLocked();
@@ -1875,8 +1875,8 @@ private final void startActivityLocked(ActivityRecord r, boolean newTask,
18751875
mNoAnimActivities.remove(r);
18761876
}
18771877
r.updateOptionsLocked(options);
1878-
mService.mWindowManager.addAppToken(
1879-
addPos, r.appToken, r.task.taskId, r.info.screenOrientation, r.fullscreen,
1878+
mService.mWindowManager.addAppToken(addPos, r.userId, r.appToken,
1879+
r.task.taskId, r.info.screenOrientation, r.fullscreen,
18801880
(r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
18811881
boolean doShow = true;
18821882
if (newTask) {
@@ -1914,8 +1914,8 @@ private final void startActivityLocked(ActivityRecord r, boolean newTask,
19141914
} else {
19151915
// If this is the first activity, don't do any fancy animations,
19161916
// because there is nothing for it to animate on top of.
1917-
mService.mWindowManager.addAppToken(addPos, r.appToken, r.task.taskId,
1918-
r.info.screenOrientation, r.fullscreen,
1917+
mService.mWindowManager.addAppToken(addPos, r.userId, r.appToken,
1918+
r.task.taskId, r.info.screenOrientation, r.fullscreen,
19191919
(r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0);
19201920
ActivityOptions.abort(options);
19211921
}

services/java/com/android/server/wm/AppWindowToken.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
* really activity) that is displaying windows.
3838
*/
3939
class AppWindowToken extends WindowToken {
40+
// The user who owns this app window token.
41+
final int userId;
4042
// Non-null only for application tokens.
4143
final IApplicationToken appToken;
4244

@@ -98,9 +100,10 @@ class AppWindowToken extends WindowToken {
98100
// Input application handle used by the input dispatcher.
99101
final InputApplicationHandle mInputApplicationHandle;
100102

101-
AppWindowToken(WindowManagerService _service, IApplicationToken _token) {
103+
AppWindowToken(WindowManagerService _service, int _userId, IApplicationToken _token) {
102104
super(_service, _token.asBinder(),
103105
WindowManager.LayoutParams.TYPE_APPLICATION, true);
106+
userId = _userId;
104107
appWindowToken = this;
105108
appToken = _token;
106109
mInputApplicationHandle = new InputApplicationHandle(this);
@@ -225,7 +228,8 @@ WindowState findMainWindow() {
225228
void dump(PrintWriter pw, String prefix) {
226229
super.dump(pw, prefix);
227230
if (appToken != null) {
228-
pw.print(prefix); pw.println("app=true");
231+
pw.print(prefix); pw.print("app=true");
232+
pw.print(" userId="); pw.println(userId);
229233
}
230234
if (allAppWindows.size() > 0) {
231235
pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);

services/java/com/android/server/wm/WindowManagerService.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,7 +3200,7 @@ private WindowInfo getWindowInfoForWindowStateLocked(WindowState window) {
32003200
return info;
32013201
}
32023202

3203-
private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp) {
3203+
private AttributeCache.Entry getCachedAnimations(int userId, WindowManager.LayoutParams lp) {
32043204
if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: layout params pkg="
32053205
+ (lp != null ? lp.packageName : null)
32063206
+ " resId=0x" + (lp != null ? Integer.toHexString(lp.windowAnimations) : null));
@@ -3215,13 +3215,13 @@ private AttributeCache.Entry getCachedAnimations(WindowManager.LayoutParams lp)
32153215
}
32163216
if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
32173217
+ packageName);
3218-
return AttributeCache.instance().get(packageName, resId,
3218+
return AttributeCache.instance().get(userId, packageName, resId,
32193219
com.android.internal.R.styleable.WindowAnimation);
32203220
}
32213221
return null;
32223222
}
32233223

3224-
private AttributeCache.Entry getCachedAnimations(String packageName, int resId) {
3224+
private AttributeCache.Entry getCachedAnimations(int userId, String packageName, int resId) {
32253225
if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: package="
32263226
+ packageName + " resId=0x" + Integer.toHexString(resId));
32273227
if (packageName != null) {
@@ -3230,17 +3230,17 @@ private AttributeCache.Entry getCachedAnimations(String packageName, int resId)
32303230
}
32313231
if (DEBUG_ANIM) Slog.v(TAG, "Loading animations: picked package="
32323232
+ packageName);
3233-
return AttributeCache.instance().get(packageName, resId,
3233+
return AttributeCache.instance().get(userId, packageName, resId,
32343234
com.android.internal.R.styleable.WindowAnimation);
32353235
}
32363236
return null;
32373237
}
32383238

3239-
Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
3239+
Animation loadAnimation(int userId, WindowManager.LayoutParams lp, int animAttr) {
32403240
int anim = 0;
32413241
Context context = mContext;
32423242
if (animAttr >= 0) {
3243-
AttributeCache.Entry ent = getCachedAnimations(lp);
3243+
AttributeCache.Entry ent = getCachedAnimations(userId, lp);
32443244
if (ent != null) {
32453245
context = ent.context;
32463246
anim = ent.array.getResourceId(animAttr, 0);
@@ -3252,11 +3252,11 @@ Animation loadAnimation(WindowManager.LayoutParams lp, int animAttr) {
32523252
return null;
32533253
}
32543254

3255-
private Animation loadAnimation(String packageName, int resId) {
3255+
private Animation loadAnimation(int userId, String packageName, int resId) {
32563256
int anim = 0;
32573257
Context context = mContext;
32583258
if (resId >= 0) {
3259-
AttributeCache.Entry ent = getCachedAnimations(packageName, resId);
3259+
AttributeCache.Entry ent = getCachedAnimations(userId, packageName, resId);
32603260
if (ent != null) {
32613261
context = ent.context;
32623262
anim = resId;
@@ -3484,7 +3484,7 @@ private boolean applyAnimationLocked(AppWindowToken atoken,
34843484
Animation a;
34853485
boolean initialized = false;
34863486
if (mNextAppTransitionType == ActivityOptions.ANIM_CUSTOM) {
3487-
a = loadAnimation(mNextAppTransitionPackage, enter ?
3487+
a = loadAnimation(atoken.userId, mNextAppTransitionPackage, enter ?
34883488
mNextAppTransitionEnter : mNextAppTransitionExit);
34893489
if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.v(TAG,
34903490
"applyAnimation: atoken=" + atoken
@@ -3565,7 +3565,7 @@ private boolean applyAnimationLocked(AppWindowToken atoken,
35653565
: com.android.internal.R.styleable.WindowAnimation_wallpaperIntraCloseExitAnimation;
35663566
break;
35673567
}
3568-
a = animAttr != 0 ? loadAnimation(lp, animAttr) : null;
3568+
a = animAttr != 0 ? loadAnimation(atoken.userId, lp, animAttr) : null;
35693569
if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.v(TAG,
35703570
"applyAnimation: atoken=" + atoken
35713571
+ " anim=" + a
@@ -3752,7 +3752,7 @@ private void addAppTokenToAnimating(final int addPos, final AppWindowToken atoke
37523752
}
37533753

37543754
@Override
3755-
public void addAppToken(int addPos, IApplicationToken token,
3755+
public void addAppToken(int addPos, int userId, IApplicationToken token,
37563756
int groupId, int requestedOrientation, boolean fullscreen, boolean showWhenLocked) {
37573757
if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
37583758
"addAppToken()")) {
@@ -3779,7 +3779,7 @@ public void addAppToken(int addPos, IApplicationToken token,
37793779
Slog.w(TAG, "Attempted to add existing app token: " + token);
37803780
return;
37813781
}
3782-
atoken = new AppWindowToken(this, token);
3782+
atoken = new AppWindowToken(this, userId, token);
37833783
atoken.inputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
37843784
atoken.groupId = groupId;
37853785
atoken.appFullscreen = fullscreen;
@@ -4396,8 +4396,8 @@ public void setAppStartingWindow(IBinder token, String pkg,
43964396
if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Checking theme of starting window: 0x"
43974397
+ Integer.toHexString(theme));
43984398
if (theme != 0) {
4399-
AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
4400-
com.android.internal.R.styleable.Window);
4399+
AttributeCache.Entry ent = AttributeCache.instance().get(wtoken.userId,
4400+
pkg, theme, com.android.internal.R.styleable.Window);
44014401
if (ent == null) {
44024402
// Whoops! App doesn't exist. Um. Okay. We'll just
44034403
// pretend like we didn't see that.

services/java/com/android/server/wm/WindowStateAnimator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.graphics.Rect;
1515
import android.graphics.Region;
1616
import android.os.Debug;
17+
import android.os.UserHandle;
1718
import android.util.Slog;
1819
import android.view.DisplayInfo;
1920
import android.view.Surface;
@@ -1533,7 +1534,8 @@ boolean applyAnimationLocked(int transit, boolean isEntrance) {
15331534
break;
15341535
}
15351536
if (attr >= 0) {
1536-
a = mService.loadAnimation(mWin.mAttrs, attr);
1537+
a = mService.loadAnimation(UserHandle.getUserId(mWin.mOwnerUid),
1538+
mWin.mAttrs, attr);
15371539
}
15381540
}
15391541
if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,

tests/permission/src/com/android/framework/permission/tests/WindowManagerPermissionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testMANAGE_APP_TOKENS() {
9393
}
9494

9595
try {
96-
mWm.addAppToken(0, null, 0, 0, false, false);
96+
mWm.addAppToken(0, 0, null, 0, 0, false, false);
9797
fail("IWindowManager.addAppToken did not throw SecurityException as"
9898
+ " expected");
9999
} catch (SecurityException e) {

tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public boolean hasSystemNavBar() throws RemoteException {
8787
// ---- unused implementation of IWindowManager ----
8888

8989
@Override
90-
public void addAppToken(int arg0, IApplicationToken arg1, int arg2, int arg3, boolean arg4,
90+
public void addAppToken(int arg0, int arg1p5, IApplicationToken arg1, int arg2, int arg3, boolean arg4,
9191
boolean arg5)
9292
throws RemoteException {
9393
// TODO Auto-generated method stub

0 commit comments

Comments
 (0)