1 /*
2  * Copyright (C) 2014 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.systemui.statusbar.policy;
18 
19 import android.content.Context;
20 import android.util.Log;
21 import android.view.accessibility.AccessibilityManager;
22 
23 import java.io.FileDescriptor;
24 import java.io.PrintWriter;
25 import java.util.ArrayList;
26 
27 public class AccessibilityController implements
28         AccessibilityManager.AccessibilityStateChangeListener,
29         AccessibilityManager.TouchExplorationStateChangeListener {
30 
31     private final ArrayList<AccessibilityStateChangedCallback> mChangeCallbacks = new ArrayList<>();
32 
33     private boolean mAccessibilityEnabled;
34     private boolean mTouchExplorationEnabled;
35 
AccessibilityController(Context context)36     public AccessibilityController(Context context) {
37         AccessibilityManager am =
38                 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
39         am.addTouchExplorationStateChangeListener(this);
40         am.addAccessibilityStateChangeListener(this);
41         mAccessibilityEnabled = am.isEnabled();
42         mTouchExplorationEnabled = am.isTouchExplorationEnabled();
43     }
44 
isAccessibilityEnabled()45     public boolean isAccessibilityEnabled() {
46         return mAccessibilityEnabled;
47     }
48 
isTouchExplorationEnabled()49     public boolean isTouchExplorationEnabled() {
50         return mTouchExplorationEnabled;
51     }
52 
dump(FileDescriptor fd, PrintWriter pw, String[] args)53     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
54         pw.println("AccessibilityController state:");
55         pw.print("  mAccessibilityEnabled="); pw.println(mAccessibilityEnabled);
56         pw.print("  mTouchExplorationEnabled="); pw.println(mTouchExplorationEnabled);
57     }
58 
addStateChangedCallback(AccessibilityStateChangedCallback cb)59     public void addStateChangedCallback(AccessibilityStateChangedCallback cb) {
60         mChangeCallbacks.add(cb);
61         cb.onStateChanged(mAccessibilityEnabled, mTouchExplorationEnabled);
62     }
63 
removeStateChangedCallback(AccessibilityStateChangedCallback cb)64     public void removeStateChangedCallback(AccessibilityStateChangedCallback cb) {
65         mChangeCallbacks.remove(cb);
66     }
67 
fireChanged()68     private void fireChanged() {
69         final int N = mChangeCallbacks.size();
70         for (int i = 0; i < N; i++) {
71             mChangeCallbacks.get(i).onStateChanged(mAccessibilityEnabled, mTouchExplorationEnabled);
72         }
73     }
74 
75     @Override
onAccessibilityStateChanged(boolean enabled)76     public void onAccessibilityStateChanged(boolean enabled) {
77         mAccessibilityEnabled = enabled;
78         fireChanged();
79     }
80 
81     @Override
onTouchExplorationStateChanged(boolean enabled)82     public void onTouchExplorationStateChanged(boolean enabled) {
83         mTouchExplorationEnabled = enabled;
84         fireChanged();
85     }
86 
87     public interface AccessibilityStateChangedCallback {
onStateChanged(boolean accessibilityEnabled, boolean touchExplorationEnabled)88         void onStateChanged(boolean accessibilityEnabled, boolean touchExplorationEnabled);
89     }
90 }
91