• 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.Fragment;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ServiceInfo;
26 import android.service.notification.ZenModeConfig;
27 import android.support.v7.preference.Preference;
28 import android.support.v7.preference.PreferenceViewHolder;
29 import android.view.View;
30 
31 import com.android.internal.logging.nano.MetricsProto;
32 import com.android.settings.R;
33 import com.android.settings.utils.ManagedServiceSettings;
34 import com.android.settings.utils.ZenServiceListing;
35 import com.android.settingslib.TwoTargetPreference;
36 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
37 
38 import java.util.Map;
39 
40 public class ZenRulePreference extends TwoTargetPreference {
41     private static final ManagedServiceSettings.Config CONFIG =
42             ZenModeAutomationSettings.getConditionProviderConfig();
43     final CharSequence mName;
44     final String mId;
45     boolean appExists;
46     final Fragment mParent;
47     final Preference mPref;
48     final Context mContext;
49     final ZenModeBackend mBackend;
50     final ZenServiceListing mServiceListing;
51     final PackageManager mPm;
52     final MetricsFeatureProvider mMetricsFeatureProvider;
53 
ZenRulePreference(Context context, final Map.Entry<String, AutomaticZenRule> ruleEntry, Fragment parent, MetricsFeatureProvider metricsProvider)54     public ZenRulePreference(Context context,
55             final Map.Entry<String, AutomaticZenRule> ruleEntry,
56             Fragment parent, MetricsFeatureProvider metricsProvider) {
57         super(context);
58 
59         mBackend = ZenModeBackend.getInstance(context);
60         mContext = context;
61         final AutomaticZenRule rule = ruleEntry.getValue();
62         mName = rule.getName();
63         mId = ruleEntry.getKey();
64         mParent = parent;
65         mPm = mContext.getPackageManager();
66         mServiceListing = new ZenServiceListing(mContext, CONFIG);
67         mServiceListing.reloadApprovedServices();
68         mPref = this;
69         mMetricsFeatureProvider = metricsProvider;
70 
71         setAttributes(rule);
72     }
73 
74     @Override
getSecondTargetResId()75     protected int getSecondTargetResId() {
76         if (mId != null && ZenModeConfig.DEFAULT_RULE_IDS.contains(mId)) {
77             return 0;
78         }
79 
80         return R.layout.zen_rule_widget;
81     }
82 
83     @Override
onBindViewHolder(PreferenceViewHolder view)84     public void onBindViewHolder(PreferenceViewHolder view) {
85         super.onBindViewHolder(view);
86 
87         View v = view.findViewById(R.id.delete_zen_rule);
88         if (v != null) {
89             v.setOnClickListener(mDeleteListener);
90         }
91     }
92 
93     private final View.OnClickListener mDeleteListener = new View.OnClickListener() {
94         @Override
95         public void onClick(View v) {
96             showDeleteRuleDialog(mParent, mId, mName.toString());
97         }
98     };
99 
showDeleteRuleDialog(final Fragment parent, final String ruleId, final String ruleName)100     private void showDeleteRuleDialog(final Fragment parent, final String ruleId,
101             final String ruleName) {
102         ZenDeleteRuleDialog.show(parent, ruleName, ruleId,
103                 new ZenDeleteRuleDialog.PositiveClickListener() {
104                     @Override
105                     public void onOk(String id) {
106                         mMetricsFeatureProvider.action(mContext,
107                                 MetricsProto.MetricsEvent.ACTION_ZEN_DELETE_RULE_OK);
108                         mBackend.removeZenRule(id);
109                     }
110                 });
111     }
112 
setAttributes(AutomaticZenRule rule)113     protected void setAttributes(AutomaticZenRule rule) {
114         final boolean isSchedule = ZenModeConfig.isValidScheduleConditionId(
115                 rule.getConditionId());
116         final boolean isEvent = ZenModeConfig.isValidEventConditionId(rule.getConditionId());
117         final boolean isSystemRule = isSchedule || isEvent;
118 
119         try {
120             ApplicationInfo info = mPm.getApplicationInfo(rule.getOwner().getPackageName(), 0);
121             setSummary(computeRuleSummary(rule, isSystemRule, info.loadLabel(mPm)));
122         } catch (PackageManager.NameNotFoundException e) {
123             appExists = false;
124             return;
125         }
126 
127         appExists = true;
128         setTitle(rule.getName());
129         setPersistent(false);
130 
131         final String action = isSchedule ? ZenModeScheduleRuleSettings.ACTION
132                 : isEvent ? ZenModeEventRuleSettings.ACTION : "";
133         ServiceInfo si = mServiceListing.findService(rule.getOwner());
134         ComponentName settingsActivity = AbstractZenModeAutomaticRulePreferenceController.
135                 getSettingsActivity(si);
136         setIntent(AbstractZenModeAutomaticRulePreferenceController.getRuleIntent(action,
137                 settingsActivity, mId));
138         setSelectable(settingsActivity != null || isSystemRule);
139         setKey(mId);
140     }
141 
computeRuleSummary(AutomaticZenRule rule, boolean isSystemRule, CharSequence providerLabel)142     private String computeRuleSummary(AutomaticZenRule rule, boolean isSystemRule,
143             CharSequence providerLabel) {
144         return (rule == null || !rule.isEnabled())
145                 ? mContext.getResources().getString(R.string.switch_off_text)
146                 : mContext.getResources().getString(R.string.switch_on_text);
147     }
148 }
149