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  */
16 package com.android.systemui.volume;
17 
18 import android.animation.LayoutTransition;
19 import android.animation.ValueAnimator;
20 import android.content.Context;
21 import android.provider.Settings.Global;
22 import android.service.notification.ZenModeConfig;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.ImageView;
26 import android.widget.LinearLayout;
27 import android.widget.TextView;
28 
29 import com.android.systemui.R;
30 import com.android.systemui.statusbar.policy.ZenModeController;
31 
32 import java.util.Objects;
33 
34 /**
35  * Zen mode information (and end button) attached to the bottom of the volume dialog.
36  */
37 public 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 
ZenFooter(Context context, AttributeSet attrs)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
onFinishInflate()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 
init(final ZenModeController controller)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 
cleanup()86     public void cleanup() {
87         mController.removeCallback(mZenCallback);
88     }
89 
setZen(int zen)90     private void setZen(int zen) {
91         if (mZen == zen) return;
92         mZen = zen;
93         update();
94     }
95 
setConfig(ZenModeConfig config)96     private void setConfig(ZenModeConfig config) {
97         if (Objects.equals(mConfig, config)) return;
98         mConfig = config;
99         update();
100     }
101 
isZen()102     public boolean isZen() {
103         return isZenPriority() || isZenAlarms() || isZenNone();
104     }
105 
isZenPriority()106     private boolean isZenPriority() {
107         return mZen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
108     }
109 
isZenAlarms()110     private boolean isZenAlarms() {
111         return mZen == Global.ZEN_MODE_ALARMS;
112     }
113 
isZenNone()114     private boolean isZenNone() {
115         return mZen == Global.ZEN_MODE_NO_INTERRUPTIONS;
116     }
117 
update()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 
onConfigurationChanged()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