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