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