Update DND people Settings pages

Settings > Sound > Do Not Disturb > People
- Redesign of messages + calls settings pages
- Redesign of conversations page
- Added strings for Apps page

Test: make RunSettingsRoboTests7
Bug: 151845457
Change-Id: I58d22a31c398418c053e06a4c6bf5ed8c482ee35
This commit is contained in:
Beverly
2020-03-19 16:58:20 -04:00
committed by Beverly Tai
parent 509273cc03
commit e65db314f1
20 changed files with 1081 additions and 827 deletions

View File

@@ -16,8 +16,6 @@
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.PRIORITY_CATEGORY_CONVERSATIONS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON;
@@ -501,7 +499,7 @@ public class ZenModeBackend {
private List<String> getStarredContacts() {
Cursor cursor = null;
try {
cursor = queryData();
cursor = queryStarredContactsData();
return getStarredContacts(cursor);
} finally {
if (cursor != null) {
@@ -510,7 +508,7 @@ public class ZenModeBackend {
}
}
public String getStarredContactsSummary(Context context) {
String getStarredContactsSummary(Context context) {
List<String> starredContacts = getStarredContacts();
int numStarredContacts = starredContacts.size();
@@ -536,13 +534,24 @@ public class ZenModeBackend {
return ListFormatter.getInstance().format(displayContacts);
}
private Cursor queryData() {
String getContactsNumberSummary(Context context) {
return context.getResources().getString(R.string.zen_mode_contacts_senders_summary,
queryAllContactsData().getCount());
}
private Cursor queryStarredContactsData() {
return mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
new String[]{ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
ContactsContract.Data.STARRED + "=1", null,
ContactsContract.Data.TIMES_CONTACTED);
}
private Cursor queryAllContactsData() {
return mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
new String[]{ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
null, null, null);
}
@VisibleForTesting
public static final Comparator<Map.Entry<String, AutomaticZenRule>> RULE_COMPARATOR =
new Comparator<Map.Entry<String, AutomaticZenRule>>() {

View File

@@ -0,0 +1,88 @@
/*
* Copyright (C) 2020 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 android.app.settings.SettingsEnums;
import android.content.Context;
import android.provider.SearchIndexableResource;
import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
/**
* DND Calls Settings page to determine which priority senders can bypass DND.
*/
@SearchIndexable
public class ZenModeCallsSettings extends ZenModeSettingsBase {
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context, getSettingsLifecycle());
}
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
Lifecycle lifecycle) {
List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new ZenModePrioritySendersPreferenceController(context,
"zen_mode_settings_category_calls", lifecycle, false));
controllers.add(new ZenModeRepeatCallersPreferenceController(context, lifecycle,
context.getResources().getInteger(com.android.internal.R.integer
.config_zen_repeat_callers_threshold)));
controllers.add(new ZenModeBehaviorFooterPreferenceController(
context, lifecycle, R.string.zen_mode_calls_footer));
return controllers;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.zen_mode_calls_settings;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.DND_CALLS;
}
/**
* For Search.
*/
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
boolean enabled) {
final ArrayList<SearchIndexableResource> result = new ArrayList<>();
final SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.zen_mode_calls_settings;
result.add(sir);
return result;
}
@Override
public List<AbstractPreferenceController> createPreferenceControllers(
Context context) {
return buildPreferenceControllers(context, null);
}
};
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2020 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 android.app.NotificationManager;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settingslib.core.lifecycle.Lifecycle;
/**
* Controls the summary for preference found at:
* Settings > Sound > Do Not Disturb > People > Conversations
*/
public class ZenModeConversationsPreferenceController extends AbstractZenModePreferenceController {
private final ZenModeBackend mBackend;
private Preference mPreference;
public ZenModeConversationsPreferenceController(Context context,
String key, Lifecycle lifecycle) {
super(context, key, lifecycle);
mBackend = ZenModeBackend.getInstance(context);
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(KEY);
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
switch (getZenMode()) {
case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
case Settings.Global.ZEN_MODE_ALARMS:
mPreference.setEnabled(false);
mPreference.setSummary(mBackend.getAlarmsTotalSilencePeopleSummary(
NotificationManager.Policy.PRIORITY_CATEGORY_CONVERSATIONS));
break;
default:
preference.setEnabled(true);
preference.setSummary(mBackend.getConversationSummary());
}
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2020 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 android.app.settings.SettingsEnums;
import android.content.Context;
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;
import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
/**
* Settings > Sound > Do Not Disturb > Conversationss
*/
@SearchIndexable
public class ZenModeConversationsSettings extends ZenModeSettingsBase {
private final NotificationBackend mNotificationBackend = new NotificationBackend();
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context, getSettingsLifecycle(), mNotificationBackend);
}
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
Lifecycle lifecycle, NotificationBackend notificationBackend) {
List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new ZenModePriorityConversationsPreferenceController(context,
"zen_mode_conversations_radio_buttons", lifecycle, notificationBackend));
return controllers;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.zen_mode_conversations_settings;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.DND_CONVERSATIONS;
}
/**
* For Search.
*/
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.zen_mode_conversations_settings) {
@Override
public List<AbstractPreferenceController> createPreferenceControllers(
Context context) {
return buildPreferenceControllers(context, null, null);
}
};
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright (C) 2020 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 android.app.settings.SettingsEnums;
import android.content.Context;
import android.provider.SearchIndexableResource;
import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
/**
* DND Messages Settings page to determine which priority senders can bypass DND.
* "Messages" include SMS, MMS, and messaging apps.
*/
@SearchIndexable
public class ZenModeMessagesSettings extends ZenModeSettingsBase {
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context, getSettingsLifecycle());
}
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
Lifecycle lifecycle) {
List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new ZenModePrioritySendersPreferenceController(context,
"zen_mode_settings_category_messages", lifecycle, true));
controllers.add(new ZenModeBehaviorFooterPreferenceController(
context, lifecycle, R.string.zen_mode_messages_footer));
return controllers;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.zen_mode_messages_settings;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.DND_MESSAGES;
}
/**
* For Search.
*/
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
boolean enabled) {
final ArrayList<SearchIndexableResource> result = new ArrayList<>();
final SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.zen_mode_messages_settings;
result.add(sir);
return result;
}
@Override
public List<AbstractPreferenceController> createPreferenceControllers(
Context context) {
return buildPreferenceControllers(context, null);
}
};
}

