• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             if (preference.getParent() != null) {
86                 preference.getParent().setVisible(true);
87             }
88             RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
89             pref.setDisabledByAdmin(mAdmin);
90             if (mChannel != null) {
91                 pref.setChecked(mChannel.canShowBadge());
92                 pref.setEnabled(!pref.isDisabledByAdmin());
93             } else {
94                 pref.setChecked(mAppRow.showBadge);
95             }
96         }
97     }
98 
99     @Override
onPreferenceChange(Preference preference, Object newValue)100     public boolean onPreferenceChange(Preference preference, Object newValue) {
101         final boolean showBadge = (Boolean) newValue;
102         if (mChannel != null) {
103             mChannel.setShowBadge(showBadge);
104             saveChannel();
105         } else if (mAppRow != null){
106             mAppRow.showBadge = showBadge;
107             mBackend.setShowBadge(mAppRow.pkg, mAppRow.uid, showBadge);
108         }
109         return true;
110     }
111 
112 }
113