2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
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;
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;
36 import static android.provider.Settings.System.NOTIFICATION_LIGHT_PULSE;
38 public class PulseNotificationPreferenceController extends AbstractPreferenceController
39 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
40 LifecycleObserver, OnResume, OnPause {
42 private static final String TAG = "PulseNotifPrefContr";
43 private static final String KEY_NOTIFICATION_PULSE = "notification_pulse";
44 private SettingObserver mSettingObserver;
46 public PulseNotificationPreferenceController(Context context) {
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);
60 public void onResume() {
61 if (mSettingObserver != null) {
62 mSettingObserver.register(mContext.getContentResolver(), true /* register */);
67 public void onPause() {
68 if (mSettingObserver != null) {
69 mSettingObserver.register(mContext.getContentResolver(), false /* register */);
74 public String getPreferenceKey() {
75 return KEY_NOTIFICATION_PULSE;
79 public boolean isAvailable() {
80 return mContext.getResources()
81 .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed);
85 public void updateState(Preference preference) {
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");
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);
102 class SettingObserver extends ContentObserver {
104 private final Uri NOTIFICATION_LIGHT_PULSE_URI =
105 Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
107 private final Preference mPreference;
109 public SettingObserver(Preference preference) {
110 super(new Handler());
111 mPreference = preference;
114 public void register(ContentResolver cr, boolean register) {
116 cr.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, false, this);
118 cr.unregisterContentObserver(this);
123 public void onChange(boolean selfChange, Uri uri) {
124 super.onChange(selfChange, uri);
125 if (NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) {
126 updateState(mPreference);