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