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 ? false : mAppRow.showBadge;
66             }
67         }
68         return true;
69     }
70 
71     @Override
isIncludedInFilter()72     boolean isIncludedInFilter() {
73         return mPreferenceFilter.contains(NotificationChannel.EDIT_LAUNCHER);
74     }
75 
updateState(Preference preference)76     public void updateState(Preference preference) {
77         if (mAppRow != null) {
78             RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
79             pref.setDisabledByAdmin(mAdmin);
80             if (mChannel != null) {
81                 pref.setChecked(mChannel.canShowBadge());
82                 pref.setEnabled(!pref.isDisabledByAdmin());
83             } else {
84                 pref.setChecked(mAppRow.showBadge);
85             }
86         }
87     }
88 
89     @Override
onPreferenceChange(Preference preference, Object newValue)90     public boolean onPreferenceChange(Preference preference, Object newValue) {
91         final boolean showBadge = (Boolean) newValue;
92         if (mChannel != null) {
93             mChannel.setShowBadge(showBadge);
94             saveChannel();
95         } else if (mAppRow != null){
96             mAppRow.showBadge = showBadge;
97             mBackend.setShowBadge(mAppRow.pkg, mAppRow.uid, showBadge);
98         }
99         return true;
100     }
101 
102 }
103