From bce17f97bc8aab63d637ecfe54aef9fcfb757fe6 Mon Sep 17 00:00:00 2001 From: ryanlwlin Date: Thu, 24 Jun 2021 15:09:44 +0800 Subject: [PATCH] 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 --- .../accessibilityservice/AccessibilityService.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/java/android/accessibilityservice/AccessibilityService.java b/core/java/android/accessibilityservice/AccessibilityService.java index 9372c293c32d0..806283229d988 100644 --- a/core/java/android/accessibilityservice/AccessibilityService.java +++ b/core/java/android/accessibilityservice/AccessibilityService.java @@ -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