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.ON;
20 
21 import android.content.Context;
22 import android.os.Vibrator;
23 import android.provider.Settings;
24 import android.text.TextUtils;
25 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.Preference;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.BasePreferenceController;
31 import com.android.settings.core.SubSettingLauncher;
32 
33 /** Controller for "Vibration & haptics" settings page. */
34 public class VibrationPreferenceController extends BasePreferenceController {
35 
36     private final boolean mHasVibrator;
37 
VibrationPreferenceController(Context context, String key)38     public VibrationPreferenceController(Context context, String key) {
39         super(context, key);
40         mHasVibrator = context.getSystemService(Vibrator.class).hasVibrator();
41     }
42 
43     @Override
getAvailabilityStatus()44     public int getAvailabilityStatus() {
45         return mHasVibrator ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
46     }
47 
48     @Override
getSummary()49     public CharSequence getSummary() {
50         final boolean isVibrateOn = Settings.System.getInt(mContext.getContentResolver(),
51                 Settings.System.VIBRATE_ON, ON) == ON;
52         return mContext.getText(
53                 isVibrateOn
54                         ? R.string.accessibility_vibration_settings_state_on
55                         : R.string.accessibility_vibration_settings_state_off);
56     }
57 
58     @VisibleForTesting
launchVibrationSettingsFragment(Class klass)59     void launchVibrationSettingsFragment(Class klass) {
60         new SubSettingLauncher(mContext)
61                 .setSourceMetricsCategory(getMetricsCategory())
62                 .setDestination(klass.getName())
63                 .launch();
64     }
65 
66     @Override
handlePreferenceTreeClick(Preference preference)67     public boolean handlePreferenceTreeClick(Preference preference) {
68         if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
69             if (mContext.getResources().getInteger(
70                     R.integer.config_vibration_supported_intensity_levels) > 1) {
71                 launchVibrationSettingsFragment(VibrationIntensitySettingsFragment.class);
72             } else {
73                 launchVibrationSettingsFragment(VibrationSettings.class);
74             }
75             return true;
76         }
77         return super.handlePreferenceTreeClick(preference);
78     }
79 
80 
81 }
82