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 
26 import com.android.internal.accessibility.AccessibilityShortcutController;
27 import com.android.settings.R;
28 import com.android.settings.accessibility.AccessibilityShortcutPreferenceFragment;
29 import com.android.settings.accessibility.ShortcutPreference;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settingslib.widget.IllustrationPreference;
32 
33 /**
34  * Fragment for One-handed mode settings
35  *
36  * <p>The child {@link AccessibilityShortcutPreferenceFragment} shows the actual UI for
37  * providing basic accessibility shortcut service setup.
38  */
39 public class OneHandedSettings extends AccessibilityShortcutPreferenceFragment {
40 
41     private static final String ONE_HANDED_SHORTCUT_KEY = "one_handed_shortcuts_preference";
42     private static final String ONE_HANDED_ILLUSTRATION_KEY = "one_handed_header";
43     private String mFeatureName;
44     private OneHandedSettingsUtils mUtils;
45 
46     @Override
updatePreferenceStates()47     protected void updatePreferenceStates() {
48         OneHandedSettingsUtils.setUserId(UserHandle.myUserId());
49         super.updatePreferenceStates();
50 
51         final IllustrationPreference preference =
52                 (IllustrationPreference) getPreferenceScreen().findPreference(
53                         ONE_HANDED_ILLUSTRATION_KEY);
54         if (preference != null) {
55             final boolean isSwipeDownNotification =
56                     OneHandedSettingsUtils.isSwipeDownNotificationEnabled(getContext());
57             preference.setLottieAnimationResId(
58                     isSwipeDownNotification ? R.raw.lottie_swipe_for_notifications
59                             : R.raw.lottie_one_hand_mode);
60         }
61     }
62 
63     @Override
getDialogMetricsCategory(int dialogId)64     public int getDialogMetricsCategory(int dialogId) {
65         final int dialogMetrics = super.getDialogMetricsCategory(dialogId);
66         return dialogMetrics == SettingsEnums.ACTION_UNKNOWN ? SettingsEnums.SETTINGS_ONE_HANDED
67                 : dialogMetrics;
68     }
69 
70     @Override
getMetricsCategory()71     public int getMetricsCategory() {
72         return SettingsEnums.SETTINGS_ONE_HANDED;
73     }
74 
75     @Override
getShortcutPreferenceKey()76     protected String getShortcutPreferenceKey() {
77         return ONE_HANDED_SHORTCUT_KEY;
78     }
79 
80     @Override
updateShortcutTitle(ShortcutPreference shortcutPreference)81     protected void updateShortcutTitle(ShortcutPreference shortcutPreference) {
82         shortcutPreference.setTitle(R.string.one_handed_mode_shortcut_title);
83     }
84 
85     @Override
showGeneralCategory()86     protected boolean showGeneralCategory() {
87         return true;
88     }
89 
90     @Override
onStart()91     public void onStart() {
92         super.onStart();
93         mUtils = new OneHandedSettingsUtils(this.getContext());
94         mUtils.registerToggleAwareObserver(uri -> {
95             Activity activity = getActivity();
96             if (activity != null) {
97                 activity.runOnUiThread(() -> updatePreferenceStates());
98             }
99         });
100     }
101 
102     @Override
onStop()103     public void onStop() {
104         super.onStop();
105         mUtils.unregisterToggleAwareObserver();
106     }
107 
108     @Override
getComponentName()109     protected ComponentName getComponentName() {
110         return AccessibilityShortcutController.ONE_HANDED_COMPONENT_NAME;
111     }
112 
113     @Override
getLabelName()114     protected CharSequence getLabelName() {
115         return mFeatureName;
116     }
117 
118     @Override
getPreferenceScreenResId()119     protected int getPreferenceScreenResId() {
120         return R.xml.one_handed_settings;
121     }
122 
123     @Override
getLogTag()124     protected String getLogTag() {
125         return null;
126     }
127 
128     @Override
onCreate(Bundle savedInstanceState)129     public void onCreate(Bundle savedInstanceState) {
130         mFeatureName = getContext().getString(R.string.one_handed_title);
131         super.onCreate(savedInstanceState);
132     }
133 
134     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
135             new BaseSearchIndexProvider(R.xml.one_handed_settings) {
136                 @Override
137                 protected boolean isPageSearchEnabled(Context context) {
138                     return OneHandedSettingsUtils.isSupportOneHandedMode();
139                 }
140             };
141 }
142