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