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