Skip to content

Commit 8cba7bb

Browse files
authored
暴露接口,让业务可以开启Android端内部日志输出,便于调试问题 (alibaba#1853)
1 parent e873f81 commit 8cba7bb

File tree

10 files changed

+151
-69
lines changed

10 files changed

+151
-69
lines changed

android/src/main/java/com/idlefish/flutterboost/FlutterBoost.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void setup(Application application, FlutterBoostDelegate delegate, Callba
6161
options = FlutterBoostSetupOptions.createDefault();
6262
}
6363
isBackForegroundEventOverridden = options.shouldOverrideBackForegroundEvent();
64+
FlutterBoostUtils.setDebugLoggingEnabled(options.isDebugLoggingEnabled());
6465

6566
// 1. initialize default engine
6667
FlutterEngine engine = getEngine();

android/src/main/java/com/idlefish/flutterboost/FlutterBoostPlugin.java

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
2727

2828
public class FlutterBoostPlugin implements FlutterPlugin, NativeRouterApi, ActivityAware {
29-
private static final String TAG = FlutterBoostPlugin.class.getSimpleName();
30-
private static final boolean DEBUG = false;
31-
29+
private static final String TAG = "FlutterBoost_java";
3230
private static final String APP_LIFECYCLE_CHANGED_KEY = "app_lifecycle_changed_key";
3331
private static final String LIFECYCLE_STATE = "lifecycleState";
3432
private static final int FLUTTER_APP_STATE_RESUMED = 0;
@@ -43,6 +41,10 @@ public class FlutterBoostPlugin implements FlutterPlugin, NativeRouterApi, Activ
4341

4442
private HashMap<String, LinkedList<EventListener>> listenersTable = new HashMap<>();
4543

44+
private boolean isDebugLoggingEnabled() {
45+
return FlutterBoostUtils.isDebugLoggingEnabled();
46+
}
47+
4648
public FlutterRouterApi getChannel() {
4749
return channel;
4850
}
@@ -57,7 +59,7 @@ public FlutterBoostDelegate getDelegate() {
5759

5860
@Override
5961
public void onAttachedToEngine(FlutterPluginBinding binding) {
60-
if (DEBUG) Log.v(TAG, "#onAttachedToEngine");
62+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onAttachedToEngine: " + this);
6163
NativeRouterApi.setup(binding.getBinaryMessenger(), this);
6264
engine = binding.getFlutterEngine();
6365
channel = new FlutterRouterApi(binding.getBinaryMessenger());
@@ -66,14 +68,14 @@ public void onAttachedToEngine(FlutterPluginBinding binding) {
6668

6769
@Override
6870
public void onDetachedFromEngine(FlutterPluginBinding binding) {
69-
if (DEBUG) Log.v(TAG, "#onDetachedFromEngine");
71+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onDetachedFromEngine: " + this);
7072
engine = null;
7173
channel = null;
7274
}
7375

7476
@Override
7577
public void pushNativeRoute(CommonParams params) {
76-
if (DEBUG) Log.v(TAG, "#pushNativeRoute: " + params.getPageName());
78+
if (isDebugLoggingEnabled()) Log.d(TAG, "#pushNativeRoute: " + params.getUniqueId() + ", " + this);
7779
if (delegate != null) {
7880
requestCode++;
7981
if (pageNames != null) {
@@ -92,7 +94,7 @@ public void pushNativeRoute(CommonParams params) {
9294

9395
@Override
9496
public void pushFlutterRoute(CommonParams params) {
95-
if (DEBUG) Log.v(TAG, "#pushFlutterRoute: " + params.getPageName() + ", " + params.getUniqueId());
97+
if (isDebugLoggingEnabled()) Log.d(TAG, "#pushFlutterRoute: " + params.getUniqueId() + ", " + this);
9698
if (delegate != null) {
9799
FlutterBoostRouteOptions options = new FlutterBoostRouteOptions.Builder()
98100
.pageName(params.getPageName())
@@ -108,7 +110,7 @@ public void pushFlutterRoute(CommonParams params) {
108110

109111
@Override
110112
public void popRoute(CommonParams params, Messages.Result<Void> result) {
111-
if (DEBUG) Log.v(TAG, "#popRoute: " + params.getPageName() + ", " + params.getUniqueId());
113+
if (isDebugLoggingEnabled()) Log.d(TAG, "#popRoute: " + params.getUniqueId() + ", " + this);
112114
if (delegate != null) {
113115
FlutterBoostRouteOptions options = new FlutterBoostRouteOptions.Builder()
114116
.pageName(params.getPageName())
@@ -142,18 +144,19 @@ public StackInfo getStackFromHost() {
142144
if (dartStack == null) {
143145
return StackInfo.fromMap(new HashMap());
144146
}
145-
if (DEBUG) Log.v(TAG, "#getStackFromHost: " + dartStack);
147+
if (isDebugLoggingEnabled()) Log.d(TAG, "#getStackFromHost: " + dartStack + ", " + this);
146148
return dartStack;
147149
}
148150

149151
@Override
150152
public void saveStackToHost(StackInfo arg) {
151153
dartStack = arg;
152-
if (DEBUG) Log.v(TAG, "#saveStackToHost: " + dartStack);
154+
if (isDebugLoggingEnabled()) Log.d(TAG, "#saveStackToHost: " + dartStack + ", " + this);
153155
}
154156

155157
@Override
156158
public void sendEventToNative(CommonParams arg) {
159+
if (isDebugLoggingEnabled()) Log.d(TAG, "#sendEventToNative: " + this);
157160
//deal with the event from flutter side
158161
String key = arg.getKey();
159162
Map<String, Object> arguments = arg.getArguments();
@@ -174,6 +177,7 @@ public void sendEventToNative(CommonParams arg) {
174177
}
175178

176179
ListenerRemover addEventListener(String key, EventListener listener) {
180+
if (isDebugLoggingEnabled()) Log.d(TAG, "#addEventListener: " + key + ", " + this);
177181
assert (key != null && listener != null);
178182

179183
LinkedList<EventListener> listeners = listenersTable.get(key);
@@ -188,13 +192,15 @@ ListenerRemover addEventListener(String key, EventListener listener) {
188192
}
189193

190194
void sendEventToFlutter(String key, Map<String, Object> args) {
195+
if (isDebugLoggingEnabled()) Log.d(TAG, "#sendEventToFlutter: " + key + ", " + this);
191196
Messages.CommonParams params = new Messages.CommonParams();
192197
params.setKey(key);
193198
params.setArguments(args);
194199
getChannel().sendEventToFlutter(params, reply -> {});
195200
}
196201

197202
void changeFlutterAppLifecycle(int state) {
203+
if (isDebugLoggingEnabled()) Log.d(TAG, "#changeFlutterAppLifecycle: " + state + ", " + this);
198204
assert (state == FLUTTER_APP_STATE_PAUSED || state == FLUTTER_APP_STATE_RESUMED);
199205
Map arguments = new HashMap();
200206
arguments.put(LIFECYCLE_STATE, state);
@@ -211,14 +217,15 @@ private void checkEngineState() {
211217

212218
public void pushRoute(String uniqueId, String pageName, Map<String, Object> arguments,
213219
final FlutterRouterApi.Reply<Void> callback) {
214-
if (DEBUG) Log.v(TAG, "#pushRoute: " + pageName + ", " + uniqueId);
220+
if (isDebugLoggingEnabled()) Log.d(TAG, "#pushRoute start: " + pageName + ", " + uniqueId + ", " + this);
215221
if (channel != null) {
216222
checkEngineState();
217223
CommonParams params = new CommonParams();
218224
params.setUniqueId(uniqueId);
219225
params.setPageName(pageName);
220226
params.setArguments(arguments);
221227
channel.pushRoute(params, reply -> {
228+
if (isDebugLoggingEnabled()) Log.d(TAG, "#pushRoute end: " + pageName + ", " + uniqueId);
222229
if (callback != null) {
223230
callback.reply(null);
224231
}
@@ -229,12 +236,13 @@ public void pushRoute(String uniqueId, String pageName, Map<String, Object> argu
229236
}
230237

231238
public void popRoute(String uniqueId, final FlutterRouterApi.Reply<Void> callback) {
232-
if (DEBUG) Log.v(TAG, "#popRoute: " + uniqueId);
239+
if (isDebugLoggingEnabled()) Log.d(TAG, "#popRoute start: " + uniqueId + ", " + this);
233240
if (channel != null) {
234241
checkEngineState();
235242
CommonParams params = new CommonParams();
236243
params.setUniqueId(uniqueId);
237244
channel.popRoute(params, reply -> {
245+
if (isDebugLoggingEnabled()) Log.d(TAG, "#popRoute end: " + uniqueId + ", " + this);
238246
if (callback != null) {
239247
callback.reply(null);
240248
}
@@ -245,21 +253,25 @@ public void popRoute(String uniqueId, final FlutterRouterApi.Reply<Void> callbac
245253
}
246254

247255
public void onBackPressed() {
256+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onBackPressed start: " + this);
248257
if (channel != null) {
249258
checkEngineState();
250-
channel.onBackPressed(reply -> {});
259+
channel.onBackPressed(reply -> {
260+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onBackPressed end: " + this);
261+
});
251262
} else {
252263
throw new RuntimeException("FlutterBoostPlugin might *NOT* have attached to engine yet!");
253264
}
254265
}
255266

256267
public void removeRoute(String uniqueId, final FlutterRouterApi.Reply<Void> callback) {
257-
if (DEBUG) Log.v(TAG, "#removeRoute: " + uniqueId);
268+
if (isDebugLoggingEnabled()) Log.d(TAG, "#removeRoute start: " + uniqueId + ", " + this);
258269
if (channel != null) {
259270
checkEngineState();
260271
CommonParams params = new CommonParams();
261272
params.setUniqueId(uniqueId);
262273
channel.removeRoute(params, reply -> {
274+
if (isDebugLoggingEnabled()) Log.d(TAG, "#removeRoute end: " + uniqueId + ", " + this);
263275
if (callback != null) {
264276
callback.reply(null);
265277
}
@@ -270,63 +282,67 @@ public void removeRoute(String uniqueId, final FlutterRouterApi.Reply<Void> call
270282
}
271283

272284
public void onForeground() {
285+
Log.d(TAG, "## onForeground start: " + this);
273286
if (channel != null) {
274287
checkEngineState();
275288
CommonParams params = new CommonParams();
276289
channel.onForeground(params, reply -> {
290+
Log.d(TAG, "## onForeground end: " + this);
277291
});
278292

279293
// The scheduling frames are resumed when [onForeground] is called.
280294
changeFlutterAppLifecycle(FLUTTER_APP_STATE_RESUMED);
281295
} else {
282296
throw new RuntimeException("FlutterBoostPlugin might *NOT* have attached to engine yet!");
283297
}
284-
Log.v(TAG, "## onForeground: " + channel);
285298
}
286299

287300
public void onBackground() {
301+
Log.d(TAG, "## onBackground start: " + this);
288302
if (channel != null) {
289303
checkEngineState();
290304
CommonParams params = new CommonParams();
291305
channel.onBackground(params, reply -> {
306+
Log.d(TAG, "## onBackground end: " + this);
292307
});
293308

294309
// The scheduling frames are paused when [onBackground] is called.
295310
changeFlutterAppLifecycle(FLUTTER_APP_STATE_PAUSED);
296311
} else {
297312
throw new RuntimeException("FlutterBoostPlugin might *NOT* have attached to engine yet!");
298313
}
299-
Log.v(TAG, "## onBackground: " + channel);
300314
}
301315

302316
public void onContainerShow(String uniqueId) {
303-
if (DEBUG) Log.v(TAG, "#onContainerShow: " + uniqueId);
317+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onContainerShow start: " + uniqueId + ", " + this);
304318
if (channel != null) {
305319
checkEngineState();
306320
CommonParams params = new CommonParams();
307321
params.setUniqueId(uniqueId);
308322
channel.onContainerShow(params, reply -> {
323+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onContainerShow end: " + uniqueId + ", " + this);
309324
});
310325
} else {
311326
throw new RuntimeException("FlutterBoostPlugin might *NOT* have attached to engine yet!");
312327
}
313328
}
314329

315330
public void onContainerHide(String uniqueId) {
316-
if (DEBUG) Log.v(TAG, "#onContainerHide: " + uniqueId);
331+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onContainerHide start: " + uniqueId + ", " + this);
317332
if (channel != null) {
318333
checkEngineState();
319334
CommonParams params = new CommonParams();
320335
params.setUniqueId(uniqueId);
321336
channel.onContainerHide(params, reply -> {
337+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onContainerHide end: " + uniqueId + ", " + this);
322338
});
323339
} else {
324340
throw new RuntimeException("FlutterBoostPlugin might *NOT* have attached to engine yet!");
325341
}
326342
}
327343

328344
public void onContainerCreated(FlutterViewContainer container) {
329-
if (DEBUG) Log.v(TAG, "#onContainerCreated: " + container.getUniqueId());
345+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onContainerCreated: " + container.getUniqueId() + ", " + this);
330346
FlutterContainerManager.instance().addContainer(container.getUniqueId(), container);
331347
if (FlutterContainerManager.instance().getContainerSize() == 1) {
332348
changeFlutterAppLifecycle(FLUTTER_APP_STATE_RESUMED);
@@ -335,7 +351,7 @@ public void onContainerCreated(FlutterViewContainer container) {
335351

336352
public void onContainerAppeared(FlutterViewContainer container, Runnable onPushRouteComplete) {
337353
String uniqueId = container.getUniqueId();
338-
if (DEBUG) Log.v(TAG, "#onContainerAppeared: " + uniqueId);
354+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onContainerAppeared: " + uniqueId + ", " + this);
339355
FlutterContainerManager.instance().activateContainer(uniqueId, container);
340356
pushRoute(uniqueId, container.getUrl(), container.getUrlParams(), reply -> {
341357
if (FlutterContainerManager.instance().isTopContainer(uniqueId)) {
@@ -350,13 +366,13 @@ public void onContainerAppeared(FlutterViewContainer container, Runnable onPushR
350366

351367
public void onContainerDisappeared(FlutterViewContainer container) {
352368
String uniqueId = container.getUniqueId();
353-
if (DEBUG) Log.v(TAG, "#onContainerDisappeared: " + uniqueId);
369+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onContainerDisappeared: " + uniqueId + ", " + this);
354370
onContainerHide(uniqueId);
355371
}
356372

357373
public void onContainerDestroyed(FlutterViewContainer container) {
358374
String uniqueId = container.getUniqueId();
359-
if (DEBUG) Log.v(TAG, "#onContainerDestroyed: " + uniqueId);
375+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onContainerDestroyed: " + uniqueId + ", " + this);
360376
removeRoute(uniqueId, reply -> {});
361377
FlutterContainerManager.instance().removeContainer(uniqueId);
362378
if (FlutterContainerManager.instance().getContainerSize() == 0) {
@@ -366,7 +382,7 @@ public void onContainerDestroyed(FlutterViewContainer container) {
366382

367383
@Override
368384
public void onAttachedToActivity(ActivityPluginBinding activityPluginBinding) {
369-
if (DEBUG) Log.v(TAG, "#onAttachedToActivity");
385+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onAttachedToActivity: " + this);
370386
activityPluginBinding.addActivityResultListener((requestCode, resultCode, intent) -> {
371387
if (channel != null) {
372388
checkEngineState();
@@ -382,7 +398,7 @@ public void onAttachedToActivity(ActivityPluginBinding activityPluginBinding) {
382398

383399
// Get a result back from an activity when it ends.
384400
channel.onNativeResult(params, reply -> {
385-
if (DEBUG) Log.v(TAG, "#onNativeResult, pageName=" + pageName);
401+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onNativeResult return, pageName=" + pageName + ", " + this);
386402
});
387403
}
388404
} else {
@@ -394,16 +410,16 @@ public void onAttachedToActivity(ActivityPluginBinding activityPluginBinding) {
394410

395411
@Override
396412
public void onDetachedFromActivityForConfigChanges() {
397-
if (DEBUG) Log.v(TAG, "#onDetachedFromActivityForConfigChanges");
413+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onDetachedFromActivityForConfigChanges: " + this);
398414
}
399415

400416
@Override
401417
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding activityPluginBinding) {
402-
if (DEBUG) Log.v(TAG, "#onReattachedToActivityForConfigChanges");
418+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onReattachedToActivityForConfigChanges: " + this);
403419
}
404420

405421
@Override
406422
public void onDetachedFromActivity() {
407-
if (DEBUG) Log.v(TAG, "#onDetachedFromActivity");
423+
if (isDebugLoggingEnabled()) Log.d(TAG, "#onDetachedFromActivity: " + this);
408424
}
409425
}

android/src/main/java/com/idlefish/flutterboost/FlutterBoostSetupOptions.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ public class FlutterBoostSetupOptions {
1010
private final String initialRoute;
1111
private final String dartEntrypoint;
1212
private final String[] shellArgs;
13+
private final boolean isDebugLoggingEnabled;
1314
private final boolean shouldOverrideBackForegroundEvent;
1415
private FlutterEngineProvider flutterEngineProvider;
1516

1617
private FlutterBoostSetupOptions(Builder builder) {
1718
this.initialRoute = builder.initialRoute;
1819
this.dartEntrypoint = builder.dartEntrypoint;
1920
this.shellArgs = builder.shellArgs;
21+
this.isDebugLoggingEnabled = builder.isDebugLoggingEnabled;
2022
this.shouldOverrideBackForegroundEvent = builder.shouldOverrideBackForegroundEvent;
2123
this.flutterEngineProvider = builder.flutterEngineProvider;
2224
}
@@ -41,6 +43,10 @@ public FlutterEngineProvider flutterEngineProvider() {
4143
return flutterEngineProvider;
4244
}
4345

46+
public boolean isDebugLoggingEnabled() {
47+
return isDebugLoggingEnabled;
48+
}
49+
4450
public boolean shouldOverrideBackForegroundEvent() {
4551
return shouldOverrideBackForegroundEvent;
4652
}
@@ -63,13 +69,15 @@ public String toString() {
6369
}
6470
return "initialRoute:" + this.initialRoute +
6571
", dartEntrypoint:" + this.dartEntrypoint +
72+
", isDebugLoggingEnabled: " + this.isDebugLoggingEnabled +
6673
", shouldOverrideBackForegroundEvent:" + this.shouldOverrideBackForegroundEvent +
6774
", shellArgs:" + sb.toString();
6875
}
6976

7077
public static class Builder {
7178
private String initialRoute = "/";
7279
private String dartEntrypoint = "main";
80+
private boolean isDebugLoggingEnabled = false;
7381
private boolean shouldOverrideBackForegroundEvent = false;
7482
private String[] shellArgs;
7583
private FlutterEngineProvider flutterEngineProvider;
@@ -97,6 +105,11 @@ public Builder flutterEngineProvider(FlutterEngineProvider flutterEngineProvider
97105
return this;
98106
}
99107

108+
public Builder isDebugLoggingEnabled(boolean enable) {
109+
isDebugLoggingEnabled = enable;
110+
return this;
111+
}
112+
100113
// Determines whether to override back/foreground event.
101114
public Builder shouldOverrideBackForegroundEvent(boolean override) {
102115
shouldOverrideBackForegroundEvent = override;

android/src/main/java/com/idlefish/flutterboost/FlutterBoostUtils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121
* Helper methods to deal with common tasks.
2222
*/
2323
public class FlutterBoostUtils {
24+
// Control whether the internal debugging logs are turned on.
25+
private static boolean sEnableDebugLogging = false;
26+
27+
public static void setDebugLoggingEnabled(boolean enable) {
28+
sEnableDebugLogging = enable;
29+
}
30+
31+
public static boolean isDebugLoggingEnabled() {
32+
return sEnableDebugLogging;
33+
}
34+
2435
public static String createUniqueId(String name) {
2536
return UUID.randomUUID().toString() + "_" + name;
2637
}

0 commit comments

Comments
 (0)