1 /*
2  * Copyright (C) 2021 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.wm.shell.onehanded;
18 
19 import android.content.Context;
20 import android.view.accessibility.AccessibilityEvent;
21 import android.view.accessibility.AccessibilityManager;
22 
23 import androidx.annotation.NonNull;
24 
25 import com.android.wm.shell.R;
26 
27 import java.io.PrintWriter;
28 
29 /**
30  * The util for handling A11y events.
31  */
32 public final class OneHandedAccessibilityUtil {
33     private static final String TAG = "OneHandedAccessibilityUtil";
34 
35     private final AccessibilityManager mAccessibilityManager;
36     private final String mStartOneHandedDescription;
37     private final String mStopOneHandedDescription;
38     private final String mPackageName;
39 
40     private String mDescription;
41 
OneHandedAccessibilityUtil(Context context)42     public OneHandedAccessibilityUtil(Context context) {
43         mAccessibilityManager = AccessibilityManager.getInstance(context);
44         mPackageName = context.getPackageName();
45         mStartOneHandedDescription = context.getResources().getString(
46                 R.string.accessibility_action_start_one_handed);
47         mStopOneHandedDescription = context.getResources().getString(
48                 R.string.accessibility_action_stop_one_handed);
49     }
50 
51     /**
52      * Gets One-Handed start description.
53      * @return text of start description.
54      */
getOneHandedStartDescription()55     public String getOneHandedStartDescription() {
56         return mStartOneHandedDescription;
57     }
58 
59     /**
60      * Gets One-Handed stop description.
61      * @return text of stop description.
62      */
getOneHandedStopDescription()63     public String getOneHandedStopDescription() {
64         return mStopOneHandedDescription;
65     }
66 
67     /**
68      * Announcement of A11y Events
69      * @param description for accessibility announcement text
70      */
announcementForScreenReader(String description)71     public void announcementForScreenReader(String description) {
72         if (!mAccessibilityManager.isTouchExplorationEnabled()) {
73             return;
74         }
75         mDescription = description;
76         final AccessibilityEvent event = AccessibilityEvent.obtain();
77         event.setPackageName(mPackageName);
78         event.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
79         event.getText().add(mDescription);
80         mAccessibilityManager.sendAccessibilityEvent(event);
81     }
82 
dump(@onNull PrintWriter pw)83     public void dump(@NonNull PrintWriter pw) {
84         final String innerPrefix = "  ";
85         pw.println(TAG);
86         pw.print(innerPrefix + "mPackageName=");
87         pw.println(mPackageName);
88         pw.print(innerPrefix + "mDescription=");
89         pw.println(mDescription);
90     }
91 }
92