1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.tuner;
16 
17 import android.annotation.Nullable;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.provider.Settings;
21 import android.provider.Settings.Global;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.view.View.OnClickListener;
25 import android.view.ViewGroup;
26 import android.widget.Checkable;
27 import android.widget.LinearLayout;
28 import android.widget.TextView;
29 
30 import com.android.systemui.Prefs;
31 import com.android.systemui.R;
32 import com.android.systemui.statusbar.policy.ZenModeController;
33 import com.android.systemui.volume.ZenModePanel;
34 import com.android.systemui.volume.ZenModePanel.Callback;
35 
36 public class TunerZenModePanel extends LinearLayout implements OnClickListener {
37     private static final String TAG = "TunerZenModePanel";
38 
39     private Callback mCallback;
40     private ZenModePanel mZenModePanel;
41     private View mHeaderSwitch;
42     private int mZenMode;
43     private ZenModeController mController;
44     private View mButtons;
45     private View mMoreSettings;
46     private View mDone;
47     private OnClickListener mDoneListener;
48     private boolean mEditing;
49 
TunerZenModePanel(Context context, @Nullable AttributeSet attrs)50     public TunerZenModePanel(Context context, @Nullable AttributeSet attrs) {
51         super(context, attrs);
52     }
53 
init(ZenModeController zenModeController)54     public void init(ZenModeController zenModeController) {
55         mController = zenModeController;
56         mHeaderSwitch = findViewById(R.id.tuner_zen_switch);
57         mHeaderSwitch.setVisibility(View.VISIBLE);
58         mHeaderSwitch.setOnClickListener(this);
59         ((TextView) mHeaderSwitch.findViewById(android.R.id.title)).setText(
60                 R.string.quick_settings_dnd_label);
61         mZenModePanel = (ZenModePanel) findViewById(R.id.zen_mode_panel);
62         mZenModePanel.init(zenModeController);
63         mButtons = findViewById(R.id.tuner_zen_buttons);
64         mMoreSettings = mButtons.findViewById(android.R.id.button2);
65         mMoreSettings.setOnClickListener(this);
66         ((TextView) mMoreSettings).setText(R.string.quick_settings_more_settings);
67         mDone = mButtons.findViewById(android.R.id.button1);
68         mDone.setOnClickListener(this);
69         ((TextView) mDone).setText(R.string.quick_settings_done);
70         // Hide the resizing space because it causes issues in the volume panel.
71         ViewGroup detail_header = findViewById(R.id.tuner_zen_switch);
72         detail_header.getChildAt(0).setVisibility(View.GONE);
73         // No background so it can blend with volume panel.
74         findViewById(R.id.edit_container).setBackground(null);
75     }
76 
77     @Override
onDetachedFromWindow()78     protected void onDetachedFromWindow() {
79         super.onDetachedFromWindow();
80         mEditing = false;
81     }
82 
setCallback(Callback zenPanelCallback)83     public void setCallback(Callback zenPanelCallback) {
84         mCallback = zenPanelCallback;
85         mZenModePanel.setCallback(zenPanelCallback);
86     }
87 
88     @Override
onClick(View v)89     public void onClick(View v) {
90         if (v == mHeaderSwitch) {
91             mEditing = true;
92             if (mZenMode == Global.ZEN_MODE_OFF) {
93                 mZenMode = Prefs.getInt(mContext, Prefs.Key.DND_FAVORITE_ZEN,
94                         Global.ZEN_MODE_ALARMS);
95                 mController.setZen(mZenMode, null, TAG);
96                 postUpdatePanel();
97             } else {
98                 mZenMode = Global.ZEN_MODE_OFF;
99                 mController.setZen(Global.ZEN_MODE_OFF, null, TAG);
100                 postUpdatePanel();
101             }
102         } else if (v == mMoreSettings) {
103             Intent intent = new Intent(Settings.ACTION_ZEN_MODE_SETTINGS);
104             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
105             getContext().startActivity(intent);
106         } else if (v == mDone) {
107             mEditing = false;
108             setVisibility(View.GONE);
109             mDoneListener.onClick(v);
110         }
111     }
112 
isEditing()113     public boolean isEditing() {
114         return mEditing;
115     }
116 
setZenState(int zenMode)117     public void setZenState(int zenMode) {
118         mZenMode = zenMode;
119         postUpdatePanel();
120     }
121 
postUpdatePanel()122     private void postUpdatePanel() {
123         // The complicated structure from reusing the same ZenPanel has resulted in some
124         // unstableness/flickering from callbacks coming in quickly. To solve this just
125         // post the UI updates a little bit.
126         removeCallbacks(mUpdate);
127         postDelayed(mUpdate, 40);
128     }
129 
setDoneListener(OnClickListener onClickListener)130     public void setDoneListener(OnClickListener onClickListener) {
131         mDoneListener = onClickListener;
132     }
133 
updatePanel()134     private void updatePanel() {
135         boolean zenOn = mZenMode != Global.ZEN_MODE_OFF;
136         ((Checkable) mHeaderSwitch.findViewById(android.R.id.toggle)).setChecked(zenOn);
137         mZenModePanel.setVisibility(zenOn ? View.VISIBLE : View.GONE);
138         mButtons.setVisibility(zenOn ? View.VISIBLE : View.GONE);
139     }
140 
141     private final Runnable mUpdate = new Runnable() {
142         @Override
143         public void run() {
144             updatePanel();
145         }
146     };
147 }
148