1 /*
2  * Copyright (C) 2024 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.modes;
18 
19 import static android.service.notification.ZenPolicy.PEOPLE_TYPE_ANYONE;
20 import static android.service.notification.ZenPolicy.STATE_ALLOW;
21 
22 import android.content.Context;
23 
24 import androidx.annotation.NonNull;
25 import androidx.preference.Preference;
26 import androidx.preference.TwoStatePreference;
27 
28 import com.android.settings.R;
29 
30 class ZenModeRepeatCallersPreferenceController extends AbstractZenModePreferenceController
31         implements Preference.OnPreferenceChangeListener {
32 
33     private final int mRepeatCallersThreshold;
34 
ZenModeRepeatCallersPreferenceController(Context context, String key, ZenModesBackend backend, int repeatCallersThreshold)35     public ZenModeRepeatCallersPreferenceController(Context context,
36             String key, ZenModesBackend backend, int repeatCallersThreshold) {
37         super(context, key, backend);
38 
39         mRepeatCallersThreshold = repeatCallersThreshold;
40     }
41 
42     @Override
updateState(Preference preference, @NonNull ZenMode zenMode)43     public void updateState(Preference preference, @NonNull ZenMode zenMode) {
44         TwoStatePreference pref = (TwoStatePreference) preference;
45 
46         boolean anyCallersCanBypassDnd =
47                 zenMode.getPolicy().getPriorityCategoryCalls() == STATE_ALLOW
48                 && zenMode.getPolicy().getPriorityCallSenders() == PEOPLE_TYPE_ANYONE;
49         // if any caller can bypass dnd then repeat callers preference is disabled
50         if (anyCallersCanBypassDnd) {
51             pref.setEnabled(false);
52             pref.setChecked(true);
53         } else {
54             pref.setEnabled(true);
55             pref.setChecked(
56                     zenMode.getPolicy().getPriorityCategoryRepeatCallers() == STATE_ALLOW);
57         }
58 
59         setRepeatCallerSummary(preference);
60     }
61 
62     @Override
onPreferenceChange(@onNull Preference preference, Object newValue)63     public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
64         final boolean allowRepeatCallers = (Boolean) newValue;
65         return savePolicy(policy -> policy.allowRepeatCallers(allowRepeatCallers));
66     }
67 
setRepeatCallerSummary(Preference preference)68     private void setRepeatCallerSummary(Preference preference) {
69         preference.setSummary(mContext.getString(R.string.zen_mode_repeat_callers_summary,
70                 mRepeatCallersThreshold));
71     }
72 }
73