1 /* 2 * Copyright (C) 2016 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; 18 19 import static android.provider.Settings.System.VIBRATE_WHEN_RINGING; 20 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.database.ContentObserver; 24 import android.net.Uri; 25 import android.os.Handler; 26 import android.provider.Settings; 27 import android.support.v7.preference.Preference; 28 import android.support.v7.preference.PreferenceScreen; 29 import android.text.TextUtils; 30 31 import com.android.settings.Utils; 32 import com.android.settings.core.TogglePreferenceController; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnPause; 35 import com.android.settingslib.core.lifecycle.events.OnResume; 36 37 public class VibrateWhenRingPreferenceController extends TogglePreferenceController 38 implements LifecycleObserver, OnResume, OnPause { 39 40 private static final String KEY_VIBRATE_WHEN_RINGING = "vibrate_when_ringing"; 41 private final int DEFAULT_VALUE = 0; 42 private final int NOTIFICATION_VIBRATE_WHEN_RINGING = 1; 43 private SettingObserver mSettingObserver; 44 VibrateWhenRingPreferenceController(Context context, String key)45 public VibrateWhenRingPreferenceController(Context context, String key) { 46 super(context, key); 47 } 48 49 @Override isChecked()50 public boolean isChecked() { 51 return Settings.System.getInt(mContext.getContentResolver(), 52 VIBRATE_WHEN_RINGING, DEFAULT_VALUE) != DEFAULT_VALUE; 53 } 54 55 @Override setChecked(boolean isChecked)56 public boolean setChecked(boolean isChecked) { 57 return Settings.System.putInt(mContext.getContentResolver(), VIBRATE_WHEN_RINGING, 58 isChecked ? NOTIFICATION_VIBRATE_WHEN_RINGING : DEFAULT_VALUE); 59 } 60 61 @Override 62 @AvailabilityStatus getAvailabilityStatus()63 public int getAvailabilityStatus() { 64 return Utils.isVoiceCapable(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 65 } 66 67 @Override isSliceable()68 public boolean isSliceable() { 69 return TextUtils.equals(getPreferenceKey(), "vibrate_when_ringing"); 70 } 71 72 @Override displayPreference(PreferenceScreen screen)73 public void displayPreference(PreferenceScreen screen) { 74 super.displayPreference(screen); 75 Preference preference = screen.findPreference(KEY_VIBRATE_WHEN_RINGING); 76 if (preference != null) { 77 mSettingObserver = new SettingObserver(preference); 78 preference.setPersistent(false); 79 } 80 } 81 82 @Override onResume()83 public void onResume() { 84 if (mSettingObserver != null) { 85 mSettingObserver.register(true /* register */); 86 } 87 } 88 89 @Override onPause()90 public void onPause() { 91 if (mSettingObserver != null) { 92 mSettingObserver.register(false /* register */); 93 } 94 } 95 96 private final class SettingObserver extends ContentObserver { 97 98 private final Uri VIBRATE_WHEN_RINGING_URI = 99 Settings.System.getUriFor(VIBRATE_WHEN_RINGING); 100 101 private final Preference mPreference; 102 SettingObserver(Preference preference)103 public SettingObserver(Preference preference) { 104 super(new Handler()); 105 mPreference = preference; 106 } 107 register(boolean register)108 public void register(boolean register) { 109 final ContentResolver cr = mContext.getContentResolver(); 110 if (register) { 111 cr.registerContentObserver(VIBRATE_WHEN_RINGING_URI, false, this); 112 } else { 113 cr.unregisterContentObserver(this); 114 } 115 } 116 117 @Override onChange(boolean selfChange, Uri uri)118 public void onChange(boolean selfChange, Uri uri) { 119 super.onChange(selfChange, uri); 120 if (VIBRATE_WHEN_RINGING_URI.equals(uri)) { 121 updateState(mPreference); 122 } 123 } 124 } 125 } 126