Api review: AutomaticZenRule
Bug: 27364144 Change-Id: Ic0ea5d4958711cf5216e87ad8054460ad4246856
This commit is contained in:
@@ -37,7 +37,7 @@ import com.android.settings.Settings.WifiCallingSuggestionActivity;
|
|||||||
import com.android.settings.Settings.ZenModeAutomationSuggestionActivity;
|
import com.android.settings.Settings.ZenModeAutomationSuggestionActivity;
|
||||||
import com.android.settingslib.drawer.Tile;
|
import com.android.settingslib.drawer.Tile;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Home of all stupidly dynamic Settings Suggestions checks.
|
* The Home of all stupidly dynamic Settings Suggestions checks.
|
||||||
@@ -86,10 +86,10 @@ public class SuggestionsChecks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasEnabledZenAutoRules() {
|
private boolean hasEnabledZenAutoRules() {
|
||||||
List<AutomaticZenRule> zenRules = NotificationManager.from(mContext).getAutomaticZenRules();
|
Collection<AutomaticZenRule> zenRules =
|
||||||
final int N = zenRules.size();
|
NotificationManager.from(mContext).getAutomaticZenRules().values();
|
||||||
for (int i = 0; i < N; i++) {
|
for (AutomaticZenRule rule : zenRules) {
|
||||||
if (zenRules.get(i).isEnabled()) {
|
if (rule.isEnabled()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -46,7 +46,9 @@ import com.android.settings.notification.ManagedServiceSettings.Config;
|
|||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
||||||
|
|
||||||
@@ -111,15 +113,15 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
|||||||
AutomaticZenRule rule = new AutomaticZenRule(ruleName, ri.serviceComponent,
|
AutomaticZenRule rule = new AutomaticZenRule(ruleName, ri.serviceComponent,
|
||||||
ri.defaultConditionId, NotificationManager.INTERRUPTION_FILTER_PRIORITY,
|
ri.defaultConditionId, NotificationManager.INTERRUPTION_FILTER_PRIORITY,
|
||||||
true);
|
true);
|
||||||
AutomaticZenRule savedRule = addZenRule(rule);
|
String savedRuleId = addZenRule(rule);
|
||||||
if (savedRule != null) {
|
if (savedRuleId != null) {
|
||||||
startActivity(getRuleIntent(ri.settingsAction, null, savedRule.getId()));
|
startActivity(getRuleIntent(ri.settingsAction, null, savedRuleId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.show();
|
}.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showDeleteRuleDialog(final String ruleId, final String ruleName) {
|
private void showDeleteRuleDialog(final String ruleId, final CharSequence ruleName) {
|
||||||
new AlertDialog.Builder(mContext)
|
new AlertDialog.Builder(mContext)
|
||||||
.setMessage(getString(R.string.zen_mode_delete_rule_confirmation, ruleName))
|
.setMessage(getString(R.string.zen_mode_delete_rule_confirmation, ruleName))
|
||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
@@ -147,8 +149,9 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
|||||||
return intent;
|
return intent;
|
||||||
}
|
}
|
||||||
|
|
||||||
private AutomaticZenRule[] sortedRules() {
|
private Map.Entry<String,AutomaticZenRule>[] sortedRules() {
|
||||||
final AutomaticZenRule[] rt = mRules.toArray(new AutomaticZenRule[mRules.size()]);
|
final Map.Entry<String,AutomaticZenRule>[] rt =
|
||||||
|
mRules.toArray(new Map.Entry[mRules.size()]);
|
||||||
Arrays.sort(rt, RULE_COMPARATOR);
|
Arrays.sort(rt, RULE_COMPARATOR);
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
@@ -156,8 +159,8 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
|||||||
private void updateControls() {
|
private void updateControls() {
|
||||||
final PreferenceScreen root = getPreferenceScreen();
|
final PreferenceScreen root = getPreferenceScreen();
|
||||||
root.removeAll();
|
root.removeAll();
|
||||||
final AutomaticZenRule[] sortedRules = sortedRules();
|
final Map.Entry<String,AutomaticZenRule>[] sortedRules = sortedRules();
|
||||||
for (AutomaticZenRule sortedRule : sortedRules) {
|
for (Map.Entry<String,AutomaticZenRule> sortedRule : sortedRules) {
|
||||||
ZenRulePreference pref = new ZenRulePreference(getPrefContext(), sortedRule);
|
ZenRulePreference pref = new ZenRulePreference(getPrefContext(), sortedRule);
|
||||||
if (pref.appExists) {
|
if (pref.appExists) {
|
||||||
root.addPreference(pref);
|
root.addPreference(pref);
|
||||||
@@ -247,15 +250,17 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Comparator<AutomaticZenRule> RULE_COMPARATOR =
|
private static final Comparator<Map.Entry<String,AutomaticZenRule>> RULE_COMPARATOR =
|
||||||
new Comparator<AutomaticZenRule>() {
|
new Comparator<Map.Entry<String,AutomaticZenRule>>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(AutomaticZenRule lhs, AutomaticZenRule rhs) {
|
public int compare(Map.Entry<String,AutomaticZenRule> lhs,
|
||||||
int byDate = Long.compare(lhs.getCreationTime(), rhs.getCreationTime());
|
Map.Entry<String,AutomaticZenRule> rhs) {
|
||||||
|
int byDate = Long.compare(lhs.getValue().getCreationTime(),
|
||||||
|
rhs.getValue().getCreationTime());
|
||||||
if (byDate != 0) {
|
if (byDate != 0) {
|
||||||
return byDate;
|
return byDate;
|
||||||
} else {
|
} else {
|
||||||
return key(lhs).compareTo(key(rhs));
|
return key(lhs.getValue()).compareTo(key(rhs.getValue()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,20 +268,22 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
|||||||
final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId()) ? 1
|
final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId()) ? 1
|
||||||
: ZenModeConfig.isValidEventConditionId(rule.getConditionId()) ? 2
|
: ZenModeConfig.isValidEventConditionId(rule.getConditionId()) ? 2
|
||||||
: 3;
|
: 3;
|
||||||
return type + rule.getName();
|
return type + rule.getName().toString();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private class ZenRulePreference extends Preference {
|
private class ZenRulePreference extends Preference {
|
||||||
final String mName;
|
final CharSequence mName;
|
||||||
final String mId;
|
final String mId;
|
||||||
final boolean appExists;
|
final boolean appExists;
|
||||||
|
|
||||||
public ZenRulePreference(Context context, final AutomaticZenRule rule) {
|
public ZenRulePreference(Context context,
|
||||||
|
final Map.Entry<String, AutomaticZenRule> ruleEntry) {
|
||||||
super(context);
|
super(context);
|
||||||
|
|
||||||
|
final AutomaticZenRule rule = ruleEntry.getValue();
|
||||||
mName = rule.getName();
|
mName = rule.getName();
|
||||||
mId = rule.getId();
|
mId = ruleEntry.getKey();
|
||||||
|
|
||||||
final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
|
final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
|
||||||
rule.getConditionId());
|
rule.getConditionId());
|
||||||
|
@@ -123,7 +123,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
|||||||
if (zenMode == mRule.getInterruptionFilter()) return false;
|
if (zenMode == mRule.getInterruptionFilter()) return false;
|
||||||
if (DEBUG) Log.d(TAG, "onPrefChange zenMode=" + zenMode);
|
if (DEBUG) Log.d(TAG, "onPrefChange zenMode=" + zenMode);
|
||||||
mRule.setInterruptionFilter(zenMode);
|
mRule.setInterruptionFilter(zenMode);
|
||||||
setZenRule(mRule);
|
setZenRule(mId, mRule);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -166,7 +166,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
|||||||
MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_ENABLE_RULE, enabled);
|
MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_ENABLE_RULE, enabled);
|
||||||
if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled);
|
if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled);
|
||||||
mRule.setEnabled(enabled);
|
mRule.setEnabled(enabled);
|
||||||
setZenRule(mRule);
|
setZenRule(mId, mRule);
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
final int toastText = getEnabledToastText();
|
final int toastText = getEnabledToastText();
|
||||||
if (toastText != 0) {
|
if (toastText != 0) {
|
||||||
@@ -182,7 +182,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
|||||||
|
|
||||||
protected void updateRule(Uri newConditionId) {
|
protected void updateRule(Uri newConditionId) {
|
||||||
mRule.setConditionId(newConditionId);
|
mRule.setConditionId(newConditionId);
|
||||||
setZenRule(mRule);
|
setZenRule(mId, mRule);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -219,7 +219,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
|||||||
@Override
|
@Override
|
||||||
public void onOk(String ruleName) {
|
public void onOk(String ruleName) {
|
||||||
mRule.setName(ruleName);
|
mRule.setName(ruleName);
|
||||||
setZenRule(mRule);
|
setZenRule(mId, mRule);
|
||||||
}
|
}
|
||||||
}.show();
|
}.show();
|
||||||
}
|
}
|
||||||
@@ -243,7 +243,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
|||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_DELETE_RULE_OK);
|
MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_DELETE_RULE_OK);
|
||||||
mDeleting = true;
|
mDeleting = true;
|
||||||
removeZenRule(mRule.getId());
|
removeZenRule(mId);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.show();
|
.show();
|
||||||
|
@@ -31,7 +31,10 @@ import android.util.Log;
|
|||||||
|
|
||||||
import com.android.settings.RestrictedSettingsFragment;
|
import com.android.settings.RestrictedSettingsFragment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
|
abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
|
||||||
protected static final String TAG = "ZenModeSettings";
|
protected static final String TAG = "ZenModeSettings";
|
||||||
@@ -41,7 +44,7 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
|
|||||||
private final SettingsObserver mSettingsObserver = new SettingsObserver();
|
private final SettingsObserver mSettingsObserver = new SettingsObserver();
|
||||||
|
|
||||||
protected Context mContext;
|
protected Context mContext;
|
||||||
protected List<AutomaticZenRule> mRules;
|
protected Set<Map.Entry<String, AutomaticZenRule>> mRules;
|
||||||
protected int mZenMode;
|
protected int mZenMode;
|
||||||
|
|
||||||
abstract protected void onZenModeChanged();
|
abstract protected void onZenModeChanged();
|
||||||
@@ -92,16 +95,17 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected AutomaticZenRule addZenRule(AutomaticZenRule rule) {
|
protected String addZenRule(AutomaticZenRule rule) {
|
||||||
|
String id = NotificationManager.from(mContext).addAutomaticZenRule(rule);
|
||||||
final AutomaticZenRule savedRule =
|
final AutomaticZenRule savedRule =
|
||||||
NotificationManager.from(mContext).addAutomaticZenRule(rule);
|
NotificationManager.from(mContext).getAutomaticZenRule(id);
|
||||||
maybeRefreshRules(savedRule != null, true);
|
maybeRefreshRules(savedRule != null, true);
|
||||||
return savedRule;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean setZenRule(AutomaticZenRule rule) {
|
protected boolean setZenRule(String id, AutomaticZenRule rule) {
|
||||||
final boolean success =
|
final boolean success =
|
||||||
NotificationManager.from(mContext).updateAutomaticZenRule(rule);
|
NotificationManager.from(mContext).updateAutomaticZenRule(id, rule);
|
||||||
maybeRefreshRules(success, true);
|
maybeRefreshRules(success, true);
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
@@ -127,8 +131,10 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
|
|||||||
NotificationManager.from(mContext).setZenMode(zenMode, conditionId, TAG);
|
NotificationManager.from(mContext).setZenMode(zenMode, conditionId, TAG);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<AutomaticZenRule> getZenModeRules() {
|
private Set<Map.Entry<String, AutomaticZenRule>> getZenModeRules() {
|
||||||
return NotificationManager.from(mContext).getAutomaticZenRules();
|
Map<String, AutomaticZenRule> ruleMap
|
||||||
|
= NotificationManager.from(mContext).getAutomaticZenRules();
|
||||||
|
return ruleMap.entrySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class SettingsObserver extends ContentObserver {
|
private final class SettingsObserver extends ContentObserver {
|
||||||
|
@@ -31,10 +31,10 @@ public abstract class ZenRuleNameDialog {
|
|||||||
|
|
||||||
private final AlertDialog mDialog;
|
private final AlertDialog mDialog;
|
||||||
private final EditText mEditText;
|
private final EditText mEditText;
|
||||||
private final String mOriginalRuleName;
|
private final CharSequence mOriginalRuleName;
|
||||||
private final boolean mIsNew;
|
private final boolean mIsNew;
|
||||||
|
|
||||||
public ZenRuleNameDialog(Context context, String ruleName) {
|
public ZenRuleNameDialog(Context context, CharSequence ruleName) {
|
||||||
mIsNew = ruleName == null;
|
mIsNew = ruleName == null;
|
||||||
mOriginalRuleName = ruleName;
|
mOriginalRuleName = ruleName;
|
||||||
final View v = LayoutInflater.from(context).inflate(R.layout.zen_rule_name, null, false);
|
final View v = LayoutInflater.from(context).inflate(R.layout.zen_rule_name, null, false);
|
||||||
@@ -52,7 +52,7 @@ public abstract class ZenRuleNameDialog {
|
|||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
final String newName = trimmedText();
|
final String newName = trimmedText();
|
||||||
if (!mIsNew && mOriginalRuleName != null
|
if (!mIsNew && mOriginalRuleName != null
|
||||||
&& mOriginalRuleName.equalsIgnoreCase(newName)) {
|
&& mOriginalRuleName.equals(newName)) {
|
||||||
return; // no change to an existing rule, just dismiss
|
return; // no change to an existing rule, just dismiss
|
||||||
}
|
}
|
||||||
onOk(newName);
|
onOk(newName);
|
||||||
|
Reference in New Issue
Block a user