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.widget.Checkable;
26 import android.widget.LinearLayout;
27 import android.widget.TextView;
28 import com.android.systemui.Prefs;
29 import com.android.systemui.R;
30 import com.android.systemui.statusbar.policy.ZenModeController;
31 import com.android.systemui.volume.ZenModePanel;
32 import com.android.systemui.volume.ZenModePanel.Callback;
33 
34 public class TunerZenModePanel extends LinearLayout implements OnClickListener {
35     private static final String TAG = "TunerZenModePanel";
36 
37     private Callback mCallback;
38     private ZenModePanel mZenModePanel;
39     private View mHeaderSwitch;
40     private int mZenMode;
41     private ZenModeController mController;
42     private View mButtons;
43     private View mMoreSettings;
44     private View mDone;
45     private OnClickListener mDoneListener;
46     private boolean mEditing;
47 
TunerZenModePanel(Context context, @Nullable AttributeSet attrs)48     public TunerZenModePanel(Context context, @Nullable AttributeSet attrs) {
49         super(context, attrs);
50     }
51 
init(ZenModeController zenModeController)52     public void init(ZenModeController zenModeController) {
53         mController = zenModeController;
54         mHeaderSwitch = findViewById(R.id.tuner_zen_switch);
55         mHeaderSwitch.setVisibility(View.VISIBLE);
56         mHeaderSwitch.setOnClickListener(this);
57         mHeaderSwitch.findViewById(com.android.internal.R.id.up).setVisibility(View.GONE);
58         ((TextView) mHeaderSwitch.findViewById(android.R.id.title)).setText(
59                 R.string.quick_settings_dnd_label);
60         mZenModePanel = (ZenModePanel) findViewById(R.id.zen_mode_panel);
61         mZenModePanel.init(zenModeController);
62         mButtons = findViewById(R.id.tuner_zen_buttons);
63         mMoreSettings = mButtons.findViewById(android.R.id.button2);
64         mMoreSettings.setOnClickListener(this);
65         ((TextView) mMoreSettings).setText(R.string.quick_settings_more_settings);
66         mDone = mButtons.findViewById(android.R.id.button1);
67         mDone.setOnClickListener(this);
68         ((TextView) mDone).setText(R.string.quick_settings_done);
69     }
70 
71     @Override
onDetachedFromWindow()72     protected void onDetachedFromWindow() {
73         super.onDetachedFromWindow();
74         mEditing = false;
75     }
76 
setCallback(Callback zenPanelCallback)77     public void setCallback(Callback zenPanelCallback) {
78         mCallback = zenPanelCallback;
79         mZenModePanel.setCallback(zenPanelCallback);
80     }
81 
82     @Override
onClick(View v)83     public void onClick(View v) {
84         if (v == mHeaderSwitch) {
85             mEditing = true;
86             if (mZenMode == Global.ZEN_MODE_OFF) {
87                 mZenMode = Prefs.getInt(mContext, Prefs.Key.DND_FAVORITE_ZEN,
88                         Global.ZEN_MODE_ALARMS);
89                 mController.setZen(mZenMode, null, TAG);
90                 postUpdatePanel();
91             } else {
92                 mZenMode = Global.ZEN_MODE_OFF;
93                 mController.setZen(Global.ZEN_MODE_OFF, null, TAG);
94                 postUpdatePanel();
95             }
96         } else if (v == mMoreSettings) {
97             Intent intent = new Intent(Settings.ACTION_ZEN_MODE_SETTINGS);
98             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
99             getContext().startActivity(intent);
100         } else if (v == mDone) {
101             mEditing = false;
102             setVisibility(View.GONE);
103             mDoneListener.onClick(v);
104         }
105     }
106 
isEditing()107     public boolean isEditing() {
108         return mEditing;
109     }
110 
setZenState(int zenMode)111     public void setZenState(int zenMode) {
112         mZenMode = zenMode;
113         postUpdatePanel();
114     }
115 
postUpdatePanel()116     private void postUpdatePanel() {
117         // The complicated structure from reusing the same ZenPanel has resulted in some
118         // unstableness/flickering from callbacks coming in quickly. To solve this just
119         // post the UI updates a little bit.
120         removeCallbacks(mUpdate);
121         postDelayed(mUpdate, 40);
122     }
123 
setDoneListener(OnClickListener onClickListener)124     public void setDoneListener(OnClickListener onClickListener) {
125         mDoneListener = onClickListener;
126     }
127 
updatePanel()128     private void updatePanel() {
129         boolean zenOn = mZenMode != Global.ZEN_MODE_OFF;
130         ((Checkable) mHeaderSwitch.findViewById(android.R.id.toggle)).setChecked(zenOn);
131         mZenModePanel.setVisibility(zenOn ? View.VISIBLE : View.GONE);
132         mButtons.setVisibility(zenOn ? View.VISIBLE : View.GONE);
133     }
134 
135     private final Runnable mUpdate = new Runnable() {
136         @Override
137         public void run() {
138             updatePanel();
139         }
140     };
141 }
142