Merge "Update Zen Automatic Rule Settings page"
This commit is contained in:
committed by
Android (Google) Code Review
commit
3645640904
@@ -25,7 +25,6 @@ import android.content.pm.PackageManager;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.provider.Settings;
|
||||
import android.service.notification.ConditionProviderService;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.preference.Preference;
|
||||
@@ -34,20 +33,15 @@ import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
abstract public class AbstractZenModeAutomaticRulePreferenceController extends
|
||||
AbstractZenModePreferenceController implements PreferenceControllerMixin {
|
||||
|
||||
protected ZenModeBackend mBackend;
|
||||
protected Fragment mParent;
|
||||
protected Set<Map.Entry<String, AutomaticZenRule>> mRules;
|
||||
protected Map.Entry<String, AutomaticZenRule>[] mRules;
|
||||
protected PackageManager mPm;
|
||||
private static List<String> mDefaultRuleIds;
|
||||
|
||||
public AbstractZenModeAutomaticRulePreferenceController(Context context, String key, Fragment
|
||||
parent, Lifecycle lifecycle) {
|
||||
@@ -60,20 +54,14 @@ abstract public class AbstractZenModeAutomaticRulePreferenceController extends
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
mRules = getZenModeRules();
|
||||
mRules = mBackend.getAutomaticZenRules();
|
||||
}
|
||||
|
||||
private static List<String> getDefaultRuleIds() {
|
||||
if (mDefaultRuleIds == null) {
|
||||
mDefaultRuleIds = ZenModeConfig.DEFAULT_RULE_IDS;
|
||||
protected Map.Entry<String, AutomaticZenRule>[] getRules() {
|
||||
if (mRules == null) {
|
||||
mRules = mBackend.getAutomaticZenRules();
|
||||
}
|
||||
return mDefaultRuleIds;
|
||||
}
|
||||
|
||||
private Set<Map.Entry<String, AutomaticZenRule>> getZenModeRules() {
|
||||
Map<String, AutomaticZenRule> ruleMap =
|
||||
NotificationManager.from(mContext).getAutomaticZenRules();
|
||||
return ruleMap.entrySet();
|
||||
return mRules;
|
||||
}
|
||||
|
||||
protected void showNameRuleDialog(final ZenRuleInfo ri, Fragment parent) {
|
||||
@@ -81,16 +69,6 @@ abstract public class AbstractZenModeAutomaticRulePreferenceController extends
|
||||
RuleNameChangeListener(ri));
|
||||
}
|
||||
|
||||
protected Map.Entry<String, AutomaticZenRule>[] sortedRules() {
|
||||
if (mRules == null) {
|
||||
mRules = getZenModeRules();
|
||||
}
|
||||
final Map.Entry<String, AutomaticZenRule>[] rt =
|
||||
mRules.toArray(new Map.Entry[mRules.size()]);
|
||||
Arrays.sort(rt, RULE_COMPARATOR);
|
||||
return rt;
|
||||
}
|
||||
|
||||
protected static Intent getRuleIntent(String settingsAction,
|
||||
ComponentName configurationActivity, String ruleId) {
|
||||
final Intent intent = new Intent()
|
||||
@@ -104,35 +82,6 @@ abstract public class AbstractZenModeAutomaticRulePreferenceController extends
|
||||
return intent;
|
||||
}
|
||||
|
||||
private static final Comparator<Map.Entry<String, AutomaticZenRule>> RULE_COMPARATOR =
|
||||
new Comparator<Map.Entry<String, AutomaticZenRule>>() {
|
||||
@Override
|
||||
public int compare(Map.Entry<String, AutomaticZenRule> lhs,
|
||||
Map.Entry<String, AutomaticZenRule> rhs) {
|
||||
// if it's a default rule, should be at the top of automatic rules
|
||||
boolean lhsIsDefaultRule = getDefaultRuleIds().contains(lhs.getKey());
|
||||
boolean rhsIsDefaultRule = getDefaultRuleIds().contains(rhs.getKey());
|
||||
if (lhsIsDefaultRule != rhsIsDefaultRule) {
|
||||
return lhsIsDefaultRule ? -1 : 1;
|
||||
}
|
||||
|
||||
int byDate = Long.compare(lhs.getValue().getCreationTime(),
|
||||
rhs.getValue().getCreationTime());
|
||||
if (byDate != 0) {
|
||||
return byDate;
|
||||
} else {
|
||||
return key(lhs.getValue()).compareTo(key(rhs.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
private String key(AutomaticZenRule rule) {
|
||||
final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId())
|
||||
? 1 : ZenModeConfig.isValidEventConditionId(rule.getConditionId())
|
||||
? 2 : 3;
|
||||
return type + rule.getName().toString();
|
||||
}
|
||||
};
|
||||
|
||||
public static ZenRuleInfo getRuleInfo(PackageManager pm, ServiceInfo si) {
|
||||
if (si == null || si.metaData == null) {
|
||||
return null;
|
||||
|
||||
@@ -136,7 +136,7 @@ public class ZenAutomaticRuleHeaderPreferenceController extends AbstractZenModeP
|
||||
MetricsProto.MetricsEvent.ACTION_ZEN_MODE_RULE_NAME_CHANGE_OK);
|
||||
mRule.setName(ruleName);
|
||||
mRule.setModified(true);
|
||||
mBackend.setZenRule(mId, mRule);
|
||||
mBackend.updateZenRule(mId, mRule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,6 @@ public class ZenAutomaticRuleSwitchPreferenceController extends
|
||||
final boolean enabled = isChecked;
|
||||
if (enabled == mRule.isEnabled()) return;
|
||||
mRule.setEnabled(enabled);
|
||||
mBackend.setZenRule(mId, mRule);
|
||||
mBackend.updateZenRule(mId, mRule);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,12 +21,12 @@ import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
public class ZenModeAlarmsPreferenceController extends
|
||||
AbstractZenModePreferenceController implements Preference.OnPreferenceChangeListener {
|
||||
|
||||
@@ -78,6 +78,7 @@ public class ZenModeAlarmsPreferenceController extends
|
||||
mMetricsFeatureProvider.action(mContext, MetricsProto.MetricsEvent.ACTION_ZEN_ALLOW_ALARMS,
|
||||
allowAlarms);
|
||||
mBackend.saveSoundPolicy(Policy.PRIORITY_CATEGORY_ALARMS, allowAlarms);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import androidx.preference.PreferenceScreen;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ZenModeAutomaticRulesPreferenceController extends
|
||||
AbstractZenModeAutomaticRulePreferenceController {
|
||||
@@ -62,16 +63,40 @@ public class ZenModeAutomaticRulesPreferenceController extends
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
Map.Entry<String, AutomaticZenRule>[] sortedRules = getRules();
|
||||
final int currNumPreferences = mPreferenceCategory.getPreferenceCount();
|
||||
if (currNumPreferences == sortedRules.length) {
|
||||
for (int i = 0; i < sortedRules.length; i++) {
|
||||
ZenRulePreference pref = (ZenRulePreference) mPreferenceCategory.getPreference(i);
|
||||
// we are either:
|
||||
// 1. updating the enabled state or name of the rule
|
||||
// 2. rule was added or deleted, so reload the entire list
|
||||
if (Objects.equals(pref.mId, sortedRules[i].getKey())) {
|
||||
AutomaticZenRule rule = sortedRules[i].getValue();
|
||||
pref.setName(rule.getName());
|
||||
pref.setChecked(rule.isEnabled());
|
||||
} else {
|
||||
reloadAllRules(sortedRules);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
reloadAllRules(sortedRules);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void reloadAllRules(Map.Entry<String, AutomaticZenRule>[] rules) {
|
||||
mPreferenceCategory.removeAll();
|
||||
Map.Entry<String, AutomaticZenRule>[] sortedRules = sortedRules();
|
||||
for (Map.Entry<String, AutomaticZenRule> sortedRule : sortedRules) {
|
||||
ZenRulePreference pref = new ZenRulePreference(mPreferenceCategory.getContext(),
|
||||
sortedRule, mParent, mMetricsFeatureProvider);
|
||||
for (Map.Entry<String, AutomaticZenRule> rule : rules) {
|
||||
ZenRulePreference pref = createZenRulePreference(rule);
|
||||
mPreferenceCategory.addPreference(pref);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
ZenRulePreference createZenRulePreference(Map.Entry<String, AutomaticZenRule> rule) {
|
||||
return new ZenRulePreference(mPreferenceCategory.getContext(),
|
||||
rule, mParent, mMetricsFeatureProvider);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -16,9 +16,15 @@
|
||||
|
||||
package com.android.settings.notification;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.AutomaticZenRule;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.service.notification.ConditionProviderService;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
@@ -34,10 +40,14 @@ import com.android.settingslib.search.SearchIndexable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@SearchIndexable
|
||||
public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
||||
protected final ManagedServiceSettings.Config CONFIG = getConditionProviderConfig();
|
||||
private CharSequence[] mDeleteDialogRuleNames;
|
||||
private String[] mDeleteDialogRuleIds;
|
||||
private boolean[] mDeleteDialogChecked;
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
@@ -74,6 +84,52 @@ public class ZenModeAutomationSettings extends ZenModeSettingsBase {
|
||||
.setNoun("condition provider")
|
||||
.build();
|
||||
}
|
||||
private final int DELETE_RULES = 1;
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||
menu.add(Menu.NONE, DELETE_RULES, Menu.NONE, R.string.zen_mode_delete_automatic_rules);
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case DELETE_RULES:
|
||||
Map.Entry<String, AutomaticZenRule>[] rules = mBackend.getAutomaticZenRules();
|
||||
mDeleteDialogRuleNames = new CharSequence[rules.length];
|
||||
mDeleteDialogRuleIds = new String[rules.length];
|
||||
mDeleteDialogChecked = new boolean[rules.length];
|
||||
for (int i = 0; i < rules.length; i++) {
|
||||
mDeleteDialogRuleNames[i] = rules[i].getValue().getName();
|
||||
mDeleteDialogRuleIds[i] = rules[i].getKey();
|
||||
}
|
||||
new AlertDialog.Builder(mContext)
|
||||
.setTitle(R.string.zen_mode_delete_automatic_rules)
|
||||
.setMultiChoiceItems(mDeleteDialogRuleNames, null,
|
||||
new DialogInterface.OnMultiChoiceClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which,
|
||||
boolean isChecked) {
|
||||
mDeleteDialogChecked[which] = isChecked;
|
||||
}
|
||||
})
|
||||
.setPositiveButton(R.string.zen_mode_schedule_delete,
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
for (int i = 0; i < rules.length; i++) {
|
||||
if (mDeleteDialogChecked[i]) {
|
||||
mBackend.removeZenRule(mDeleteDialogRuleIds[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).show();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For Search.
|
||||
|
||||
@@ -32,6 +32,11 @@ import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ZenModeBackend {
|
||||
@VisibleForTesting
|
||||
protected static final String ZEN_MODE_FROM_ANYONE = "zen_mode_from_anyone";
|
||||
@@ -42,6 +47,7 @@ public class ZenModeBackend {
|
||||
@VisibleForTesting
|
||||
protected static final String ZEN_MODE_FROM_NONE = "zen_mode_from_none";
|
||||
protected static final int SOURCE_NONE = -1;
|
||||
private static List<String> mDefaultRuleIds;
|
||||
|
||||
private static ZenModeBackend sInstance;
|
||||
|
||||
@@ -79,7 +85,7 @@ public class ZenModeBackend {
|
||||
Settings.Global.ZEN_MODE, mZenMode);
|
||||
}
|
||||
|
||||
protected boolean setZenRule(String id, AutomaticZenRule rule) {
|
||||
protected boolean updateZenRule(String id, AutomaticZenRule rule) {
|
||||
return NotificationManager.from(mContext).updateAutomaticZenRule(id, rule);
|
||||
}
|
||||
|
||||
@@ -294,13 +300,65 @@ public class ZenModeBackend {
|
||||
return NotificationManager.from(mContext).removeAutomaticZenRule(ruleId);
|
||||
}
|
||||
|
||||
public NotificationManager.Policy getConsolidatedPolicy() {
|
||||
return NotificationManager.from(mContext).getConsolidatedNotificationPolicy();
|
||||
}
|
||||
|
||||
protected String addZenRule(AutomaticZenRule rule) {
|
||||
try {
|
||||
String id = NotificationManager.from(mContext).addAutomaticZenRule(rule);
|
||||
NotificationManager.from(mContext).getAutomaticZenRule(id);
|
||||
return id;
|
||||
return NotificationManager.from(mContext).addAutomaticZenRule(rule);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected Map.Entry<String, AutomaticZenRule>[] getAutomaticZenRules() {
|
||||
Map<String, AutomaticZenRule> ruleMap =
|
||||
NotificationManager.from(mContext).getAutomaticZenRules();
|
||||
final Map.Entry<String, AutomaticZenRule>[] rt = ruleMap.entrySet().toArray(
|
||||
new Map.Entry[ruleMap.size()]);
|
||||
Arrays.sort(rt, RULE_COMPARATOR);
|
||||
return rt;
|
||||
}
|
||||
|
||||
protected AutomaticZenRule getAutomaticZenRule(String id) {
|
||||
return NotificationManager.from(mContext).getAutomaticZenRule(id);
|
||||
}
|
||||
|
||||
private static List<String> getDefaultRuleIds() {
|
||||
if (mDefaultRuleIds == null) {
|
||||
mDefaultRuleIds = ZenModeConfig.DEFAULT_RULE_IDS;
|
||||
}
|
||||
return mDefaultRuleIds;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public static final Comparator<Map.Entry<String, AutomaticZenRule>> RULE_COMPARATOR =
|
||||
new Comparator<Map.Entry<String, AutomaticZenRule>>() {
|
||||
@Override
|
||||
public int compare(Map.Entry<String, AutomaticZenRule> lhs,
|
||||
Map.Entry<String, AutomaticZenRule> rhs) {
|
||||
// if it's a default rule, should be at the top of automatic rules
|
||||
boolean lhsIsDefaultRule = getDefaultRuleIds().contains(lhs.getKey());
|
||||
boolean rhsIsDefaultRule = getDefaultRuleIds().contains(rhs.getKey());
|
||||
if (lhsIsDefaultRule != rhsIsDefaultRule) {
|
||||
return lhsIsDefaultRule ? -1 : 1;
|
||||
}
|
||||
|
||||
int byDate = Long.compare(lhs.getValue().getCreationTime(),
|
||||
rhs.getValue().getCreationTime());
|
||||
if (byDate != 0) {
|
||||
return byDate;
|
||||
} else {
|
||||
return key(lhs.getValue()).compareTo(key(rhs.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
private String key(AutomaticZenRule rule) {
|
||||
final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId())
|
||||
? 1 : ZenModeConfig.isValidEventConditionId(rule.getConditionId())
|
||||
? 2 : 3;
|
||||
return type + rule.getName().toString();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ public abstract class ZenModeRuleSettingsBase extends ZenModeSettingsBase {
|
||||
|
||||
protected void updateRule(Uri newConditionId) {
|
||||
mRule.setConditionId(newConditionId);
|
||||
mBackend.setZenRule(mId, mRule);
|
||||
mBackend.updateZenRule(mId, mRule);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -64,7 +64,6 @@ public class ZenModeScheduleRuleSettings extends ZenModeRuleSettingsBase {
|
||||
private TimePickerPreference mEnd;
|
||||
private SwitchPreference mExitAtAlarm;
|
||||
private AlertDialog mDayDialog;
|
||||
|
||||
private ScheduleInfo mSchedule;
|
||||
|
||||
@Override
|
||||
@@ -186,7 +185,6 @@ public class ZenModeScheduleRuleSettings extends ZenModeRuleSettingsBase {
|
||||
final int summaryFormat = nextDay ? R.string.zen_mode_end_time_next_day_summary_format : 0;
|
||||
mEnd.setSummaryFormat(summaryFormat);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateControlsInternal() {
|
||||
updateDays();
|
||||
|
||||
@@ -16,16 +16,23 @@
|
||||
|
||||
package com.android.settings.notification;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.icu.text.ListFormatter;
|
||||
import android.net.Uri;
|
||||
import android.provider.Settings;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ZenModeSettingsFooterPreferenceController extends AbstractZenModePreferenceController {
|
||||
|
||||
protected static final String KEY = "footer_preference";
|
||||
@@ -65,6 +72,31 @@ public class ZenModeSettingsFooterPreferenceController extends AbstractZenModePr
|
||||
|
||||
protected String getFooterText() {
|
||||
ZenModeConfig config = getZenModeConfig();
|
||||
|
||||
NotificationManager.Policy appliedPolicy = mBackend.getConsolidatedPolicy();
|
||||
NotificationManager.Policy defaultPolicy = config.toNotificationPolicy();
|
||||
final boolean usingCustomPolicy = !Objects.equals(appliedPolicy, defaultPolicy);
|
||||
|
||||
if (usingCustomPolicy) {
|
||||
final List<ZenModeConfig.ZenRule> activeRules = getActiveRules(config);
|
||||
final List<String> rulesNames = new ArrayList<>();
|
||||
for (ZenModeConfig.ZenRule rule : activeRules) {
|
||||
if (rule.name != null) {
|
||||
rulesNames.add(rule.name);
|
||||
}
|
||||
}
|
||||
if (rulesNames.size() > 0) {
|
||||
String rules = ListFormatter.getInstance().format(rulesNames);
|
||||
if (!rules.isEmpty()) {
|
||||
return mContext.getString(R.string.zen_mode_settings_dnd_custom_settings_footer,
|
||||
rules);
|
||||
}
|
||||
}
|
||||
}
|
||||
return getFooterUsingDefaultPolicy(config);
|
||||
}
|
||||
|
||||
private String getFooterUsingDefaultPolicy(ZenModeConfig config) {
|
||||
String footerText = "";
|
||||
long latestEndTime = -1;
|
||||
|
||||
@@ -116,4 +148,18 @@ public class ZenModeSettingsFooterPreferenceController extends AbstractZenModePr
|
||||
}
|
||||
return footerText;
|
||||
}
|
||||
|
||||
private List<ZenModeConfig.ZenRule> getActiveRules(ZenModeConfig config) {
|
||||
List<ZenModeConfig.ZenRule> zenRules = new ArrayList<>();
|
||||
if (config.manualRule != null) {
|
||||
zenRules.add(config.manualRule);
|
||||
}
|
||||
|
||||
for (ZenModeConfig.ZenRule automaticRule : config.automaticRules.values()) {
|
||||
if (automaticRule.isAutomaticActive()) {
|
||||
zenRules.add(automaticRule);
|
||||
}
|
||||
}
|
||||
return zenRules;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,17 +19,18 @@ package com.android.settings.notification;
|
||||
import android.app.AutomaticZenRule;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
import android.view.View;
|
||||
import android.widget.CheckBox;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.utils.ManagedServiceSettings;
|
||||
import com.android.settings.utils.ZenServiceListing;
|
||||
@@ -37,11 +38,11 @@ import com.android.settingslib.TwoTargetPreference;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ZenRulePreference extends TwoTargetPreference {
|
||||
private static final ManagedServiceSettings.Config CONFIG =
|
||||
ZenModeAutomationSettings.getConditionProviderConfig();
|
||||
final CharSequence mName;
|
||||
final String mId;
|
||||
boolean appExists;
|
||||
final Fragment mParent;
|
||||
@@ -51,16 +52,23 @@ public class ZenRulePreference extends TwoTargetPreference {
|
||||
final ZenServiceListing mServiceListing;
|
||||
final PackageManager mPm;
|
||||
final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
final AutomaticZenRule mRule;
|
||||
CharSequence mName;
|
||||
|
||||
private boolean mIsSystemRule;
|
||||
private Intent mIntent;
|
||||
private boolean mChecked;
|
||||
private CheckBox mCheckBox;
|
||||
|
||||
public ZenRulePreference(Context context,
|
||||
final Map.Entry<String, AutomaticZenRule> ruleEntry,
|
||||
Fragment parent, MetricsFeatureProvider metricsProvider) {
|
||||
super(context);
|
||||
|
||||
setLayoutResource(R.layout.preference_checkable_two_target);
|
||||
mBackend = ZenModeBackend.getInstance(context);
|
||||
mContext = context;
|
||||
final AutomaticZenRule rule = ruleEntry.getValue();
|
||||
mName = rule.getName();
|
||||
mRule = ruleEntry.getValue();
|
||||
mName = mRule.getName();
|
||||
mId = ruleEntry.getKey();
|
||||
mParent = parent;
|
||||
mPm = mContext.getPackageManager();
|
||||
@@ -68,65 +76,97 @@ public class ZenRulePreference extends TwoTargetPreference {
|
||||
mServiceListing.reloadApprovedServices();
|
||||
mPref = this;
|
||||
mMetricsFeatureProvider = metricsProvider;
|
||||
|
||||
setAttributes(rule);
|
||||
mChecked = mRule.isEnabled();
|
||||
setAttributes(mRule);
|
||||
setWidgetLayoutResource(getSecondTargetResId());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getSecondTargetResId() {
|
||||
if (mId != null && ZenModeConfig.DEFAULT_RULE_IDS.contains(mId)) {
|
||||
return 0;
|
||||
if (mIntent != null) {
|
||||
return R.layout.zen_rule_widget;
|
||||
}
|
||||
|
||||
return R.layout.zen_rule_widget;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder view) {
|
||||
super.onBindViewHolder(view);
|
||||
View settingsWidget = view.findViewById(android.R.id.widget_frame);
|
||||
View divider = view.findViewById(R.id.two_target_divider);
|
||||
if (mIntent != null) {
|
||||
divider.setVisibility(View.VISIBLE);
|
||||
settingsWidget.setVisibility(View.VISIBLE);
|
||||
settingsWidget.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
mContext.startActivity(mIntent);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
divider.setVisibility(View.GONE);
|
||||
settingsWidget.setVisibility(View.GONE);
|
||||
settingsWidget.setOnClickListener(null);
|
||||
}
|
||||
|
||||
View v = view.findViewById(R.id.delete_zen_rule);
|
||||
if (v != null) {
|
||||
v.setOnClickListener(mDeleteListener);
|
||||
View checkboxContainer = view.findViewById(R.id.checkbox_container);
|
||||
if (checkboxContainer != null) {
|
||||
checkboxContainer.setOnClickListener(mOnCheckBoxClickListener);
|
||||
}
|
||||
mCheckBox = (CheckBox) view.findViewById(com.android.internal.R.id.checkbox);
|
||||
if (mCheckBox != null) {
|
||||
mCheckBox.setChecked(mChecked);
|
||||
}
|
||||
}
|
||||
|
||||
private final View.OnClickListener mDeleteListener = new View.OnClickListener() {
|
||||
public boolean isChecked() {
|
||||
return mChecked;
|
||||
}
|
||||
|
||||
public void setChecked(boolean checked) {
|
||||
mChecked = checked;
|
||||
if (mCheckBox != null) {
|
||||
mCheckBox.setChecked(checked);
|
||||
}
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
if (!Objects.equals(mName, name)) {
|
||||
mName = name;
|
||||
setTitle(mName);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick() {
|
||||
mOnCheckBoxClickListener.onClick(null);
|
||||
}
|
||||
|
||||
private View.OnClickListener mOnCheckBoxClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
showDeleteRuleDialog(mParent, mId, mName.toString());
|
||||
mRule.setEnabled(!mChecked);
|
||||
mBackend.updateZenRule(mId, mRule);
|
||||
setChecked(mRule.isEnabled());
|
||||
setAttributes(mRule);
|
||||
}
|
||||
};
|
||||
|
||||
private void showDeleteRuleDialog(final Fragment parent, final String ruleId,
|
||||
final String ruleName) {
|
||||
ZenDeleteRuleDialog.show(parent, ruleName, ruleId,
|
||||
new ZenDeleteRuleDialog.PositiveClickListener() {
|
||||
@Override
|
||||
public void onOk(String id) {
|
||||
mMetricsFeatureProvider.action(mContext,
|
||||
MetricsProto.MetricsEvent.ACTION_ZEN_DELETE_RULE_OK);
|
||||
mBackend.removeZenRule(id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected void setAttributes(AutomaticZenRule rule) {
|
||||
final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
|
||||
rule.getConditionId(), true);
|
||||
final boolean isEvent = ZenModeConfig.isValidEventConditionId(rule.getConditionId());
|
||||
final boolean isSystemRule = isSchedule || isEvent;
|
||||
mIsSystemRule = isSchedule || isEvent;
|
||||
|
||||
try {
|
||||
ApplicationInfo info = mPm.getApplicationInfo(rule.getOwner().getPackageName(), 0);
|
||||
setSummary(computeRuleSummary(rule, isSystemRule, info.loadLabel(mPm)));
|
||||
setSummary(computeRuleSummary(rule, mIsSystemRule, info.loadLabel(mPm)));
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
appExists = false;
|
||||
return;
|
||||
}
|
||||
|
||||
appExists = true;
|
||||
setTitle(rule.getName());
|
||||
setTitle(mName);
|
||||
setPersistent(false);
|
||||
|
||||
final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION
|
||||
@@ -134,9 +174,8 @@ public class ZenRulePreference extends TwoTargetPreference {
|
||||
ServiceInfo si = mServiceListing.findService(rule.getOwner());
|
||||
ComponentName settingsActivity = AbstractZenModeAutomaticRulePreferenceController.
|
||||
getSettingsActivity(si);
|
||||
setIntent(AbstractZenModeAutomaticRulePreferenceController.getRuleIntent(action,
|
||||
settingsActivity, mId));
|
||||
setSelectable(settingsActivity != null || isSystemRule);
|
||||
mIntent = AbstractZenModeAutomaticRulePreferenceController.getRuleIntent(action,
|
||||
settingsActivity, mId);
|
||||
setKey(mId);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user