ZenFooter.java revision f88d8082a86bee00c604cbbcfb5261f5573936fe
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.systemui.volume;
17
18import android.animation.LayoutTransition;
19import android.animation.ValueAnimator;
20import android.content.Context;
21import android.content.res.Resources;
22import android.provider.Settings.Global;
23import android.service.notification.Condition;
24import android.util.AttributeSet;
25import android.util.Log;
26import android.util.TypedValue;
27import android.view.View;
28import android.widget.CompoundButton;
29import android.widget.CompoundButton.OnCheckedChangeListener;
30import android.widget.LinearLayout;
31import android.widget.Switch;
32import android.widget.TextView;
33
34import com.android.systemui.R;
35import com.android.systemui.statusbar.policy.ZenModeController;
36
37/**
38 * Switch bar + zen mode panel (conditions) attached to the bottom of the volume dialog.
39 */
40public class ZenFooter extends LinearLayout {
41    private static final String TAG = Util.logTag(ZenFooter.class);
42
43    private final Context mContext;
44    private final float mSecondaryAlpha;
45    private final LayoutTransition mLayoutTransition;
46
47    private ZenModeController mController;
48    private Switch mSwitch;
49    private ZenModePanel mZenModePanel;
50    private View mZenModePanelButtons;
51    private View mZenModePanelMoreButton;
52    private View mZenModePanelDoneButton;
53    private View mSwitchBar;
54    private View mSwitchBarIcon;
55    private View mSummary;
56    private TextView mSummaryLine1;
57    private TextView mSummaryLine2;
58    private boolean mFooterExpanded;
59    private int mZen = -1;
60    private Callback mCallback;
61
62    public ZenFooter(Context context, AttributeSet attrs) {
63        super(context, attrs);
64        mContext = context;
65        mSecondaryAlpha = getFloat(context.getResources(), R.dimen.volume_secondary_alpha);
66        mLayoutTransition = new LayoutTransition();
67        mLayoutTransition.setDuration(new ValueAnimator().getDuration() / 2);
68        mLayoutTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
69        mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
70    }
71
72    private static float getFloat(Resources r, int resId) {
73        final TypedValue tv = new TypedValue();
74        r.getValue(resId, tv, true);
75        return tv.getFloat();
76    }
77
78    @Override
79    protected void onFinishInflate() {
80        super.onFinishInflate();
81        mSwitchBar = findViewById(R.id.volume_zen_switch_bar);
82        mSwitchBarIcon = findViewById(R.id.volume_zen_switch_bar_icon);
83        mSwitch = (Switch) findViewById(R.id.volume_zen_switch);
84        mZenModePanel = (ZenModePanel) findViewById(R.id.zen_mode_panel);
85        mZenModePanelButtons = findViewById(R.id.volume_zen_mode_panel_buttons);
86        mZenModePanelMoreButton = findViewById(R.id.volume_zen_mode_panel_more);
87        mZenModePanelDoneButton = findViewById(R.id.volume_zen_mode_panel_done);
88        mSummary = findViewById(R.id.volume_zen_panel_summary);
89        mSummaryLine1 = (TextView) findViewById(R.id.volume_zen_panel_summary_line_1);
90        mSummaryLine2 = (TextView) findViewById(R.id.volume_zen_panel_summary_line_2);
91    }
92
93    public void init(ZenModeController controller, Callback callback) {
94        mCallback = callback;
95        mController = controller;
96        mZenModePanel.init(controller);
97        mZenModePanel.setEmbedded(true);
98        mSwitch.setOnCheckedChangeListener(mCheckedListener);
99        mController.addCallback(new ZenModeController.Callback() {
100            @Override
101            public void onZenChanged(int zen) {
102                setZen(zen);
103            }
104            @Override
105            public void onExitConditionChanged(Condition exitCondition) {
106                update();
107            }
108        });
109        mSwitchBar.setOnClickListener(new OnClickListener() {
110            @Override
111            public void onClick(View v) {
112                mSwitch.setChecked(!mSwitch.isChecked());
113            }
114        });
115        mZenModePanelMoreButton.setOnClickListener(new OnClickListener() {
116            @Override
117            public void onClick(View v) {
118                if (mCallback != null) {
119                    mCallback.onSettingsClicked();
120                }
121            }
122        });
123        mZenModePanelDoneButton.setOnClickListener(new OnClickListener() {
124            @Override
125            public void onClick(View v) {
126                if (mCallback != null) {
127                    mCallback.onDoneClicked();
128                }
129            }
130        });
131        mZen = mController.getZen();
132        update();
133    }
134
135    private void setZen(int zen) {
136        if (mZen == zen) return;
137        mZen = zen;
138        update();
139    }
140
141    public boolean isZen() {
142        return isZenPriority() || isZenNone();
143    }
144
145    private boolean isZenPriority() {
146        return mZen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
147    }
148
149    private boolean isZenNone() {
150        return mZen == Global.ZEN_MODE_NO_INTERRUPTIONS;
151    }
152
153    @Override
154    protected void onDetachedFromWindow() {
155        super.onDetachedFromWindow();
156        setLayoutTransition(null);
157        setFooterExpanded(false);
158    }
159
160    @Override
161    protected void onAttachedToWindow() {
162        super.onAttachedToWindow();
163        setLayoutTransition(mLayoutTransition);
164    }
165
166    private boolean setFooterExpanded(boolean expanded) {
167        if (mFooterExpanded == expanded) return false;
168        mFooterExpanded = expanded;
169        update();
170        if (mCallback != null) {
171            mCallback.onFooterExpanded();
172        }
173        return true;
174    }
175
176    public boolean isFooterExpanded() {
177        return mFooterExpanded;
178    }
179
180    public void update() {
181        final boolean isZen = isZen();
182        mSwitch.setOnCheckedChangeListener(null);
183        mSwitch.setChecked(isZen);
184        mSwitch.setOnCheckedChangeListener(mCheckedListener);
185        Util.setVisOrGone(mZenModePanel, isZen && mFooterExpanded);
186        Util.setVisOrGone(mZenModePanelButtons, isZen && mFooterExpanded);
187        Util.setVisOrGone(mSummary, isZen && !mFooterExpanded);
188        mSwitchBarIcon.setAlpha(isZen ? 1 : mSecondaryAlpha);
189        final String line1 =
190                isZenPriority() ? mContext.getString(R.string.interruption_level_priority)
191                : isZenNone() ? mContext.getString(R.string.interruption_level_none)
192                : null;
193        Util.setText(mSummaryLine1, line1);
194        Util.setText(mSummaryLine2, mZenModePanel.getExitConditionText());
195    }
196
197    private final OnCheckedChangeListener mCheckedListener = new OnCheckedChangeListener() {
198        @Override
199        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
200            if (D.BUG) Log.d(TAG, "onCheckedChanged " + isChecked);
201            if (isChecked != isZen()) {
202                final int newZen = isChecked ? Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS
203                        : Global.ZEN_MODE_OFF;
204                mZen = newZen;  // this one's optimistic
205                setFooterExpanded(isChecked);
206                mController.setZen(newZen);
207            }
208        }
209    };
210
211    public interface Callback {
212        void onFooterExpanded();
213        void onSettingsClicked();
214        void onDoneClicked();
215    }
216}
217