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