Updates to automatic rule pages in Settings
- Re-added metrics for zen behavior preference controllers - Dialogs in zen mode settings are rotate-friendly - Automatic rules are refreshed on update state - User-created (and default) automatic rules are always priority only and user cannot change this - Automatic rules redesigned to have headers Test: make ROBOTEST_FILTER=ZenModeAutomaticRulesPreferenceControllerTest RunSettingsRoboTests -j40 Bug: 63077372 Fixes: 68324465 Fixes: 69057696 Change-Id: I163acef2715dd4e60bfc08207f0e22352c4c0e28
This commit is contained in:
@@ -16,62 +16,43 @@
|
||||
|
||||
package com.android.settings.notification;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.AutomaticZenRule;
|
||||
import android.app.Fragment;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.service.notification.ConditionProviderService;
|
||||
import android.support.v7.preference.DropDownPreference;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.Preference.OnPreferenceChangeListener;
|
||||
import android.support.v7.preference.Preference.OnPreferenceClickListener;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.Switch;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.widget.SwitchBar;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import java.util.List;
|
||||
public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase {
|
||||
|
||||
public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
||||
implements SwitchBar.OnSwitchChangeListener {
|
||||
protected static final String TAG = ZenModeSettingsBase.TAG;
|
||||
protected static final boolean DEBUG = ZenModeSettingsBase.DEBUG;
|
||||
|
||||
private static final String KEY_RULE_NAME = "rule_name";
|
||||
private static final String KEY_ZEN_MODE = "zen_mode";
|
||||
|
||||
protected Context mContext;
|
||||
protected boolean mDisableListeners;
|
||||
protected AutomaticZenRule mRule;
|
||||
protected String mId;
|
||||
|
||||
private boolean mDeleting;
|
||||
private Preference mRuleName;
|
||||
private SwitchBar mSwitchBar;
|
||||
private DropDownPreference mZenMode;
|
||||
private Toast mEnabledToast;
|
||||
protected ZenAutomaticRuleHeaderPreferenceController mHeader;
|
||||
protected ZenAutomaticRuleSwitchPreferenceController mSwitch;
|
||||
|
||||
abstract protected void onCreateInternal();
|
||||
abstract protected boolean setRule(AutomaticZenRule rule);
|
||||
abstract protected String getZenModeDependency();
|
||||
abstract protected void updateControlsInternal();
|
||||
abstract protected int getEnabledToastText();
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
@@ -99,8 +80,6 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
||||
|
||||
super.onCreate(icicle);
|
||||
|
||||
setHasOptionsMenu(true);
|
||||
|
||||
onCreateInternal();
|
||||
|
||||
final PreferenceScreen root = getPreferenceScreen();
|
||||
@@ -112,37 +91,6 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
mZenMode = (DropDownPreference) root.findPreference(KEY_ZEN_MODE);
|
||||
mZenMode.setEntries(new CharSequence[] {
|
||||
getString(R.string.zen_mode_option_important_interruptions),
|
||||
getString(R.string.zen_mode_option_alarms),
|
||||
getString(R.string.zen_mode_option_no_interruptions),
|
||||
});
|
||||
mZenMode.setEntryValues(new CharSequence[] {
|
||||
Integer.toString(NotificationManager.INTERRUPTION_FILTER_PRIORITY),
|
||||
Integer.toString(NotificationManager.INTERRUPTION_FILTER_ALARMS),
|
||||
Integer.toString(NotificationManager.INTERRUPTION_FILTER_NONE),
|
||||
});
|
||||
mZenMode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if (mDisableListeners) return false;
|
||||
final int zenMode = Integer.parseInt((String) newValue);
|
||||
if (zenMode == mRule.getInterruptionFilter()) return false;
|
||||
if (DEBUG) Log.d(TAG, "onPrefChange zenMode=" + zenMode);
|
||||
mRule.setInterruptionFilter(zenMode);
|
||||
mBackend.setZenRule(mId, mRule);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
mZenMode.setOrder(10); // sort at the bottom of the category
|
||||
mZenMode.setDependency(getZenModeDependency());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -155,43 +103,39 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
||||
final SettingsActivity activity = (SettingsActivity) getActivity();
|
||||
mSwitchBar = activity.getSwitchBar();
|
||||
mSwitchBar.addOnSwitchChangeListener(this);
|
||||
mSwitchBar.show();
|
||||
public int getHelpResource() {
|
||||
return R.string.help_uri_interruptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
mSwitchBar.removeOnSwitchChangeListener(this);
|
||||
mSwitchBar.hide();
|
||||
/**
|
||||
* Update state of header preference managed by PreferenceController.
|
||||
*/
|
||||
protected void updateHeader() {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
|
||||
mSwitch.onResume(mRule,mId);
|
||||
mSwitch.displayPreference(screen);
|
||||
updatePreference(mSwitch);
|
||||
|
||||
mHeader.onResume(mRule);
|
||||
mHeader.displayPreference(screen);
|
||||
updatePreference(mHeader);
|
||||
}
|
||||
|
||||
@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.isEnabled()) return;
|
||||
mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_ENABLE_RULE, enabled);
|
||||
if (DEBUG) Log.d(TAG, "onSwitchChanged enabled=" + enabled);
|
||||
mRule.setEnabled(enabled);
|
||||
mBackend.setZenRule(mId, mRule);
|
||||
if (enabled) {
|
||||
final int toastText = getEnabledToastText();
|
||||
if (toastText != 0) {
|
||||
mEnabledToast = Toast.makeText(mContext, toastText, Toast.LENGTH_SHORT);
|
||||
mEnabledToast.show();
|
||||
}
|
||||
} else {
|
||||
if (mEnabledToast != null) {
|
||||
mEnabledToast.cancel();
|
||||
}
|
||||
private void updatePreference(AbstractPreferenceController controller) {
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
if (!controller.isAvailable()) {
|
||||
return;
|
||||
}
|
||||
final String key = controller.getPreferenceKey();
|
||||
|
||||
final Preference preference = screen.findPreference(key);
|
||||
if (preference == null) {
|
||||
Log.d(TAG, String.format("Cannot find preference with key %s in Controller %s",
|
||||
key, controller.getClass().getSimpleName()));
|
||||
return;
|
||||
}
|
||||
controller.updateState(preference);
|
||||
}
|
||||
|
||||
protected void updateRule(Uri newConditionId) {
|
||||
@@ -207,33 +151,6 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
if (DEBUG) Log.d(TAG, "onCreateOptionsMenu");
|
||||
inflater.inflate(R.menu.zen_mode_rule, menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (DEBUG) Log.d(TAG, "onOptionsItemSelected " + item.getItemId());
|
||||
if (item.getItemId() == R.id.delete) {
|
||||
mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_ZEN_DELETE_RULE);
|
||||
showDeleteRuleDialog();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
private void showRuleNameDialog() {
|
||||
new ZenRuleNameDialog(mContext, mRule.getName(), null) {
|
||||
@Override
|
||||
public void onOk(String ruleName) {
|
||||
mRule.setName(ruleName);
|
||||
mBackend.setZenRule(mId, mRule);
|
||||
}
|
||||
}.show();
|
||||
}
|
||||
|
||||
private boolean refreshRuleOrFinish() {
|
||||
mRule = getZenRule();
|
||||
if (DEBUG) Log.d(TAG, "mRule=" + mRule);
|
||||
@@ -244,42 +161,22 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
||||
return false;
|
||||
}
|
||||
|
||||
private void showDeleteRuleDialog() {
|
||||
final AlertDialog dialog = new AlertDialog.Builder(mContext)
|
||||
.setMessage(getString(R.string.zen_mode_delete_rule_confirmation, mRule.getName()))
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.setPositiveButton(R.string.zen_mode_delete_rule_button, new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
mMetricsFeatureProvider.action(mContext,
|
||||
MetricsEvent.ACTION_ZEN_DELETE_RULE_OK);
|
||||
mDeleting = true;
|
||||
mBackend.removeZenRule(mId);
|
||||
}
|
||||
})
|
||||
.show();
|
||||
final View messageView = dialog.findViewById(android.R.id.message);
|
||||
if (messageView != null) {
|
||||
messageView.setTextDirection(View.TEXT_DIRECTION_LOCALE);
|
||||
}
|
||||
private void showRuleNameDialog() {
|
||||
ZenRuleNameDialog.show(this, mRule.getName(), null, new RuleNameChangeListener());
|
||||
}
|
||||
|
||||
private void toastAndFinish() {
|
||||
if (!mDeleting) {
|
||||
Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT)
|
||||
Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
}
|
||||
getActivity().finish();
|
||||
}
|
||||
|
||||
private void updateRuleName() {
|
||||
Activity activity = getActivity();
|
||||
if (activity != null) {
|
||||
activity.setTitle(mRule.getName());
|
||||
if (mRule != null) {
|
||||
mRuleName.setSummary(mRule.getName());
|
||||
} else {
|
||||
if (DEBUG) Log.d(TAG, "updateRuleName - activity title and mRuleName "
|
||||
+ "not updated; getActivity() returned null");
|
||||
if (DEBUG) Log.d(TAG, "updateRuleName - mRuleName "
|
||||
+ "not updated; mRuleName returned null");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,10 +188,19 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase
|
||||
mDisableListeners = true;
|
||||
updateRuleName();
|
||||
updateControlsInternal();
|
||||
mZenMode.setValue(Integer.toString(mRule.getInterruptionFilter()));
|
||||
if (mSwitchBar != null) {
|
||||
mSwitchBar.setChecked(mRule.isEnabled());
|
||||
}
|
||||
updateHeader();
|
||||
mDisableListeners = false;
|
||||
}
|
||||
|
||||
public class RuleNameChangeListener implements ZenRuleNameDialog.PositiveClickListener {
|
||||
public RuleNameChangeListener() {}
|
||||
|
||||
@Override
|
||||
public void onOk(String ruleName, Fragment parent) {
|
||||
mMetricsFeatureProvider.action(mContext,
|
||||
MetricsProto.MetricsEvent.ACTION_ZEN_MODE_RULE_NAME_CHANGE_OK);
|
||||
mRule.setName(ruleName);
|
||||
mBackend.setZenRule(mId, mRule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user