• 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.content.Context;
20 import android.net.Uri;
21 import android.provider.Settings;
22 import android.service.notification.ZenModeConfig;
23 import android.support.v7.preference.Preference;
24 
25 import com.android.settings.R;
26 import com.android.settingslib.core.lifecycle.Lifecycle;
27 
28 public class ZenModeSettingsFooterPreferenceController extends AbstractZenModePreferenceController {
29 
30     protected static final String KEY = "footer_preference";
31 
ZenModeSettingsFooterPreferenceController(Context context, Lifecycle lifecycle)32     public ZenModeSettingsFooterPreferenceController(Context context, Lifecycle lifecycle) {
33         super(context, KEY, lifecycle);
34     }
35 
36     @Override
isAvailable()37     public boolean isAvailable() {
38         switch(getZenMode()) {
39             case Settings.Global.ZEN_MODE_ALARMS:
40             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
41             case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
42                 return true;
43             case Settings.Global.ZEN_MODE_OFF:
44             default:
45                 return false;
46         }
47     }
48 
49     @Override
getPreferenceKey()50     public String getPreferenceKey() {
51         return KEY;
52     }
53 
54     @Override
updateState(Preference preference)55     public void updateState(Preference preference) {
56         super.updateState(preference);
57 
58         boolean isAvailable = isAvailable();
59         preference.setVisible(isAvailable);
60         if (isAvailable) {
61             preference.setTitle(getFooterText());
62         }
63     }
64 
getFooterText()65     protected String getFooterText() {
66         ZenModeConfig config = getZenModeConfig();
67         String footerText = "";
68         long latestEndTime = -1;
69 
70         // DND turned on by manual rule
71         if (config.manualRule != null) {
72             final Uri id = config.manualRule.conditionId;
73             if (config.manualRule.enabler != null) {
74                 // app triggered manual rule
75                 String appOwner = mZenModeConfigWrapper.getOwnerCaption(config.manualRule.enabler);
76                 if (!appOwner.isEmpty()) {
77                     footerText = mContext.getString(
78                             R.string.zen_mode_settings_dnd_automatic_rule_app, appOwner);
79                 }
80             } else {
81                 if (id == null) {
82                     return mContext.getString(
83                             R.string.zen_mode_settings_dnd_manual_indefinite);
84                 } else {
85                     latestEndTime = mZenModeConfigWrapper.parseManualRuleTime(id);
86                     if (latestEndTime > 0) {
87                         final CharSequence formattedTime = mZenModeConfigWrapper.getFormattedTime(
88                                 latestEndTime, mContext.getUserId());
89                         footerText = mContext.getString(
90                                 R.string.zen_mode_settings_dnd_manual_end_time,
91                                 formattedTime);
92                     }
93                 }
94             }
95         }
96 
97         // DND turned on by an automatic rule
98         for (ZenModeConfig.ZenRule automaticRule : config.automaticRules.values()) {
99             if (automaticRule.isAutomaticActive()) {
100                 // set footer if 3rd party rule
101                 if (!mZenModeConfigWrapper.isTimeRule(automaticRule.conditionId)) {
102                     return mContext.getString(R.string.zen_mode_settings_dnd_automatic_rule,
103                             automaticRule.name);
104                 } else {
105                     // set footer if automatic rule end time is the latest active rule end time
106                     long endTime = mZenModeConfigWrapper.parseAutomaticRuleEndTime(
107                             automaticRule.conditionId);
108                     if (endTime > latestEndTime) {
109                         latestEndTime = endTime;
110                         footerText = mContext.getString(
111                                 R.string.zen_mode_settings_dnd_automatic_rule, automaticRule.name);
112                     }
113                 }
114             }
115         }
116         return footerText;
117     }
118 }
119