Fix strictMode violation in Accesibility Service

From Android R, using the WindowManager from non-ui context
violates the vm policy. It may cause incorrect window bounds
with the given floating window. AccessibilityService provides
Accesibility overlay usage that needs to set window token to
the windowManager. However, Setting it when the service is bounded
violates the policy.
To fixt it, we set the window token when developers get WindowManager
first time.

Bug: 175785781
Test: atest AccessibilityOverlayTest, use the test apk to check if
the warning message is shown in the log
      manual test to see if SelectToSpeak works well
Change-Id: Ia873488626aa4da111499282d1971f836cf111cd

Change-Id: Ia87df60c393bb0e40d85ced4184c78a09d7b427b
This commit is contained in:
ryanlwlin
2021-06-24 15:09:44 +08:00
parent 5c990f822f
commit bce17f97bc

View File

@@ -2074,6 +2074,10 @@ public abstract class AccessibilityService extends Service {
if (WINDOW_SERVICE.equals(name)) {
if (mWindowManager == null) {
mWindowManager = (WindowManager) getBaseContext().getSystemService(name);
final WindowManagerImpl wm = (WindowManagerImpl) mWindowManager;
// Set e default token obtained from the connection to ensure client could use
// accessibility overlay.
wm.setDefaultToken(mWindowToken);
}
return mWindowManager;
}
@@ -2182,8 +2186,10 @@ public abstract class AccessibilityService extends Service {
// The client may have already obtained the window manager, so
// update the default token on whatever manager we gave them.
final WindowManagerImpl wm = (WindowManagerImpl) getSystemService(WINDOW_SERVICE);
wm.setDefaultToken(windowToken);
if (mWindowManager != null) {
final WindowManagerImpl wm = (WindowManagerImpl) mWindowManager;
wm.setDefaultToken(mWindowToken);
}
}
@Override