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.Vibrator
20 import androidx.fragment.app.Fragment
21 import com.android.settings.R
22 import com.android.settings.flags.Flags
23 import com.android.settingslib.metadata.PreferenceAvailabilityProvider
24 import com.android.settingslib.metadata.ProvidePreferenceScreen
25 import com.android.settingslib.metadata.preferenceHierarchy
26 import com.android.settingslib.preference.PreferenceScreenCreator
27 
28 /**
29  * Accessibility settings for vibration.
30  */
31 // LINT.IfChange
32 @ProvidePreferenceScreen
33 class VibrationScreen : PreferenceScreenCreator, PreferenceAvailabilityProvider {
34     override val key: String
35         get() = KEY
36 
37     override val title: Int
38         get() = R.string.accessibility_vibration_settings_title
39 
40     override val keywords: Int
41         get() = R.string.keywords_vibration
42 
isAvailablenull43     override fun isAvailable(context: Context) =
44         context.isVibratorAvailable() && context.getSupportedVibrationIntensityLevels() == 1
45 
46     override fun isFlagEnabled(context: Context): Boolean = Flags.catalystVibrationIntensityScreen()
47 
48     override fun hasCompleteHierarchy() = false
49 
50     override fun fragmentClass(): Class<out Fragment>? = VibrationSettings::class.java
51 
52     override fun getPreferenceHierarchy(context: Context) = preferenceHierarchy(this) {
53         +VibrationMainSwitchPreference()
54     }
55 
56     companion object {
57         const val KEY = "vibration_screen"
58     }
59 }
60 
61 /** Returns true if the device has a system vibrator, false otherwise. */
Contextnull62 fun Context.isVibratorAvailable(): Boolean =
63     getSystemService(Vibrator::class.java).hasVibrator()
64 
65 /** Returns the number of vibration intensity levels supported by this device. */
66 fun Context.getSupportedVibrationIntensityLevels(): Int =
67     resources.getInteger(R.integer.config_vibration_supported_intensity_levels)
68 
69 // LINT.ThenChange(VibrationPreferenceController.java)
70