Merge "Merge messages & conversations settings for DND priority senders."

This commit is contained in:
Yuri Lin
2021-10-18 15:03:27 +00:00
committed by Android (Google) Code Review
17 changed files with 1585 additions and 302 deletions

View File

@@ -18,12 +18,12 @@ package com.android.settings.notification.zen;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.service.notification.ZenPolicy;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.notification.NotificationBackend;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.widget.FooterPreference;
@@ -32,7 +32,6 @@ import java.util.List;
public class ZenCustomRuleMessagesSettings extends ZenCustomRuleSettingsBase {
private static final String MESSAGES_KEY = "zen_mode_messages";
private static final String STARRED_CONTACTS_KEY = "zen_mode_starred_contacts_messages";
private static final String PREFERENCE_CATEGORY_KEY = "zen_mode_settings_category_messages";
@Override
@@ -48,11 +47,9 @@ public class ZenCustomRuleMessagesSettings extends ZenCustomRuleSettingsBase {
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
mControllers = new ArrayList<>();
mControllers.add(new ZenRuleMessagesPreferenceController(context, MESSAGES_KEY,
getSettingsLifecycle()));
mControllers.add(new ZenRuleStarredContactsPreferenceController(context,
getSettingsLifecycle(), ZenPolicy.PRIORITY_CATEGORY_MESSAGES,
STARRED_CONTACTS_KEY));
mControllers.add(new ZenRulePrioritySendersPreferenceController(context,
PREFERENCE_CATEGORY_KEY, getSettingsLifecycle(), true,
new NotificationBackend()));
return mControllers;
}
@@ -65,6 +62,8 @@ public class ZenCustomRuleMessagesSettings extends ZenCustomRuleSettingsBase {
public void updatePreferences() {
super.updatePreferences();
PreferenceScreen screen = getPreferenceScreen();
// TODO(b/200600958): It seems that this string does not currently update to indicate when
// messages aren't in fact blocked by the rule.
Preference footerPreference = screen.findPreference(FooterPreference.KEY_FOOTER);
footerPreference.setTitle(mContext.getResources().getString(
R.string.zen_mode_custom_messages_footer, mRule.getName()));

View File

@@ -287,6 +287,20 @@ public class ZenModeBackend {
}
}
protected static int getContactSettingFromZenPolicySetting(int setting) {
switch (setting) {
case ZenPolicy.PEOPLE_TYPE_ANYONE:
return NotificationManager.Policy.PRIORITY_SENDERS_ANY;
case ZenPolicy.PEOPLE_TYPE_CONTACTS:
return NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS;
case ZenPolicy.PEOPLE_TYPE_STARRED:
return NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
case ZenPolicy.PEOPLE_TYPE_NONE:
default:
return SOURCE_NONE;
}
}
protected int getAlarmsTotalSilencePeopleSummary(int category) {
if (category == NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) {
return R.string.zen_mode_none_messages;

View File

@@ -21,6 +21,7 @@ import android.content.Context;
import android.provider.SearchIndexableResource;
import com.android.settings.R;
import com.android.settings.notification.NotificationBackend;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -44,9 +45,8 @@ public class ZenModeCallsSettings extends ZenModeSettingsBase {
Lifecycle lifecycle) {
List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new ZenModePrioritySendersPreferenceController(context,
"zen_mode_settings_category_calls", lifecycle, false));
controllers.add(new ZenModeSendersImagePreferenceController(context,
"zen_mode_calls_image", lifecycle, false));
"zen_mode_settings_category_calls", lifecycle, false,
new NotificationBackend()));
controllers.add(new ZenModeRepeatCallersPreferenceController(context, lifecycle,
context.getResources().getInteger(com.android.internal.R.integer
.config_zen_repeat_callers_threshold)));

View File

@@ -21,6 +21,7 @@ import android.content.Context;
import android.provider.SearchIndexableResource;
import com.android.settings.R;
import com.android.settings.notification.NotificationBackend;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -44,10 +45,9 @@ public class ZenModeMessagesSettings extends ZenModeSettingsBase {
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
Lifecycle lifecycle) {
List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new ZenModeSendersImagePreferenceController(context,
"zen_mode_messages_image", lifecycle, true));
controllers.add(new ZenModePrioritySendersPreferenceController(context,
"zen_mode_settings_category_messages", lifecycle, true));
"zen_mode_settings_category_messages", lifecycle, true,
new NotificationBackend()));
controllers.add(new ZenModeBehaviorFooterPreferenceController(
context, lifecycle, R.string.zen_mode_messages_footer));
return controllers;

