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