1 /* 2 * Copyright (C) 2015 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.res.Resources; 20 import android.os.Bundle; 21 import android.provider.Settings; 22 import android.support.v7.preference.Preference; 23 import android.view.accessibility.AccessibilityManager; 24 import android.widget.Switch; 25 26 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 27 import com.android.settings.R; 28 import com.android.settings.widget.SeekBarPreference; 29 import com.android.settings.widget.SwitchBar; 30 31 /** 32 * Fragment for preference screen for settings related to Automatically click after mouse stops 33 * feature. 34 */ 35 public class ToggleAutoclickPreferenceFragment extends ToggleFeaturePreferenceFragment 36 implements SwitchBar.OnSwitchChangeListener, Preference.OnPreferenceChangeListener { 37 38 /** Min allowed autoclick delay value. */ 39 private static final int MIN_AUTOCLICK_DELAY = 200; 40 /** Max allowed autoclick delay value. */ 41 private static final int MAX_AUTOCLICK_DELAY = 1000; 42 /** 43 * Allowed autoclick delay values are discrete. This is the difference between two allowed 44 * values. 45 */ 46 private static final int AUTOCLICK_DELAY_STEP = 100; 47 48 /** 49 * Resource ids from which autoclick preference summaries should be derived. The strings have 50 * placeholder for integer delay value. 51 */ 52 private static final int[] mAutoclickPreferenceSummaries = { 53 R.plurals.accessibilty_autoclick_preference_subtitle_extremely_short_delay, 54 R.plurals.accessibilty_autoclick_preference_subtitle_very_short_delay, 55 R.plurals.accessibilty_autoclick_preference_subtitle_short_delay, 56 R.plurals.accessibilty_autoclick_preference_subtitle_long_delay, 57 R.plurals.accessibilty_autoclick_preference_subtitle_very_long_delay 58 }; 59 60 /** 61 * Seek bar preference for autoclick delay value. The seek bar has values between 0 and 62 * number of possible discrete autoclick delay values. These will have to be converted to actual 63 * delay values before saving them in settings. 64 */ 65 private SeekBarPreference mDelay; 66 67 /** 68 * Gets string that should be used as a autoclick preference summary for provided autoclick 69 * delay. 70 * @param resources Resources from which string should be retrieved. 71 * @param delay Delay for whose value summary should be retrieved. 72 */ 73 static CharSequence getAutoclickPreferenceSummary(Resources resources, int delay) { 74 int summaryIndex = getAutoclickPreferenceSummaryIndex(delay); 75 return resources.getQuantityString( 76 mAutoclickPreferenceSummaries[summaryIndex], delay, delay); 77 } 78 79 /** 80 * Finds index of the summary that should be used for the provided autoclick delay. 81 */ 82 private static int getAutoclickPreferenceSummaryIndex(int delay) { 83 if (delay <= MIN_AUTOCLICK_DELAY) { 84 return 0; 85 } 86 if (delay >= MAX_AUTOCLICK_DELAY) { 87 return mAutoclickPreferenceSummaries.length - 1; 88 } 89 int rangeSize = (MAX_AUTOCLICK_DELAY - MIN_AUTOCLICK_DELAY) / 90 (mAutoclickPreferenceSummaries.length - 1); 91 return (delay - MIN_AUTOCLICK_DELAY) / rangeSize; 92 } 93 94 @Override 95 protected void onPreferenceToggled(String preferenceKey, boolean enabled) { 96 Settings.Secure.putInt(getContentResolver(), preferenceKey, enabled ? 1 : 0); 97 mDelay.setEnabled(enabled); 98 } 99 100 @Override 101 public int getMetricsCategory() { 102 return MetricsEvent.ACCESSIBILITY_TOGGLE_AUTOCLICK; 103 } 104 105 @Override 106 public int getHelpResource() { 107 return R.string.help_url_autoclick; 108 } 109 110 @Override 111 protected int getPreferenceScreenResId() { 112 return R.xml.accessibility_autoclick_settings; 113 } 114 115 @Override 116 public void onCreate(Bundle savedInstanceState) { 117 super.onCreate(savedInstanceState); 118 119 int delay = Settings.Secure.getInt( 120 getContentResolver(), Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY, 121 AccessibilityManager.AUTOCLICK_DELAY_DEFAULT); 122 123 // Initialize seek bar preference. Sets seek bar size to the number of possible delay 124 // values. 125 mDelay = (SeekBarPreference) findPreference("autoclick_delay"); 126 mDelay.setMax(delayToSeekBarProgress(MAX_AUTOCLICK_DELAY)); 127 mDelay.setProgress(delayToSeekBarProgress(delay)); 128 mDelay.setOnPreferenceChangeListener(this); 129 mFooterPreferenceMixin.createFooterPreference() 130 .setTitle(R.string.accessibility_autoclick_description); 131 } 132 133 @Override 134 protected void onInstallSwitchBarToggleSwitch() { 135 super.onInstallSwitchBarToggleSwitch(); 136 137 int value = Settings.Secure.getInt(getContentResolver(), 138 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, 0); 139 mSwitchBar.setCheckedInternal(value == 1); 140 mSwitchBar.addOnSwitchChangeListener(this); 141 mDelay.setEnabled(value == 1); 142 } 143 144 @Override 145 protected void onRemoveSwitchBarToggleSwitch() { 146 super.onRemoveSwitchBarToggleSwitch(); 147 mSwitchBar.removeOnSwitchChangeListener(this); 148 } 149 150 @Override 151 public void onSwitchChanged(Switch switchView, boolean isChecked) { 152 onPreferenceToggled(Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, isChecked); 153 } 154 155 @Override 156 public boolean onPreferenceChange(Preference preference, Object newValue) { 157 if (preference == mDelay && newValue instanceof Integer) { 158 Settings.Secure.putInt(getContentResolver(), 159 Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY, 160 seekBarProgressToDelay((int)newValue)); 161 return true; 162 } 163 return false; 164 } 165 166 /** 167 * Converts seek bar preference progress value to autoclick delay associated with it. 168 */ 169 private int seekBarProgressToDelay(int progress) { 170 return progress * AUTOCLICK_DELAY_STEP + MIN_AUTOCLICK_DELAY; 171 } 172 173 /** 174 * Converts autoclick delay value to seek bar preference progress values that represents said 175 * delay. 176 */ 177 private int delayToSeekBarProgress(int delay) { 178 return (delay - MIN_AUTOCLICK_DELAY) / AUTOCLICK_DELAY_STEP; 179 } 180 } 181