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.app.NotificationManager.IMPORTANCE_LOW; 20 import static android.app.NotificationManager.IMPORTANCE_NONE; 21 import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; 22 23 import android.app.NotificationChannel; 24 import android.app.NotificationManager; 25 import android.content.Context; 26 import android.widget.CompoundButton; 27 import android.widget.CompoundButton.OnCheckedChangeListener; 28 29 import androidx.preference.Preference; 30 31 import com.android.settings.R; 32 import com.android.settings.core.PreferenceControllerMixin; 33 import com.android.settings.notification.NotificationBackend; 34 import com.android.settings.widget.SettingsMainSwitchPreference; 35 36 public class BlockPreferenceController extends NotificationPreferenceController 37 implements PreferenceControllerMixin, OnCheckedChangeListener { 38 39 private static final String KEY_BLOCK = "block"; 40 private NotificationSettings.DependentFieldListener mDependentFieldListener; 41 BlockPreferenceController(Context context, NotificationSettings.DependentFieldListener dependentFieldListener, NotificationBackend backend)42 public BlockPreferenceController(Context context, 43 NotificationSettings.DependentFieldListener dependentFieldListener, 44 NotificationBackend backend) { 45 super(context, backend); 46 mDependentFieldListener = dependentFieldListener; 47 } 48 49 @Override getPreferenceKey()50 public String getPreferenceKey() { 51 return KEY_BLOCK; 52 } 53 54 @Override isAvailable()55 public boolean isAvailable() { 56 if (mAppRow == null) { 57 return false; 58 } 59 if (mPreferenceFilter != null && !isIncludedInFilter()) { 60 return false; 61 } 62 return true; 63 } 64 65 @Override isIncludedInFilter()66 boolean isIncludedInFilter() { 67 return mPreferenceFilter.contains(NotificationChannel.EDIT_IMPORTANCE); 68 } 69 updateState(Preference preference)70 public void updateState(Preference preference) { 71 SettingsMainSwitchPreference bar = (SettingsMainSwitchPreference) preference; 72 if (bar != null) { 73 String switchBarText = getSwitchBarText(); 74 bar.setTitle(switchBarText); 75 bar.show(); 76 try { 77 bar.addOnSwitchChangeListener(this); 78 } catch (IllegalStateException e) { 79 // an exception is thrown if you try to add the listener twice 80 } 81 bar.setDisabledByAdmin(mAdmin); 82 83 if (mChannel != null && (!isChannelBlockable() || !isChannelConfigurable(mChannel))) { 84 bar.setSwitchBarEnabled(false); 85 } 86 87 if (mChannelGroup != null && !isChannelGroupBlockable()) { 88 bar.setSwitchBarEnabled(false); 89 } 90 91 if (mChannel == null && !isAppBlockable()) { 92 bar.setSwitchBarEnabled(false); 93 } 94 95 if (mChannel != null) { 96 bar.setChecked(!mAppRow.banned 97 && mChannel.getImportance() != NotificationManager.IMPORTANCE_NONE); 98 } else if (mChannelGroup != null) { 99 bar.setChecked(!mAppRow.banned && !mChannelGroup.isBlocked()); 100 } else { 101 bar.setChecked(!mAppRow.banned); 102 } 103 } 104 } 105 106 @Override onCheckedChanged(CompoundButton buttonView, boolean isChecked)107 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 108 boolean blocked = !isChecked; 109 if (mChannel != null) { 110 final int originalImportance = mChannel.getImportance(); 111 // setting the initial state of the switch in updateState() triggers this callback. 112 // It's always safe to override the importance if it's meant to be blocked or if 113 // it was blocked and we are unblocking it. 114 if (blocked || originalImportance == IMPORTANCE_NONE) { 115 final int importance = blocked 116 ? IMPORTANCE_NONE 117 : isDefaultChannel() 118 ? IMPORTANCE_UNSPECIFIED 119 : Math.max(mChannel.getOriginalImportance(), IMPORTANCE_LOW); 120 mChannel.setImportance(importance); 121 saveChannel(); 122 } 123 if (mBackend.onlyHasDefaultChannel(mAppRow.pkg, mAppRow.uid)) { 124 if (mAppRow.banned != blocked) { 125 mAppRow.banned = blocked; 126 mBackend.setNotificationsEnabledForPackage(mAppRow.pkg, mAppRow.uid, !blocked); 127 } 128 } 129 } else if (mChannelGroup != null) { 130 mChannelGroup.setBlocked(blocked); 131 mBackend.updateChannelGroup(mAppRow.pkg, mAppRow.uid, mChannelGroup); 132 } else if (mAppRow != null) { 133 mAppRow.banned = blocked; 134 mBackend.setNotificationsEnabledForPackage(mAppRow.pkg, mAppRow.uid, !blocked); 135 } 136 mDependentFieldListener.onFieldValueChanged(); 137 } 138 getSwitchBarText()139 String getSwitchBarText() { 140 if (mChannel != null) { 141 return mContext.getString(R.string.notification_content_block_title); 142 } else { 143 CharSequence fieldContextName; 144 if (mChannelGroup != null) { 145 fieldContextName = mChannelGroup.getName(); 146 } else { 147 fieldContextName = mAppRow.label; 148 } 149 return mContext.getString(R.string.notification_app_switch_label, fieldContextName); 150 } 151 } 152 } 153