1 /* 2 * Copyright (C) 2017 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 com.android.launcher3.compat; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.text.TextUtils; 22 import android.util.Log; 23 import android.view.View; 24 import android.view.accessibility.AccessibilityEvent; 25 import android.view.accessibility.AccessibilityManager; 26 27 import androidx.annotation.Nullable; 28 29 import com.android.launcher3.Utilities; 30 import com.android.launcher3.testing.TestProtocol; 31 32 public class AccessibilityManagerCompat { 33 isAccessibilityEnabled(Context context)34 public static boolean isAccessibilityEnabled(Context context) { 35 return getManager(context).isEnabled(); 36 } 37 isObservedEventType(Context context, int eventType)38 public static boolean isObservedEventType(Context context, int eventType) { 39 // TODO: Use new API once available 40 return isAccessibilityEnabled(context); 41 } 42 43 /** 44 * 45 * @param target The view the accessibility event is initialized on. 46 * If null, this method has no effect. 47 * @param type See TYPE_ constants defined in {@link AccessibilityEvent}. 48 * @param text Optional text to add to the event, which will be announced to the user. 49 */ sendCustomAccessibilityEvent(@ullable View target, int type, @Nullable String text)50 public static void sendCustomAccessibilityEvent(@Nullable View target, int type, 51 @Nullable String text) { 52 if (target != null && isObservedEventType(target.getContext(), type)) { 53 AccessibilityEvent event = AccessibilityEvent.obtain(type); 54 target.onInitializeAccessibilityEvent(event); 55 if (!TextUtils.isEmpty(text)) { 56 event.getText().add(text); 57 } 58 getManager(target.getContext()).sendAccessibilityEvent(event); 59 } 60 } 61 getManager(Context context)62 private static AccessibilityManager getManager(Context context) { 63 return (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE); 64 } 65 sendStateEventToTest(Context context, int stateOrdinal)66 public static void sendStateEventToTest(Context context, int stateOrdinal) { 67 final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context); 68 if (accessibilityManager == null) return; 69 70 final Bundle parcel = new Bundle(); 71 parcel.putInt(TestProtocol.STATE_FIELD, stateOrdinal); 72 73 sendEventToTest(accessibilityManager, TestProtocol.SWITCHED_TO_STATE_MESSAGE, parcel); 74 Log.d(TestProtocol.PERMANENT_DIAG_TAG, "sendStateEventToTest: " + stateOrdinal); 75 } 76 sendScrollFinishedEventToTest(Context context)77 public static void sendScrollFinishedEventToTest(Context context) { 78 final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context); 79 if (accessibilityManager == null) return; 80 81 sendEventToTest(accessibilityManager, TestProtocol.SCROLL_FINISHED_MESSAGE, null); 82 } 83 sendPauseDetectedEventToTest(Context context)84 public static void sendPauseDetectedEventToTest(Context context) { 85 final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context); 86 if (accessibilityManager == null) return; 87 88 sendEventToTest(accessibilityManager, TestProtocol.PAUSE_DETECTED_MESSAGE, null); 89 } 90 sendEventToTest( AccessibilityManager accessibilityManager, String eventTag, Bundle data)91 private static void sendEventToTest( 92 AccessibilityManager accessibilityManager, String eventTag, Bundle data) { 93 final AccessibilityEvent e = AccessibilityEvent.obtain( 94 AccessibilityEvent.TYPE_ANNOUNCEMENT); 95 e.setClassName(eventTag); 96 e.setParcelableData(data); 97 accessibilityManager.sendAccessibilityEvent(e); 98 } 99 100 /** 101 * Returns accessibility manager to be used for communication with UI Automation tests. 102 * The tests may exchange custom accessibility messages with the launcher; the accessibility 103 * manager is used in these communications. 104 * 105 * If the launcher runs not under a test, the return is null, and no attempt to process or send 106 * custom accessibility messages should be made. 107 */ getAccessibilityManagerForTest(Context context)108 private static AccessibilityManager getAccessibilityManagerForTest(Context context) { 109 // If not running in a test harness, don't participate in test exchanges. 110 if (!Utilities.IS_RUNNING_IN_TEST_HARNESS) return null; 111 112 final AccessibilityManager accessibilityManager = getManager(context); 113 if (!accessibilityManager.isEnabled()) return null; 114 115 return accessibilityManager; 116 } 117 getRecommendedTimeoutMillis(Context context, int originalTimeout, int flags)118 public static int getRecommendedTimeoutMillis(Context context, int originalTimeout, int flags) { 119 if (Utilities.ATLEAST_Q) { 120 return getManager(context).getRecommendedTimeoutMillis(originalTimeout, flags); 121 } 122 return originalTimeout; 123 } 124 } 125