• 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.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(
74                 accessibilityManager, context, TestProtocol.SWITCHED_TO_STATE_MESSAGE, parcel);
75         Log.d(TestProtocol.PERMANENT_DIAG_TAG, "sendStateEventToTest: " + stateOrdinal);
76     }
77 
sendScrollFinishedEventToTest(Context context)78     public static void sendScrollFinishedEventToTest(Context context) {
79         final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context);
80         if (accessibilityManager == null) return;
81 
82         sendEventToTest(accessibilityManager, context, TestProtocol.SCROLL_FINISHED_MESSAGE, null);
83     }
84 
sendPauseDetectedEventToTest(Context context)85     public static void sendPauseDetectedEventToTest(Context context) {
86         final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context);
87         if (accessibilityManager == null) return;
88 
89         sendEventToTest(accessibilityManager, context, TestProtocol.PAUSE_DETECTED_MESSAGE, null);
90     }
91 
sendEventToTest( AccessibilityManager accessibilityManager, Context context, String eventTag, Bundle data)92     private static void sendEventToTest(
93             AccessibilityManager accessibilityManager,
94             Context context, String eventTag, Bundle data) {
95         final AccessibilityEvent e = AccessibilityEvent.obtain(
96                 AccessibilityEvent.TYPE_ANNOUNCEMENT);
97         e.setClassName(eventTag);
98         e.setParcelableData(data);
99         e.setPackageName(context.getApplicationContext().getPackageName());
100         accessibilityManager.sendAccessibilityEvent(e);
101     }
102 
103     /**
104      * Returns accessibility manager to be used for communication with UI Automation tests.
105      * The tests may exchange custom accessibility messages with the launcher; the accessibility
106      * manager is used in these communications.
107      *
108      * If the launcher runs not under a test, the return is null, and no attempt to process or send
109      * custom accessibility messages should be made.
110      */
getAccessibilityManagerForTest(Context context)111     private static AccessibilityManager getAccessibilityManagerForTest(Context context) {
112         // If not running in a test harness, don't participate in test exchanges.
113         if (!Utilities.IS_RUNNING_IN_TEST_HARNESS) return null;
114 
115         final AccessibilityManager accessibilityManager = getManager(context);
116         if (!accessibilityManager.isEnabled()) return null;
117 
118         return accessibilityManager;
119     }
120 
getRecommendedTimeoutMillis(Context context, int originalTimeout, int flags)121     public static int getRecommendedTimeoutMillis(Context context, int originalTimeout, int flags) {
122         if (Utilities.ATLEAST_Q) {
123             return getManager(context).getRecommendedTimeoutMillis(originalTimeout, flags);
124         }
125         return originalTimeout;
126     }
127 }
128