Skip to content

Commit 408a616

Browse files
committed
Merge branch 'develop'
# Conflicts: # GoRouter/src/main/java/com/wyjson/router/core/Card.java # GoRouter/src/main/java/com/wyjson/router/core/CardMeta.java # GoRouter/src/main/java/com/wyjson/router/core/GoRouter.java # GoRouter/src/main/java/com/wyjson/router/interceptor/InterceptorUtils.java # GoRouter/src/main/java/com/wyjson/router/interceptor/service/impl/InterceptorServiceImpl.java # GoRouter/src/main/java/com/wyjson/router/thread/DefaultPoolExecutor.java # GoRouter/src/main/java/com/wyjson/router/thread/DefaultThreadFactory.java # GoRouter/src/main/java/com/wyjson/router/utils/DefaultLogger.java # GoRouter/src/main/java/com/wyjson/router/utils/ILogger.java
2 parents 3207479 + dcf1c5c commit 408a616

File tree

19 files changed

+252
-165
lines changed

19 files changed

+252
-165
lines changed

GoRouter/src/main/java/com/wyjson/router/core/Card.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.os.Parcelable;
77
import android.util.SparseArray;
88

9+
import androidx.annotation.NonNull;
910
import androidx.annotation.Nullable;
1011
import androidx.annotation.RequiresApi;
1112
import androidx.core.app.ActivityOptionsCompat;
@@ -291,4 +292,26 @@ public int getTimeout() {
291292
public void setTimeout(int timeout) {
292293
this.timeout = timeout;
293294
}
295+
296+
@NonNull
297+
@Override
298+
public String toString() {
299+
if (!GoRouter.logger.isShowLog()) {
300+
return "";
301+
}
302+
return "Card{" +
303+
"path='" + getPath() + '\'' +
304+
", uri=" + uri +
305+
", mBundle=" + mBundle +
306+
", flags=" + flags +
307+
", greenChannel=" + greenChannel +
308+
", action='" + action + '\'' +
309+
", context=" + (context != null ? context.getClass().getSimpleName() : null) +
310+
", optionsCompat=" + optionsCompat +
311+
", enterAnim=" + enterAnim +
312+
", exitAnim=" + exitAnim +
313+
", interceptorException=" + interceptorException +
314+
", timeout=" + timeout +
315+
'}';
316+
}
294317
}

GoRouter/src/main/java/com/wyjson/router/core/CardMeta.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.wyjson.router.core;
22

3+
import androidx.annotation.NonNull;
4+
35
import com.wyjson.router.enums.ParamType;
46
import com.wyjson.router.enums.RouteType;
57
import com.wyjson.router.utils.TextUtils;
@@ -60,10 +62,11 @@ private RouteType extractType(String path) {
6062
if (!TextUtils.isEmpty(defaultType)) {
6163
routeType = RouteType.getType(defaultType);
6264
}
63-
} catch (Exception ignored) {
65+
} catch (Exception e) {
66+
GoRouter.logger.warning(null, "[extractType] " + e.getMessage());
6467
}
6568
if (routeType == null) {
66-
GoRouter.logger.warning(null, "The route type is incorrect! The path[" + path + "] type can only end with " + RouteType.toStringByValues());
69+
GoRouter.logger.error(null, "[extractType] The route type is incorrect! The path[" + path + "] type can only end with " + RouteType.toStringByValues());
6770
}
6871
return routeType;
6972
}
@@ -133,4 +136,18 @@ public CardMeta putParcelable(String key) {
133136
getParamsType().put(key, ParamType.Parcelable);
134137
return this;
135138
}
139+
140+
@NonNull
141+
public String toSuperString() {
142+
if (!GoRouter.logger.isShowLog()) {
143+
return "";
144+
}
145+
return "CardMeta{" +
146+
"path='" + path + '\'' +
147+
", type=" + type +
148+
", pathClass=" + pathClass +
149+
", tag=" + tag +
150+
", paramsType=" + paramsType +
151+
'}';
152+
}
136153
}

