• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.NotificationManager;
21 import android.app.settings.SettingsEnums;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.ActivityInfo;
26 import android.content.pm.ComponentInfo;
27 import android.content.pm.PackageManager;
28 import android.content.pm.ServiceInfo;
29 import android.provider.Settings;
30 import android.service.notification.ConditionProviderService;
31 
32 import androidx.fragment.app.Fragment;
33 import androidx.preference.Preference;
34 
35 import com.android.settings.core.PreferenceControllerMixin;
36 import com.android.settingslib.core.lifecycle.Lifecycle;
37 
38 import java.util.Map;
39 
40 abstract public class AbstractZenModeAutomaticRulePreferenceController extends
41         AbstractZenModePreferenceController implements PreferenceControllerMixin {
42 
43     protected ZenModeBackend mBackend;
44     protected Fragment mParent;
45     protected Map.Entry<String, AutomaticZenRule>[] mRules;
46     protected PackageManager mPm;
47 
AbstractZenModeAutomaticRulePreferenceController(Context context, String key, Fragment parent, Lifecycle lifecycle)48     public AbstractZenModeAutomaticRulePreferenceController(Context context, String key, Fragment
49             parent, Lifecycle lifecycle) {
50         super(context, key, lifecycle);
51         mBackend = ZenModeBackend.getInstance(context);
52         mPm = mContext.getPackageManager();
53         mParent = parent;
54     }
55 
56     @Override
updateState(Preference preference)57     public void updateState(Preference preference) {
58         super.updateState(preference);
59         mRules = mBackend.getAutomaticZenRules();
60     }
61 
getRules()62     protected Map.Entry<String, AutomaticZenRule>[] getRules() {
63         if (mRules == null) {
64             mRules = mBackend.getAutomaticZenRules();
65         }
66         return mRules;
67     }
68 
showNameRuleDialog(final ZenRuleInfo ri, Fragment parent)69     protected void showNameRuleDialog(final ZenRuleInfo ri, Fragment parent) {
70         ZenRuleNameDialog.show(parent, null, ri.defaultConditionId, new
71                 RuleNameChangeListener(ri));
72     }
73 
getRuleIntent(String settingsAction, ComponentName configurationActivity, String ruleId)74     protected static Intent getRuleIntent(String settingsAction,
75             ComponentName configurationActivity, String ruleId) {
76         final Intent intent = new Intent()
77                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
78                 .putExtra(ConditionProviderService.EXTRA_RULE_ID, ruleId);
79         if (configurationActivity != null) {
80             intent.setComponent(configurationActivity);
81         } else {
82             intent.setAction(settingsAction);
83         }
84         return intent;
85     }
86 
getRuleInfo(PackageManager pm, ComponentInfo ci)87     public static ZenRuleInfo getRuleInfo(PackageManager pm, ComponentInfo ci) {
88         if (ci == null || ci.metaData == null) {
89             return null;
90         }
91         final String ruleType = (ci instanceof ServiceInfo)
92                 ? ci.metaData.getString(ConditionProviderService.META_DATA_RULE_TYPE)
93                 : ci.metaData.getString(NotificationManager.META_DATA_AUTOMATIC_RULE_TYPE);
94 
95         final ComponentName configurationActivity = getSettingsActivity(null, ci);
96         if (ruleType != null && !ruleType.trim().isEmpty() && configurationActivity != null) {
97             final ZenRuleInfo ri = new ZenRuleInfo();
98             ri.serviceComponent =
99                     (ci instanceof ServiceInfo) ? new ComponentName(ci.packageName, ci.name) : null;
100             ri.settingsAction = Settings.ACTION_ZEN_MODE_EXTERNAL_RULE_SETTINGS;
101             ri.title = ruleType;
102             ri.packageName = ci.packageName;
103             ri.configurationActivity = configurationActivity;
104             ri.packageLabel = ci.applicationInfo.loadLabel(pm);
105             ri.ruleInstanceLimit = (ci instanceof ServiceInfo)
106                     ? ci.metaData.getInt(ConditionProviderService.META_DATA_RULE_INSTANCE_LIMIT, -1)
107                     : ci.metaData.getInt(NotificationManager.META_DATA_RULE_INSTANCE_LIMIT, -1);
108             return ri;
109         }
110         return null;
111     }
112 
getSettingsActivity(AutomaticZenRule rule, ComponentInfo ci)113     protected static ComponentName getSettingsActivity(AutomaticZenRule rule, ComponentInfo ci) {
114         // prefer config activity on the rule itself; fallback to manifest definition
115         if (rule != null && rule.getConfigurationActivity() != null) {
116             return rule.getConfigurationActivity();
117         }
118         if (ci == null) {
119             return null;
120         }
121         // new activity backed rule
122         if (ci instanceof ActivityInfo) {
123             return new ComponentName(ci.packageName, ci.name);
124         }
125         // old service backed rule
126         if (ci.metaData != null) {
127             final String configurationActivity = ci.metaData.getString(
128                     ConditionProviderService.META_DATA_CONFIGURATION_ACTIVITY);
129             if (configurationActivity != null) {
130                 return ComponentName.unflattenFromString(configurationActivity);
131             }
132         }
133 
134         return null;
135     }
136 
137     public class RuleNameChangeListener implements ZenRuleNameDialog.PositiveClickListener {
138         ZenRuleInfo mRuleInfo;
139 
RuleNameChangeListener(ZenRuleInfo ruleInfo)140         public RuleNameChangeListener(ZenRuleInfo ruleInfo) {
141             mRuleInfo = ruleInfo;
142         }
143 
144         @Override
onOk(String ruleName, Fragment parent)145         public void onOk(String ruleName, Fragment parent) {
146             mMetricsFeatureProvider.action(mContext,
147                     SettingsEnums.ACTION_ZEN_MODE_RULE_NAME_CHANGE_OK);
148             AutomaticZenRule rule = new AutomaticZenRule(ruleName, mRuleInfo.serviceComponent,
149                     mRuleInfo.configurationActivity, mRuleInfo.defaultConditionId, null,
150                     NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
151             String savedRuleId = mBackend.addZenRule(rule);
152             if (savedRuleId != null) {
153                 parent.startActivity(getRuleIntent(mRuleInfo.settingsAction, null,
154                         savedRuleId));
155             }
156         }
157     }
158 }
159