1 /* 2 * Copyright (C) 2022 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 android.app.Activity; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.widget.SeekBar; 25 26 import androidx.annotation.NonNull; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.settings.R; 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settings.widget.LabeledSeekBarPreference; 32 import com.android.settingslib.core.lifecycle.LifecycleObserver; 33 import com.android.settingslib.core.lifecycle.events.OnCreate; 34 import com.android.settingslib.core.lifecycle.events.OnDestroy; 35 import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState; 36 37 import com.google.android.setupcompat.util.WizardManagerHelper; 38 39 import java.util.Optional; 40 41 /** 42 * The controller of {@link LabeledSeekBarPreference} that listens to display size and font size 43 * settings changes and updates preview size threshold smoothly. 44 */ 45 abstract class PreviewSizeSeekBarController extends BasePreferenceController implements 46 TextReadingResetController.ResetStateListener, LifecycleObserver, OnCreate, 47 OnDestroy, OnSaveInstanceState { 48 private final PreviewSizeData<? extends Number> mSizeData; 49 private static final String KEY_SAVED_QS_TOOLTIP_RESHOW = "qs_tooltip_reshow"; 50 private boolean mSeekByTouch; 51 private Optional<ProgressInteractionListener> mInteractionListener = Optional.empty(); 52 private LabeledSeekBarPreference mSeekBarPreference; 53 private int mLastProgress; 54 private boolean mNeedsQSTooltipReshow = false; 55 private AccessibilityQuickSettingsTooltipWindow mTooltipWindow; 56 private final Handler mHandler; 57 58 private String[] mStateLabels = null; 59 60 private final SeekBar.OnSeekBarChangeListener mSeekBarChangeListener = 61 new SeekBar.OnSeekBarChangeListener() { 62 @Override 63 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 64 setSeekbarStateDescription(progress); 65 66 if (mInteractionListener.isEmpty()) { 67 return; 68 } 69 70 final ProgressInteractionListener interactionListener = 71 mInteractionListener.get(); 72 // Avoid timing issues to update the corresponding preview fail when clicking 73 // the increase/decrease button. 74 seekBar.post(interactionListener::notifyPreferenceChanged); 75 76 if (!mSeekByTouch) { 77 interactionListener.onProgressChanged(); 78 onProgressFinalized(); 79 } 80 } 81 82 @Override 83 public void onStartTrackingTouch(SeekBar seekBar) { 84 mSeekByTouch = true; 85 } 86 87 @Override 88 public void onStopTrackingTouch(SeekBar seekBar) { 89 mSeekByTouch = false; 90 91 mInteractionListener.ifPresent(ProgressInteractionListener::onEndTrackingTouch); 92 onProgressFinalized(); 93 } 94 }; 95 PreviewSizeSeekBarController(Context context, String preferenceKey, @NonNull PreviewSizeData<? extends Number> sizeData)96 PreviewSizeSeekBarController(Context context, String preferenceKey, 97 @NonNull PreviewSizeData<? extends Number> sizeData) { 98 super(context, preferenceKey); 99 mSizeData = sizeData; 100 mHandler = new Handler(context.getMainLooper()); 101 } 102 103 @Override onCreate(Bundle savedInstanceState)104 public void onCreate(Bundle savedInstanceState) { 105 // Restore the tooltip. 106 if (savedInstanceState != null 107 && savedInstanceState.containsKey(KEY_SAVED_QS_TOOLTIP_RESHOW)) { 108 mNeedsQSTooltipReshow = savedInstanceState.getBoolean(KEY_SAVED_QS_TOOLTIP_RESHOW); 109 } 110 } 111 112 @Override onDestroy()113 public void onDestroy() { 114 // remove runnables in the queue. 115 mHandler.removeCallbacksAndMessages(null); 116 final boolean isTooltipWindowShowing = mTooltipWindow != null && mTooltipWindow.isShowing(); 117 if (isTooltipWindowShowing) { 118 mTooltipWindow.dismiss(); 119 } 120 } 121 122 @Override onSaveInstanceState(Bundle outState)123 public void onSaveInstanceState(Bundle outState) { 124 final boolean isTooltipWindowShowing = mTooltipWindow != null && mTooltipWindow.isShowing(); 125 if (mNeedsQSTooltipReshow || isTooltipWindowShowing) { 126 outState.putBoolean(KEY_SAVED_QS_TOOLTIP_RESHOW, /* value= */ true); 127 } 128 } 129 setInteractionListener(ProgressInteractionListener interactionListener)130 void setInteractionListener(ProgressInteractionListener interactionListener) { 131 mInteractionListener = Optional.ofNullable(interactionListener); 132 } 133 134 @Override getAvailabilityStatus()135 public int getAvailabilityStatus() { 136 return AVAILABLE; 137 } 138 139 @Override displayPreference(PreferenceScreen screen)140 public void displayPreference(PreferenceScreen screen) { 141 super.displayPreference(screen); 142 143 final int dataSize = mSizeData.getValues().size(); 144 final int initialIndex = mSizeData.getInitialIndex(); 145 mLastProgress = initialIndex; 146 mSeekBarPreference = screen.findPreference(getPreferenceKey()); 147 mSeekBarPreference.setMax(dataSize - 1); 148 mSeekBarPreference.setProgress(initialIndex); 149 mSeekBarPreference.setContinuousUpdates(true); 150 mSeekBarPreference.setOnSeekBarChangeListener(mSeekBarChangeListener); 151 if (mNeedsQSTooltipReshow) { 152 mHandler.post(this::showQuickSettingsTooltipIfNeeded); 153 } 154 setSeekbarStateDescription(mSeekBarPreference.getProgress()); 155 } 156 157 @Override resetState()158 public void resetState() { 159 final int defaultProgress = mSizeData.getValues().indexOf(mSizeData.getDefaultValue()); 160 mSeekBarPreference.setProgress(defaultProgress); 161 162 // Immediately take the effect of updating the progress to avoid waiting for receiving 163 // the event to delay update. 164 mInteractionListener.ifPresent(ProgressInteractionListener::onProgressChanged); 165 } 166 167 /** 168 * Stores the String array we would like to use for describing the state of seekbar progress 169 * and updates the state description with current progress. 170 * 171 * @param labels The state descriptions to be announced for each progress. 172 */ setProgressStateLabels(String[] labels)173 public void setProgressStateLabels(String[] labels) { 174 mStateLabels = labels; 175 if (mStateLabels == null) { 176 return; 177 } 178 updateState(mSeekBarPreference); 179 } 180 181 /** 182 * Sets the state of seekbar based on current progress. The progress of seekbar is 183 * corresponding to the index of the string array. If the progress is larger than or equals 184 * to the length of the array, the state description is set to an empty string. 185 */ setSeekbarStateDescription(int index)186 private void setSeekbarStateDescription(int index) { 187 if (mStateLabels == null) { 188 return; 189 } 190 mSeekBarPreference.setSeekBarStateDescription( 191 (index < mStateLabels.length) 192 ? mStateLabels[index] : ""); 193 } 194 195 private void onProgressFinalized() { 196 // Using progress in SeekBarPreference since the progresses in 197 // SeekBarPreference and seekbar are not always the same. 198 // See {@link androidx.preference.Preference#callChangeListener(Object)} 199 int seekBarPreferenceProgress = mSeekBarPreference.getProgress(); 200 if (seekBarPreferenceProgress != mLastProgress) { 201 showQuickSettingsTooltipIfNeeded(); 202 mLastProgress = seekBarPreferenceProgress; 203 } 204 } 205 206 private void showQuickSettingsTooltipIfNeeded() { 207 final ComponentName tileComponentName = getTileComponentName(); 208 if (tileComponentName == null) { 209 // Returns if no tile service assigned. 210 return; 211 } 212 213 if (mContext instanceof Activity 214 && WizardManagerHelper.isAnySetupWizard(((Activity) mContext).getIntent())) { 215 // Don't show QuickSettingsTooltip in Setup Wizard 216 return; 217 } 218 219 if (!mNeedsQSTooltipReshow && AccessibilityQuickSettingUtils.hasValueInSharedPreferences( 220 mContext, tileComponentName)) { 221 // Returns if quick settings tooltip only show once. 222 return; 223 } 224 225 // TODO (287728819): Move tooltip showing to SystemUI 226 // Since the lifecycle of controller is independent of that of the preference, doing 227 // null check on seekbar is a temporary solution for the case that seekbar view 228 // is not ready when we would like to show the tooltip. If the seekbar is not ready, 229 // we give up showing the tooltip and also do not reshow it in the future. 230 if (mSeekBarPreference.getSeekbar() != null) { 231 mTooltipWindow = new AccessibilityQuickSettingsTooltipWindow(mContext); 232 mTooltipWindow.setup(getTileTooltipContent(), 233 R.drawable.accessibility_auto_added_qs_tooltip_illustration); 234 mTooltipWindow.showAtTopCenter(mSeekBarPreference.getSeekbar()); 235 } 236 AccessibilityQuickSettingUtils.optInValueToSharedPreferences(mContext, 237 tileComponentName); 238 mNeedsQSTooltipReshow = false; 239 } 240 241 /** Returns the accessibility Quick Settings tile component name. */ 242 abstract ComponentName getTileComponentName(); 243 244 /** Returns accessibility Quick Settings tile tooltip content. */ 245 abstract CharSequence getTileTooltipContent(); 246 247 248 /** 249 * Interface for callbacks when users interact with the seek bar. 250 */ 251 interface ProgressInteractionListener { 252 253 /** 254 * Called when the progress is changed. 255 */ 256 void notifyPreferenceChanged(); 257 258 /** 259 * Called when the progress is changed without tracking touch. 260 */ 261 void onProgressChanged(); 262 263 /** 264 * Called when the seek bar is end tracking. 265 */ 266 void onEndTrackingTouch(); 267 } 268 } 269