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