ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 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.accessibility; |
| 18 | |
| 19 | import static android.content.Context.MODE_PRIVATE; |
| 20 | |
| 21 | import android.content.ContentResolver; |
| 22 | import android.content.Context; |
| 23 | import android.content.SharedPreferences; |
| 24 | import android.content.res.Resources; |
| 25 | import android.provider.Settings; |
| 26 | import android.util.ArrayMap; |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 27 | |
| 28 | import androidx.lifecycle.LifecycleObserver; |
| 29 | import androidx.preference.Preference; |
| 30 | import androidx.preference.PreferenceScreen; |
| 31 | |
| 32 | import com.android.settings.R; |
| 33 | import com.android.settings.core.BasePreferenceController; |
| 34 | import com.android.settings.core.PreferenceControllerMixin; |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 35 | import com.android.settingslib.core.lifecycle.Lifecycle; |
ChenJean | 62aab02 | 2020-02-05 19:08:38 +0800 | [diff] [blame] | 36 | import com.android.settingslib.widget.LayoutPreference; |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 37 | import com.android.settingslib.widget.RadioButtonPreference; |
| 38 | |
| 39 | import java.util.Map; |
| 40 | |
| 41 | /** |
| 42 | * Controller class that controls accessibility autoclick settings. |
| 43 | */ |
| 44 | public class ToggleAutoclickPreferenceController extends BasePreferenceController implements |
ChenJean | 62aab02 | 2020-02-05 19:08:38 +0800 | [diff] [blame] | 45 | LifecycleObserver, RadioButtonPreference.OnClickListener, PreferenceControllerMixin { |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 46 | |
| 47 | private static final String CONTROL_AUTOCLICK_DELAY_SECURE = |
| 48 | Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY; |
ChenJean | 62aab02 | 2020-02-05 19:08:38 +0800 | [diff] [blame] | 49 | private static final String KEY_AUTOCLICK_CUSTOM_SEEKBAR = "autoclick_custom_seekbar"; |
| 50 | static final String KEY_DELAY_MODE = "delay_mode"; |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 51 | |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 52 | private static final int AUTOCLICK_OFF_MODE = 0; |
| 53 | private static final int AUTOCLICK_CUSTOM_MODE = 2000; |
| 54 | |
| 55 | // Pair the preference key and autoclick mode value. |
| 56 | private final Map<String, Integer> mAccessibilityAutoclickKeyToValueMap = new ArrayMap<>(); |
| 57 | |
| 58 | private SharedPreferences mSharedPreferences; |
| 59 | private final ContentResolver mContentResolver; |
| 60 | private final Resources mResources; |
| 61 | private OnChangeListener mOnChangeListener; |
| 62 | private RadioButtonPreference mDelayModePref; |
| 63 | |
| 64 | /** |
| 65 | * Seek bar preference for autoclick delay value. The seek bar has values between 0 and |
| 66 | * number of possible discrete autoclick delay values. These will have to be converted to actual |
| 67 | * delay values before saving them in settings. |
| 68 | */ |
ChenJean | 62aab02 | 2020-02-05 19:08:38 +0800 | [diff] [blame] | 69 | private LayoutPreference mSeekBerPreference; |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 70 | private int mCurrentUiAutoClickMode; |
| 71 | |
| 72 | public ToggleAutoclickPreferenceController(Context context, String preferenceKey) { |
| 73 | super(context, preferenceKey); |
| 74 | |
| 75 | mSharedPreferences = context.getSharedPreferences(context.getPackageName(), MODE_PRIVATE); |
| 76 | mContentResolver = context.getContentResolver(); |
| 77 | mResources = context.getResources(); |
| 78 | |
| 79 | setAutoclickModeToKeyMap(); |
| 80 | } |
| 81 | |
| 82 | public ToggleAutoclickPreferenceController(Context context, Lifecycle lifecycle, |
| 83 | String preferenceKey) { |
| 84 | super(context, preferenceKey); |
| 85 | |
| 86 | mSharedPreferences = context.getSharedPreferences(context.getPackageName(), MODE_PRIVATE); |
| 87 | mContentResolver = context.getContentResolver(); |
| 88 | mResources = context.getResources(); |
| 89 | |
| 90 | setAutoclickModeToKeyMap(); |
| 91 | |
| 92 | if (lifecycle != null) { |
| 93 | lifecycle.addObserver(this); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public void setOnChangeListener(OnChangeListener listener) { |
| 98 | mOnChangeListener = listener; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public int getAvailabilityStatus() { |
| 103 | return AVAILABLE; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void displayPreference(PreferenceScreen screen) { |
| 108 | super.displayPreference(screen); |
| 109 | |
| 110 | mDelayModePref = (RadioButtonPreference) |
| 111 | screen.findPreference(getPreferenceKey()); |
| 112 | mDelayModePref.setOnClickListener(this); |
ChenJean | 62aab02 | 2020-02-05 19:08:38 +0800 | [diff] [blame] | 113 | mSeekBerPreference = (LayoutPreference) screen.findPreference(KEY_AUTOCLICK_CUSTOM_SEEKBAR); |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 114 | updateState((Preference) mDelayModePref); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void onRadioButtonClicked(RadioButtonPreference preference) { |
menghanli | 771163f | 2020-04-16 22:33:06 +0800 | [diff] [blame] | 119 | final int value = mAccessibilityAutoclickKeyToValueMap.get(mPreferenceKey); |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 120 | handleRadioButtonPreferenceChange(value); |
| 121 | if (mOnChangeListener != null) { |
| 122 | mOnChangeListener.onCheckedChanged(mDelayModePref); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private void updatePreferenceCheckedState(int mode) { |
| 127 | if (mCurrentUiAutoClickMode == mode) { |
| 128 | mDelayModePref.setChecked(true); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private void updatePreferenceVisibleState(int mode) { |
ChenJean | 62aab02 | 2020-02-05 19:08:38 +0800 | [diff] [blame] | 133 | mSeekBerPreference.setVisible(mCurrentUiAutoClickMode == mode); |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void updateState(Preference preference) { |
| 138 | super.updateState(preference); |
| 139 | |
menghanli | 771163f | 2020-04-16 22:33:06 +0800 | [diff] [blame] | 140 | final boolean enabled = Settings.Secure.getInt(mContext.getContentResolver(), |
| 141 | Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, 0) == 1; |
| 142 | |
| 143 | mCurrentUiAutoClickMode = |
| 144 | enabled ? getSharedPreferenceForAutoClickMode() : AUTOCLICK_OFF_MODE; |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 145 | |
| 146 | // Reset RadioButton. |
| 147 | mDelayModePref.setChecked(false); |
menghanli | 771163f | 2020-04-16 22:33:06 +0800 | [diff] [blame] | 148 | final int mode = mAccessibilityAutoclickKeyToValueMap.get(mDelayModePref.getKey()); |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 149 | updatePreferenceCheckedState(mode); |
| 150 | updatePreferenceVisibleState(mode); |
| 151 | } |
| 152 | |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 153 | /** Listener interface handles checked event. */ |
| 154 | public interface OnChangeListener { |
| 155 | /** |
| 156 | * A hook that is called when preference checked. |
| 157 | */ |
| 158 | void onCheckedChanged(Preference preference); |
| 159 | } |
| 160 | |
| 161 | private void setAutoclickModeToKeyMap() { |
menghanli | 771163f | 2020-04-16 22:33:06 +0800 | [diff] [blame] | 162 | final String[] autoclickKeys = mResources.getStringArray( |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 163 | R.array.accessibility_autoclick_control_selector_keys); |
| 164 | |
menghanli | 771163f | 2020-04-16 22:33:06 +0800 | [diff] [blame] | 165 | final int[] autoclickValues = mResources.getIntArray( |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 166 | R.array.accessibility_autoclick_selector_values); |
| 167 | |
| 168 | final int autoclickValueCount = autoclickValues.length; |
| 169 | for (int i = 0; i < autoclickValueCount; i++) { |
| 170 | mAccessibilityAutoclickKeyToValueMap.put(autoclickKeys[i], autoclickValues[i]); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private void handleRadioButtonPreferenceChange(int preference) { |
menghanli | 771163f | 2020-04-16 22:33:06 +0800 | [diff] [blame] | 175 | putSecureInt(Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, |
| 176 | (preference != AUTOCLICK_OFF_MODE) ? /* enabled */ 1 : /* disabled */ 0); |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 177 | |
| 178 | mSharedPreferences.edit().putInt(KEY_DELAY_MODE, preference).apply(); |
| 179 | |
menghanli | 771163f | 2020-04-16 22:33:06 +0800 | [diff] [blame] | 180 | if (preference != AUTOCLICK_CUSTOM_MODE) { |
| 181 | putSecureInt(CONTROL_AUTOCLICK_DELAY_SECURE, preference); |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 182 | } |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | private void putSecureInt(String name, int value) { |
| 186 | Settings.Secure.putInt(mContentResolver, name, value); |
| 187 | } |
| 188 | |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 189 | private int getSharedPreferenceForAutoClickMode() { |
menghanli | 771163f | 2020-04-16 22:33:06 +0800 | [diff] [blame] | 190 | return mSharedPreferences.getInt(KEY_DELAY_MODE, AUTOCLICK_CUSTOM_MODE); |
ChenJean | 7afc1fa | 2020-01-03 14:32:34 +0800 | [diff] [blame] | 191 | } |
| 192 | } |