}
private void showNameRuleDialog(final ZenRuleInfo ri) {
- new ZenRuleNameDialog(mContext, mServiceListing, null, mRules) {
+ new ZenRuleNameDialog(mContext, null) {
@Override
public void onOk(String ruleName) {
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ADD_RULE_OK);
AutomaticZenRule rule = new AutomaticZenRule(ruleName, ri.serviceComponent,
ri.defaultConditionId, NotificationManager.INTERRUPTION_FILTER_PRIORITY,
true);
- if (setZenRule(rule)) {
- startActivity(getRuleIntent(ri.settingsAction, null, rule.getName()));
+ AutomaticZenRule savedRule = addZenRule(rule);
+ if (savedRule != null) {
+ startActivity(getRuleIntent(ri.settingsAction, null, savedRule.getId()));
}
}
}.show();
}
- private void showDeleteRuleDialog(final String ruleName) {
+ private void showDeleteRuleDialog(final String ruleId, final String ruleName) {
new AlertDialog.Builder(mContext)
.setMessage(getString(R.string.zen_mode_delete_rule_confirmation, ruleName))
.setNegativeButton(R.string.cancel, null)
@Override
public void onClick(DialogInterface dialog, int which) {
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK);
- removeZenRule(ruleName);
+ removeZenRule(ruleId);
}
})
.show();
}
private Intent getRuleIntent(String settingsAction, ComponentName configurationActivity,
- String ruleName) {
+ String ruleId) {
Intent intent = new Intent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
- .putExtra(ConditionProviderService.EXTRA_RULE_NAME, ruleName);
+ .putExtra(ConditionProviderService.EXTRA_RULE_ID, ruleId);
if (configurationActivity != null) {
intent.setComponent(configurationActivity);
} else {
public static ZenRuleInfo getRuleInfo(ServiceInfo si) {
if (si == null || si.metaData == null) return null;
final String ruleType = si.metaData.getString(ConditionProviderService.META_DATA_RULE_TYPE);
- final String defaultConditionId =
- si.metaData.getString(ConditionProviderService.META_DATA_DEFAULT_CONDITION_ID);
- if (ruleType != null && !ruleType.trim().isEmpty() && defaultConditionId != null) {
+ final ComponentName configurationActivity = getSettingsActivity(si);
+ if (ruleType != null && !ruleType.trim().isEmpty() && configurationActivity != null) {
final ZenRuleInfo ri = new ZenRuleInfo();
ri.settingsAction = Settings.ACTION_ZEN_MODE_EXTERNAL_RULE_SETTINGS;
ri.title = ruleType;
private class ZenRulePreference extends Preference {
final String mName;
+ final String mId;
public ZenRulePreference(Context context, final AutomaticZenRule rule) {
super(context);
mName = rule.getName();
+ mId = rule.getId();
final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
rule.getConditionId());
: isEvent ? ZenModeEventRuleSettings.ACTION : "";
ServiceInfo si = mServiceListing.findService(mContext, CONFIG, rule.getOwner());
ComponentName settingsActivity = getSettingsActivity(si);
- setIntent(getRuleIntent(action, settingsActivity, rule.getName()));
+ setIntent(getRuleIntent(action, settingsActivity, mId));
setWidgetLayoutResource(R.layout.zen_rule_widget);
}
private final View.OnClickListener mDeleteListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
- showDeleteRuleDialog(mName);
+ showDeleteRuleDialog(mId, mName);
}
};
}
protected Context mContext;
protected boolean mDisableListeners;
protected AutomaticZenRule mRule;
- protected String mName;
+ protected String mId;
private boolean mDeleting;
private Preference mRuleName;
return;
}
- mName = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_NAME);
- if (DEBUG) Log.d(TAG, "mName=" + mName);
+ mId = intent.getStringExtra(ConditionProviderService.EXTRA_RULE_ID);
+ if (DEBUG) Log.d(TAG, "mId=" + mId);
if (refreshRuleOrFinish()) {
return;
}
}
private void showRuleNameDialog() {
- new ZenRuleNameDialog(mContext, null, mRule.getName(), mRules) {
+ new ZenRuleNameDialog(mContext, mRule.getName()) {
@Override
public void onOk(String ruleName) {
- renameZenRule(mRule.getName(), ruleName);
+ mRule.setName(ruleName);
+ setZenRule(mRule);
}
}.show();
}
public void onClick(DialogInterface dialog, int which) {
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_DELETE_RULE_OK);
mDeleting = true;
- removeZenRule(mRule.getName());
+ removeZenRule(mRule.getId());
}
})
.show();
}
private AutomaticZenRule getZenRule() {
- return NotificationManager.from(mContext).getAutomaticZenRule(mName);
- }
-
- 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;
+ return NotificationManager.from(mContext).getAutomaticZenRule(mId);
}
private void updateControls() {
}
}
+ protected AutomaticZenRule addZenRule(AutomaticZenRule rule) {
+ final AutomaticZenRule savedRule =
+ NotificationManager.from(mContext).addAutomaticZenRule(rule);
+ maybeRefreshRules(savedRule != null, true);
+ return savedRule;
+ }
+
protected boolean setZenRule(AutomaticZenRule rule) {
final boolean success =
- NotificationManager.from(mContext).addOrUpdateAutomaticZenRule(rule);
+ NotificationManager.from(mContext).updateAutomaticZenRule(rule);
maybeRefreshRules(success, true);
return success;
}
- protected boolean removeZenRule(String name) {
+ protected boolean removeZenRule(String id) {
final boolean success =
- NotificationManager.from(mContext).removeAutomaticZenRule(name);
+ NotificationManager.from(mContext).removeAutomaticZenRule(id);
maybeRefreshRules(success, true);
return success;
}
private final AlertDialog mDialog;
private final EditText mEditText;
- private final View mWarning;
- private final ColorStateList mWarningTint;
- private final ColorStateList mOriginalTint;
private final String mOriginalRuleName;
- private final ArraySet<String> mExistingNames;
- private final ServiceListing mServiceListing;
private final boolean mIsNew;
- public ZenRuleNameDialog(Context context, ServiceListing serviceListing, String ruleName,
- List<AutomaticZenRule> rules) {
- mServiceListing = serviceListing;
+ public ZenRuleNameDialog(Context context, String ruleName) {
mIsNew = ruleName == null;
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);
mEditText = (EditText) v.findViewById(R.id.rule_name);
- mWarning = v.findViewById(R.id.rule_name_warning);
if (!mIsNew) {
mEditText.setText(ruleName);
}
- TypedValue outValue = new TypedValue();
- context.getTheme().resolveAttribute(android.R.attr.colorAccent, outValue, true);
- mOriginalTint = ColorStateList.valueOf(outValue.data);
mEditText.setSelectAllOnFocus(true);
mDialog = new AlertDialog.Builder(context)
})
.setNegativeButton(R.string.cancel, null)
.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);
public void 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() {
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);
- }
-}
\ No newline at end of file
+}