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.NotificationManager.IMPORTANCE_DEFAULT; 20 import static android.app.NotificationManager.IMPORTANCE_NONE; 21 import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; 22 23 import android.app.NotificationManager; 24 import android.content.Context; 25 import android.widget.Switch; 26 27 import androidx.preference.Preference; 28 29 import com.android.settings.R; 30 import com.android.settings.core.PreferenceControllerMixin; 31 import com.android.settings.widget.SwitchBar; 32 import com.android.settingslib.widget.LayoutPreference; 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 return true; 58 } 59 updateState(Preference preference)60 public void updateState(Preference preference) { 61 LayoutPreference pref = (LayoutPreference) preference; 62 pref.setSelectable(false); 63 SwitchBar bar = pref.findViewById(R.id.switch_bar); 64 if (bar != null) { 65 bar.setSwitchBarText(R.string.notification_switch_label, 66 R.string.notification_switch_label); 67 bar.show(); 68 try { 69 bar.addOnSwitchChangeListener(this); 70 } catch (IllegalStateException e) { 71 // an exception is thrown if you try to add the listener twice 72 } 73 bar.setDisabledByAdmin(mAdmin); 74 75 if (mChannel != null && !isChannelBlockable()) { 76 bar.setEnabled(false); 77 } 78 79 if (mChannelGroup != null && !isChannelGroupBlockable()) { 80 bar.setEnabled(false); 81 } 82 83 if (mChannel == null && mAppRow.systemApp 84 && (!mAppRow.banned || mAppRow.lockedImportance)) { 85 bar.setEnabled(false); 86 } 87 88 if (mChannel != null) { 89 bar.setChecked(!mAppRow.banned 90 && mChannel.getImportance() != NotificationManager.IMPORTANCE_NONE); 91 } else if (mChannelGroup != null) { 92 bar.setChecked(!mAppRow.banned && !mChannelGroup.isBlocked()); 93 } else { 94 bar.setChecked(!mAppRow.banned); 95 } 96 } 97 } 98 99 @Override onSwitchChanged(Switch switchView, boolean isChecked)100 public void onSwitchChanged(Switch switchView, boolean isChecked) { 101 boolean blocked = !isChecked; 102 if (mChannel != null) { 103 final int originalImportance = mChannel.getImportance(); 104 // setting the initial state of the switch in updateState() triggers this callback. 105 // It's always safe to override the importance if it's meant to be blocked or if 106 // it was blocked and we are unblocking it. 107 if (blocked || originalImportance == IMPORTANCE_NONE) { 108 final int importance = blocked ? IMPORTANCE_NONE 109 : isDefaultChannel() ? IMPORTANCE_UNSPECIFIED : IMPORTANCE_DEFAULT; 110 mChannel.setImportance(importance); 111 saveChannel(); 112 } 113 if (mBackend.onlyHasDefaultChannel(mAppRow.pkg, mAppRow.uid)) { 114 if (mAppRow.banned != blocked) { 115 mAppRow.banned = blocked; 116 mBackend.setNotificationsEnabledForPackage(mAppRow.pkg, mAppRow.uid, !blocked); 117 } 118 } 119 } else if (mChannelGroup != null) { 120 mChannelGroup.setBlocked(blocked); 121 mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, mChannelGroup); 122 } else if (mAppRow != null) { 123 mAppRow.banned = blocked; 124 mBackend.setNotificationsEnabledForPackage(mAppRow.pkg, mAppRow.uid, !blocked); 125 } 126 mImportanceListener.onImportanceChanged(); 127 } 128 } 129