[Physical Keyboard] Add slow keys dialog

demo: https://screenshot.googleplex.com/7CPpfHvAixe9Awr.png
Add dialog for Slow keys, which have same set up with Bounce keys.
Move dialog logic to InputSettingPreferenceController, to reuse it from
slow keys controller.

Bug: 346949547
Test: atest SettingsRoboTests
Flag: com.android.settings.keyboard.keyboard_and_touchpad_a11y_new_page_enabled

Change-Id: Ia4d74fcf3f20ed68cb252503e88d99ea79eae291
This commit is contained in:
shaoweishen
2024-10-15 04:53:55 +00:00
committed by Shaowei Shen
parent 9936dae2be
commit 7aefcf71b6
10 changed files with 254 additions and 145 deletions

View File

@@ -21,15 +21,9 @@ import android.hardware.input.InputSettings;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.LifecycleObserver;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@@ -37,23 +31,19 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settingslib.PrimarySwitchPreference;
import java.util.concurrent.TimeUnit;
public class KeyboardAccessibilityBounceKeysController extends
InputSettingPreferenceController implements
LifecycleObserver {
private static final int CUSTOM_PROGRESS_INTERVAL = 100;
private static final long MILLISECOND_IN_SECONDS = TimeUnit.SECONDS.toMillis(1);
public static final int BOUNCE_KEYS_THRESHOLD = 500;
private AlertDialog mAlertDialog;
@Nullable
private PrimarySwitchPreference mPrimaryPreference;
public KeyboardAccessibilityBounceKeysController(@NonNull Context context,
@NonNull String key) {
super(context, key);
constructDialog(context);
constructDialog(context, R.string.bounce_keys_dialog_title,
R.string.bounce_keys_dialog_subtitle);
}
@Override
@@ -87,8 +77,7 @@ public class KeyboardAccessibilityBounceKeysController extends
@Override
public boolean setChecked(boolean isChecked) {
InputSettings.setAccessibilityBounceKeysThreshold(mContext,
isChecked ? BOUNCE_KEYS_THRESHOLD : 0);
updateInputSettingKeysValue(isChecked ? BOUNCE_KEYS_THRESHOLD : 0);
return true;
}
@@ -106,94 +95,13 @@ public class KeyboardAccessibilityBounceKeysController extends
Settings.Secure.ACCESSIBILITY_BOUNCE_KEYS);
}
private void constructDialog(Context context) {
mAlertDialog = new AlertDialog.Builder(context)
.setView(R.layout.dialog_a11y_bounce_key)
.setPositiveButton(android.R.string.ok,
(dialog, which) -> {
RadioGroup radioGroup =
mAlertDialog.findViewById(R.id.bounce_key_value_group);
SeekBar seekbar = mAlertDialog.findViewById(
R.id.bounce_key_value_custom_slider);
RadioButton customRadioButton = mAlertDialog.findViewById(
R.id.bounce_key_value_custom);
int threshold;
if (customRadioButton.isChecked()) {
threshold = seekbar.getProgress() * CUSTOM_PROGRESS_INTERVAL;
} else {
int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
threshold = checkedRadioButtonId == R.id.bounce_key_value_600 ? 600
: checkedRadioButtonId == R.id.bounce_key_value_400 ? 400
: checkedRadioButtonId == R.id.bounce_key_value_200
? 200 : 0;
}
InputSettings.setAccessibilityBounceKeysThreshold(context, threshold);
})
.setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.dismiss())
.create();
mAlertDialog.setOnShowListener(dialog -> {
RadioGroup cannedValueRadioGroup = mAlertDialog.findViewById(
R.id.bounce_key_value_group);
RadioButton customRadioButton = mAlertDialog.findViewById(R.id.bounce_key_value_custom);
TextView customValueTextView = mAlertDialog.findViewById(
R.id.bounce_key_value_custom_value);
SeekBar customProgressBar = mAlertDialog.findViewById(
R.id.bounce_key_value_custom_slider);
customProgressBar.incrementProgressBy(CUSTOM_PROGRESS_INTERVAL);
customProgressBar.setProgress(1);
View customValueView = mAlertDialog.findViewById(R.id.custom_value_option);
customValueView.setOnClickListener(l -> customRadioButton.performClick());
customRadioButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
cannedValueRadioGroup.clearCheck();
}
customValueTextView.setVisibility(isChecked ? View.VISIBLE : View.GONE);
customValueTextView.setText(
progressToThresholdInSecond(customProgressBar.getProgress()));
customProgressBar.setVisibility(isChecked ? View.VISIBLE : View.GONE);
buttonView.setChecked(isChecked);
});
cannedValueRadioGroup.setOnCheckedChangeListener(
(group, checkedId) -> customRadioButton.setChecked(false));
customProgressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
customValueTextView.setText(progressToThresholdInSecond(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
initStateBasedOnThreshold(cannedValueRadioGroup, customRadioButton, customValueTextView,
customProgressBar);
});
@Override
protected void updateInputSettingKeysValue(int thresholdTimeMillis) {
InputSettings.setAccessibilityBounceKeysThreshold(mContext, thresholdTimeMillis);
}
private static String progressToThresholdInSecond(int progress) {
return String.valueOf((double) progress * CUSTOM_PROGRESS_INTERVAL
/ MILLISECOND_IN_SECONDS);
}
private void initStateBasedOnThreshold(RadioGroup cannedValueRadioGroup,
RadioButton customRadioButton, TextView customValueTextView,
SeekBar customProgressBar) {
int bounceKeysThreshold = InputSettings.getAccessibilityBounceKeysThreshold(mContext);
switch (bounceKeysThreshold) {
case 600 -> cannedValueRadioGroup.check(R.id.bounce_key_value_600);
case 400 -> cannedValueRadioGroup.check(R.id.bounce_key_value_400);
case 0, 200 -> cannedValueRadioGroup.check(R.id.bounce_key_value_200);
default -> {
customValueTextView.setText(
String.valueOf(
(double) bounceKeysThreshold / MILLISECOND_IN_SECONDS));
customProgressBar.setProgress(bounceKeysThreshold / CUSTOM_PROGRESS_INTERVAL);
customRadioButton.setChecked(true);
}
}
@Override
protected int getInputSettingKeysValue() {
return InputSettings.getAccessibilityBounceKeysThreshold(mContext);
}
}