2
2
3
3
import android .util .Log ;
4
4
5
- import java .io .IOException ;
6
5
import java .lang .annotation .ElementType ;
7
6
import java .lang .annotation .Retention ;
8
7
import java .lang .annotation .RetentionPolicy ;
9
8
import java .lang .annotation .Target ;
10
9
import java .lang .reflect .InvocationTargetException ;
11
10
import java .lang .reflect .Method ;
12
11
import java .util .ArrayList ;
13
- import java .util .Collections ;
14
- import java .util .Comparator ;
15
12
import java .util .HashMap ;
16
13
import java .util .HashSet ;
17
14
import java .util .List ;
31
28
*/
32
29
public final class BusUtils {
33
30
34
- private static final Object NULL = "nULl" ;
35
- private static final String TAG = "BusUtils" ;
36
- private static final String PREFIX = "blankj.bus/" ;
31
+ private static final Object NULL = "nULl" ;
32
+ private static final String TAG = "BusUtils" ;
37
33
38
34
private final Map <String , List <BusInfo >> mTag_BusInfoListMap = new HashMap <>();
39
35
@@ -42,65 +38,24 @@ public final class BusUtils {
42
38
private final Map <String , Map <String , Object >> mClassName_Tag_Arg4StickyMap = new ConcurrentHashMap <>();
43
39
44
40
private BusUtils () {
45
- try {
46
- String [] tags = Utils .getApp ().getAssets ().list (PREFIX );
47
- if (tags == null || tags .length == 0 ) {
48
- Log .w (TAG , "no bus" );
49
- return ;
50
- }
51
- for (String tag : tags ) {
52
- String [] busInfos = Utils .getApp ().getAssets ().list (PREFIX + tag );
53
- if (busInfos == null || busInfos .length == 0 ) {
54
- Log .w (TAG , "The tag of <" + tag + "> no bus." );
55
- continue ;
56
- }
57
- for (String busInfo : busInfos ) {
58
- parseBusInfo (tag , busInfo );
59
- }
60
- sortBusByPriority (tag );
61
- }
62
- } catch (IOException e ) {
63
- e .printStackTrace ();
64
- }
41
+ init ();
65
42
}
66
43
67
- private void parseBusInfo (String tag , String busInfo ) {
68
- String [] split = busInfo .split ("-" );
69
- if (split .length != 7 ) {
70
- Log .e (TAG , "The tag of <" + tag + ">'s bus <" + busInfo + "> which format is wrong." );
71
- return ;
72
- }
73
- String className = split [0 ];
74
- String funName = split [1 ];
75
- String paramType = split [2 ];
76
- String paramName = split [3 ];
77
- boolean sticky = Boolean .parseBoolean (split [4 ]);
78
- String threadMode = split [5 ];
79
- int priority ;
80
- try {
81
- priority = Integer .parseInt (split [6 ]);
82
- } catch (NumberFormatException e ) {
83
- Log .e (TAG , "The tag of <" + tag + ">'s bus <" + busInfo + "> which format is wrong." );
84
- return ;
85
- }
86
- registerBusInner (tag , className , funName , paramType , paramName , sticky , threadMode , priority );
87
- }
44
+ /**
45
+ * It'll be injected the bus who have {@link Bus} annotation
46
+ * by function of {@link BusUtils#registerBus} when execute transform task.
47
+ */
48
+ private void init () {/*inject*/ }
88
49
89
- private void sortBusByPriority (String tag ) {
90
- List <BusInfo > busInfoList = mTag_BusInfoListMap .get (tag );
91
- if (busInfoList != null && busInfoList .size () > 1 ) {
92
- Collections .sort (busInfoList , new Comparator <BusInfo >() {
93
- @ Override
94
- public int compare (BusInfo o0 , BusInfo o1 ) {
95
- return o1 .priority - o0 .priority ;
96
- }
97
- });
98
- }
50
+ private void registerBus (String tag ,
51
+ String className , String funName , String paramType , String paramName ,
52
+ boolean sticky , String threadMode ) {
53
+ registerBus (tag , className , funName , paramType , paramName , sticky , threadMode , 0 );
99
54
}
100
55
101
- private void registerBusInner (String tag ,
102
- String className , String funName , String paramType , String paramName ,
103
- boolean sticky , String threadMode , int priority ) {
56
+ private void registerBus (String tag ,
57
+ String className , String funName , String paramType , String paramName ,
58
+ boolean sticky , String threadMode , int priority ) {
104
59
List <BusInfo > busInfoList = mTag_BusInfoListMap .get (tag );
105
60
if (busInfoList == null ) {
106
61
busInfoList = new ArrayList <>();
@@ -109,12 +64,6 @@ private void registerBusInner(String tag,
109
64
busInfoList .add (new BusInfo (className , funName , paramType , paramName , sticky , threadMode , priority ));
110
65
}
111
66
112
- public static void registerBus (String tag ,
113
- String className , String funName , String paramType , String paramName ,
114
- boolean sticky , String threadMode , int priority ) {
115
- getInstance ().registerBusInner (tag , className , funName , paramType , paramName , sticky , threadMode , priority );
116
- }
117
-
118
67
public static void register (final Object bus ) {
119
68
getInstance ().registerInner (bus );
120
69
}
@@ -152,20 +101,6 @@ public String toString() {
152
101
return "BusUtils: " + mTag_BusInfoListMap ;
153
102
}
154
103
155
- static Runnable getPreLoadRunnable () {
156
- return new Runnable () {
157
- @ Override
158
- public void run () {
159
- preLoad ();
160
- }
161
- };
162
- }
163
-
164
- private static void preLoad () {
165
- //noinspection ResultOfMethodCallIgnored
166
- getInstance ();
167
- }
168
-
169
104
private static BusUtils getInstance () {
170
105
return LazyHolder .INSTANCE ;
171
106
}
@@ -244,9 +179,6 @@ private void postInner(final String tag, final Object arg, final boolean sticky)
244
179
if (busInfo .method == null ) {
245
180
Method method = getMethodByBusInfo (busInfo );
246
181
if (method == null ) {
247
- Log .e (TAG , "The bus of tag <" + tag + ">'s method <" + busInfo .funName +
248
- ("" .equals (busInfo .paramType ) ? "()" : ("(" + busInfo .paramType + " " + busInfo .paramName + ")" ))
249
- + "> is not exists." );
250
182
return ;
251
183
}
252
184
busInfo .method = method ;
@@ -302,7 +234,7 @@ public void run() {
302
234
};
303
235
switch (busInfo .threadMode ) {
304
236
case "MAIN" :
305
- UtilsBridge .runOnUiThread (runnable );
237
+ ThreadUtils .runOnUiThread (runnable );
306
238
return ;
307
239
case "IO" :
308
240
ThreadUtils .getIoPool ().execute (runnable );
@@ -399,6 +331,12 @@ private void removeStickyInner(final String tag) {
399
331
}
400
332
}
401
333
334
+ static void registerBus4Test (String tag ,
335
+ String className , String funName , String paramType , String paramName ,
336
+ boolean sticky , String threadMode , int priority ) {
337
+ getInstance ().registerBus (tag , className , funName , paramType , paramName , sticky , threadMode , priority );
338
+ }
339
+
402
340
private static final class BusInfo {
403
341
404
342
String className ;
0 commit comments