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.annotation.NonNull;
20 import android.content.Context;
21 import android.widget.Button;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.settings.R;
26 import com.android.settingslib.widget.LayoutPreference;
27 
28 public class ZenModeButtonPreferenceController extends AbstractZenModePreferenceController {
29 
30     private Button mZenButton;
31 
ZenModeButtonPreferenceController(Context context, String key, ZenModesBackend backend)32     public ZenModeButtonPreferenceController(Context context, String key, ZenModesBackend backend) {
33         super(context, key, backend);
34     }
35 
36     @Override
isAvailable(ZenMode zenMode)37     public boolean isAvailable(ZenMode zenMode) {
38         return zenMode.getRule().isManualInvocationAllowed() && zenMode.getRule().isEnabled();
39     }
40 
41     @Override
updateState(Preference preference, @NonNull ZenMode zenMode)42     public void updateState(Preference preference, @NonNull ZenMode zenMode) {
43         if (mZenButton == null) {
44             mZenButton = ((LayoutPreference) preference).findViewById(R.id.activate_mode);
45         }
46         mZenButton.setOnClickListener(v -> {
47             if (zenMode.isActive()) {
48                 mBackend.deactivateMode(zenMode);
49             } else {
50                 mBackend.activateMode(zenMode, null);
51             }
52         });
53         if (zenMode.isActive()) {
54             mZenButton.setText(R.string.zen_mode_button_turn_off);
55         } else {
56             mZenButton.setText(R.string.zen_mode_button_turn_on);
57         }
58     }
59 }
60