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