1 /*
2  * Copyright (C) 2020 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 static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_ANYONE;
20 import static android.app.NotificationManager.Policy.CONVERSATION_SENDERS_NONE;
21 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS;
22 import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_ANY;
23 
24 import android.app.NotificationManager;
25 import android.content.Context;
26 import android.provider.Settings;
27 
28 import androidx.preference.Preference;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.PreferenceControllerMixin;
32 import com.android.settingslib.core.lifecycle.Lifecycle;
33 
34 /**
35  * Controls the summary for preference found at:
36  *  Settings > Sound > Do Not Disturb > People
37  */
38 public class ZenModePeoplePreferenceController extends
39         AbstractZenModePreferenceController implements PreferenceControllerMixin {
40 
41     private final String KEY;
42 
ZenModePeoplePreferenceController(Context context, Lifecycle lifecycle, String key)43     public ZenModePeoplePreferenceController(Context context, Lifecycle lifecycle, String key) {
44         super(context, key, lifecycle);
45         KEY = key;
46     }
47 
48     @Override
getPreferenceKey()49     public String getPreferenceKey() {
50         return KEY;
51     }
52 
53     @Override
isAvailable()54     public boolean isAvailable() {
55         return true;
56     }
57 
58     @Override
updateState(Preference preference)59     public void updateState(Preference preference) {
60         super.updateState(preference);
61 
62         switch (getZenMode()) {
63             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
64             case Settings.Global.ZEN_MODE_ALARMS:
65                 preference.setEnabled(false);
66                 preference.setSummary(mBackend.getAlarmsTotalSilencePeopleSummary(
67                         NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES));
68                 break;
69             default:
70                 preference.setEnabled(true);
71                 preference.setSummary(getPeopleSummary());
72         }
73     }
74 
getPeopleSummary()75     private String getPeopleSummary() {
76         final int callersAllowed = mBackend.getPriorityCallSenders();
77         final int messagesAllowed = mBackend.getPriorityMessageSenders();
78         final int conversationsAllowed = mBackend.getPriorityConversationSenders();
79         final boolean areRepeatCallersAllowed =
80                 mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_REPEAT_CALLERS);
81 
82         if (callersAllowed == PRIORITY_SENDERS_ANY
83                 && messagesAllowed == PRIORITY_SENDERS_ANY
84                 && conversationsAllowed == CONVERSATION_SENDERS_ANYONE) {
85             return mContext.getResources().getString(R.string.zen_mode_people_all);
86         } else if (callersAllowed == ZenModeBackend.SOURCE_NONE
87                 && messagesAllowed == ZenModeBackend.SOURCE_NONE
88                 && conversationsAllowed == CONVERSATION_SENDERS_NONE
89                 && !areRepeatCallersAllowed) {
90             return mContext.getResources().getString(R.string.zen_mode_people_none);
91         } else {
92             return mContext.getResources().getString(R.string.zen_mode_people_some);
93         }
94     }
95 }
96