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