Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 1 | /* |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 2 | * Copyright 2017 Google Inc. |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 17 | package com.android.internal.accessibility; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 18 | |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 19 | import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG; |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 20 | import static android.view.accessibility.AccessibilityManager.ACCESSIBILITY_SHORTCUT_KEY; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 21 | |
| 22 | import static com.android.internal.util.ArrayUtils.convertToLongArray; |
| 23 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 24 | import android.accessibilityservice.AccessibilityServiceInfo; |
menghanli | e83358b | 2020-02-11 16:47:18 +0800 | [diff] [blame] | 25 | import android.annotation.IntDef; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 26 | import android.app.ActivityManager; |
Phil Weaver | 8c58d45 | 2017-07-28 12:58:15 -0700 | [diff] [blame] | 27 | import android.app.ActivityThread; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 28 | import android.app.AlertDialog; |
| 29 | import android.content.ComponentName; |
| 30 | import android.content.ContentResolver; |
| 31 | import android.content.Context; |
| 32 | import android.content.DialogInterface; |
Zhen Sun | 62e54c2 | 2017-08-17 15:56:20 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageManager; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 34 | import android.database.ContentObserver; |
| 35 | import android.media.AudioAttributes; |
| 36 | import android.media.Ringtone; |
| 37 | import android.media.RingtoneManager; |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 38 | import android.net.Uri; |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 39 | import android.os.Build; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 40 | import android.os.Handler; |
| 41 | import android.os.UserHandle; |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 42 | import android.os.Vibrator; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 43 | import android.provider.Settings; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 44 | import android.speech.tts.TextToSpeech; |
| 45 | import android.speech.tts.Voice; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 46 | import android.text.TextUtils; |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 47 | import android.util.ArrayMap; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 48 | import android.util.Slog; |
| 49 | import android.view.Window; |
| 50 | import android.view.WindowManager; |
| 51 | import android.view.accessibility.AccessibilityManager; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 52 | import android.widget.Toast; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 53 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 54 | import com.android.internal.R; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 55 | import com.android.internal.util.function.pooled.PooledLambda; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 56 | |
menghanli | e83358b | 2020-02-11 16:47:18 +0800 | [diff] [blame] | 57 | import java.lang.annotation.Retention; |
| 58 | import java.lang.annotation.RetentionPolicy; |
Jeff Sharkey | 8b0cff7 | 2020-03-09 15:49:01 -0600 | [diff] [blame] | 59 | import java.util.Collection; |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 60 | import java.util.Collections; |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 61 | import java.util.List; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 62 | import java.util.Locale; |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 63 | import java.util.Map; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 64 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 65 | /** |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 66 | * Class to help manage the accessibility shortcut key |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 67 | */ |
| 68 | public class AccessibilityShortcutController { |
| 69 | private static final String TAG = "AccessibilityShortcutController"; |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 70 | |
| 71 | // Dummy component names for framework features |
| 72 | public static final ComponentName COLOR_INVERSION_COMPONENT_NAME = |
| 73 | new ComponentName("com.android.server.accessibility", "ColorInversion"); |
| 74 | public static final ComponentName DALTONIZER_COMPONENT_NAME = |
| 75 | new ComponentName("com.android.server.accessibility", "Daltonizer"); |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 76 | public static final String MAGNIFICATION_CONTROLLER_NAME = |
| 77 | "com.android.server.accessibility.MagnificationController"; |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 78 | |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 79 | private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder() |
| 80 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 81 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY) |
| 82 | .build(); |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 83 | private static Map<ComponentName, ToggleableFrameworkFeatureInfo> sFrameworkShortcutFeaturesMap; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 84 | |
| 85 | private final Context mContext; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 86 | private final Handler mHandler; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 87 | private AlertDialog mAlertDialog; |
| 88 | private boolean mIsShortcutEnabled; |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 89 | private boolean mEnabledOnLockScreen; |
| 90 | private int mUserId; |
| 91 | |
menghanli | e83358b | 2020-02-11 16:47:18 +0800 | [diff] [blame] | 92 | @Retention(RetentionPolicy.SOURCE) |
| 93 | @IntDef({ |
| 94 | DialogStaus.NOT_SHOWN, |
| 95 | DialogStaus.SHOWN, |
| 96 | }) |
| 97 | /** Denotes the user shortcut type. */ |
| 98 | private @interface DialogStaus { |
| 99 | int NOT_SHOWN = 0; |
| 100 | int SHOWN = 1; |
| 101 | } |
| 102 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 103 | // Visible for testing |
| 104 | public FrameworkObjectProvider mFrameworkObjectProvider = new FrameworkObjectProvider(); |
| 105 | |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 106 | /** |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 107 | * @return An immutable map from dummy component names to feature info for toggling a framework |
| 108 | * feature |
| 109 | */ |
| 110 | public static Map<ComponentName, ToggleableFrameworkFeatureInfo> |
| 111 | getFrameworkShortcutFeaturesMap() { |
| 112 | if (sFrameworkShortcutFeaturesMap == null) { |
| 113 | Map<ComponentName, ToggleableFrameworkFeatureInfo> featuresMap = new ArrayMap<>(2); |
| 114 | featuresMap.put(COLOR_INVERSION_COMPONENT_NAME, |
| 115 | new ToggleableFrameworkFeatureInfo( |
| 116 | Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, |
| 117 | "1" /* Value to enable */, "0" /* Value to disable */, |
| 118 | R.string.color_inversion_feature_name)); |
| 119 | featuresMap.put(DALTONIZER_COMPONENT_NAME, |
| 120 | new ToggleableFrameworkFeatureInfo( |
| 121 | Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, |
| 122 | "1" /* Value to enable */, "0" /* Value to disable */, |
| 123 | R.string.color_correction_feature_name)); |
| 124 | sFrameworkShortcutFeaturesMap = Collections.unmodifiableMap(featuresMap); |
| 125 | } |
| 126 | return sFrameworkShortcutFeaturesMap; |
| 127 | } |
| 128 | |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 129 | public AccessibilityShortcutController(Context context, Handler handler, int initialUserId) { |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 130 | mContext = context; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 131 | mHandler = handler; |
Phil Weaver | f287180 | 2017-04-19 11:40:32 -0700 | [diff] [blame] | 132 | mUserId = initialUserId; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 133 | |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 134 | // Keep track of state of shortcut settings |
| 135 | final ContentObserver co = new ContentObserver(handler) { |
| 136 | @Override |
Jeff Sharkey | 8b0cff7 | 2020-03-09 15:49:01 -0600 | [diff] [blame] | 137 | public void onChange(boolean selfChange, Collection<Uri> uris, int flags, int userId) { |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 138 | if (userId == mUserId) { |
| 139 | onSettingsChanged(); |
| 140 | } |
| 141 | } |
| 142 | }; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 143 | mContext.getContentResolver().registerContentObserver( |
| 144 | Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE), |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 145 | false, co, UserHandle.USER_ALL); |
| 146 | mContext.getContentResolver().registerContentObserver( |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 147 | Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN), |
| 148 | false, co, UserHandle.USER_ALL); |
Phil Weaver | c5865d6 | 2018-03-02 16:00:43 -0800 | [diff] [blame] | 149 | mContext.getContentResolver().registerContentObserver( |
| 150 | Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN), |
| 151 | false, co, UserHandle.USER_ALL); |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 152 | setCurrentUser(mUserId); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 155 | public void setCurrentUser(int currentUserId) { |
| 156 | mUserId = currentUserId; |
| 157 | onSettingsChanged(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Check if the shortcut is available. |
| 162 | * |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 163 | * @param phoneLocked Whether or not the phone is currently locked. |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 164 | * |
| 165 | * @return {@code true} if the shortcut is available |
| 166 | */ |
| 167 | public boolean isAccessibilityShortcutAvailable(boolean phoneLocked) { |
| 168 | return mIsShortcutEnabled && (!phoneLocked || mEnabledOnLockScreen); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | public void onSettingsChanged() { |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 172 | final boolean hasShortcutTarget = hasShortcutTarget(); |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 173 | final ContentResolver cr = mContext.getContentResolver(); |
Phil Weaver | c5865d6 | 2018-03-02 16:00:43 -0800 | [diff] [blame] | 174 | // Enable the shortcut from the lockscreen by default if the dialog has been shown |
| 175 | final int dialogAlreadyShown = Settings.Secure.getIntForUser( |
menghanli | e83358b | 2020-02-11 16:47:18 +0800 | [diff] [blame] | 176 | cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, DialogStaus.NOT_SHOWN, |
| 177 | mUserId); |
Phil Weaver | ce687c5 | 2017-03-15 08:51:52 -0700 | [diff] [blame] | 178 | mEnabledOnLockScreen = Settings.Secure.getIntForUser( |
Phil Weaver | c5865d6 | 2018-03-02 16:00:43 -0800 | [diff] [blame] | 179 | cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_ON_LOCK_SCREEN, |
| 180 | dialogAlreadyShown, mUserId) == 1; |
menghanli | 390f8f9 | 2020-03-11 20:52:47 +0800 | [diff] [blame] | 181 | mIsShortcutEnabled = hasShortcutTarget; |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Called when the accessibility shortcut is activated |
| 186 | */ |
| 187 | public void performAccessibilityShortcut() { |
| 188 | Slog.d(TAG, "Accessibility shortcut activated"); |
| 189 | final ContentResolver cr = mContext.getContentResolver(); |
| 190 | final int userId = ActivityManager.getCurrentUser(); |
| 191 | final int dialogAlreadyShown = Settings.Secure.getIntForUser( |
menghanli | e83358b | 2020-02-11 16:47:18 +0800 | [diff] [blame] | 192 | cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, DialogStaus.NOT_SHOWN, |
| 193 | userId); |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 194 | // Play a notification vibration |
| 195 | Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); |
| 196 | if ((vibrator != null) && vibrator.hasVibrator()) { |
| 197 | // Don't check if haptics are disabled, as we need to alert the user that their |
| 198 | // way of interacting with the phone may change if they activate the shortcut |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 199 | long[] vibePattern = convertToLongArray( |
| 200 | mContext.getResources().getIntArray(R.array.config_longPressVibePattern)); |
Phil Weaver | 32ea372 | 2017-03-13 11:32:01 -0700 | [diff] [blame] | 201 | vibrator.vibrate(vibePattern, -1, VIBRATION_ATTRIBUTES); |
| 202 | } |
| 203 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 204 | if (dialogAlreadyShown == 0) { |
| 205 | // The first time, we show a warning rather than toggle the service to give the user a |
| 206 | // chance to turn off this feature before stuff gets enabled. |
| 207 | mAlertDialog = createShortcutWarningDialog(userId); |
| 208 | if (mAlertDialog == null) { |
| 209 | return; |
| 210 | } |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 211 | if (!performTtsPrompt(mAlertDialog)) { |
| 212 | playNotificationTone(); |
| 213 | } |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 214 | Window w = mAlertDialog.getWindow(); |
| 215 | WindowManager.LayoutParams attr = w.getAttributes(); |
| 216 | attr.type = TYPE_KEYGUARD_DIALOG; |
| 217 | w.setAttributes(attr); |
| 218 | mAlertDialog.show(); |
| 219 | Settings.Secure.putIntForUser( |
menghanli | e83358b | 2020-02-11 16:47:18 +0800 | [diff] [blame] | 220 | cr, Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, DialogStaus.SHOWN, |
| 221 | userId); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 222 | } else { |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 223 | playNotificationTone(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 224 | if (mAlertDialog != null) { |
| 225 | mAlertDialog.dismiss(); |
| 226 | mAlertDialog = null; |
| 227 | } |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 228 | showToast(); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 229 | mFrameworkObjectProvider.getAccessibilityManagerInstance(mContext) |
| 230 | .performAccessibilityShortcut(); |
| 231 | } |
| 232 | } |
| 233 | |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 234 | /** |
| 235 | * Show toast if current assigned shortcut target is an accessibility service and its target |
| 236 | * sdk version is less than or equal to Q, or greater than Q and does not request |
| 237 | * accessibility button. |
| 238 | */ |
| 239 | private void showToast() { |
| 240 | final AccessibilityServiceInfo serviceInfo = getInfoForTargetService(); |
| 241 | if (serviceInfo == null) { |
| 242 | return; |
| 243 | } |
| 244 | final String serviceName = getShortcutFeatureDescription(/* no summary */ false); |
| 245 | if (serviceName == null) { |
| 246 | return; |
| 247 | } |
| 248 | final boolean requestA11yButton = (serviceInfo.flags |
| 249 | & AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON) != 0; |
| 250 | if (serviceInfo.getResolveInfo().serviceInfo.applicationInfo |
| 251 | .targetSdkVersion > Build.VERSION_CODES.Q && requestA11yButton) { |
| 252 | return; |
| 253 | } |
| 254 | // For accessibility services, show a toast explaining what we're doing. |
| 255 | String toastMessageFormatString = mContext.getString(isServiceEnabled(serviceInfo) |
| 256 | ? R.string.accessibility_shortcut_disabling_service |
| 257 | : R.string.accessibility_shortcut_enabling_service); |
| 258 | String toastMessage = String.format(toastMessageFormatString, serviceName); |
| 259 | Toast warningToast = mFrameworkObjectProvider.makeToastFromText( |
| 260 | mContext, toastMessage, Toast.LENGTH_LONG); |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 261 | warningToast.show(); |
| 262 | } |
| 263 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 264 | private AlertDialog createShortcutWarningDialog(int userId) { |
menghanli | e83358b | 2020-02-11 16:47:18 +0800 | [diff] [blame] | 265 | final String warningMessage = mContext.getString( |
| 266 | R.string.accessibility_shortcut_toogle_warning); |
Phil Weaver | 8c58d45 | 2017-07-28 12:58:15 -0700 | [diff] [blame] | 267 | final AlertDialog alertDialog = mFrameworkObjectProvider.getAlertDialogBuilder( |
| 268 | // Use SystemUI context so we pick up any theme set in a vendor overlay |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 269 | mFrameworkObjectProvider.getSystemUiContext()) |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 270 | .setTitle(R.string.accessibility_shortcut_warning_dialog_title) |
| 271 | .setMessage(warningMessage) |
| 272 | .setCancelable(false) |
| 273 | .setPositiveButton(R.string.leave_accessibility_shortcut_on, null) |
| 274 | .setNegativeButton(R.string.disable_accessibility_shortcut, |
| 275 | (DialogInterface d, int which) -> { |
| 276 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
| 277 | Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE, "", |
| 278 | userId); |
menghanli | e83358b | 2020-02-11 16:47:18 +0800 | [diff] [blame] | 279 | |
| 280 | // If canceled, treat as if the dialog has never been shown |
| 281 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 282 | Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, |
| 283 | DialogStaus.NOT_SHOWN, userId); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 284 | }) |
| 285 | .setOnCancelListener((DialogInterface d) -> { |
| 286 | // If canceled, treat as if the dialog has never been shown |
| 287 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
menghanli | e83358b | 2020-02-11 16:47:18 +0800 | [diff] [blame] | 288 | Settings.Secure.ACCESSIBILITY_SHORTCUT_DIALOG_SHOWN, |
| 289 | DialogStaus.NOT_SHOWN, userId); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 290 | }) |
| 291 | .create(); |
| 292 | return alertDialog; |
| 293 | } |
| 294 | |
| 295 | private AccessibilityServiceInfo getInfoForTargetService() { |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 296 | final ComponentName targetComponentName = getShortcutTargetComponentName(); |
| 297 | if (targetComponentName == null) { |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 298 | return null; |
| 299 | } |
| 300 | AccessibilityManager accessibilityManager = |
| 301 | mFrameworkObjectProvider.getAccessibilityManagerInstance(mContext); |
| 302 | return accessibilityManager.getInstalledServiceInfoWithComponentName( |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 303 | targetComponentName); |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 306 | private String getShortcutFeatureDescription(boolean includeSummary) { |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 307 | final ComponentName targetComponentName = getShortcutTargetComponentName(); |
| 308 | if (targetComponentName == null) { |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 309 | return null; |
| 310 | } |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 311 | final ToggleableFrameworkFeatureInfo frameworkFeatureInfo = |
| 312 | getFrameworkShortcutFeaturesMap().get(targetComponentName); |
| 313 | if (frameworkFeatureInfo != null) { |
| 314 | return frameworkFeatureInfo.getLabel(mContext); |
| 315 | } |
| 316 | final AccessibilityServiceInfo serviceInfo = mFrameworkObjectProvider |
| 317 | .getAccessibilityManagerInstance(mContext).getInstalledServiceInfoWithComponentName( |
| 318 | targetComponentName); |
| 319 | if (serviceInfo == null) { |
| 320 | return null; |
| 321 | } |
| 322 | final PackageManager pm = mContext.getPackageManager(); |
| 323 | String label = serviceInfo.getResolveInfo().loadLabel(pm).toString(); |
Phil Weaver | 0f9aa4c | 2018-02-26 13:17:28 -0800 | [diff] [blame] | 324 | CharSequence summary = serviceInfo.loadSummary(pm); |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 325 | if (!includeSummary || TextUtils.isEmpty(summary)) { |
| 326 | return label; |
| 327 | } |
| 328 | return String.format("%s\n%s", label, summary); |
| 329 | } |
| 330 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 331 | private boolean isServiceEnabled(AccessibilityServiceInfo serviceInfo) { |
| 332 | AccessibilityManager accessibilityManager = |
| 333 | mFrameworkObjectProvider.getAccessibilityManagerInstance(mContext); |
| 334 | return accessibilityManager.getEnabledAccessibilityServiceList( |
| 335 | AccessibilityServiceInfo.FEEDBACK_ALL_MASK).contains(serviceInfo); |
| 336 | } |
| 337 | |
Zhen Sun | 62e54c2 | 2017-08-17 15:56:20 -0700 | [diff] [blame] | 338 | private boolean hasFeatureLeanback() { |
| 339 | return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK); |
| 340 | } |
| 341 | |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 342 | private void playNotificationTone() { |
| 343 | // Use USAGE_ASSISTANCE_ACCESSIBILITY for TVs to ensure that TVs play the ringtone as they |
| 344 | // have less ways of providing feedback like vibration. |
| 345 | final int audioAttributesUsage = hasFeatureLeanback() |
| 346 | ? AudioAttributes.USAGE_ASSISTANCE_ACCESSIBILITY |
| 347 | : AudioAttributes.USAGE_NOTIFICATION_EVENT; |
| 348 | |
| 349 | // Play a notification tone |
| 350 | final Ringtone tone = mFrameworkObjectProvider.getRingtone(mContext, |
| 351 | Settings.System.DEFAULT_NOTIFICATION_URI); |
| 352 | if (tone != null) { |
| 353 | tone.setAudioAttributes(new AudioAttributes.Builder() |
| 354 | .setUsage(audioAttributesUsage) |
| 355 | .build()); |
| 356 | tone.play(); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | private boolean performTtsPrompt(AlertDialog alertDialog) { |
Rhed Jao | 5d9618b | 2018-12-24 18:59:13 +0800 | [diff] [blame] | 361 | final String serviceName = getShortcutFeatureDescription(false /* no summary */); |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 362 | final AccessibilityServiceInfo serviceInfo = getInfoForTargetService(); |
Rhed Jao | 5d9618b | 2018-12-24 18:59:13 +0800 | [diff] [blame] | 363 | if (TextUtils.isEmpty(serviceName) || serviceInfo == null) { |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 364 | return false; |
| 365 | } |
| 366 | if ((serviceInfo.flags & AccessibilityServiceInfo |
| 367 | .FLAG_REQUEST_SHORTCUT_WARNING_DIALOG_SPOKEN_FEEDBACK) == 0) { |
| 368 | return false; |
| 369 | } |
Rhed Jao | 5d9618b | 2018-12-24 18:59:13 +0800 | [diff] [blame] | 370 | final TtsPrompt tts = new TtsPrompt(serviceName); |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 371 | alertDialog.setOnDismissListener(dialog -> tts.dismiss()); |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | /** |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 376 | * Returns {@code true} if any shortcut targets were assigned to accessibility shortcut key. |
| 377 | */ |
| 378 | private boolean hasShortcutTarget() { |
| 379 | // AccessibilityShortcutController is initialized earlier than AccessibilityManagerService. |
| 380 | // AccessibilityManager#getAccessibilityShortcutTargets may not return correct shortcut |
| 381 | // targets during boot. Needs to read settings directly here. |
| 382 | String shortcutTargets = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
| 383 | Settings.Secure.ACCESSIBILITY_SHORTCUT_TARGET_SERVICE, mUserId); |
Rhed Jao | f3fbcf5 | 2020-01-15 21:44:22 +0800 | [diff] [blame] | 384 | // A11y warning dialog updates settings to empty string, when user disables a11y shortcut. |
| 385 | // Only fallback to default a11y service, when setting is never updated. |
| 386 | if (shortcutTargets == null) { |
Rhed Jao | 6dad25d | 2019-10-22 22:15:05 +0800 | [diff] [blame] | 387 | shortcutTargets = mContext.getString(R.string.config_defaultAccessibilityService); |
| 388 | } |
| 389 | return !TextUtils.isEmpty(shortcutTargets); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Gets the component name of the shortcut target. |
| 394 | * |
| 395 | * @return The component name, or null if it's assigned by multiple targets. |
| 396 | */ |
| 397 | private ComponentName getShortcutTargetComponentName() { |
| 398 | final List<String> shortcutTargets = mFrameworkObjectProvider |
| 399 | .getAccessibilityManagerInstance(mContext) |
| 400 | .getAccessibilityShortcutTargets(ACCESSIBILITY_SHORTCUT_KEY); |
| 401 | if (shortcutTargets.size() != 1) { |
| 402 | return null; |
| 403 | } |
| 404 | return ComponentName.unflattenFromString(shortcutTargets.get(0)); |
| 405 | } |
| 406 | |
| 407 | /** |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 408 | * Class to wrap TextToSpeech for shortcut dialog spoken feedback. |
| 409 | */ |
| 410 | private class TtsPrompt implements TextToSpeech.OnInitListener { |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 411 | private static final int RETRY_MILLIS = 1000; |
| 412 | |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 413 | private final CharSequence mText; |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 414 | |
| 415 | private int mRetryCount = 3; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 416 | private boolean mDismiss; |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 417 | private boolean mLanguageReady = false; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 418 | private TextToSpeech mTts; |
| 419 | |
Rhed Jao | 5d9618b | 2018-12-24 18:59:13 +0800 | [diff] [blame] | 420 | TtsPrompt(String serviceName) { |
| 421 | mText = mContext.getString(R.string.accessibility_shortcut_spoken_feedback, |
| 422 | serviceName); |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 423 | mTts = mFrameworkObjectProvider.getTextToSpeech(mContext, this); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Releases the resources used by the TextToSpeech, when dialog dismiss. |
| 428 | */ |
| 429 | public void dismiss() { |
| 430 | mDismiss = true; |
| 431 | mHandler.sendMessage(PooledLambda.obtainMessage(TextToSpeech::shutdown, mTts)); |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public void onInit(int status) { |
| 436 | if (status != TextToSpeech.SUCCESS) { |
| 437 | Slog.d(TAG, "Tts init fail, status=" + Integer.toString(status)); |
| 438 | playNotificationTone(); |
| 439 | return; |
| 440 | } |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 441 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 442 | TtsPrompt::waitForTtsReady, this)); |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | private void play() { |
| 446 | if (mDismiss) { |
| 447 | return; |
| 448 | } |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 449 | final int status = mTts.speak(mText, TextToSpeech.QUEUE_FLUSH, null, null); |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 450 | if (status != TextToSpeech.SUCCESS) { |
| 451 | Slog.d(TAG, "Tts play fail"); |
| 452 | playNotificationTone(); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 457 | * Waiting for tts is ready to speak. Trying again if tts language pack is not available |
| 458 | * or tts voice data is not installed yet. |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 459 | */ |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 460 | private void waitForTtsReady() { |
| 461 | if (mDismiss) { |
| 462 | return; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 463 | } |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 464 | if (!mLanguageReady) { |
| 465 | final int status = mTts.setLanguage(Locale.getDefault()); |
| 466 | // True if language is available and TTS#loadVoice has called once |
| 467 | // that trigger TTS service to start initialization. |
| 468 | mLanguageReady = status != TextToSpeech.LANG_MISSING_DATA |
| 469 | && status != TextToSpeech.LANG_NOT_SUPPORTED; |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 470 | } |
Dieter Hsu | 0dbbb42 | 2020-02-12 14:52:59 +0800 | [diff] [blame] | 471 | if (mLanguageReady) { |
| 472 | final Voice voice = mTts.getVoice(); |
| 473 | final boolean voiceDataInstalled = voice != null |
| 474 | && voice.getFeatures() != null |
| 475 | && !voice.getFeatures().contains( |
| 476 | TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED); |
| 477 | if (voiceDataInstalled) { |
| 478 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 479 | TtsPrompt::play, this)); |
| 480 | return; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | if (mRetryCount == 0) { |
| 485 | Slog.d(TAG, "Tts not ready to speak."); |
| 486 | playNotificationTone(); |
| 487 | return; |
| 488 | } |
| 489 | // Retry if TTS service not ready yet. |
| 490 | mRetryCount -= 1; |
| 491 | mHandler.sendMessageDelayed(PooledLambda.obtainMessage( |
| 492 | TtsPrompt::waitForTtsReady, this), RETRY_MILLIS); |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 496 | /** |
| 497 | * Immutable class to hold info about framework features that can be controlled by shortcut |
| 498 | */ |
| 499 | public static class ToggleableFrameworkFeatureInfo { |
| 500 | private final String mSettingKey; |
| 501 | private final String mSettingOnValue; |
| 502 | private final String mSettingOffValue; |
| 503 | private final int mLabelStringResourceId; |
| 504 | // These go to the settings wrapper |
| 505 | private int mIconDrawableId; |
| 506 | |
| 507 | ToggleableFrameworkFeatureInfo(String settingKey, String settingOnValue, |
| 508 | String settingOffValue, int labelStringResourceId) { |
| 509 | mSettingKey = settingKey; |
| 510 | mSettingOnValue = settingOnValue; |
| 511 | mSettingOffValue = settingOffValue; |
| 512 | mLabelStringResourceId = labelStringResourceId; |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * @return The settings key to toggle between two values |
| 517 | */ |
| 518 | public String getSettingKey() { |
| 519 | return mSettingKey; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * @return The value to write to settings to turn the feature on |
| 524 | */ |
| 525 | public String getSettingOnValue() { |
| 526 | return mSettingOnValue; |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * @return The value to write to settings to turn the feature off |
| 531 | */ |
| 532 | public String getSettingOffValue() { |
| 533 | return mSettingOffValue; |
| 534 | } |
| 535 | |
| 536 | public String getLabel(Context context) { |
| 537 | return context.getString(mLabelStringResourceId); |
| 538 | } |
| 539 | } |
| 540 | |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 541 | // Class to allow mocking of static framework calls |
| 542 | public static class FrameworkObjectProvider { |
| 543 | public AccessibilityManager getAccessibilityManagerInstance(Context context) { |
| 544 | return AccessibilityManager.getInstance(context); |
| 545 | } |
| 546 | |
| 547 | public AlertDialog.Builder getAlertDialogBuilder(Context context) { |
| 548 | return new AlertDialog.Builder(context); |
| 549 | } |
| 550 | |
| 551 | public Toast makeToastFromText(Context context, CharSequence charSequence, int duration) { |
| 552 | return Toast.makeText(context, charSequence, duration); |
| 553 | } |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 554 | |
| 555 | public Context getSystemUiContext() { |
| 556 | return ActivityThread.currentActivityThread().getSystemUiContext(); |
| 557 | } |
Rhed Jao | 6182af7 | 2018-08-22 18:48:59 +0800 | [diff] [blame] | 558 | |
| 559 | /** |
| 560 | * @param ctx A context for TextToSpeech |
| 561 | * @param listener TextToSpeech initialization callback |
| 562 | * @return TextToSpeech instance |
| 563 | */ |
| 564 | public TextToSpeech getTextToSpeech(Context ctx, TextToSpeech.OnInitListener listener) { |
| 565 | return new TextToSpeech(ctx, listener); |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * @param ctx context for ringtone |
| 570 | * @param uri ringtone uri |
| 571 | * @return Ringtone instance |
| 572 | */ |
| 573 | public Ringtone getRingtone(Context ctx, Uri uri) { |
| 574 | return RingtoneManager.getRingtone(ctx, uri); |
| 575 | } |
Phil Weaver | 106fe73 | 2016-11-22 18:18:39 -0800 | [diff] [blame] | 576 | } |
| 577 | } |