Use id-based zen rule APIs.

Also names no longer have to be unique.

Bug: 22977552
Change-Id: I8dbee85c15d12d5380345447047a0d49c903522e
This commit is contained in:
Julia Reynolds
2015-10-07 19:32:22 -04:00
parent 38ee17a4ae
commit 25cb8f0691
4 changed files with 34 additions and 83 deletions

View File

@@ -106,21 +106,22 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
} }
private void showNameRuleDialog(final ZenRuleInfo ri) { private void showNameRuleDialog(final ZenRuleInfo ri) {
new ZenRuleNameDialog(mContext, mServiceListing, null, mRules) { new ZenRuleNameDialog(mContext, null) {
@Override @Override
public void onOk(String ruleName) { public void onOk(String ruleName) {
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ADD_RULE_OK); MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ADD_RULE_OK);
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);
if (setZenRule(rule)) { AutomaticZenRule savedRule = addZenRule(rule);
startActivity(getRuleIntent(ri.settingsAction, null, rule.getName())); if (savedRule != null) {
startActivity(getRuleIntent(ri.settingsAction, null, savedRule.getId()));
} }
} }
}.show(); }.show();
} }
private void showDeleteRuleDialog(final String ruleName) { private void showDeleteRuleDialog(final String ruleId, final String 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)
@@ -129,17 +130,17 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK); MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK);
removeZenRule(ruleName); removeZenRule(ruleId);
} }
}) })
.show(); .show();
} }
private Intent getRuleIntent(String settingsAction, ComponentName configurationActivity, private Intent getRuleIntent(String settingsAction, ComponentName configurationActivity,
String ruleName) { String ruleId) {
Intent intent = new Intent() Intent intent = new Intent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.putExtra(ConditionProviderService.EXTRA_RULE_NAME, ruleName); .putExtra(ConditionProviderService.EXTRA_RULE_ID, ruleId);
if (configurationActivity != null) { if (configurationActivity != null) {
intent.setComponent(configurationActivity); intent.setComponent(configurationActivity);
} else { } else {
@@ -237,9 +238,8 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
public static ZenRuleInfo getRuleInfo(ServiceInfo si) { public static ZenRuleInfo getRuleInfo(ServiceInfo si) {
if (si == null || si.metaData == null) return null; if (si == null || si.metaData == null) return null;
final String ruleType = si.metaData.getString(ConditionProviderService.META_DATA_RULE_TYPE); final String ruleType = si.metaData.getString(ConditionProviderService.META_DATA_RULE_TYPE);
final String defaultConditionId = final ComponentName configurationActivity = getSettingsActivity(si);
si.metaData.getString(ConditionProviderService.META_DATA_DEFAULT_CONDITION_ID); if (ruleType != null && !ruleType.trim().isEmpty() && configurationActivity != null) {
if (ruleType != null && !ruleType.trim().isEmpty() && defaultConditionId != null) {
final ZenRuleInfo ri = new ZenRuleInfo(); final ZenRuleInfo ri = new ZenRuleInfo();
ri.settingsAction = Settings.ACTION_ZEN_MODE_EXTERNAL_RULE_SETTINGS; ri.settingsAction = Settings.ACTION_ZEN_MODE_EXTERNAL_RULE_SETTINGS;
ri.title = ruleType; ri.title = ruleType;
@@ -279,11 +279,13 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
private class ZenRulePreference extends Preference { private class ZenRulePreference extends Preference {
final String mName; final String mName;
final String mId;
public ZenRulePreference(Context context, final AutomaticZenRule rule) { public ZenRulePreference(Context context, final AutomaticZenRule rule) {
super(context); super(context);
mName = rule.getName(); mName = rule.getName();
mId = rule.getId();
final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId( final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
rule.getConditionId()); rule.getConditionId());
@@ -306,7 +308,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
: isEvent ? ZenModeEventRuleSettings.ACTION : ""; : isEvent ? ZenModeEventRuleSettings.ACTION : "";
ServiceInfo si = mServiceListing.findService(mContext, CONFIG, rule.getOwner()); ServiceInfo si = mServiceListing.findService(mContext, CONFIG, rule.getOwner());
ComponentName settingsActivity = getSettingsActivity(si); ComponentName settingsActivity = getSettingsActivity(si);
setIntent(getRuleIntent(action, settingsActivity, rule.getName())); setIntent(getRuleIntent(action, settingsActivity, mId));
setWidgetLayoutResource(R.layout.zen_rule_widget); setWidgetLayoutResource(R.layout.zen_rule_widget);
} }
@@ -324,7 +326,7 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
private final View.OnClickListener mDeleteListener = new View.OnClickListener() { private final View.OnClickListener mDeleteListener = new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
showDeleteRuleDialog(mName); showDeleteRuleDialog(mId, mName);
} }
}; };
} }

View File

@@ -55,7 +55,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
protected Context mContext; protected Context mContext;
protected boolean mDisableListeners; protected boolean mDisableListeners;
protected AutomaticZenRule mRule; protected AutomaticZenRule mRule;
protected String mName; protected String mId;
private boolean mDeleting; private boolean mDeleting;
private Preference mRuleName; private Preference mRuleName;
@@ -83,8 +83,8 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
return; return;
} }
mName = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_NAME); mId = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_ID);
if (DEBUG) Log.d(TAG, "mName=" + mName); if (DEBUG) Log.d(TAG, "mId=" + mId);
if (refreshRuleOrFinish()) { if (refreshRuleOrFinish()) {
return; return;
} }
@@ -211,10 +211,11 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
} }
private void showRuleNameDialog() { private void showRuleNameDialog() {
new ZenRuleNameDialog(mContext, null, mRule.getName(), mRules) { new ZenRuleNameDialog(mContext, mRule.getName()) {
@Override @Override
public void onOk(String ruleName) { public void onOk(String ruleName) {
renameZenRule(mRule.getName(), ruleName); mRule.setName(ruleName);
setZenRule(mRule);
} }
}.show(); }.show();
} }
@@ -238,7 +239,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK); MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK);
mDeleting = true; mDeleting = true;
removeZenRule(mRule.getName()); removeZenRule(mRule.getId());
} }
}) })
.show(); .show();
@@ -262,17 +263,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
} }
private AutomaticZenRule getZenRule() { private AutomaticZenRule getZenRule() {
return NotificationManager.from(mContext).getAutomaticZenRule(mName); return NotificationManager.from(mContext).getAutomaticZenRule(mId);
}
private boolean renameZenRule(String oldName, String newName) {
final boolean success =
NotificationManager.from(mContext).renameAutomaticZenRule(oldName, newName);
if (success) {
mName = newName;
}
maybeRefreshRules(success, true);
return success;
} }
private void updateControls() { private void updateControls() {

View File

@@ -87,16 +87,23 @@ abstract public class ZenModeSettingsBase extends RestrictedSettingsFragment {
} }
} }
protected AutomaticZenRule addZenRule(AutomaticZenRule rule) {
final AutomaticZenRule savedRule =
NotificationManager.from(mContext).addAutomaticZenRule(rule);
maybeRefreshRules(savedRule != null, true);
return savedRule;
}
protected boolean setZenRule(AutomaticZenRule rule) { protected boolean setZenRule(AutomaticZenRule rule) {
final boolean success = final boolean success =
NotificationManager.from(mContext).addOrUpdateAutomaticZenRule(rule); NotificationManager.from(mContext).updateAutomaticZenRule(rule);
maybeRefreshRules(success, true); maybeRefreshRules(success, true);
return success; return success;
} }
protected boolean removeZenRule(String name) { protected boolean removeZenRule(String id) {
final boolean success = final boolean success =
NotificationManager.from(mContext).removeAutomaticZenRule(name); NotificationManager.from(mContext).removeAutomaticZenRule(id);
maybeRefreshRules(success, true); maybeRefreshRules(success, true);
return success; return success;
} }

