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.provider.Settings.Global;
22import android.service.notification.ZenModeConfig;
23import android.util.AttributeSet;
24import android.view.View;
25import android.widget.ImageView;
26import android.widget.LinearLayout;
27import android.widget.TextView;
28
29import com.android.systemui.R;
30import com.android.systemui.statusbar.policy.ZenModeController;
31
32import java.util.Objects;
33
34/**
35 * Zen mode information (and end button) attached to the bottom of the volume dialog.
36 */
37public class ZenFooter extends LinearLayout {
38    private static final String TAG = Util.logTag(ZenFooter.class);
39
40    private final Context mContext;
41    private final SpTexts mSpTexts;
42
43    private ImageView mIcon;
44    private TextView mSummaryLine1;
45    private TextView mSummaryLine2;
46    private TextView mEndNowButton;
47    private int mZen = -1;
48    private ZenModeConfig mConfig;
49    private ZenModeController mController;
50
51    public ZenFooter(Context context, AttributeSet attrs) {
52        super(context, attrs);
53        mContext = context;
54        mSpTexts = new SpTexts(mContext);
55        final LayoutTransition layoutTransition = new LayoutTransition();
56        layoutTransition.setDuration(new ValueAnimator().getDuration() / 2);
57        setLayoutTransition(layoutTransition);
58    }
59
60    @Override
61    protected void onFinishInflate() {
62        super.onFinishInflate();
63        mIcon = (ImageView) findViewById(R.id.volume_zen_icon);
64        mSummaryLine1 = (TextView) findViewById(R.id.volume_zen_summary_line_1);
65        mSummaryLine2 = (TextView) findViewById(R.id.volume_zen_summary_line_2);
66        mEndNowButton = (TextView) findViewById(R.id.volume_zen_end_now);
67        mSpTexts.add(mSummaryLine1);
68        mSpTexts.add(mSummaryLine2);
69        mSpTexts.add(mEndNowButton);
70    }
71
72    public void init(final ZenModeController controller) {
73        mEndNowButton.setOnClickListener(new OnClickListener() {
74            @Override
75            public void onClick(View v) {
76                controller.setZen(Global.ZEN_MODE_OFF, null, TAG);
77            }
78        });
79        mZen = controller.getZen();
80        mConfig = controller.getConfig();
81        mController = controller;
82        mController.addCallback(mZenCallback);
83        update();
84    }
85
86    public void cleanup() {
87        mController.removeCallback(mZenCallback);
88    }
89
90    private void setZen(int zen) {
91        if (mZen == zen) return;
92        mZen = zen;
93        update();
94    }
95
96    private void setConfig(ZenModeConfig config) {
97        if (Objects.equals(mConfig, config)) return;
98        mConfig = config;
99        update();
100    }
101
102    public boolean isZen() {
103        return isZenPriority() || isZenAlarms() || isZenNone();
104    }
105
106    private boolean isZenPriority() {
107        return mZen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
108    }
109
110    private boolean isZenAlarms() {
111        return mZen == Global.ZEN_MODE_ALARMS;
112    }
113
114    private boolean isZenNone() {
115        return mZen == Global.ZEN_MODE_NO_INTERRUPTIONS;
116    }
117
118    public void update() {
119        mIcon.setImageResource(isZenNone() ? R.drawable.ic_dnd_total_silence : R.drawable.ic_dnd);
120        final String line1 =
121                isZenPriority() ? mContext.getString(R.string.interruption_level_priority)
122                : isZenAlarms() ? mContext.getString(R.string.interruption_level_alarms)
123                : isZenNone() ? mContext.getString(R.string.interruption_level_none)
124                : null;
125        Util.setText(mSummaryLine1, line1);
126
127        final boolean isForever = mConfig != null && mConfig.manualRule != null
128                && mConfig.manualRule.conditionId == null;
129        final CharSequence line2 =
130                isForever ? mContext.getString(com.android.internal.R.string.zen_mode_forever_dnd)
131                : ZenModeConfig.getConditionSummary(mContext, mConfig, mController.getCurrentUser(),
132                        true /*shortVersion*/);
133        Util.setText(mSummaryLine2, line2);
134    }
135
136    public void onConfigurationChanged() {
137        Util.setText(mEndNowButton, mContext.getString(R.string.volume_zen_end_now));
138        mSpTexts.update();
139    }
140
141    private final ZenModeController.Callback mZenCallback = new ZenModeController.Callback() {
142        @Override
143        public void onZenChanged(int zen) {
144            setZen(zen);
145        }
146        @Override
147        public void onConfigChanged(ZenModeConfig config) {
148            setConfig(config);
149        }
150    };
151}
152