1 /*
2  * Copyright (C) 2018 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.os.VibrationAttributes;
24 import android.os.Vibrator;
25 import android.provider.Settings;
26 
27 /** Preference controller for haptic feedback intensity */
28 public class HapticFeedbackIntensityPreferenceController
29         extends VibrationIntensityPreferenceController {
30 
31     /** General configuration for haptic feedback intensity settings. */
32     public static final class HapticFeedbackVibrationPreferenceConfig
33             extends VibrationPreferenceConfig {
34 
HapticFeedbackVibrationPreferenceConfig(Context context)35         public HapticFeedbackVibrationPreferenceConfig(Context context) {
36             super(context, Settings.System.HAPTIC_FEEDBACK_INTENSITY,
37                     VibrationAttributes.USAGE_TOUCH);
38         }
39 
40         @Override
readIntensity()41         public int readIntensity() {
42             final int hapticFeedbackEnabled = Settings.System.getInt(mContentResolver,
43                     Settings.System.HAPTIC_FEEDBACK_ENABLED, ON);
44 
45             if (hapticFeedbackEnabled == OFF) {
46                 // HAPTIC_FEEDBACK_ENABLED is deprecated but should still be applied if the user has
47                 // turned it off already.
48                 return Vibrator.VIBRATION_INTENSITY_OFF;
49             }
50 
51             return super.readIntensity();
52         }
53 
54         @Override
updateIntensity(int intensity)55         public boolean updateIntensity(int intensity) {
56             final boolean success = super.updateIntensity(intensity);
57             final boolean isIntensityOff = intensity == Vibrator.VIBRATION_INTENSITY_OFF;
58 
59             Settings.System.putInt(mContentResolver, Settings.System.HAPTIC_FEEDBACK_ENABLED,
60                     isIntensityOff ? OFF : ON);
61             // HAPTIC_FEEDBACK_ENABLED is deprecated but should still reflect the intensity setting.
62 
63             // HARDWARE_HAPTIC_FEEDBACK_INTENSITY is dependent on this setting, but should not be
64             // disabled by it.
65             Settings.System.putInt(mContentResolver,
66                     Settings.System.HARDWARE_HAPTIC_FEEDBACK_INTENSITY,
67                     isIntensityOff ? getDefaultIntensity() : intensity);
68 
69             return success;
70         }
71     }
72 
HapticFeedbackIntensityPreferenceController(Context context, String preferenceKey)73     public HapticFeedbackIntensityPreferenceController(Context context, String preferenceKey) {
74         super(context, preferenceKey, new HapticFeedbackVibrationPreferenceConfig(context));
75     }
76 
HapticFeedbackIntensityPreferenceController(Context context, String preferenceKey, int supportedIntensityLevels)77     protected HapticFeedbackIntensityPreferenceController(Context context, String preferenceKey,
78             int supportedIntensityLevels) {
79         super(context, preferenceKey, new HapticFeedbackVibrationPreferenceConfig(context),
80                 supportedIntensityLevels);
81     }
82 
83     @Override
getAvailabilityStatus()84     public int getAvailabilityStatus() {
85         return AVAILABLE;
86     }
87 }
88