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.annotation.IntDef;
23 import androidx.preference.ListPreference;
24 import androidx.preference.Preference;
25 
26 import com.android.settings.core.BasePreferenceController;
27 
28 import com.google.common.primitives.Ints;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 
33 /** Preference controller that controls the preferred location in accessibility button page. */
34 public class AccessibilityButtonLocationPreferenceController extends BasePreferenceController
35         implements Preference.OnPreferenceChangeListener {
36 
37     @Retention(RetentionPolicy.SOURCE)
38     @IntDef({
39             Location.FLOATING_MENU,
40             Location.NAVIGATION_BAR,
41     })
42     private @interface Location {
43         int FLOATING_MENU = 1;
44         int NAVIGATION_BAR = 0;
45     }
46 
AccessibilityButtonLocationPreferenceController(Context context, String preferenceKey)47     public AccessibilityButtonLocationPreferenceController(Context context, String preferenceKey) {
48         super(context, preferenceKey);
49     }
50 
51     @Override
getAvailabilityStatus()52     public int getAvailabilityStatus() {
53         return AccessibilityUtil.isGestureNavigateEnabled(mContext)
54                 ? CONDITIONALLY_UNAVAILABLE : AVAILABLE;
55     }
56 
57     @Override
onPreferenceChange(Preference preference, Object newValue)58     public boolean onPreferenceChange(Preference preference, Object newValue) {
59         final Integer value = Ints.tryParse((String) newValue);
60         if (value != null) {
61             putCurrentAccessibilityButtonMode(value);
62         }
63         return true;
64     }
65 
66     @Override
updateState(Preference preference)67     public void updateState(Preference preference) {
68         super.updateState(preference);
69         final ListPreference listPreference = (ListPreference) preference;
70 
71         listPreference.setValue(String.valueOf(getCurrentAccessibilityButtonMode()));
72     }
73 
74     @Location
getCurrentAccessibilityButtonMode()75     private int getCurrentAccessibilityButtonMode() {
76         return Settings.Secure.getInt(mContext.getContentResolver(),
77                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, Location.FLOATING_MENU);
78     }
79 
putCurrentAccessibilityButtonMode(@ocation int location)80     private void putCurrentAccessibilityButtonMode(@Location int location) {
81         Settings.Secure.putInt(mContext.getContentResolver(),
82                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, location);
83     }
84 }
85