11package com .crazecoder .flutterbugly ;
22
3- import android .annotation .SuppressLint ;
43import android .app .Activity ;
4+ import android .content .Context ;
55import android .text .TextUtils ;
66
77import androidx .annotation .NonNull ;
8+ import androidx .annotation .Nullable ;
89
910import com .crazecoder .flutterbugly .bean .BuglyInitResultInfo ;
1011import com .crazecoder .flutterbugly .utils .JsonUtil ;
2223import io .flutter .embedding .engine .plugins .FlutterPlugin ;
2324import io .flutter .embedding .engine .plugins .activity .ActivityAware ;
2425import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
26+ import io .flutter .plugin .common .BinaryMessenger ;
2527import io .flutter .plugin .common .MethodCall ;
2628import io .flutter .plugin .common .MethodChannel ;
2729import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
3133/**
3234 * FlutterBuglyPlugin
3335 */
34- public class FlutterBuglyPlugin implements FlutterPlugin , MethodCallHandler , ActivityAware {
36+ public class FlutterBuglyPlugin implements FlutterPlugin , ActivityAware , MethodCallHandler {
37+ private MethodChannel channel ;
38+ private Context applicationContext ;
39+ private Activity activity ;
3540 private Result result ;
3641 private boolean isResultSubmitted = false ;
37- private static MethodChannel channel ;
38- @ SuppressLint ("StaticFieldLeak" )
39- private static Activity activity ;
40- private FlutterPluginBinding flutterPluginBinding ;
4142
4243
4344 /**
4445 * Plugin registration.
4546 */
47+ @ Deprecated
4648 public static void registerWith (Registrar registrar ) {
47- channel = new MethodChannel (registrar .messenger (), "crazecoder/flutter_bugly" );
48- FlutterBuglyPlugin plugin = new FlutterBuglyPlugin ();
49- channel .setMethodCallHandler (plugin );
50- activity = registrar .activity ();
49+ FlutterBuglyPlugin instance = new FlutterBuglyPlugin ();
50+ instance .onAttachedToEngine (registrar .context (), registrar .activity (), registrar .messenger ());
51+ }
52+
53+ //
54+ public void onAttachedToEngine (@ NonNull Context applicationContext , @ Nullable Activity activity , @ NonNull BinaryMessenger messenger ) {
55+ channel = new MethodChannel (messenger , "crazecoder/flutter_bugly" );
56+ channel .setMethodCallHandler (this );
57+ this .applicationContext = applicationContext ;
58+ this .activity = activity ;
59+ }
60+
61+ // --- FlutterPlugin
62+
63+ @ Override
64+ public void onAttachedToEngine (@ NonNull FlutterPluginBinding binding ) {
65+ onAttachedToEngine (binding .getApplicationContext (), null , binding .getBinaryMessenger ());
66+ }
67+
68+ @ Override
69+ public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
70+ channel .setMethodCallHandler (null );
71+ channel = null ;
72+
73+ applicationContext = null ;
74+ }
75+
76+ // --- ActivityAware
77+
78+ @ Override
79+ public void onAttachedToActivity (@ NonNull ActivityPluginBinding binding ) {
80+ activity = binding .getActivity ();
81+ }
82+
83+ @ Override
84+ public void onDetachedFromActivityForConfigChanges () {
85+ onDetachedFromActivity ();
5186 }
5287
88+ @ Override
89+ public void onReattachedToActivityForConfigChanges (@ NonNull ActivityPluginBinding binding ) {
90+ onAttachedToActivity (binding );
91+ }
92+
93+ @ Override
94+ public void onDetachedFromActivity () {
95+ activity = null ;
96+ }
97+
98+ // --- MethodCallHandler
99+
53100 @ Override
54101 public void onMethodCall (final MethodCall call , @ NonNull final Result result ) {
55102 isResultSubmitted = false ;
@@ -97,14 +144,16 @@ public void onUpgrade(int ret, UpgradeInfo strategy, boolean isManual, boolean i
97144 }
98145 } : null ;
99146 }
100- Beta .canShowUpgradeActs .add (activity .getClass ());
147+ if (activity != null ) {
148+ Beta .canShowUpgradeActs .add (activity .getClass ());
149+ }
101150
102151 String appId = call .argument ("appId" ).toString ();
103- Bugly .init (activity . getApplicationContext () , appId , BuildConfig .DEBUG );
152+ Bugly .init (applicationContext , appId , BuildConfig .DEBUG );
104153 if (call .hasArgument ("channel" )) {
105154 String channel = call .argument ("channel" );
106155 if (!TextUtils .isEmpty (channel ))
107- Bugly .setAppChannel (activity . getApplicationContext () , channel );
156+ Bugly .setAppChannel (applicationContext , channel );
108157 }
109158 result (getResultBean (true , appId , "Bugly 初始化成功" ));
110159 } else {
@@ -113,21 +162,21 @@ public void onUpgrade(int ret, UpgradeInfo strategy, boolean isManual, boolean i
113162 } else if (call .method .equals ("setUserId" )) {
114163 if (call .hasArgument ("userId" )) {
115164 String userId = call .argument ("userId" );
116- Bugly .setUserId (activity . getApplicationContext () , userId );
165+ Bugly .setUserId (applicationContext , userId );
117166 }
118167 result (null );
119168 } else if (call .method .equals ("setUserTag" )) {
120169 if (call .hasArgument ("userTag" )) {
121170 Integer userTag = call .argument ("userTag" );
122171 if (userTag != null )
123- Bugly .setUserTag (activity . getApplicationContext () , userTag );
172+ Bugly .setUserTag (applicationContext , userTag );
124173 }
125174 result (null );
126175 } else if (call .method .equals ("putUserData" )) {
127176 if (call .hasArgument ("key" ) && call .hasArgument ("value" )) {
128177 String userDataKey = call .argument ("key" );
129178 String userDataValue = call .argument ("value" );
130- Bugly .putUserData (activity . getApplicationContext () , userDataKey , userDataValue );
179+ Bugly .putUserData (applicationContext , userDataKey , userDataValue );
131180 }
132181 result (null );
133182 } else if (call .method .equals ("checkUpgrade" )) {
@@ -185,37 +234,4 @@ private BuglyInitResultInfo getResultBean(boolean isSuccess, String appId, Strin
185234 bean .setMessage (msg );
186235 return bean ;
187236 }
188-
189- @ Override
190- public void onAttachedToEngine (@ NonNull FlutterPluginBinding binding ) {
191- this .flutterPluginBinding = binding ;
192- }
193-
194- @ Override
195- public void onDetachedFromEngine (@ NonNull FlutterPluginBinding binding ) {
196- channel .setMethodCallHandler (null );
197- flutterPluginBinding = null ;
198- }
199-
200- @ Override
201- public void onAttachedToActivity (@ NonNull ActivityPluginBinding binding ) {
202- activity = binding .getActivity ();
203- channel = new MethodChannel (flutterPluginBinding .getFlutterEngine ().getDartExecutor (), "crazecoder/flutter_bugly" );
204- channel .setMethodCallHandler (this );
205- }
206-
207- @ Override
208- public void onDetachedFromActivityForConfigChanges () {
209-
210- }
211-
212- @ Override
213- public void onReattachedToActivityForConfigChanges (@ NonNull ActivityPluginBinding binding ) {
214-
215- }
216-
217- @ Override
218- public void onDetachedFromActivity () {
219-
220- }
221- }
237+ }
0 commit comments