• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.settings.SettingsEnums.ACTION_VIBRATION_HAPTICS
19 import android.content.Context
20 import android.os.VibrationAttributes
21 import android.os.Vibrator
22 import android.provider.Settings
23 import androidx.preference.Preference
24 import com.android.settings.R
25 import com.android.settings.contract.KEY_VIBRATION_HAPTICS
26 import com.android.settings.metrics.PreferenceActionMetricsProvider
27 import com.android.settingslib.datastore.KeyValueStore
28 import com.android.settingslib.datastore.KeyValueStoreDelegate
29 import com.android.settingslib.datastore.SettingsSystemStore
30 import com.android.settingslib.metadata.BooleanValuePreference
31 import com.android.settingslib.metadata.PreferenceMetadata
32 import com.android.settingslib.metadata.ReadWritePermit
33 import com.android.settingslib.metadata.SensitivityLevel
34 import com.android.settingslib.preference.MainSwitchPreferenceBinding
35 
36 /** Accessibility settings for vibration. */
37 // LINT.IfChange
38 class VibrationMainSwitchPreference :
39     BooleanValuePreference,
40     MainSwitchPreferenceBinding,
41     PreferenceActionMetricsProvider,
42     Preference.OnPreferenceChangeListener {
43 
44     override val key
45         get() = KEY
46 
47     override val title
48         get() = R.string.accessibility_vibration_primary_switch_title
49 
50     override val keywords: Int
51         get() = R.string.keywords_accessibility_vibration_primary_switch
52 
53     override val preferenceActionMetrics: Int
54         get() = ACTION_VIBRATION_HAPTICS
55 
tagsnull56     override fun tags(context: Context) = arrayOf(KEY_VIBRATION_HAPTICS)
57 
58     override fun storage(context: Context): KeyValueStore = VibrationMainSwitchStore(context)
59 
60     override fun getReadPermissions(context: Context) = SettingsSystemStore.getReadPermissions()
61 
62     override fun getReadPermit(context: Context, callingPid: Int, callingUid: Int) =
63         ReadWritePermit.ALLOW
64 
65     override fun getWritePermissions(context: Context) = SettingsSystemStore.getWritePermissions()
66 
67     override fun getWritePermit(context: Context, callingPid: Int, callingUid: Int) =
68         ReadWritePermit.ALLOW
69 
70     override val sensitivityLevel: Int
71         get() = SensitivityLevel.NO_SENSITIVITY
72 
73     override fun bind(preference: Preference, metadata: PreferenceMetadata) {
74         super.bind(preference, metadata)
75         preference.onPreferenceChangeListener = this
76     }
77 
onPreferenceChangenull78     override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean {
79         if (newValue == true) {
80             // Play a haptic as preview for the main toggle only when touch feedback is enabled.
81             VibrationPreferenceConfig.playVibrationPreview(
82                 preference.context.vibrator,
83                 VibrationAttributes.USAGE_TOUCH,
84             )
85         }
86         return true
87     }
88 
89     companion object {
90         const val KEY = Settings.System.VIBRATE_ON
91     }
92 }
93 
94 /** Provides SettingsStore for vibration main switch with custom default value. */
95 @Suppress("UNCHECKED_CAST")
96 class VibrationMainSwitchStore(
97     context: Context,
98     private val settingsStore: KeyValueStore = SettingsSystemStore.get(context),
99 ) : KeyValueStoreDelegate {
100 
101     override val keyValueStoreDelegate
102         get() = settingsStore
103 
getDefaultValuenull104     override fun <T : Any> getDefaultValue(key: String, valueType: Class<T>) = DEFAULT_VALUE as T
105 
106     companion object {
107         private const val DEFAULT_VALUE = true
108     }
109 }
110 
111 val Context.vibrator: Vibrator
112     get() = getSystemService(Vibrator::class.java)!!
113 
114 // LINT.ThenChange(VibrationMainSwitchPreferenceController.java)
115