• 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, ZenModeBackend backend)61     public ZenRulePreference(Context context,
62             final Map.Entry<String, AutomaticZenRule> ruleEntry,
63             Fragment parent, MetricsFeatureProvider metricsProvider,
64             ZenModeBackend backend) {
65         super(context);
66         mBackend = backend;
67         mContext = context;
68         mRule = ruleEntry.getValue();
69         mName = mRule.getName();
70         mId = ruleEntry.getKey();
71         setKey(mId);
72         mParent = parent;
73         mPm = mContext.getPackageManager();
74         mServiceListing = new ZenServiceListing(mContext, CONFIG);
75         mServiceListing.reloadApprovedServices();
76         mPref = this;
77         mMetricsFeatureProvider = metricsProvider;
78         setAttributes(mRule);
79         setWidgetLayoutResource(getSecondTargetResId());
80 
81         // initialize the checked state of the preference
82         super.setChecked(mRule.isEnabled());
83     }
84 
updatePreference(AutomaticZenRule rule)85     public void updatePreference(AutomaticZenRule rule) {
86         if (!mRule.getName().equals(rule.getName())) {
87             mName = rule.getName();
88             setTitle(mName);
89         }
90 
91         if (mRule.isEnabled() != rule.isEnabled()) {
92             setChecked(rule.isEnabled());
93         }
94         setSummary(computeRuleSummary(rule));
95         mRule = rule;
96     }
97 
98     @Override
onClick()99     public void onClick() {
100         if (mIntent != null) {
101             mContext.startActivity(mIntent);
102         }
103     }
104 
105     @Override
setChecked(boolean checked)106     public void setChecked(boolean checked) {
107         mRule.setEnabled(checked);
108         mBackend.updateZenRule(mId, mRule);
109         setAttributes(mRule);
110         super.setChecked(checked);
111     }
112 
setAttributes(AutomaticZenRule rule)113     protected void setAttributes(AutomaticZenRule rule) {
114         final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
115                 rule.getConditionId(), true);
116         final boolean isEvent = ZenModeConfig.isValidEventConditionId(rule.getConditionId());
117 
118         setSummary(computeRuleSummary(rule));
119 
120         setTitle(mName);
121         setPersistent(false);
122 
123         final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION
124                 : isEvent ? ZenModeEventRuleSettings.ACTION : "";
125         ComponentInfo si = mServiceListing.findService(rule.getOwner());
126         ComponentName settingsActivity = AbstractZenModeAutomaticRulePreferenceController.
127                 getSettingsActivity(mPm, rule, si);
128         mIntent = AbstractZenModeAutomaticRulePreferenceController.getRuleIntent(action,
129                 settingsActivity, mId);
130         // If the intent's activity for this rule doesn't exist or resolve to anything, disable the
131         // preference and rule.
132         List<ResolveInfo> results = mPm.queryIntentActivities(
133                 mIntent, PackageManager.ResolveInfoFlags.of(0));
134         if (mIntent.resolveActivity(mPm) == null || results.size() == 0) {
135             Log.w(TAG, "intent for zen rule invalid: " + mIntent);
136             mIntent = null;
137             setEnabled(false);
138         }
139         setKey(mId);
140     }
141 
computeRuleSummary(AutomaticZenRule rule)142     private String computeRuleSummary(AutomaticZenRule rule) {
143         if (rule != null) {
144             // handle schedule-based rules
145             ScheduleInfo schedule =
146                     ZenModeConfig.tryParseScheduleConditionId(rule.getConditionId());
147             if (schedule != null) {
148                 String desc = mScheduleHelper.getDaysAndTimeSummary(mContext, schedule);
149                 return (desc != null) ? desc :
150                         mContext.getResources().getString(
151                                 R.string.zen_mode_schedule_rule_days_none);
152             }
153 
154             // handle event-based rules
155             ZenModeConfig.EventInfo event =
156                     ZenModeConfig.tryParseEventConditionId(rule.getConditionId());
157             if (event != null) {
158                 if (event.calName != null) {
159                     return event.calName;
160                 } else {
161                     return mContext.getResources().getString(
162                             R.string.zen_mode_event_rule_calendar_any);
163                 }
164             }
165         }
166 
167         return (rule == null || !rule.isEnabled())
168                 ? mContext.getResources().getString(R.string.switch_off_text)
169                 : mContext.getResources().getString(R.string.switch_on_text);
170     }
171 }
172