View File

@@ -16,9 +16,6 @@
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.Activity;
import android.app.Application;
import android.app.settings.SettingsEnums;
@@ -56,16 +53,12 @@ public class ZenModePeopleSettings extends ZenModeSettingsBase implements Indexa
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
Lifecycle lifecycle, Application app, Fragment host, FragmentManager fragmentManager) {
List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new ZenModePriorityMessagesPreferenceController(context, lifecycle));
controllers.add(new ZenModeStarredContactsPreferenceController(context, lifecycle,
PRIORITY_CATEGORY_MESSAGES, "zen_mode_starred_contacts_messages"));
controllers.add(new ZenModePriorityCallsPreferenceController(context, lifecycle));
controllers.add(new ZenModeStarredContactsPreferenceController(context, lifecycle,
PRIORITY_CATEGORY_CALLS, "zen_mode_starred_contacts_callers"));
controllers.add(new ZenModeRepeatCallersPreferenceController(context, lifecycle,
context.getResources().getInteger(com.android.internal.R.integer
.config_zen_repeat_callers_threshold)));
controllers.add(new ZenModePriorityConversationsPreferenceController(context, lifecycle));
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,
"zen_mode_people_messages"));
controllers.add(new ZenModeSettingsFooterPreferenceController(context, lifecycle,
fragmentManager));
return controllers;

View File

