1 /* 2 * Copyright (C) 2021 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.zen; 18 19 import static com.android.settings.notification.zen.ZenPrioritySendersHelper.UNKNOWN; 20 21 import android.content.Context; 22 import android.os.AsyncTask; 23 import android.service.notification.ZenPolicy; 24 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceCategory; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.internal.annotations.VisibleForTesting; 30 import com.android.settings.notification.NotificationBackend; 31 import com.android.settingslib.core.lifecycle.Lifecycle; 32 import com.android.settingslib.widget.SelectorWithWidgetPreference; 33 34 /** 35 * Shared controller for custom rule priority senders settings for both messages and calls. 36 * 37 * Most functionality is the same as that of the main zen mode messages and calls settings; 38 * these controllers handle which senders are allowed to break through DND for messages or calls, 39 * with possible settings options being: starred contacts, all contacts, priority conversations 40 * (for messages only), anyone, or no one. 41 */ 42 public class ZenRulePrioritySendersPreferenceController 43 extends AbstractZenCustomRulePreferenceController { 44 private final boolean mIsMessages; // if this is false, then this preference is for calls 45 46 private PreferenceCategory mPreferenceCategory; 47 private ZenPrioritySendersHelper mHelper; 48 ZenRulePrioritySendersPreferenceController(Context context, String key, Lifecycle lifecycle, boolean isMessages, NotificationBackend notificationBackend)49 public ZenRulePrioritySendersPreferenceController(Context context, String key, 50 Lifecycle lifecycle, boolean isMessages, NotificationBackend notificationBackend) { 51 super(context, key, lifecycle); 52 mIsMessages = isMessages; 53 54 mHelper = new ZenPrioritySendersHelper( 55 context, isMessages, mBackend, notificationBackend, mSelectorClickListener); 56 } 57 58 @Override displayPreference(PreferenceScreen screen)59 public void displayPreference(PreferenceScreen screen) { 60 mPreferenceCategory = screen.findPreference(getPreferenceKey()); 61 mHelper.displayPreference(mPreferenceCategory); 62 super.displayPreference(screen); 63 } 64 65 @Override getPreferenceKey()66 public String getPreferenceKey() { 67 return KEY; 68 } 69 70 @Override updateState(Preference preference)71 public void updateState(Preference preference) { 72 super.updateState(preference); 73 if (mRule != null && mRule.getZenPolicy() != null) { 74 final int currContactsSetting = getPrioritySenders(); 75 final int currConversationsSetting = getPriorityConversationSenders(); 76 mHelper.updateState(currContactsSetting, currConversationsSetting); 77 } 78 } 79 80 @Override onResume()81 public void onResume() { 82 super.onResume(); 83 if (mIsMessages) { 84 updateChannelCounts(); 85 } 86 mHelper.updateSummaries(); 87 } 88 updateChannelCounts()89 private void updateChannelCounts() { 90 // Load conversations 91 new AsyncTask<Void, Void, Void>() { 92 @Override 93 protected Void doInBackground(Void... unused) { 94 mHelper.updateChannelCounts(); 95 return null; 96 } 97 98 @Override 99 protected void onPostExecute(Void unused) { 100 if (mContext == null) { 101 return; 102 } 103 updateState(mPreferenceCategory); 104 } 105 }.execute(); 106 } 107 getPrioritySenders()108 private int getPrioritySenders() { 109 if (mRule == null || mRule.getZenPolicy() == null) { 110 return UNKNOWN; 111 } 112 if (mIsMessages) { 113 return ZenModeBackend.getContactSettingFromZenPolicySetting( 114 mRule.getZenPolicy().getPriorityMessageSenders()); 115 } else { 116 return ZenModeBackend.getContactSettingFromZenPolicySetting( 117 mRule.getZenPolicy().getPriorityCallSenders()); 118 } 119 } 120 getPriorityConversationSenders()121 private int getPriorityConversationSenders() { 122 if (mRule == null || mRule.getZenPolicy() == null) { 123 return UNKNOWN; 124 } 125 return mRule.getZenPolicy().getPriorityConversationSenders(); 126 } 127 128 // Returns the ZenPolicySetting enum associated with the provided NotificationManager.Policy. zenPolicySettingFromSender(int senderSetting)129 static @ZenPolicy.PeopleType int zenPolicySettingFromSender(int senderSetting) { 130 return ZenModeBackend.getZenPolicySettingFromPrefKey( 131 ZenModeBackend.getKeyFromSetting(senderSetting)); 132 } 133 134 @VisibleForTesting 135 SelectorWithWidgetPreference.OnClickListener mSelectorClickListener = 136 new SelectorWithWidgetPreference.OnClickListener() { 137 @Override 138 public void onRadioButtonClicked(SelectorWithWidgetPreference preference) { 139 if (mRule == null || mRule.getZenPolicy() == null) { 140 return; 141 } 142 143 final int[] settingsToSave = mHelper.settingsToSaveOnClick(preference, 144 getPrioritySenders(), getPriorityConversationSenders()); 145 final int prioritySendersSetting = settingsToSave[0]; 146 final int priorityConvosSetting = settingsToSave[1]; 147 148 // if both are UNKNOWN then just return 149 if (prioritySendersSetting == UNKNOWN && priorityConvosSetting == UNKNOWN) { 150 return; 151 } 152 153 if (prioritySendersSetting != UNKNOWN) { 154 if (mIsMessages) { 155 mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy()) 156 .allowMessages( 157 zenPolicySettingFromSender(prioritySendersSetting)) 158 .build()); 159 } else { 160 mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy()) 161 .allowCalls( 162 zenPolicySettingFromSender(prioritySendersSetting)) 163 .build()); 164 } 165 } 166 167 if (mIsMessages && priorityConvosSetting != UNKNOWN) { 168 mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy()) 169 .allowConversations(priorityConvosSetting) 170 .build()); 171 } 172 173 // Save any changes 174 mBackend.updateZenRule(mId, mRule); 175 } 176 }; 177 } 178