Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of 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, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package androidx.core.view.accessibility; |
| 18 | |
| 19 | import android.accessibilityservice.AccessibilityServiceInfo; |
| 20 | import android.os.Build; |
Aurimas Liutikas | 9dede51 | 2018-03-13 14:08:08 -0700 | [diff] [blame] | 21 | import android.view.accessibility.AccessibilityManager; |
| 22 | |
Alan Viverette | e4131dc | 2021-11-12 10:35:12 -0500 | [diff] [blame] | 23 | import androidx.annotation.DoNotInline; |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 24 | import androidx.annotation.NonNull; |
| 25 | import androidx.annotation.RequiresApi; |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 26 | |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * Helper for accessing features in {@link AccessibilityManager}. |
| 31 | */ |
| 32 | public final class AccessibilityManagerCompat { |
| 33 | /** |
| 34 | * Registers an {@link AccessibilityManager.AccessibilityStateChangeListener} for changes in |
| 35 | * the global accessibility state of the system. |
| 36 | * |
| 37 | * @param manager The accessibility manager. |
| 38 | * @param listener The listener. |
| 39 | * @return True if successfully registered. |
| 40 | * |
| 41 | * @deprecated Use {@link AccessibilityManager#addAccessibilityStateChangeListener( |
| 42 | * AccessibilityManager.AccessibilityStateChangeListener)} directly. |
| 43 | */ |
Alan Viverette | 170c27a | 2019-10-18 15:54:50 -0400 | [diff] [blame] | 44 | @SuppressWarnings("deprecation") |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 45 | @Deprecated |
| 46 | public static boolean addAccessibilityStateChangeListener(AccessibilityManager manager, |
| 47 | AccessibilityStateChangeListener listener) { |
| 48 | if (listener == null) { |
| 49 | return false; |
| 50 | } |
| 51 | return manager.addAccessibilityStateChangeListener( |
| 52 | new AccessibilityStateChangeListenerWrapper(listener)); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Unregisters an {@link AccessibilityManager.AccessibilityStateChangeListener}. |
| 57 | * |
| 58 | * @param manager The accessibility manager. |
| 59 | * @param listener The listener. |
| 60 | * @return True if successfully unregistered. |
| 61 | * |
| 62 | * @deprecated Use {@link AccessibilityManager#removeAccessibilityStateChangeListener( |
| 63 | * AccessibilityManager.AccessibilityStateChangeListener)} directly. |
| 64 | */ |
Alan Viverette | 170c27a | 2019-10-18 15:54:50 -0400 | [diff] [blame] | 65 | @SuppressWarnings("deprecation") |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 66 | @Deprecated |
| 67 | public static boolean removeAccessibilityStateChangeListener(AccessibilityManager manager, |
| 68 | AccessibilityStateChangeListener listener) { |
| 69 | if (listener == null) { |
| 70 | return false; |
| 71 | } |
| 72 | return manager.removeAccessibilityStateChangeListener( |
| 73 | new AccessibilityStateChangeListenerWrapper(listener)); |
| 74 | } |
| 75 | |
Alan Viverette | 170c27a | 2019-10-18 15:54:50 -0400 | [diff] [blame] | 76 | @SuppressWarnings("deprecation") |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 77 | private static class AccessibilityStateChangeListenerWrapper |
| 78 | implements AccessibilityManager.AccessibilityStateChangeListener { |
| 79 | AccessibilityStateChangeListener mListener; |
| 80 | |
| 81 | AccessibilityStateChangeListenerWrapper( |
| 82 | @NonNull AccessibilityStateChangeListener listener) { |
| 83 | mListener = listener; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int hashCode() { |
| 88 | return mListener.hashCode(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean equals(Object o) { |
| 93 | if (this == o) { |
| 94 | return true; |
| 95 | } |
Aurimas Liutikas | 16c8010 | 2019-10-18 13:45:49 -0700 | [diff] [blame] | 96 | if (!(o instanceof AccessibilityStateChangeListenerWrapper)) { |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | AccessibilityStateChangeListenerWrapper other = |
| 100 | (AccessibilityStateChangeListenerWrapper) o; |
| 101 | return mListener.equals(other.mListener); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void onAccessibilityStateChanged(boolean enabled) { |
| 106 | mListener.onAccessibilityStateChanged(enabled); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the {@link AccessibilityServiceInfo}s of the installed accessibility services. |
| 112 | * |
| 113 | * @param manager The accessibility manager. |
| 114 | * @return An unmodifiable list with {@link AccessibilityServiceInfo}s. |
| 115 | * |
| 116 | * @deprecated Use {@link AccessibilityManager#getInstalledAccessibilityServiceList()} directly. |
| 117 | */ |
| 118 | @Deprecated |
| 119 | public static List<AccessibilityServiceInfo> getInstalledAccessibilityServiceList( |
| 120 | AccessibilityManager manager) { |
| 121 | return manager.getInstalledAccessibilityServiceList(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the {@link AccessibilityServiceInfo}s of the enabled accessibility services |
| 126 | * for a given feedback type. |
| 127 | * |
| 128 | * @param manager The accessibility manager. |
| 129 | * @param feedbackTypeFlags The feedback type flags. |
| 130 | * @return An unmodifiable list with {@link AccessibilityServiceInfo}s. |
| 131 | * |
| 132 | * @see AccessibilityServiceInfo#FEEDBACK_AUDIBLE |
| 133 | * @see AccessibilityServiceInfo#FEEDBACK_GENERIC |
| 134 | * @see AccessibilityServiceInfo#FEEDBACK_HAPTIC |
| 135 | * @see AccessibilityServiceInfo#FEEDBACK_SPOKEN |
| 136 | * @see AccessibilityServiceInfo#FEEDBACK_VISUAL |
| 137 | * |
| 138 | * @deprecated Use {@link AccessibilityManager#getEnabledAccessibilityServiceList(int)} |
| 139 | * directly. |
| 140 | */ |
| 141 | @Deprecated |
| 142 | public static List<AccessibilityServiceInfo> getEnabledAccessibilityServiceList( |
| 143 | AccessibilityManager manager, int feedbackTypeFlags) { |
| 144 | return manager.getEnabledAccessibilityServiceList(feedbackTypeFlags); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns if the touch exploration in the system is enabled. |
| 149 | * |
| 150 | * @param manager The accessibility manager. |
| 151 | * @return True if touch exploration is enabled, false otherwise. |
| 152 | * |
| 153 | * @deprecated Use {@link AccessibilityManager#isTouchExplorationEnabled()} directly. |
| 154 | */ |
| 155 | @Deprecated |
| 156 | public static boolean isTouchExplorationEnabled(AccessibilityManager manager) { |
| 157 | return manager.isTouchExplorationEnabled(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Registers a {@link TouchExplorationStateChangeListener} for changes in |
| 162 | * the global touch exploration state of the system. |
| 163 | * |
| 164 | * @param listener The listener. |
| 165 | * @return True if successfully registered. |
| 166 | */ |
Alan Viverette | e4131dc | 2021-11-12 10:35:12 -0500 | [diff] [blame] | 167 | public static boolean addTouchExplorationStateChangeListener( |
| 168 | @NonNull AccessibilityManager manager, |
| 169 | @NonNull TouchExplorationStateChangeListener listener) { |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 170 | if (Build.VERSION.SDK_INT >= 19) { |
Alan Viverette | e4131dc | 2021-11-12 10:35:12 -0500 | [diff] [blame] | 171 | return Api19Impl.addTouchExplorationStateChangeListenerWrapper(manager, listener); |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 172 | } else { |
| 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Unregisters a {@link TouchExplorationStateChangeListener}. |
| 179 | * |
| 180 | * @param listener The listener. |
| 181 | * @return True if successfully unregistered. |
| 182 | */ |
Alan Viverette | e4131dc | 2021-11-12 10:35:12 -0500 | [diff] [blame] | 183 | public static boolean removeTouchExplorationStateChangeListener( |
| 184 | @NonNull AccessibilityManager manager, |
| 185 | @NonNull TouchExplorationStateChangeListener listener) { |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 186 | if (Build.VERSION.SDK_INT >= 19) { |
Alan Viverette | e4131dc | 2021-11-12 10:35:12 -0500 | [diff] [blame] | 187 | return Api19Impl.removeTouchExplorationStateChangeListenerWrapper(manager, listener); |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 188 | } else { |
| 189 | return false; |
| 190 | } |
| 191 | } |
| 192 | |
Daniel Norman | 3f7690a | 2022-07-22 12:20:24 -0700 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * Whether the current accessibility request comes from an |
| 196 | * {@link android.accessibilityservice.AccessibilityService} with the |
| 197 | * {@link AccessibilityServiceInfo#isAccessibilityTool} |
| 198 | * property set to true. |
| 199 | * |
| 200 | * <p> |
| 201 | * You can use this method inside {@link android.view.accessibility.AccessibilityNodeProvider} |
| 202 | * to decide how to populate your nodes. |
| 203 | * </p> |
| 204 | * |
| 205 | * <p> |
| 206 | * <strong>Note:</strong> The return value is valid only when an |
| 207 | * {@link android.view.accessibility.AccessibilityNodeInfo} request is in progress, can |
| 208 | * change from one request to another, and has no meaning when a request is not in progress. |
| 209 | * </p> |
| 210 | * |
| 211 | * @return True if the current request is from a tool that sets isAccessibilityTool. |
| 212 | */ |
| 213 | public static boolean isRequestFromAccessibilityTool(@NonNull AccessibilityManager manager) { |
| 214 | if (Build.VERSION.SDK_INT >= 34) { |
| 215 | return Api34Impl.isRequestFromAccessibilityTool(manager); |
| 216 | } else { |
| 217 | // To preserve behavior, assume every service isAccessibilityTool if the system does |
| 218 | // not support this check. |
| 219 | return true; |
| 220 | } |
| 221 | } |
| 222 | |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 223 | @RequiresApi(19) |
Aurimas Liutikas | 16c8010 | 2019-10-18 13:45:49 -0700 | [diff] [blame] | 224 | private static final class TouchExplorationStateChangeListenerWrapper |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 225 | implements AccessibilityManager.TouchExplorationStateChangeListener { |
| 226 | final TouchExplorationStateChangeListener mListener; |
| 227 | |
| 228 | TouchExplorationStateChangeListenerWrapper( |
| 229 | @NonNull TouchExplorationStateChangeListener listener) { |
| 230 | mListener = listener; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public int hashCode() { |
| 235 | return mListener.hashCode(); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public boolean equals(Object o) { |
| 240 | if (this == o) { |
| 241 | return true; |
| 242 | } |
Aurimas Liutikas | 16c8010 | 2019-10-18 13:45:49 -0700 | [diff] [blame] | 243 | if (!(o instanceof TouchExplorationStateChangeListenerWrapper)) { |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | TouchExplorationStateChangeListenerWrapper other = |
| 247 | (TouchExplorationStateChangeListenerWrapper) o; |
| 248 | return mListener.equals(other.mListener); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void onTouchExplorationStateChanged(boolean enabled) { |
| 253 | mListener.onTouchExplorationStateChanged(enabled); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Listener for the accessibility state. |
| 259 | * |
| 260 | * @deprecated Use {@link AccessibilityManager.AccessibilityStateChangeListener} directly |
| 261 | * instead of this listener. |
| 262 | */ |
Alan Viverette | 170c27a | 2019-10-18 15:54:50 -0400 | [diff] [blame] | 263 | @SuppressWarnings("deprecation") |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 264 | @Deprecated |
| 265 | public static abstract class AccessibilityStateChangeListenerCompat |
| 266 | implements AccessibilityStateChangeListener { |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Listener for the accessibility state. |
| 271 | * |
| 272 | * @deprecated Use {@link AccessibilityManager.AccessibilityStateChangeListener} directly |
| 273 | * instead of this listener. |
| 274 | */ |
| 275 | @Deprecated |
| 276 | public interface AccessibilityStateChangeListener { |
| 277 | /** |
| 278 | * Called back on change in the accessibility state. |
| 279 | * |
| 280 | * @param enabled Whether accessibility is enabled. |
| 281 | * |
| 282 | * @deprecated Use {@link AccessibilityManager.AccessibilityStateChangeListener} directly. |
| 283 | */ |
| 284 | @Deprecated |
| 285 | void onAccessibilityStateChanged(boolean enabled); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Listener for the system touch exploration state. To listen for changes to |
| 290 | * the touch exploration state on the device, implement this interface and |
| 291 | * register it with the system by calling |
| 292 | * {@link #addTouchExplorationStateChangeListener}. |
| 293 | */ |
| 294 | public interface TouchExplorationStateChangeListener { |
| 295 | /** |
| 296 | * Called when the touch exploration enabled state changes. |
| 297 | * |
| 298 | * @param enabled Whether touch exploration is enabled. |
| 299 | */ |
| 300 | void onTouchExplorationStateChanged(boolean enabled); |
| 301 | } |
| 302 | |
Alan Viverette | e4131dc | 2021-11-12 10:35:12 -0500 | [diff] [blame] | 303 | private AccessibilityManagerCompat() { |
| 304 | } |
| 305 | |
Daniel Norman | 3f7690a | 2022-07-22 12:20:24 -0700 | [diff] [blame] | 306 | @RequiresApi(34) |
| 307 | static class Api34Impl { |
| 308 | private Api34Impl() { |
| 309 | // This class is not instantiable. |
| 310 | } |
| 311 | |
| 312 | @DoNotInline |
| 313 | static boolean isRequestFromAccessibilityTool(AccessibilityManager accessibilityManager) { |
| 314 | return accessibilityManager.isRequestFromAccessibilityTool(); |
| 315 | } |
| 316 | } |
Alan Viverette | e4131dc | 2021-11-12 10:35:12 -0500 | [diff] [blame] | 317 | @RequiresApi(19) |
| 318 | static class Api19Impl { |
| 319 | private Api19Impl() { |
| 320 | // This class is not instantiable. |
| 321 | } |
| 322 | |
| 323 | @DoNotInline |
| 324 | static boolean addTouchExplorationStateChangeListenerWrapper( |
| 325 | AccessibilityManager accessibilityManager, |
| 326 | TouchExplorationStateChangeListener listener) { |
| 327 | return accessibilityManager.addTouchExplorationStateChangeListener( |
| 328 | new TouchExplorationStateChangeListenerWrapper(listener)); |
| 329 | } |
| 330 | |
| 331 | @DoNotInline |
| 332 | static boolean removeTouchExplorationStateChangeListenerWrapper( |
| 333 | AccessibilityManager accessibilityManager, |
| 334 | TouchExplorationStateChangeListener listener) { |
| 335 | return accessibilityManager.removeTouchExplorationStateChangeListener( |
| 336 | new TouchExplorationStateChangeListenerWrapper(listener)); |
| 337 | } |
| 338 | } |
Aurimas Liutikas | ac5fe7c | 2018-03-06 14:40:53 -0800 | [diff] [blame] | 339 | } |