blob: 9f4ed8f02f649fade6e42d9f36fcfbe002702535 [file] [log] [blame]
Beverlya58e52a2017-10-27 14:44:23 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.notification;
18
19import android.app.AutomaticZenRule;
20import android.app.Fragment;
21import android.app.NotificationManager;
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.PackageManager;
26import android.content.pm.ServiceInfo;
27import android.provider.Settings;
28import android.service.notification.ConditionProviderService;
29import android.service.notification.ZenModeConfig;
Fan Zhangde117042018-09-04 13:52:15 -070030
Aurimas Liutikas03fcde32018-04-17 11:22:43 -070031import androidx.preference.Preference;
Beverlya58e52a2017-10-27 14:44:23 -040032
Beverly32352212017-11-20 17:33:01 -050033import com.android.internal.logging.nano.MetricsProto;
Beverlya58e52a2017-10-27 14:44:23 -040034import com.android.settings.core.PreferenceControllerMixin;
Beverly32352212017-11-20 17:33:01 -050035import com.android.settingslib.core.lifecycle.Lifecycle;
Beverlya58e52a2017-10-27 14:44:23 -040036
37import java.util.Arrays;
38import java.util.Comparator;
Beverly2c5895d2017-12-05 15:50:26 -050039import java.util.List;
Beverlya58e52a2017-10-27 14:44:23 -040040import java.util.Map;
41import java.util.Set;
42
43abstract public class AbstractZenModeAutomaticRulePreferenceController extends
Beverly32352212017-11-20 17:33:01 -050044 AbstractZenModePreferenceController implements PreferenceControllerMixin {
Beverlya58e52a2017-10-27 14:44:23 -040045
Beverlya58e52a2017-10-27 14:44:23 -040046 protected ZenModeBackend mBackend;
47 protected Fragment mParent;
48 protected Set<Map.Entry<String, AutomaticZenRule>> mRules;
49 protected PackageManager mPm;
Beverly2c5895d2017-12-05 15:50:26 -050050 private static List<String> mDefaultRuleIds;
Beverlya58e52a2017-10-27 14:44:23 -040051
Beverly32352212017-11-20 17:33:01 -050052 public AbstractZenModeAutomaticRulePreferenceController(Context context, String key, Fragment
53 parent, Lifecycle lifecycle) {
54 super(context, key, lifecycle);
Beverlya58e52a2017-10-27 14:44:23 -040055 mBackend = ZenModeBackend.getInstance(context);
Beverlya58e52a2017-10-27 14:44:23 -040056 mPm = mContext.getPackageManager();
Beverly32352212017-11-20 17:33:01 -050057 mParent = parent;
Beverlya58e52a2017-10-27 14:44:23 -040058 }
59
60 @Override
61 public void updateState(Preference preference) {
62 super.updateState(preference);
63 mRules = getZenModeRules();
64 }
65
Beverly2c5895d2017-12-05 15:50:26 -050066 private static List<String> getDefaultRuleIds() {
67 if (mDefaultRuleIds == null) {
68 mDefaultRuleIds = ZenModeConfig.DEFAULT_RULE_IDS;
69 }
70 return mDefaultRuleIds;
71 }
72
Beverlya58e52a2017-10-27 14:44:23 -040073 private Set<Map.Entry<String, AutomaticZenRule>> getZenModeRules() {
74 Map<String, AutomaticZenRule> ruleMap =
75 NotificationManager.from(mContext).getAutomaticZenRules();
76 return ruleMap.entrySet();
77 }
78
Beverly32352212017-11-20 17:33:01 -050079 protected void showNameRuleDialog(final ZenRuleInfo ri, Fragment parent) {
80 ZenRuleNameDialog.show(parent, null, ri.defaultConditionId, new
81 RuleNameChangeListener(ri));
Beverlya58e52a2017-10-27 14:44:23 -040082 }
83
84 protected Map.Entry<String, AutomaticZenRule>[] sortedRules() {
85 if (mRules == null) {
86 mRules = getZenModeRules();
87 }
88 final Map.Entry<String, AutomaticZenRule>[] rt =
89 mRules.toArray(new Map.Entry[mRules.size()]);
90 Arrays.sort(rt, RULE_COMPARATOR);
91 return rt;
92 }
93
94 protected static Intent getRuleIntent(String settingsAction,
95 ComponentName configurationActivity, String ruleId) {
96 final Intent intent = new Intent()
97 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
98 .putExtra(ConditionProviderService.EXTRA_RULE_ID, ruleId);
99 if (configurationActivity != null) {
100 intent.setComponent(configurationActivity);
101 } else {
102 intent.setAction(settingsAction);
103 }
104 return intent;
105 }
106
107 private static final Comparator<Map.Entry<String, AutomaticZenRule>> RULE_COMPARATOR =
108 new Comparator<Map.Entry<String, AutomaticZenRule>>() {
109 @Override
110 public int compare(Map.Entry<String, AutomaticZenRule> lhs,
111 Map.Entry<String, AutomaticZenRule> rhs) {
Beverly2c5895d2017-12-05 15:50:26 -0500112 // if it's a default rule, should be at the top of automatic rules
113 boolean lhsIsDefaultRule = getDefaultRuleIds().contains(lhs.getKey());
114 boolean rhsIsDefaultRule = getDefaultRuleIds().contains(rhs.getKey());
115 if (lhsIsDefaultRule != rhsIsDefaultRule) {
116 return lhsIsDefaultRule ? -1 : 1;
117 }
118
Beverlya58e52a2017-10-27 14:44:23 -0400119 int byDate = Long.compare(lhs.getValue().getCreationTime(),
120 rhs.getValue().getCreationTime());
121 if (byDate != 0) {
122 return byDate;
123 } else {
124 return key(lhs.getValue()).compareTo(key(rhs.getValue()));
125 }
126 }
127
128 private String key(AutomaticZenRule rule) {
129 final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId())
130 ? 1 : ZenModeConfig.isValidEventConditionId(rule.getConditionId())
131 ? 2 : 3;
132 return type + rule.getName().toString();
133 }
134 };
135
136 public static ZenRuleInfo getRuleInfo(PackageManager pm, ServiceInfo si) {
137 if (si == null || si.metaData == null) {
138 return null;
139 }
140 final String ruleType = si.metaData.getString(ConditionProviderService.META_DATA_RULE_TYPE);
141 final ComponentName configurationActivity = getSettingsActivity(si);
142 if (ruleType != null && !ruleType.trim().isEmpty() && configurationActivity != null) {
143 final ZenRuleInfo ri = new ZenRuleInfo();
144 ri.serviceComponent = new ComponentName(si.packageName, si.name);
145 ri.settingsAction = Settings.ACTION_ZEN_MODE_EXTERNAL_RULE_SETTINGS;
146 ri.title = ruleType;
147 ri.packageName = si.packageName;
148 ri.configurationActivity = getSettingsActivity(si);
149 ri.packageLabel = si.applicationInfo.loadLabel(pm);
150 ri.ruleInstanceLimit =
151 si.metaData.getInt(ConditionProviderService.META_DATA_RULE_INSTANCE_LIMIT, -1);
152 return ri;
153 }
154 return null;
155 }
156
157 protected static ComponentName getSettingsActivity(ServiceInfo si) {
158 if (si == null || si.metaData == null) {
159 return null;
160 }
161 final String configurationActivity =
162 si.metaData.getString(ConditionProviderService.META_DATA_CONFIGURATION_ACTIVITY);
163 if (configurationActivity != null) {
164 return ComponentName.unflattenFromString(configurationActivity);
165 }
166 return null;
167 }
Beverly32352212017-11-20 17:33:01 -0500168
169 public class RuleNameChangeListener implements ZenRuleNameDialog.PositiveClickListener {
170 ZenRuleInfo mRuleInfo;
171
172 public RuleNameChangeListener(ZenRuleInfo ruleInfo) {
173 mRuleInfo = ruleInfo;
174 }
175
176 @Override
177 public void onOk(String ruleName, Fragment parent) {
178 mMetricsFeatureProvider.action(mContext,
179 MetricsProto.MetricsEvent.ACTION_ZEN_MODE_RULE_NAME_CHANGE_OK);
180 AutomaticZenRule rule = new AutomaticZenRule(ruleName, mRuleInfo.serviceComponent,
181 mRuleInfo.defaultConditionId,
182 NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
183 String savedRuleId = mBackend.addZenRule(rule);
184 if (savedRuleId != null) {
185 parent.startActivity(getRuleIntent(mRuleInfo.settingsAction, null,
186 savedRuleId));
187 }
188 }
189 }
Beverlya58e52a2017-10-27 14:44:23 -0400190}