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 import android.util.Log; 29 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.NOTIFICATION_LIGHT_PULSE; 37 38 public class PulseNotificationPreferenceController extends AbstractPreferenceController 39 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener, 40 LifecycleObserver, OnResume, OnPause { 41 42 private static final String TAG = "PulseNotifPrefContr"; 43 private static final String KEY_NOTIFICATION_PULSE = "notification_pulse"; 44 private SettingObserver mSettingObserver; 45 46 public PulseNotificationPreferenceController(Context context) { 47 super(context); 48 } 49 50 @Override 51 public void displayPreference(PreferenceScreen screen) { 52 super.displayPreference(screen); 53 Preference preference = screen.findPreference(KEY_NOTIFICATION_PULSE); 54 if (preference != null) { 55 mSettingObserver = new SettingObserver(preference); 56 } 57 } 58 59 @Override 60 public void onResume() { 61 if (mSettingObserver != null) { 62 mSettingObserver.register(mContext.getContentResolver(), true /* register */); 63 } 64 } 65 66 @Override 67 public void onPause() { 68 if (mSettingObserver != null) { 69 mSettingObserver.register(mContext.getContentResolver(), false /* register */); 70 } 71 } 72 73 @Override 74 public String getPreferenceKey() { 75 return KEY_NOTIFICATION_PULSE; 76 } 77 78 @Override 79 public boolean isAvailable() { 80 return mContext.getResources() 81 .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed); 82 } 83 84 @Override 85 public void updateState(Preference preference) { 86 try { 87 final boolean checked = Settings.System.getInt(mContext.getContentResolver(), 88 NOTIFICATION_LIGHT_PULSE) == 1; 89 ((TwoStatePreference) preference).setChecked(checked); 90 } catch (Settings.SettingNotFoundException snfe) { 91 Log.e(TAG, NOTIFICATION_LIGHT_PULSE + " not found"); 92 } 93 } 94 95 @Override 96 public boolean onPreferenceChange(Preference preference, Object newValue) { 97 final boolean val = (Boolean) newValue; 98 return Settings.System.putInt(mContext.getContentResolver(), 99 NOTIFICATION_LIGHT_PULSE, val ? 1 : 0); 100 } 101 102 class SettingObserver extends ContentObserver { 103 104 private final Uri NOTIFICATION_LIGHT_PULSE_URI = 105 Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); 106 107 private final Preference mPreference; 108 109 public SettingObserver(Preference preference) { 110 super(new Handler()); 111 mPreference = preference; 112 } 113 114 public void register(ContentResolver cr, boolean register) { 115 if (register) { 116 cr.registerContentObserver(NOTIFICATION_LIGHT_PULSE_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 (NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { 126 updateState(mPreference); 127 } 128 } 129 } 130 } 131