• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.example.android.supportv4.accessibility;
18 
19 import android.accessibilityservice.AccessibilityServiceInfo;
20 import android.app.Activity;
21 import android.app.Service;
22 import android.content.pm.ResolveInfo;
23 import android.os.Bundle;
24 import android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat;
25 import android.support.v4.view.accessibility.AccessibilityManagerCompat;
26 import android.view.accessibility.AccessibilityManager;
27 import android.widget.TextView;
28 import android.widget.Toast;
29 
30 import com.example.android.supportv4.R;
31 
32 import java.util.List;
33 
34 /**
35  * <p>
36  * This class demonstrates how to use the support library to register
37  * an AccessibilityManager.AccessibilityStateChangeListener introduced
38  * in ICS to watch changes to the global accessibility state on the
39  * device in a backwards compatible manner.
40  * </p>
41  * <p>
42  * This class also demonstrates how to use the support library to query
43  * information about enabled accessibility services via APIs introduced
44  * in ICS in a backwards compatible manner.
45  * </p>
46  */
47 public class AccessibilityManagerSupportActivity extends Activity {
48 
49     /** Handle to the accessibility manager service. */
50     private AccessibilityManager mAccessibilityManager;
51 
52     /** Handle to the View showing accessibility services summary */
53     private TextView mAccessibilityStateView;
54 
55     /**
56      * {@inheritDoc}
57      */
58     @Override
onCreate(Bundle savedInstanceState)59     public void onCreate(Bundle savedInstanceState) {
60         super.onCreate(savedInstanceState);
61         setContentView(R.layout.accessibility_manager);
62         mAccessibilityManager = (AccessibilityManager) getSystemService(
63                 Service.ACCESSIBILITY_SERVICE);
64         mAccessibilityStateView = findViewById(R.id.accessibility_state);
65         registerAccessibilityStateChangeListener();
66     }
67 
68     /**
69      * {@inheritDoc}
70      */
71     @Override
onResume()72     public void onResume() {
73         super.onResume();
74         updateAccessibilityStateView();
75     }
76 
77     /**
78      * Registers an AccessibilityStateChangeListener that show a Toast
79      * when the global accessibility state on the device changes.
80      */
registerAccessibilityStateChangeListener()81     private void registerAccessibilityStateChangeListener() {
82         // The AccessibilityStateChange listener APIs were added in ICS. Therefore to be
83         // backwards compatible we use the APIs in the support library. Note that if the
84         // platform API version is lower and the called API is not available no listener
85         // is added and you will not receive a call of onAccessibilityStateChanged.
86         AccessibilityManagerCompat.addAccessibilityStateChangeListener(mAccessibilityManager,
87                 new AccessibilityManagerCompat.AccessibilityStateChangeListener() {
88             @Override
89             public void onAccessibilityStateChanged(boolean enabled) {
90                 Toast.makeText(AccessibilityManagerSupportActivity.this,
91                         getString(R.string.accessibility_manager_accessibility_state,
92                                 Boolean.toString(enabled)),
93                         Toast.LENGTH_SHORT).show();
94             }
95         });
96     }
97 
98     /**
99      * Updates the content of a TextView with description of the enabled
100      * accessibility services.
101      */
updateAccessibilityStateView()102     private void updateAccessibilityStateView() {
103         // The API for getting the enabled accessibility services based on feedback
104         // type was added in ICS. Therefore to be backwards compatible we use the
105         // APIs in the support library. Note that if the platform API version is lower
106         // and the called API is not available an empty list of services is returned.
107         List<AccessibilityServiceInfo> enabledServices =
108             AccessibilityManagerCompat.getEnabledAccessibilityServiceList(mAccessibilityManager,
109                     AccessibilityServiceInfo.FEEDBACK_SPOKEN);
110         if (!enabledServices.isEmpty()) {
111             StringBuilder builder = new StringBuilder();
112             final int enabledServiceCount = enabledServices.size();
113             for (int i = 0; i < enabledServiceCount; i++) {
114                 AccessibilityServiceInfo service = enabledServices.get(i);
115                 // Some new APIs were added in ICS for getting more information about
116                 // an accessibility service. Again accessed them via the support library.
117                 ResolveInfo resolveInfo = service.getResolveInfo();
118                 String serviceDescription = getString(
119                         R.string.accessibility_manager_enabled_service,
120                         resolveInfo.loadLabel(getPackageManager()),
121                         AccessibilityServiceInfoCompat.feedbackTypeToString(service.feedbackType),
122                         AccessibilityServiceInfoCompat.loadDescription(
123                                 service, getPackageManager()),
124                         service.getSettingsActivityName());
125                 builder.append(serviceDescription);
126             }
127             mAccessibilityStateView.setText(builder);
128         } else {
129             // Either no services or the platform API version is not high enough.
130             mAccessibilityStateView.setText(getString(
131                     R.string.accessibility_manager_no_enabled_services));
132         }
133     }
134 }
135