View File

@@ -40,29 +40,17 @@ public abstract class ZenRuleNameDialog {
private final AlertDialog mDialog; private final AlertDialog mDialog;
private final EditText mEditText; private final EditText mEditText;
private final View mWarning;
private final ColorStateList mWarningTint;
private final ColorStateList mOriginalTint;
private final String mOriginalRuleName; private final String mOriginalRuleName;
private final ArraySet<String> mExistingNames;
private final ServiceListing mServiceListing;
private final boolean mIsNew; private final boolean mIsNew;
public ZenRuleNameDialog(Context context, ServiceListing serviceListing, String ruleName, public ZenRuleNameDialog(Context context, String ruleName) {
List<AutomaticZenRule> rules) {
mServiceListing = serviceListing;
mIsNew = ruleName == null; mIsNew = ruleName == null;
mOriginalRuleName = ruleName; mOriginalRuleName = ruleName;
mWarningTint = ColorStateList.valueOf(context.getColor(R.color.zen_rule_name_warning));
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);
mEditText = (EditText) v.findViewById(R.id.rule_name); mEditText = (EditText) v.findViewById(R.id.rule_name);
mWarning = v.findViewById(R.id.rule_name_warning);
if (!mIsNew) { if (!mIsNew) {
mEditText.setText(ruleName); mEditText.setText(ruleName);
} }
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.colorAccent, outValue, true);
mOriginalTint = ColorStateList.valueOf(outValue.data);
mEditText.setSelectAllOnFocus(true); mEditText.setSelectAllOnFocus(true);
mDialog = new AlertDialog.Builder(context) mDialog = new AlertDialog.Builder(context)
@@ -81,52 +69,15 @@ public abstract class ZenRuleNameDialog {
}) })
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.create(); .create();
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// noop
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// noop
}
@Override
public void afterTextChanged(Editable s) {
updatePositiveButtonAndWarning();
}
});
mExistingNames = getAutomaticRuleNames(rules);
} }
abstract public void onOk(String ruleName); abstract public void onOk(String ruleName);
public void show() { public void show() {
mDialog.show(); mDialog.show();
updatePositiveButtonAndWarning();
}
public ArraySet<String> getAutomaticRuleNames(List<AutomaticZenRule> rules) {
final ArraySet<String> rt = new ArraySet<String>(rules.size());
for (int i = 0; i < rules.size(); i++) {
rt.add(rules.get(i).getName().toLowerCase());
}
return rt;
} }
private String trimmedText() { private String trimmedText() {
return mEditText.getText() == null ? null : mEditText.getText().toString().trim(); return mEditText.getText() == null ? null : mEditText.getText().toString().trim();
} }
private void updatePositiveButtonAndWarning() {
final String name = trimmedText();
final boolean validName = !TextUtils.isEmpty(name)
&& (name.equalsIgnoreCase(mOriginalRuleName)
|| !mExistingNames.contains(name.toLowerCase()));
mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(validName);
final boolean showWarning = !TextUtils.isEmpty(name) && !validName;
mWarning.setVisibility(showWarning ? View.VISIBLE : View.INVISIBLE);
mEditText.setBackgroundTintList(showWarning ? mWarningTint : mOriginalTint);
}
} }