|
1 | 1 | package com.sensorsdata.analytics.sensorsanalyticsflutterplugin; |
2 | 2 |
|
| 3 | +import android.app.Activity; |
3 | 4 | import android.text.TextUtils; |
4 | 5 |
|
5 | 6 | import androidx.annotation.NonNull; |
6 | 7 |
|
| 8 | +import com.sensorsdata.analytics.android.sdk.SAConfigOptions; |
7 | 9 | import com.sensorsdata.analytics.android.sdk.SALog; |
8 | 10 | import com.sensorsdata.analytics.android.sdk.SensorsDataAPI; |
9 | 11 |
|
|
19 | 21 | import java.util.Map; |
20 | 22 |
|
21 | 23 | import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 24 | +import io.flutter.embedding.engine.plugins.activity.ActivityAware; |
| 25 | +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; |
22 | 26 | import io.flutter.plugin.common.MethodCall; |
23 | 27 | import io.flutter.plugin.common.MethodChannel; |
24 | 28 | import io.flutter.plugin.common.MethodChannel.MethodCallHandler; |
25 | 29 | import io.flutter.plugin.common.MethodChannel.Result; |
26 | | -import io.flutter.plugin.common.PluginRegistry.Registrar; |
27 | 30 |
|
28 | 31 | /** |
29 | 32 | * Sensors Analytics Flutter Plugin |
30 | 33 | */ |
31 | | -public class SensorsAnalyticsFlutterPlugin implements FlutterPlugin, MethodCallHandler { |
| 34 | +public class SensorsAnalyticsFlutterPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware { |
32 | 35 | private MethodChannel channel; |
| 36 | + private Activity mActivity; |
33 | 37 | private static final String TAG = "SA.SensorsAnalyticsFlutterPlugin"; |
34 | 38 |
|
35 | 39 | @Override |
36 | 40 | public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { |
37 | | - channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "sensors_analytics_flutter_plugin"); |
| 41 | + channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "sensors_analytics_flutter_plugin"); |
38 | 42 | channel.setMethodCallHandler(this); |
39 | 43 | } |
40 | 44 |
|
41 | | - public static void registerWith(Registrar registrar) { |
42 | | - final MethodChannel channel = new MethodChannel(registrar.messenger(), "sensors_analytics_flutter_plugin"); |
43 | | - channel.setMethodCallHandler(new SensorsAnalyticsFlutterPlugin()); |
44 | | - } |
45 | | - |
46 | 45 | @Override |
47 | 46 | public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { |
48 | 47 | try { |
@@ -174,15 +173,14 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { |
174 | 173 | case "profileUnsetPushId": |
175 | 174 | profileUnsetPushId(list); |
176 | 175 | break; |
177 | | - case "enableDataCollect": |
178 | | - enableDataCollect(); |
| 176 | + case "init": |
| 177 | + startWithConfig(list); |
179 | 178 | break; |
180 | 179 | default: |
181 | 180 | result.notImplemented(); |
182 | 181 | break; |
183 | 182 | } |
184 | 183 | } catch (Exception e) { |
185 | | - e.printStackTrace(); |
186 | 184 | SALog.d(TAG, e.getMessage()); |
187 | 185 | } |
188 | 186 | } |
@@ -342,7 +340,9 @@ private void login(List list) { |
342 | 340 | /** |
343 | 341 | * trackViewScreen 触发 $AppViewScreen 事件 |
344 | 342 | */ |
| 343 | + @SuppressWarnings("deprecation") |
345 | 344 | private void trackViewScreen(List list) { |
| 345 | + //使用了 Deprecated 方法,编译时会产生 xxxx 类使用或覆盖了已过时的 API。 |
346 | 346 | SensorsDataAPI.sharedInstance().trackViewScreen((String) list.get(0), assertProperties((Map) list.get(1))); |
347 | 347 | } |
348 | 348 |
|
@@ -490,11 +490,86 @@ private void profileUnsetPushId(List list) { |
490 | 490 | SensorsDataAPI.sharedInstance().profileUnsetPushId((String) list.get(0)); |
491 | 491 | } |
492 | 492 |
|
493 | | - /** |
494 | | - * 开启数据采集 |
495 | | - */ |
496 | | - private void enableDataCollect() { |
497 | | - SensorsDataAPI.sharedInstance().enableDataCollect(); |
| 493 | + private void startWithConfig(List list) { |
| 494 | + Map map = (Map) list.get(0); |
| 495 | + Object serverUrl = map.get("serverUrl"); |
| 496 | + |
| 497 | + SAConfigOptions configOptions = new SAConfigOptions(serverUrl == null ? "" : serverUrl.toString()); |
| 498 | + Object autotrackTypes = map.get("autotrackTypes"); |
| 499 | + if (autotrackTypes != null) { |
| 500 | + configOptions.setAutoTrackEventType((Integer) autotrackTypes); |
| 501 | + } |
| 502 | + |
| 503 | + Object networkTypes = map.get("networkTypes"); |
| 504 | + if (networkTypes != null) { |
| 505 | + configOptions.setNetworkTypePolicy((Integer) networkTypes); |
| 506 | + } |
| 507 | + |
| 508 | + Object flushInterval = map.get("flushInterval"); |
| 509 | + if (flushInterval != null) { |
| 510 | + configOptions.setFlushInterval((Integer) flushInterval); |
| 511 | + } |
| 512 | + |
| 513 | + Object flushBulkSize = map.get("flushBulkSize"); |
| 514 | + if (flushBulkSize != null) { |
| 515 | + configOptions.setFlushBulkSize((Integer) flushBulkSize); |
| 516 | + } |
| 517 | + |
| 518 | + Object enableLog = map.get("enableLog"); |
| 519 | + if (enableLog != null) { |
| 520 | + configOptions.enableLog((Boolean) enableLog); |
| 521 | + } |
| 522 | + |
| 523 | + Object encrypt = map.get("encrypt"); |
| 524 | + if (encrypt != null) { |
| 525 | + configOptions.enableEncrypt((Boolean) encrypt); |
| 526 | + } |
| 527 | + |
| 528 | + Object heatMap = map.get("heatMap"); |
| 529 | + if (heatMap != null) { |
| 530 | + configOptions.enableHeatMap((Boolean) heatMap); |
| 531 | + } |
| 532 | + |
| 533 | + Object androidConfig = map.get("android"); |
| 534 | + boolean jellybean = false; |
| 535 | + if (androidConfig != null) { |
| 536 | + Map androidConfigMap = (Map) androidConfig; |
| 537 | + Object maxCacheSize = androidConfigMap.get("maxCacheSize"); |
| 538 | + if (maxCacheSize != null) { |
| 539 | + configOptions.setMaxCacheSize(Long.parseLong(maxCacheSize.toString())); |
| 540 | + } |
| 541 | + |
| 542 | + Object jellybeanObj = androidConfigMap.get("jellybean"); |
| 543 | + if (jellybeanObj != null) { |
| 544 | + jellybean = (boolean) jellybeanObj; |
| 545 | + } |
| 546 | + Object subProcessFlush = androidConfigMap.get("subProcessFlush"); |
| 547 | + if (subProcessFlush != null && (boolean) subProcessFlush) { |
| 548 | + configOptions.enableSubProcessFlushData(); |
| 549 | + } |
| 550 | + } |
| 551 | + |
| 552 | + Object javaScriptBridge = map.get("javaScriptBridge"); |
| 553 | + if (javaScriptBridge != null && (boolean) javaScriptBridge) { |
| 554 | + configOptions.enableJavaScriptBridge(jellybean); |
| 555 | + } |
| 556 | + |
| 557 | + Object visualizedConfig = map.get("visualized"); |
| 558 | + if (visualizedConfig != null) { |
| 559 | + Map visualizedConfigMap = (Map) visualizedConfig; |
| 560 | + Object autoTrack = visualizedConfigMap.get("autoTrack"); |
| 561 | + if (autoTrack != null) { |
| 562 | + configOptions.enableVisualizedAutoTrack((Boolean) autoTrack); |
| 563 | + } |
| 564 | + |
| 565 | + Object properties = visualizedConfigMap.get("properties"); |
| 566 | + if (properties != null) { |
| 567 | + configOptions.enableVisualizedProperties((Boolean) properties); |
| 568 | + } |
| 569 | + } |
| 570 | + |
| 571 | + |
| 572 | + SensorsDataAPI.startWithConfigOptions(mActivity, configOptions); |
498 | 573 | } |
499 | 574 |
|
500 | 575 | private JSONObject assertProperties(Map map) { |
@@ -522,4 +597,23 @@ private String assertEventName(String eventName) { |
522 | 597 | return eventName; |
523 | 598 | } |
524 | 599 |
|
| 600 | + @Override |
| 601 | + public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { |
| 602 | + mActivity = binding.getActivity(); |
| 603 | + } |
| 604 | + |
| 605 | + @Override |
| 606 | + public void onDetachedFromActivityForConfigChanges() { |
| 607 | + |
| 608 | + } |
| 609 | + |
| 610 | + @Override |
| 611 | + public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) { |
| 612 | + mActivity = binding.getActivity(); |
| 613 | + } |
| 614 | + |
| 615 | + @Override |
| 616 | + public void onDetachedFromActivity() { |
| 617 | + mActivity = null; |
| 618 | + } |
525 | 619 | } |
0 commit comments