1 /*
2  * Copyright (C) 2019 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 android.content.Context;
20 import android.os.Vibrator;
21 import android.provider.Settings;
22 
23 import com.android.settings.R;
24 import com.android.settings.core.BasePreferenceController;
25 
26 public class VibrationPreferenceController extends BasePreferenceController {
27 
28     private final Vibrator mVibrator;
29 
VibrationPreferenceController(Context context, String preferenceKey)30     public VibrationPreferenceController(Context context, String preferenceKey) {
31         super(context, preferenceKey);
32         mVibrator = mContext.getSystemService(Vibrator.class);
33     }
34 
35     @Override
getAvailabilityStatus()36     public int getAvailabilityStatus() {
37         return AVAILABLE;
38     }
39 
40     @Override
getSummary()41     public CharSequence getSummary() {
42         int ringIntensity = Settings.System.getInt(mContext.getContentResolver(),
43                 Settings.System.RING_VIBRATION_INTENSITY,
44                 mVibrator.getDefaultRingVibrationIntensity());
45         if (Settings.System.getInt(mContext.getContentResolver(),
46                 Settings.System.VIBRATE_WHEN_RINGING, 0) == 0
47                 && !AccessibilitySettings.isRampingRingerEnabled(mContext)) {
48             ringIntensity = Vibrator.VIBRATION_INTENSITY_OFF;
49         }
50         final CharSequence ringIntensityString =
51                 VibrationIntensityPreferenceController.getIntensityString(mContext, ringIntensity);
52 
53         final int notificationIntensity = Settings.System.getInt(mContext.getContentResolver(),
54                 Settings.System.NOTIFICATION_VIBRATION_INTENSITY,
55                 mVibrator.getDefaultNotificationVibrationIntensity());
56         final CharSequence notificationIntensityString =
57                 VibrationIntensityPreferenceController.getIntensityString(mContext,
58                         notificationIntensity);
59 
60         int touchIntensity = Settings.System.getInt(mContext.getContentResolver(),
61                 Settings.System.HAPTIC_FEEDBACK_INTENSITY,
62                 mVibrator.getDefaultHapticFeedbackIntensity());
63         if (Settings.System.getInt(mContext.getContentResolver(),
64                 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 0) {
65             touchIntensity = Vibrator.VIBRATION_INTENSITY_OFF;
66         }
67         final CharSequence touchIntensityString =
68                 VibrationIntensityPreferenceController.getIntensityString(mContext, touchIntensity);
69 
70         if (ringIntensity == touchIntensity && ringIntensity == notificationIntensity) {
71             return ringIntensityString;
72         } else {
73             return mContext.getString(R.string.accessibility_vibration_summary, ringIntensityString,
74                     notificationIntensityString, touchIntensityString);
75         }
76     }
77 }
78