AccessibilityManagerSupportActivity.java revision 1212111afe952e9e5f423f777414067dbda74f24
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.android.accessibilityservice.AccessibilityServiceInfoCompat;
25import android.support.v4.view.accessibility.AccessibilityManagerCompat;
26import android.support.v4.view.accessibility.AccessibilityManagerCompat.AccessibilityStateChangeListenerCompat;
27import android.view.accessibility.AccessibilityManager;
28import android.widget.TextView;
29import android.widget.Toast;
30
31import com.example.android.supportv4.R;
32
33import java.util.List;
34
35/**
36 * <p>
37 * This class demonstrates how to use the support library to register
38 * an AccessibilityManager.AccessibilityStateChangeListener introduced
39 * in ICS to watch changes to the global accessibility state on the
40 * device in a backwards compatible manner.
41 * </p>
42 * <p>
43 * This class also demonstrates how to use the support library to query
44 * information about enabled accessibility services via APIs introduced
45 * in ICS in a backwards compatible manner.
46 * </p>
47 */
48public class AccessibilityManagerSupportActivity extends Activity {
49
50    /** Handle to the accessibility manager service. */
51    private AccessibilityManager mAccessibilityManager;
52
53    /** Handle to the View showing accessibility services summary */
54    private TextView mAccessibilityStateView;
55
56    /**
57     * {@inheritDoc}
58     */
59    @Override
60    public void onCreate(Bundle savedInstanceState) {
61        super.onCreate(savedInstanceState);
62        setContentView(R.layout.accessibility_manager);
63        mAccessibilityManager = (AccessibilityManager) getSystemService(
64                Service.ACCESSIBILITY_SERVICE);
65        mAccessibilityStateView = (TextView) findViewById(R.id.accessibility_state);
66        registerAccessibilityStateChangeListener();
67    }
68
69    /**
70     * {@inheritDoc}
71     */
72    @Override
73    public void onResume() {
74        super.onResume();
75        updateAccessibilityStateView();
76    }
77
78    /**
79     * Registers an AccessibilityStateChangeListener that show a Toast
80     * when the global accessibility state on the device changes.
81     */
82    private void registerAccessibilityStateChangeListener() {
83        // The AccessibilityStateChange listener APIs were added in ICS. Therefore to be
84        // backwards compatible we use the APIs in the support library. Note that if the
85        // platform API version is lower and the called API is not available no listener
86        // is added and you will not receive a call of onAccessibilityStateChanged.
87        AccessibilityManagerCompat.addAccessibilityStateChangeListener(mAccessibilityManager,
88                new AccessibilityStateChangeListenerCompat() {
89            @Override
90            public void onAccessibilityStateChanged(boolean enabled) {
91                Toast.makeText(AccessibilityManagerSupportActivity.this,
92                        getString(R.string.accessibility_manager_accessibility_state, 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     */
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 = AccessibilityServiceInfoCompat.getResolveInfo(service);
118                String serviceDescription = getString(
119                        R.string.accessibility_manager_enabled_service,
120                        resolveInfo.loadLabel(getPackageManager()),
121                        AccessibilityServiceInfoCompat.feedbackTypeToString(service.feedbackType),
122                        AccessibilityServiceInfoCompat.getDescription(service),
123                        AccessibilityServiceInfoCompat.getSettingsActivityName(service));
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