View File

@@ -56,10 +56,6 @@ public class ZenModePeopleSettings extends ZenModeSettingsBase implements Indexa
Lifecycle lifecycle, Application app, Fragment host, FragmentManager fragmentManager,
NotificationBackend notificationBackend) {
List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new ZenModeConversationsImagePreferenceController(context,
"zen_mode_conversations_image", lifecycle, notificationBackend));
controllers.add(new ZenModeConversationsPreferenceController(context,
"zen_mode_conversations", lifecycle));
controllers.add(new ZenModeCallsPreferenceController(context, lifecycle,
"zen_mode_people_calls"));
controllers.add(new ZenModeMessagesPreferenceController(context, lifecycle,

View File

@@ -19,83 +19,53 @@ package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CALLS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
import android.app.NotificationManager;
import static com.android.settings.notification.zen.ZenPrioritySendersHelper.UNKNOWN;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.Contacts;
import android.view.View;
import android.os.AsyncTask;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.notification.NotificationBackend;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
import java.util.ArrayList;
import java.util.List;
/**
* Common preference controller functionality shared by
* ZenModePriorityMessagesPreferenceController and ZenModePriorityCallsPreferenceController.
* Common preference controller functionality for zen mode priority senders preferences for both
* messages and calls.
*
* This includes the options to choose the priority senders that are allowed to bypass DND for
* calls or messages. This can be one of four values: starred contacts, all contacts, anyone, or
* no one.
* These controllers handle the settings regarding which priority senders that are allowed to
* bypass DND for calls or messages, which may be one the following values: starred contacts, all
* contacts, priority conversations (for messages only), anyone, or no one.
*
* Most of the functionality is handled by ZenPrioritySendersHelper, so that it can also be shared
* with settings controllers for custom rules. This class handles the parts of the behavior where
* settings must be written to the relevant backends, as that's where this class diverges from
* custom rules.
*/
public class ZenModePrioritySendersPreferenceController
extends AbstractZenModePreferenceController {
@VisibleForTesting static final String KEY_ANY = "senders_anyone";
@VisibleForTesting static final String KEY_CONTACTS = "senders_contacts";
@VisibleForTesting static final String KEY_STARRED = "senders_starred_contacts";
@VisibleForTesting static final String KEY_NONE = "senders_none";
private static final Intent ALL_CONTACTS_INTENT =
new Intent(Contacts.Intents.UI.LIST_DEFAULT)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
private static final Intent STARRED_CONTACTS_INTENT =
new Intent(Contacts.Intents.UI.LIST_STARRED_ACTION)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
private static final Intent FALLBACK_INTENT = new Intent(Intent.ACTION_MAIN)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
private final PackageManager mPackageManager;
private final boolean mIsMessages; // if this is false, then this preference is for calls
private PreferenceCategory mPreferenceCategory;
private List<SelectorWithWidgetPreference> mSelectorWithWidgetPreferences = new ArrayList<>();
private ZenPrioritySendersHelper mHelper;
public ZenModePrioritySendersPreferenceController(Context context, String key,
Lifecycle lifecycle, boolean isMessages) {
Lifecycle lifecycle, boolean isMessages, NotificationBackend notificationBackend) {
super(context, key, lifecycle);
mIsMessages = isMessages;
mPackageManager = mContext.getPackageManager();
if (!FALLBACK_INTENT.hasCategory(Intent.CATEGORY_APP_CONTACTS)) {
FALLBACK_INTENT.addCategory(Intent.CATEGORY_APP_CONTACTS);
}
mHelper = new ZenPrioritySendersHelper(
context, isMessages, mBackend, notificationBackend, mSelectorClickListener);
}
@Override
public void displayPreference(PreferenceScreen screen) {
mPreferenceCategory = screen.findPreference(getPreferenceKey());
if (mPreferenceCategory.findPreference(KEY_ANY) == null) {
makeRadioPreference(KEY_STARRED,
com.android.settings.R.string.zen_mode_from_starred);
makeRadioPreference(KEY_CONTACTS,
com.android.settings.R.string.zen_mode_from_contacts);
makeRadioPreference(KEY_ANY,
com.android.settings.R.string.zen_mode_from_anyone);
makeRadioPreference(KEY_NONE,
mIsMessages
? com.android.settings.R.string.zen_mode_none_messages
: com.android.settings.R.string.zen_mode_none_calls);
updateSummaries();
}
mHelper.displayPreference(mPreferenceCategory);
super.displayPreference(screen);
}
@@ -111,53 +81,37 @@ public class ZenModePrioritySendersPreferenceController
@Override
public void updateState(Preference preference) {
final int currSetting = getPrioritySenders();
for (SelectorWithWidgetPreference pref : mSelectorWithWidgetPreferences) {
pref.setChecked(keyToSetting(pref.getKey()) == currSetting);
}
final int currContactsSetting = getPrioritySenders();
final int currConversationsSetting = getPriorityConversationSenders();
mHelper.updateState(currContactsSetting, currConversationsSetting);
}
@Override
public void onResume() {
super.onResume();
updateSummaries();
}
private void updateSummaries() {
for (SelectorWithWidgetPreference pref : mSelectorWithWidgetPreferences) {
pref.setSummary(getSummary(pref.getKey()));
if (mIsMessages) {
updateChannelCounts();
}
mHelper.updateSummaries();
}
private static int keyToSetting(String key) {
switch (key) {
case KEY_STARRED:
return NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
case KEY_CONTACTS:
return NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS;
case KEY_ANY:
return NotificationManager.Policy.PRIORITY_SENDERS_ANY;
case KEY_NONE:
default:
return ZenModeBackend.SOURCE_NONE;
}
}
private String getSummary(String key) {
switch (key) {
case KEY_STARRED:
return mBackend.getStarredContactsSummary(mContext);
case KEY_CONTACTS:
return mBackend.getContactsNumberSummary(mContext);
case KEY_ANY:
return mContext.getResources().getString(mIsMessages
? R.string.zen_mode_all_messages_summary
: R.string.zen_mode_all_calls_summary);
case KEY_NONE:
default:
private void updateChannelCounts() {
// Load conversations
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... unused) {
mHelper.updateChannelCounts();
return null;
}
}
@Override
protected void onPostExecute(Void unused) {
if (mContext == null) {
return;
}
updateState(mPreferenceCategory);
}
}.execute();
}
private int getPrioritySenders() {
@@ -168,72 +122,34 @@ public class ZenModePrioritySendersPreferenceController
}
}
private SelectorWithWidgetPreference makeRadioPreference(String key, int titleId) {
final SelectorWithWidgetPreference pref =
new SelectorWithWidgetPreference(mPreferenceCategory.getContext());
pref.setKey(key);
pref.setTitle(titleId);
pref.setOnClickListener(mRadioButtonClickListener);
View.OnClickListener widgetClickListener = getWidgetClickListener(key);
if (widgetClickListener != null) {
pref.setExtraWidgetOnClickListener(widgetClickListener);
private int getPriorityConversationSenders() {
if (mIsMessages) {
return mBackend.getPriorityConversationSenders();
}
mPreferenceCategory.addPreference(pref);
mSelectorWithWidgetPreferences.add(pref);
return pref;
return UNKNOWN;
}
private SelectorWithWidgetPreference.OnClickListener mRadioButtonClickListener =
@VisibleForTesting
SelectorWithWidgetPreference.OnClickListener mSelectorClickListener =
new SelectorWithWidgetPreference.OnClickListener() {
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
int selectedSetting = keyToSetting(preference.getKey());
if (selectedSetting != getPrioritySenders()) {
// The settingsToSaveOnClick function takes whether or not the preference is a
// checkbox into account to determine whether this selection is checked or unchecked.
final int[] settingsToSave = mHelper.settingsToSaveOnClick(preference,
getPrioritySenders(), getPriorityConversationSenders());
final int prioritySendersSetting = settingsToSave[0];
final int priorityConvosSetting = settingsToSave[1];
if (prioritySendersSetting != UNKNOWN) {
mBackend.saveSenders(
mIsMessages ? PRIORITY_CATEGORY_MESSAGES : PRIORITY_CATEGORY_CALLS,
selectedSetting);
prioritySendersSetting);
}
if (mIsMessages && priorityConvosSetting != UNKNOWN) {
mBackend.saveConversationSenders(priorityConvosSetting);
}
}
};
private View.OnClickListener getWidgetClickListener(String key) {
if (!KEY_CONTACTS.equals(key) && !KEY_STARRED.equals(key)) {
return null;
}
if (KEY_STARRED.equals(key) && !isStarredIntentValid()) {
return null;
}
if (KEY_CONTACTS.equals(key) && !isContactsIntentValid()) {
return null;
}
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (KEY_STARRED.equals(key)
&& STARRED_CONTACTS_INTENT.resolveActivity(mPackageManager) != null) {
mContext.startActivity(STARRED_CONTACTS_INTENT);
} else if (KEY_CONTACTS.equals(key)
&& ALL_CONTACTS_INTENT.resolveActivity(mPackageManager) != null) {
mContext.startActivity(ALL_CONTACTS_INTENT);
} else {
mContext.startActivity(FALLBACK_INTENT);
}
}
};
}
private boolean isStarredIntentValid() {
return STARRED_CONTACTS_INTENT.resolveActivity(mPackageManager) != null
|| FALLBACK_INTENT.resolveActivity(mPackageManager) != null;
}
private boolean isContactsIntentValid() {
return ALL_CONTACTS_INTENT.resolveActivity(mPackageManager) != null
|| FALLBACK_INTENT.resolveActivity(mPackageManager) != null;
}
}

