• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.gestures;
18 
19 import android.app.Activity;
20 import android.app.settings.SettingsEnums;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.os.UserHandle;
25 import android.util.Log;
26 
27 import com.android.internal.accessibility.AccessibilityShortcutController;
28 import com.android.settings.R;
29 import com.android.settings.accessibility.AccessibilityShortcutPreferenceFragment;
30 import com.android.settings.accessibility.AccessibilityUtil.QuickSettingsTooltipType;
31 import com.android.settings.search.BaseSearchIndexProvider;
32 import com.android.settingslib.widget.IllustrationPreference;
33 import com.android.settingslib.widget.MainSwitchPreference;
34 
35 /**
36  * Fragment for One-handed mode settings
37  *
38  * <p>The child {@link AccessibilityShortcutPreferenceFragment} shows the actual UI for
39  * providing basic accessibility shortcut service setup.
40  */
41 public class OneHandedSettings extends AccessibilityShortcutPreferenceFragment {
42 
43     private static final String TAG = "OneHandedSettings";
44     private static final String ONE_HANDED_SHORTCUT_KEY = "one_handed_shortcuts_preference";
45     private static final String ONE_HANDED_ILLUSTRATION_KEY = "one_handed_header";
46     protected static final String ONE_HANDED_MAIN_SWITCH_KEY =
47             "gesture_one_handed_mode_enabled_main_switch";
48     private String mFeatureName;
49     private OneHandedSettingsUtils mUtils;
50 
51     /**
52      * One handed settings no need to set any restriction key for pin protected.
53      */
OneHandedSettings()54     public OneHandedSettings() {
55         super(/* restrictionKey= */ null);
56     }
57 
58     @Override
updatePreferenceStates()59     protected void updatePreferenceStates() {
60         OneHandedSettingsUtils.setUserId(UserHandle.myUserId());
61         super.updatePreferenceStates();
62 
63         final IllustrationPreference illustrationPreference =
64                 getPreferenceScreen().findPreference(ONE_HANDED_ILLUSTRATION_KEY);
65         final boolean isSwipeDownNotification =
66                 OneHandedSettingsUtils.isSwipeDownNotificationEnabled(getContext());
67         illustrationPreference.setLottieAnimationResId(
68                 isSwipeDownNotification ? R.raw.lottie_swipe_for_notifications
69                         : R.raw.lottie_one_hand_mode);
70 
71         final MainSwitchPreference mainSwitchPreference =
72                 getPreferenceScreen().findPreference(ONE_HANDED_MAIN_SWITCH_KEY);
73         mainSwitchPreference.addOnSwitchChangeListener((switchView, isChecked) -> {
74             switchView.setChecked(isChecked);
75             if (isChecked) {
76                 showQuickSettingsTooltipIfNeeded(QuickSettingsTooltipType.GUIDE_TO_DIRECT_USE);
77             }
78         });
79     }
80 
81     @Override
getDialogMetricsCategory(int dialogId)82     public int getDialogMetricsCategory(int dialogId) {
83         final int dialogMetrics = super.getDialogMetricsCategory(dialogId);
84         return dialogMetrics == SettingsEnums.ACTION_UNKNOWN ? SettingsEnums.SETTINGS_ONE_HANDED
85                 : dialogMetrics;
86     }
87 
88     @Override
getMetricsCategory()89     public int getMetricsCategory() {
90         return SettingsEnums.SETTINGS_ONE_HANDED;
91     }
92 
93     @Override
getShortcutPreferenceKey()94     protected String getShortcutPreferenceKey() {
95         return ONE_HANDED_SHORTCUT_KEY;
96     }
97 
98     @Override
getShortcutTitle()99     protected CharSequence getShortcutTitle() {
100         return getText(R.string.one_handed_mode_shortcut_title);
101     }
102 
103     @Override
showGeneralCategory()104     protected boolean showGeneralCategory() {
105         return true;
106     }
107 
108     @Override
onStart()109     public void onStart() {
110         super.onStart();
111         mUtils = new OneHandedSettingsUtils(this.getContext());
112         mUtils.registerToggleAwareObserver(uri -> {
113             Activity activity = getActivity();
114             if (activity != null) {
115                 activity.runOnUiThread(() -> updatePreferenceStates());
116             }
117         });
118     }
119 
120     @Override
onStop()121     public void onStop() {
122         super.onStop();
123         mUtils.unregisterToggleAwareObserver();
124     }
125 
126     @Override
getComponentName()127     protected ComponentName getComponentName() {
128         return AccessibilityShortcutController.ONE_HANDED_COMPONENT_NAME;
129     }
130 
131     @Override
getLabelName()132     protected CharSequence getLabelName() {
133         return mFeatureName;
134     }
135 
136     @Override
getTileComponentName()137     protected ComponentName getTileComponentName() {
138         return AccessibilityShortcutController.ONE_HANDED_TILE_COMPONENT_NAME;
139     }
140 
141     @Override
getTileTooltipContent(@uickSettingsTooltipType int type)142     protected CharSequence getTileTooltipContent(@QuickSettingsTooltipType int type) {
143         final Context context = getContext();
144         if (context == null) {
145             Log.w(TAG, "OneHandedSettings not attached to a context.");
146             return null;
147         }
148         return type == QuickSettingsTooltipType.GUIDE_TO_EDIT
149                 ? context.getText(R.string.accessibility_one_handed_mode_qs_tooltip_content)
150                 : context.getText(
151                         R.string.accessibility_one_handed_mode_auto_added_qs_tooltip_content);
152     }
153 
154     @Override
getPreferenceScreenResId()155     protected int getPreferenceScreenResId() {
156         return R.xml.one_handed_settings;
157     }
158 
159     @Override
getLogTag()160     protected String getLogTag() {
161         return TAG;
162     }
163 
164     @Override
onCreate(Bundle savedInstanceState)165     public void onCreate(Bundle savedInstanceState) {
166         mFeatureName = getContext().getString(R.string.one_handed_title);
167         super.onCreate(savedInstanceState);
168     }
169 
170     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
171             new BaseSearchIndexProvider(R.xml.one_handed_settings) {
172                 @Override
173                 protected boolean isPageSearchEnabled(Context context) {
174                     return OneHandedSettingsUtils.isSupportOneHandedMode();
175                 }
176             };
177 }
178