1 /*
2  * Copyright (C) 2015 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.settings.accessibility;
18 
19 import android.accessibilityservice.AccessibilityServiceInfo;
20 import android.content.ComponentName;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.content.pm.ServiceInfo;
24 import android.os.Bundle;
25 import android.provider.Settings;
26 import android.support.v7.preference.Preference;
27 import android.text.TextUtils;
28 import android.view.accessibility.AccessibilityManager;
29 
30 import com.android.internal.logging.MetricsProto.MetricsEvent;
31 import com.android.settings.DialogCreatable;
32 import com.android.settings.R;
33 import com.android.settings.SettingsPreferenceFragment;
34 
35 import java.util.List;
36 
37 /**
38  * Activity with the accessibility settings specific to Setup Wizard.
39  */
40 public class AccessibilitySettingsForSetupWizard extends SettingsPreferenceFragment
41         implements DialogCreatable, Preference.OnPreferenceChangeListener {
42 
43     private static final String TAG = AccessibilitySettingsForSetupWizard.class.getSimpleName();
44 
45     // Preferences.
46     private static final String DISPLAY_MAGNIFICATION_PREFERENCE =
47             "screen_magnification_preference";
48     private static final String SCREEN_READER_PREFERENCE = "screen_reader_preference";
49     private static final String FONT_SIZE_PREFERENCE = "font_size_preference";
50 
51     // Preference controls.
52     private Preference mDisplayMagnificationPreference;
53     private Preference mScreenReaderPreference;
54 
55     @Override
getMetricsCategory()56     protected int getMetricsCategory() {
57         return MetricsEvent.SUW_ACCESSIBILITY;
58     }
59 
60     @Override
onCreate(Bundle icicle)61     public void onCreate(Bundle icicle) {
62         super.onCreate(icicle);
63         addPreferencesFromResource(R.xml.accessibility_settings_for_setup_wizard);
64 
65         mDisplayMagnificationPreference = findPreference(DISPLAY_MAGNIFICATION_PREFERENCE);
66         mScreenReaderPreference = findPreference(SCREEN_READER_PREFERENCE);
67     }
68 
69     @Override
onResume()70     public void onResume() {
71         super.onResume();
72         updateScreenReaderPreference();
73     }
74 
75     @Override
onActivityCreated(Bundle savedInstanceState)76     public void onActivityCreated(Bundle savedInstanceState) {
77         super.onActivityCreated(savedInstanceState);
78         setHasOptionsMenu(false);
79     }
80 
81     @Override
onPreferenceChange(Preference preference, Object newValue)82     public boolean onPreferenceChange(Preference preference, Object newValue) {
83         return false;
84     }
85 
86     @Override
onPreferenceTreeClick(Preference preference)87     public boolean onPreferenceTreeClick(Preference preference) {
88         if (mDisplayMagnificationPreference == preference) {
89             Bundle extras = mDisplayMagnificationPreference.getExtras();
90             extras.putString(AccessibilitySettings.EXTRA_TITLE,
91                     getString(R.string.accessibility_screen_magnification_title));
92             extras.putCharSequence(AccessibilitySettings.EXTRA_SUMMARY,
93                     getText(R.string.accessibility_screen_magnification_summary));
94             extras.putBoolean(AccessibilitySettings.EXTRA_CHECKED,
95                     Settings.Secure.getInt(getContentResolver(),
96                     Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, 0) == 1);
97         }
98 
99         return super.onPreferenceTreeClick(preference);
100     }
101 
findFirstServiceWithSpokenFeedback()102     private AccessibilityServiceInfo findFirstServiceWithSpokenFeedback() {
103         final AccessibilityManager manager =
104                 getActivity().getSystemService(AccessibilityManager.class);
105         final List<AccessibilityServiceInfo> accessibilityServices =
106                 manager.getInstalledAccessibilityServiceList();
107         for (AccessibilityServiceInfo info : accessibilityServices) {
108             if ((info.feedbackType & AccessibilityServiceInfo.FEEDBACK_SPOKEN) != 0) {
109                 return info;
110             }
111         }
112 
113         return null;
114     }
115 
updateScreenReaderPreference()116     private void updateScreenReaderPreference() {
117         // Find a screen reader.
118         AccessibilityServiceInfo info = findFirstServiceWithSpokenFeedback();
119         if (info == null) {
120             mScreenReaderPreference.setEnabled(false);
121         } else {
122             mScreenReaderPreference.setEnabled(true);
123         }
124 
125         ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo;
126         String title = info.getResolveInfo().loadLabel(getPackageManager()).toString();
127         mScreenReaderPreference.setTitle(title);
128 
129         ComponentName componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name);
130         mScreenReaderPreference.setKey(componentName.flattenToString());
131 
132         // Update the extras.
133         Bundle extras = mScreenReaderPreference.getExtras();
134         extras.putParcelable(AccessibilitySettings.EXTRA_COMPONENT_NAME, componentName);
135 
136         extras.putString(AccessibilitySettings.EXTRA_PREFERENCE_KEY,
137                 mScreenReaderPreference.getKey());
138         extras.putString(AccessibilitySettings.EXTRA_TITLE, title);
139 
140         String description = info.loadDescription(getPackageManager());
141         if (TextUtils.isEmpty(description)) {
142             description = getString(R.string.accessibility_service_default_description);
143         }
144         extras.putString(AccessibilitySettings.EXTRA_SUMMARY, description);
145     }
146 }
147