• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.SettingsEnums;
20 import android.content.Context;
21 import android.provider.Settings;
22 import android.view.View;
23 import android.widget.Button;
24 
25 import androidx.fragment.app.FragmentManager;
26 import androidx.preference.Preference;
27 
28 import com.android.settings.R;
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 import com.android.settingslib.widget.LayoutPreference;
32 
33 public class ZenModeButtonPreferenceController extends AbstractZenModePreferenceController
34         implements PreferenceControllerMixin {
35 
36     public static final String KEY = "zen_mode_toggle";
37 
38     private static final String TAG = "EnableZenModeButton";
39     private final FragmentManager mFragment;
40     private Button mZenButtonOn;
41     private Button mZenButtonOff;
42 
ZenModeButtonPreferenceController(Context context, Lifecycle lifecycle, FragmentManager fragment)43     public ZenModeButtonPreferenceController(Context context, Lifecycle lifecycle, FragmentManager
44             fragment) {
45         super(context, KEY, lifecycle);
46         mFragment = fragment;
47     }
48 
49     @Override
isAvailable()50     public boolean isAvailable() {
51         return true;
52     }
53 
54     @Override
getPreferenceKey()55     public String getPreferenceKey() {
56         return KEY;
57     }
58 
59     @Override
updateState(Preference preference)60     public void updateState(Preference preference) {
61         super.updateState(preference);
62 
63         if (null == mZenButtonOn) {
64             mZenButtonOn = ((LayoutPreference) preference)
65                     .findViewById(R.id.zen_mode_settings_turn_on_button);
66             updateZenButtonOnClickListener();
67         }
68 
69         if (null == mZenButtonOff) {
70             mZenButtonOff = ((LayoutPreference) preference)
71                     .findViewById(R.id.zen_mode_settings_turn_off_button);
72             mZenButtonOff.setOnClickListener(v -> {
73                 mMetricsFeatureProvider.action(mContext,
74                         SettingsEnums.ACTION_ZEN_TOGGLE_DND_BUTTON, false);
75                 mBackend.setZenMode(Settings.Global.ZEN_MODE_OFF);
76             });
77         }
78 
79         updateButtons();
80     }
81 
updateButtons()82     private void updateButtons() {
83         switch (getZenMode()) {
84             case Settings.Global.ZEN_MODE_ALARMS:
85             case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
86             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
87                 mZenButtonOff.setVisibility(View.VISIBLE);
88                 mZenButtonOn.setVisibility(View.GONE);
89                 break;
90             case Settings.Global.ZEN_MODE_OFF:
91             default:
92                 mZenButtonOff.setVisibility(View.GONE);
93                 updateZenButtonOnClickListener();
94                 mZenButtonOn.setVisibility(View.VISIBLE);
95         }
96     }
97 
updateZenButtonOnClickListener()98     private void updateZenButtonOnClickListener() {
99         int zenDuration = getZenDuration();
100         switch (zenDuration) {
101             case Settings.Secure.ZEN_DURATION_PROMPT:
102                 mZenButtonOn.setOnClickListener(v -> {
103                     mMetricsFeatureProvider.action(mContext,
104                             SettingsEnums.ACTION_ZEN_TOGGLE_DND_BUTTON, false);
105                     new SettingsEnableZenModeDialog().show(mFragment, TAG);
106                 });
107                 break;
108             case Settings.Secure.ZEN_DURATION_FOREVER:
109                 mZenButtonOn.setOnClickListener(v -> {
110                     mMetricsFeatureProvider.action(mContext,
111                             SettingsEnums.ACTION_ZEN_TOGGLE_DND_BUTTON, false);
112                     mBackend.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
113                 });
114                 break;
115             default:
116                 mZenButtonOn.setOnClickListener(v -> {
117                     mMetricsFeatureProvider.action(mContext,
118                             SettingsEnums.ACTION_ZEN_TOGGLE_DND_BUTTON, false);
119                     mBackend.setZenModeForDuration(zenDuration);
120                 });
121         }
122     }
123 }