1 /*
2  * Copyright (C) 2017 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.notification.app;
18 
19 import android.app.NotificationChannel;
20 import android.app.NotificationManager;
21 import android.content.Context;
22 import android.os.Vibrator;
23 
24 import androidx.preference.Preference;
25 
26 import com.android.settings.core.PreferenceControllerMixin;
27 import com.android.settings.notification.NotificationBackend;
28 import com.android.settings.notification.app.NotificationPreferenceController;
29 import com.android.settingslib.RestrictedSwitchPreference;
30 
31 public class VibrationPreferenceController extends NotificationPreferenceController
32         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
33 
34     private static final String KEY_VIBRATE = "vibrate";
35     private final Vibrator mVibrator;
36 
VibrationPreferenceController(Context context, NotificationBackend backend)37     public VibrationPreferenceController(Context context, NotificationBackend backend) {
38         super(context, backend);
39         mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
40     }
41 
42     @Override
getPreferenceKey()43     public String getPreferenceKey() {
44         return KEY_VIBRATE;
45     }
46 
47     @Override
isAvailable()48     public boolean isAvailable() {
49         if (!super.isAvailable() || mChannel == null) {
50             return false;
51        }
52         return checkCanBeVisible(NotificationManager.IMPORTANCE_DEFAULT)
53                 && !isDefaultChannel()
54                 && mVibrator != null
55                 && mVibrator.hasVibrator();
56     }
57 
58     @Override
isIncludedInFilter()59     boolean isIncludedInFilter() {
60         return mPreferenceFilter.contains(NotificationChannel.EDIT_VIBRATION);
61     }
62 
updateState(Preference preference)63     public void updateState(Preference preference) {
64         if (mChannel != null) {
65             RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
66             pref.setDisabledByAdmin(mAdmin);
67             pref.setEnabled(!pref.isDisabledByAdmin());
68             pref.setChecked(mChannel.shouldVibrate());
69         }
70     }
71 
72     @Override
onPreferenceChange(Preference preference, Object newValue)73     public boolean onPreferenceChange(Preference preference, Object newValue) {
74         if (mChannel != null) {
75             final boolean vibrate = (Boolean) newValue;
76             mChannel.enableVibration(vibrate);
77             saveChannel();
78         }
79         return true;
80     }
81 }
82