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