1 /*
2  * Copyright (C) 2022 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.os.Vibrator;
21 
22 import androidx.preference.Preference;
23 import androidx.preference.PreferenceScreen;
24 
25 import com.android.settings.R;
26 import com.android.settings.core.TogglePreferenceController;
27 import com.android.settingslib.core.lifecycle.LifecycleObserver;
28 import com.android.settingslib.core.lifecycle.events.OnStart;
29 import com.android.settingslib.core.lifecycle.events.OnStop;
30 
31 /** Abstract preference controller for a vibration intensity setting, that has only ON/OFF states */
32 public abstract class VibrationTogglePreferenceController extends TogglePreferenceController
33         implements LifecycleObserver, OnStart, OnStop {
34 
35     protected final VibrationPreferenceConfig mPreferenceConfig;
36     private final VibrationPreferenceConfig.SettingObserver mSettingsContentObserver;
37 
VibrationTogglePreferenceController(Context context, String preferenceKey, VibrationPreferenceConfig preferenceConfig)38     protected VibrationTogglePreferenceController(Context context, String preferenceKey,
39             VibrationPreferenceConfig preferenceConfig) {
40         super(context, preferenceKey);
41         mPreferenceConfig = preferenceConfig;
42         mSettingsContentObserver = new VibrationPreferenceConfig.SettingObserver(
43                 preferenceConfig);
44     }
45 
46     @Override
onStart()47     public void onStart() {
48         mSettingsContentObserver.register(mContext);
49     }
50 
51     @Override
onStop()52     public void onStop() {
53         mSettingsContentObserver.unregister(mContext);
54     }
55 
56     @Override
displayPreference(PreferenceScreen screen)57     public void displayPreference(PreferenceScreen screen) {
58         super.displayPreference(screen);
59         final Preference preference = screen.findPreference(getPreferenceKey());
60         mSettingsContentObserver.onDisplayPreference(this, preference);
61         preference.setEnabled(mPreferenceConfig.isPreferenceEnabled());
62     }
63 
64     @Override
updateState(Preference preference)65     public void updateState(Preference preference) {
66         super.updateState(preference);
67         if (preference != null) {
68             preference.setEnabled(mPreferenceConfig.isPreferenceEnabled());
69             preference.setSummary(mPreferenceConfig.getSummary());
70         }
71     }
72 
73     @Override
isChecked()74     public boolean isChecked() {
75         return mPreferenceConfig.isPreferenceEnabled()
76                 && (mPreferenceConfig.readIntensity() != Vibrator.VIBRATION_INTENSITY_OFF);
77     }
78 
79     @Override
setChecked(boolean isChecked)80     public boolean setChecked(boolean isChecked) {
81         if (!mPreferenceConfig.isPreferenceEnabled()) {
82             // Ignore toggle updates when the preference is disabled.
83             return false;
84         }
85         final int newIntensity = isChecked
86                 ? mPreferenceConfig.getDefaultIntensity()
87                 : Vibrator.VIBRATION_INTENSITY_OFF;
88         final boolean success = mPreferenceConfig.updateIntensity(newIntensity);
89 
90         if (success && isChecked) {
91             mPreferenceConfig.playVibrationPreview();
92         }
93 
94         return success;
95     }
96 
97     @Override
getSliceHighlightMenuRes()98     public int getSliceHighlightMenuRes() {
99         return R.string.menu_key_accessibility;
100     }
101 }
102