• 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.zen;
18 
19 import android.app.AutomaticZenRule;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.ComponentInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.service.notification.ZenModeConfig;
27 import android.service.notification.ZenModeConfig.ScheduleInfo;
28 import android.util.Log;
29 
30 import androidx.fragment.app.Fragment;
31 import androidx.preference.Preference;
32 
33 import com.android.settings.R;
34 import com.android.settings.utils.ManagedServiceSettings;
35 import com.android.settings.utils.ZenServiceListing;
36 import com.android.settingslib.PrimarySwitchPreference;
37 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
38 
39 import java.util.List;
40 import java.util.Map;
41 
42 public class ZenRulePreference extends PrimarySwitchPreference {
43     private static final String TAG = "ZenRulePreference";
44     private static final ManagedServiceSettings.Config CONFIG =
45             ZenModeAutomationSettings.getConditionProviderConfig();
46     final String mId;
47     final Fragment mParent;
48     final Preference mPref;
49     final Context mContext;
50     final ZenModeBackend mBackend;
51     final ZenServiceListing mServiceListing;
52     final PackageManager mPm;
53     final MetricsFeatureProvider mMetricsFeatureProvider;
54     AutomaticZenRule mRule;
55     CharSequence mName;
56 
57     private Intent mIntent;
58 
59     private final ZenRuleScheduleHelper mScheduleHelper = new ZenRuleScheduleHelper();
60 
ZenRulePreference(Context context, final Map.Entry<String, AutomaticZenRule> ruleEntry, Fragment parent, MetricsFeatureProvider metricsProvider)61     public ZenRulePreference(Context context,
62             final Map.Entry<String, AutomaticZenRule> ruleEntry,
63             Fragment parent, MetricsFeatureProvider metricsProvider) {
64         super(context);
65         mBackend = ZenModeBackend.getInstance(context);
66         mContext = context;
67         mRule = ruleEntry.getValue();
68         mName = mRule.getName();
69         mId = ruleEntry.getKey();
70         mParent = parent;
71         mPm = mContext.getPackageManager();
72         mServiceListing = new ZenServiceListing(mContext, CONFIG);
73         mServiceListing.reloadApprovedServices();
74         mPref = this;
75         mMetricsFeatureProvider = metricsProvider;
76         setAttributes(mRule);
77         setWidgetLayoutResource(getSecondTargetResId());
78 
79         // initialize the checked state of the preference
80         super.setChecked(mRule.isEnabled());
81     }
82 
updatePreference(AutomaticZenRule rule)83     public void updatePreference(AutomaticZenRule rule) {
84         if (!mRule.getName().equals(rule.getName())) {
85             mName = rule.getName();
86             setTitle(mName);
87         }
88 
89         if (mRule.isEnabled() != rule.isEnabled()) {
90             setChecked(rule.isEnabled());
91         }
92         setSummary(computeRuleSummary(rule));
93         mRule = rule;
94     }
95 
96     @Override
onClick()97     public void onClick() {
98         mContext.startActivity(mIntent);
99     }
100 
101     @Override
setChecked(boolean checked)102     public void setChecked(boolean checked) {
103         mRule.setEnabled(checked);
104         mBackend.updateZenRule(mId, mRule);
105         setAttributes(mRule);
106         super.setChecked(checked);
107     }
108 
setAttributes(AutomaticZenRule rule)109     protected void setAttributes(AutomaticZenRule rule) {
110         final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
111                 rule.getConditionId(), true);
112         final boolean isEvent = ZenModeConfig.isValidEventConditionId(rule.getConditionId());
113 
114         setSummary(computeRuleSummary(rule));
115 
116         setTitle(mName);
117         setPersistent(false);
118 
119         final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION
120                 : isEvent ? ZenModeEventRuleSettings.ACTION : "";
121         ComponentInfo si = mServiceListing.findService(rule.getOwner());
122         ComponentName settingsActivity = AbstractZenModeAutomaticRulePreferenceController.
123                 getSettingsActivity(mPm, rule, si);
124         mIntent = AbstractZenModeAutomaticRulePreferenceController.getRuleIntent(action,
125                 settingsActivity, mId);
126         // If the intent's activity for this rule doesn't exist or resolve to anything, disable the
127         // preference and rule.
128         List<ResolveInfo> results = mPm.queryIntentActivities(
129                 mIntent, PackageManager.ResolveInfoFlags.of(0));
130         if (mIntent.resolveActivity(mPm) == null || results.size() == 0) {
131             Log.w(TAG, "intent for zen rule invalid: " + mIntent);
132             mIntent = null;
133             setEnabled(false);
134         }
135         setKey(mId);
136     }
137 
computeRuleSummary(AutomaticZenRule rule)138     private String computeRuleSummary(AutomaticZenRule rule) {
139         if (rule != null) {
140             // handle schedule-based rules
141             ScheduleInfo schedule =
142                     ZenModeConfig.tryParseScheduleConditionId(rule.getConditionId());
143             if (schedule != null) {
144                 String desc = mScheduleHelper.getDaysAndTimeSummary(mContext, schedule);
145                 return (desc != null) ? desc :
146                         mContext.getResources().getString(
147                                 R.string.zen_mode_schedule_rule_days_none);
148             }
149 
150             // handle event-based rules
151             ZenModeConfig.EventInfo event =
152                     ZenModeConfig.tryParseEventConditionId(rule.getConditionId());
153             if (event != null) {
154                 if (event.calName != null) {
155                     return event.calName;
156                 } else {
157                     return mContext.getResources().getString(
158                             R.string.zen_mode_event_rule_calendar_any);
159                 }
160             }
161         }
162 
163         return (rule == null || !rule.isEnabled())
164                 ? mContext.getResources().getString(R.string.switch_off_text)
165                 : mContext.getResources().getString(R.string.switch_on_text);
166     }
167 }
168