• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
21 
22 import android.content.Context;
23 import android.database.ContentObserver;
24 import android.net.Uri;
25 import android.os.Handler;
26 import android.os.Looper;
27 import android.os.VibrationAttributes;
28 import android.os.Vibrator;
29 import android.provider.Settings;
30 
31 import androidx.annotation.NonNull;
32 import androidx.annotation.Nullable;
33 import androidx.preference.Preference;
34 import androidx.preference.PreferenceScreen;
35 
36 import com.android.settings.R;
37 import com.android.settings.core.TogglePreferenceController;
38 import com.android.settingslib.core.lifecycle.LifecycleObserver;
39 import com.android.settingslib.core.lifecycle.events.OnStart;
40 import com.android.settingslib.core.lifecycle.events.OnStop;
41 
42 /**
43  * Preference controller for the main switch setting for vibration and haptics screen.
44  *
45  * <p>This preference is controlled by the setting key{@link Settings.System#VIBRATE_ON}, and it
46  * will disable the entire settings screen once the settings is turned OFF. All device haptics will
47  * be disabled by this setting, except the flagged alerts and accessibility touch feedback.
48  */
49 // LINT.IfChange
50 public class VibrationMainSwitchPreferenceController extends TogglePreferenceController
51         implements LifecycleObserver, OnStart, OnStop {
52 
53     private final ContentObserver mSettingObserver;
54     private final Vibrator mVibrator;
55     private @Nullable Preference mPreference;
56 
VibrationMainSwitchPreferenceController(Context context, String preferenceKey)57     public VibrationMainSwitchPreferenceController(Context context, String preferenceKey) {
58         super(context, preferenceKey);
59         mVibrator = context.getSystemService(Vibrator.class);
60         Handler handler = Looper.myLooper() != null ? new Handler(/* async= */ true) : null;
61         mSettingObserver = new ContentObserver(handler) {
62             @Override
63             public void onChange(boolean selfChange, Uri uri) {
64                 if (mPreference != null) {
65                     updateState(mPreference);
66                 }
67             }
68         };
69     }
70 
71     @Override
getAvailabilityStatus()72     public int getAvailabilityStatus() {
73         return AVAILABLE;
74     }
75 
76     @Override
displayPreference(@onNull PreferenceScreen screen)77     public void displayPreference(@NonNull PreferenceScreen screen) {
78         super.displayPreference(screen);
79         mPreference = screen.findPreference(getPreferenceKey());
80     }
81 
82     @Override
onStart()83     public void onStart() {
84         mContext.getContentResolver().registerContentObserver(
85                 Settings.System.getUriFor(VibrationPreferenceConfig.MAIN_SWITCH_SETTING_KEY),
86                 /* notifyForDescendants= */ false,
87                 mSettingObserver);
88     }
89 
90     @Override
onStop()91     public void onStop() {
92         mContext.getContentResolver().unregisterContentObserver(mSettingObserver);
93     }
94 
95     @Override
isChecked()96     public boolean isChecked() {
97         return VibrationPreferenceConfig.isMainVibrationSwitchEnabled(
98                 mContext.getContentResolver());
99     }
100 
101     @Override
setChecked(boolean isChecked)102     public boolean setChecked(boolean isChecked) {
103         // The main switch change can be triggered by both the user click and the
104         // SettingsMainSwitchPreferenceController state change. Make sure we only do it once.
105         boolean wasChecked = isChecked();
106         boolean success = Settings.System.putInt(mContext.getContentResolver(),
107                 VibrationPreferenceConfig.MAIN_SWITCH_SETTING_KEY,
108                 isChecked ? ON : OFF);
109 
110         if (success && !wasChecked && isChecked) {
111             // Play a haptic as preview for the main toggle only when touch feedback is enabled.
112             VibrationPreferenceConfig.playVibrationPreview(
113                     mVibrator, VibrationAttributes.USAGE_TOUCH);
114         }
115 
116         return success;
117     }
118 
119     @Override
getSliceHighlightMenuRes()120     public int getSliceHighlightMenuRes() {
121         return R.string.menu_key_accessibility;
122     }
123 }
124 // LINT.ThenChange(VibrationMainSwitchPreference.kt)
125