4040import io .flutter .plugin .common .MethodChannel ;
4141import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
4242import io .flutter .plugin .common .MethodChannel .Result ;
43+ import io .flutter .plugin .common .PluginRegistry ;
4344
4445import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_EVENTS_CHANNEL ;
4546import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_FAILURE ;
46- import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_ON_APP_OPEN_ATTRIBUTION ;
47- import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_ON_INSTALL_CONVERSION_DATA_LOADED ;
4847import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_SUCCESS ;
4948
5049/**
@@ -58,6 +57,7 @@ public class AppsflyerSdkPlugin implements MethodCallHandler, FlutterPlugin, Act
5857 private static String cachedOnAttributionFailure ;
5958 private static String cachedOnConversionDataFail ;
6059 private static DeepLinkResult cachedDeepLinkResult ;
60+
6161 final Handler uiThreadHandler = new Handler (Looper .getMainLooper ());
6262 private EventChannel mEventChannel ;
6363 /**
@@ -77,42 +77,16 @@ public class AppsflyerSdkPlugin implements MethodCallHandler, FlutterPlugin, Act
7777 private Boolean isFacebookDeferredApplinksEnabled = false ;
7878 private Boolean isSetDisableAdvertisingIdentifiersEnable = false ;
7979 private Map <String , Map <String , Object >> mCallbacks = new HashMap <>();
80- MethodCallHandler callbacksHandler = new MethodCallHandler () {
80+
81+ PluginRegistry .NewIntentListener onNewIntentListener = new PluginRegistry .NewIntentListener () {
8182 @ Override
82- public void onMethodCall (MethodCall call , Result result ) {
83- final String method = call .method ;
84- if ("startListening" .equals (method )) {
85- startListening (call .arguments , result );
86- } else {
87- result .notImplemented ();
88- }
83+ public boolean onNewIntent (Intent intent ) {
84+ activity .setIntent (intent );
85+ return false ;
8986 }
9087 };
91- private DeepLinkListener deepLinkListener = new DeepLinkListener () {
9288
93- @ Override
94- public void onDeepLinking (DeepLinkResult deepLinkResult ) {
95- if (saveCallbacks ) {
96- cachedDeepLinkResult = deepLinkResult ;
97- return ;
98- }
99- if (udlCallback ) {
100- runOnUIThread (deepLinkResult , AppsFlyerConstants .AF_UDL_CALLBACK , AF_SUCCESS );
101- } else {
102- try {
103- JSONObject obj = new JSONObject ();
104- obj .put ("status" , AF_SUCCESS );
105- obj .put ("type" , AppsFlyerConstants .AF_UDL_CALLBACK );
106- obj .put ("data" , deepLinkResult .getDeepLink ().getClickEvent ());
107-
108- sendEventToDart (obj , AF_EVENTS_CHANNEL );
109- } catch (JSONException e ) {
110- e .printStackTrace ();
111- }
112- }
113- }
114- };
115- private AppsFlyerConversionListener afConversionListener = new AppsFlyerConversionListener () {
89+ private final AppsFlyerConversionListener afConversionListener = new AppsFlyerConversionListener () {
11690 @ Override
11791 public void onConversionDataSuccess (Map <String , Object > map ) {
11892 if (saveCallbacks ) {
@@ -122,8 +96,6 @@ public void onConversionDataSuccess(Map<String, Object> map) {
12296 if (gcdCallback ) {
12397 JSONObject dataObj = new JSONObject (replaceNullValues (map ));
12498 runOnUIThread (dataObj , AppsFlyerConstants .AF_GCD_CALLBACK , AF_SUCCESS );
125- } else {
126- handleSuccess (AF_ON_INSTALL_CONVERSION_DATA_LOADED , map , AF_EVENTS_CHANNEL );
12799 }
128100 }
129101
@@ -136,8 +108,6 @@ public void onConversionDataFail(String s) {
136108 if (gcdCallback ) {
137109 JSONObject obj = buildJsonResponse (s , AF_FAILURE );
138110 runOnUIThread (obj , AppsFlyerConstants .AF_GCD_CALLBACK , AF_FAILURE );
139- } else {
140- handleError (AF_ON_INSTALL_CONVERSION_DATA_LOADED , s , AF_EVENTS_CHANNEL );
141111 }
142112 }
143113
@@ -151,8 +121,6 @@ public void onAppOpenAttribution(Map<String, String> map) {
151121 if (oaoaCallback ) {
152122 JSONObject obj = new JSONObject (replaceNullValues (objMap ));
153123 runOnUIThread (obj , AppsFlyerConstants .AF_OAOA_CALLBACK , AF_SUCCESS );
154- } else {
155- handleSuccess (AF_ON_APP_OPEN_ATTRIBUTION , objMap , AF_EVENTS_CHANNEL );
156124 }
157125 }
158126
@@ -165,23 +133,46 @@ public void onAttributionFailure(String errorMessage) {
165133 if (oaoaCallback ) {
166134 JSONObject obj = buildJsonResponse (errorMessage , AF_FAILURE );
167135 runOnUIThread (obj , AppsFlyerConstants .AF_OAOA_CALLBACK , AF_FAILURE );
136+ }
137+ }
138+ };
139+ private final DeepLinkListener afDeepLinkListener = new DeepLinkListener () {
140+
141+ @ Override
142+ public void onDeepLinking (DeepLinkResult deepLinkResult ) {
143+ if (saveCallbacks ) {
144+ cachedDeepLinkResult = deepLinkResult ;
145+ return ;
146+ }
147+ if (udlCallback ) {
148+ runOnUIThread (deepLinkResult , AppsFlyerConstants .AF_UDL_CALLBACK , AF_SUCCESS );
149+ }
150+ }
151+ };
152+
153+ private final MethodCallHandler callbacksHandler = new MethodCallHandler () {
154+ @ Override
155+ public void onMethodCall (MethodCall call , Result result ) {
156+ final String method = call .method ;
157+ if ("startListening" .equals (method )) {
158+ startListening (call .arguments , result );
168159 } else {
169- handleError ( AF_ON_APP_OPEN_ATTRIBUTION , errorMessage , AF_EVENTS_CHANNEL );
160+ result . notImplemented ( );
170161 }
171162 }
172163 };
173164
174165 private void onAttachedToEngine (Context applicationContext , BinaryMessenger messenger ) {
175166 this .mContext = applicationContext ;
176167 this .mEventChannel = new EventChannel (messenger , AF_EVENTS_CHANNEL );
177- mEventChannel .setStreamHandler (new AppsFlyerStreamHandler (mContext ));
178168 mMethodChannel = new MethodChannel (messenger , AppsFlyerConstants .AF_METHOD_CHANNEL );
179169 mMethodChannel .setMethodCallHandler (this );
180170 mCallbackChannel = new MethodChannel (messenger , AppsFlyerConstants .AF_CALLBACK_CHANNEL );
181171 mCallbackChannel .setMethodCallHandler (callbacksHandler );
182172
183173 }
184174
175+
185176 private void startListening (Object arguments , Result rawResult ) {
186177 // Get callback id
187178 String callbackName = (String ) arguments ;
@@ -325,7 +316,7 @@ private void setDisableAdvertisingIdentifiers(MethodCall call, Result result) {
325316
326317 private void enableFacebookDeferredApplinks (MethodCall call , Result result ) {
327318 isFacebookDeferredApplinksEnabled = (boolean ) call .argument ("isFacebookDeferredApplinksEnabled" );
328- ;
319+
329320 if (isFacebookDeferredApplinksEnabled ) {
330321 AppsFlyerLib .getInstance ().enableFacebookDeferredApplinks (true );
331322 } else {
@@ -415,7 +406,7 @@ private void generateInviteLink(MethodCall call, Result rawResult) {
415406 }
416407
417408 CreateOneLinkHttpTask .ResponseListener listener = new CreateOneLinkHttpTask .ResponseListener () {
418- JSONObject obj = new JSONObject ();
409+ final JSONObject obj = new JSONObject ();
419410
420411 @ Override
421412 public void onResponse (final String oneLinkUrl ) {
@@ -456,8 +447,21 @@ public void run() {
456447 JSONObject args = new JSONObject ();
457448 try {
458449 args .put ("id" , callbackName );
459- args .put ("status" , status );
460- args .put ("data" , data .toString ());
450+ //return data for UDL
451+ if (callbackName .equals (AppsFlyerConstants .AF_UDL_CALLBACK )) {
452+ DeepLinkResult dp = (DeepLinkResult ) data ;
453+ args .put ("deepLinkStatus" , dp .getStatus ().toString ());
454+ if (dp .getError () != null ) {
455+ args .put ("deepLinkError" , dp .getError ().toString ());
456+ }
457+ if (dp .getStatus () == DeepLinkResult .Status .FOUND ) {
458+ args .put ("deepLinkObj" , dp .getDeepLink ().AFInAppEventParameterName );
459+ }
460+ } else { // return data for conversionData and OAOA
461+ JSONObject dataJSON = (JSONObject ) data ;
462+ args .put ("status" , status );
463+ args .put ("data" , data .toString ());
464+ }
461465 } catch (JSONException e ) {
462466 e .printStackTrace ();
463467 }
@@ -507,30 +511,19 @@ private void registerValidatorListener() {
507511 AppsFlyerInAppPurchaseValidatorListener validatorListener = new AppsFlyerInAppPurchaseValidatorListener () {
508512 @ Override
509513 public void onValidateInApp () {
510- try {
511- JSONObject obj = new JSONObject ();
512- if (validatePurchaseCallback ) {
513- runOnUIThread (obj , AppsFlyerConstants .AF_VALIDATE_PURCHASE , AF_SUCCESS );
514- } else {
515- obj .put ("status" , AF_SUCCESS );
516- sendEventToDart (obj , AF_EVENTS_CHANNEL );
517- }
518- } catch (JSONException e ) {
519- e .printStackTrace ();
514+ if (validatePurchaseCallback ) {
515+ runOnUIThread (new JSONObject (), AppsFlyerConstants .AF_VALIDATE_PURCHASE , AF_SUCCESS );
520516 }
521-
522517 }
523518
519+
524520 @ Override
525521 public void onValidateInAppFailure (String s ) {
526522 try {
527523 JSONObject obj = new JSONObject ();
528524 obj .put ("error" , s );
529525 if (validatePurchaseCallback ) {
530526 runOnUIThread (obj , AppsFlyerConstants .AF_VALIDATE_PURCHASE , AF_FAILURE );
531- } else {
532- obj .put ("status" , AF_FAILURE );
533- sendEventToDart (obj , AF_EVENTS_CHANNEL );
534527 }
535528 } catch (JSONException e ) {
536529 e .printStackTrace ();
@@ -667,7 +660,7 @@ private void initSdk(MethodCall call, final MethodChannel.Result result) {
667660 // added Unified deeplink
668661 boolean getUdl = (boolean ) call .argument (AppsFlyerConstants .AF_UDL );
669662 if (getUdl ) {
670- instance .subscribeForDeepLink (deepLinkListener );
663+ instance .subscribeForDeepLink (afDeepLinkListener );
671664 }
672665
673666 boolean isDebug = (boolean ) call .argument (AppsFlyerConstants .AF_IS_DEBUG );
@@ -710,6 +703,10 @@ private void logEvent(MethodCall call, MethodChannel.Result result) {
710703
711704 //RD-65582
712705 private void sendCachedCallbacksToDart () {
706+ if (cachedDeepLinkResult != null ) {
707+ afDeepLinkListener .onDeepLinking (cachedDeepLinkResult );
708+ cachedDeepLinkResult = null ;
709+ }
713710 if (cachedOnConversionDataSuccess != null ) {
714711 afConversionListener .onConversionDataSuccess (cachedOnConversionDataSuccess );
715712 cachedOnConversionDataSuccess = null ;
@@ -726,41 +723,9 @@ private void sendCachedCallbacksToDart() {
726723 afConversionListener .onConversionDataFail (cachedOnConversionDataFail );
727724 cachedOnConversionDataFail = null ;
728725 }
729- if (cachedDeepLinkResult != null ) {
730- deepLinkListener .onDeepLinking (cachedDeepLinkResult );
731- cachedDeepLinkResult = null ;
732- }
733726 }
734727
735728
736- private void handleSuccess (String eventType , Map <String , Object > data , String channel ) {
737- try {
738- JSONObject obj = new JSONObject ();
739- obj .put ("status" , AF_SUCCESS );
740- obj .put ("type" , eventType );
741- obj .put ("data" , new JSONObject (replaceNullValues (data )));
742-
743- sendEventToDart (obj , channel );
744- } catch (JSONException e ) {
745- e .printStackTrace ();
746- }
747- }
748-
749- private void handleError (String eventType , String errorMessage , String channel ) {
750-
751- try {
752- JSONObject obj = new JSONObject ();
753-
754- obj .put ("status" , AF_FAILURE );
755- obj .put ("type" , eventType );
756- obj .put ("data" , errorMessage );
757-
758- sendEventToDart (obj , channel );
759- } catch (JSONException e ) {
760- e .printStackTrace ();
761- }
762- }
763-
764729 private JSONObject buildJsonResponse (Object data , String status ) {
765730 JSONObject obj = new JSONObject ();
766731 try {
@@ -786,13 +751,6 @@ private Map<String, Object> replaceNullValues(Map<String, Object> map) {
786751 return newMap ;
787752 }
788753
789- private void sendEventToDart (final JSONObject params , String channel ) {
790- Intent intent = new Intent ();
791- intent .addFlags (Intent .FLAG_INCLUDE_STOPPED_PACKAGES );
792- intent .setAction (AppsFlyerConstants .AF_BROADCAST_ACTION_NAME );
793- intent .putExtra ("params" , params .toString ());
794- LocalBroadcastManager .getInstance (mContext ).sendBroadcast (intent );
795- }
796754
797755 @ Override
798756 public void onAttachedToEngine (FlutterPluginBinding binding ) {
@@ -812,6 +770,7 @@ public void onAttachedToActivity(ActivityPluginBinding binding) {
812770 activity = binding .getActivity ();
813771 mIntent = binding .getActivity ().getIntent ();
814772 mApplication = binding .getActivity ().getApplication ();
773+ binding .addOnNewIntentListener (onNewIntentListener );
815774 }
816775
817776 @ Override
@@ -822,14 +781,13 @@ public void onDetachedFromActivityForConfigChanges() {
822781 @ Override
823782 public void onReattachedToActivityForConfigChanges (ActivityPluginBinding binding ) {
824783 sendCachedCallbacksToDart ();
784+ binding .addOnNewIntentListener (onNewIntentListener );
825785 }
826786
827787 @ Override
828788 public void onDetachedFromActivity () {
829789 activity = null ;
830790 saveCallbacks = true ;
831-
832-
833791 }
834792
835793}
0 commit comments