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 int newValue = Integer.parseInt((String) object);
54         Settings.Secure.putInt(mContext.getContentResolver(),
55                 Settings.Secure.LONG_PRESS_TIMEOUT, newValue);
56         return true;
57 
58     }
59 
60     @Override
updateState(Preference preference)61     public void updateState(Preference preference) {
62         super.updateState(preference);
63         final ListPreference listPreference = (ListPreference) preference;
64 
65         listPreference.setValue(getLongPressTimeoutValue());
66     }
67 
getLongPressTimeoutValue()68     private String getLongPressTimeoutValue() {
69         final int longPressTimeout = Settings.Secure.getInt(mContext.getContentResolver(),
70                 Settings.Secure.LONG_PRESS_TIMEOUT, mLongPressTimeoutDefault);
71         return String.valueOf(longPressTimeout);
72     }
73 
initLongPressTimeoutValueToTitleMap()74     private void initLongPressTimeoutValueToTitleMap() {
75         if (mLongPressTimeoutValueToTitleMap.size() == 0) {
76             final String[] timeoutValues = mContext.getResources().getStringArray(
77                     R.array.long_press_timeout_selector_values);
78             mLongPressTimeoutDefault = Integer.parseInt(timeoutValues[0]);
79             final String[] timeoutTitles = mContext.getResources().getStringArray(
80                     R.array.long_press_timeout_selector_titles);
81             final int timeoutValueCount = timeoutValues.length;
82             for (int i = 0; i < timeoutValueCount; i++) {
83                 mLongPressTimeoutValueToTitleMap.put(timeoutValues[i], timeoutTitles[i]);
84             }
85         }
86     }
87 }
88