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.shared.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      * @param target The view the accessibility event is initialized on.
45      *               If null, this method has no effect.
46      * @param type   See TYPE_ constants defined in {@link AccessibilityEvent}.
47      * @param text   Optional text to add to the event, which will be announced to the user.
48      */
sendCustomAccessibilityEvent(@ullable View target, int type, @Nullable String text)49     public static void sendCustomAccessibilityEvent(@Nullable View target, int type,
50             @Nullable String text) {
51         if (target != null && isObservedEventType(target.getContext(), type)) {
52             AccessibilityEvent event = AccessibilityEvent.obtain(type);
53             target.onInitializeAccessibilityEvent(event);
54             if (!TextUtils.isEmpty(text)) {
55                 event.getText().add(text);
56             }
57             getManager(target.getContext()).sendAccessibilityEvent(event);
58         }
59     }
60 
getManager(Context context)61     private static AccessibilityManager getManager(Context context) {
62         return (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
63     }
64 
sendStateEventToTest(Context context, int stateOrdinal)65     public static void sendStateEventToTest(Context context, int stateOrdinal) {
66         final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context);
67         if (accessibilityManager == null) return;
68 
69         final Bundle parcel = new Bundle();
70         parcel.putInt(TestProtocol.STATE_FIELD, stateOrdinal);
71 
72         sendEventToTest(
73                 accessibilityManager, context, TestProtocol.SWITCHED_TO_STATE_MESSAGE, parcel);
74         Log.d(TestProtocol.PERMANENT_DIAG_TAG, "sendStateEventToTest: " + stateOrdinal);
75     }
76 
sendTestProtocolEventToTest(Context context, String testProtocolEvent)77     public static void sendTestProtocolEventToTest(Context context, String testProtocolEvent) {
78         final AccessibilityManager accessibilityManager = getAccessibilityManagerForTest(context);
79         if (accessibilityManager == null) return;
80 
81         sendEventToTest(accessibilityManager, context, testProtocolEvent, null);
82     }
sendEventToTest( AccessibilityManager accessibilityManager, Context context, String eventTag, Bundle data)83     private static void sendEventToTest(
84             AccessibilityManager accessibilityManager,
85             Context context, String eventTag, Bundle data) {
86         final AccessibilityEvent e = AccessibilityEvent.obtain(
87                 AccessibilityEvent.TYPE_ANNOUNCEMENT);
88         e.setClassName(eventTag);
89         e.setParcelableData(data);
90         e.setPackageName(context.getApplicationContext().getPackageName());
91         accessibilityManager.sendAccessibilityEvent(e);
92     }
93 
94     /**
95      * Returns accessibility manager to be used for communication with UI Automation tests.
96      * The tests may exchange custom accessibility messages with the launcher; the accessibility
97      * manager is used in these communications.
98      *
99      * If the launcher runs not under a test, the return is null, and no attempt to process or send
100      * custom accessibility messages should be made.
101      */
getAccessibilityManagerForTest(Context context)102     private static AccessibilityManager getAccessibilityManagerForTest(Context context) {
103         // If not running in a test harness, don't participate in test exchanges.
104         if (!Utilities.isRunningInTestHarness()) return null;
105 
106         final AccessibilityManager accessibilityManager = getManager(context);
107         if (!accessibilityManager.isEnabled()) return null;
108 
109         return accessibilityManager;
110     }
111 
getRecommendedTimeoutMillis(Context context, int originalTimeout, int flags)112     public static int getRecommendedTimeoutMillis(Context context, int originalTimeout, int flags) {
113         return getManager(context).getRecommendedTimeoutMillis(originalTimeout, flags);
114     }
115 }
116