summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/volume/ZenFooter.java
blob: ef8257c0a798aec5df5eaf0b25ebfebbff625a5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.systemui.volume;

import android.animation.LayoutTransition;
import android.animation.ValueAnimator;
import android.app.ActivityManager;
import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings.Global;
import android.service.notification.ZenModeConfig;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.android.systemui.R;
import com.android.systemui.statusbar.policy.ZenModeController;

import java.util.Objects;

/**
 * Switch bar + zen mode panel (conditions) attached to the bottom of the volume dialog.
 */
public class ZenFooter extends LinearLayout {
    private static final String TAG = Util.logTag(ZenFooter.class);

    private final Context mContext;
    private final float mSecondaryAlpha;
    private final LayoutTransition mLayoutTransition;

    private ZenModeController mController;
    private Switch mSwitch;
    private ZenModePanel mZenModePanel;
    private View mZenModePanelButtons;
    private View mZenModePanelMoreButton;
    private View mZenModePanelDoneButton;
    private View mSwitchBar;
    private View mSwitchBarIcon;
    private View mSummary;
    private TextView mSummaryLine1;
    private TextView mSummaryLine2;
    private boolean mFooterExpanded;
    private int mZen = -1;
    private ZenModeConfig mConfig;
    private Callback mCallback;

    public ZenFooter(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mSecondaryAlpha = getFloat(context.getResources(), R.dimen.volume_secondary_alpha);
        mLayoutTransition = new LayoutTransition();
        mLayoutTransition.setDuration(new ValueAnimator().getDuration() / 2);
        mLayoutTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
        mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
    }

    private static float getFloat(Resources r, int resId) {
        final TypedValue tv = new TypedValue();
        r.getValue(resId, tv, true);
        return tv.getFloat();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mSwitchBar = findViewById(R.id.volume_zen_switch_bar);
        mSwitchBarIcon = findViewById(R.id.volume_zen_switch_bar_icon);
        mSwitch = (Switch) findViewById(R.id.volume_zen_switch);
        mZenModePanel = (ZenModePanel) findViewById(R.id.zen_mode_panel);
        mZenModePanelButtons = findViewById(R.id.volume_zen_mode_panel_buttons);
        mZenModePanelMoreButton = findViewById(R.id.volume_zen_mode_panel_more);
        mZenModePanelDoneButton = findViewById(R.id.volume_zen_mode_panel_done);
        mSummary = findViewById(R.id.volume_zen_panel_summary);
        mSummaryLine1 = (TextView) findViewById(R.id.volume_zen_panel_summary_line_1);
        mSummaryLine2 = (TextView) findViewById(R.id.volume_zen_panel_summary_line_2);
    }

    public void init(ZenModeController controller, Callback callback) {
        mCallback = callback;
        mController = controller;
        mZenModePanel.init(controller);
        mZenModePanel.setEmbedded(true);
        mSwitch.setOnCheckedChangeListener(mCheckedListener);
        mController.addCallback(new ZenModeController.Callback() {
            @Override
            public void onZenChanged(int zen) {
                setZen(zen);
            }
            @Override
            public void onConfigChanged(ZenModeConfig config) {
                setConfig(config);
            }
        });
        mSwitchBar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mSwitch.setChecked(!mSwitch.isChecked());
            }
        });
        mZenModePanelMoreButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallback != null) {
                    mCallback.onSettingsClicked();
                }
            }
        });
        mZenModePanelDoneButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallback != null) {
                    mCallback.onDoneClicked();
                }
            }
        });
        mZen = mController.getZen();
        mConfig = mController.getConfig();
        update();
    }

    private void setZen(int zen) {
        if (mZen == zen) return;
        mZen = zen;
        update();
    }

    private void setConfig(ZenModeConfig config) {
        if (Objects.equals(mConfig, config)) return;
        mConfig = config;
        update();
    }

    public boolean isZen() {
        return isZenPriority() || isZenAlarms() || isZenNone();
    }

    private boolean isZenPriority() {
        return mZen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
    }

    private boolean isZenAlarms() {
        return mZen == Global.ZEN_MODE_ALARMS;
    }

    private boolean isZenNone() {
        return mZen == Global.ZEN_MODE_NO_INTERRUPTIONS;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        setLayoutTransition(null);
        setFooterExpanded(false);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        setLayoutTransition(mLayoutTransition);
    }

    private boolean setFooterExpanded(boolean expanded) {
        if (mFooterExpanded == expanded) return false;
        mFooterExpanded = expanded;
        update();
        if (mCallback != null) {
            mCallback.onFooterExpanded();
        }
        return true;
    }

    public boolean isFooterExpanded() {
        return mFooterExpanded;
    }

    public void update() {
        final boolean isZen = isZen();
        mSwitch.setOnCheckedChangeListener(null);
        mSwitch.setChecked(isZen);
        mSwitch.setOnCheckedChangeListener(mCheckedListener);
        Util.setVisOrGone(mZenModePanel, isZen && mFooterExpanded);
        Util.setVisOrGone(mZenModePanelButtons, isZen && mFooterExpanded);
        Util.setVisOrGone(mSummary, isZen && !mFooterExpanded);
        mSwitchBarIcon.setAlpha(isZen ? 1 : mSecondaryAlpha);
        final String line1 =
                isZenPriority() ? mContext.getString(R.string.interruption_level_priority)
                : isZenAlarms() ? mContext.getString(R.string.interruption_level_alarms)
                : isZenNone() ? mContext.getString(R.string.interruption_level_none)
                : null;
        Util.setText(mSummaryLine1, line1);
        final String line2 = ZenModeConfig.getConditionSummary(mContext, mConfig,
                ActivityManager.getCurrentUser());
        Util.setText(mSummaryLine2, line2);
    }

    private final OnCheckedChangeListener mCheckedListener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (D.BUG) Log.d(TAG, "onCheckedChanged " + isChecked);
            if (isChecked != isZen()) {
                final int newZen = isChecked ? Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS
                        : Global.ZEN_MODE_OFF;
                mZen = newZen;  // this one's optimistic
                setFooterExpanded(isChecked);
                mController.setZen(newZen, null, TAG);
            }
        }
    };

    public interface Callback {
        void onFooterExpanded();
        void onSettingsClicked();
        void onDoneClicked();
    }
}