Settings: External automatic rule settings.

- Add external automatic rule settings page with the common
   settings for all rules (enabled, name, zen mode).
 - Pull common rule-instance settings into settings base class, share
   with existing schedule rule settings.
 - New page not searchable since it is at the rule-instance level.
 - Obtain external rule information from existing conditions provider
   metadata.  Includes rule type caption, sub-configuration activity,
   and default condition id.
 - If external condition providers exist with the appropriate metadata,
   display the external rule types as options in the new rule dialog.
   (max of 3 external types)
 - Pull common managed service listing code out of common settings base
   class and into a more reusable helper class.

Bug: 20064962
Change-Id: Ibc13607490b7312a7d9f7f3bd61c3cfcf71a2794
This commit is contained in:
John Spurlock
2015-04-10 11:59:54 -04:00
parent 3b1a4c6cc2
commit c96a5dcbfc
15 changed files with 989 additions and 483 deletions

View File

@@ -25,141 +25,58 @@ import android.app.FragmentManager;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.ScheduleInfo;
import android.service.notification.ZenModeConfig.ZenRule;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Switch;
import android.widget.TimePicker;
import android.widget.Toast;
import com.android.internal.logging.MetricsLogger;
import com.android.settings.DropDownPreference;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.widget.SwitchBar;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
public class ZenModeScheduleRuleSettings extends ZenModeSettingsBase
implements SwitchBar.OnSwitchChangeListener {
private static final String TAG = ZenModeSettingsBase.TAG;
private static final boolean DEBUG = ZenModeSettingsBase.DEBUG;
private static final String KEY_RULE_NAME = "rule_name";
public class ZenModeScheduleRuleSettings extends ZenModeRuleSettingsBase {
private static final String KEY_DAYS = "days";
private static final String KEY_START_TIME = "start_time";
private static final String KEY_END_TIME = "end_time";
private static final String KEY_ZEN_MODE = "zen_mode";
private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("EEE");
public static final String ACTION = Settings.ACTION_ZEN_MODE_SCHEDULE_RULE_SETTINGS;
public static final String EXTRA_RULE_ID = "rule_id";
private Context mContext;
private boolean mDisableListeners;
private SwitchBar mSwitchBar;
private Preference mRuleName;
private Preference mDays;
private TimePickerPreference mStart;
private TimePickerPreference mEnd;
private DropDownPreference mZenMode;
private String mRuleId;
private ZenRule mRule;
private ScheduleInfo mSchedule;
private boolean mDeleting;
@Override
protected void onZenModeChanged() {
// noop
}
@Override
protected void onZenModeConfigChanged() {
if (!refreshRuleOrFinish()) {
updateControls();
}
}
private boolean refreshRuleOrFinish() {
mRule = mConfig.automaticRules.get(mRuleId);
if (DEBUG) Log.d(TAG, "mRule=" + mRule);
mSchedule = mRule != null ? ZenModeConfig.tryParseScheduleConditionId(mRule.conditionId)
protected boolean setRule(ZenRule rule) {
mSchedule = rule != null ? ZenModeConfig.tryParseScheduleConditionId(rule.conditionId)
: null;
if (mSchedule == null) {
toastAndFinish();
return true;
}
return false;
return mSchedule != null;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (DEBUG) Log.d(TAG, "onCreateOptionsMenu");
inflater.inflate(R.menu.zen_mode_rule, menu);
protected String getZenModeDependency() {
return mDays.getKey();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (DEBUG) Log.d(TAG, "onOptionsItemSelected " + item.getItemId());
if (item.getItemId() == R.id.delete) {
showDeleteRuleDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mContext = getActivity();
final Intent intent = getActivity().getIntent();
if (DEBUG) Log.d(TAG, "onCreate getIntent()=" + intent);
if (intent == null) {
Log.w(TAG, "No intent");
toastAndFinish();
return;
}
mRuleId = intent.getStringExtra(EXTRA_RULE_ID);
if (DEBUG) Log.d(TAG, "mRuleId=" + mRuleId);
if (refreshRuleOrFinish()) {
return;
}
protected void onCreateInternal() {
addPreferencesFromResource(R.xml.zen_mode_schedule_rule_settings);
final PreferenceScreen root = getPreferenceScreen();
setHasOptionsMenu(true);
mRuleName = root.findPreference(KEY_RULE_NAME);
mRuleName.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
showRuleNameDialog();
return true;
}
});
mDays = root.findPreference(KEY_DAYS);
mDays.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
@@ -186,10 +103,7 @@ public class ZenModeScheduleRuleSettings extends ZenModeSettingsBase
if (DEBUG) Log.d(TAG, "onPrefChange start h=" + hour + " m=" + minute);
mSchedule.startHour = hour;
mSchedule.startMinute = minute;
mRule.conditionId = ZenModeConfig.toScheduleConditionId(mSchedule);
mRule.condition = null;
mRule.snoozing = false;
setZenModeConfig(mConfig);
updateRule(ZenModeConfig.toScheduleConditionId(mSchedule));
return true;
}
});
@@ -211,63 +125,12 @@ public class ZenModeScheduleRuleSettings extends ZenModeSettingsBase
if (DEBUG) Log.d(TAG, "onPrefChange end h=" + hour + " m=" + minute);
mSchedule.endHour = hour;
mSchedule.endMinute = minute;
mRule.conditionId = ZenModeConfig.toScheduleConditionId(mSchedule);
mRule.condition = null;
mRule.snoozing = false;
setZenModeConfig(mConfig);
updateRule(ZenModeConfig.toScheduleConditionId(mSchedule));
return true;
}
});
root.addPreference(mEnd);
mEnd.setDependency(mDays.getKey());
mZenMode = (DropDownPreference) root.findPreference(KEY_ZEN_MODE);
mZenMode.addItem(R.string.zen_mode_option_important_interruptions, Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
mZenMode.addItem(R.string.zen_mode_option_alarms, Global.ZEN_MODE_ALARMS);
mZenMode.addItem(R.string.zen_mode_option_no_interruptions, Global.ZEN_MODE_NO_INTERRUPTIONS);
mZenMode.setCallback(new DropDownPreference.Callback() {
@Override
public boolean onItemSelected(int pos, Object value) {
if (mDisableListeners) return true;
final int zenMode = (Integer) value;
if (zenMode == mRule.zenMode) return true;
if (DEBUG) Log.d(TAG, "onPrefChange zenMode=" + zenMode);
mRule.zenMode = zenMode;
setZenModeConfig(mConfig);
return true;
}
});
mZenMode.setOrder(10); // sort at the bottom of the category
mZenMode.setDependency(mDays.getKey());
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
mSwitchBar.addOnSwitchChangeListener(this);
mSwitchBar.show();
}
@Override
public void onDestroyView() {
super.onDestroyView();
mSwitchBar.removeOnSwitchChangeListener(this);
mSwitchBar.hide();
}
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
if (DEBUG) Log.d(TAG, "onSwitchChanged " + isChecked);
if (mDisableListeners) return;
final boolean enabled = isChecked;
if (enabled == mRule.enabled) return;
if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled);
mRule.enabled = enabled;
mRule.snoozing = false;
setZenModeConfig(mConfig);
}
private void updateDays() {
@@ -308,28 +171,11 @@ public class ZenModeScheduleRuleSettings extends ZenModeSettingsBase
}
@Override
public void onResume() {
super.onResume();
updateControls();
}
private void updateRuleName() {
getActivity().setTitle(mRule.name);
mRuleName.setSummary(mRule.name);
}
private void updateControls() {
mDisableListeners = true;
updateRuleName();
protected void updateControlsInternal() {
updateDays();
mStart.setTime(mSchedule.startHour, mSchedule.startMinute);
mEnd.setTime(mSchedule.endHour, mSchedule.endMinute);
mZenMode.setSelectedValue(mRule.zenMode);
mDisableListeners = false;
updateEndSummary();
if (mSwitchBar != null) {
mSwitchBar.setChecked(mRule.enabled);
}
}
@Override
@@ -337,34 +183,6 @@ public class ZenModeScheduleRuleSettings extends ZenModeSettingsBase
return MetricsLogger.NOTIFICATION_ZEN_MODE_SCHEDULE_RULE;
}
private void showDeleteRuleDialog() {
new AlertDialog.Builder(mContext)
.setMessage(getString(R.string.zen_mode_delete_rule_confirmation, mRule.name))
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.zen_mode_delete_rule_button, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mDeleting = true;
mConfig.automaticRules.remove(mRuleId);
setZenModeConfig(mConfig);
}
})
.show();
}
private void showRuleNameDialog() {
new ZenRuleNameDialog(mContext, mRule.name, mConfig.getAutomaticRuleNames()) {
@Override
public void onOk(String ruleName) {
final ZenModeConfig newConfig = mConfig.copy();
final ZenRule rule = newConfig.automaticRules.get(mRuleId);
if (rule == null) return;
rule.name = ruleName;
setZenModeConfig(newConfig);
}
}.show();
}
private void showDaysDialog() {
new AlertDialog.Builder(mContext)
.setTitle(R.string.zen_mode_schedule_rule_days)
@@ -375,10 +193,7 @@ public class ZenModeScheduleRuleSettings extends ZenModeSettingsBase
if (Arrays.equals(days, mSchedule.days)) return;
if (DEBUG) Log.d(TAG, "days.onChanged days=" + Arrays.asList(days));
mSchedule.days = days;
mRule.conditionId = ZenModeConfig.toScheduleConditionId(mSchedule);
mRule.condition = null;
mRule.snoozing = false;
setZenModeConfig(mConfig);
updateRule(ZenModeConfig.toScheduleConditionId(mSchedule));
}
})
.setOnDismissListener(new OnDismissListener() {
@@ -391,14 +206,6 @@ public class ZenModeScheduleRuleSettings extends ZenModeSettingsBase
.show();
}
private void toastAndFinish() {
if (!mDeleting) {
Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT)
.show();
}
getActivity().finish();
}
private static class TimePickerPreference extends Preference {
private final Context mContext;
@@ -474,4 +281,5 @@ public class ZenModeScheduleRuleSettings extends ZenModeSettingsBase
boolean onSetTime(int hour, int minute);
}
}
}