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

@@ -18,12 +18,16 @@ package com.android.settings.notification;
import static android.service.notification.ZenModeConfig.ALL_DAYS;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ServiceInfo;
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.ConditionProviderService;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.ScheduleInfo;
import android.service.notification.ZenModeConfig.ZenRule;
@@ -35,30 +39,39 @@ import android.view.MenuItem;
import com.android.internal.logging.MetricsLogger;
import com.android.settings.R;
import com.android.settings.notification.ManagedServiceSettings.Config;
import com.android.settings.notification.ZenRuleNameDialog.RuleInfo;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.TreeSet;
public class ZenModeAutomationSettings extends ZenModeSettingsBase {
private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("EEE");
static final Config CONFIG = getConditionProviderConfig();
private final Calendar mCalendar = Calendar.getInstance();
private ServiceListing mServiceListing;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setHasOptionsMenu(true);
addPreferencesFromResource(R.xml.zen_mode_automation_settings);
mServiceListing = new ServiceListing(mContext, CONFIG);
mServiceListing.addCallback(mServiceListingCallback);
mServiceListing.reload();
mServiceListing.setListening(true);
}
private void showRule(String ruleId, String ruleName) {
if (DEBUG) Log.d(TAG, "showRule " + ruleId + " name=" + ruleName);
mContext.startActivity(new Intent(ZenModeScheduleRuleSettings.ACTION)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.putExtra(ZenModeScheduleRuleSettings.EXTRA_RULE_ID, ruleId));
@Override
public void onDestroy() {
super.onDestroy();
mServiceListing.setListening(false);
mServiceListing.removeCallback(mServiceListingCallback);
}
@Override
@@ -75,29 +88,6 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
return super.onOptionsItemSelected(item);
}
private void showAddRuleDialog() {
new ZenRuleNameDialog(mContext, "", mConfig.getAutomaticRuleNames()) {
@Override
public void onOk(String ruleName) {
final ScheduleInfo schedule = new ScheduleInfo();
schedule.days = ZenModeConfig.ALL_DAYS;
schedule.startHour = 22;
schedule.endHour = 7;
final ZenRule rule = new ZenRule();
rule.name = ruleName;
rule.enabled = true;
rule.zenMode = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
rule.conditionId = ZenModeConfig.toScheduleConditionId(schedule);
final ZenModeConfig newConfig = mConfig.copy();
final String ruleId = newConfig.newRuleId();
newConfig.automaticRules.put(ruleId, rule);
if (setZenModeConfig(newConfig)) {
showRule(ruleId, rule.name);
}
}
}.show();
}
@Override
protected void onZenModeChanged() {
// don't care
@@ -114,15 +104,42 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
updateControls();
}
private void showAddRuleDialog() {
new ZenRuleNameDialog(mContext, mServiceListing, null, mConfig.getAutomaticRuleNames()) {
@Override
public void onOk(String ruleName, RuleInfo ri) {
final ZenRule rule = new ZenRule();
rule.name = ruleName;
rule.enabled = true;
rule.zenMode = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
rule.conditionId = ri.defaultConditionId;
rule.component = ri.serviceComponent;
final ZenModeConfig newConfig = mConfig.copy();
final String ruleId = newConfig.newRuleId();
newConfig.automaticRules.put(ruleId, rule);
if (setZenModeConfig(newConfig)) {
showRule(ri.settingsAction, ri.configurationActivity, ruleId, rule.name);
}
}
}.show();
}
private void showRule(String settingsAction, ComponentName configurationActivity,
String ruleId, String ruleName) {
if (DEBUG) Log.d(TAG, "showRule " + ruleId + " name=" + ruleName);
mContext.startActivity(new Intent(settingsAction)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.putExtra(ZenModeRuleSettingsBase.EXTRA_RULE_ID, ruleId));
}
private void updateControls() {
final PreferenceScreen root = getPreferenceScreen();
root.removeAll();
if (mConfig == null) return;
for (int i = 0; i < mConfig.automaticRules.size(); i++) {
final String id = mConfig.automaticRules.keyAt(i);
final ZenRule rule = mConfig.automaticRules.valueAt(i);
if (!ZenModeConfig.isValidScheduleConditionId(rule.conditionId)) continue;
final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(rule.conditionId);
final Preference p = new Preference(mContext);
p.setTitle(rule.name);
p.setSummary(computeRuleSummary(rule));
@@ -130,7 +147,9 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
p.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
showRule(id, rule.name);
final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION
: ZenModeExternalRuleSettings.ACTION;
showRule(action, null, id, rule.name);
return true;
}
});
@@ -146,13 +165,16 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
private String computeRuleSummary(ZenRule rule) {
if (rule == null || !rule.enabled) return getString(R.string.switch_off_text);
final ScheduleInfo schedule = ZenModeConfig.tryParseScheduleConditionId(rule.conditionId);
if (schedule == null) return getString(R.string.switch_on_text);
final String days = computeContiguousDayRanges(schedule.days);
final String start = getTime(schedule.startHour, schedule.startMinute);
final String end = getTime(schedule.endHour, schedule.endMinute);
final String time = getString(R.string.summary_range_verbal_combination, start, end);
final String mode = ZenModeSettings.computeZenModeCaption(getResources(), rule.zenMode);
return getString(R.string.zen_mode_rule_summary_template, days, time, mode);
String summary = getString(R.string.switch_on_text);
if (schedule != null) {
final String days = computeContiguousDayRanges(schedule.days);
final String start = getTime(schedule.startHour, schedule.startMinute);
final String end = getTime(schedule.endHour, schedule.endMinute);
final String time = getString(R.string.summary_range_verbal_combination, start, end);
summary = getString(R.string.zen_mode_rule_summary_combination, days, time);
}
return getString(R.string.zen_mode_rule_summary_combination, summary, mode);
}
private String getTime(int hour, int minute) {
@@ -199,4 +221,30 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
return DAY_FORMAT.format(mCalendar.getTime());
}
private static Config getConditionProviderConfig() {
final Config c = new Config();
c.tag = TAG;
c.setting = Settings.Secure.ENABLED_CONDITION_PROVIDERS;
c.intentAction = ConditionProviderService.SERVICE_INTERFACE;
c.permission = android.Manifest.permission.BIND_CONDITION_PROVIDER_SERVICE;
c.noun = "condition provider";
return c;
}
private final ServiceListing.Callback mServiceListingCallback = new ServiceListing.Callback() {
@Override
public void onServicesReloaded(List<ServiceInfo> services) {
for (ServiceInfo service : services) {
final RuleInfo ri = ZenModeExternalRuleSettings.getRuleInfo(service);
if (ri != null && ri.serviceComponent != null
&& ri.settingsAction == ZenModeExternalRuleSettings.ACTION) {
if (!mServiceListing.isEnabled(ri.serviceComponent)) {
Log.i(TAG, "Enabling external condition provider: " + ri.serviceComponent);
mServiceListing.setEnabled(ri.serviceComponent, true);
}
}
}
}
};
}