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