• 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.ComponentName;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.provider.Settings;
23 import android.service.notification.ZenModeConfig;
24 import android.support.v7.preference.Preference;
25 
26 import com.android.settings.R;
27 import com.android.settingslib.core.lifecycle.Lifecycle;
28 
29 public class ZenModeBehaviorFooterPreferenceController extends AbstractZenModePreferenceController {
30 
31     protected static final String KEY = "footer_preference";
32     private final int mTitleRes;
33 
ZenModeBehaviorFooterPreferenceController(Context context, Lifecycle lifecycle, int titleRes)34     public ZenModeBehaviorFooterPreferenceController(Context context, Lifecycle lifecycle,
35             int titleRes) {
36         super(context, KEY, lifecycle);
37         mTitleRes = titleRes;
38     }
39 
40     @Override
isAvailable()41     public boolean isAvailable() {
42         return true;
43     }
44 
45     @Override
getPreferenceKey()46     public String getPreferenceKey() {
47         return KEY;
48     }
49 
50     @Override
updateState(Preference preference)51     public void updateState(Preference preference) {
52         super.updateState(preference);
53         preference.setTitle(getFooterText());
54     }
55 
getFooterText()56     protected String getFooterText() {
57         if (isDeprecatedZenMode(getZenMode())) {
58             ZenModeConfig config = getZenModeConfig();
59 
60             // DND turned on by manual rule with deprecated zen mode
61             if (config.manualRule != null &&
62                     isDeprecatedZenMode(config.manualRule.zenMode)) {
63                 final Uri id = config.manualRule.conditionId;
64                 if (config.manualRule.enabler != null) {
65                     // app triggered manual rule
66                     String appOwner = mZenModeConfigWrapper.getOwnerCaption(
67                             config.manualRule.enabler);
68                     if (!appOwner.isEmpty()) {
69                         return mContext.getString(R.string.zen_mode_app_set_behavior, appOwner);
70                     }
71                 } else {
72                     return mContext.getString(R.string.zen_mode_qs_set_behavior);
73                 }
74             }
75 
76             // DND turned on by an automatic rule with deprecated zen mode
77             for (ZenModeConfig.ZenRule automaticRule : config.automaticRules.values()) {
78                 if (automaticRule.isAutomaticActive() && isDeprecatedZenMode(
79                         automaticRule.zenMode)) {
80                     ComponentName component = automaticRule.component;
81                     if (component != null) {
82                         return mContext.getString(R.string.zen_mode_app_set_behavior,
83                                 component.getPackageName());
84                     }
85                 }
86             }
87 
88             return mContext.getString(R.string.zen_mode_unknown_app_set_behavior);
89         } else {
90             return mContext.getString(mTitleRes);
91         }
92     }
93 
isDeprecatedZenMode(int zenMode)94     private boolean isDeprecatedZenMode(int zenMode) {
95         switch (zenMode) {
96             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
97             case Settings.Global.ZEN_MODE_ALARMS:
98                 return true;
99             default:
100                 return false;
101         }
102     }
103 }