GoRouter/src/main/java/com/wyjson/router/core/GoRouter.java

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.wyjson.router.interfaces.DegradeService;
2626
import com.wyjson.router.interfaces.IInterceptor;
2727
import com.wyjson.router.interfaces.IService;
28-
import com.wyjson.router.interfaces.PathReplaceService;
2928
import com.wyjson.router.interfaces.PretreatmentService;
3029
import com.wyjson.router.service.ServiceHelper;
3130
import com.wyjson.router.thread.DefaultPoolExecutor;
@@ -50,7 +49,7 @@ public final class GoRouter {
5049
public static ILogger logger = new DefaultLogger("GoRouter");
5150

5251
private GoRouter() {
53-
logger.info(null, "GoRouter init!");
52+
logger.info(null, "[GoRouter] init!");
5453
InterceptorUtils.clearIterator();
5554
addService(InterceptorServiceImpl.class);
5655
}
@@ -77,24 +76,31 @@ public static void setLogger(ILogger userLogger) {
7776
}
7877
}
7978

80-
static synchronized void openLog() {
79+
public static synchronized void openLog() {
8180
logger.showLog(true);
82-
logger.info(null, "GoRouter openLog");
81+
logger.info(null, "[openLog]");
8382
}
8483

8584
public static synchronized void printStackTrace() {
8685
logger.showStackTrace(true);
87-
logger.info(null, "GoRouter printStackTrace");
86+
logger.info(null, "[printStackTrace]");
8887
}
8988

9089
@Nullable
9190
CardMeta getCardMeta(Card card) {
92-
return routes.get(card.getPath());
91+
CardMeta cardMeta = routes.get(card.getPath());
92+
if (cardMeta != null) {
93+
GoRouter.logger.info(null, "[getCardMeta] " + cardMeta.toSuperString());
94+
} else {
95+
GoRouter.logger.warning(null, "[getCardMeta] null");
96+
}
97+
return cardMeta;
9398
}
9499

95100
void addCardMeta(CardMeta cardMeta) {
96101
if (cardMeta.getType() != null) {
97102
routes.put(cardMeta.getPath(), cardMeta);
103+
GoRouter.logger.debug(null, "[addCardMeta] size:" + routes.size() + ", commit:" + cardMeta.toSuperString());
98104
} else {
99105
throw new RouterException("The route type is incorrect! The path[" + cardMeta.getPath() + "] type can only end with " + RouteType.toStringByValues());
100106
}
@@ -151,23 +157,13 @@ public Card build(String path) {
151157
public Card build(String path, Bundle bundle) {
152158
if (TextUtils.isEmpty(path)) {
153159
throw new RouterException("[path] Parameter is invalid!");
154-
} else {
155-
PathReplaceService pService = getService(PathReplaceService.class);
156-
if (pService != null) {
157-
path = pService.forString(path);
158-
}
159160
}
160161
return new Card(path, bundle);
161162
}
162163

163164
public Card build(Uri uri) {
164165
if (uri == null || TextUtils.isEmpty(uri.toString())) {
165-
throw new RouterException("[uri] Parameter invalid!");
166-
} else {
167-
PathReplaceService pService = getService(PathReplaceService.class);
168-
if (pService != null) {
169-
uri = pService.forUri(uri);
170-
}
166+
throw new RouterException("[uri] Parameter is invalid!");
171167
}
172168
return new Card(uri);
173169
}
@@ -205,6 +201,7 @@ public <T> void inject(T target) {
205201
Object value = bundle.get(paramsName);
206202
if (value == null)
207203
continue;
204+
logger.debug(null, "[inject] " + paramsName + ":" + value);
208205
try {
209206
Field injectField = target.getClass().getDeclaredField(paramsName);
210207
injectField.setAccessible(true);
@@ -214,17 +211,24 @@ public <T> void inject(T target) {
214211
}
215212
}
216213
}
214+
logger.debug(null, "[inject] Auto Inject Success!");
217215
}
218216

219217
@Nullable
220218
Object go(Context context, Card card, int requestCode, GoCallback callback) {
221-
PretreatmentService pretreatmentService = getService(PretreatmentService.class);
222-
if (pretreatmentService != null && !pretreatmentService.onPretreatment(context, card)) {
223-
// 预处理失败,导航取消
224-
return null;
225-
}
226219
card.setContext(context);
227220
card.setInterceptorException(null);
221+
logger.debug(null, "[go] " + card.toString());
222+
PretreatmentService pretreatmentService = getService(PretreatmentService.class);
223+
if (pretreatmentService != null) {
224+
if (!pretreatmentService.onPretreatment(context, card)) {
225+
// 预处理失败,导航取消
226+
logger.debug(null, "[go] PretreatmentService Failure!");
227+
return null;
228+
}
229+
} else {
230+
logger.warning(null, "[go] This [PretreatmentService] was not found!");
231+
}
228232
CardMeta cardMeta = getCardMeta(card);
229233
if (cardMeta != null) {
230234
card.setPathClass(cardMeta.getPathClass());
@@ -252,6 +256,7 @@ Object go(Context context, Card card, int requestCode, GoCallback callback) {
252256
}
253257

254258
runInMainThread(() -> {
259+
logger.debug(null, "[go] [onFound] " + card.toString());
255260
if (callback != null) {
256261
callback.onFound(card);
257262
}
@@ -284,7 +289,6 @@ public void onInterrupt(Card card, @NonNull Throwable exception) {
284289
return goFragment(context, card, cardMeta.getPathClass(), callback);
285290
}
286291
} else {
287-
// There is no route.
288292
onLost(context, card, callback);
289293
}
290294
return null;
@@ -336,12 +340,15 @@ private void setValue(Card card, ParamType type, String key, String value) {
336340

337341
private void onLost(Context context, Card card, GoCallback callback) {
338342
runInMainThread(() -> {
343+
logger.error(null, "[onLost] There is no route. path[" + card.getPath() + "]");
339344
if (callback != null) {
340345
callback.onLost(card);
341346
} else {
342347
DegradeService degradeService = getService(DegradeService.class);
343348
if (degradeService != null) {
344349
degradeService.onLost(context, card);
350+
} else {
351+
logger.warning(null, "[onLost] This [DegradeService] was not found!");
345352
}
346353
}
347354
});
@@ -382,6 +389,7 @@ private void goActivity(Context context, Card card, int requestCode, Class<?> cl
382389
((Activity) context).overridePendingTransition(card.getEnterAnim(), card.getExitAnim());
383390
}
384391

392+
logger.debug(null, "[goActivity] [onArrival] Complete!");
385393
if (callback != null) {
386394
callback.onArrival(card);
387395
}
@@ -396,6 +404,7 @@ private Object goFragment(Context context, Card card, Class<?> cls, GoCallback c
396404
((Fragment) instance).setArguments(card.getExtras());
397405
}
398406
runInMainThread(() -> {
407+
logger.debug(null, "[goFragment] [onArrival] Complete!");
399408
if (callback != null) {
400409
callback.onArrival(card);
401410
}

GoRouter/src/main/java/com/wyjson/router/interceptor/InterceptorUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.wyjson.router.interceptor;
22

3+
import com.wyjson.router.core.GoRouter;
34
import com.wyjson.router.exception.RouterException;
45
import com.wyjson.router.interfaces.IInterceptor;
56

@@ -32,6 +33,9 @@ public static void addInterceptor(int priority, Class<? extends IInterceptor> in
3233
IInterceptor instance = interceptor.getConstructor().newInstance();
3334
instance.init();
3435
interceptors.put(priority, instance);
36+
37+
String title = isForce ? "[setInterceptor]" : "[addInterceptor]";
38+
GoRouter.logger.debug(null, title + " size:" + interceptors.size() + ", priority:" + priority + " -> " + interceptor.getSimpleName());
3539
} catch (Exception e) {
3640
throw new RouterException(e);
3741
}

GoRouter/src/main/java/com/wyjson/router/interceptor/UniqueKeyTreeMap.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.wyjson.router.interceptor;
22

3+
import androidx.annotation.NonNull;
4+
5+
import com.wyjson.router.core.GoRouter;
36
import com.wyjson.router.exception.RouterException;
47

8+
import java.util.Iterator;
59
import java.util.TreeMap;
610

711
public class UniqueKeyTreeMap<K, V> extends TreeMap<K, V> {
@@ -20,4 +24,29 @@ public V put(K key, V value) {
2024
return super.put(key, value);
2125
}
2226
}
27+
28+
@NonNull
29+
@Override
30+
public String toString() {
31+
if (!GoRouter.logger.isShowLog()) {
32+
return "";
33+
}
34+
Iterator<Entry<K, V>> i = entrySet().iterator();
35+
if (!i.hasNext())
36+
return "{}";
37+
38+
StringBuilder sb = new StringBuilder();
39+
sb.append('{');
40+
for (; ; ) {
41+
Entry<K, V> e = i.next();
42+
K key = e.getKey();
43+
V value = e.getValue();
44+
sb.append(key == this ? "(this Map)" : key);
45+
sb.append("->");
46+
sb.append(value == this ? "(this Map)" : value.getClass().getSimpleName());
47+
if (!i.hasNext())
48+
return sb.append('}').toString();
49+
sb.append(',').append(' ');
50+
}
51+
}
2352
}

GoRouter/src/main/java/com/wyjson/router/interceptor/service/impl/InterceptorServiceImpl.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.wyjson.router.interceptor.service.impl;
22

3+
import androidx.annotation.NonNull;
4+
35
import com.wyjson.router.core.Card;
46
import com.wyjson.router.core.GoRouter;
57
import com.wyjson.router.exception.RouterException;
@@ -18,6 +20,7 @@ public class InterceptorServiceImpl implements InterceptorService {
1820

1921
@Override
2022
public void doInterceptions(Card card, InterceptorCallback callback) {
23+
GoRouter.logger.info(null, "[doInterceptions] " + InterceptorUtils.getInterceptors());
2124
if (MapUtils.isNotEmpty(InterceptorUtils.getInterceptors())) {
2225
Iterator<Map.Entry<Integer, IInterceptor>> iterator = InterceptorUtils.getInterceptors().entrySet().iterator();
2326
GoRouter.getInstance().getExecutor().execute(new Runnable() {
@@ -28,7 +31,9 @@ public void run() {
2831
execute(card, iterator, interceptorCounter);
2932
interceptorCounter.await(card.getTimeout(), TimeUnit.SECONDS);
3033
if (interceptorCounter.getCount() > 0) {
31-
callback.onInterrupt(card, new RouterException("The interceptor processing timed out."));
34+
RouterException exception = new RouterException("The interceptor processing timed out.");
35+
GoRouter.logger.warning(null, "[doInterceptions] [onInterrupt] message:" + exception.getMessage());
36+
callback.onInterrupt(card, exception);
3237
} else if (card.getInterceptorException() != null) {
3338
callback.onInterrupt(card, card.getInterceptorException());
3439
} else {
@@ -46,16 +51,18 @@ public void run() {
4651

4752
private static void execute(Card card, Iterator<Map.Entry<Integer, IInterceptor>> iterator, final CancelableCountDownLatch counter) {
4853
if (iterator.hasNext()) {
49-
iterator.next().getValue().process(card, new InterceptorCallback() {
54+
Map.Entry<Integer, IInterceptor> interceptorEntry = iterator.next();
55+
interceptorEntry.getValue().process(card, new InterceptorCallback() {
5056
@Override
5157
public void onContinue(Card card) {
5258
counter.countDown();
5359
execute(card, iterator, counter);
5460
}
5561

5662
@Override
57-
public void onInterrupt(Card card, Throwable exception) {
63+
public void onInterrupt(Card card, @NonNull Throwable exception) {
5864
card.setInterceptorException(exception == null ? new RouterException() : exception);
65+
GoRouter.logger.warning(null, "[doInterceptions] [onInterrupt] {" + interceptorEntry.getKey() + "->" + interceptorEntry.getValue().getClass().getSimpleName() + "} message:" + card.getInterceptorException().getMessage());
5966
counter.cancel();
6067
}
6168
});

GoRouter/src/main/java/com/wyjson/router/interfaces/PathReplaceService.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)