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