1 /* 2 * Copyright (C) 2019 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.Context; 20 import android.provider.Settings; 21 22 import androidx.preference.ListPreference; 23 import androidx.preference.Preference; 24 25 import com.android.settings.R; 26 import com.android.settings.core.BasePreferenceController; 27 28 import java.util.HashMap; 29 import java.util.Map; 30 31 public class SelectLongPressTimeoutPreferenceController extends BasePreferenceController implements 32 Preference.OnPreferenceChangeListener { 33 34 private final Map<String, String> mLongPressTimeoutValueToTitleMap; 35 private int mLongPressTimeoutDefault; 36 SelectLongPressTimeoutPreferenceController(Context context, String preferenceKey)37 public SelectLongPressTimeoutPreferenceController(Context context, String preferenceKey) { 38 super(context, preferenceKey); 39 mLongPressTimeoutValueToTitleMap = new HashMap<>(); 40 initLongPressTimeoutValueToTitleMap(); 41 } 42 43 @Override getAvailabilityStatus()44 public int getAvailabilityStatus() { 45 return AVAILABLE; 46 } 47 48 @Override onPreferenceChange(Preference preference, Object object)49 public boolean onPreferenceChange(Preference preference, Object object) { 50 if (!(preference instanceof ListPreference)) { 51 return false; 52 } 53 final ListPreference listPreference = (ListPreference) preference; 54 final int newValue = Integer.parseInt((String) object); 55 Settings.Secure.putInt(mContext.getContentResolver(), 56 Settings.Secure.LONG_PRESS_TIMEOUT, newValue); 57 updateState(listPreference); 58 return true; 59 60 } 61 62 @Override updateState(Preference preference)63 public void updateState(Preference preference) { 64 super.updateState(preference); 65 if (!(preference instanceof ListPreference)) { 66 return; 67 } 68 final ListPreference listPreference = (ListPreference) preference; 69 listPreference.setValue(getLongPressTimeoutValue()); 70 } 71 72 @Override getSummary()73 public CharSequence getSummary() { 74 return mLongPressTimeoutValueToTitleMap.get(getLongPressTimeoutValue()); 75 } 76 getLongPressTimeoutValue()77 private String getLongPressTimeoutValue() { 78 final int longPressTimeout = Settings.Secure.getInt(mContext.getContentResolver(), 79 Settings.Secure.LONG_PRESS_TIMEOUT, mLongPressTimeoutDefault); 80 return String.valueOf(longPressTimeout); 81 } 82 initLongPressTimeoutValueToTitleMap()83 private void initLongPressTimeoutValueToTitleMap() { 84 if (mLongPressTimeoutValueToTitleMap.size() == 0) { 85 final String[] timeoutValues = mContext.getResources().getStringArray( 86 R.array.long_press_timeout_selector_values); 87 mLongPressTimeoutDefault = Integer.parseInt(timeoutValues[0]); 88 final String[] timeoutTitles = mContext.getResources().getStringArray( 89 R.array.long_press_timeout_selector_titles); 90 final int timeoutValueCount = timeoutValues.length; 91 for (int i = 0; i < timeoutValueCount; i++) { 92 mLongPressTimeoutValueToTitleMap.put(timeoutValues[i], timeoutTitles[i]); 93 } 94 } 95 } 96 } 97