4949
5050import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_EVENTS_CHANNEL ;
5151import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_FAILURE ;
52+ import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_PLUGIN_TAG ;
5253import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_SUCCESS ;
5354
5455/**
@@ -203,7 +204,7 @@ private void startListening(Object arguments, Result rawResult) {
203204 @ Override
204205 public void onMethodCall (MethodCall call , Result result ) {
205206 if (activity == null ) {
206- Log .d ("AppsFlyer" , "Activity isn't attached to the flutter engine" );
207+ Log .d (AF_PLUGIN_TAG , LogMessages . ACTIVITY_NOT_ATTACHED_TO_ENGINE );
207208 return ;
208209 }
209210 final String method = call .method ;
@@ -362,11 +363,11 @@ private void performOnDeepLinking(MethodCall call, Result result) {
362363 AppsFlyerLib .getInstance ().performOnDeepLinking (intent , mApplication );
363364 result .success (null );
364365 } else {
365- Log .d ("AppsFlyer" , "performOnDeepLinking: intent is null!" );
366+ Log .d (AF_PLUGIN_TAG , "performOnDeepLinking: intent is null!" );
366367 result .error ("NO_INTENT" , "The intent is null" , null );
367368 }
368369 } else {
369- Log .d ("AppsFlyer" , "performOnDeepLinking: activity is null!" );
370+ Log .d (AF_PLUGIN_TAG , "performOnDeepLinking: activity is null!" );
370371 result .error ("NO_ACTIVITY" , "The current activity is null" , null );
371372 }
372373 }
@@ -379,34 +380,37 @@ private void anonymizeUser(MethodCall call, Result result) {
379380
380381 private void startSDKwithHandler (MethodCall call , final Result result ) {
381382 try {
382- final AppsFlyerLib instance = AppsFlyerLib .getInstance ();
383- instance .start (activity , null , new AppsFlyerRequestListener () {
383+ final AppsFlyerLib appsFlyerLib = AppsFlyerLib .getInstance ();
384+
385+ appsFlyerLib .start (activity , null , new AppsFlyerRequestListener () {
384386 @ Override
385387 public void onSuccess () {
386- uiThreadHandler . post ( new Runnable ( ) {
387- @ Override
388- public void run () {
389- mMethodChannel . invokeMethod ( "onSuccess" , null );
390- }
391- });
388+ if ( mMethodChannel != null ) {
389+ uiThreadHandler . post (() -> mMethodChannel . invokeMethod ( "onSuccess" , null ));
390+ } else {
391+ Log . e ( AF_PLUGIN_TAG , LogMessages . METHOD_CHANNEL_IS_NULL );
392+ result . error ( "NULL_OBJECT" , LogMessages . METHOD_CHANNEL_IS_NULL , null );
393+ }
392394 }
393395
394396 @ Override
395397 public void onError (final int errorCode , final String errorMessage ) {
396- uiThreadHandler .post (new Runnable () {
397- @ Override
398- public void run () {
398+ if (mMethodChannel != null ) {
399+ uiThreadHandler .post (() -> {
399400 HashMap <String , Object > errorDetails = new HashMap <>();
400401 errorDetails .put ("errorCode" , errorCode );
401402 errorDetails .put ("errorMessage" , errorMessage );
402403 mMethodChannel .invokeMethod ("onError" , errorDetails );
403- }
404- });
404+ });
405+ } else {
406+ Log .e (AF_PLUGIN_TAG , LogMessages .METHOD_CHANNEL_IS_NULL );
407+ result .error ("NULL_OBJECT" , LogMessages .METHOD_CHANNEL_IS_NULL , null );
408+ }
405409 }
406410 });
407411 result .success (null );
408- } catch (Exception e ) {
409- result .error ("UNEXPECTED_ERROR" , e .getMessage (), null );
412+ } catch (Throwable t ) {
413+ result .error ("UNEXPECTED_ERROR" , t .getMessage (), null );
410414 }
411415 }
412416
@@ -532,14 +536,14 @@ private void sendPushNotificationData(MethodCall call, Result result) {
532536 Bundle bundle ;
533537
534538 if (pushPayload == null ) {
535- Log .d ("AppsFlyer" , "Push payload is null" );
539+ Log .d (AF_PLUGIN_TAG , "Push payload is null" );
536540 return ;
537541 }
538542
539543 try {
540544 bundle = this .jsonToBundle (new JSONObject (pushPayload ));
541545 } catch (JSONException e ) {
542- Log .d ("AppsFlyer" , "Can't parse pushPayload to bundle" );
546+ Log .d (AF_PLUGIN_TAG , "Can't parse pushPayload to bundle" );
543547 return ;
544548 }
545549
@@ -557,7 +561,7 @@ private void sendPushNotificationData(MethodCall call, Result result) {
557561 }
558562
559563 if (errorMsg != null ) {
560- Log .d ("AppsFlyer" , errorMsg );
564+ Log .d (AF_PLUGIN_TAG , errorMsg );
561565 return ;
562566 }
563567
@@ -963,8 +967,8 @@ private void logAdRevenue(MethodCall call, Result result) {
963967 try {
964968 String monetizationNetwork = requireNonNullArgument (call , "monetizationNetwork" );
965969 String currencyIso4217Code = requireNonNullArgument (call , "currencyIso4217Code" );
966- double revenue = requireNonNullArgument (call ,"revenue" );
967- String mediationNetworkString = requireNonNullArgument (call ,"mediationNetwork" );
970+ double revenue = requireNonNullArgument (call , "revenue" );
971+ String mediationNetworkString = requireNonNullArgument (call , "mediationNetwork" );
968972
969973 MediationNetwork mediationNetwork = MediationNetwork .valueOf (mediationNetworkString .toUpperCase ());
970974
@@ -984,10 +988,9 @@ private void logAdRevenue(MethodCall call, Result result) {
984988 } catch (IllegalArgumentException e ) {
985989 // The IllegalArgumentException could come from either requireNonNullArgument or valueOf methods.
986990 result .error ("INVALID_ARGUMENT_PROVIDED" , e .getMessage (), null );
987- }
988- catch (Throwable t ) {
991+ } catch (Throwable t ) {
989992 result .error ("UNEXPECTED_ERROR" , "[logAdRevenue]: An unexpected error occurred: " + t .getMessage (), null );
990- Log .e ("AppsFlyer" , "Unexpected exception occurred: [logAdRevenue]" , t );
993+ Log .e (AF_PLUGIN_TAG , "Unexpected exception occurred: [logAdRevenue]" , t );
991994 }
992995 }
993996
@@ -1004,7 +1007,7 @@ private void logAdRevenue(MethodCall call, Result result) {
10041007 private <T > T requireNonNullArgument (MethodCall call , String argumentName ) throws IllegalArgumentException {
10051008 T argument = call .argument (argumentName );
10061009 if (argument == null ) {
1007- Log .e ("AppsFlyer" , "Exception occurred when trying to: " + call .method + "->" + argumentName + " must not be null" );
1010+ Log .e (AF_PLUGIN_TAG , "Exception occurred when trying to: " + call .method + "->" + argumentName + " must not be null" );
10081011 throw new IllegalArgumentException ("[" + call .method + "]: " + argumentName + " must not be null" );
10091012 }
10101013 return argument ;
0 commit comments