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.app.settings.SettingsEnums;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.pm.ServiceInfo;
24 import android.os.Bundle;
25 import android.text.TextUtils;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.accessibility.AccessibilityManager;
30 
31 import androidx.preference.Preference;
32 import androidx.recyclerview.widget.RecyclerView;
33 
34 import com.android.settings.R;
35 import com.android.settings.SettingsPreferenceFragment;
36 
37 import com.google.android.setupdesign.GlifPreferenceLayout;
38 
39 import java.util.List;
40 
41 /**
42  * Activity with the accessibility settings specific to Setup Wizard.
43  */
44 public class AccessibilitySettingsForSetupWizard extends SettingsPreferenceFragment
45         implements Preference.OnPreferenceChangeListener {
46 
47     // Preferences.
48     private static final String DISPLAY_MAGNIFICATION_PREFERENCE =
49             "screen_magnification_preference";
50     private static final String SCREEN_READER_PREFERENCE = "screen_reader_preference";
51     private static final String SELECT_TO_SPEAK_PREFERENCE = "select_to_speak_preference";
52     private static final String FONT_SIZE_PREFERENCE = "font_size_preference";
53 
54     // Package names and service names used to identify screen reader and SelectToSpeak services.
55     private static final String SCREEN_READER_PACKAGE_NAME = "com.google.android.marvin.talkback";
56     private static final String SCREEN_READER_SERVICE_NAME =
57             "com.google.android.marvin.talkback.TalkBackService";
58     private static final String SELECT_TO_SPEAK_PACKAGE_NAME = "com.google.android.marvin.talkback";
59     private static final String SELECT_TO_SPEAK_SERVICE_NAME =
60             "com.google.android.accessibility.selecttospeak.SelectToSpeakService";
61 
62     // Preference controls.
63     private Preference mDisplayMagnificationPreference;
64     private Preference mScreenReaderPreference;
65     private Preference mSelectToSpeakPreference;
66 
67     @Override
getMetricsCategory()68     public int getMetricsCategory() {
69         return SettingsEnums.SUW_ACCESSIBILITY;
70     }
71 
72     @Override
onViewCreated(View view, Bundle savedInstanceState)73     public void onViewCreated(View view, Bundle savedInstanceState) {
74         super.onViewCreated(view, savedInstanceState);
75 
76         GlifPreferenceLayout layout = (GlifPreferenceLayout) view;
77         layout.setDividerInsets(Integer.MAX_VALUE, 0);
78 
79         layout.setHeaderText(R.string.vision_settings_title);
80     }
81 
82     @Override
onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)83     public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent,
84         Bundle savedInstanceState) {
85         GlifPreferenceLayout layout = (GlifPreferenceLayout) parent;
86         return layout.onCreateRecyclerView(inflater, parent, savedInstanceState);
87     }
88 
89     @Override
onCreate(Bundle icicle)90     public void onCreate(Bundle icicle) {
91         super.onCreate(icicle);
92         addPreferencesFromResource(R.xml.accessibility_settings_for_setup_wizard);
93 
94         mDisplayMagnificationPreference = findPreference(DISPLAY_MAGNIFICATION_PREFERENCE);
95         mScreenReaderPreference = findPreference(SCREEN_READER_PREFERENCE);
96         mSelectToSpeakPreference = findPreference(SELECT_TO_SPEAK_PREFERENCE);
97     }
98 
99     @Override
onResume()100     public void onResume() {
101         super.onResume();
102         updateAccessibilityServicePreference(mScreenReaderPreference,
103                 findService(SCREEN_READER_PACKAGE_NAME, SCREEN_READER_SERVICE_NAME));
104         updateAccessibilityServicePreference(mSelectToSpeakPreference,
105                 findService(SELECT_TO_SPEAK_PACKAGE_NAME, SELECT_TO_SPEAK_SERVICE_NAME));
106         configureMagnificationPreferenceIfNeeded(mDisplayMagnificationPreference);
107     }
108 
109     @Override
onActivityCreated(Bundle savedInstanceState)110     public void onActivityCreated(Bundle savedInstanceState) {
111         super.onActivityCreated(savedInstanceState);
112         setHasOptionsMenu(false);
113     }
114 
115     @Override
onPreferenceChange(Preference preference, Object newValue)116     public boolean onPreferenceChange(Preference preference, Object newValue) {
117         return false;
118     }
119 
120     @Override
onPreferenceTreeClick(Preference preference)121     public boolean onPreferenceTreeClick(Preference preference) {
122         if (mDisplayMagnificationPreference == preference) {
123             Bundle extras = mDisplayMagnificationPreference.getExtras();
124             extras.putBoolean(AccessibilitySettings.EXTRA_LAUNCHED_FROM_SUW, true);
125         }
126 
127         return super.onPreferenceTreeClick(preference);
128     }
129 
findService(String packageName, String serviceName)130     private AccessibilityServiceInfo findService(String packageName, String serviceName) {
131         final AccessibilityManager manager =
132                 getActivity().getSystemService(AccessibilityManager.class);
133         final List<AccessibilityServiceInfo> accessibilityServices =
134                 manager.getInstalledAccessibilityServiceList();
135         for (AccessibilityServiceInfo info : accessibilityServices) {
136             ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo;
137             if (packageName.equals(serviceInfo.packageName)
138                     && serviceName.equals(serviceInfo.name)) {
139                 return info;
140             }
141         }
142 
143         return null;
144     }
145 
updateAccessibilityServicePreference(Preference preference, AccessibilityServiceInfo info)146     private void updateAccessibilityServicePreference(Preference preference,
147             AccessibilityServiceInfo info) {
148         if (info == null) {
149             getPreferenceScreen().removePreference(preference);
150             return;
151         }
152 
153         ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo;
154         String title = info.getResolveInfo().loadLabel(getPackageManager()).toString();
155         preference.setTitle(title);
156         ComponentName componentName = new ComponentName(serviceInfo.packageName, serviceInfo.name);
157         preference.setKey(componentName.flattenToString());
158 
159         // Update the extras.
160         Bundle extras = preference.getExtras();
161         extras.putParcelable(AccessibilitySettings.EXTRA_COMPONENT_NAME, componentName);
162 
163         extras.putString(AccessibilitySettings.EXTRA_PREFERENCE_KEY,
164             preference.getKey());
165         extras.putString(AccessibilitySettings.EXTRA_TITLE, title);
166 
167         String description = info.loadDescription(getPackageManager());
168         if (TextUtils.isEmpty(description)) {
169             description = getString(R.string.accessibility_service_default_description);
170         }
171         extras.putString(AccessibilitySettings.EXTRA_SUMMARY, description);
172     }
173 
configureMagnificationPreferenceIfNeeded(Preference preference)174     private static void configureMagnificationPreferenceIfNeeded(Preference preference) {
175         // Some devices support only a single magnification mode. In these cases, we redirect to
176         // the magnification mode's UI directly, rather than showing a PreferenceScreen with a
177         // single list item.
178         final Context context = preference.getContext();
179         if (!MagnificationPreferenceFragment.isApplicable(context.getResources())) {
180             preference.setFragment(
181                     ToggleScreenMagnificationPreferenceFragmentForSetupWizard.class.getName());
182             final Bundle extras = preference.getExtras();
183             MagnificationGesturesPreferenceController
184                     .populateMagnificationGesturesPreferenceExtras(extras, context);
185         }
186     }
187 }
188