-
Notifications
You must be signed in to change notification settings - Fork 31
/
ToastUtils.java
333 lines (281 loc) · 11.1 KB
/
ToastUtils.java
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package me.shouheng.utils.ui;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import me.shouheng.utils.UtilsApp;
/**
* Toast utils. Except basic displaying message by a bubble styled system toast.
* You are also allowed to custom the view of toast by calling
* {@link #setToastViewCallback(ToastViewCallback)} method.
*
* @author Shouheng Wang (shouheng2020@gmail.com)
* @version 2019/5/12 18:19
*/
public final class ToastUtils {
private static final int COLOR_DEFAULT = 0xFEFFFFFF;
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
private static final String NULL = "null";
private static Toast sToast;
private static int sGravity = -1;
private static int sXOffset = -1;
private static int sYOffset = -1;
private static int sMsgColor = COLOR_DEFAULT;
private static int sMsgTextSize = -1;
private static ToastViewCallback toastViewCallback;
public static void showShort(final CharSequence text) {
show(text == null ? NULL : text, Toast.LENGTH_SHORT, ToastStyle.NORMAL, null);
}
public static void showShortWithStyle(final CharSequence text, @ToastStyle int style) {
show(text == null ? NULL : text, Toast.LENGTH_SHORT, style, null);
}
public static void showShort(final CharSequence text, ToastViewGetter getter) {
show(text == null ? NULL : text, Toast.LENGTH_SHORT, ToastStyle.NORMAL, getter);
}
public static void showShort(@StringRes final int resId) {
show(resId, Toast.LENGTH_SHORT, ToastStyle.NORMAL, null);
}
public static void showShortWithStyle(@StringRes final int resId, @ToastStyle int style) {
show(resId, Toast.LENGTH_SHORT, style, null);
}
public static void showShort(@StringRes final int resId, ToastViewGetter getter) {
show(resId, Toast.LENGTH_SHORT, ToastStyle.NORMAL, getter);
}
public static void showShort(@StringRes final int resId, final Object... args) {
show(resId, Toast.LENGTH_SHORT, ToastStyle.NORMAL, null, args);
}
public static void showShort(final String format, final Object... args) {
show(format, Toast.LENGTH_SHORT, ToastStyle.NORMAL, null, args);
}
public static void showLong(final CharSequence text) {
show(text == null ? NULL : text, Toast.LENGTH_LONG, ToastStyle.NORMAL, null);
}
public static void showLongWithStyle(final CharSequence text, @ToastStyle int style) {
show(text == null ? NULL : text, Toast.LENGTH_LONG, style, null);
}
public static void showLong(final CharSequence text, ToastViewGetter getter) {
show(text == null ? NULL : text, Toast.LENGTH_LONG, ToastStyle.NORMAL, getter);
}
public static void showLong(@StringRes final int resId) {
show(resId, Toast.LENGTH_LONG, ToastStyle.NORMAL, null);
}
public static void showLongWithStyle(@StringRes final int resId, @ToastStyle int style) {
show(resId, Toast.LENGTH_LONG, style, null);
}
public static void showLong(@StringRes final int resId, ToastViewGetter getter) {
show(resId, Toast.LENGTH_LONG, ToastStyle.NORMAL, getter);
}
public static void showLong(@StringRes final int resId, final Object... args) {
show(resId, Toast.LENGTH_LONG, ToastStyle.NORMAL, null, args);
}
public static void showLong(final String format, final Object... args) {
show(format, Toast.LENGTH_LONG, ToastStyle.NORMAL, null, args);
}
public static void setGravity(final int gravity, final int xOffset, final int yOffset) {
sGravity = gravity;
sXOffset = xOffset;
sYOffset = yOffset;
}
public static void setMsgColor(@ColorInt final int msgColor) {
sMsgColor = msgColor;
}
public static void setMsgTextSize(final int textSize) {
sMsgTextSize = textSize;
}
public static void setToastViewCallback(ToastViewCallback toastViewCallback) {
ToastUtils.toastViewCallback = toastViewCallback;
}
public static ToastViewCallback getToastViewCallback() {
return toastViewCallback;
}
public static void cancel() {
if (sToast != null) {
sToast.cancel();
}
}
/*---------------------------------- 内部方法 --------------------------------------*/
private static void show(
@StringRes final int resId,
final int duration,
@ToastStyle final int style,
ToastViewGetter getter
) {
show(UtilsApp.getApp().getResources().getText(resId).toString(), duration, style, getter);
}
private static void show(
@StringRes final int resId,
final int duration,
@ToastStyle final int style,
ToastViewGetter getter,
final Object... args
) {
show(String.format(UtilsApp.getApp().getResources().getString(resId), args), duration, style, getter);
}
private static void show(
final String format,
final int duration,
@ToastStyle final int style,
ToastViewGetter getter,
final Object... args
) {
String text;
if (format == null) {
text = NULL;
} else {
text = String.format(format, args);
if (text == null) {
text = NULL;
}
}
show(text, duration, style, getter);
}
private static void show(
final CharSequence text,
final int duration,
@ToastStyle final int style,
final ToastViewGetter getter
) {
HANDLER.post(new Runnable() {
@SuppressLint("ShowToast")
@Override
public void run() {
cancel();
sToast = Toast.makeText(UtilsApp.getApp(), text, duration);
View toastView = sToast.getView();
TextView tvMessage = null;
if (toastView != null) {
tvMessage = toastView.findViewById(android.R.id.message);
}/* else {
tvMessage = new AppCompatTextView(UtilsApp.getApp());
tvMessage.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tvMessage.setText(text);
sToast.setView(tvMessage);
}*/
if (tvMessage != null) {
if (sMsgColor != COLOR_DEFAULT) {
tvMessage.setTextColor(sMsgColor);
}
if (sMsgTextSize != -1) {
tvMessage.setTextSize(sMsgTextSize);
}
}
if (sGravity != -1 || sXOffset != -1 || sYOffset != -1) {
sToast.setGravity(sGravity, sXOffset, sYOffset);
}
// View from getter is prior then global toastViewCallback.
if (getter != null) {
sToast.setView(getter.getView(text));
} else {
View view;
if (toastViewCallback != null && (view = toastViewCallback.getView(text, style)) != null) {
sToast.setView(view);
}
}
showToast();
}
});
}
private static void showToast() {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
try {
// noinspection JavaReflectionMemberAccess
Field field = View.class.getDeclaredField("mContext");
field.setAccessible(true);
field.set(sToast.getView(), new ApplicationContextWrapperForApi25());
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
sToast.show();
}
private static final class ApplicationContextWrapperForApi25 extends ContextWrapper {
ApplicationContextWrapperForApi25() {
super(UtilsApp.getApp());
}
@Override
public Context getApplicationContext() {
return this;
}
@Override
public Object getSystemService(@NonNull String name) {
if (Context.WINDOW_SERVICE.equals(name)) {
// noinspection ConstantConditions
return new WindowManagerWrapper(
(WindowManager) getBaseContext().getSystemService(name)
);
}
return super.getSystemService(name);
}
private static final class WindowManagerWrapper implements WindowManager {
private final WindowManager base;
private WindowManagerWrapper(@NonNull WindowManager base) {
this.base = base;
}
@Override
public Display getDefaultDisplay() {
return base.getDefaultDisplay();
}
@Override
public void removeViewImmediate(View view) {
base.removeViewImmediate(view);
}
@Override
public void addView(View view, ViewGroup.LayoutParams params) {
try {
base.addView(view, params);
} catch (BadTokenException e) {
Log.e("WindowManagerWrapper", e.getMessage());
} catch (Throwable throwable) {
Log.e("WindowManagerWrapper", "[addView]", throwable);
}
}
@Override
public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
base.updateViewLayout(view, params);
}
@Override
public void removeView(View view) {
base.removeView(view);
}
}
}
/** The custom toast view factory method. */
public interface ToastViewCallback {
/** Get normal styled toast view. */
View getView(CharSequence text, @ToastStyle int style);
}
/** The custom toast view getter. */
public interface ToastViewGetter {
View getView(CharSequence text);
}
@IntDef({ToastStyle.NORMAL, ToastStyle.INFO, ToastStyle.WARN, ToastStyle.ERROR, ToastStyle.SUCCESS})
@Retention(RetentionPolicy.SOURCE)
public @interface ToastStyle {
int NORMAL = 0;
int INFO = 1;
int WARN = 2;
int ERROR = 3;
int SUCCESS = 4;
}
private ToastUtils() {
throw new UnsupportedOperationException("u can't initialize me!");
}
}