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 android.content.Context;
20 import android.service.notification.ZenModeConfig;
21 
22 import androidx.annotation.NonNull;
23 import androidx.preference.Preference;
24 import androidx.preference.TwoStatePreference;
25 
26 /**
27  * Preference controller controlling whether a time schedule-based mode ends at the next alarm.
28  */
29 class ZenModeExitAtAlarmPreferenceController extends
30         AbstractZenModePreferenceController implements Preference.OnPreferenceChangeListener {
31     private ZenModeConfig.ScheduleInfo mSchedule;
32 
ZenModeExitAtAlarmPreferenceController(Context context, String key, ZenModesBackend backend)33     ZenModeExitAtAlarmPreferenceController(Context context,
34             String key, ZenModesBackend backend) {
35         super(context, key, backend);
36     }
37 
38     @Override
updateState(Preference preference, @NonNull ZenMode zenMode)39     public void updateState(Preference preference, @NonNull ZenMode zenMode) {
40         mSchedule = ZenModeConfig.tryParseScheduleConditionId(zenMode.getRule().getConditionId());
41         ((TwoStatePreference) preference).setChecked(mSchedule.exitAtAlarm);
42     }
43 
44     @Override
onPreferenceChange(@onNull Preference preference, Object newValue)45     public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
46         final boolean exitAtAlarm = (Boolean) newValue;
47         if (mSchedule.exitAtAlarm != exitAtAlarm) {
48             mSchedule.exitAtAlarm = exitAtAlarm;
49             return saveMode(mode -> {
50                 mode.getRule().setConditionId(ZenModeConfig.toScheduleConditionId(mSchedule));
51                 return mode;
52             });
53         }
54         return false;
55     }
56 }
57