From 8fcba0353e201a268789a01077b62c418649c792 Mon Sep 17 00:00:00 2001 From: Rhed Jao Date: Wed, 15 Apr 2020 23:06:08 +0800 Subject: [PATCH] Fixes long press the a11y shortcut no response An a11y service targeting sdk version > Q and requesting a11y button should be turned on by the Settings when it's assigned to the a11y shortcut by the user. An a11y button callback is sent to the a11y service when the user long press the a11y shortcut. This issue happened when an disabled a11y service is assigned to the a11y shortcut, and upgraded to the target sdk version > Q. Framework fails to send the callback and no response when user long press the shortcut. Turns on the a11y service and show up a toast to fix this issue. Bug: 153517972 Test: atest AccessibilityShortcutControllerTest Change-Id: Ie4fc9bbeaf08ba674cfe6382eab214b2a15c004c --- .../AccessibilityShortcutController.java | 14 ++++++++------ .../AccessibilityShortcutControllerTest.java | 6 ++++++ .../accessibility/AccessibilityManagerService.java | 13 ++++++++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/core/java/com/android/internal/accessibility/AccessibilityShortcutController.java b/core/java/com/android/internal/accessibility/AccessibilityShortcutController.java index d64b5f1118dc1..be66d0c238cc6 100644 --- a/core/java/com/android/internal/accessibility/AccessibilityShortcutController.java +++ b/core/java/com/android/internal/accessibility/AccessibilityShortcutController.java @@ -232,9 +232,8 @@ public class AccessibilityShortcutController { } /** - * Show toast if current assigned shortcut target is an accessibility service and its target - * sdk version is less than or equal to Q, or greater than Q and does not request - * accessibility button. + * Show toast to alert the user that the accessibility shortcut turned on or off an + * accessibility service. */ private void showToast() { final AccessibilityServiceInfo serviceInfo = getInfoForTargetService(); @@ -247,12 +246,15 @@ public class AccessibilityShortcutController { } final boolean requestA11yButton = (serviceInfo.flags & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0; - if (serviceInfo.getResolveInfo().serviceInfo.applicationInfo - .targetSdkVersion > Build.VERSION_CODES.Q && requestA11yButton) { + final boolean isServiceEnabled = isServiceEnabled(serviceInfo); + if (serviceInfo.getResolveInfo().serviceInfo.applicationInfo.targetSdkVersion + > Build.VERSION_CODES.Q && requestA11yButton && isServiceEnabled) { + // An accessibility button callback is sent to the target accessibility service. + // No need to show up a toast in this case. return; } // For accessibility services, show a toast explaining what we're doing. - String toastMessageFormatString = mContext.getString(isServiceEnabled(serviceInfo) + String toastMessageFormatString = mContext.getString(isServiceEnabled ? R.string.accessibility_shortcut_disabling_service : R.string.accessibility_shortcut_enabling_service); String toastMessage = String.format(toastMessageFormatString, serviceName); diff --git a/core/tests/coretests/src/com/android/internal/accessibility/AccessibilityShortcutControllerTest.java b/core/tests/coretests/src/com/android/internal/accessibility/AccessibilityShortcutControllerTest.java index 4a33da6805857..b21504c73772d 100644 --- a/core/tests/coretests/src/com/android/internal/accessibility/AccessibilityShortcutControllerTest.java +++ b/core/tests/coretests/src/com/android/internal/accessibility/AccessibilityShortcutControllerTest.java @@ -462,6 +462,7 @@ public class AccessibilityShortcutControllerTest { configureValidShortcutService(); configureApplicationTargetSdkVersion(Build.VERSION_CODES.R); configureRequestAccessibilityButton(); + configureEnabledService(); Settings.Secure.putInt(mContentResolver, ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, 1); getController().performAccessibilityShortcut(); @@ -610,6 +611,11 @@ public class AccessibilityShortcutControllerTest { }).when(mHandler).sendMessageAtTime(any(), anyLong()); } + private void configureEnabledService() throws Exception { + when(mAccessibilityManagerService.getEnabledAccessibilityServiceList(anyInt(), anyInt())) + .thenReturn(Collections.singletonList(mServiceInfo)); + } + private AccessibilityShortcutController getController() { AccessibilityShortcutController accessibilityShortcutController = new AccessibilityShortcutController(mContext, mHandler, 0); diff --git a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java index 7230b00f87ad0..d252f9e69a22c 100644 --- a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java +++ b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java @@ -2441,7 +2441,11 @@ public class AccessibilityManagerService extends IAccessibilityManager.Stub * accessibility button. * 2) For {@link AccessibilityManager#ACCESSIBILITY_SHORTCUT_KEY} type and service targeting sdk * version <= Q: turns on / off the accessibility service. - * 3) For services targeting sdk version > Q: + * 3) For {@link AccessibilityManager#ACCESSIBILITY_SHORTCUT_KEY} type and service targeting sdk + * version > Q and request accessibility button: turn on the accessibility service if it's + * not in the enabled state. + * (It'll happen when a service is disabled and assigned to shortcut then upgraded.) + * 4) For services targeting sdk version > Q: * a) Turns on / off the accessibility service, if service does not request accessibility * button. * b) Callbacks to accessibility service if service is bounded and requests accessibility @@ -2475,6 +2479,13 @@ public class AccessibilityManagerService extends IAccessibilityManager.Stub } return true; } + if (shortcutType == ACCESSIBILITY_SHORTCUT_KEY && targetSdk > Build.VERSION_CODES.Q + && requestA11yButton) { + if (!userState.getEnabledServicesLocked().contains(assignedTarget)) { + enableAccessibilityServiceLocked(assignedTarget, mCurrentUserId); + return true; + } + } // Callbacks to a11y service if it's bounded and requests a11y button. if (serviceConnection == null || !userState.mBoundServices.contains(serviceConnection)