View File

@@ -18,6 +18,7 @@ package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CALLS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CONVERSATIONS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_EVENTS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
@@ -116,6 +117,7 @@ public class ZenModeSettings extends ZenModeSettingsBase {
PRIORITY_CATEGORY_MEDIA,
PRIORITY_CATEGORY_SYSTEM,
PRIORITY_CATEGORY_MESSAGES,
PRIORITY_CATEGORY_CONVERSATIONS,
PRIORITY_CATEGORY_EVENTS,
PRIORITY_CATEGORY_REMINDERS,
PRIORITY_CATEGORY_CALLS,
@@ -168,12 +170,19 @@ public class ZenModeSettings extends ZenModeSettingsBase {
String getMessagesSettingSummary(Policy policy) {
List<String> enabledCategories = getEnabledCategories(policy,
category -> PRIORITY_CATEGORY_MESSAGES == category, false);
category -> PRIORITY_CATEGORY_MESSAGES == category
|| PRIORITY_CATEGORY_CONVERSATIONS == category, true);
int numCategories = enabledCategories.size();
if (numCategories == 0) {
return mContext.getString(R.string.zen_mode_none_messages);
} else {
} else if (numCategories == 1) {
return enabledCategories.get(0);
} else {
// While this string name seems like a slight misnomer: it's borrowing the analogous
// calls-summary functionality to combine two permissions.
return mContext.getString(R.string.zen_mode_calls_summary_two,
enabledCategories.get(0),
enabledCategories.get(1));
}
}
@@ -250,6 +259,15 @@ public class ZenModeSettings extends ZenModeSettingsBase {
continue;
}
// For conversations, only the "priority conversations" setting is relevant; any
// other setting is subsumed by the messages-specific messaging.
if (category == Policy.PRIORITY_CATEGORY_CONVERSATIONS
&& isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_CONVERSATIONS)
&& policy.priorityConversationSenders
!= Policy.CONVERSATION_SENDERS_IMPORTANT) {
continue;
}
enabledCategories.add(getCategory(category, policy, isFirst));
}
}
@@ -282,11 +300,20 @@ public class ZenModeSettings extends ZenModeSettingsBase {
} else if (category == Policy.PRIORITY_CATEGORY_MESSAGES) {
if (policy.priorityMessageSenders == Policy.PRIORITY_SENDERS_ANY) {
return mContext.getString(R.string.zen_mode_from_anyone);
} else if (policy.priorityMessageSenders == Policy.PRIORITY_SENDERS_CONTACTS){
} else if (policy.priorityMessageSenders == Policy.PRIORITY_SENDERS_CONTACTS) {
return mContext.getString(R.string.zen_mode_from_contacts);
} else {
return mContext.getString(R.string.zen_mode_from_starred);
}
} else if (category == Policy.PRIORITY_CATEGORY_CONVERSATIONS
&& policy.priorityConversationSenders
== Policy.CONVERSATION_SENDERS_IMPORTANT) {
if (isFirst) {
return mContext.getString(R.string.zen_mode_from_important_conversations);
} else {
return mContext.getString(
R.string.zen_mode_from_important_conversations_second);
}
} else if (category == Policy.PRIORITY_CATEGORY_EVENTS) {
if (isFirst) {
return mContext.getString(R.string.zen_mode_events_list_first);

View File

@@ -0,0 +1,388 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_ANYONE;
import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_IMPORTANT;
import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_NONE;
import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_ANY;
import android.app.NotificationManager;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ParceledListSlice;
import android.icu.text.MessageFormat;
import android.provider.Contacts;
import android.service.notification.ConversationChannelWrapper;
import android.view.View;
import androidx.preference.PreferenceCategory;
import com.android.settings.R;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.notification.NotificationBackend;
import com.android.settings.notification.app.ConversationListSettings;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* Shared class implementing priority senders logic to be used both for zen mode and zen custom
* rules, governing which senders can break through DND. This helper class controls creating
* and displaying the relevant preferences for either messages or calls mode, and determining
* what the priority and conversation senders settings should be given a click.
*
* The outer classes govern how those settings are stored -- for instance, where and how they
* are saved, and where they're read from to get current status.
*/
public class ZenPrioritySendersHelper {
public static final String TAG = "ZenPrioritySendersHelper";
static final int UNKNOWN = -10;
static final String KEY_ANY = "senders_anyone";
static final String KEY_CONTACTS = "senders_contacts";
static final String KEY_STARRED = "senders_starred_contacts";
static final String KEY_IMPORTANT = "conversations_important";
static final String KEY_NONE = "senders_none";
private int mNumImportantConversations = UNKNOWN;
private static final Intent ALL_CONTACTS_INTENT =
new Intent(Contacts.Intents.UI.LIST_DEFAULT)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
private static final Intent STARRED_CONTACTS_INTENT =
new Intent(Contacts.Intents.UI.LIST_STARRED_ACTION)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
private static final Intent FALLBACK_INTENT = new Intent(Intent.ACTION_MAIN)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
private final Context mContext;
private final ZenModeBackend mZenModeBackend;
private final NotificationBackend mNotificationBackend;
private final PackageManager mPackageManager;
private final boolean mIsMessages; // if this is false, then this preference is for calls
private final SelectorWithWidgetPreference.OnClickListener mSelectorClickListener;
private PreferenceCategory mPreferenceCategory;
private List<SelectorWithWidgetPreference> mSelectorPreferences = new ArrayList<>();
public ZenPrioritySendersHelper(Context context, boolean isMessages,
ZenModeBackend zenModeBackend, NotificationBackend notificationBackend,
SelectorWithWidgetPreference.OnClickListener clickListener) {
mContext = context;
mIsMessages = isMessages;
mZenModeBackend = zenModeBackend;
mNotificationBackend = notificationBackend;
mSelectorClickListener = clickListener;
mPackageManager = mContext.getPackageManager();
if (!FALLBACK_INTENT.hasCategory(Intent.CATEGORY_APP_CONTACTS)) {
FALLBACK_INTENT.addCategory(Intent.CATEGORY_APP_CONTACTS);
}
}
void displayPreference(PreferenceCategory preferenceCategory) {
mPreferenceCategory = preferenceCategory;
if (mPreferenceCategory.getPreferenceCount() == 0) {
makeSelectorPreference(KEY_STARRED,
com.android.settings.R.string.zen_mode_from_starred, mIsMessages);
makeSelectorPreference(KEY_CONTACTS,
com.android.settings.R.string.zen_mode_from_contacts, mIsMessages);
if (mIsMessages) {
makeSelectorPreference(KEY_IMPORTANT,
com.android.settings.R.string.zen_mode_from_important_conversations, true);
updateChannelCounts();
}
makeSelectorPreference(KEY_ANY,
com.android.settings.R.string.zen_mode_from_anyone, mIsMessages);
makeSelectorPreference(KEY_NONE,
com.android.settings.R.string.zen_mode_none_messages, mIsMessages);
updateSummaries();
}
}
void updateState(int currContactsSetting, int currConversationsSetting) {
for (SelectorWithWidgetPreference pref : mSelectorPreferences) {
// for each preference, check whether the current state matches what this state
// would look like if the button were checked.
final int[] checkedState = keyToSettingEndState(pref.getKey(), true);
final int checkedContactsSetting = checkedState[0];
final int checkedConversationsSetting = checkedState[1];
boolean match = checkedContactsSetting == currContactsSetting;
if (mIsMessages && checkedConversationsSetting != UNKNOWN) {
// "UNKNOWN" in checkedContactsSetting means this preference doesn't govern
// the priority senders setting, so the full match happens when either
// the priority senders setting matches or if it's UNKNOWN so only the conversation
// setting needs to match.
match = (match || checkedContactsSetting == UNKNOWN)
&& (checkedConversationsSetting == currConversationsSetting);
}
pref.setChecked(match);
}
}
void updateSummaries() {
for (SelectorWithWidgetPreference pref : mSelectorPreferences) {
pref.setSummary(getSummary(pref.getKey()));
}
}
// Gets the desired end state of the priority senders and conversations for the given key
// and whether it is being checked or unchecked. UNKNOWN indicates no change in state.
//
// Returns an integer array with 2 entries. The first entry is the setting for priority senders
// and the second entry is for priority conversation senders; if isMessages is false, then
// no changes will ever be prescribed for conversation senders.
int[] keyToSettingEndState(String key, boolean checked) {
int[] endState = new int[]{ UNKNOWN, UNKNOWN };
if (!checked) {
// Unchecking any priority-senders-based state should reset the state to NONE.
// "Unchecking" the NONE state doesn't do anything, in practice.
switch (key) {
case KEY_STARRED:
case KEY_CONTACTS:
case KEY_ANY:
case KEY_NONE:
endState[0] = ZenModeBackend.SOURCE_NONE;
}
// For messages, unchecking "priority conversations" and "any" should reset conversation
// state to "NONE" as well.
if (mIsMessages) {
switch (key) {
case KEY_IMPORTANT:
case KEY_ANY:
case KEY_NONE:
endState[1] = CONVERSATION_SENDERS_NONE;
}
}
} else {
// All below is for the enabling (checked) state.
switch (key) {
case KEY_STARRED:
endState[0] = NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
break;
case KEY_CONTACTS:
endState[0] = NotificationManager.Policy.PRIORITY_SENDERS_CONTACTS;
break;
case KEY_ANY:
endState[0] = NotificationManager.Policy.PRIORITY_SENDERS_ANY;
break;
case KEY_NONE:
endState[0] = ZenModeBackend.SOURCE_NONE;
}
// In the messages case *only*, also handle changing of conversation settings.
if (mIsMessages) {
switch (key) {
case KEY_IMPORTANT:
endState[1] = CONVERSATION_SENDERS_IMPORTANT;
break;
case KEY_ANY:
endState[1] = CONVERSATION_SENDERS_ANYONE;
break;
case KEY_NONE:
endState[1] = CONVERSATION_SENDERS_NONE;
}
}
}
// Error case check: if somehow, after all of that, endState is still {UNKNOWN, UNKNOWN},
// something has gone wrong.
if (endState[0] == UNKNOWN && endState[1] == UNKNOWN) {
throw new IllegalArgumentException("invalid key " + key);
}
return endState;
}
// Returns the preferences, if any, that should be newly saved for the specified setting and
// checked state in an array where index 0 is the new senders setting and 1 the new
// conversations setting. A return value of UNKNOWN indicates that nothing should change.
//
// The returned conversations setting will always be UNKNOWN (not to change) in the calls case.
//
// Checking and unchecking is mostly an operation of setting or unsetting the relevant
// preference, except for some special handling where the conversation setting overlaps:
// - setting or unsetting "priority contacts" or "contacts" has no effect on the
// priority conversation setting, and vice versa
// - if "priority conversations" is selected, and the user checks "anyone", the conversation
// setting is also set to any conversations
// - if "anyone" is previously selected, and the user clicks "priority conversations", then
// the contacts setting is additionally reset to "none".
// - if "anyone" is previously selected, and the user clicks one of the contacts values,
// then the conversations setting is additionally reset to "none".
int[] settingsToSaveOnClick(SelectorWithWidgetPreference preference,
int currSendersSetting, int currConvosSetting) {
int[] savedSettings = new int[]{ UNKNOWN, UNKNOWN };
// If the preference isn't a checkbox, always consider this to be "checking" the setting.
// Otherwise, toggle.
final int[] endState = keyToSettingEndState(preference.getKey(),
preference.isCheckBox() ? !preference.isChecked() : true);
final int prioritySendersSetting = endState[0];
final int priorityConvosSetting = endState[1];
if (prioritySendersSetting != UNKNOWN && prioritySendersSetting != currSendersSetting) {
savedSettings[0] = prioritySendersSetting;
}
// Only handle conversation settings for the messages case. If not messages, there should
// never be any change to the conversation senders setting.
if (mIsMessages) {
if (priorityConvosSetting != UNKNOWN
&& priorityConvosSetting != currConvosSetting) {
savedSettings[1] = priorityConvosSetting;
}
// Special-case handling for the "priority conversations" checkbox:
// If a specific selection exists for priority senders (starred, contacts), we leave
// it untouched. Otherwise (when the senders is set to "any"), set it to NONE.
if (preference.getKey() == KEY_IMPORTANT
&& currSendersSetting == PRIORITY_SENDERS_ANY) {
savedSettings[0] = ZenModeBackend.SOURCE_NONE;
}
// Flip-side special case for clicking either "contacts" option: if a specific selection
// exists for priority conversations, leave it untouched; otherwise, set to none.
if ((preference.getKey() == KEY_STARRED || preference.getKey() == KEY_CONTACTS)
&& currConvosSetting == CONVERSATION_SENDERS_ANYONE) {
savedSettings[1] = CONVERSATION_SENDERS_NONE;
}
}
return savedSettings;
}
private String getSummary(String key) {
switch (key) {
case KEY_STARRED:
return mZenModeBackend.getStarredContactsSummary(mContext);
case KEY_CONTACTS:
return mZenModeBackend.getContactsNumberSummary(mContext);
case KEY_IMPORTANT:
return getConversationSummary();
case KEY_ANY:
return mContext.getResources().getString(mIsMessages
? R.string.zen_mode_all_messages_summary
: R.string.zen_mode_all_calls_summary);
case KEY_NONE:
default:
return null;
}
}
private String getConversationSummary() {
final int numConversations = mNumImportantConversations;
if (numConversations == UNKNOWN) {
return null;
} else {
MessageFormat msgFormat = new MessageFormat(
mContext.getString(R.string.zen_mode_conversations_count),
Locale.getDefault());
Map<String, Object> args = new HashMap<>();
args.put("count", numConversations);
return msgFormat.format(args);
}
}
void updateChannelCounts() {
// Load conversations
ParceledListSlice<ConversationChannelWrapper> impConversations =
mNotificationBackend.getConversations(true);
int numImportantConversations = 0;
if (impConversations != null) {
for (ConversationChannelWrapper conversation : impConversations.getList()) {
if (!conversation.getNotificationChannel().isDemoted()) {
numImportantConversations++;
}
}
}
mNumImportantConversations = numImportantConversations;
}
private SelectorWithWidgetPreference makeSelectorPreference(String key, int titleId,
boolean isCheckbox) {
final SelectorWithWidgetPreference pref =
new SelectorWithWidgetPreference(mPreferenceCategory.getContext(), isCheckbox);
pref.setKey(key);
pref.setTitle(titleId);
pref.setOnClickListener(mSelectorClickListener);
View.OnClickListener widgetClickListener = getWidgetClickListener(key);
if (widgetClickListener != null) {
pref.setExtraWidgetOnClickListener(widgetClickListener);
}
mPreferenceCategory.addPreference(pref);
mSelectorPreferences.add(pref);
return pref;
}
private View.OnClickListener getWidgetClickListener(String key) {
if (!KEY_CONTACTS.equals(key) && !KEY_STARRED.equals(key) && !KEY_IMPORTANT.equals(key)) {
return null;
}
if (KEY_STARRED.equals(key) && !isStarredIntentValid()) {
return null;
}
if (KEY_CONTACTS.equals(key) && !isContactsIntentValid()) {
return null;
}
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (KEY_STARRED.equals(key)
&& STARRED_CONTACTS_INTENT.resolveActivity(mPackageManager) != null) {
mContext.startActivity(STARRED_CONTACTS_INTENT);
} else if (KEY_CONTACTS.equals(key)
&& ALL_CONTACTS_INTENT.resolveActivity(mPackageManager) != null) {
mContext.startActivity(ALL_CONTACTS_INTENT);
} else if (KEY_IMPORTANT.equals(key)) {
new SubSettingLauncher(mContext)
.setDestination(ConversationListSettings.class.getName())
.setSourceMetricsCategory(SettingsEnums.DND_CONVERSATIONS)
.launch();
} else {
mContext.startActivity(FALLBACK_INTENT);
}
}
};
}
private boolean isStarredIntentValid() {
return STARRED_CONTACTS_INTENT.resolveActivity(mPackageManager) != null
|| FALLBACK_INTENT.resolveActivity(mPackageManager) != null;
}
private boolean isContactsIntentValid() {
return ALL_CONTACTS_INTENT.resolveActivity(mPackageManager) != null
|| FALLBACK_INTENT.resolveActivity(mPackageManager) != null;
}
}

