blob: cc70a6f549e0b2891cf396440dc034a5ba67c849 [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;
30import android.support.v7.preference.Preference;
31
Beverly32352212017-11-20 17:33:01 -050032import com.android.internal.logging.nano.MetricsProto;
Beverlya58e52a2017-10-27 14:44:23 -040033import com.android.settings.core.PreferenceControllerMixin;
Beverly32352212017-11-20 17:33:01 -050034import com.android.settingslib.core.lifecycle.Lifecycle;
Beverlya58e52a2017-10-27 14:44:23 -040035
36import java.util.Arrays;
37import java.util.Comparator;
38import java.util.Map;
39import java.util.Set;
40
41abstract public class AbstractZenModeAutomaticRulePreferenceController extends
Beverly32352212017-11-20 17:33:01 -050042 AbstractZenModePreferenceController implements PreferenceControllerMixin {
Beverlya58e52a2017-10-27 14:44:23 -040043
Beverlya58e52a2017-10-27 14:44:23 -040044 protected ZenModeBackend mBackend;
45 protected Fragment mParent;
46 protected Set<Map.Entry<String, AutomaticZenRule>> mRules;
47 protected PackageManager mPm;
48
Beverly32352212017-11-20 17:33:01 -050049 public AbstractZenModeAutomaticRulePreferenceController(Context context, String key, Fragment
50 parent, Lifecycle lifecycle) {
51 super(context, key, lifecycle);
Beverlya58e52a2017-10-27 14:44:23 -040052 mBackend = ZenModeBackend.getInstance(context);
Beverlya58e52a2017-10-27 14:44:23 -040053 mPm = mContext.getPackageManager();
Beverly32352212017-11-20 17:33:01 -050054 mParent = parent;
Beverlya58e52a2017-10-27 14:44:23 -040055 }
56
57 @Override
58 public void updateState(Preference preference) {
59 super.updateState(preference);
60 mRules = getZenModeRules();
61 }
62
63 private Set<Map.Entry<String, AutomaticZenRule>> getZenModeRules() {
64 Map<String, AutomaticZenRule> ruleMap =
65 NotificationManager.from(mContext).getAutomaticZenRules();
66 return ruleMap.entrySet();
67 }
68
Beverly32352212017-11-20 17:33:01 -050069 protected void showNameRuleDialog(final ZenRuleInfo ri, Fragment parent) {
70 ZenRuleNameDialog.show(parent, null, ri.defaultConditionId, new
71 RuleNameChangeListener(ri));
Beverlya58e52a2017-10-27 14:44:23 -040072 }
73
74 protected Map.Entry<String, AutomaticZenRule>[] sortedRules() {
75 if (mRules == null) {
76 mRules = getZenModeRules();
77 }
78 final Map.Entry<String, AutomaticZenRule>[] rt =
79 mRules.toArray(new Map.Entry[mRules.size()]);
80 Arrays.sort(rt, RULE_COMPARATOR);
81 return rt;
82 }
83
84 protected static Intent getRuleIntent(String settingsAction,
85 ComponentName configurationActivity, String ruleId) {
86 final Intent intent = new Intent()
87 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
88 .putExtra(ConditionProviderService.EXTRA_RULE_ID, ruleId);
89 if (configurationActivity != null) {
90 intent.setComponent(configurationActivity);
91 } else {
92 intent.setAction(settingsAction);
93 }
94 return intent;
95 }
96
97 private static final Comparator<Map.Entry<String, AutomaticZenRule>> RULE_COMPARATOR =
98 new Comparator<Map.Entry<String, AutomaticZenRule>>() {
99 @Override
100 public int compare(Map.Entry<String, AutomaticZenRule> lhs,
101 Map.Entry<String, AutomaticZenRule> rhs) {
102 int byDate = Long.compare(lhs.getValue().getCreationTime(),
103 rhs.getValue().getCreationTime());
104 if (byDate != 0) {
105 return byDate;
106 } else {
107 return key(lhs.getValue()).compareTo(key(rhs.getValue()));
108 }
109 }
110
111 private String key(AutomaticZenRule rule) {
112 final int type = ZenModeConfig.isValidScheduleConditionId(rule.getConditionId())
113 ? 1 : ZenModeConfig.isValidEventConditionId(rule.getConditionId())
114 ? 2 : 3;
115 return type + rule.getName().toString();
116 }
117 };
118
119 public static ZenRuleInfo getRuleInfo(PackageManager pm, ServiceInfo si) {
120 if (si == null || si.metaData == null) {
121 return null;
122 }
123 final String ruleType = si.metaData.getString(ConditionProviderService.META_DATA_RULE_TYPE);
124 final ComponentName configurationActivity = getSettingsActivity(si);
125 if (ruleType != null && !ruleType.trim().isEmpty() && configurationActivity != null) {
126 final ZenRuleInfo ri = new ZenRuleInfo();
127 ri.serviceComponent = new ComponentName(si.packageName, si.name);
128 ri.settingsAction = Settings.ACTION_ZEN_MODE_EXTERNAL_RULE_SETTINGS;
129 ri.title = ruleType;
130 ri.packageName = si.packageName;
131 ri.configurationActivity = getSettingsActivity(si);
132 ri.packageLabel = si.applicationInfo.loadLabel(pm);
133 ri.ruleInstanceLimit =
134 si.metaData.getInt(ConditionProviderService.META_DATA_RULE_INSTANCE_LIMIT, -1);
135 return ri;
136 }
137 return null;
138 }
139
140 protected static ComponentName getSettingsActivity(ServiceInfo si) {
141 if (si == null || si.metaData == null) {
142 return null;
143 }
144 final String configurationActivity =
145 si.metaData.getString(ConditionProviderService.META_DATA_CONFIGURATION_ACTIVITY);
146 if (configurationActivity != null) {
147 return ComponentName.unflattenFromString(configurationActivity);
148 }
149 return null;
150 }
Beverly32352212017-11-20 17:33:01 -0500151
152 public class RuleNameChangeListener implements ZenRuleNameDialog.PositiveClickListener {
153 ZenRuleInfo mRuleInfo;
154
155 public RuleNameChangeListener(ZenRuleInfo ruleInfo) {
156 mRuleInfo = ruleInfo;
157 }
158
159 @Override
160 public void onOk(String ruleName, Fragment parent) {
161 mMetricsFeatureProvider.action(mContext,
162 MetricsProto.MetricsEvent.ACTION_ZEN_MODE_RULE_NAME_CHANGE_OK);
163 AutomaticZenRule rule = new AutomaticZenRule(ruleName, mRuleInfo.serviceComponent,
164 mRuleInfo.defaultConditionId,
165 NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
166 String savedRuleId = mBackend.addZenRule(rule);
167 if (savedRuleId != null) {
168 parent.startActivity(getRuleIntent(mRuleInfo.settingsAction, null,
169 savedRuleId));
170 }
171 }
172 }
Beverlya58e52a2017-10-27 14:44:23 -0400173}