<!-- Do not disturb: Summary for the zen mode automation option Suggestion. [CHAR LIMIT=NONE] -->
<string name="zen_mode_automation_suggestion_summary">Limit sounds & vibrations at certain times</string>
+ <!-- Do not disturb: Switch toggle to toggle whether to use an automatic dnd rule or not [CHAR LIMIT=40] -->
+ <string name="zen_mode_use_automatic_rule">Use rule</string>
+
<!-- Do not disturb: Zen mode option: Important interruptions [CHAR LIMIT=60] -->
<string name="zen_mode_option_important_interruptions">Priority only</string>
android:key="zen_automatic_rule_switch"
android:layout="@layout/styled_switch_bar" />
- <!-- Rule name -->
- <Preference
- android:key="rule_name"
- android:title="@string/zen_mode_rule_name"
- android:persistent="false" />
-
<!-- During events for -->
<DropDownPreference
android:key="calendar"
android:key="zen_automatic_rule_switch"
android:layout="@layout/styled_switch_bar" />
- <!-- Rule name -->
- <Preference
- android:key="rule_name"
- android:title="@string/zen_mode_rule_name"
- android:persistent="false" />
-
<!-- Days -->
<Preference
android:key="days"
import static com.android.settings.widget.EntityHeaderController.PREF_KEY_APP_HEADER;
import android.app.AutomaticZenRule;
+import android.app.Fragment;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Slog;
import android.view.View;
+import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.core.PreferenceControllerMixin;
private final String KEY = PREF_KEY_APP_HEADER;
private final PreferenceFragment mFragment;
private AutomaticZenRule mRule;
+ private String mId;
private EntityHeaderController mController;
public ZenAutomaticRuleHeaderPreferenceController(Context context, PreferenceFragment fragment,
mController = EntityHeaderController
.newInstance(mFragment.getActivity(), mFragment,
pref.findViewById(R.id.entity_header));
+
+ mController.setEditZenRuleNameListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ ZenRuleNameDialog.show(mFragment, mRule.getName(), null,
+ new RuleNameChangeListener());
+ }
+ });
}
pref = mController.setIcon(getIcon())
.setPackageName(mRule.getOwner().getPackageName())
.setUid(mContext.getUserId())
.setHasAppInfoLink(false)
- .setButtonActions(EntityHeaderController.ActionType.ACTION_NONE,
+ .setButtonActions(EntityHeaderController.ActionType.ACTION_DND_RULE_PREFERENCE,
EntityHeaderController.ActionType.ACTION_NONE)
.done(mFragment.getActivity(), mContext);
return null;
}
- protected void onResume(AutomaticZenRule rule) {
+ protected void onResume(AutomaticZenRule rule, String id) {
mRule = rule;
+ mId = id;
+ }
+
+ 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);
+ }
}
}
import android.app.Fragment;
import android.content.Context;
import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
import android.widget.Switch;
import android.widget.Toast;
private String mId;
private Toast mEnabledToast;
private int mToastTextResource;
+ private SwitchBar mSwitchBar;
public ZenAutomaticRuleSwitchPreferenceController(Context context, Fragment parent,
int toastTextResource, Lifecycle lifecycle) {
return mRule != null && mId != null;
}
+ @Override
+ public void displayPreference(PreferenceScreen screen) {
+ super.displayPreference(screen);
+ LayoutPreference pref = (LayoutPreference) screen.findPreference(KEY);
+ mSwitchBar = pref.findViewById(R.id.switch_bar);
+
+ if (mSwitchBar != null) {
+ mSwitchBar.setSwitchBarText(R.string.zen_mode_use_automatic_rule,
+ R.string.zen_mode_use_automatic_rule);
+ try {
+ mSwitchBar.addOnSwitchChangeListener(this);
+ } catch (IllegalStateException e) {
+ // an exception is thrown if you try to add the listener twice
+ }
+ mSwitchBar.show();
+ }
+ }
+
+
public void onResume(AutomaticZenRule rule, String id) {
mRule = rule;
mId = id;
}
public void updateState(Preference preference) {
- LayoutPreference pref = (LayoutPreference) preference;
- SwitchBar bar = pref.findViewById(R.id.switch_bar);
if (mRule != null) {
- bar.setChecked(mRule.isEnabled());
- }
- if (bar != null) {
- bar.show();
- try {
- bar.addOnSwitchChangeListener(this);
- } catch (IllegalStateException e) {
- // an exception is thrown if you try to add the listener twice
- }
+ mSwitchBar.setChecked(mRule.isEnabled());
}
- bar.show();
}
@Override
savePolicy(getNewPriorityCategories(allowSenders, category),
priorityCallSenders, priorityMessagesSenders, mPolicy.suppressedVisualEffects);
- if (ZenModeSettingsBase.DEBUG) Log.d(TAG, "onPrefChange allow=" +
- stringCategory + allowSenders + " allow" + stringCategory + "From="
+ if (ZenModeSettingsBase.DEBUG) Log.d(TAG, "onPrefChange allow" +
+ stringCategory + "=" + allowSenders + " allow" + stringCategory + "From="
+ ZenModeConfig.sourceToString(allowSendersFrom));
}
package com.android.settings.notification;
import android.app.AutomaticZenRule;
-import android.app.Fragment;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.service.notification.ConditionProviderService;
import android.support.v7.preference.Preference;
-import android.support.v7.preference.Preference.OnPreferenceClickListener;
import android.support.v7.preference.PreferenceScreen;
import android.util.Log;
import android.widget.Toast;
-import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settingslib.core.AbstractPreferenceController;
protected static final String TAG = ZenModeSettingsBase.TAG;
protected static final boolean DEBUG = ZenModeSettingsBase.DEBUG;
- private static final String KEY_RULE_NAME = "rule_name";
-
protected Context mContext;
protected boolean mDisableListeners;
protected AutomaticZenRule mRule;
protected String mId;
- private Preference mRuleName;
protected ZenAutomaticRuleHeaderPreferenceController mHeader;
protected ZenAutomaticRuleSwitchPreferenceController mSwitch;
}
super.onCreate(icicle);
-
onCreateInternal();
-
- final PreferenceScreen root = getPreferenceScreen();
- mRuleName = root.findPreference(KEY_RULE_NAME);
- mRuleName.setOnPreferenceClickListener(new OnPreferenceClickListener() {
- @Override
- public boolean onPreferenceClick(Preference preference) {
- showRuleNameDialog();
- return true;
- }
- });
}
@Override
protected void updateHeader() {
final PreferenceScreen screen = getPreferenceScreen();
- mSwitch.onResume(mRule,mId);
+ mSwitch.onResume(mRule, mId);
mSwitch.displayPreference(screen);
updatePreference(mSwitch);
- mHeader.onResume(mRule);
+ mHeader.onResume(mRule, mId);
mHeader.displayPreference(screen);
updatePreference(mHeader);
}
return false;
}
- private void showRuleNameDialog() {
- ZenRuleNameDialog.show(this, mRule.getName(), null, new RuleNameChangeListener());
- }
-
private void toastAndFinish() {
Toast.makeText(mContext, R.string.zen_mode_rule_not_found_text, Toast.LENGTH_SHORT)
.show();
getActivity().finish();
}
- private void updateRuleName() {
- if (mRule != null) {
- mRuleName.setSummary(mRule.getName());
- } else {
- if (DEBUG) Log.d(TAG, "updateRuleName - mRuleName "
- + "not updated; mRuleName returned null");
- }
- }
-
private AutomaticZenRule getZenRule() {
return NotificationManager.from(mContext).getAutomaticZenRule(mId);
}
private void updateControls() {
mDisableListeners = true;
- updateRuleName();
updateControlsInternal();
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);
- }
- }
}
@IntDef({ActionType.ACTION_NONE,
ActionType.ACTION_APP_PREFERENCE,
- ActionType.ACTION_NOTIF_PREFERENCE})
+ ActionType.ACTION_NOTIF_PREFERENCE,
+ ActionType.ACTION_DND_RULE_PREFERENCE,})
@Retention(RetentionPolicy.SOURCE)
public @interface ActionType {
int ACTION_NONE = 0;
int ACTION_APP_PREFERENCE = 1;
int ACTION_NOTIF_PREFERENCE = 2;
+ int ACTION_DND_RULE_PREFERENCE = 3;
}
public static final String PREF_KEY_APP_HEADER = "pref_app_header";
private boolean mIsInstantApp;
+ private View.OnClickListener mEditRuleNameOnClickListener;
+
/**
* Creates a new instance of the controller.
*
return this;
}
+ public EntityHeaderController setEditZenRuleNameListener(View.OnClickListener listener) {
+ this.mEditRuleNameOnClickListener = listener;
+ return this;
+ }
+
/**
* Done mutating entity header, rebinds everything and return a new {@link LayoutPreference}.
*/
return;
}
switch (action) {
+ case ActionType.ACTION_DND_RULE_PREFERENCE: {
+ if (mEditRuleNameOnClickListener == null) {
+ button.setVisibility(View.GONE);
+ } else {
+ button.setImageResource(R.drawable.ic_mode_edit);
+ button.setVisibility(View.VISIBLE);
+ button.setOnClickListener(mEditRuleNameOnClickListener);
+ }
+ return;
+ }
case ActionType.ACTION_NOTIF_PREFERENCE: {
if (mAppNotifPrefIntent == null) {
button.setVisibility(View.GONE);
}
@Test
+ public void bindButton_hasEditRuleNameClickListener_shouldShowButton() {
+ final ResolveInfo info = new ResolveInfo();
+ info.activityInfo = new ActivityInfo();
+ info.activityInfo.packageName = "123";
+ info.activityInfo.name = "321";
+ final View view = mLayoutInflater
+ .inflate(R.layout.settings_entity_header, null /* root */);
+ when(mActivity.getApplicationContext()).thenReturn(mContext);
+
+ mController = EntityHeaderController.newInstance(mActivity, mFragment, view);
+ mController.setEditZenRuleNameListener(new View.OnClickListener() {
+ public void onClick(View v) {
+ // do nothing
+ }
+ });
+ mController.setButtonActions(
+ EntityHeaderController.ActionType.ACTION_DND_RULE_PREFERENCE,
+ EntityHeaderController.ActionType.ACTION_NONE);
+ mController.done(mActivity);
+
+ final ImageButton button1 = view.findViewById(android.R.id.button1);
+ assertThat(button1.getVisibility()).isEqualTo(View.VISIBLE);
+ assertThat(button1.getDrawable()).isNotNull();
+ assertThat(view.findViewById(android.R.id.button2).getVisibility())
+ .isEqualTo(View.GONE);
+ }
+
+ @Test
+ public void bindButton_noEditRuleNameClickListener_shouldNotShowButton() {
+ final ResolveInfo info = new ResolveInfo();
+ info.activityInfo = new ActivityInfo();
+ info.activityInfo.packageName = "123";
+ info.activityInfo.name = "321";
+ final View view = mLayoutInflater
+ .inflate(R.layout.settings_entity_header, null /* root */);
+ when(mActivity.getApplicationContext()).thenReturn(mContext);
+
+ mController = EntityHeaderController.newInstance(mActivity, mFragment, view);
+ mController.setButtonActions(
+ EntityHeaderController.ActionType.ACTION_DND_RULE_PREFERENCE,
+ EntityHeaderController.ActionType.ACTION_NONE);
+ mController.done(mActivity);
+
+ assertThat(view.findViewById(android.R.id.button1).getVisibility())
+ .isEqualTo(View.GONE);
+ assertThat(view.findViewById(android.R.id.button2).getVisibility())
+ .isEqualTo(View.GONE);
+ }
+
+
+ @Test
public void bindButton_noAppPref_shouldNotShowButton() {
final View appLinks = mLayoutInflater
.inflate(R.layout.settings_entity_header, null /* root */);