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.view.View; 22 import android.view.accessibility.AccessibilityEvent; 23 import android.view.accessibility.AccessibilityManager; 24 import android.view.accessibility.AccessibilityNodeInfo; 25 26 import com.android.launcher3.testing.TestProtocol; 27 import com.android.launcher3.Utilities; 28 29 import java.util.function.Consumer; 30 31 public class AccessibilityManagerCompat { 32 isAccessibilityEnabled(Context context)33 public static boolean isAccessibilityEnabled(Context context) { 34 return getManager(context).isEnabled(); 35 } 36 isObservedEventType(Context context, int eventType)37 public static boolean isObservedEventType(Context context, int eventType) { 38 // TODO: Use new API once available 39 return isAccessibilityEnabled(context); 40 } 41 sendCustomAccessibilityEvent(View target, int type, String text)42 public static void sendCustomAccessibilityEvent(View target, int type, String text) { 43 if (isObservedEventType(target.getContext(), type)) { 44 AccessibilityEvent event = AccessibilityEvent.obtain(type); 45 target.onInitializeAccessibilityEvent(event); 46 event.getText().add(text); 47 getManager(target.getContext()).sendAccessibilityEvent(event); 48 } 49 } 50 getManager(Context context)51 private static AccessibilityManager getManager(Context context) { 52 return (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE); 53 } 54 sendStateEventToTest(Context context, int stateOrdinal)55 public static void sendStateEventToTest(Context context, int stateOrdinal) { 56 final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context); 57 if (accessibilityManager == null) return; 58 59 final Bundle parcel = new Bundle(); 60 parcel.putInt(TestProtocol.STATE_FIELD, stateOrdinal); 61 62 sendEventToTest(accessibilityManager, TestProtocol.SWITCHED_TO_STATE_MESSAGE, parcel); 63 } 64 sendScrollFinishedEventToTest(Context context)65 public static void sendScrollFinishedEventToTest(Context context) { 66 final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context); 67 if (accessibilityManager == null) return; 68 69 sendEventToTest(accessibilityManager, TestProtocol.SCROLL_FINISHED_MESSAGE, null); 70 } 71 sendPauseDetectedEventToTest(Context context)72 public static void sendPauseDetectedEventToTest(Context context) { 73 final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context); 74 if (accessibilityManager == null) return; 75 76 sendEventToTest(accessibilityManager, TestProtocol.PAUSE_DETECTED_MESSAGE, null); 77 } 78 sendEventToTest( AccessibilityManager accessibilityManager, String eventTag, Bundle data)79 private static void sendEventToTest( 80 AccessibilityManager accessibilityManager, String eventTag, Bundle data) { 81 final AccessibilityEvent e = AccessibilityEvent.obtain( 82 AccessibilityEvent.TYPE_ANNOUNCEMENT); 83 e.setClassName(eventTag); 84 e.setParcelableData(data); 85 accessibilityManager.sendAccessibilityEvent(e); 86 } 87 88 /** 89 * Returns accessibility manager to be used for communication with UI Automation tests. 90 * The tests may exchange custom accessibility messages with the launcher; the accessibility 91 * manager is used in these communications. 92 * 93 * If the launcher runs not under a test, the return is null, and no attempt to process or send 94 * custom accessibility messages should be made. 95 */ getAccessibilityManagerForTest(Context context)96 private static AccessibilityManager getAccessibilityManagerForTest(Context context) { 97 // If not running in a test harness, don't participate in test exchanges. 98 if (!Utilities.IS_RUNNING_IN_TEST_HARNESS) return null; 99 100 final AccessibilityManager accessibilityManager = getManager(context); 101 if (!accessibilityManager.isEnabled()) return null; 102 103 return accessibilityManager; 104 } 105 processTestRequest(Context context, String eventTag, int action, Bundle request, Consumer<Bundle> responseFiller)106 public static boolean processTestRequest(Context context, String eventTag, int action, 107 Bundle request, Consumer<Bundle> responseFiller) { 108 final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context); 109 if (accessibilityManager == null) return false; 110 111 // The test sends a request via a ACTION_SET_TEXT. 112 if (action == AccessibilityNodeInfo.ACTION_SET_TEXT && 113 eventTag.equals(request.getCharSequence( 114 AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE))) { 115 final Bundle response = new Bundle(); 116 responseFiller.accept(response); 117 AccessibilityManagerCompat.sendEventToTest( 118 accessibilityManager, eventTag + TestProtocol.RESPONSE_MESSAGE_POSTFIX, response); 119 return true; 120 } 121 return false; 122 } 123 getRecommendedTimeoutMillis(Context context, int originalTimeout, int flags)124 public static int getRecommendedTimeoutMillis(Context context, int originalTimeout, int flags) { 125 if (Utilities.ATLEAST_Q) { 126 return getManager(context).getRecommendedTimeoutMillis(originalTimeout, flags); 127 } 128 return originalTimeout; 129 } 130 } 131