| 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; |
| 30 | import android.support.v7.preference.Preference; |
| 31 | |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 32 | import com.android.internal.logging.nano.MetricsProto; |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 33 | import com.android.settings.core.PreferenceControllerMixin; |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 34 | import com.android.settingslib.core.lifecycle.Lifecycle; |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 35 | |
| 36 | import java.util.Arrays; |
| 37 | import java.util.Comparator; |
| 38 | import java.util.Map; |
| 39 | import java.util.Set; |
| 40 | |
| 41 | abstract public class AbstractZenModeAutomaticRulePreferenceController extends |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 42 | AbstractZenModePreferenceController implements PreferenceControllerMixin { |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 43 | |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 44 | protected ZenModeBackend mBackend; |
| 45 | protected Fragment mParent; |
| 46 | protected Set<Map.Entry<String, AutomaticZenRule>> mRules; |
| 47 | protected PackageManager mPm; |
| 48 | |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 49 | public AbstractZenModeAutomaticRulePreferenceController(Context context, String key, Fragment |
| 50 | parent, Lifecycle lifecycle) { |
| 51 | super(context, key, lifecycle); |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 52 | mBackend = ZenModeBackend.getInstance(context); |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 53 | mPm = mContext.getPackageManager(); |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 54 | mParent = parent; |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 55 | } |
| 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 | |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 69 | protected void showNameRuleDialog(final ZenRuleInfo ri, Fragment parent) { |
| 70 | ZenRuleNameDialog.show(parent, null, ri.defaultConditionId, new |
| 71 | RuleNameChangeListener(ri)); |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 72 | } |
| 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 | } |
| Beverly | 3235221 | 2017-11-20 17:33:01 -0500 | [diff] [blame] | 151 | |
| 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 | } |
| Beverly | a58e52a | 2017-10-27 14:44:23 -0400 | [diff] [blame] | 173 | } |