Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2017 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.Secure.NOTIFICATION_BADGING;
     36 
     37 public class BadgingNotificationPreferenceController extends PreferenceController implements
     38         Preference.OnPreferenceChangeListener, LifecycleObserver, OnResume, OnPause {
     39 
     40     private static final String TAG = "BadgeNotifPrefContr";
     41     private static final String KEY_NOTIFICATION_BADGING = "notification_badging";
     42     private static final int DEFAULT_VALUE = 1;
     43 
     44     private SettingObserver mSettingObserver;
     45 
     46     public BadgingNotificationPreferenceController(Context context) {
     47         super(context);
     48     }
     49 
     50     @Override
     51     public void displayPreference(PreferenceScreen screen) {
     52         super.displayPreference(screen);
     53         Preference preference = screen.findPreference(NOTIFICATION_BADGING);
     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_BADGING;
     76     }
     77 
     78     @Override
     79     public boolean isAvailable() {
     80         return mContext.getResources()
     81                 .getBoolean(com.android.internal.R.bool.config_notificationBadging);
     82     }
     83 
     84     @Override
     85     public void updateState(Preference preference) {
     86         final boolean checked = Settings.Secure.getInt(mContext.getContentResolver(),
     87                 NOTIFICATION_BADGING, DEFAULT_VALUE) == 1;
     88         ((TwoStatePreference) preference).setChecked(checked);
     89     }
     90 
     91     @Override
     92     public boolean onPreferenceChange(Preference preference, Object newValue) {
     93         final boolean val = (Boolean) newValue;
     94         return Settings.Secure.putInt(mContext.getContentResolver(),
     95                 NOTIFICATION_BADGING, val ? 1 : 0);
     96     }
     97 
     98     class SettingObserver extends ContentObserver {
     99 
    100         private final Uri NOTIFICATION_BADGING_URI =
    101                 Settings.Secure.getUriFor(NOTIFICATION_BADGING);
    102 
    103         private final Preference mPreference;
    104 
    105         public SettingObserver(Preference preference) {
    106             super(new Handler());
    107             mPreference = preference;
    108         }
    109 
    110         public void register(ContentResolver cr, boolean register) {
    111             if (register) {
    112                 cr.registerContentObserver(NOTIFICATION_BADGING_URI, false, this);
    113             } else {
    114                 cr.unregisterContentObserver(this);
    115             }
    116         }
    117 
    118         @Override
    119         public void onChange(boolean selfChange, Uri uri) {
    120             super.onChange(selfChange, uri);
    121             if (NOTIFICATION_BADGING_URI.equals(uri)) {
    122                 updateState(mPreference);
    123             }
    124         }
    125     }
    126 }
    127