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.VisibleForTesting;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 import androidx.preference.TwoStatePreference;
30 
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settingslib.core.lifecycle.LifecycleObserver;
33 import com.android.settingslib.core.lifecycle.events.OnPause;
34 import com.android.settingslib.core.lifecycle.events.OnResume;
35 
36 /** Preference controller that controls the fade switch button in accessibility button page. */
37 public class FloatingMenuFadePreferenceController extends BasePreferenceController implements
38         Preference.OnPreferenceChangeListener, LifecycleObserver, OnResume, OnPause {
39 
40     private static final int OFF = 0;
41     private static final int ON = 1;
42 
43     private final ContentResolver mContentResolver;
44     @VisibleForTesting
45     final ContentObserver mContentObserver;
46 
47     @VisibleForTesting
48     TwoStatePreference mPreference;
49 
FloatingMenuFadePreferenceController(Context context, String preferenceKey)50     public FloatingMenuFadePreferenceController(Context context, String preferenceKey) {
51         super(context, preferenceKey);
52         mContentResolver = context.getContentResolver();
53         mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
54             @Override
55             public void onChange(boolean selfChange) {
56                 updateAvailabilityStatus();
57             }
58         };
59     }
60 
61     @Override
getAvailabilityStatus()62     public int getAvailabilityStatus() {
63         return AccessibilityUtil.isFloatingMenuEnabled(mContext)
64                 ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
65     }
66 
67     @Override
displayPreference(PreferenceScreen screen)68     public void displayPreference(PreferenceScreen screen) {
69         super.displayPreference(screen);
70 
71         mPreference = screen.findPreference(getPreferenceKey());
72     }
73 
74     @Override
onPreferenceChange(Preference preference, Object newValue)75     public boolean onPreferenceChange(Preference preference, Object newValue) {
76         final boolean isEnabled = (boolean) newValue;
77         putFloatingMenuFadeValue(isEnabled);
78         return true;
79     }
80 
81     @Override
updateState(Preference preference)82     public void updateState(Preference preference) {
83         super.updateState(preference);
84         final TwoStatePreference switchPreference = (TwoStatePreference) preference;
85 
86         switchPreference.setChecked(getFloatingMenuFadeValue() == ON);
87     }
88 
89     @Override
onResume()90     public void onResume() {
91         mContentResolver.registerContentObserver(
92                 Settings.Secure.getUriFor(
93                         Settings.Secure.ACCESSIBILITY_BUTTON_MODE),
94                         /* notifyForDescendants= */ false, mContentObserver);
95     }
96 
97     @Override
onPause()98     public void onPause() {
99         mContentResolver.unregisterContentObserver(mContentObserver);
100     }
101 
updateAvailabilityStatus()102     private void updateAvailabilityStatus() {
103         mPreference.setEnabled(AccessibilityUtil.isFloatingMenuEnabled(mContext));
104     }
105 
getFloatingMenuFadeValue()106     private int getFloatingMenuFadeValue() {
107         return Settings.Secure.getInt(mContentResolver,
108                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_FADE_ENABLED, ON);
109     }
110 
putFloatingMenuFadeValue(boolean isEnabled)111     private void putFloatingMenuFadeValue(boolean isEnabled) {
112         Settings.Secure.putInt(mContentResolver,
113                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_FADE_ENABLED,
114                 isEnabled ? ON : OFF);
115     }
116 }
117