OSDN Git Service

Fix 'show badge' for pre O apps
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / BadgePreferenceController.java
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;
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 import android.support.v7.preference.Preference;
25
26 import com.android.settings.core.PreferenceControllerMixin;
27 import com.android.settingslib.RestrictedSwitchPreference;
28
29 public class BadgePreferenceController extends NotificationPreferenceController
30         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
31
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;
36
37     public BadgePreferenceController(Context context,
38             NotificationBackend backend) {
39         super(context, backend);
40     }
41
42     @Override
43     public String getPreferenceKey() {
44         return KEY_BADGE;
45     }
46
47     @Override
48     public boolean isAvailable() {
49         if (!super.isAvailable()) {
50             return false;
51         }
52         if (mAppRow == null && mChannel == null) {
53             return false;
54         }
55         if (Settings.Secure.getInt(mContext.getContentResolver(),
56                 NOTIFICATION_BADGING, SYSTEM_WIDE_ON) == SYSTEM_WIDE_OFF) {
57             return false;
58         }
59         if (mChannel != null) {
60             if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(mChannel.getId())) {
61                 return true;
62             } else {
63                 return mAppRow.showBadge;
64             }
65         }
66         return true;
67     }
68
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());
76             } else {
77                 pref.setChecked(mAppRow.showBadge);
78             }
79         }
80     }
81
82     @Override
83     public boolean onPreferenceChange(Preference preference, Object newValue) {
84         final boolean showBadge = (Boolean) newValue;
85         if (mChannel != null) {
86             mChannel.setShowBadge(showBadge);
87             saveChannel();
88         } else if (mAppRow != null){
89             mAppRow.showBadge = showBadge;
90             mBackend.setShowBadge(mAppRow.pkg, mAppRow.uid, showBadge);
91         }
92         return true;
93     }
94
95 }