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