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 com.android.settings.Utils.getAdaptiveIcon; 20 import static com.android.settings.accessibility.AccessibilityUtil.AccessibilityServiceFragmentType.VOLUME_SHORTCUT_TOGGLE; 21 import static com.android.settingslib.widget.TwoTargetPreference.ICON_SIZE_MEDIUM; 22 23 import android.accessibilityservice.AccessibilityServiceInfo; 24 import android.app.settings.SettingsEnums; 25 import android.content.ComponentName; 26 import android.content.Context; 27 import android.content.pm.ServiceInfo; 28 import android.graphics.Color; 29 import android.graphics.drawable.Drawable; 30 import android.os.Bundle; 31 import android.view.LayoutInflater; 32 import android.view.View; 33 import android.view.ViewGroup; 34 import android.view.accessibility.AccessibilityManager; 35 import android.widget.LinearLayout; 36 37 import androidx.preference.Preference; 38 import androidx.recyclerview.widget.RecyclerView; 39 40 import com.android.settings.R; 41 import com.android.settings.SettingsPreferenceFragment; 42 import com.android.settingslib.RestrictedPreference; 43 44 import com.google.android.setupdesign.GlifPreferenceLayout; 45 import com.google.android.setupdesign.util.ThemeHelper; 46 47 import java.util.List; 48 49 /** 50 * Activity with the accessibility settings specific to Setup Wizard. 51 */ 52 public class AccessibilitySettingsForSetupWizard extends SettingsPreferenceFragment 53 implements Preference.OnPreferenceChangeListener { 54 55 // Preferences. 56 private static final String DISPLAY_MAGNIFICATION_PREFERENCE = 57 "screen_magnification_preference"; 58 private static final String SCREEN_READER_PREFERENCE = "screen_reader_preference"; 59 private static final String SELECT_TO_SPEAK_PREFERENCE = "select_to_speak_preference"; 60 61 // Package names and service names used to identify screen reader and SelectToSpeak services. 62 private static final String SCREEN_READER_PACKAGE_NAME = "com.google.android.marvin.talkback"; 63 private static final String SCREEN_READER_SERVICE_NAME = 64 "com.google.android.marvin.talkback.TalkBackService"; 65 private static final String SELECT_TO_SPEAK_PACKAGE_NAME = "com.google.android.marvin.talkback"; 66 private static final String SELECT_TO_SPEAK_SERVICE_NAME = 67 "com.google.android.accessibility.selecttospeak.SelectToSpeakService"; 68 69 // Preference controls. 70 private Preference mDisplayMagnificationPreference; 71 private RestrictedPreference mScreenReaderPreference; 72 private RestrictedPreference mSelectToSpeakPreference; 73 74 @Override getMetricsCategory()75 public int getMetricsCategory() { 76 return SettingsEnums.SUW_ACCESSIBILITY; 77 } 78 79 @Override onViewCreated(View view, Bundle savedInstanceState)80 public void onViewCreated(View view, Bundle savedInstanceState) { 81 super.onViewCreated(view, savedInstanceState); 82 83 final GlifPreferenceLayout layout = (GlifPreferenceLayout) view; 84 layout.setDividerInsets(Integer.MAX_VALUE, 0); 85 layout.setDescriptionText(R.string.vision_settings_description); 86 layout.setHeaderText(R.string.vision_settings_title); 87 layout.setIcon(getPrefContext().getDrawable(R.drawable.ic_accessibility_visibility)); 88 89 if (ThemeHelper.shouldApplyExtendedPartnerConfig(getActivity())) { 90 final LinearLayout headerLayout = layout.findManagedViewById(R.id.sud_layout_header); 91 headerLayout.setPadding(0, headerLayout.getPaddingTop(), 0, 92 headerLayout.getPaddingBottom()); 93 } 94 } 95 96 @Override onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)97 public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, 98 Bundle savedInstanceState) { 99 final GlifPreferenceLayout layout = (GlifPreferenceLayout) parent; 100 return layout.onCreateRecyclerView(inflater, parent, savedInstanceState); 101 } 102 103 @Override onCreate(Bundle icicle)104 public void onCreate(Bundle icicle) { 105 super.onCreate(icicle); 106 addPreferencesFromResource(R.xml.accessibility_settings_for_setup_wizard); 107 108 mDisplayMagnificationPreference = findPreference(DISPLAY_MAGNIFICATION_PREFERENCE); 109 mScreenReaderPreference = findPreference(SCREEN_READER_PREFERENCE); 110 mSelectToSpeakPreference = findPreference(SELECT_TO_SPEAK_PREFERENCE); 111 } 112 113 @Override onResume()114 public void onResume() { 115 super.onResume(); 116 updateAccessibilityServicePreference(mScreenReaderPreference, 117 SCREEN_READER_PACKAGE_NAME, SCREEN_READER_SERVICE_NAME, 118 VolumeShortcutToggleScreenReaderPreferenceFragmentForSetupWizard.class.getName()); 119 updateAccessibilityServicePreference(mSelectToSpeakPreference, 120 SELECT_TO_SPEAK_PACKAGE_NAME, SELECT_TO_SPEAK_SERVICE_NAME, 121 VolumeShortcutToggleSelectToSpeakPreferenceFragmentForSetupWizard.class.getName()); 122 configureMagnificationPreferenceIfNeeded(mDisplayMagnificationPreference); 123 } 124 125 @Override onActivityCreated(Bundle savedInstanceState)126 public void onActivityCreated(Bundle savedInstanceState) { 127 super.onActivityCreated(savedInstanceState); 128 setHasOptionsMenu(false); 129 } 130 131 @Override onPreferenceChange(Preference preference, Object newValue)132 public boolean onPreferenceChange(Preference preference, Object newValue) { 133 return false; 134 } 135 136 @Override onPreferenceTreeClick(Preference preference)137 public boolean onPreferenceTreeClick(Preference preference) { 138 if (mDisplayMagnificationPreference == preference) { 139 Bundle extras = mDisplayMagnificationPreference.getExtras(); 140 extras.putBoolean(AccessibilitySettings.EXTRA_LAUNCHED_FROM_SUW, true); 141 } 142 143 return super.onPreferenceTreeClick(preference); 144 } 145 findService(String packageName, String serviceName)146 private AccessibilityServiceInfo findService(String packageName, String serviceName) { 147 final AccessibilityManager manager = 148 getActivity().getSystemService(AccessibilityManager.class); 149 final List<AccessibilityServiceInfo> accessibilityServices = 150 manager.getInstalledAccessibilityServiceList(); 151 for (AccessibilityServiceInfo info : accessibilityServices) { 152 ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo; 153 if (packageName.equals(serviceInfo.packageName) 154 && serviceName.equals(serviceInfo.name)) { 155 return info; 156 } 157 } 158 159 return null; 160 } 161 updateAccessibilityServicePreference(RestrictedPreference preference, String packageName, String serviceName, String targetFragment)162 private void updateAccessibilityServicePreference(RestrictedPreference preference, 163 String packageName, String serviceName, String targetFragment) { 164 final AccessibilityServiceInfo info = findService(packageName, serviceName); 165 if (info == null) { 166 getPreferenceScreen().removePreference(preference); 167 return; 168 } 169 170 final ServiceInfo serviceInfo = info.getResolveInfo().serviceInfo; 171 final Drawable icon = info.getResolveInfo().loadIcon(getPackageManager()); 172 preference.setIcon(getAdaptiveIcon(getContext(), icon, Color.WHITE)); 173 preference.setIconSize(ICON_SIZE_MEDIUM); 174 final String title = info.getResolveInfo().loadLabel(getPackageManager()).toString(); 175 preference.setTitle(title); 176 final ComponentName componentName = 177 new ComponentName(serviceInfo.packageName, serviceInfo.name); 178 preference.setKey(componentName.flattenToString()); 179 if (AccessibilityUtil.getAccessibilityServiceFragmentType(info) == VOLUME_SHORTCUT_TOGGLE) { 180 preference.setFragment(targetFragment); 181 } 182 183 // Update the extras. 184 final Bundle extras = preference.getExtras(); 185 extras.putParcelable(AccessibilitySettings.EXTRA_COMPONENT_NAME, componentName); 186 187 extras.putString(AccessibilitySettings.EXTRA_PREFERENCE_KEY, 188 preference.getKey()); 189 extras.putString(AccessibilitySettings.EXTRA_TITLE, title); 190 191 final String description = info.loadDescription(getPackageManager()); 192 extras.putString(AccessibilitySettings.EXTRA_SUMMARY, description); 193 194 extras.putInt(AccessibilitySettings.EXTRA_ANIMATED_IMAGE_RES, info.getAnimatedImageRes()); 195 196 final String htmlDescription = info.loadHtmlDescription(getPackageManager()); 197 extras.putString(AccessibilitySettings.EXTRA_HTML_DESCRIPTION, htmlDescription); 198 } 199 configureMagnificationPreferenceIfNeeded(Preference preference)200 private static void configureMagnificationPreferenceIfNeeded(Preference preference) { 201 final Context context = preference.getContext(); 202 preference.setFragment( 203 ToggleScreenMagnificationPreferenceFragmentForSetupWizard.class.getName()); 204 final Bundle extras = preference.getExtras(); 205 MagnificationGesturesPreferenceController 206 .populateMagnificationGesturesPreferenceExtras(extras, context); 207 } 208 } 209