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.NotificationManager.Policy;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.provider.Settings;
23 import android.util.Log;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceScreen;
27 import androidx.preference.SwitchPreference;
28 
29 import com.android.settings.R;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 
32 public class ZenModeRepeatCallersPreferenceController extends AbstractZenModePreferenceController
33         implements Preference.OnPreferenceChangeListener {
34 
35     protected static final String KEY = "zen_mode_repeat_callers";
36 
37     private final ZenModeBackend mBackend;
38     private final int mRepeatCallersThreshold;
39 
ZenModeRepeatCallersPreferenceController(Context context, Lifecycle lifecycle, int repeatCallersThreshold)40     public ZenModeRepeatCallersPreferenceController(Context context, Lifecycle lifecycle,
41             int repeatCallersThreshold) {
42         super(context, KEY, lifecycle);
43 
44         mRepeatCallersThreshold = repeatCallersThreshold;
45         mBackend = ZenModeBackend.getInstance(context);
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
displayPreference(PreferenceScreen screen)59     public void displayPreference(PreferenceScreen screen) {
60         super.displayPreference(screen);
61         setRepeatCallerSummary(screen.findPreference(KEY));
62     }
63 
64     @Override
updateState(Preference preference)65     public void updateState(Preference preference) {
66         super.updateState(preference);
67 
68         SwitchPreference pref = (SwitchPreference) preference;
69         switch (getZenMode()) {
70             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
71             case Settings.Global.ZEN_MODE_ALARMS:
72                 pref.setEnabled(false);
73                 pref.setChecked(false);
74                 break;
75             default:
76                 boolean anyCallersCanBypassDnd = (mBackend.isPriorityCategoryEnabled(
77                         Policy.PRIORITY_CATEGORY_CALLS)
78                         && mBackend.getPriorityCallSenders() == Policy.PRIORITY_SENDERS_ANY);
79                 // if any caller can bypass dnd then repeat callers preference is disabled
80                 if (anyCallersCanBypassDnd) {
81                     pref.setEnabled(false);
82                     pref.setChecked(true);
83                 } else {
84                     pref.setEnabled(true);
85                     pref.setChecked(mBackend.isPriorityCategoryEnabled(
86                             Policy.PRIORITY_CATEGORY_REPEAT_CALLERS));
87                 }
88         }
89     }
90 
91     @Override
onPreferenceChange(Preference preference, Object newValue)92     public boolean onPreferenceChange(Preference preference, Object newValue) {
93         final boolean allowRepeatCallers = (Boolean) newValue;
94         if (ZenModeSettingsBase.DEBUG) {
95             Log.d(TAG, "onPrefChange allowRepeatCallers=" + allowRepeatCallers);
96         }
97         mMetricsFeatureProvider.action(mContext,
98                 SettingsEnums.ACTION_ZEN_ALLOW_REPEAT_CALLS, allowRepeatCallers);
99         mBackend.saveSoundPolicy(Policy.PRIORITY_CATEGORY_REPEAT_CALLERS, allowRepeatCallers);
100         return true;
101     }
102 
setRepeatCallerSummary(Preference preference)103     private void setRepeatCallerSummary(Preference preference) {
104         preference.setSummary(mContext.getString(R.string.zen_mode_repeat_callers_summary,
105                 mRepeatCallersThreshold));
106     }
107 }
108