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
|
/*
* Copyright (C) 2014 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 static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.provider.Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.os.UserHandle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.systemui.R;
public class ZenToast {
private static final String ACTION_SHOW = ZenToast.class.getName() + ".SHOW";
private static final String ACTION_HIDE = ZenToast.class.getName() + ".HIDE";
private static final String EXTRA_ZEN = "zen";
private static final String EXTRA_TEXT = "text";
private static final int MSG_SHOW = 1;
private static final int MSG_HIDE = 2;
private final Context mContext;
private final WindowManager mWindowManager;
private View mZenToast;
public ZenToast(Context context) {
mContext = context;
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
final IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_SHOW);
filter.addAction(ACTION_HIDE);
mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, mHandler);
}
public void show(int zen) {
mHandler.removeMessages(MSG_HIDE);
mHandler.removeMessages(MSG_SHOW);
mHandler.obtainMessage(MSG_SHOW, zen, 0).sendToTarget();
}
public void hide() {
mHandler.removeMessages(MSG_HIDE);
mHandler.removeMessages(MSG_SHOW);
mHandler.obtainMessage(MSG_HIDE).sendToTarget();
}
private void handleShow(int zen, String overrideText) {
handleHide();
String text;
final int iconRes;
switch (zen) {
case ZEN_MODE_NO_INTERRUPTIONS:
text = mContext.getString(R.string.zen_no_interruptions);
iconRes = R.drawable.ic_zen_none;
break;
case ZEN_MODE_IMPORTANT_INTERRUPTIONS:
text = mContext.getString(R.string.zen_important_interruptions);
iconRes = R.drawable.ic_zen_important;
break;
default:
return;
}
if (overrideText != null) {
text = overrideText;
}
final Resources res = mContext.getResources();
final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = res.getDimensionPixelSize(R.dimen.zen_toast_width);
params.format = PixelFormat.TRANSLUCENT;
params.windowAnimations = R.style.ZenToastAnimations;
params.type = WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
params.setTitle(getClass().getSimpleName());
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
params.gravity = Gravity.CENTER;
params.packageName = mContext.getPackageName();
mZenToast = LayoutInflater.from(mContext).inflate(R.layout.zen_toast, null);
final TextView message = (TextView) mZenToast.findViewById(android.R.id.message);
message.setText(text);
final ImageView icon = (ImageView) mZenToast.findViewById(android.R.id.icon);
icon.setImageResource(iconRes);
mZenToast.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
@Override
public void onViewDetachedFromWindow(View v) {
// noop
}
@Override
public void onViewAttachedToWindow(View v) {
mZenToast.announceForAccessibility(message.getText());
}
});
mWindowManager.addView(mZenToast, params);
final int animDuration = res.getInteger(R.integer.zen_toast_animation_duration);
final int visibleDuration = res.getInteger(R.integer.zen_toast_visible_duration);
mHandler.sendEmptyMessageDelayed(MSG_HIDE, animDuration + visibleDuration);
}
private void handleHide() {
if (mZenToast != null) {
mWindowManager.removeView(mZenToast);
mZenToast = null;
}
}
private final Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_SHOW:
handleShow(msg.arg1, null);
break;
case MSG_HIDE:
handleHide();
break;
}
}
};
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION_SHOW.equals(intent.getAction())) {
final int zen = intent.getIntExtra(EXTRA_ZEN, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
final String text = intent.getStringExtra(EXTRA_TEXT);
handleShow(zen, text);
} else if (ACTION_HIDE.equals(intent.getAction())) {
handleHide();
}
}
};
}
|