2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
19 import static android.provider.Settings.Secure.NOTIFICATION_BADGING;
21 import android.app.NotificationChannel;
22 import android.content.Context;
23 import android.provider.Settings;
24 import android.support.v7.preference.Preference;
26 import com.android.settings.core.PreferenceControllerMixin;
27 import com.android.settingslib.RestrictedSwitchPreference;
29 public class BadgePreferenceController extends NotificationPreferenceController
30 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
32 private static final String TAG = "BadgePrefContr";
33 private static final String KEY_BADGE = "badge";
34 private static final int SYSTEM_WIDE_ON = 1;
35 private static final int SYSTEM_WIDE_OFF = 0;
37 public BadgePreferenceController(Context context,
38 NotificationBackend backend) {
39 super(context, backend);
43 public String getPreferenceKey() {
48 public boolean isAvailable() {
49 if (!super.isAvailable()) {
52 if (mAppRow == null && mChannel == null) {
55 if (Settings.Secure.getInt(mContext.getContentResolver(),
56 NOTIFICATION_BADGING, SYSTEM_WIDE_ON) == SYSTEM_WIDE_OFF) {
59 if (mChannel != null) {
60 if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(mChannel.getId())) {
63 return mAppRow.showBadge;
69 public void updateState(Preference preference) {
70 if (mAppRow != null) {
71 RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
72 pref.setDisabledByAdmin(mAdmin);
73 if (mChannel != null) {
74 pref.setChecked(mChannel.canShowBadge());
75 pref.setEnabled(isChannelConfigurable() && !pref.isDisabledByAdmin());
77 pref.setChecked(mAppRow.showBadge);
83 public boolean onPreferenceChange(Preference preference, Object newValue) {
84 final boolean showBadge = (Boolean) newValue;
85 if (mChannel != null) {
86 mChannel.setShowBadge(showBadge);
88 } else if (mAppRow != null){
89 mAppRow.showBadge = showBadge;
90 mBackend.setShowBadge(mAppRow.pkg, mAppRow.uid, showBadge);