1 /*
2  * Copyright (C) 2021 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 com.google.common.primitives.Ints;
29 
30 import java.util.Optional;
31 
32 /** Preference controller that controls the preferred location in accessibility button page. */
33 public class AccessibilityButtonLocationPreferenceController extends BasePreferenceController
34         implements Preference.OnPreferenceChangeListener {
35 
36     private Optional<Integer> mDefaultLocation = Optional.empty();
37 
AccessibilityButtonLocationPreferenceController(Context context, String preferenceKey)38     public AccessibilityButtonLocationPreferenceController(Context context, String preferenceKey) {
39         super(context, preferenceKey);
40     }
41 
42     @Override
getAvailabilityStatus()43     public int getAvailabilityStatus() {
44         return AccessibilityUtil.isGestureNavigateEnabled(mContext)
45                 ? CONDITIONALLY_UNAVAILABLE : AVAILABLE;
46     }
47 
48     @Override
onPreferenceChange(Preference preference, Object newValue)49     public boolean onPreferenceChange(Preference preference, Object newValue) {
50         final ListPreference listPreference = (ListPreference) preference;
51         final Integer value = Ints.tryParse((String) newValue);
52         if (value != null) {
53             Settings.Secure.putInt(mContext.getContentResolver(),
54                     Settings.Secure.ACCESSIBILITY_BUTTON_MODE, value);
55             updateState(listPreference);
56         }
57         return true;
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(getCurrentAccessibilityButtonMode());
66     }
67 
getCurrentAccessibilityButtonMode()68     private String getCurrentAccessibilityButtonMode() {
69         final int mode = Settings.Secure.getInt(mContext.getContentResolver(),
70                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, getDefaultLocationValue());
71         return String.valueOf(mode);
72     }
73 
getDefaultLocationValue()74     private int getDefaultLocationValue() {
75         if (!mDefaultLocation.isPresent()) {
76             final String[] valuesList = mContext.getResources().getStringArray(
77                     R.array.accessibility_button_location_selector_values);
78             mDefaultLocation = Optional.of(Integer.parseInt(valuesList[0]));
79         }
80         return mDefaultLocation.get();
81     }
82 }
83