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.zen;
18 
19 import static android.view.accessibility.AccessibilityEvent.TYPE_VIEW_FOCUSED;
20 
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.provider.Settings;
24 import android.view.View;
25 import android.widget.Button;
26 
27 import androidx.fragment.app.FragmentManager;
28 import androidx.preference.Preference;
29 
30 import com.android.settings.R;
31 import com.android.settings.core.PreferenceControllerMixin;
32 import com.android.settings.dashboard.DashboardFragment;
33 import com.android.settings.notification.SettingsEnableZenModeDialog;
34 import com.android.settingslib.core.lifecycle.Lifecycle;
35 import com.android.settingslib.widget.LayoutPreference;
36 
37 public class ZenModeButtonPreferenceController extends AbstractZenModePreferenceController
38         implements PreferenceControllerMixin {
39 
40     public static final String KEY = "zen_mode_toggle";
41 
42     private static final String TAG = "EnableZenModeButton";
43     private final FragmentManager mFragment;
44 
45     // DND can also be toggled from QS. If DND wasn't toggled by this preference, don't
46     // reroute focus.
47     private boolean mRefocusButton = false;
48     private Button mZenButtonOn;
49     private Button mZenButtonOff;
50 
ZenModeButtonPreferenceController(Context context, Lifecycle lifecycle, FragmentManager fragment)51     public ZenModeButtonPreferenceController(Context context, Lifecycle lifecycle, FragmentManager
52             fragment) {
53         super(context, KEY, lifecycle);
54         mFragment = fragment;
55     }
56 
57     @Override
isAvailable()58     public boolean isAvailable() {
59         return true;
60     }
61 
62     @Override
getPreferenceKey()63     public String getPreferenceKey() {
64         return KEY;
65     }
66 
67     @Override
updateState(Preference preference)68     public void updateState(Preference preference) {
69         super.updateState(preference);
70 
71         if (null == mZenButtonOn) {
72             mZenButtonOn = ((LayoutPreference) preference)
73                     .findViewById(R.id.zen_mode_settings_turn_on_button);
74             updateZenButtonOnClickListener(preference);
75         }
76 
77         if (null == mZenButtonOff) {
78             mZenButtonOff = ((LayoutPreference) preference)
79                     .findViewById(R.id.zen_mode_settings_turn_off_button);
80             mZenButtonOff.setOnClickListener(v -> {
81                 mRefocusButton = true;
82                 writeMetrics(preference, false);
83                 mBackend.setZenMode(Settings.Global.ZEN_MODE_OFF);
84             });
85         }
86 
87         updatePreference(preference);
88     }
89 
updatePreference(Preference preference)90     private void updatePreference(Preference preference) {
91         switch (getZenMode()) {
92             case Settings.Global.ZEN_MODE_ALARMS:
93             case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
94             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
95                 mZenButtonOff.setVisibility(View.VISIBLE);
96                 mZenButtonOn.setVisibility(View.GONE);
97                 if (mRefocusButton) {
98                     mRefocusButton = false;
99                     mZenButtonOff.sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
100                 }
101                 break;
102             case Settings.Global.ZEN_MODE_OFF:
103             default:
104                 mZenButtonOff.setVisibility(View.GONE);
105                 updateZenButtonOnClickListener(preference);
106                 mZenButtonOn.setVisibility(View.VISIBLE);
107                 if (mRefocusButton) {
108                     mRefocusButton = false;
109                     mZenButtonOn.sendAccessibilityEvent(TYPE_VIEW_FOCUSED);
110                 }
111         }
112     }
113 
updateZenButtonOnClickListener(Preference preference)114     private void updateZenButtonOnClickListener(Preference preference) {
115         mZenButtonOn.setOnClickListener(v -> {
116             mRefocusButton = true;
117             writeMetrics(preference, true);
118             int zenDuration = getZenDuration();
119             switch (zenDuration) {
120                 case Settings.Secure.ZEN_DURATION_PROMPT:
121                     new SettingsEnableZenModeDialog().show(mFragment, TAG);
122                     break;
123                 case Settings.Secure.ZEN_DURATION_FOREVER:
124                     mBackend.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
125                     break;
126                 default:
127                     mBackend.setZenModeForDuration(zenDuration);
128             }
129         });
130     }
131 
writeMetrics(Preference preference, boolean buttonOn)132     private void writeMetrics(Preference preference, boolean buttonOn) {
133         mMetricsFeatureProvider.logClickedPreference(preference,
134                 preference.getExtras().getInt(DashboardFragment.CATEGORY));
135         mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ZEN_TOGGLE_DND_BUTTON,
136                 buttonOn);
137     }
138 }