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.app; 18 19 import static android.provider.Settings.Secure.NOTIFICATION_BADGING; 20 21 import android.app.NotificationChannel; 22 import android.content.Context; 23 import android.provider.Settings; 24 25 import androidx.preference.Preference; 26 27 import com.android.settings.core.PreferenceControllerMixin; 28 import com.android.settings.notification.NotificationBackend; 29 import com.android.settingslib.RestrictedSwitchPreference; 30 31 public class BadgePreferenceController extends NotificationPreferenceController 32 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener { 33 34 private static final String TAG = "BadgePrefContr"; 35 private static final String KEY_BADGE = "badge"; 36 private static final int SYSTEM_WIDE_ON = 1; 37 private static final int SYSTEM_WIDE_OFF = 0; 38 BadgePreferenceController(Context context, NotificationBackend backend)39 public BadgePreferenceController(Context context, 40 NotificationBackend backend) { 41 super(context, backend); 42 } 43 44 @Override getPreferenceKey()45 public String getPreferenceKey() { 46 return KEY_BADGE; 47 } 48 49 @Override isAvailable()50 public boolean isAvailable() { 51 if (!super.isAvailable()) { 52 return false; 53 } 54 if (mAppRow == null && mChannel == null) { 55 return false; 56 } 57 if (Settings.Secure.getInt(mContext.getContentResolver(), 58 NOTIFICATION_BADGING, SYSTEM_WIDE_ON) == SYSTEM_WIDE_OFF) { 59 return false; 60 } 61 if (mChannel != null) { 62 if (isDefaultChannel()) { 63 return true; 64 } else { 65 return mAppRow == null 66 ? false 67 : mAppRow.channelCount == 0 68 ? false 69 : mAppRow.showBadge; 70 } 71 } 72 if (mAppRow.channelCount == 0) { 73 return false; 74 } 75 return true; 76 } 77 78 @Override isIncludedInFilter()79 boolean isIncludedInFilter() { 80 return mPreferenceFilter.contains(NotificationChannel.EDIT_LAUNCHER); 81 } 82 updateState(Preference preference)83 public void updateState(Preference preference) { 84 if (mAppRow != null) { 85 RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference; 86 pref.setDisabledByAdmin(mAdmin); 87 if (mChannel != null) { 88 pref.setChecked(mChannel.canShowBadge()); 89 pref.setEnabled(!pref.isDisabledByAdmin()); 90 } else { 91 pref.setChecked(mAppRow.showBadge); 92 } 93 } 94 } 95 96 @Override onPreferenceChange(Preference preference, Object newValue)97 public boolean onPreferenceChange(Preference preference, Object newValue) { 98 final boolean showBadge = (Boolean) newValue; 99 if (mChannel != null) { 100 mChannel.setShowBadge(showBadge); 101 saveChannel(); 102 } else if (mAppRow != null){ 103 mAppRow.showBadge = showBadge; 104 mBackend.setShowBadge(mAppRow.pkg, mAppRow.uid, showBadge); 105 } 106 return true; 107 } 108 109 } 110