1 /* 2 * Copyright 2024 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.inputmethod; 18 19 import static android.app.settings.SettingsEnums.ACTION_SLOW_KEYS_DISABLED; 20 import static android.app.settings.SettingsEnums.ACTION_SLOW_KEYS_ENABLED; 21 22 import android.content.Context; 23 import android.hardware.input.InputSettings; 24 import android.net.Uri; 25 import android.provider.Settings; 26 import android.text.TextUtils; 27 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 import androidx.lifecycle.LifecycleObserver; 31 import androidx.preference.Preference; 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.settingslib.PrimarySwitchPreference; 35 36 public class KeyboardAccessibilitySlowKeysController extends 37 InputSettingPreferenceController implements 38 LifecycleObserver { 39 public static final int SLOW_KEYS_THRESHOLD = 500; 40 private static final String KEY_TAG = "slow_keys_dialog_tag"; 41 42 @Nullable 43 private PrimarySwitchPreference mPrimarySwitchPreference; 44 KeyboardAccessibilitySlowKeysController(@onNull Context context, @NonNull String key)45 public KeyboardAccessibilitySlowKeysController(@NonNull Context context, @NonNull String key) { 46 super(context, key); 47 } 48 49 @Override displayPreference(@onNull PreferenceScreen screen)50 public void displayPreference(@NonNull PreferenceScreen screen) { 51 super.displayPreference(screen); 52 mPrimarySwitchPreference = screen.findPreference(getPreferenceKey()); 53 } 54 55 @Override isChecked()56 public boolean isChecked() { 57 return InputSettings.isAccessibilitySlowKeysEnabled(mContext); 58 } 59 60 @Override setChecked(boolean isChecked)61 public boolean setChecked(boolean isChecked) { 62 updateInputSettingKeysValue(isChecked ? SLOW_KEYS_THRESHOLD : 0); 63 mMetricsFeatureProvider.action(mContext, 64 isChecked ? ACTION_SLOW_KEYS_ENABLED : ACTION_SLOW_KEYS_DISABLED); 65 return true; 66 } 67 68 @Override onInputSettingUpdated()69 protected void onInputSettingUpdated() { 70 if (mPrimarySwitchPreference != null) { 71 mPrimarySwitchPreference.setChecked( 72 InputSettings.isAccessibilitySlowKeysEnabled(mContext)); 73 } 74 } 75 76 @Override getSettingUri()77 protected Uri getSettingUri() { 78 return Settings.Secure.getUriFor( 79 Settings.Secure.ACCESSIBILITY_SLOW_KEYS); 80 } 81 82 @Override handlePreferenceTreeClick(@onNull Preference preference)83 public boolean handlePreferenceTreeClick(@NonNull Preference preference) { 84 if (!TextUtils.equals(preference.getKey(), getPreferenceKey()) 85 || mFragmentManager == null) { 86 return false; 87 } 88 KeyboardAccessibilitySlowKeysDialogFragment.getInstance().show(mFragmentManager, KEY_TAG); 89 return true; 90 } 91 92 @Override updateInputSettingKeysValue(int thresholdTimeMillis)93 protected void updateInputSettingKeysValue(int thresholdTimeMillis) { 94 InputSettings.setAccessibilitySlowKeysThreshold(mContext, thresholdTimeMillis); 95 } 96 } 97