Merge "Update Zen Automatic Rule Settings page"
This commit is contained in:
committed by
Android (Google) Code Review
commit
3645640904
@@ -20,13 +20,13 @@
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal">
|
||||
<ImageView
|
||||
android:id="@+id/delete_zen_rule"
|
||||
android:id="@+id/zen_automatic_rule_widget"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="fill_parent"
|
||||
android:paddingStart="16dip"
|
||||
android:paddingEnd="16dip"
|
||||
android:src="@drawable/ic_delete"
|
||||
android:contentDescription="@string/zen_mode_delete_rule"
|
||||
android:src="@drawable/ic_settings"
|
||||
android:contentDescription="zen_mode_rule_settings"
|
||||
android:layout_gravity="center"
|
||||
android:background="?android:attr/selectableItemBackground" />
|
||||
</LinearLayout>
|
||||
|
@@ -7117,6 +7117,12 @@
|
||||
<!-- Do not disturb: Title for the zen mode automation option in Settings. [CHAR LIMIT=40] -->
|
||||
<string name="zen_mode_automation_settings_title">Schedules</string>
|
||||
|
||||
<!-- Do not disturb: Title for dialog that allows users to delete DND rules/schedules[CHAR LIMIT=40] -->
|
||||
<string name="zen_mode_delete_automatic_rules">Delete schedules</string>
|
||||
|
||||
<!-- Do not disturb: Delete text button presented in a dialog to confirm the user would like to delete the selected DND rules. [CHAR LIMIT=30] -->
|
||||
<string name="zen_mode_schedule_delete">Delete</string>
|
||||
|
||||
<!-- Do not disturb: Title for the zen mode automatic rules page in settings. [CHAR LIMIT=30] -->
|
||||
<string name="zen_mode_automation_settings_page_title">Do Not Disturb</string>
|
||||
|
||||
@@ -7253,6 +7259,9 @@
|
||||
<!-- [CHAR LIMIT=110] Zen mode settings footer: Footer how DND was triggered by an app -->
|
||||
<string name="zen_mode_settings_dnd_automatic_rule_app">Do Not Disturb was automatically turned on by an app (<xliff:g id="app_name" example="Android Services">%s</xliff:g>)</string>
|
||||
|
||||
<!-- [CHAR LIMIT=120] Zen mode settings footer: Footer informing user DND has custom settings. -->
|
||||
<string name="zen_mode_settings_dnd_custom_settings_footer">Do Not Disturb is on for <xliff:g id="rule_names" example="Sleeping and Work">%s</xliff:g> with custom settings.</string>
|
||||
|
||||
<!--[CHAR LIMIT=40] Zen Interruption level: Priority. -->
|
||||
<string name="zen_interruption_level_priority">Priority only</string>
|
||||
|
||||
@@ -7848,6 +7857,15 @@
|
||||
<!-- [CHAR LIMIT=NONE] Zen mode settings: Downtime rule setting -->
|
||||
<string name="zen_mode_schedule_alarm_summary">Stop at the end time or next alarm, whichever comes first</string>
|
||||
|
||||
<!-- [CHAR LIMIT=80] Zen mode settings: Title for preference to allow custom behavior for the dnd schedule -->
|
||||
<string name="zen_mode_custom_behavior_title">Do Not Disturb behavior</string>
|
||||
|
||||
<!-- [CHAR LIMIT=120] Zen mode settings: Summay text indicating the currenty dnd schedule is using the default dnd settings -->
|
||||
<string name="zen_mode_custom_behavior_summary_default">Use default settings</string>
|
||||
|
||||
<!-- [CHAR LIMIT=120] Zen mode settings: Summay text indicating the currenty dnd schedule is using custom behavior -->
|
||||
<string name="zen_mode_custom_behavior_summary">Create custom settings for this schedule</string>
|
||||
|
||||
<!-- [CHAR LIMIT=40] General divider text when concatenating multiple items in a text summary -->
|
||||
<string name="summary_divider_text">,\u0020</string>
|
||||
|
||||
|
@@ -43,4 +43,10 @@
|
||||
android:summary="@string/zen_mode_schedule_alarm_summary"
|
||||
android:order="99" />
|
||||
|
||||
<!-- Custom Do Not Disturb Setting-->
|
||||
<Preference
|
||||
android:key="zen_schedule_custom_setting"
|
||||
android:title="@string/zen_mode_custom_behavior_title"
|
||||
android:order="100" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
@@ -25,7 +25,6 @@ import android.content.pm.PackageManager;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.provider.Settings;
|
||||
import android.service.notification.ConditionProviderService;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.preference.Preference;
|
||||
@@ -34,20 +33,15 @@ import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
abstract public class AbstractZenModeAutomaticRulePreferenceController extends
|
||||
AbstractZenModePreferenceController implements PreferenceControllerMixin {
|
||||
|
||||
protected ZenModeBackend mBackend;
|
||||
protected Fragment mParent;
|
||||
protected Set<Map.Entry<String, AutomaticZenRule>> mRules;
|
||||
protected Map.Entry<String, AutomaticZenRule>[] mRules;
|
||||
protected PackageManager mPm;
|
||||
private static List<String> mDefaultRuleIds;
|
||||
|
||||
public AbstractZenModeAutomaticRulePreferenceController(Context context, String key, Fragment
|
||||
parent, Lifecycle lifecycle) {
|
||||
@@ -60,20 +54,14 @@ abstract public class AbstractZenModeAutomaticRulePreferenceController extends
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
mRules = getZenModeRules();
|
||||
mRules = mBackend.getAutomaticZenRules();
|
||||
}
|
||||
|
||||
private static List<String> getDefaultRuleIds() {
|
||||
if (mDefaultRuleIds == null) {
|
||||
mDefaultRuleIds = ZenModeConfig.DEFAULT_RULE_IDS;
|
||||
protected Map.Entry<String, AutomaticZenRule>[] getRules() {
|
||||
if (mRules == null) {
|
||||
mRules = mBackend.getAutomaticZenRules();
|
||||
}
|
||||
return mDefaultRuleIds;
|
||||
}
|
||||
|
||||
private Set<Map.Entry<String, AutomaticZenRule>> getZenModeRules() {
|
||||
Map<String, AutomaticZenRule> ruleMap =
|
||||
NotificationManager.from(mContext).getAutomaticZenRules();
|
||||
return ruleMap.entrySet();
|
||||
return mRules;
|
||||
}
|
||||
|
||||
protected void showNameRuleDialog(final ZenRuleInfo ri, Fragment parent) {
|
||||
@@ -81,16 +69,6 @@ abstract public class AbstractZenModeAutomaticRulePreferenceController extends
|
||||
RuleNameChangeListener(ri));
|
||||
}
|
||||
|
||||
protected Map.Entry<String, AutomaticZenRule>[] sortedRules() {
|
||||
if (mRules == null) {
|
||||
mRules = getZenModeRules();
|
||||
}
|
||||
final Map.Entry<String, AutomaticZenRule>[] rt =
|
||||
mRules.toArray(new Map.Entry[mRules.size()]);
|
||||
Arrays.sort(rt, RULE_COMPARATOR);
|
||||
return rt;
|
||||
}
|
||||
|
||||
protected static Intent getRuleIntent(String settingsAction,
|
||||
ComponentName configurationActivity, String ruleId) {
|
||||
final Intent intent = new Intent()
|
||||
@@ -104,35 +82,6 @@ abstract public class AbstractZenModeAutomaticRulePreferenceController extends
|
||||
return intent;
|
||||
}
|
||||
|
||||
private static final Comparator<Map.Entry<String, AutomaticZenRule>> RULE_COMPARATOR =
|
||||
new Comparator<Map.Entry<String, AutomaticZenRule>>() {
|
||||
@Override
|
||||
public int compare(Map.Entry<String, AutomaticZenRule> lhs,
|
||||
Map.Entry<String, AutomaticZenRule> rhs) {
|
||||
// if it's a default rule, should be at the top of automatic rules
|
||||
boolean lhsIsDefaultRule = getDefaultRuleIds().contains(lhs.getKey());
|
||||
boolean rhsIsDefaultRule = getDefaultRuleIds().contains(rhs.getKey());
|
||||
if (lhsIsDefaultRule != rhsIsDefaultRule) {
|
||||
return lhsIsDefaultRule ? -1 : 1;
|
||||
}
|
||||
|
||||
int byDate = Long.compare(lhs.getValue().getCreationTime(),
|
||||
rhs.getValue().getCreationTime());
|
||||
if (byDate != 0) {
|
||||
return byDate;
|
||||
} else {
|
||||
return key(lhs.getValue()).compareTo(key(rhs.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
private String key(AutomaticZenRule rule) {
|
||||
final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId())
|
||||
? 1 : ZenModeConfig.isValidEventConditionId(rule.getConditionId())
|
||||
? 2 : 3;
|
||||
return type + rule.getName().toString();
|
||||
}
|
||||
};
|
||||
|
||||
public static ZenRuleInfo getRuleInfo(PackageManager pm, ServiceInfo si) {
|
||||
if (si == null || si.metaData == null) {
|
||||
return null;
|
||||
|
@@ -136,7 +136,7 @@ public class ZenAutomaticRuleHeaderPreferenceController extends AbstractZenModeP
|
||||
MetricsProto.MetricsEvent.ACTION_ZEN_MODE_RULE_NAME_CHANGE_OK);
|
||||
mRule.setName(ruleName);
|
||||
mRule.setModified(true);
|
||||
mBackend.setZenRule(mId, mRule);
|
||||
mBackend.updateZenRule(mId, mRule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -88,6 +88,6 @@ public class ZenAutomaticRuleSwitchPreferenceController extends
|
||||
final boolean enabled = isChecked;
|
||||
if (enabled == mRule.isEnabled()) return;
|
||||
mRule.setEnabled(enabled);
|
||||
mBackend.setZenRule(mId, mRule);
|
||||
mBackend.updateZenRule(mId, mRule);
|
||||
}
|
||||
}
|
||||
|
@@ -21,12 +21,12 @@ import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
public class ZenModeAlarmsPreferenceController extends
|
||||
AbstractZenModePreferenceController implements Preference.OnPreferenceChangeListener {
|
||||
|
||||
@@ -78,6 +78,7 @@ public class ZenModeAlarmsPreferenceController extends
|
||||
mMetricsFeatureProvider.action(mContext, MetricsProto.MetricsEvent.ACTION_ZEN_ALLOW_ALARMS,
|
||||
allowAlarms);
|
||||
mBackend.saveSoundPolicy(Policy.PRIORITY_CATEGORY_ALARMS, allowAlarms);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -28,6 +28,7 @@ import androidx.preference.PreferenceScreen;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ZenModeAutomaticRulesPreferenceController extends
|
||||
AbstractZenModeAutomaticRulePreferenceController {
|
||||
@@ -62,16 +63,40 @@ public class ZenModeAutomaticRulesPreferenceController extends
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
Map.Entry<String, AutomaticZenRule>[] sortedRules = getRules();
|
||||
final int currNumPreferences = mPreferenceCategory.getPreferenceCount();
|
||||
if (currNumPreferences == sortedRules.length) {
|
||||
for (int i = 0; i < sortedRules.length; i++) {
|
||||
ZenRulePreference pref = (ZenRulePreference) mPreferenceCategory.getPreference(i);
|
||||
// we are either:
|
||||
// 1. updating the enabled state or name of the rule
|
||||
// 2. rule was added or deleted, so reload the entire list
|
||||
if (Objects.equals(pref.mId, sortedRules[i].getKey())) {
|
||||
AutomaticZenRule rule = sortedRules[i].getValue();
|
||||
pref.setName(rule.getName());
|
||||
pref.setChecked(rule.isEnabled());
|
||||
} else {
|
||||
reloadAllRules(sortedRules);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
reloadAllRules(sortedRules);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void reloadAllRules(Map.Entry<String, AutomaticZenRule>[] rules) {
|
||||
mPreferenceCategory.removeAll();
|
||||
Map.Entry<String, AutomaticZenRule>[] sortedRules = sortedRules();
|
||||
for (Map.Entry<String, AutomaticZenRule> sortedRule : sortedRules) {
|
||||
ZenRulePreference pref = new ZenRulePreference(mPreferenceCategory.getContext(),
|
||||
sortedRule, mParent, mMetricsFeatureProvider);
|
||||
for (Map.Entry<String, AutomaticZenRule> rule : rules) {
|
||||
ZenRulePreference pref = createZenRulePreference(rule);
|
||||
mPreferenceCategory.addPreference(pref);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
ZenRulePreference createZenRulePreference(Map.Entry<String, AutomaticZenRule> rule) {
|
||||
return new ZenRulePreference(mPreferenceCategory.getContext(),
|
||||
rule, mParent, mMetricsFeatureProvider);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@@ -16,9 +16,15 @@
|
||||
|
||||
package com.android.settings.notification;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.AutomaticZenRule;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.service.notification.ConditionProviderService;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
@@ -34,10 +40,14 @@ import com.android.settingslib.search.SearchIndexable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@SearchIndexable
|
||||
public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
||||
protected final ManagedServiceSettings.Config CONFIG = getConditionProviderConfig();
|
||||
private CharSequence[] mDeleteDialogRuleNames;
|
||||
private String[] mDeleteDialogRuleIds;
|
||||
private boolean[] mDeleteDialogChecked;
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
@@ -74,6 +84,52 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
||||
.setNoun("condition provider")
|
||||
.build();
|
||||
}
|
||||
private final int DELETE_RULES = 1;
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
menu.add(Menu.NONE, DELETE_RULES, Menu.NONE, R.string.zen_mode_delete_automatic_rules);
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case DELETE_RULES:
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = mBackend.getAutomaticZenRules();
|
||||
mDeleteDialogRuleNames = new CharSequence[rules.length];
|
||||
mDeleteDialogRuleIds = new String[rules.length];
|
||||
mDeleteDialogChecked = new boolean[rules.length];
|
||||
for (int i = 0; i < rules.length; i++) {
|
||||
mDeleteDialogRuleNames[i] = rules[i].getValue().getName();
|
||||
mDeleteDialogRuleIds[i] = rules[i].getKey();
|
||||
}
|
||||
new AlertDialog.Builder(mContext)
|
||||
.setTitle(R.string.zen_mode_delete_automatic_rules)
|
||||
.setMultiChoiceItems(mDeleteDialogRuleNames, null,
|
||||
new DialogInterface.OnMultiChoiceClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which,
|
||||
boolean isChecked) {
|
||||
mDeleteDialogChecked[which] = isChecked;
|
||||
}
|
||||
})
|
||||
.setPositiveButton(R.string.zen_mode_schedule_delete,
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
for (int i = 0; i < rules.length; i++) {
|
||||
if (mDeleteDialogChecked[i]) {
|
||||
mBackend.removeZenRule(mDeleteDialogRuleIds[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).show();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For Search.
|
||||
|
@@ -32,6 +32,11 @@ import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ZenModeBackend {
|
||||
@VisibleForTesting
|
||||
protected static final String ZEN_MODE_FROM_ANYONE = "zen_mode_from_anyone";
|
||||
@@ -42,6 +47,7 @@ public class ZenModeBackend {
|
||||
@VisibleForTesting
|
||||
protected static final String ZEN_MODE_FROM_NONE = "zen_mode_from_none";
|
||||
protected static final int SOURCE_NONE = -1;
|
||||
private static List<String> mDefaultRuleIds;
|
||||
|
||||
private static ZenModeBackend sInstance;
|
||||
|
||||
@@ -79,7 +85,7 @@ public class ZenModeBackend {
|
||||
Settings.Global.ZEN_MODE, mZenMode);
|
||||
}
|
||||
|
||||
protected boolean setZenRule(String id, AutomaticZenRule rule) {
|
||||
protected boolean updateZenRule(String id, AutomaticZenRule rule) {
|
||||
return NotificationManager.from(mContext).updateAutomaticZenRule(id, rule);
|
||||
}
|
||||
|
||||
@@ -294,13 +300,65 @@ public class ZenModeBackend {
|
||||
return NotificationManager.from(mContext).removeAutomaticZenRule(ruleId);
|
||||
}
|
||||
|
||||
public NotificationManager.Policy getConsolidatedPolicy() {
|
||||
return NotificationManager.from(mContext).getConsolidatedNotificationPolicy();
|
||||
}
|
||||
|
||||
protected String addZenRule(AutomaticZenRule rule) {
|
||||
try {
|
||||
String id = NotificationManager.from(mContext).addAutomaticZenRule(rule);
|
||||
NotificationManager.from(mContext).getAutomaticZenRule(id);
|
||||
return id;
|
||||
return NotificationManager.from(mContext).addAutomaticZenRule(rule);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected Map.Entry<String, AutomaticZenRule>[] getAutomaticZenRules() {
|
||||
Map<String, AutomaticZenRule> ruleMap =
|
||||
NotificationManager.from(mContext).getAutomaticZenRules();
|
||||
final Map.Entry<String, AutomaticZenRule>[] rt = ruleMap.entrySet().toArray(
|
||||
new Map.Entry[ruleMap.size()]);
|
||||
Arrays.sort(rt, RULE_COMPARATOR);
|
||||
return rt;
|
||||
}
|
||||
|
||||
protected AutomaticZenRule getAutomaticZenRule(String id) {
|
||||
return NotificationManager.from(mContext).getAutomaticZenRule(id);
|
||||
}
|
||||
|
||||
private static List<String> getDefaultRuleIds() {
|
||||
if (mDefaultRuleIds == null) {
|
||||
mDefaultRuleIds = ZenModeConfig.DEFAULT_RULE_IDS;
|
||||
}
|
||||
return mDefaultRuleIds;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static final Comparator<Map.Entry<String, AutomaticZenRule>> RULE_COMPARATOR =
|
||||
new Comparator<Map.Entry<String, AutomaticZenRule>>() {
|
||||
@Override
|
||||
public int compare(Map.Entry<String, AutomaticZenRule> lhs,
|
||||
Map.Entry<String, AutomaticZenRule> rhs) {
|
||||
// if it's a default rule, should be at the top of automatic rules
|
||||
boolean lhsIsDefaultRule = getDefaultRuleIds().contains(lhs.getKey());
|
||||
boolean rhsIsDefaultRule = getDefaultRuleIds().contains(rhs.getKey());
|
||||
if (lhsIsDefaultRule != rhsIsDefaultRule) {
|
||||
return lhsIsDefaultRule ? -1 : 1;
|
||||
}
|
||||
|
||||
int byDate = Long.compare(lhs.getValue().getCreationTime(),
|
||||
rhs.getValue().getCreationTime());
|
||||
if (byDate != 0) {
|
||||
return byDate;
|
||||
} else {
|
||||
return key(lhs.getValue()).compareTo(key(rhs.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
private String key(AutomaticZenRule rule) {
|
||||
final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId())
|
||||
? 1 : ZenModeConfig.isValidEventConditionId(rule.getConditionId())
|
||||
? 2 : 3;
|
||||
return type + rule.getName().toString();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@@ -124,7 +124,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase {
|
||||
|
||||
protected void updateRule(Uri newConditionId) {
|
||||
mRule.setConditionId(newConditionId);
|
||||
mBackend.setZenRule(mId, mRule);
|
||||
mBackend.updateZenRule(mId, mRule);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -64,7 +64,6 @@ public class ZenModeScheduleRuleSettings extends ZenModeRuleSettingsBase {
|
||||
private TimePickerPreference mEnd;
|
||||
private SwitchPreference mExitAtAlarm;
|
||||
private AlertDialog mDayDialog;
|
||||
|
||||
private ScheduleInfo mSchedule;
|
||||
|
||||
@Override
|
||||
@@ -186,7 +185,6 @@ public class ZenModeScheduleRuleSettings extends ZenModeRuleSettingsBase {
|
||||
final int summaryFormat = nextDay ? R.string.zen_mode_end_time_next_day_summary_format : 0;
|
||||
mEnd.setSummaryFormat(summaryFormat);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateControlsInternal() {
|
||||
updateDays();
|
||||
|
@@ -16,16 +16,23 @@
|
||||
|
||||
package com.android.settings.notification;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.icu.text.ListFormatter;
|
||||
import android.net.Uri;
|
||||
import android.provider.Settings;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ZenModeSettingsFooterPreferenceController extends AbstractZenModePreferenceController {
|
||||
|
||||
protected static final String KEY = "footer_preference";
|
||||
@@ -65,6 +72,31 @@ public class ZenModeSettingsFooterPreferenceController extends AbstractZenModePr
|
||||
|
||||
protected String getFooterText() {
|
||||
ZenModeConfig config = getZenModeConfig();
|
||||
|
||||
NotificationManager.Policy appliedPolicy = mBackend.getConsolidatedPolicy();
|
||||
NotificationManager.Policy defaultPolicy = config.toNotificationPolicy();
|
||||
final boolean usingCustomPolicy = !Objects.equals(appliedPolicy, defaultPolicy);
|
||||
|
||||
if (usingCustomPolicy) {
|
||||
final List<ZenModeConfig.ZenRule> activeRules = getActiveRules(config);
|
||||
final List<String> rulesNames = new ArrayList<>();
|
||||
for (ZenModeConfig.ZenRule rule : activeRules) {
|
||||
if (rule.name != null) {
|
||||
rulesNames.add(rule.name);
|
||||
}
|
||||
}
|
||||
if (rulesNames.size() > 0) {
|
||||
String rules = ListFormatter.getInstance().format(rulesNames);
|
||||
if (!rules.isEmpty()) {
|
||||
return mContext.getString(R.string.zen_mode_settings_dnd_custom_settings_footer,
|
||||
rules);
|
||||
}
|
||||
}
|
||||
}
|
||||
return getFooterUsingDefaultPolicy(config);
|
||||
}
|
||||
|
||||
private String getFooterUsingDefaultPolicy(ZenModeConfig config) {
|
||||
String footerText = "";
|
||||
long latestEndTime = -1;
|
||||
|
||||
@@ -116,4 +148,18 @@ public class ZenModeSettingsFooterPreferenceController extends AbstractZenModePr
|
||||
}
|
||||
return footerText;
|
||||
}
|
||||
|
||||
private List<ZenModeConfig.ZenRule> getActiveRules(ZenModeConfig config) {
|
||||
List<ZenModeConfig.ZenRule> zenRules = new ArrayList<>();
|
||||
if (config.manualRule != null) {
|
||||
zenRules.add(config.manualRule);
|
||||
}
|
||||
|
||||
for (ZenModeConfig.ZenRule automaticRule : config.automaticRules.values()) {
|
||||
if (automaticRule.isAutomaticActive()) {
|
||||
zenRules.add(automaticRule);
|
||||
}
|
||||
}
|
||||
return zenRules;
|
||||
}
|
||||
}
|
||||
|
@@ -19,17 +19,18 @@ package com.android.settings.notification;
|
||||
import android.app.AutomaticZenRule;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
import android.view.View;
|
||||
import android.widget.CheckBox;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.utils.ManagedServiceSettings;
|
||||
import com.android.settings.utils.ZenServiceListing;
|
||||
@@ -37,11 +38,11 @@ import com.android.settingslib.TwoTargetPreference;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ZenRulePreference extends TwoTargetPreference {
|
||||
private static final ManagedServiceSettings.Config CONFIG =
|
||||
ZenModeAutomationSettings.getConditionProviderConfig();
|
||||
final CharSequence mName;
|
||||
final String mId;
|
||||
boolean appExists;
|
||||
final Fragment mParent;
|
||||
@@ -51,16 +52,23 @@ public class ZenRulePreference extends TwoTargetPreference {
|
||||
final ZenServiceListing mServiceListing;
|
||||
final PackageManager mPm;
|
||||
final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
final AutomaticZenRule mRule;
|
||||
CharSequence mName;
|
||||
|
||||
private boolean mIsSystemRule;
|
||||
private Intent mIntent;
|
||||
private boolean mChecked;
|
||||
private CheckBox mCheckBox;
|
||||
|
||||
public ZenRulePreference(Context context,
|
||||
final Map.Entry<String, AutomaticZenRule> ruleEntry,
|
||||
Fragment parent, MetricsFeatureProvider metricsProvider) {
|
||||
super(context);
|
||||
|
||||
setLayoutResource(R.layout.preference_checkable_two_target);
|
||||
mBackend = ZenModeBackend.getInstance(context);
|
||||
mContext = context;
|
||||
final AutomaticZenRule rule = ruleEntry.getValue();
|
||||
mName = rule.getName();
|
||||
mRule = ruleEntry.getValue();
|
||||
mName = mRule.getName();
|
||||
mId = ruleEntry.getKey();
|
||||
mParent = parent;
|
||||
mPm = mContext.getPackageManager();
|
||||
@@ -68,65 +76,97 @@ public class ZenRulePreference extends TwoTargetPreference {
|
||||
mServiceListing.reloadApprovedServices();
|
||||
mPref = this;
|
||||
mMetricsFeatureProvider = metricsProvider;
|
||||
|
||||
setAttributes(rule);
|
||||
mChecked = mRule.isEnabled();
|
||||
setAttributes(mRule);
|
||||
setWidgetLayoutResource(getSecondTargetResId());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getSecondTargetResId() {
|
||||
if (mId != null && ZenModeConfig.DEFAULT_RULE_IDS.contains(mId)) {
|
||||
return 0;
|
||||
if (mIntent != null) {
|
||||
return R.layout.zen_rule_widget;
|
||||
}
|
||||
|
||||
return R.layout.zen_rule_widget;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder view) {
|
||||
super.onBindViewHolder(view);
|
||||
View settingsWidget = view.findViewById(android.R.id.widget_frame);
|
||||
View divider = view.findViewById(R.id.two_target_divider);
|
||||
if (mIntent != null) {
|
||||
divider.setVisibility(View.VISIBLE);
|
||||
settingsWidget.setVisibility(View.VISIBLE);
|
||||
settingsWidget.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
mContext.startActivity(mIntent);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
divider.setVisibility(View.GONE);
|
||||
settingsWidget.setVisibility(View.GONE);
|
||||
settingsWidget.setOnClickListener(null);
|
||||
}
|
||||
|
||||
View v = view.findViewById(R.id.delete_zen_rule);
|
||||
if (v != null) {
|
||||
v.setOnClickListener(mDeleteListener);
|
||||
View checkboxContainer = view.findViewById(R.id.checkbox_container);
|
||||
if (checkboxContainer != null) {
|
||||
checkboxContainer.setOnClickListener(mOnCheckBoxClickListener);
|
||||
}
|
||||
mCheckBox = (CheckBox) view.findViewById(com.android.internal.R.id.checkbox);
|
||||
if (mCheckBox != null) {
|
||||
mCheckBox.setChecked(mChecked);
|
||||
}
|
||||
}
|
||||
|
||||
private final View.OnClickListener mDeleteListener = new View.OnClickListener() {
|
||||
public boolean isChecked() {
|
||||
return mChecked;
|
||||
}
|
||||
|
||||
public void setChecked(boolean checked) {
|
||||
mChecked = checked;
|
||||
if (mCheckBox != null) {
|
||||
mCheckBox.setChecked(checked);
|
||||
}
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
if (!Objects.equals(mName, name)) {
|
||||
mName = name;
|
||||
setTitle(mName);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick() {
|
||||
mOnCheckBoxClickListener.onClick(null);
|
||||
}
|
||||
|
||||
private View.OnClickListener mOnCheckBoxClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
showDeleteRuleDialog(mParent, mId, mName.toString());
|
||||
mRule.setEnabled(!mChecked);
|
||||
mBackend.updateZenRule(mId, mRule);
|
||||
setChecked(mRule.isEnabled());
|
||||
setAttributes(mRule);
|
||||
}
|
||||
};
|
||||
|
||||
private void showDeleteRuleDialog(final Fragment parent, final String ruleId,
|
||||
final String ruleName) {
|
||||
ZenDeleteRuleDialog.show(parent, ruleName, ruleId,
|
||||
new ZenDeleteRuleDialog.PositiveClickListener() {
|
||||
@Override
|
||||
public void onOk(String id) {
|
||||
mMetricsFeatureProvider.action(mContext,
|
||||
MetricsProto.MetricsEvent.ACTION_ZEN_DELETE_RULE_OK);
|
||||
mBackend.removeZenRule(id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void setAttributes(AutomaticZenRule rule) {
|
||||
final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
|
||||
rule.getConditionId(), true);
|
||||
final boolean isEvent = ZenModeConfig.isValidEventConditionId(rule.getConditionId());
|
||||
final boolean isSystemRule = isSchedule || isEvent;
|
||||
mIsSystemRule = isSchedule || isEvent;
|
||||
|
||||
try {
|
||||
ApplicationInfo info = mPm.getApplicationInfo(rule.getOwner().getPackageName(), 0);
|
||||
setSummary(computeRuleSummary(rule, isSystemRule, info.loadLabel(mPm)));
|
||||
setSummary(computeRuleSummary(rule, mIsSystemRule, info.loadLabel(mPm)));
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
appExists = false;
|
||||
return;
|
||||
}
|
||||
|
||||
appExists = true;
|
||||
setTitle(rule.getName());
|
||||
setTitle(mName);
|
||||
setPersistent(false);
|
||||
|
||||
final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION
|
||||
@@ -134,9 +174,8 @@ public class ZenRulePreference extends TwoTargetPreference {
|
||||
ServiceInfo si = mServiceListing.findService(rule.getOwner());
|
||||
ComponentName settingsActivity = AbstractZenModeAutomaticRulePreferenceController.
|
||||
getSettingsActivity(si);
|
||||
setIntent(AbstractZenModeAutomaticRulePreferenceController.getRuleIntent(action,
|
||||
settingsActivity, mId));
|
||||
setSelectable(settingsActivity != null || isSystemRule);
|
||||
mIntent = AbstractZenModeAutomaticRulePreferenceController.getRuleIntent(action,
|
||||
settingsActivity, mId);
|
||||
setKey(mId);
|
||||
}
|
||||
|
||||
|
@@ -16,13 +16,17 @@
|
||||
|
||||
package com.android.settings.notification;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.AutomaticZenRule;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
|
||||
@@ -31,161 +35,122 @@ import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.internal.util.reflection.FieldSetter;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class ZenModeAutomaticRulesPreferenceControllerTest {
|
||||
|
||||
private static final String GENERIC_RULE_NAME = "test";
|
||||
private static final String DEFAULT_ID_1 = "DEFAULT_1";
|
||||
private static final String DEFAULT_ID_2 = "DEFAULT_2";
|
||||
|
||||
private ZenModeAutomaticRulesPreferenceController mController;
|
||||
private final List<String> mDefaultIds = Arrays.asList(DEFAULT_ID_1, DEFAULT_ID_2);
|
||||
|
||||
@Mock
|
||||
private ZenModeBackend mBackend;
|
||||
@Mock
|
||||
private NotificationManager mNotificationManager;
|
||||
@Mock
|
||||
private PreferenceCategory mockPref;
|
||||
@Mock
|
||||
private NotificationManager.Policy mPolicy;
|
||||
@Mock
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
|
||||
@Mock
|
||||
private ZenRulePreference mZenRulePreference;
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
ShadowApplication shadowApplication = ShadowApplication.getInstance();
|
||||
shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
|
||||
mController = new ZenModeAutomaticRulesPreferenceController(mContext, mock(Fragment.class),
|
||||
mock(Lifecycle.class));
|
||||
|
||||
mController = spy(new ZenModeAutomaticRulesPreferenceController(mContext, mock(Fragment.class),
|
||||
null));
|
||||
ReflectionHelpers.setField(mController, "mBackend", mBackend);
|
||||
ReflectionHelpers.setField(mController, "mDefaultRuleIds", mDefaultIds);
|
||||
|
||||
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
|
||||
mockPref);
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
doReturn(mZenRulePreference).when(mController).createZenRulePreference(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_checkRuleOrderingDescending() {
|
||||
final int NUM_RULES = 4;
|
||||
when(mNotificationManager.getAutomaticZenRules()).thenReturn(
|
||||
mockAutoZenRulesDecreasingCreationTime(NUM_RULES));
|
||||
public void testUpdateState_clearsPreferencesWhenAddingNewPreferences() {
|
||||
final int NUM_RULES = 3;
|
||||
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
||||
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
|
||||
assertEquals(NUM_RULES, rules.length);
|
||||
String ruleId1 = "test1_id";
|
||||
String ruleId2 = "test2_id";
|
||||
String ruleId3 = "test3_id";
|
||||
|
||||
// check ordering, most recent should be at the bottom/end (ie higher creation time)
|
||||
for (int i = 0; i < NUM_RULES; i++) {
|
||||
assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i].getKey());
|
||||
}
|
||||
AutomaticZenRule autoRule1 = new AutomaticZenRule("test_rule_1", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
|
||||
AutomaticZenRule autoRule2 = new AutomaticZenRule("test_rule_2", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20);
|
||||
AutomaticZenRule autoRule3 = new AutomaticZenRule("test_rule_3", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 30);
|
||||
|
||||
rMap.put(ruleId1, autoRule1);
|
||||
rMap.put(ruleId2, autoRule2);
|
||||
rMap.put(ruleId3, autoRule3);
|
||||
|
||||
// should add 3 new preferences to mockPref
|
||||
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
||||
mController.updateState(mockPref);
|
||||
verify(mockPref, times(1)).removeAll();
|
||||
verify(mockPref, times(NUM_RULES)).addPreference(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_checkRuleOrderingAscending() {
|
||||
final int NUM_RULES = 4;
|
||||
when(mNotificationManager.getAutomaticZenRules()).thenReturn(
|
||||
mockAutoZenRulesAscendingCreationTime(NUM_RULES));
|
||||
public void testUpdateState_clearsPreferencesWhenRemovingPreferences(){
|
||||
final int NUM_RULES = 2;
|
||||
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
||||
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
|
||||
assertEquals(NUM_RULES, rules.length);
|
||||
String ruleId1 = "test1_id";
|
||||
String ruleId2 = "test2_id";
|
||||
|
||||
// check ordering, most recent should be at the bottom/end (ie higher creation time)
|
||||
for (int i = 0; i < NUM_RULES; i++) {
|
||||
assertEquals(GENERIC_RULE_NAME + i, rules[i].getKey());
|
||||
}
|
||||
AutomaticZenRule autoRule1 = new AutomaticZenRule("test_rule_1", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
|
||||
AutomaticZenRule autoRule2 = new AutomaticZenRule("test_rule_2", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20);
|
||||
|
||||
rMap.put(ruleId1, autoRule1);
|
||||
rMap.put(ruleId2, autoRule2);
|
||||
|
||||
// update state should re-add all preferences since a preference was deleted
|
||||
when(mockPref.getPreferenceCount()).thenReturn(NUM_RULES + 2);
|
||||
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
||||
mController.updateState(mockPref);
|
||||
verify(mockPref, times(1)).removeAll();
|
||||
verify(mockPref, times(NUM_RULES)).addPreference(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_checkRuleOrderingDescending_withDefaultRules() {
|
||||
final int NUM_RULES = 4;
|
||||
public void testUpdateState_updateEnableState() throws NoSuchFieldException {
|
||||
final int NUM_RULES = 1;
|
||||
Map<String, AutomaticZenRule> rMap = new HashMap<>();
|
||||
String testId = "test1_id";
|
||||
AutomaticZenRule rule = new AutomaticZenRule("rule_name", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10);
|
||||
rMap.put(testId, rule);
|
||||
|
||||
Map<String, AutomaticZenRule> ruleMap = mockAutoZenRulesDecreasingCreationTime(NUM_RULES);
|
||||
ruleMap.put(DEFAULT_ID_2, new AutomaticZenRule("DEFAULT_1_NAME", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20));
|
||||
ruleMap.put(DEFAULT_ID_1, new AutomaticZenRule("DEFAULT_1_NAME", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10));
|
||||
when(mNotificationManager.getAutomaticZenRules()).thenReturn(ruleMap);
|
||||
when(mockPref.getPreferenceCount()).thenReturn(NUM_RULES);
|
||||
when(mockPref.getPreference(anyInt())).thenReturn(mZenRulePreference);
|
||||
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
|
||||
assertEquals(NUM_RULES + 2, rules.length);
|
||||
|
||||
assertEquals(rules[0].getKey(), DEFAULT_ID_1);
|
||||
assertEquals(rules[1].getKey(), DEFAULT_ID_2);
|
||||
// NON-DEFAULT RULES check ordering, most recent at the bottom/end
|
||||
for (int i = 0; i < NUM_RULES; i++) {
|
||||
assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i + 2].getKey());
|
||||
}
|
||||
// update state should NOT re-add all the preferences, should only update enable state
|
||||
rule.setEnabled(false);
|
||||
rMap.put(testId, rule);
|
||||
mockGetAutomaticZenRules(NUM_RULES, rMap);
|
||||
FieldSetter.setField(mZenRulePreference, ZenRulePreference.class.getDeclaredField("mId"), testId);
|
||||
mController.updateState(mockPref);
|
||||
verify(mZenRulePreference, times(1)).setChecked(false);
|
||||
verify(mController, never()).reloadAllRules(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_checkRuleOrderingMix() {
|
||||
final int NUM_RULES = 4;
|
||||
// map with creation times: 0, 2, 4, 6
|
||||
Map<String,AutomaticZenRule> rMap = mockAutoZenRulesAscendingCreationTime(NUM_RULES);
|
||||
|
||||
final String insertedRule1 = "insertedRule1";
|
||||
rMap.put(insertedRule1, new AutomaticZenRule(insertedRule1, null, null,
|
||||
Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 5));
|
||||
|
||||
final String insertedRule2 = "insertedRule2";
|
||||
rMap.put(insertedRule2, new AutomaticZenRule(insertedRule2, null, null,
|
||||
Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 3));
|
||||
|
||||
// rule map with rule creation times, 0, 2, 4, 6, 5, 3
|
||||
// sort should create ordering based on creation times: 0, 2, 3, 4, 5, 6
|
||||
when(mNotificationManager.getAutomaticZenRules()).thenReturn(rMap);
|
||||
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = mController.sortedRules();
|
||||
assertEquals(NUM_RULES + 2, rules.length); // inserted 2 rules
|
||||
|
||||
// check ordering of inserted rules
|
||||
assertEquals(insertedRule1, rules[4].getKey());
|
||||
assertEquals(insertedRule2, rules[2].getKey());
|
||||
}
|
||||
|
||||
private Map<String, AutomaticZenRule> mockAutoZenRulesAscendingCreationTime(int numRules) {
|
||||
Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < numRules; i++) {
|
||||
ruleMap.put(GENERIC_RULE_NAME + i, new AutomaticZenRule(GENERIC_RULE_NAME + i, null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, i * 2));
|
||||
}
|
||||
|
||||
return ruleMap;
|
||||
}
|
||||
|
||||
private Map<String, AutomaticZenRule> mockAutoZenRulesDecreasingCreationTime(int numRules) {
|
||||
Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < numRules; i++) {
|
||||
ruleMap.put(GENERIC_RULE_NAME + i, new AutomaticZenRule(GENERIC_RULE_NAME + i, null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, numRules - i));
|
||||
}
|
||||
|
||||
return ruleMap;
|
||||
private void mockGetAutomaticZenRules(int numRules, Map<String, AutomaticZenRule> rules) {
|
||||
Map.Entry<String, AutomaticZenRule>[] arr = new Map.Entry[numRules];
|
||||
rules.entrySet().toArray(arr);
|
||||
when(mBackend.getAutomaticZenRules()).thenReturn(arr);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,108 @@
|
||||
package com.android.settings.notification;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
import android.app.AutomaticZenRule;
|
||||
import android.provider.Settings;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class ZenModeBackendTest {
|
||||
|
||||
private static final String GENERIC_RULE_NAME = "test";
|
||||
private static final String DEFAULT_ID_1 = ZenModeConfig.EVENTS_DEFAULT_RULE_ID;
|
||||
private static final String DEFAULT_ID_2 = ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID;
|
||||
|
||||
@Test
|
||||
public void updateState_checkRuleOrderingDescending() {
|
||||
final int NUM_RULES = 4;
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesDescendingCreationTime(
|
||||
NUM_RULES, false);
|
||||
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
|
||||
|
||||
// check ordering, most recent should be at the end
|
||||
for (int i = 0; i < NUM_RULES; i++) {
|
||||
assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i].getKey());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_checkRuleOrderingAscending() {
|
||||
final int NUM_RULES = 4;
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesAscendingCreationTime(
|
||||
NUM_RULES, false);
|
||||
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
|
||||
|
||||
// check ordering, most recent should be at the end
|
||||
for (int i = 0; i < NUM_RULES; i++) {
|
||||
assertEquals(GENERIC_RULE_NAME + i, rules[i].getKey());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_checkRuleOrderingDescending_withDefaultRules() {
|
||||
final int NUM_RULES = 4;
|
||||
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = populateAutoZenRulesDescendingCreationTime(NUM_RULES,
|
||||
true);
|
||||
Arrays.sort(rules, ZenModeBackend.RULE_COMPARATOR);
|
||||
|
||||
assertEquals(rules[0].getKey(), DEFAULT_ID_1);
|
||||
assertEquals(rules[1].getKey(), DEFAULT_ID_2);
|
||||
// NON-DEFAULT RULES check ordering, most recent at the bottom/end
|
||||
for (int i = 0; i < NUM_RULES; i++) {
|
||||
assertEquals(GENERIC_RULE_NAME + (NUM_RULES - 1 - i), rules[i + 2].getKey());
|
||||
}
|
||||
}
|
||||
|
||||
private Map.Entry<String, AutomaticZenRule>[] populateAutoZenRulesAscendingCreationTime(
|
||||
int numRules, boolean addDefaultRules) {
|
||||
Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < numRules; i++) {
|
||||
ruleMap.put(GENERIC_RULE_NAME + i, new AutomaticZenRule(GENERIC_RULE_NAME + i, null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, i * 2));
|
||||
}
|
||||
|
||||
if (addDefaultRules) {
|
||||
ruleMap.put(DEFAULT_ID_1, new AutomaticZenRule("DEFAULT_1_NAME", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20));
|
||||
ruleMap.put(DEFAULT_ID_2, new AutomaticZenRule("DEFAULT_2_NAME", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10));
|
||||
}
|
||||
|
||||
Map.Entry<String, AutomaticZenRule>[] toReturn = new Map.Entry[ruleMap.size()];
|
||||
ruleMap.entrySet().toArray(toReturn);
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private Map.Entry<String, AutomaticZenRule>[] populateAutoZenRulesDescendingCreationTime(
|
||||
int numRules, boolean addDefaultRules) {
|
||||
Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < numRules; i++) {
|
||||
ruleMap.put(GENERIC_RULE_NAME + i, new AutomaticZenRule(GENERIC_RULE_NAME + i, null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, numRules - i));
|
||||
}
|
||||
|
||||
if (addDefaultRules) {
|
||||
ruleMap.put(DEFAULT_ID_1, new AutomaticZenRule("DEFAULT_1_NAME", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 10));
|
||||
ruleMap.put(DEFAULT_ID_2, new AutomaticZenRule("DEFAULT_2_NAME", null,
|
||||
null, Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, true, 20));
|
||||
}
|
||||
|
||||
Map.Entry<String, AutomaticZenRule>[] toReturn = new Map.Entry[ruleMap.size()];
|
||||
ruleMap.entrySet().toArray(toReturn);
|
||||
return toReturn;
|
||||
}
|
||||
}
|
@@ -126,7 +126,7 @@ public class ZenModeSettingsFooterPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void app_manualRule_setFooterTitle() {
|
||||
public void testDefaultNotifPolicy_app_manualRule_setFooterTitle() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
|
||||
injectManualRuleFromApp();
|
||||
mController.updateState(mockPref);
|
||||
@@ -137,7 +137,7 @@ public class ZenModeSettingsFooterPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void time_manualRule_setFooterTitle() {
|
||||
public void testDefaultNotifPolicy_time_manualRule_setFooterTitle() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
|
||||
String placeholder = "placeholder";
|
||||
injectManualRuleWithTimeCountdown(1000, placeholder);
|
||||
@@ -148,7 +148,7 @@ public class ZenModeSettingsFooterPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forever_manualRule_setFooterTitle() {
|
||||
public void testDefaultNotifPolicy_forever_manualRule_setFooterTitle() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
|
||||
injectManualRuleWithIndefiniteEnd();
|
||||
mController.updateState(mockPref);
|
||||
@@ -158,7 +158,7 @@ public class ZenModeSettingsFooterPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void automaticRule_noManualRule_setFooterTitle() {
|
||||
public void testDefaultNotifPolicy_automaticRule_noManualRule_setFooterTitle() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
|
||||
// no manual rule
|
||||
ReflectionHelpers.setField(mZenModeConfig, MANUAL_RULE_FIELD, null);
|
||||
@@ -174,7 +174,7 @@ public class ZenModeSettingsFooterPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void manualRuleEndsLast_hasAutomaticRule_setFooterTitle() {
|
||||
public void testDefaultNotifPolicy_manualRuleEndsLast_hasAutomaticRule_setFooterTitle() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
|
||||
// manual rule that ends after automatic rule ends
|
||||
injectManualRuleWithIndefiniteEnd();
|
||||
@@ -190,7 +190,7 @@ public class ZenModeSettingsFooterPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void automaticRuleEndsLast_hasManualRule_setFooterTitle() {
|
||||
public void testDefaultNotifPolicy_automaticRuleEndsLast_hasManualRule_setFooterTitle() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
|
||||
// manual rule that ends before automatic rule ends
|
||||
injectManualRuleWithTimeCountdown(1000, "");
|
||||
@@ -208,14 +208,15 @@ public class ZenModeSettingsFooterPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleAutomaticRules_appAutoRuleautomaticRuleApp_setFooterTitle() {
|
||||
public void testDefaultNotifPolicy_multipleAutomaticRules_autoRuleApp_setFooterTitle() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
|
||||
|
||||
// automatic rule that ends after manual rule ends
|
||||
ZenRule rule1 = injectNewAutomaticRule(TEST_RULE_NAME + "1", false, false);
|
||||
when(mConfigWrapper.parseAutomaticRuleEndTime(rule1.conditionId)).thenReturn(10000L);
|
||||
|
||||
ZenRule rule2 = injectNewAutomaticRule(TEST_RULE_NAME + "2", true, true);
|
||||
// automatic rule that is an app
|
||||
injectNewAutomaticRule(TEST_RULE_NAME + "2", true, true);
|
||||
|
||||
ZenRule rule3 = injectNewAutomaticRule(TEST_RULE_NAME + "3", true, false);
|
||||
when(mConfigWrapper.parseAutomaticRuleEndTime(rule3.conditionId)).thenReturn(9000L);
|
||||
@@ -229,7 +230,7 @@ public class ZenModeSettingsFooterPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleAutomaticRules_setFooterTitle() {
|
||||
public void testDefaultNotifPolicy_multipleAutomaticRules_setFooterTitle() {
|
||||
Settings.Global.putInt(mContentResolver, ZEN_MODE, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
|
||||
|
||||
// automatic rule that ends after manual rule ends
|
||||
|
Reference in New Issue
Block a user