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.NotificationManager; 20 import android.content.Context; 21 import android.os.Vibrator; 22 23 import androidx.preference.Preference; 24 25 import com.android.settings.core.PreferenceControllerMixin; 26 import com.android.settings.notification.NotificationBackend; 27 import com.android.settings.notification.app.NotificationPreferenceController; 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 = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 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 updateState(Preference preference)57 public void updateState(Preference preference) { 58 if (mChannel != null) { 59 RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference; 60 pref.setDisabledByAdmin(mAdmin); 61 pref.setEnabled(!pref.isDisabledByAdmin()); 62 pref.setChecked(mChannel.shouldVibrate()); 63 } 64 } 65 66 @Override onPreferenceChange(Preference preference, Object newValue)67 public boolean onPreferenceChange(Preference preference, Object newValue) { 68 if (mChannel != null) { 69 final boolean vibrate = (Boolean) newValue; 70 mChannel.enableVibration(vibrate); 71 saveChannel(); 72 } 73 return true; 74 } 75 } 76