-
Notifications
You must be signed in to change notification settings - Fork 2
/
Toast.java
142 lines (121 loc) · 4.13 KB
/
Toast.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
package com.gaogeek.toast;
/**
* Created by gaogeek on 2017/8/2.
*/
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Toast {
private Handler handler;
public static int LENGTH_SHORT = android.widget.Toast.LENGTH_SHORT;
public static int LENGTH_LONG = android.widget.Toast.LENGTH_LONG;
private Context mContext;
private Object mObj;
private Method showMethod, hideMethod;
private android.widget.Toast mToast;
private String text = "Toast";
private int time = 2500;
private boolean hasReflectException = false;
private static Toast instance;
public static Toast makeText(Context context, String text, int delay) {
if (instance == null) {
instance = new Toast(context);
}
instance.setDuration(delay);
instance.setText(text);
return instance;
}
private Toast(Context context) {
if (context == null || context.getApplicationContext() == null) {
throw new NullPointerException("context can't be null");
}
this.mContext = context;
initTN();
if (handler == null) {
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Toast.this.hideToast();
}
};
}
}
public void setText(String text) {
this.text = text;
}
public void setText(int resId) {
setText(mContext.getText(resId).toString());
}
public void setDuration(int t) {
if (t == android.widget.Toast.LENGTH_SHORT) {
this.time = 2500;
} else if (t == android.widget.Toast.LENGTH_LONG) {
this.time = 3500;
} else if (t > 1000) {
this.time = t;
}
}
public final void show() {
if (!hasReflectException) {
mToast.setText(text);
showToast();
handler.removeMessages(1);
handler.sendEmptyMessageDelayed(1, time);
}
}
private void initTN() {
Field mTN;
if (mToast == null) {
mToast = android.widget.Toast.makeText(mContext, text, android.widget.Toast.LENGTH_SHORT);
}
Class<android.widget.Toast> clazz = android.widget.Toast.class;
try {
mTN = clazz.getDeclaredField("mTN");
mTN.setAccessible(true);
mObj = mTN.get(mToast);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
showMethod = mObj.getClass().getDeclaredMethod("show", IBinder.class);
} else {
showMethod = mObj.getClass().getDeclaredMethod("show", new Class<?>[0]);
}
hideMethod = mObj.getClass().getDeclaredMethod("hide", new Class<?>[0]);
Field mY = mObj.getClass().getDeclaredField("mY");
mY.setAccessible(true);
mY.set(mObj, dip2px(mContext, 68F));
hasReflectException = false;
} catch (Exception e) {
hasReflectException = true;
}
}
private void showToast() {
try {
Field mNextView = mObj.getClass().getDeclaredField("mNextView");
mNextView.setAccessible(true);
mNextView.set(mObj, mToast.getView());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
showMethod.invoke(mObj, new Object[1]);
} else {
showMethod.invoke(mObj, new Object[0]);
}
hasReflectException = false;
} catch (Exception e) {
hasReflectException = true;
}
}
private void hideToast() {
try {
hideMethod.invoke(mObj, new Object[0]);
hasReflectException = false;
} catch (Exception e) {
hasReflectException = true;
}
}
private int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}