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.view.ViewGroup;
26import android.widget.Checkable;
27import android.widget.LinearLayout;
28import android.widget.TextView;
29
30import com.android.systemui.Prefs;
31import com.android.systemui.R;
32import com.android.systemui.statusbar.policy.ZenModeController;
33import com.android.systemui.volume.ZenModePanel;
34import com.android.systemui.volume.ZenModePanel.Callback;
35
36public 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
50    public TunerZenModePanel(Context context, @Nullable AttributeSet attrs) {
51        super(context, attrs);
52    }
53
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
78    protected void onDetachedFromWindow() {
79        super.onDetachedFromWindow();
80        mEditing = false;
81    }
82
83    public void setCallback(Callback zenPanelCallback) {
84        mCallback = zenPanelCallback;
85        mZenModePanel.setCallback(zenPanelCallback);
86    }
87
88    @Override
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
113    public boolean isEditing() {
114        return mEditing;
115    }
116
117    public void setZenState(int zenMode) {
118        mZenMode = zenMode;
119        postUpdatePanel();
120    }
121
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
130    public void setDoneListener(OnClickListener onClickListener) {
131        mDoneListener = onClickListener;
132    }
133
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