View File

@@ -0,0 +1,178 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.zen;
import static com.android.settings.notification.zen.ZenPrioritySendersHelper.UNKNOWN;
import android.app.AutomaticZenRule;
import android.content.Context;
import android.os.AsyncTask;
import android.service.notification.ZenPolicy;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.notification.NotificationBackend;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
/**
* Shared controller for custom rule priority senders settings for both messages and calls.
*
* Most functionality is the same as that of the main zen mode messages and calls settings;
* these controllers handle which senders are allowed to break through DND for messages or calls,
* with possible settings options being: starred contacts, all contacts, priority conversations
* (for messages only), anyone, or no one.
*/
public class ZenRulePrioritySendersPreferenceController
extends AbstractZenCustomRulePreferenceController {
private final boolean mIsMessages; // if this is false, then this preference is for calls
private PreferenceCategory mPreferenceCategory;
private ZenPrioritySendersHelper mHelper;
public ZenRulePrioritySendersPreferenceController(Context context, String key,
Lifecycle lifecycle, boolean isMessages, NotificationBackend notificationBackend) {
super(context, key, lifecycle);
mIsMessages = isMessages;
mHelper = new ZenPrioritySendersHelper(
context, isMessages, mBackend, notificationBackend, mSelectorClickListener);
}
@Override
public void displayPreference(PreferenceScreen screen) {
mPreferenceCategory = screen.findPreference(getPreferenceKey());
mHelper.displayPreference(mPreferenceCategory);
super.displayPreference(screen);
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (mRule != null && mRule.getZenPolicy() != null) {
final int currContactsSetting = getPrioritySenders();
final int currConversationsSetting = getPriorityConversationSenders();
mHelper.updateState(currContactsSetting, currConversationsSetting);
}
}
@Override
public void onResume(AutomaticZenRule rule, String id) {
super.onResume(rule, id);
if (mIsMessages) {
updateChannelCounts();
}
mHelper.updateSummaries();
}
private void updateChannelCounts() {
// Load conversations
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... unused) {
mHelper.updateChannelCounts();
return null;
}
@Override
protected void onPostExecute(Void unused) {
if (mContext == null) {
return;
}
updateState(mPreferenceCategory);
}
}.execute();
}
private int getPrioritySenders() {
if (mRule == null || mRule.getZenPolicy() == null) {
return UNKNOWN;
}
if (mIsMessages) {
return ZenModeBackend.getContactSettingFromZenPolicySetting(
mRule.getZenPolicy().getPriorityMessageSenders());
} else {
return ZenModeBackend.getContactSettingFromZenPolicySetting(
mRule.getZenPolicy().getPriorityCallSenders());
}
}
private int getPriorityConversationSenders() {
if (mRule == null || mRule.getZenPolicy() == null) {
return UNKNOWN;
}
return mRule.getZenPolicy().getPriorityConversationSenders();
}
// Returns the ZenPolicySetting enum associated with the provided NotificationManager.Policy.
static @ZenPolicy.PeopleType int zenPolicySettingFromSender(int senderSetting) {
return ZenModeBackend.getZenPolicySettingFromPrefKey(
ZenModeBackend.getKeyFromSetting(senderSetting));
}
@VisibleForTesting
SelectorWithWidgetPreference.OnClickListener mSelectorClickListener =
new SelectorWithWidgetPreference.OnClickListener() {
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
if (mRule == null || mRule.getZenPolicy() == null) {
return;
}
final int[] settingsToSave = mHelper.settingsToSaveOnClick(preference,
getPrioritySenders(), getPriorityConversationSenders());
final int prioritySendersSetting = settingsToSave[0];
final int priorityConvosSetting = settingsToSave[1];
// if both are UNKNOWN then just return
if (prioritySendersSetting == UNKNOWN && priorityConvosSetting == UNKNOWN) {
return;
}
if (prioritySendersSetting != UNKNOWN) {
if (mIsMessages) {
mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy())
.allowMessages(
zenPolicySettingFromSender(prioritySendersSetting))
.build());
} else {
mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy())
.allowCalls(
zenPolicySettingFromSender(prioritySendersSetting))
.build());
}
}
if (mIsMessages && priorityConvosSetting != UNKNOWN) {
mRule.setZenPolicy(new ZenPolicy.Builder(mRule.getZenPolicy())
.allowConversations(priorityConvosSetting)
.build());
}
// Save any changes
mBackend.updateZenRule(mId, mRule);
}
};
}