1 /* 2 * Copyright (C) 2017 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.res.Resources; 24 import android.os.Bundle; 25 import android.provider.SearchIndexableResource; 26 import android.provider.Settings; 27 import android.support.annotation.VisibleForTesting; 28 import android.support.v7.preference.Preference; 29 import android.text.TextUtils; 30 import android.view.accessibility.AccessibilityManager; 31 32 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 33 import com.android.settings.R; 34 import com.android.settings.dashboard.DashboardFragment; 35 import com.android.settings.search.BaseSearchIndexProvider; 36 import com.android.settings.search.Indexable; 37 import com.android.settings.search.actionbar.SearchMenuController; 38 import com.android.settings.support.actionbar.HelpResourceProvider; 39 40 import java.util.Arrays; 41 import java.util.List; 42 43 public final class MagnificationPreferenceFragment extends DashboardFragment { 44 @VisibleForTesting 45 static final int ON = 1; 46 @VisibleForTesting 47 static final int OFF = 0; 48 49 private static final String TAG = "MagnificationPreferenceFragment"; 50 51 // Settings App preference keys 52 private static final String PREFERENCE_TITLE_KEY = "magnification_preference_screen_title"; 53 54 // Pseudo ComponentName used to represent navbar magnification in Settings.Secure. 55 private static final String MAGNIFICATION_COMPONENT_ID = 56 "com.android.server.accessibility.MagnificationController"; 57 58 private boolean mLaunchedFromSuw = false; 59 60 @Override 61 public int getMetricsCategory() { 62 return MetricsEvent.ACCESSIBILITY_SCREEN_MAGNIFICATION_SETTINGS; 63 } 64 65 @Override 66 protected String getLogTag() { 67 return TAG; 68 } 69 70 @Override 71 public int getHelpResource() { 72 return R.string.help_url_magnification; 73 } 74 75 @Override 76 protected int getPreferenceScreenResId() { 77 return R.xml.accessibility_magnification_settings; 78 } 79 80 @Override 81 public void onAttach(Context context) { 82 super.onAttach(context); 83 final Bundle args = getArguments(); 84 if ((args != null) && args.containsKey(AccessibilitySettings.EXTRA_LAUNCHED_FROM_SUW)) { 85 mLaunchedFromSuw = args.getBoolean(AccessibilitySettings.EXTRA_LAUNCHED_FROM_SUW); 86 } 87 use(MagnificationGesturesPreferenceController.class) 88 .setIsFromSUW(mLaunchedFromSuw); 89 use(MagnificationNavbarPreferenceController.class) 90 .setIsFromSUW(mLaunchedFromSuw); 91 } 92 93 @Override 94 public boolean onPreferenceTreeClick(Preference preference) { 95 if (mLaunchedFromSuw) { 96 // If invoked from SUW, redirect to fragment instrumented for Vision Settings metrics 97 preference.setFragment( 98 ToggleScreenMagnificationPreferenceFragmentForSetupWizard.class.getName()); 99 Bundle args = preference.getExtras(); 100 // Copy from AccessibilitySettingsForSetupWizardActivity, hide search and help menu 101 args.putInt(HelpResourceProvider.HELP_URI_RESOURCE_KEY, 0); 102 args.putBoolean(SearchMenuController.NEED_SEARCH_ICON_IN_ACTION_BAR, false); 103 } 104 return super.onPreferenceTreeClick(preference); 105 } 106 107 static CharSequence getConfigurationWarningStringForSecureSettingsKey(String key, 108 Context context) { 109 if (!Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED.equals(key)) { 110 return null; 111 } 112 if (Settings.Secure.getInt(context.getContentResolver(), 113 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED, 0) == 0) { 114 return null; 115 } 116 final AccessibilityManager am = (AccessibilityManager) context.getSystemService( 117 Context.ACCESSIBILITY_SERVICE); 118 final String assignedId = Settings.Secure.getString(context.getContentResolver(), 119 Settings.Secure.ACCESSIBILITY_BUTTON_TARGET_COMPONENT); 120 if (!TextUtils.isEmpty(assignedId) && !MAGNIFICATION_COMPONENT_ID.equals(assignedId)) { 121 final ComponentName assignedComponentName = ComponentName.unflattenFromString( 122 assignedId); 123 final List<AccessibilityServiceInfo> activeServices = 124 am.getEnabledAccessibilityServiceList( 125 AccessibilityServiceInfo.FEEDBACK_ALL_MASK); 126 final int serviceCount = activeServices.size(); 127 for (int i = 0; i < serviceCount; i++) { 128 final AccessibilityServiceInfo info = activeServices.get(i); 129 if (info.getComponentName().equals(assignedComponentName)) { 130 final CharSequence assignedServiceName = info.getResolveInfo().loadLabel( 131 context.getPackageManager()); 132 return context.getString( 133 R.string.accessibility_screen_magnification_navbar_configuration_warning, 134 assignedServiceName); 135 } 136 } 137 } 138 return null; 139 } 140 141 static boolean isChecked(ContentResolver contentResolver, String settingsKey) { 142 return Settings.Secure.getInt(contentResolver, settingsKey, OFF) == ON; 143 } 144 145 static boolean setChecked(ContentResolver contentResolver, String settingsKey, 146 boolean isChecked) { 147 return Settings.Secure.putInt(contentResolver, settingsKey, isChecked ? ON : OFF); 148 } 149 150 /** 151 * @return {@code true} if this fragment should be shown, {@code false} otherwise. This 152 * fragment is shown in the case that more than one magnification mode is available. 153 */ 154 static boolean isApplicable(Resources res) { 155 return res.getBoolean(com.android.internal.R.bool.config_showNavigationBar); 156 } 157 158 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 159 new BaseSearchIndexProvider() { 160 @Override 161 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, 162 boolean enabled) { 163 final SearchIndexableResource sir = new SearchIndexableResource(context); 164 sir.xmlResId = R.xml.accessibility_magnification_settings; 165 return Arrays.asList(sir); 166 } 167 168 @Override 169 protected boolean isPageSearchEnabled(Context context) { 170 return isApplicable(context.getResources()); 171 } 172 173 @Override 174 public List<String> getNonIndexableKeys(Context context) { 175 List<String> keys = super.getNonIndexableKeys(context); 176 keys.add(PREFERENCE_TITLE_KEY); 177 return keys; 178 } 179 }; 180 } 181