@@ -16,37 +16,65 @@
package com.android.settings.notification.zen;
import static android.service.notification.ZenPolicy.CONVERSATION_SENDERS_ANYONE;
import static android.service.notification.ZenPolicy.CONVERSATION_SENDERS_IMPORTANT;
import static android.service.notification.ZenPolicy.CONVERSATION_SENDERS_NONE;
import android.app.NotificationManager;
import android.content.Context;
import android.provider.Settings;
import android.content.pm.ParceledListSlice;
import android.os.AsyncTask;
import android.service.notification.ConversationChannelWrapper;
import androidx.preference.ListPreference;
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.RadioButtonPreference;
import java.util.ArrayList;
import java.util.List;
/**
* Options to choose the priority conversations that are allowed to bypass DND.
*/
public class ZenModePriorityConversationsPreferenceController
extends AbstractZenModePreferenceController
implements Preference.OnPreferenceChangeListener {
extends AbstractZenModePreferenceController {
private static final int UNSET = -1;
@VisibleForTesting static final String KEY_ALL = "conversations_all";
@VisibleForTesting static final String KEY_IMPORTANT = "conversations_important";
@VisibleForTesting static final String KEY_NONE = "conversations_none";
protected static final String KEY = "zen_mode_conversations";
private final ZenModeBackend mBackend;
private ListPreference mPreference;
private final NotificationBackend mNotificationBackend;
public ZenModePriorityConversationsPreferenceController(Context context, Lifecycle lifecycle) {
super(context, KEY, lifecycle);
mBackend = ZenModeBackend.getInstance(context);
private int mNumImportantConversations = UNSET;
private int mNumConversations = UNSET;
private PreferenceCategory mPreferenceCategory;
private List<RadioButtonPreference> mRadioButtonPreferences = new ArrayList<>();
public ZenModePriorityConversationsPreferenceController(Context context, String key,
Lifecycle lifecycle, NotificationBackend notificationBackend) {
super(context, key, lifecycle);
mNotificationBackend = notificationBackend;
}
@Override
public String getPreferenceKey() {
return KEY;
public void displayPreference(PreferenceScreen screen) {
mPreferenceCategory = screen.findPreference(getPreferenceKey());
if (mPreferenceCategory.findPreference(KEY_ALL) == null) {
makeRadioPreference(KEY_ALL, R.string.zen_mode_from_all_conversations);
makeRadioPreference(KEY_IMPORTANT, R.string.zen_mode_from_important_conversations);
makeRadioPreference(KEY_NONE, R.string.zen_mode_from_no_conversations);
updateChannelCounts();
}
super.displayPreference(screen);
}
@Override
public void onResume() {
super.onResume();
updateChannelCounts();
}
@Override
@@ -55,50 +83,98 @@ public class ZenModePriorityConversationsPreferenceController
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(KEY);
public String getPreferenceKey() {
return KEY;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
updateValue(preference);
}
final int currSetting = mBackend.getPriorityConversationSenders();
@Override
public boolean onPreferenceChange(Preference preference, Object selectedContactsFrom) {
mBackend.saveConversationSenders(Integer.parseInt(selectedContactsFrom.toString()));
updateValue(preference);
return true;
}
private void updateValue(Preference preference) {
mPreference = (ListPreference) preference;
switch (getZenMode()) {
case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
case Settings.Global.ZEN_MODE_ALARMS:
mPreference.setEnabled(false);
mPreference.setValue(String.valueOf(CONVERSATION_SENDERS_NONE));
mPreference.setSummary(mBackend.getAlarmsTotalSilencePeopleSummary(
NotificationManager.Policy.PRIORITY_CATEGORY_CONVERSATIONS));
break;
default:
preference.setEnabled(true);
preference.setSummary(mBackend.getConversationSummary());
int senders = mBackend.getPriorityConversationSenders();
switch (senders) {
case CONVERSATION_SENDERS_NONE:
mPreference.setValue(String.valueOf(CONVERSATION_SENDERS_NONE));
break;
case CONVERSATION_SENDERS_IMPORTANT:
mPreference.setValue(String.valueOf(CONVERSATION_SENDERS_IMPORTANT));
break;
default:
mPreference.setValue(String.valueOf(CONVERSATION_SENDERS_ANYONE));
break;
}
for (RadioButtonPreference pref : mRadioButtonPreferences) {
pref.setChecked(keyToSetting(pref.getKey()) == currSetting);
pref.setSummary(getSummary(pref.getKey()));
}
}
private static int keyToSetting(String key) {
switch (key) {
case KEY_ALL:
return NotificationManager.Policy.CONVERSATION_SENDERS_ANYONE;
case KEY_IMPORTANT:
return NotificationManager.Policy.CONVERSATION_SENDERS_IMPORTANT;
default:
return NotificationManager.Policy.CONVERSATION_SENDERS_NONE;
}
}
private String getSummary(String key) {
int numConversations;
if (KEY_ALL.equals(key)) {
numConversations = mNumConversations;
} else if (KEY_IMPORTANT.equals(key)) {
numConversations = mNumImportantConversations;
} else {
return null;
}
if (numConversations == UNSET) {
return null;
} else if (numConversations == 0) {
return mContext.getResources().getString(
R.string.zen_mode_conversations_count_none);
} else {
return mContext.getResources().getQuantityString(
R.plurals.zen_mode_conversations_count, numConversations);
}
}
private void updateChannelCounts() {
// Load conversations
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... unused) {
ParceledListSlice<ConversationChannelWrapper> allConversations =
mNotificationBackend.getConversations(false);
if (allConversations != null) {
mNumConversations = allConversations.getList().size();
}
ParceledListSlice<ConversationChannelWrapper> importantConversations =
mNotificationBackend.getConversations(true);
if (importantConversations != null) {
mNumImportantConversations = importantConversations.getList().size();
}
return null;
}
@Override
protected void onPostExecute(Void unused) {
if (mContext == null) {
return;
}
updateState(mPreferenceCategory);
}
}.execute();
}
private RadioButtonPreference makeRadioPreference(String key, int titleId) {
RadioButtonPreference pref = new RadioButtonPreference(mPreferenceCategory.getContext());
pref.setKey(key);
pref.setTitle(titleId);
pref.setOnClickListener(mRadioButtonClickListener);
mPreferenceCategory.addPreference(pref);
mRadioButtonPreferences.add(pref);
return pref;
}
private RadioButtonPreference.OnClickListener mRadioButtonClickListener =
new RadioButtonPreference.OnClickListener() {
@Override
public void onRadioButtonClicked(RadioButtonPreference preference) {
int selectedConversationSetting = keyToSetting(preference.getKey());
if (selectedConversationSetting != mBackend.getPriorityConversationSenders()) {
mBackend.saveConversationSenders(selectedConversationSetting);
}
}
};
}

View File

@@ -1,108 +0,0 @@
/*
* Copyright (C) 2018 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 android.app.NotificationManager;
import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import androidx.annotation.VisibleForTesting;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
public class ZenModePriorityMessagesPreferenceController extends AbstractZenModePreferenceController
implements Preference.OnPreferenceChangeListener {
protected static final String KEY = "zen_mode_messages";
private final ZenModeBackend mBackend;
private ListPreference mPreference;
private final String[] mListValues;
public ZenModePriorityMessagesPreferenceController(Context context, Lifecycle lifecycle) {
super(context, KEY, lifecycle);
mBackend = ZenModeBackend.getInstance(context);
mListValues = context.getResources().getStringArray(R.array.zen_mode_contacts_values);
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(KEY);
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
updateFromContactsValue(preference);
}
@Override
public boolean onPreferenceChange(Preference preference, Object selectedContactsFrom) {
mBackend.saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES,
ZenModeBackend.getSettingFromPrefKey(selectedContactsFrom.toString()));
updateFromContactsValue(preference);
return true;
}
private void updateFromContactsValue(Preference preference) {
mPreference = (ListPreference) preference;
switch (getZenMode()) {
case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
case Settings.Global.ZEN_MODE_ALARMS:
mPreference.setEnabled(false);
mPreference.setValue(ZenModeBackend.ZEN_MODE_FROM_NONE);
mPreference.setSummary(mBackend.getAlarmsTotalSilencePeopleSummary(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES));
break;
default:
preference.setEnabled(true);
preference.setSummary(mBackend.getContactsSummary(
NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES));
final String currentVal = ZenModeBackend.getKeyFromSetting(
mBackend.getPriorityMessageSenders());
mPreference.setValue(mListValues[getIndexOfSendersValue(currentVal)]);
}
}
@VisibleForTesting
protected int getIndexOfSendersValue(String currentVal) {
int index = 3; // defaults to "none" based on R.array.zen_mode_contacts_values
for (int i = 0; i < mListValues.length; i++) {
if (TextUtils.equals(currentVal, mListValues[i])) {
return i;
}
}
return index;
}
}

View File

@@ -0,0 +1,241 @@
/*
* Copyright (C) 2020 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.PRIORITY_CATEGORY_CALLS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
import static com.android.settings.widget.RadioButtonPreferenceWithExtraWidget.EXTRA_WIDGET_VISIBILITY_GONE;
import static com.android.settings.widget.RadioButtonPreferenceWithExtraWidget.EXTRA_WIDGET_VISIBILITY_SETTING;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.Contacts;
import android.view.View;
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.widget.RadioButtonPreferenceWithExtraWidget;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.RadioButtonPreference;
import java.util.ArrayList;
import java.util.List;
/**
* Common preference controller functionality shared by
* ZenModePriorityMessagesPreferenceController and ZenModePriorityCallsPreferenceController.
*
* 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.
*/
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_ALL_CONTACTS_ACTION);
private static final Intent STARRED_CONTACTS_INTENT =
new Intent(Contacts.Intents.UI.LIST_STARRED_ACTION);
private static final Intent FALLBACK_INTENT = new Intent(Intent.ACTION_MAIN);
private final PackageManager mPackageManager;
private final boolean mIsMessages; // if this is false, then this preference is for calls
private PreferenceCategory mPreferenceCategory;
private List<RadioButtonPreferenceWithExtraWidget> mRadioButtonPreferences = new ArrayList<>();
public ZenModePrioritySendersPreferenceController(Context context, String key,
Lifecycle lifecycle, boolean isMessages) {
super(context, key, lifecycle);
mIsMessages = isMessages;
mPackageManager = mContext.getPackageManager();
if (!FALLBACK_INTENT.hasCategory(Intent.CATEGORY_APP_CONTACTS)) {
FALLBACK_INTENT.addCategory(Intent.CATEGORY_APP_CONTACTS);
}
}
@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,
com.android.settings.R.string.zen_mode_from_none);
updateSummaries();
}
super.displayPreference(screen);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public void updateState(Preference preference) {
final int currSetting = getPrioritySenders();
for (RadioButtonPreferenceWithExtraWidget pref : mRadioButtonPreferences) {
pref.setChecked(keyToSetting(pref.getKey()) == currSetting);
}
}
@Override
public void onResume() {
super.onResume();
updateSummaries();
}
private void updateSummaries() {
for (RadioButtonPreferenceWithExtraWidget pref : mRadioButtonPreferences) {
pref.setSummary(getSummary(pref.getKey()));
}
}
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(R.string.zen_mode_all_senders_summary,
mContext.getResources().getString(mIsMessages
? R.string.zen_mode_messages_list
: R.string.zen_mode_calls_list));
case KEY_NONE:
default:
return null;
}
}
private int getPrioritySenders() {
if (mIsMessages) {
return mBackend.getPriorityMessageSenders();
} else {
return mBackend.getPriorityCallSenders();
}
}
private RadioButtonPreferenceWithExtraWidget makeRadioPreference(String key, int titleId) {
RadioButtonPreferenceWithExtraWidget pref =
new RadioButtonPreferenceWithExtraWidget(mPreferenceCategory.getContext());
View.OnClickListener widgetClickListener = getWidgetClickListener(key);
if (widgetClickListener != null) {
pref.setExtraWidgetOnClickListener(widgetClickListener);
pref.setExtraWidgetVisibility(EXTRA_WIDGET_VISIBILITY_SETTING);
} else {
pref.setExtraWidgetVisibility(EXTRA_WIDGET_VISIBILITY_GONE);
}
pref.setKey(key);
pref.setTitle(titleId);
pref.setOnClickListener(mRadioButtonClickListener);
mPreferenceCategory.addPreference(pref);
mRadioButtonPreferences.add(pref);
return pref;
}
private RadioButtonPreference.OnClickListener mRadioButtonClickListener =
new RadioButtonPreference.OnClickListener() {
@Override
public void onRadioButtonClicked(RadioButtonPreference preference) {
int selectedSetting = keyToSetting(preference.getKey());
if (selectedSetting != getPrioritySenders()) {
mBackend.saveSenders(
mIsMessages ? PRIORITY_CATEGORY_MESSAGES : PRIORITY_CATEGORY_CALLS,
selectedSetting);
}
}
};
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

