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 static android.provider.Settings.Secure.NOTIFICATION_BADGING; 20 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.database.ContentObserver; 24 import android.net.Uri; 25 import android.os.Handler; 26 import android.provider.Settings; 27 import android.text.TextUtils; 28 29 import androidx.annotation.VisibleForTesting; 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.R; 34 import com.android.settings.core.PreferenceControllerMixin; 35 import com.android.settings.core.TogglePreferenceController; 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 public class BadgingNotificationPreferenceController extends TogglePreferenceController 41 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener, 42 LifecycleObserver, OnResume, OnPause { 43 44 private static final String TAG = "BadgeNotifPrefContr"; 45 @VisibleForTesting 46 static final int ON = 1; 47 @VisibleForTesting 48 static final int OFF = 0; 49 50 private SettingObserver mSettingObserver; 51 BadgingNotificationPreferenceController(Context context, String preferenceKey)52 public BadgingNotificationPreferenceController(Context context, String preferenceKey) { 53 super(context, preferenceKey); 54 } 55 56 @Override displayPreference(PreferenceScreen screen)57 public void displayPreference(PreferenceScreen screen) { 58 super.displayPreference(screen); 59 Preference preference = screen.findPreference(NOTIFICATION_BADGING); 60 if (preference != null) { 61 mSettingObserver = new SettingObserver(preference); 62 } 63 } 64 65 @Override onResume()66 public void onResume() { 67 if (mSettingObserver != null) { 68 mSettingObserver.register(mContext.getContentResolver(), true /* register */); 69 } 70 } 71 72 @Override onPause()73 public void onPause() { 74 if (mSettingObserver != null) { 75 mSettingObserver.register(mContext.getContentResolver(), false /* register */); 76 } 77 } 78 79 @Override getAvailabilityStatus()80 public int getAvailabilityStatus() { 81 return mContext.getResources() 82 .getBoolean(com.android.internal.R.bool.config_notificationBadging) 83 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 84 } 85 86 @Override isSliceable()87 public boolean isSliceable() { 88 return TextUtils.equals(getPreferenceKey(), "notification_badging"); 89 } 90 91 @Override getSliceHighlightMenuRes()92 public int getSliceHighlightMenuRes() { 93 return R.string.menu_key_notifications; 94 } 95 96 @Override isChecked()97 public boolean isChecked() { 98 return Settings.Secure.getInt(mContext.getContentResolver(), 99 NOTIFICATION_BADGING, ON) == ON; 100 } 101 102 @Override setChecked(boolean isChecked)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 SettingObserver(Preference preference)115 public SettingObserver(Preference preference) { 116 super(new Handler()); 117 mPreference = preference; 118 } 119 register(ContentResolver cr, boolean register)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 onChange(boolean selfChange, Uri uri)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