• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.internal.accessibility.AccessibilityShortcutController.MAGNIFICATION_CONTROLLER_NAME;
20 import static com.android.settings.accessibility.AccessibilityDialogUtils.DialogEnums;
21 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
22 import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
23 
24 import android.app.Dialog;
25 import android.app.settings.SettingsEnums;
26 import android.content.ContentResolver;
27 import android.content.Context;
28 import android.content.DialogInterface;
29 import android.icu.text.CaseMap;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.provider.Settings;
33 import android.text.TextUtils;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.view.accessibility.AccessibilityManager;
38 import android.view.accessibility.AccessibilityManager.TouchExplorationStateChangeListener;
39 import android.widget.CheckBox;
40 
41 import androidx.appcompat.app.AlertDialog;
42 import androidx.preference.Preference;
43 import androidx.preference.PreferenceCategory;
44 
45 import com.android.internal.annotations.VisibleForTesting;
46 import com.android.settings.DialogCreatable;
47 import com.android.settings.R;
48 import com.android.settings.accessibility.AccessibilityDialogUtils.DialogType;
49 import com.android.settings.accessibility.AccessibilityUtil.UserShortcutType;
50 import com.android.settings.utils.LocaleUtils;
51 
52 import com.google.android.setupcompat.util.WizardManagerHelper;
53 
54 import java.util.ArrayList;
55 import java.util.List;
56 import java.util.Locale;
57 import java.util.StringJoiner;
58 
59 /**
60  * Fragment that shows the actual UI for providing basic magnification accessibility service setup
61  * and does not have toggle bar to turn on service to use.
62  */
63 public class ToggleScreenMagnificationPreferenceFragment extends
64         ToggleFeaturePreferenceFragment implements
65         MagnificationModePreferenceController.DialogHelper {
66     // TODO(b/147021230): Move duplicated functions with android/internal/accessibility into util.
67     private TouchExplorationStateChangeListener mTouchExplorationStateChangeListener;
68 
69     private CheckBox mSoftwareTypeCheckBox;
70     private CheckBox mHardwareTypeCheckBox;
71     private CheckBox mTripleTapTypeCheckBox;
72 
73     private static final char COMPONENT_NAME_SEPARATOR = ':';
74     private static final TextUtils.SimpleStringSplitter sStringColonSplitter =
75             new TextUtils.SimpleStringSplitter(COMPONENT_NAME_SEPARATOR);
76 
77     private MagnificationModePreferenceController mModePreferenceController;
78     private DialogCreatable mDialogDelegate;
79 
80     @Override
onCreate(Bundle savedInstanceState)81     public void onCreate(Bundle savedInstanceState) {
82         super.onCreate(savedInstanceState);
83         getActivity().setTitle(R.string.accessibility_screen_magnification_title);
84     }
85 
86     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)87     public View onCreateView(LayoutInflater inflater, ViewGroup container,
88             Bundle savedInstanceState) {
89         mPackageName = getString(R.string.accessibility_screen_magnification_title);
90         mImageUri = new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
91                 .authority(getPrefContext().getPackageName())
92                 .appendPath(String.valueOf(R.raw.accessibility_magnification_banner))
93                 .build();
94         mTouchExplorationStateChangeListener = isTouchExplorationEnabled -> {
95             removeDialog(DialogEnums.EDIT_SHORTCUT);
96             mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
97         };
98         return super.onCreateView(inflater, container, savedInstanceState);
99     }
100 
101     @Override
onResume()102     public void onResume() {
103         super.onResume();
104 
105         final AccessibilityManager am = getPrefContext().getSystemService(
106                 AccessibilityManager.class);
107         am.addTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
108     }
109 
110     @Override
onPause()111     public void onPause() {
112         final AccessibilityManager am = getPrefContext().getSystemService(
113                 AccessibilityManager.class);
114         am.removeTouchExplorationStateChangeListener(mTouchExplorationStateChangeListener);
115 
116         super.onPause();
117     }
118 
119     @Override
onCreateDialog(int dialogId)120     public Dialog onCreateDialog(int dialogId) {
121         if (mDialogDelegate != null) {
122             final Dialog dialog = mDialogDelegate.onCreateDialog(dialogId);
123             if (dialog != null) {
124                 return dialog;
125             }
126         }
127         final AlertDialog dialog;
128         switch (dialogId) {
129             case DialogEnums.GESTURE_NAVIGATION_TUTORIAL:
130                 return AccessibilityGestureNavigationTutorial
131                         .showGestureNavigationTutorialDialog(getPrefContext());
132             case DialogEnums.MAGNIFICATION_EDIT_SHORTCUT:
133                 final CharSequence dialogTitle = getPrefContext().getString(
134                         R.string.accessibility_shortcut_title, mPackageName);
135                 final int dialogType = WizardManagerHelper.isAnySetupWizard(getIntent())
136                         ? DialogType.EDIT_SHORTCUT_MAGNIFICATION_SUW
137                         : DialogType.EDIT_SHORTCUT_MAGNIFICATION;
138                 dialog = AccessibilityDialogUtils.showEditShortcutDialog(getPrefContext(),
139                         dialogType, dialogTitle, this::callOnAlertDialogCheckboxClicked);
140                 setupMagnificationEditShortcutDialog(dialog);
141                 return dialog;
142             default:
143                 return super.onCreateDialog(dialogId);
144         }
145     }
146 
147     @Override
initSettingsPreference()148     protected void initSettingsPreference() {
149         // If the device doesn't support magnification area, it should hide the settings preference.
150         if (!getContext().getResources().getBoolean(
151                 com.android.internal.R.bool.config_magnification_area)) {
152             return;
153         }
154         mSettingsPreference = new Preference(getPrefContext());
155         mSettingsPreference.setTitle(R.string.accessibility_magnification_mode_title);
156         mSettingsPreference.setKey(MagnificationModePreferenceController.PREF_KEY);
157         mSettingsPreference.setPersistent(false);
158 
159         final PreferenceCategory generalCategory = findPreference(KEY_GENERAL_CATEGORY);
160         generalCategory.addPreference(mSettingsPreference);
161 
162         mModePreferenceController = new MagnificationModePreferenceController(getContext(),
163                 MagnificationModePreferenceController.PREF_KEY);
164         mModePreferenceController.setDialogHelper(this);
165         getSettingsLifecycle().addObserver(mModePreferenceController);
166         mModePreferenceController.displayPreference(getPreferenceScreen());
167     }
168 
169     @Override
showDialog(int dialogId)170     public void showDialog(int dialogId) {
171         super.showDialog(dialogId);
172     }
173 
174     @Override
setDialogDelegate(DialogCreatable delegate)175     public void setDialogDelegate(DialogCreatable delegate) {
176         mDialogDelegate = delegate;
177     }
178 
179     @Override
getShortcutTypeCheckBoxValue()180     protected int getShortcutTypeCheckBoxValue() {
181         if (mSoftwareTypeCheckBox == null || mHardwareTypeCheckBox == null) {
182             return NOT_SET;
183         }
184 
185         int value = UserShortcutType.EMPTY;
186         if (mSoftwareTypeCheckBox.isChecked()) {
187             value |= UserShortcutType.SOFTWARE;
188         }
189         if (mHardwareTypeCheckBox.isChecked()) {
190             value |= UserShortcutType.HARDWARE;
191         }
192         if (mTripleTapTypeCheckBox.isChecked()) {
193             value |= UserShortcutType.TRIPLETAP;
194         }
195         return value;
196     }
197 
198     @VisibleForTesting
setupMagnificationEditShortcutDialog(AlertDialog dialog)199     void setupMagnificationEditShortcutDialog(AlertDialog dialog) {
200         final View dialogSoftwareView = dialog.findViewById(R.id.software_shortcut);
201         mSoftwareTypeCheckBox = dialogSoftwareView.findViewById(R.id.checkbox);
202         setDialogTextAreaClickListener(dialogSoftwareView, mSoftwareTypeCheckBox);
203 
204         final View dialogHardwareView = dialog.findViewById(R.id.hardware_shortcut);
205         mHardwareTypeCheckBox = dialogHardwareView.findViewById(R.id.checkbox);
206         setDialogTextAreaClickListener(dialogHardwareView, mHardwareTypeCheckBox);
207 
208         final View dialogTripleTapView = dialog.findViewById(R.id.triple_tap_shortcut);
209         mTripleTapTypeCheckBox = dialogTripleTapView.findViewById(R.id.checkbox);
210         setDialogTextAreaClickListener(dialogTripleTapView, mTripleTapTypeCheckBox);
211 
212         final View advancedView = dialog.findViewById(R.id.advanced_shortcut);
213         if (mTripleTapTypeCheckBox.isChecked()) {
214             advancedView.setVisibility(View.GONE);
215             dialogTripleTapView.setVisibility(View.VISIBLE);
216         }
217 
218         updateMagnificationEditShortcutDialogCheckBox();
219     }
220 
setDialogTextAreaClickListener(View dialogView, CheckBox checkBox)221     private void setDialogTextAreaClickListener(View dialogView, CheckBox checkBox) {
222         final View dialogTextArea = dialogView.findViewById(R.id.container);
223         dialogTextArea.setOnClickListener(v -> checkBox.toggle());
224     }
225 
updateMagnificationEditShortcutDialogCheckBox()226     private void updateMagnificationEditShortcutDialogCheckBox() {
227         // If it is during onConfigChanged process then restore the value, or get the saved value
228         // when shortcutPreference is checked.
229         int value = restoreOnConfigChangedValue();
230         if (value == NOT_SET) {
231             final int lastNonEmptyUserShortcutType = PreferredShortcuts.retrieveUserShortcutType(
232                     getPrefContext(), MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE);
233             value = mShortcutPreference.isChecked() ? lastNonEmptyUserShortcutType
234                     : UserShortcutType.EMPTY;
235         }
236 
237         mSoftwareTypeCheckBox.setChecked(
238                 hasShortcutType(value, UserShortcutType.SOFTWARE));
239         mHardwareTypeCheckBox.setChecked(
240                 hasShortcutType(value, UserShortcutType.HARDWARE));
241         mTripleTapTypeCheckBox.setChecked(
242                 hasShortcutType(value, UserShortcutType.TRIPLETAP));
243     }
244 
restoreOnConfigChangedValue()245     private int restoreOnConfigChangedValue() {
246         final int savedValue = mSavedCheckBoxValue;
247         mSavedCheckBoxValue = NOT_SET;
248         return savedValue;
249     }
250 
hasShortcutType(int value, @UserShortcutType int type)251     private boolean hasShortcutType(int value, @UserShortcutType int type) {
252         return (value & type) == type;
253     }
254 
255     @Override
getShortcutTypeSummary(Context context)256     protected CharSequence getShortcutTypeSummary(Context context) {
257         if (!mShortcutPreference.isChecked()) {
258             return context.getText(R.string.switch_off_text);
259         }
260 
261         final int shortcutTypes = PreferredShortcuts.retrieveUserShortcutType(context,
262                 MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE);
263 
264         final List<CharSequence> list = new ArrayList<>();
265         final CharSequence softwareTitle = context.getText(
266                 R.string.accessibility_shortcut_edit_summary_software);
267 
268         if (hasShortcutType(shortcutTypes, UserShortcutType.SOFTWARE)) {
269             list.add(softwareTitle);
270         }
271         if (hasShortcutType(shortcutTypes, UserShortcutType.HARDWARE)) {
272             final CharSequence hardwareTitle = context.getText(
273                     R.string.accessibility_shortcut_hardware_keyword);
274             list.add(hardwareTitle);
275         }
276 
277         if (hasShortcutType(shortcutTypes, UserShortcutType.TRIPLETAP)) {
278             final CharSequence tripleTapTitle = context.getText(
279                     R.string.accessibility_shortcut_triple_tap_keyword);
280             list.add(tripleTapTitle);
281         }
282 
283         // Show software shortcut if first time to use.
284         if (list.isEmpty()) {
285             list.add(softwareTitle);
286         }
287 
288         return CaseMap.toTitle().wholeString().noLowercase().apply(Locale.getDefault(), /* iter= */
289                 null, LocaleUtils.getConcatenatedString(list));
290     }
291 
292     @Override
callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which)293     protected void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
294         final int value = getShortcutTypeCheckBoxValue();
295 
296         saveNonEmptyUserShortcutType(value);
297         optInAllMagnificationValuesToSettings(getPrefContext(), value);
298         optOutAllMagnificationValuesFromSettings(getPrefContext(), ~value);
299         mShortcutPreference.setChecked(value != UserShortcutType.EMPTY);
300         mShortcutPreference.setSummary(
301                 getShortcutTypeSummary(getPrefContext()));
302     }
303 
304     @Override
getHelpResource()305     public int getHelpResource() {
306         return R.string.help_url_magnification;
307     }
308 
309     @Override
getMetricsCategory()310     public int getMetricsCategory() {
311         // TODO: Distinguish between magnification modes
312         return SettingsEnums.ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFICATION;
313     }
314 
315     @Override
getDialogMetricsCategory(int dialogId)316     public int getDialogMetricsCategory(int dialogId) {
317         if (mDialogDelegate != null) {
318             final int category = mDialogDelegate.getDialogMetricsCategory(dialogId);
319             if (category != 0) {
320                 return category;
321             }
322         }
323 
324         switch (dialogId) {
325             case DialogEnums.GESTURE_NAVIGATION_TUTORIAL:
326                 return SettingsEnums.DIALOG_TOGGLE_SCREEN_MAGNIFICATION_GESTURE_NAVIGATION;
327             case DialogEnums.ACCESSIBILITY_BUTTON_TUTORIAL:
328                 return SettingsEnums.DIALOG_TOGGLE_SCREEN_MAGNIFICATION_ACCESSIBILITY_BUTTON;
329             case DialogEnums.MAGNIFICATION_EDIT_SHORTCUT:
330                 return SettingsEnums.DIALOG_MAGNIFICATION_EDIT_SHORTCUT;
331             default:
332                 return super.getDialogMetricsCategory(dialogId);
333         }
334     }
335 
336     @Override
getUserShortcutTypes()337     int getUserShortcutTypes() {
338         return getUserShortcutTypeFromSettings(getPrefContext());
339     }
340 
341     @Override
onPreferenceToggled(String preferenceKey, boolean enabled)342     protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
343         if (enabled && TextUtils.equals(
344                 Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_NAVBAR_ENABLED,
345                 preferenceKey)) {
346             showDialog(DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL);
347         }
348         MagnificationPreferenceFragment.setChecked(getContentResolver(), preferenceKey, enabled);
349     }
350 
351     @Override
onInstallSwitchPreferenceToggleSwitch()352     protected void onInstallSwitchPreferenceToggleSwitch() {
353         mToggleServiceSwitchPreference.setVisible(false);
354     }
355 
356     @Override
onToggleClicked(ShortcutPreference preference)357     public void onToggleClicked(ShortcutPreference preference) {
358         final int shortcutTypes = PreferredShortcuts.retrieveUserShortcutType(getPrefContext(),
359                 MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE);
360         if (preference.isChecked()) {
361             optInAllMagnificationValuesToSettings(getPrefContext(), shortcutTypes);
362             showDialog(DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL);
363         } else {
364             optOutAllMagnificationValuesFromSettings(getPrefContext(), shortcutTypes);
365         }
366         mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
367     }
368 
369     @Override
onSettingsClicked(ShortcutPreference preference)370     public void onSettingsClicked(ShortcutPreference preference) {
371         showDialog(DialogEnums.MAGNIFICATION_EDIT_SHORTCUT);
372     }
373 
374     @Override
updateShortcutPreferenceData()375     protected void updateShortcutPreferenceData() {
376         final int shortcutTypes = getUserShortcutTypeFromSettings(getPrefContext());
377         if (shortcutTypes != UserShortcutType.EMPTY) {
378             final PreferredShortcut shortcut = new PreferredShortcut(
379                     MAGNIFICATION_CONTROLLER_NAME, shortcutTypes);
380             PreferredShortcuts.saveUserShortcutType(getPrefContext(), shortcut);
381         }
382     }
383 
384     @Override
initShortcutPreference()385     protected void initShortcutPreference() {
386         mShortcutPreference = new ShortcutPreference(getPrefContext(), null);
387         mShortcutPreference.setPersistent(false);
388         mShortcutPreference.setKey(getShortcutPreferenceKey());
389         mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
390         mShortcutPreference.setOnClickCallback(this);
391 
392         final CharSequence title = getString(R.string.accessibility_shortcut_title, mPackageName);
393         mShortcutPreference.setTitle(title);
394 
395         final PreferenceCategory generalCategory = findPreference(KEY_GENERAL_CATEGORY);
396         generalCategory.addPreference(mShortcutPreference);
397     }
398 
399     @Override
updateShortcutPreference()400     protected void updateShortcutPreference() {
401         final int shortcutTypes = PreferredShortcuts.retrieveUserShortcutType(getPrefContext(),
402                 MAGNIFICATION_CONTROLLER_NAME, UserShortcutType.SOFTWARE);
403         mShortcutPreference.setChecked(
404                 hasMagnificationValuesInSettings(getPrefContext(), shortcutTypes));
405         mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
406     }
407 
408     @VisibleForTesting
saveNonEmptyUserShortcutType(int type)409     void saveNonEmptyUserShortcutType(int type) {
410         if (type == UserShortcutType.EMPTY) {
411             return;
412         }
413 
414         final PreferredShortcut shortcut = new PreferredShortcut(
415                 MAGNIFICATION_CONTROLLER_NAME, type);
416         PreferredShortcuts.saveUserShortcutType(getPrefContext(), shortcut);
417     }
418 
419     @VisibleForTesting
optInAllMagnificationValuesToSettings(Context context, int shortcutTypes)420     static void optInAllMagnificationValuesToSettings(Context context, int shortcutTypes) {
421         if ((shortcutTypes & UserShortcutType.SOFTWARE) == UserShortcutType.SOFTWARE) {
422             optInMagnificationValueToSettings(context, UserShortcutType.SOFTWARE);
423         }
424         if (((shortcutTypes & UserShortcutType.HARDWARE) == UserShortcutType.HARDWARE)) {
425             optInMagnificationValueToSettings(context, UserShortcutType.HARDWARE);
426         }
427         if (((shortcutTypes & UserShortcutType.TRIPLETAP) == UserShortcutType.TRIPLETAP)) {
428             optInMagnificationValueToSettings(context, UserShortcutType.TRIPLETAP);
429         }
430     }
431 
optInMagnificationValueToSettings(Context context, @UserShortcutType int shortcutType)432     private static void optInMagnificationValueToSettings(Context context,
433             @UserShortcutType int shortcutType) {
434         if (shortcutType == UserShortcutType.TRIPLETAP) {
435             Settings.Secure.putInt(context.getContentResolver(),
436                     Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, ON);
437             return;
438         }
439 
440         if (hasMagnificationValueInSettings(context, shortcutType)) {
441             return;
442         }
443 
444         final String targetKey = AccessibilityUtil.convertKeyFromSettings(shortcutType);
445         final String targetString = Settings.Secure.getString(context.getContentResolver(),
446                 targetKey);
447         final StringJoiner joiner = new StringJoiner(String.valueOf(COMPONENT_NAME_SEPARATOR));
448 
449         if (!TextUtils.isEmpty(targetString)) {
450             joiner.add(targetString);
451         }
452         joiner.add(MAGNIFICATION_CONTROLLER_NAME);
453 
454         Settings.Secure.putString(context.getContentResolver(), targetKey, joiner.toString());
455     }
456 
457     @VisibleForTesting
optOutAllMagnificationValuesFromSettings(Context context, int shortcutTypes)458     static void optOutAllMagnificationValuesFromSettings(Context context,
459             int shortcutTypes) {
460         if ((shortcutTypes & UserShortcutType.SOFTWARE) == UserShortcutType.SOFTWARE) {
461             optOutMagnificationValueFromSettings(context, UserShortcutType.SOFTWARE);
462         }
463         if (((shortcutTypes & UserShortcutType.HARDWARE) == UserShortcutType.HARDWARE)) {
464             optOutMagnificationValueFromSettings(context, UserShortcutType.HARDWARE);
465         }
466         if (((shortcutTypes & UserShortcutType.TRIPLETAP) == UserShortcutType.TRIPLETAP)) {
467             optOutMagnificationValueFromSettings(context, UserShortcutType.TRIPLETAP);
468         }
469     }
470 
optOutMagnificationValueFromSettings(Context context, @UserShortcutType int shortcutType)471     private static void optOutMagnificationValueFromSettings(Context context,
472             @UserShortcutType int shortcutType) {
473         if (shortcutType == UserShortcutType.TRIPLETAP) {
474             Settings.Secure.putInt(context.getContentResolver(),
475                     Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, OFF);
476             return;
477         }
478 
479         final String targetKey = AccessibilityUtil.convertKeyFromSettings(shortcutType);
480         final String targetString = Settings.Secure.getString(context.getContentResolver(),
481                 targetKey);
482 
483         if (TextUtils.isEmpty(targetString)) {
484             return;
485         }
486 
487         final StringJoiner joiner = new StringJoiner(String.valueOf(COMPONENT_NAME_SEPARATOR));
488 
489         sStringColonSplitter.setString(targetString);
490         while (sStringColonSplitter.hasNext()) {
491             final String name = sStringColonSplitter.next();
492             if (TextUtils.isEmpty(name) || MAGNIFICATION_CONTROLLER_NAME.equals(name)) {
493                 continue;
494             }
495             joiner.add(name);
496         }
497 
498         Settings.Secure.putString(context.getContentResolver(), targetKey, joiner.toString());
499     }
500 
501     @VisibleForTesting
hasMagnificationValuesInSettings(Context context, int shortcutTypes)502     static boolean hasMagnificationValuesInSettings(Context context, int shortcutTypes) {
503         boolean exist = false;
504 
505         if ((shortcutTypes & UserShortcutType.SOFTWARE) == UserShortcutType.SOFTWARE) {
506             exist = hasMagnificationValueInSettings(context, UserShortcutType.SOFTWARE);
507         }
508         if (((shortcutTypes & UserShortcutType.HARDWARE) == UserShortcutType.HARDWARE)) {
509             exist |= hasMagnificationValueInSettings(context, UserShortcutType.HARDWARE);
510         }
511         if (((shortcutTypes & UserShortcutType.TRIPLETAP) == UserShortcutType.TRIPLETAP)) {
512             exist |= hasMagnificationValueInSettings(context, UserShortcutType.TRIPLETAP);
513         }
514         return exist;
515     }
516 
hasMagnificationValueInSettings(Context context, @UserShortcutType int shortcutType)517     private static boolean hasMagnificationValueInSettings(Context context,
518             @UserShortcutType int shortcutType) {
519         if (shortcutType == UserShortcutType.TRIPLETAP) {
520             return Settings.Secure.getInt(context.getContentResolver(),
521                     Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED, OFF) == ON;
522         }
523 
524         final String targetKey = AccessibilityUtil.convertKeyFromSettings(shortcutType);
525         final String targetString = Settings.Secure.getString(context.getContentResolver(),
526                 targetKey);
527 
528         if (TextUtils.isEmpty(targetString)) {
529             return false;
530         }
531 
532         sStringColonSplitter.setString(targetString);
533         while (sStringColonSplitter.hasNext()) {
534             final String name = sStringColonSplitter.next();
535             if (MAGNIFICATION_CONTROLLER_NAME.equals(name)) {
536                 return true;
537             }
538         }
539         return false;
540     }
541 
isWindowMagnification(Context context)542     private boolean isWindowMagnification(Context context) {
543         final int mode = Settings.Secure.getIntForUser(
544                 context.getContentResolver(),
545                 Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE,
546                 Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_FULLSCREEN,
547                 context.getContentResolver().getUserId());
548         return mode == Settings.Secure.ACCESSIBILITY_MAGNIFICATION_MODE_WINDOW;
549     }
550 
getUserShortcutTypeFromSettings(Context context)551     private static int getUserShortcutTypeFromSettings(Context context) {
552         int shortcutTypes = UserShortcutType.EMPTY;
553         if (hasMagnificationValuesInSettings(context, UserShortcutType.SOFTWARE)) {
554             shortcutTypes |= UserShortcutType.SOFTWARE;
555         }
556         if (hasMagnificationValuesInSettings(context, UserShortcutType.HARDWARE)) {
557             shortcutTypes |= UserShortcutType.HARDWARE;
558         }
559         if (hasMagnificationValuesInSettings(context, UserShortcutType.TRIPLETAP)) {
560             shortcutTypes |= UserShortcutType.TRIPLETAP;
561         }
562         return shortcutTypes;
563     }
564 
565     /**
566      * Gets the service summary of magnification.
567      *
568      * @param context The current context.
569      */
getServiceSummary(Context context)570     public static CharSequence getServiceSummary(Context context) {
571         // Get the user shortcut type from settings provider.
572         final int uerShortcutType = getUserShortcutTypeFromSettings(context);
573         return (uerShortcutType != AccessibilityUtil.UserShortcutType.EMPTY)
574                 ? context.getText(R.string.accessibility_summary_shortcut_enabled)
575                 : context.getText(R.string.accessibility_summary_shortcut_disabled);
576     }
577 }
578