@@ -1,107 +0,0 @@
/*
* Copyright (C) 2018 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.PRIORITY_CATEGORY_CALLS;
import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.Contacts;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.core.lifecycle.Lifecycle;
public class ZenModeStarredContactsPreferenceController extends
AbstractZenModePreferenceController implements Preference.OnPreferenceClickListener {
private Preference mPreference;
private final int mPriorityCategory;
private final PackageManager mPackageManager;
private Intent mStarredContactsIntent;
private Intent mFallbackIntent;
public ZenModeStarredContactsPreferenceController(Context context, Lifecycle lifecycle, int
priorityCategory, String key) {
super(context, key, lifecycle);
mPriorityCategory = priorityCategory;
mPackageManager = mContext.getPackageManager();
mStarredContactsIntent = new Intent(Contacts.Intents.UI.LIST_STARRED_ACTION);
mFallbackIntent = new Intent(Intent.ACTION_MAIN);
mFallbackIntent.addCategory(Intent.CATEGORY_APP_CONTACTS);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(KEY);
if (mPreference != null) {
mPreference.setOnPreferenceClickListener(this);
}
}
@Override
public String getPreferenceKey() {
return KEY;
}
@Override
public boolean isAvailable() {
if (mPriorityCategory == PRIORITY_CATEGORY_CALLS) {
return mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_CALLS)
&& mBackend.getPriorityCallSenders() == PRIORITY_SENDERS_STARRED
&& isIntentValid();
} else if (mPriorityCategory == PRIORITY_CATEGORY_MESSAGES) {
return mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_MESSAGES)
&& mBackend.getPriorityMessageSenders() == PRIORITY_SENDERS_STARRED
&& isIntentValid();
} else {
// invalid category
return false;
}
}
@Override
public CharSequence getSummary() {
return mBackend.getStarredContactsSummary(mContext);
}
@Override
public boolean onPreferenceClick(Preference preference) {
mMetricsFeatureProvider.logClickedPreference(preference,
preference.getExtras().getInt(DashboardFragment.CATEGORY));
if (mStarredContactsIntent.resolveActivity(mPackageManager) != null) {
mContext.startActivity(mStarredContactsIntent);
} else {
mContext.startActivity(mFallbackIntent);
}
return true;
}
private boolean isIntentValid() {
return mStarredContactsIntent.resolveActivity(mPackageManager) != null
|| mFallbackIntent.resolveActivity(mPackageManager) != null;
}
}