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