22
33import android .util .Log ;
44
5- import java .io .IOException ;
65import java .lang .annotation .ElementType ;
76import java .lang .annotation .Retention ;
87import java .lang .annotation .RetentionPolicy ;
98import java .lang .annotation .Target ;
109import java .lang .reflect .InvocationTargetException ;
1110import java .lang .reflect .Method ;
1211import java .util .ArrayList ;
13- import java .util .Collections ;
14- import java .util .Comparator ;
1512import java .util .HashMap ;
1613import java .util .HashSet ;
1714import java .util .List ;
3128 */
3229public final class BusUtils {
3330
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" ;
3733
3834 private final Map <String , List <BusInfo >> mTag_BusInfoListMap = new HashMap <>();
3935
@@ -42,65 +38,24 @@ public final class BusUtils {
4238 private final Map <String , Map <String , Object >> mClassName_Tag_Arg4StickyMap = new ConcurrentHashMap <>();
4339
4440 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 ();
6542 }
6643
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*/ }
8849
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 );
9954 }
10055
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 ) {
10459 List <BusInfo > busInfoList = mTag_BusInfoListMap .get (tag );
10560 if (busInfoList == null ) {
10661 busInfoList = new ArrayList <>();
@@ -109,12 +64,6 @@ private void registerBusInner(String tag,
10964 busInfoList .add (new BusInfo (className , funName , paramType , paramName , sticky , threadMode , priority ));
11065 }
11166
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-
11867 public static void register (final Object bus ) {
11968 getInstance ().registerInner (bus );
12069 }
@@ -152,20 +101,6 @@ public String toString() {
152101 return "BusUtils: " + mTag_BusInfoListMap ;
153102 }
154103
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-
169104 private static BusUtils getInstance () {
170105 return LazyHolder .INSTANCE ;
171106 }
@@ -244,9 +179,6 @@ private void postInner(final String tag, final Object arg, final boolean sticky)
244179 if (busInfo .method == null ) {
245180 Method method = getMethodByBusInfo (busInfo );
246181 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." );
250182 return ;
251183 }
252184 busInfo .method = method ;
@@ -302,7 +234,7 @@ public void run() {
302234 };
303235 switch (busInfo .threadMode ) {
304236 case "MAIN" :
305- UtilsBridge .runOnUiThread (runnable );
237+ ThreadUtils .runOnUiThread (runnable );
306238 return ;
307239 case "IO" :
308240 ThreadUtils .getIoPool ().execute (runnable );
@@ -399,6 +331,12 @@ private void removeStickyInner(final String tag) {
399331 }
400332 }
401333
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+
402340 private static final class BusInfo {
403341
404342 String className ;
0 commit comments