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.view.accessibility.AccessibilityManager;
25import android.widget.TextView;
26import android.widget.Toast;
27
28import androidx.core.accessibilityservice.AccessibilityServiceInfoCompat;
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 = 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        mAccessibilityManager.addAccessibilityStateChangeListener(
83                new AccessibilityManager.AccessibilityStateChangeListener() {
84                    @Override
85                    public void onAccessibilityStateChanged(boolean enabled) {
86                        Toast.makeText(AccessibilityManagerSupportActivity.this,
87                                getString(R.string.accessibility_manager_accessibility_state,
88                                        Boolean.toString(enabled)),
89                                Toast.LENGTH_SHORT).show();
90                    }
91                });
92    }
93
94    /**
95     * Updates the content of a TextView with description of the enabled
96     * accessibility services.
97     */
98    private void updateAccessibilityStateView() {
99        List<AccessibilityServiceInfo> enabledServices =
100                mAccessibilityManager.getEnabledAccessibilityServiceList(
101                        AccessibilityServiceInfo.FEEDBACK_SPOKEN);
102        if (!enabledServices.isEmpty()) {
103            StringBuilder builder = new StringBuilder();
104            final int enabledServiceCount = enabledServices.size();
105            for (int i = 0; i < enabledServiceCount; i++) {
106                AccessibilityServiceInfo service = enabledServices.get(i);
107                // Some new APIs were added in ICS for getting more information about
108                // an accessibility service. Again accessed them via the support library.
109                ResolveInfo resolveInfo = service.getResolveInfo();
110                String serviceDescription = getString(
111                        R.string.accessibility_manager_enabled_service,
112                        resolveInfo.loadLabel(getPackageManager()),
113                        AccessibilityServiceInfoCompat.feedbackTypeToString(service.feedbackType),
114                        AccessibilityServiceInfoCompat.loadDescription(
115                                service, getPackageManager()),
116                        service.getSettingsActivityName());
117                builder.append(serviceDescription);
118            }
119            mAccessibilityStateView.setText(builder);
120        } else {
121            // Either no services or the platform API version is not high enough.
122            mAccessibilityStateView.setText(getString(
123                    R.string.accessibility_manager_no_enabled_services));
124        }
125    }
126}
127