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
17package com.android.systemui.statusbar.policy;
18
19import android.content.Context;
20import android.util.Log;
21import android.view.accessibility.AccessibilityManager;
22
23import java.io.FileDescriptor;
24import java.io.PrintWriter;
25import java.util.ArrayList;
26
27public 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
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
45    public boolean isAccessibilityEnabled() {
46        return mAccessibilityEnabled;
47    }
48
49    public boolean isTouchExplorationEnabled() {
50        return mTouchExplorationEnabled;
51    }
52
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
59    public void addStateChangedCallback(AccessibilityStateChangedCallback cb) {
60        mChangeCallbacks.add(cb);
61        cb.onStateChanged(mAccessibilityEnabled, mTouchExplorationEnabled);
62    }
63
64    public void removeStateChangedCallback(AccessibilityStateChangedCallback cb) {
65        mChangeCallbacks.remove(cb);
66    }
67
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
76    public void onAccessibilityStateChanged(boolean enabled) {
77        mAccessibilityEnabled = enabled;
78        fireChanged();
79    }
80
81    @Override
82    public void onTouchExplorationStateChanged(boolean enabled) {
83        mTouchExplorationEnabled = enabled;
84        fireChanged();
85    }
86
87    public interface AccessibilityStateChangedCallback {
88        void onStateChanged(boolean accessibilityEnabled, boolean touchExplorationEnabled);
89    }
90}
91