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 static android.app.Activity.RESULT_CANCELED;
20 
21 import static com.android.settings.Utils.getAdaptiveIcon;
22 import static com.android.settingslib.widget.TwoTargetPreference.ICON_SIZE_MEDIUM;
23 
24 import android.accessibilityservice.AccessibilityServiceInfo;
25 import android.app.settings.SettingsEnums;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.content.pm.ServiceInfo;
29 import android.graphics.Color;
30 import android.graphics.drawable.Drawable;
31 import android.os.Bundle;
32 import android.text.TextUtils;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.view.accessibility.AccessibilityManager;
37 
38 import androidx.annotation.VisibleForTesting;
39 import androidx.preference.Preference;
40 import androidx.recyclerview.widget.RecyclerView;
41 
42 import com.android.settings.R;
43 import com.android.settings.dashboard.DashboardFragment;
44 import com.android.settings.display.AutoBrightnessPreferenceController;
45 import com.android.settings.display.BrightnessLevelPreferenceController;
46 import com.android.settingslib.RestrictedPreference;
47 import com.android.settingslib.core.AbstractPreferenceController;
48 
49 import com.google.android.setupcompat.template.FooterBarMixin;
50 import com.google.android.setupdesign.GlifPreferenceLayout;
51 
52 import java.util.ArrayList;
53 import java.util.List;
54 
55 /**
56  * Activity with the accessibility settings specific to Setup Wizard.
57  */
58 public class AccessibilitySettingsForSetupWizard extends DashboardFragment
59         implements Preference.OnPreferenceChangeListener {
60     private static final String TAG = "AccessibilitySettingsForSetupWizard";
61 
62     // Preferences.
63     private static final String DISPLAY_MAGNIFICATION_PREFERENCE =
64             "screen_magnification_preference";
65     private static final String SCREEN_READER_PREFERENCE = "screen_reader_preference";
66     private static final String SELECT_TO_SPEAK_PREFERENCE = "select_to_speak_preference";
67 
68     // Package names and service names used to identify screen reader and SelectToSpeak services.
69     @VisibleForTesting
70     static final String SCREEN_READER_PACKAGE_NAME = "com.google.android.marvin.talkback";
71     @VisibleForTesting
72     static final String SCREEN_READER_SERVICE_NAME =
73             "com.google.android.marvin.talkback.TalkBackService";
74     @VisibleForTesting
75     static final String SELECT_TO_SPEAK_PACKAGE_NAME = "com.google.android.marvin.talkback";
76     @VisibleForTesting
77     static final String SELECT_TO_SPEAK_SERVICE_NAME =
78             "com.google.android.accessibility.selecttospeak.SelectToSpeakService";
79 
80     // Preference controls.
81     protected Preference mDisplayMagnificationPreference;
82     protected RestrictedPreference mScreenReaderPreference;
83     protected RestrictedPreference mSelectToSpeakPreference;
84 
85     @Override
getMetricsCategory()86     public int getMetricsCategory() {
87         return SettingsEnums.SUW_ACCESSIBILITY;
88     }
89 
90     @Override
onViewCreated(View view, Bundle savedInstanceState)91     public void onViewCreated(View view, Bundle savedInstanceState) {
92         super.onViewCreated(view, savedInstanceState);
93 
94         if (view instanceof GlifPreferenceLayout) {
95             final GlifPreferenceLayout layout = (GlifPreferenceLayout) view;
96             final String title = getContext().getString(R.string.vision_settings_title);
97             final String description = getContext().getString(R.string.vision_settings_description);
98             final Drawable icon = getContext().getDrawable(R.drawable.ic_accessibility_visibility);
99             AccessibilitySetupWizardUtils.updateGlifPreferenceLayout(getContext(), layout, title,
100                     description, icon);
101 
102             final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
103             AccessibilitySetupWizardUtils.setPrimaryButton(getContext(), mixin, R.string.done,
104                     () -> {
105                         setResult(RESULT_CANCELED);
106                         finish();
107                     });
108         }
109     }
110 
111     @Override
onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)112     public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent,
113             Bundle savedInstanceState) {
114         if (parent instanceof GlifPreferenceLayout) {
115             final GlifPreferenceLayout layout = (GlifPreferenceLayout) parent;
116             return layout.onCreateRecyclerView(inflater, parent, savedInstanceState);
117         }
118         return super.onCreateRecyclerView(inflater, parent, savedInstanceState);
119     }
120 
121     @Override
onCreate(Bundle icicle)122     public void onCreate(Bundle icicle) {
123         super.onCreate(icicle);
124         mDisplayMagnificationPreference = findPreference(DISPLAY_MAGNIFICATION_PREFERENCE);
125         mScreenReaderPreference = findPreference(SCREEN_READER_PREFERENCE);
126         mSelectToSpeakPreference = findPreference(SELECT_TO_SPEAK_PREFERENCE);
127     }
128 
129     @Override
onResume()130     public void onResume() {
131         super.onResume();
132         updateAccessibilityServicePreference(mScreenReaderPreference,
133                 SCREEN_READER_PACKAGE_NAME, SCREEN_READER_SERVICE_NAME);
134         updateAccessibilityServicePreference(mSelectToSpeakPreference,
135                 SELECT_TO_SPEAK_PACKAGE_NAME, SELECT_TO_SPEAK_SERVICE_NAME);
136     }
137 
138     @Override
onActivityCreated(Bundle savedInstanceState)139     public void onActivityCreated(Bundle savedInstanceState) {
140         super.onActivityCreated(savedInstanceState);
141         setHasOptionsMenu(false);
142     }
143 
144     @Override
onPreferenceChange(Preference preference, Object newValue)145     public boolean onPreferenceChange(Preference preference, Object newValue) {
146         return false;
147     }
148 
149     @Override
onPreferenceTreeClick(Preference preference)150     public boolean onPreferenceTreeClick(Preference preference) {
151         if (mDisplayMagnificationPreference == preference) {
152             Bundle extras = mDisplayMagnificationPreference.getExtras();
153             extras.putBoolean(AccessibilitySettings.EXTRA_LAUNCHED_FROM_SUW, true);
154         }
155 
156         return super.onPreferenceTreeClick(preference);
157     }
158 
159     @Override
getPreferenceScreenResId()160     protected int getPreferenceScreenResId() {
161         return R.xml.accessibility_settings_for_setup_wizard;
162     }
163 
164     @Override
getLogTag()165     protected String getLogTag() {
166         return TAG;
167     }
168 
169     @Override
createPreferenceControllers(Context context)170     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
171         final List<AbstractPreferenceController> controllers = new ArrayList<>();
172         BrightnessLevelPreferenceController brightnessLevelPreferenceController =
173                 new BrightnessLevelPreferenceController(context, getSettingsLifecycle());
174         brightnessLevelPreferenceController.setInSetupWizard(true);
175         controllers.add(brightnessLevelPreferenceController);
176         String autoBrightnessKey = context.getString(R.string.preference_key_auto_brightness);
177         AutoBrightnessPreferenceController autoBrightnessPreferenceController =
178                 new AutoBrightnessPreferenceController(context, autoBrightnessKey);
179         autoBrightnessPreferenceController.setInSetupWizard(true);
180         controllers.add(autoBrightnessPreferenceController);
181         return controllers;
182     }
183 
184     /**
185      * Returns accessibility service info by given package name and service name.
186      *
187      * @param packageName Package of accessibility service
188      * @param serviceName Class of accessibility service
189      * @return {@link AccessibilityServiceInfo} instance if available, null otherwise.
190      */
findService(String packageName, String serviceName)191     private AccessibilityServiceInfo findService(String packageName, String serviceName) {
192         final AccessibilityManager manager =
193                 getActivity().getSystemService(AccessibilityManager.class);
194         final List<AccessibilityServiceInfo> accessibilityServices =
195                 manager.getInstalledAccessibilityServiceList();
196         for (AccessibilityServiceInfo info : accessibilityServices) {
197             ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo;
198             if (TextUtils.equals(packageName, serviceInfo.packageName)
199                     && TextUtils.equals(serviceName, serviceInfo.name)) {
200                 return info;
201             }
202         }
203 
204         return null;
205     }
206 
updateAccessibilityServicePreference(RestrictedPreference preference, String packageName, String serviceName)207     private void updateAccessibilityServicePreference(RestrictedPreference preference,
208             String packageName, String serviceName) {
209         final AccessibilityServiceInfo info = findService(packageName, serviceName);
210         if (info == null) {
211             getPreferenceScreen().removePreference(preference);
212             return;
213         }
214 
215         final ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo;
216         final Drawable icon = info.getResolveInfo().loadIcon(getPackageManager());
217         preference.setIcon(getAdaptiveIcon(getContext(), icon, Color.WHITE));
218         preference.setIconSize(ICON_SIZE_MEDIUM);
219         final String title = info.getResolveInfo().loadLabel(getPackageManager()).toString();
220         preference.setTitle(title);
221         final ComponentName componentName =
222                 new ComponentName(serviceInfo.packageName, serviceInfo.name);
223         preference.setKey(componentName.flattenToString());
224 
225         // Update the extras.
226         final Bundle extras = preference.getExtras();
227         extras.putParcelable(AccessibilitySettings.EXTRA_COMPONENT_NAME, componentName);
228 
229         extras.putString(AccessibilitySettings.EXTRA_PREFERENCE_KEY,
230             preference.getKey());
231         extras.putString(AccessibilitySettings.EXTRA_TITLE, title);
232 
233         final String description = info.loadDescription(getPackageManager());
234         extras.putString(AccessibilitySettings.EXTRA_SUMMARY, description);
235 
236         extras.putInt(AccessibilitySettings.EXTRA_ANIMATED_IMAGE_RES, info.getAnimatedImageRes());
237 
238         final String htmlDescription = info.loadHtmlDescription(getPackageManager());
239         extras.putString(AccessibilitySettings.EXTRA_HTML_DESCRIPTION, htmlDescription);
240     }
241 }
242