AccessibilityManagerSupportActivity.java revision b716a74c7c8dc6c8c76e58002747863405bef1d6
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 17package com.example.android.supportv4.accessibility; 18 19import android.accessibilityservice.AccessibilityServiceInfo; 20import android.app.Activity; 21import android.app.Service; 22import android.content.pm.ResolveInfo; 23import android.os.Bundle; 24import android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat; 25import android.support.v4.view.accessibility.AccessibilityManagerCompat; 26import android.view.accessibility.AccessibilityManager; 27import android.widget.TextView; 28import android.widget.Toast; 29 30import com.example.android.supportv4.R; 31 32import 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 */ 47public 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 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 = (TextView) findViewById(R.id.accessibility_state); 65 registerAccessibilityStateChangeListener(); 66 } 67 68 /** 69 * {@inheritDoc} 70 */ 71 @Override 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 */ 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, enabled), 92 Toast.LENGTH_SHORT).show(); 93 } 94 }); 95 } 96 97 /** 98 * Updates the content of a TextView with description of the enabled 99 * accessibility services. 100 */ 101 private void updateAccessibilityStateView() { 102 // The API for getting the enabled accessibility services based on feedback 103 // type was added in ICS. Therefore to be backwards compatible we use the 104 // APIs in the support library. Note that if the platform API version is lower 105 // and the called API is not available an empty list of services is returned. 106 List<AccessibilityServiceInfo> enabledServices = 107 AccessibilityManagerCompat.getEnabledAccessibilityServiceList(mAccessibilityManager, 108 AccessibilityServiceInfo.FEEDBACK_SPOKEN); 109 if (!enabledServices.isEmpty()) { 110 StringBuilder builder = new StringBuilder(); 111 final int enabledServiceCount = enabledServices.size(); 112 for (int i = 0; i < enabledServiceCount; i++) { 113 AccessibilityServiceInfo service = enabledServices.get(i); 114 // Some new APIs were added in ICS for getting more information about 115 // an accessibility service. Again accessed them via the support library. 116 ResolveInfo resolveInfo = service.getResolveInfo(); 117 String serviceDescription = getString( 118 R.string.accessibility_manager_enabled_service, 119 resolveInfo.loadLabel(getPackageManager()), 120 AccessibilityServiceInfoCompat.feedbackTypeToString(service.feedbackType), 121 AccessibilityServiceInfoCompat.loadDescription( 122 service, getPackageManager()), 123 service.getSettingsActivityName()); 124 builder.append(serviceDescription); 125 } 126 mAccessibilityStateView.setText(builder); 127 } else { 128 // Either no services or the platform API version is not high enough. 129 mAccessibilityStateView.setText(getString( 130 R.string.accessibility_manager_no_enabled_services)); 131 } 132 } 133} 134