| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings.notification; |
| 18 | |
| 19 | import android.app.AutomaticZenRule; |
| 20 | import android.app.Fragment; |
| 21 | import android.app.NotificationManager; |
| 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.ServiceInfo; |
| 27 | import android.provider.Settings; |
| 28 | import android.service.notification.ConditionProviderService; |
| 29 | import android.service.notification.ZenModeConfig; |
| Fan Zhang | de11704 | 2018-09-04 13:52:15 -0700 | [diff] [blame^] | 30 | |
| Aurimas Liutikas | 03fcde3 | 2018-04-17 11:22:43 -0700 | [diff] [blame] | 31 | import androidx.preference.Preference; |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 32 | |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 33 | import com.android.internal.logging.nano.MetricsProto; |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 34 | import com.android.settings.core.PreferenceControllerMixin; |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 35 | import com.android.settingslib.core.lifecycle.Lifecycle; |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 36 | |
| 37 | import java.util.Arrays; |
| 38 | import java.util.Comparator; |
| Beverly | 2c5895d | 2017-12-05 15:50:26 -0500 | [diff] [blame] | 39 | import java.util.List; |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 40 | import java.util.Map; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | abstract public class AbstractZenModeAutomaticRulePreferenceController extends |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 44 | AbstractZenModePreferenceController implements PreferenceControllerMixin { |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 45 | |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 46 | protected ZenModeBackend mBackend; |
| 47 | protected Fragment mParent; |
| 48 | protected Set<Map.Entry<String, AutomaticZenRule>> mRules; |
| 49 | protected PackageManager mPm; |
| Beverly | 2c5895d | 2017-12-05 15:50:26 -0500 | [diff] [blame] | 50 | private static List<String> mDefaultRuleIds; |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 51 | |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 52 | public AbstractZenModeAutomaticRulePreferenceController(Context context, String key, Fragment |
| 53 | parent, Lifecycle lifecycle) { |
| 54 | super(context, key, lifecycle); |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 55 | mBackend = ZenModeBackend.getInstance(context); |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 56 | mPm = mContext.getPackageManager(); |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 57 | mParent = parent; |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void updateState(Preference preference) { |
| 62 | super.updateState(preference); |
| 63 | mRules = getZenModeRules(); |
| 64 | } |
| 65 | |
| Beverly | 2c5895d | 2017-12-05 15:50:26 -0500 | [diff] [blame] | 66 | private static List<String> getDefaultRuleIds() { |
| 67 | if (mDefaultRuleIds == null) { |
| 68 | mDefaultRuleIds = ZenModeConfig.DEFAULT_RULE_IDS; |
| 69 | } |
| 70 | return mDefaultRuleIds; |
| 71 | } |
| 72 | |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 73 | private Set<Map.Entry<String, AutomaticZenRule>> getZenModeRules() { |
| 74 | Map<String, AutomaticZenRule> ruleMap = |
| 75 | NotificationManager.from(mContext).getAutomaticZenRules(); |
| 76 | return ruleMap.entrySet(); |
| 77 | } |
| 78 | |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 79 | protected void showNameRuleDialog(final ZenRuleInfo ri, Fragment parent) { |
| 80 | ZenRuleNameDialog.show(parent, null, ri.defaultConditionId, new |
| 81 | RuleNameChangeListener(ri)); |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 82 | } |
| 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) { |
| Beverly | 2c5895d | 2017-12-05 15:50:26 -0500 | [diff] [blame] | 112 | // 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 | |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 119 | 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 | } |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 168 | |
| 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 | } |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 190 | } |