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.FloatRange;
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.core.SliderPreferenceController;
31 import com.android.settings.widget.SeekBarPreference;
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 transparency seekbar in accessibility button page. */
37 public class FloatingMenuTransparencyPreferenceController extends SliderPreferenceController
38         implements LifecycleObserver, OnResume, OnPause {
39 
40     @VisibleForTesting
41     @FloatRange(from = 0.0, to = 1.0)
42     static final float DEFAULT_TRANSPARENCY = 0.45f;
43     @VisibleForTesting
44     static final float MAXIMUM_TRANSPARENCY = 1.0f;
45     private static final int FADE_ENABLED = 1;
46     private static final float MIN_PROGRESS = 0f;
47     private static final float MAX_PROGRESS = 90f;
48     @VisibleForTesting
49     static final float PRECISION = 100f;
50 
51     private final ContentResolver mContentResolver;
52     @VisibleForTesting
53     final ContentObserver mContentObserver;
54 
55     @VisibleForTesting
56     SeekBarPreference mPreference;
57 
FloatingMenuTransparencyPreferenceController(Context context, String preferenceKey)58     public FloatingMenuTransparencyPreferenceController(Context context,
59             String preferenceKey) {
60         super(context, preferenceKey);
61         mContentResolver = context.getContentResolver();
62         mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
63             @Override
64             public void onChange(boolean selfChange) {
65                 updateAvailabilityStatus();
66             }
67         };
68     }
69 
70     @Override
getAvailabilityStatus()71     public int getAvailabilityStatus() {
72         return AccessibilityUtil.isFloatingMenuEnabled(mContext)
73                 ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
74     }
75 
76     @Override
displayPreference(PreferenceScreen screen)77     public void displayPreference(PreferenceScreen screen) {
78         super.displayPreference(screen);
79 
80         mPreference = screen.findPreference(getPreferenceKey());
81         mPreference.setContinuousUpdates(true);
82         mPreference.setMax(getMax());
83         mPreference.setMin(getMin());
84         mPreference.setHapticFeedbackMode(SeekBarPreference.HAPTIC_FEEDBACK_MODE_ON_ENDS);
85 
86         updateState(mPreference);
87     }
88 
89     @Override
onResume()90     public void onResume() {
91         mContentResolver.registerContentObserver(
92                 Settings.Secure.getUriFor(
93                         Settings.Secure.ACCESSIBILITY_BUTTON_MODE), /* notifyForDescendants= */
94                 false, mContentObserver);
95         mContentResolver.registerContentObserver(
96                 Settings.Secure.getUriFor(
97                         Settings.Secure.ACCESSIBILITY_FLOATING_MENU_FADE_ENABLED),
98                         /* notifyForDescendants= */ false, mContentObserver);
99     }
100 
101     @Override
onPause()102     public void onPause() {
103         mContentResolver.unregisterContentObserver(mContentObserver);
104     }
105 
106     @Override
getSliderPosition()107     public int getSliderPosition() {
108         return convertTransparencyFloatToInt(getTransparency());
109     }
110 
111     @Override
setSliderPosition(int position)112     public boolean setSliderPosition(int position) {
113         final float opacityValue = MAXIMUM_TRANSPARENCY - convertTransparencyIntToFloat(position);
114         return Settings.Secure.putFloat(mContentResolver,
115                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_OPACITY, opacityValue);
116     }
117 
118     @Override
getMax()119     public int getMax() {
120         return (int) MAX_PROGRESS;
121     }
122 
123     @Override
getMin()124     public int getMin() {
125         return (int) MIN_PROGRESS;
126     }
127 
updateAvailabilityStatus()128     private void updateAvailabilityStatus() {
129         final boolean fadeEnabled = Settings.Secure.getInt(mContentResolver,
130                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_FADE_ENABLED, FADE_ENABLED)
131                 == FADE_ENABLED;
132 
133         mPreference.setEnabled(AccessibilityUtil.isFloatingMenuEnabled(mContext) && fadeEnabled);
134     }
135 
convertTransparencyFloatToInt(float value)136     private int convertTransparencyFloatToInt(float value) {
137         return Math.round(value * PRECISION);
138     }
139 
convertTransparencyIntToFloat(int value)140     private float convertTransparencyIntToFloat(int value) {
141         return (float) value / PRECISION;
142     }
143 
getTransparency()144     private float getTransparency() {
145         float transparencyValue = MAXIMUM_TRANSPARENCY - (Settings.Secure.getFloat(mContentResolver,
146                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_OPACITY, DEFAULT_TRANSPARENCY));
147         final float minValue = MIN_PROGRESS / PRECISION;
148         final float maxValue = MAX_PROGRESS / PRECISION;
149 
150         return (transparencyValue < minValue || transparencyValue > maxValue)
151                 ? DEFAULT_TRANSPARENCY : transparencyValue;
152     }
153 }
154