• 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;
18 
19 import static android.app.NotificationChannel.DEFAULT_CHANNEL_ID;
20 import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
21 import static android.app.NotificationManager.IMPORTANCE_NONE;
22 import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
23 
24 import android.app.NotificationManager;
25 import android.content.Context;
26 import android.support.v7.preference.Preference;
27 import android.widget.Switch;
28 
29 import com.android.settings.R;
30 import com.android.settings.applications.LayoutPreference;
31 import com.android.settings.core.PreferenceControllerMixin;
32 import com.android.settings.widget.SwitchBar;
33 
34 public class BlockPreferenceController extends NotificationPreferenceController
35         implements PreferenceControllerMixin, SwitchBar.OnSwitchChangeListener {
36 
37     private static final String KEY_BLOCK = "block";
38     private NotificationSettingsBase.ImportanceListener mImportanceListener;
39 
BlockPreferenceController(Context context, NotificationSettingsBase.ImportanceListener importanceListener, NotificationBackend backend)40     public BlockPreferenceController(Context context,
41             NotificationSettingsBase.ImportanceListener importanceListener,
42             NotificationBackend backend) {
43         super(context, backend);
44         mImportanceListener = importanceListener;
45     }
46 
47     @Override
getPreferenceKey()48     public String getPreferenceKey() {
49         return KEY_BLOCK;
50     }
51 
52     @Override
isAvailable()53     public boolean isAvailable() {
54         if (mAppRow == null) {
55             return false;
56         }
57         if (mChannel != null) {
58             return isChannelBlockable();
59         } else if (mChannelGroup != null) {
60             return isChannelGroupBlockable();
61         } else {
62             return !mAppRow.systemApp || (mAppRow.systemApp && mAppRow.banned);
63         }
64     }
65 
updateState(Preference preference)66     public void updateState(Preference preference) {
67         LayoutPreference pref = (LayoutPreference) preference;
68         SwitchBar bar = pref.findViewById(R.id.switch_bar);
69         if (bar != null) {
70             bar.setSwitchBarText(R.string.notification_switch_label,
71                     R.string.notification_switch_label);
72             bar.show();
73             try {
74                 bar.addOnSwitchChangeListener(this);
75             } catch (IllegalStateException e) {
76                 // an exception is thrown if you try to add the listener twice
77             }
78             bar.setDisabledByAdmin(mAdmin);
79 
80             if (mChannel != null) {
81                 bar.setChecked(!mAppRow.banned
82                         && mChannel.getImportance() != NotificationManager.IMPORTANCE_NONE);
83             } else if (mChannelGroup != null) {
84                 bar.setChecked(!mAppRow.banned && !mChannelGroup.isBlocked());
85             } else {
86                 bar.setChecked(!mAppRow.banned);
87             }
88         }
89     }
90 
91     @Override
onSwitchChanged(Switch switchView, boolean isChecked)92     public void onSwitchChanged(Switch switchView, boolean isChecked) {
93         boolean blocked = !isChecked;
94         if (mChannel != null) {
95             final int originalImportance = mChannel.getImportance();
96             // setting the initial state of the switch in updateState() triggers this callback.
97             // It's always safe to override the importance if it's meant to be blocked or if
98             // it was blocked and we are unblocking it.
99             if (blocked || originalImportance == IMPORTANCE_NONE) {
100                 final int importance = blocked ? IMPORTANCE_NONE
101                         : isDefaultChannel() ? IMPORTANCE_UNSPECIFIED : IMPORTANCE_DEFAULT;
102                 mChannel.setImportance(importance);
103                 saveChannel();
104             }
105             if (mBackend.onlyHasDefaultChannel(mAppRow.pkg, mAppRow.uid)) {
106                 if (mAppRow.banned != blocked) {
107                     mAppRow.banned = blocked;
108                     mBackend.setNotificationsEnabledForPackage(mAppRow.pkg, mAppRow.uid, !blocked);
109                 }
110             }
111         } else if (mChannelGroup != null) {
112             mChannelGroup.setBlocked(blocked);
113             mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, mChannelGroup);
114         } else if (mAppRow != null) {
115             mAppRow.banned = blocked;
116             mBackend.setNotificationsEnabledForPackage(mAppRow.pkg, mAppRow.uid, !blocked);
117         }
118         mImportanceListener.onImportanceChanged();
119     }
120 }
121