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