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.PRIORITY_CATEGORY_ALARMS;
20 import static android.service.notification.ZenPolicy.PRIORITY_CATEGORY_EVENTS;
21 import static android.service.notification.ZenPolicy.PRIORITY_CATEGORY_MEDIA;
22 import static android.service.notification.ZenPolicy.PRIORITY_CATEGORY_REMINDERS;
23 import static android.service.notification.ZenPolicy.PRIORITY_CATEGORY_SYSTEM;
24 
25 import android.content.Context;
26 
27 import androidx.annotation.NonNull;
28 import androidx.preference.Preference;
29 import androidx.preference.TwoStatePreference;
30 
31 class ZenModeOtherPreferenceController extends AbstractZenModePreferenceController
32         implements Preference.OnPreferenceChangeListener {
33 
ZenModeOtherPreferenceController(Context context, String key, ZenModesBackend backend)34     public ZenModeOtherPreferenceController(Context context, String key,
35             ZenModesBackend backend) {
36         super(context, key, backend);
37     }
38 
39     @Override
updateState(Preference preference, @NonNull ZenMode zenMode)40     public void updateState(Preference preference, @NonNull ZenMode zenMode) {
41         TwoStatePreference pref = (TwoStatePreference) preference;
42         pref.setChecked(zenMode.getPolicy().isCategoryAllowed(getCategory(), true));
43     }
44 
45     @Override
onPreferenceChange(Preference preference, Object newValue)46     public boolean onPreferenceChange(Preference preference, Object newValue) {
47         final boolean allow = (Boolean) newValue;
48         return savePolicy(policy -> policy.allowCategory(getCategory(), allow));
49     }
50 
getCategory()51     private int getCategory() {
52         switch (getPreferenceKey()) {
53             case "modes_category_alarm":
54                 return PRIORITY_CATEGORY_ALARMS;
55             case "modes_category_media":
56                 return PRIORITY_CATEGORY_MEDIA;
57             case "modes_category_system":
58                 return PRIORITY_CATEGORY_SYSTEM;
59             case "modes_category_reminders":
60                 return PRIORITY_CATEGORY_REMINDERS;
61             case "modes_category_events":
62                 return PRIORITY_CATEGORY_EVENTS;
63         }
64         return -1;
65     }
66 }
67