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.ContentResolver;
20 import android.content.Context;
21 import android.database.ContentObserver;
22 import android.os.Handler;
23 import android.os.Looper;
24 import android.provider.Settings;
25 
26 import androidx.annotation.IntDef;
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.ListPreference;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.core.BasePreferenceController;
33 import com.android.settingslib.core.lifecycle.LifecycleObserver;
34 import com.android.settingslib.core.lifecycle.events.OnPause;
35 import com.android.settingslib.core.lifecycle.events.OnResume;
36 
37 import com.google.common.primitives.Ints;
38 
39 import java.lang.annotation.Retention;
40 import java.lang.annotation.RetentionPolicy;
41 
42 /** Preference controller that controls the preferred size in accessibility button page. */
43 public class FloatingMenuSizePreferenceController extends BasePreferenceController
44         implements Preference.OnPreferenceChangeListener, LifecycleObserver, OnResume, OnPause {
45 
46     private final ContentResolver mContentResolver;
47     @VisibleForTesting
48     final ContentObserver mContentObserver;
49 
50     @VisibleForTesting
51     ListPreference mPreference;
52 
53     @Retention(RetentionPolicy.SOURCE)
54     @IntDef({
55             Size.UNKNOWN,
56             Size.SMALL,
57             Size.LARGE,
58     })
59     public @interface Size {
60         int UNKNOWN = -1;
61         int SMALL = 0;
62         int LARGE = 1;
63     }
64 
FloatingMenuSizePreferenceController(Context context, String preferenceKey)65     public FloatingMenuSizePreferenceController(Context context, String preferenceKey) {
66         super(context, preferenceKey);
67         mContentResolver = context.getContentResolver();
68         mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
69             @Override
70             public void onChange(boolean selfChange) {
71                 updateAvailabilityStatus();
72             }
73         };
74     }
75 
76     @Override
getAvailabilityStatus()77     public int getAvailabilityStatus() {
78         return AccessibilityUtil.isFloatingMenuEnabled(mContext)
79                 ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
80     }
81 
82     @Override
displayPreference(PreferenceScreen screen)83     public void displayPreference(PreferenceScreen screen) {
84         super.displayPreference(screen);
85 
86         mPreference = screen.findPreference(getPreferenceKey());
87     }
88 
89     @Override
onPreferenceChange(Preference preference, Object newValue)90     public boolean onPreferenceChange(Preference preference, Object newValue) {
91         final Integer value = Ints.tryParse((String) newValue);
92         if (value != null) {
93             putAccessibilityFloatingMenuSize(value);
94         }
95         return true;
96     }
97 
98     @Override
updateState(Preference preference)99     public void updateState(Preference preference) {
100         super.updateState(preference);
101         final ListPreference listPreference = (ListPreference) preference;
102 
103         listPreference.setValue(String.valueOf(getAccessibilityFloatingMenuSize()));
104     }
105 
106     @Override
onResume()107     public void onResume() {
108         mContentResolver.registerContentObserver(
109                 Settings.Secure.getUriFor(
110                         Settings.Secure.ACCESSIBILITY_BUTTON_MODE), /* notifyForDescendants= */
111                 false, mContentObserver);
112 
113     }
114 
115     @Override
onPause()116     public void onPause() {
117         mContentResolver.unregisterContentObserver(mContentObserver);
118     }
119 
updateAvailabilityStatus()120     private void updateAvailabilityStatus() {
121         mPreference.setEnabled(AccessibilityUtil.isFloatingMenuEnabled(mContext));
122     }
123 
124     @Size
getAccessibilityFloatingMenuSize()125     private int getAccessibilityFloatingMenuSize() {
126         return Settings.Secure.getInt(mContentResolver,
127                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_SIZE, Size.SMALL);
128     }
129 
putAccessibilityFloatingMenuSize(@ize int value)130     private void putAccessibilityFloatingMenuSize(@Size int value) {
131         Settings.Secure.putInt(mContentResolver,
132                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_SIZE, value);
133     }
134 }
135