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