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:
@@ -17,35 +17,71 @@
|
||||
package com.android.settings.notification;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnDismissListener;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.net.Uri;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
import android.service.notification.ZenModeConfig.ScheduleInfo;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.ArraySet;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.RadioGroup;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class ZenRuleNameDialog {
|
||||
private static final String TAG = ZenModeSettings.TAG;
|
||||
private static final boolean DEBUG = ZenModeSettings.DEBUG;
|
||||
|
||||
private final AlertDialog mDialog;
|
||||
private final EditText mEditText;
|
||||
private final RadioGroup mTypes;
|
||||
private final ArraySet<String> mExistingNames;
|
||||
private final ServiceListing mServiceListing;
|
||||
private final RuleInfo[] mExternalRules = new RuleInfo[3];
|
||||
|
||||
public ZenRuleNameDialog(Context context, String ruleName, ArraySet<String> existingNames) {
|
||||
public ZenRuleNameDialog(Context context, ServiceListing serviceListing, String ruleName,
|
||||
ArraySet<String> existingNames) {
|
||||
mServiceListing = serviceListing;
|
||||
final View v = LayoutInflater.from(context).inflate(R.layout.zen_rule_name, null, false);
|
||||
mEditText = (EditText) v.findViewById(R.id.rule_name);
|
||||
mEditText.setText(ruleName);
|
||||
if (ruleName != null) {
|
||||
mEditText.setText(ruleName);
|
||||
}
|
||||
mEditText.setSelectAllOnFocus(true);
|
||||
mTypes = (RadioGroup) v.findViewById(R.id.rule_types);
|
||||
if (mServiceListing != null) {
|
||||
bindType(R.id.rule_type_schedule, defaultNewSchedule());
|
||||
bindExternalRules();
|
||||
mServiceListing.addCallback(mServiceListingCallback);
|
||||
mServiceListing.reload();
|
||||
}
|
||||
mDialog = new AlertDialog.Builder(context)
|
||||
.setTitle(R.string.zen_mode_rule_name)
|
||||
.setView(v)
|
||||
.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
onOk(trimmedText());
|
||||
onOk(trimmedText(), selectedRuleInfo());
|
||||
}
|
||||
})
|
||||
.setOnDismissListener(new OnDismissListener() {
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
if (mServiceListing != null) {
|
||||
mServiceListing.removeCallback(mServiceListingCallback);
|
||||
}
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
@@ -72,17 +108,37 @@ public abstract class ZenRuleNameDialog {
|
||||
}
|
||||
}
|
||||
|
||||
abstract public void onOk(String ruleName);
|
||||
|
||||
private String trimmedText() {
|
||||
return mEditText.getText() == null ? null : mEditText.getText().toString().trim();
|
||||
}
|
||||
abstract public void onOk(String ruleName, RuleInfo ruleInfo);
|
||||
|
||||
public void show() {
|
||||
mDialog.show();
|
||||
updatePositiveButton();
|
||||
}
|
||||
|
||||
private void bindType(int id, RuleInfo ri) {
|
||||
final RadioButton rb = (RadioButton) mTypes.findViewById(id);
|
||||
if (ri == null) {
|
||||
rb.setVisibility(View.GONE);
|
||||
return;
|
||||
}
|
||||
rb.setVisibility(View.VISIBLE);
|
||||
if (ri.caption != null) {
|
||||
rb.setText(ri.caption);
|
||||
}
|
||||
rb.setTag(ri);
|
||||
}
|
||||
|
||||
private RuleInfo selectedRuleInfo() {
|
||||
final int id = mTypes.getCheckedRadioButtonId();
|
||||
if (id == -1) return null;
|
||||
final RadioButton rb = (RadioButton) mTypes.findViewById(id);
|
||||
return (RuleInfo) rb.getTag();
|
||||
}
|
||||
|
||||
private String trimmedText() {
|
||||
return mEditText.getText() == null ? null : mEditText.getText().toString().trim();
|
||||
}
|
||||
|
||||
private void updatePositiveButton() {
|
||||
final String name = trimmedText();
|
||||
final boolean validName = !TextUtils.isEmpty(name)
|
||||
@@ -90,4 +146,51 @@ public abstract class ZenRuleNameDialog {
|
||||
mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(validName);
|
||||
}
|
||||
|
||||
private static RuleInfo defaultNewSchedule() {
|
||||
final ScheduleInfo schedule = new ScheduleInfo();
|
||||
schedule.days = ZenModeConfig.ALL_DAYS;
|
||||
schedule.startHour = 22;
|
||||
schedule.endHour = 7;
|
||||
final RuleInfo rt = new RuleInfo();
|
||||
rt.settingsAction = ZenModeScheduleRuleSettings.ACTION;
|
||||
rt.defaultConditionId = ZenModeConfig.toScheduleConditionId(schedule);
|
||||
return rt;
|
||||
}
|
||||
|
||||
private void bindExternalRules() {
|
||||
bindType(R.id.rule_type_2, mExternalRules[0]);
|
||||
bindType(R.id.rule_type_3, mExternalRules[1]);
|
||||
bindType(R.id.rule_type_4, mExternalRules[2]);
|
||||
// show radio group if we have at least one external rule type
|
||||
mTypes.setVisibility(mExternalRules[0] != null ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
|
||||
private final ServiceListing.Callback mServiceListingCallback = new ServiceListing.Callback() {
|
||||
@Override
|
||||
public void onServicesReloaded(List<ServiceInfo> services) {
|
||||
if (DEBUG) Log.d(TAG, "Services reloaded: count=" + services.size());
|
||||
mExternalRules[0] = mExternalRules[1] = mExternalRules[2] = null;
|
||||
int i = 0;
|
||||
for (ServiceInfo si : services) {
|
||||
final RuleInfo ri = ZenModeExternalRuleSettings.getRuleInfo(si);
|
||||
if (ri != null) {
|
||||
mExternalRules[i] = ri;
|
||||
i++;
|
||||
if (i == mExternalRules.length) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
bindExternalRules();
|
||||
}
|
||||
};
|
||||
|
||||
public static class RuleInfo {
|
||||
public String caption;
|
||||
public String settingsAction;
|
||||
public Uri defaultConditionId;
|
||||
public ComponentName serviceComponent;
|
||||
public ComponentName configurationActivity;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user