• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (com.android.launcher3.testing.TestProtocol.sDebugTracing) {
57             android.util.Log.e(TestProtocol.NO_ALLAPPS_EVENT_TAG, "sendStateEventToTest");
58         }
59         final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context);
60         if (accessibilityManager == null) return;
61 
62         final Bundle parcel = new Bundle();
63         parcel.putInt(TestProtocol.STATE_FIELD, stateOrdinal);
64 
65         sendEventToTest(accessibilityManager, TestProtocol.SWITCHED_TO_STATE_MESSAGE, parcel);
66     }
67 
sendScrollFinishedEventToTest(Context context)68     public static void sendScrollFinishedEventToTest(Context context) {
69         final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context);
70         if (accessibilityManager == null) return;
71 
72         sendEventToTest(accessibilityManager, TestProtocol.SCROLL_FINISHED_MESSAGE, null);
73     }
74 
sendPauseDetectedEventToTest(Context context)75     public static void sendPauseDetectedEventToTest(Context context) {
76         final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context);
77         if (accessibilityManager == null) return;
78 
79         sendEventToTest(accessibilityManager, TestProtocol.PAUSE_DETECTED_MESSAGE, null);
80     }
81 
sendEventToTest( AccessibilityManager accessibilityManager, String eventTag, Bundle data)82     private static void sendEventToTest(
83             AccessibilityManager accessibilityManager, String eventTag, Bundle data) {
84         final AccessibilityEvent e = AccessibilityEvent.obtain(
85                 AccessibilityEvent.TYPE_ANNOUNCEMENT);
86         e.setClassName(eventTag);
87         e.setParcelableData(data);
88         accessibilityManager.sendAccessibilityEvent(e);
89     }
90 
91     /**
92      * Returns accessibility manager to be used for communication with UI Automation tests.
93      * The tests may exchange custom accessibility messages with the launcher; the accessibility
94      * manager is used in these communications.
95      *
96      * If the launcher runs not under a test, the return is null, and no attempt to process or send
97      * custom accessibility messages should be made.
98      */
getAccessibilityManagerForTest(Context context)99     private static AccessibilityManager getAccessibilityManagerForTest(Context context) {
100         // If not running in a test harness, don't participate in test exchanges.
101         if (!Utilities.IS_RUNNING_IN_TEST_HARNESS) return null;
102 
103         final AccessibilityManager accessibilityManager = getManager(context);
104         if (!accessibilityManager.isEnabled()) return null;
105 
106         return accessibilityManager;
107     }
108 
processTestRequest(Context context, String eventTag, int action, Bundle request, Consumer<Bundle> responseFiller)109     public static boolean processTestRequest(Context context, String eventTag, int action,
110             Bundle request, Consumer<Bundle> responseFiller) {
111         final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context);
112         if (accessibilityManager == null) return false;
113 
114         // The test sends a request via a ACTION_SET_TEXT.
115         if (action == AccessibilityNodeInfo.ACTION_SET_TEXT &&
116                 eventTag.equals(request.getCharSequence(
117                         AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE))) {
118             final Bundle response = new Bundle();
119             responseFiller.accept(response);
120             AccessibilityManagerCompat.sendEventToTest(
121                     accessibilityManager, eventTag + TestProtocol.RESPONSE_MESSAGE_POSTFIX, response);
122             return true;
123         }
124         return false;
125     }
126 
getRecommendedTimeoutMillis(Context context, int originalTimeout, int flags)127     public static int getRecommendedTimeoutMillis(Context context, int originalTimeout, int flags) {
128         if (Utilities.ATLEAST_Q) {
129             return getManager(context).getRecommendedTimeoutMillis(originalTimeout, flags);
130         }
131         return originalTimeout;
132     }
133 }
134