1 /*
2  * Copyright (C) 2024 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 package com.android.settings.accessibility
17 
18 import android.content.Context
19 import android.os.VibrationAttributes
20 import android.os.Vibrator
21 import android.provider.Settings
22 import android.widget.CompoundButton
23 import android.widget.CompoundButton.OnCheckedChangeListener
24 import com.android.settings.R
25 import com.android.settingslib.datastore.KeyValueStore
26 import com.android.settingslib.datastore.KeyedObservableDelegate
27 import com.android.settingslib.datastore.SettingsStore
28 import com.android.settingslib.datastore.SettingsSystemStore
29 import com.android.settingslib.metadata.MainSwitchPreference
30 import com.android.settingslib.metadata.PreferenceLifecycleContext
31 import com.android.settingslib.metadata.PreferenceLifecycleProvider
32 import com.android.settingslib.metadata.ReadWritePermit
33 import com.android.settingslib.preference.MainSwitchPreferenceBinding
34 
35 /**
36  * Accessibility settings for vibration.
37  */
38 // LINT.IfChange
39 class VibrationMainSwitchPreference : MainSwitchPreference(
40     key = Settings.System.VIBRATE_ON,
41     title = R.string.accessibility_vibration_primary_switch_title,
42 ), PreferenceLifecycleProvider, OnCheckedChangeListener {
43     override val keywords: Int
44         get() = R.string.keywords_accessibility_vibration_primary_switch
45 
46     lateinit var vibrator: Vibrator
47 
storagenull48     override fun storage(context: Context): KeyValueStore =
49         VibrationMainSwitchToggleStorage(SettingsSystemStore.get(context))
50 
51     override fun getReadPermit(context: Context, myUid: Int, callingUid: Int) =
52         ReadWritePermit.ALLOW
53 
54     override fun getWritePermit(context: Context, value: Boolean?, myUid: Int, callingUid: Int) =
55         ReadWritePermit.ALLOW
56 
57     override fun onResume(context: PreferenceLifecycleContext) {
58         vibrator = context.getSystemService(Vibrator::class.java)
59         context.findPreference<com.android.settingslib.widget.MainSwitchPreference>(key)
60             ?.addOnSwitchChangeListener(this)
61     }
62 
onPausenull63     override fun onPause(context: PreferenceLifecycleContext) {
64         context.findPreference<com.android.settingslib.widget.MainSwitchPreference>(key)
65             ?.removeOnSwitchChangeListener(this)
66     }
67 
onCheckedChangednull68     override fun onCheckedChanged(button: CompoundButton, isChecked: Boolean) {
69         if (isChecked) {
70             // Play a haptic as preview for the main toggle only when touch feedback is enabled.
71             VibrationPreferenceConfig.playVibrationPreview(
72                 vibrator, VibrationAttributes.USAGE_TOUCH
73             )
74         }
75     }
76 
77     /** Provides SettingsStore for vibration main switch with custom default value. */
78     @Suppress("UNCHECKED_CAST")
79     private class VibrationMainSwitchToggleStorage(
80         private val settingsStore: SettingsStore,
81     ) : KeyedObservableDelegate<String>(settingsStore), KeyValueStore {
82 
containsnull83         override fun contains(key: String) = settingsStore.contains(key)
84 
85         override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) =
86             DEFAULT_VALUE as T
87 
88         override fun <T : Any> getValue(key: String, valueType: Class<T>) =
89             (settingsStore.getBoolean(key) ?: DEFAULT_VALUE) as T
90 
91         override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
92             settingsStore.setBoolean(key, value as Boolean?)
93         }
94     }
95 
96     companion object {
97         const val DEFAULT_VALUE = true
98     }
99 }
100 // LINT.ThenChange(VibrationMainSwitchPreferenceController.java)
101