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