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.content.Intent;
     22 import android.database.ContentObserver;
     23 import android.net.Uri;
     24 import android.os.Handler;
     25 import android.provider.Settings;
     26 import android.support.v7.preference.Preference;
     27 import android.support.v7.preference.PreferenceScreen;
     28 import android.support.v7.preference.TwoStatePreference;
     29 
     30 import com.android.settings.core.PreferenceControllerMixin;
     31 import com.android.settingslib.core.AbstractPreferenceController;
     32 import com.android.settings.R;
     33 import com.android.settings.search.DatabaseIndexingUtils;
     34 import com.android.settings.search.InlineSwitchPayload;
     35 import com.android.settings.search.ResultPayload;
     36 import com.android.settingslib.core.lifecycle.LifecycleObserver;
     37 import com.android.settingslib.core.lifecycle.events.OnPause;
     38 import com.android.settingslib.core.lifecycle.events.OnResume;
     39 
     40 import static android.provider.Settings.Secure.NOTIFICATION_BADGING;
     41 
     42 public class BadgingNotificationPreferenceController extends AbstractPreferenceController
     43         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
     44         LifecycleObserver, OnResume, OnPause {
     45 
     46     private static final String TAG = "BadgeNotifPrefContr";
     47     private static final String KEY_NOTIFICATION_BADGING = "notification_badging";
     48     private static final int ON = 1;
     49     private static final int OFF = 0;
     50 
     51     private SettingObserver mSettingObserver;
     52 
     53     public BadgingNotificationPreferenceController(Context context) {
     54         super(context);
     55     }
     56 
     57     @Override
     58     public void displayPreference(PreferenceScreen screen) {
     59         super.displayPreference(screen);
     60         Preference preference = screen.findPreference(NOTIFICATION_BADGING);
     61         if (preference != null) {
     62             mSettingObserver = new SettingObserver(preference);
     63         }
     64     }
     65 
     66     @Override
     67     public void onResume() {
     68         if (mSettingObserver != null) {
     69             mSettingObserver.register(mContext.getContentResolver(), true /* register */);
     70         }
     71     }
     72 
     73     @Override
     74     public void onPause() {
     75         if (mSettingObserver != null) {
     76             mSettingObserver.register(mContext.getContentResolver(), false /* register */);
     77         }
     78     }
     79 
     80     @Override
     81     public String getPreferenceKey() {
     82         return KEY_NOTIFICATION_BADGING;
     83     }
     84 
     85     @Override
     86     public boolean isAvailable() {
     87         return mContext.getResources()
     88                 .getBoolean(com.android.internal.R.bool.config_notificationBadging);
     89     }
     90 
     91     @Override
     92     public void updateState(Preference preference) {
     93         final boolean checked = Settings.Secure.getInt(mContext.getContentResolver(),
     94                 NOTIFICATION_BADGING, ON) == ON;
     95         ((TwoStatePreference) preference).setChecked(checked);
     96     }
     97 
     98     @Override
     99     public boolean onPreferenceChange(Preference preference, Object newValue) {
    100         final boolean val = (Boolean) newValue;
    101         return Settings.Secure.putInt(mContext.getContentResolver(),
    102                 NOTIFICATION_BADGING, val ? ON : OFF);
    103     }
    104 
    105     class SettingObserver extends ContentObserver {
    106 
    107         private final Uri NOTIFICATION_BADGING_URI =
    108                 Settings.Secure.getUriFor(NOTIFICATION_BADGING);
    109 
    110         private final Preference mPreference;
    111 
    112         public SettingObserver(Preference preference) {
    113             super(new Handler());
    114             mPreference = preference;
    115         }
    116 
    117         public void register(ContentResolver cr, boolean register) {
    118             if (register) {
    119                 cr.registerContentObserver(NOTIFICATION_BADGING_URI, false, this);
    120             } else {
    121                 cr.unregisterContentObserver(this);
    122             }
    123         }
    124 
    125         @Override
    126         public void onChange(boolean selfChange, Uri uri) {
    127             super.onChange(selfChange, uri);
    128             if (NOTIFICATION_BADGING_URI.equals(uri)) {
    129                 updateState(mPreference);
    130             }
    131         }
    132     }
    133 
    134     @Override
    135     public ResultPayload getResultPayload() {
    136         final Intent intent = DatabaseIndexingUtils.buildSubsettingIntent(mContext,
    137                 ConfigureNotificationSettings.class.getName(), KEY_NOTIFICATION_BADGING,
    138                 mContext.getString(R.string.configure_notification_settings));
    139 
    140         return new InlineSwitchPayload(Settings.Secure.NOTIFICATION_BADGING,
    141                 ResultPayload.SettingsSource.SECURE, ON /* onValue */, intent, isAvailable(),
    142                 ON /* defaultValue */);
    143     }
    144 }
    145