5252import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_PLUGIN_TAG ;
5353import static com .appsflyer .appsflyersdk .AppsFlyerConstants .AF_SUCCESS ;
5454
55+ import androidx .annotation .NonNull ;
56+
5557/**
5658 * AppsflyerSdkPlugin
5759 */
@@ -72,7 +74,6 @@ public class AppsflyerSdkPlugin implements MethodCallHandler, FlutterPlugin, Act
7274 //private FlutterView mFlutterView;
7375 private Context mContext ;
7476 private Application mApplication ;
75- private Intent mIntent ;
7677 private MethodChannel mMethodChannel ;
7778 private MethodChannel mCallbackChannel ;
7879 private Activity activity ;
@@ -175,7 +176,6 @@ private void onAttachedToEngine(Context applicationContext, BinaryMessenger mess
175176 mMethodChannel .setMethodCallHandler (this );
176177 mCallbackChannel = new MethodChannel (messenger , AppsFlyerConstants .AF_CALLBACK_CHANNEL );
177178 mCallbackChannel .setMethodCallHandler (callbacksHandler );
178-
179179 }
180180
181181
@@ -205,6 +205,7 @@ private void startListening(Object arguments, Result rawResult) {
205205 public void onMethodCall (MethodCall call , Result result ) {
206206 if (activity == null ) {
207207 Log .d (AF_PLUGIN_TAG , LogMessages .ACTIVITY_NOT_ATTACHED_TO_ENGINE );
208+ result .error ("NO_ACTIVITY" , "The current activity is null" , null );
208209 return ;
209210 }
210211 final String method = call .method ;
@@ -233,6 +234,9 @@ public void onMethodCall(MethodCall call, Result result) {
233234 case "setConsentData" :
234235 setConsentData (call , result );
235236 break ;
237+ case "setConsentDataV2" :
238+ setConsentDataV2 (call , result );
239+ break ;
236240 case "setIsUpdate" :
237241 setIsUpdate (call , result );
238242 break ;
@@ -424,6 +428,11 @@ private void startSDK(MethodCall call, final Result result) {
424428 result .success (null );
425429 }
426430
431+ /**
432+ * Sets the user consent data for tracking.
433+ * @deprecated Use {@link #setConsentDataV2(MethodCall, Result)} instead.
434+ */
435+ @ Deprecated
427436 public void setConsentData (MethodCall call , Result result ) {
428437 Map <String , Object > arguments = (Map <String , Object >) call .arguments ;
429438 Map <String , Object > consentDict = (Map <String , Object >) arguments .get ("consentData" );
@@ -445,6 +454,35 @@ public void setConsentData(MethodCall call, Result result) {
445454 result .success (null );
446455 }
447456
457+ /**
458+ * Sets the user consent data for tracking with flexible parameters.
459+ */
460+ public void setConsentDataV2 (MethodCall call , Result result ) {
461+ try {
462+ AppsFlyerConsent consent = getAppsFlyerConsentFromCall (call );
463+ AppsFlyerLib .getInstance ().setConsentData (consent );
464+ result .success (null );
465+ } catch (Exception e ) {
466+ Log .e (AF_PLUGIN_TAG , LogMessages .ERROR_WHILE_SETTING_CONSENT + e .getMessage (), e );
467+ result .error ("CONSENT_ERROR" , LogMessages .ERROR_WHILE_SETTING_CONSENT + e .getMessage (), null );
468+ }
469+ }
470+
471+ @ NonNull
472+ @ SuppressWarnings ("unchecked" )
473+ private AppsFlyerConsent getAppsFlyerConsentFromCall (MethodCall call ) {
474+ Map <String , Object > args = (Map <String , Object >) call .arguments ;
475+
476+ // Extract nullable Boolean arguments
477+ Boolean isUserSubjectToGDPR = (Boolean ) args .get ("isUserSubjectToGDPR" );
478+ Boolean consentForDataUsage = (Boolean ) args .get ("consentForDataUsage" );
479+ Boolean consentForAdsPersonalization = (Boolean ) args .get ("consentForAdsPersonalization" );
480+ Boolean hasConsentForAdStorage = (Boolean ) args .get ("hasConsentForAdStorage" );
481+
482+ // Create and return AppsFlyerConsent object with the given parameters
483+ return new AppsFlyerConsent (isUserSubjectToGDPR , consentForDataUsage , consentForAdsPersonalization , hasConsentForAdStorage );
484+ }
485+
448486 private void enableTCFDataCollection (MethodCall call , Result result ) {
449487 boolean shouldCollect = (boolean ) call .argument ("shouldCollect" );
450488 AppsFlyerLib .getInstance ().enableTCFDataCollection (shouldCollect );
@@ -1075,31 +1113,33 @@ public void onDetachedFromEngine(FlutterPluginBinding binding) {
10751113 mMethodChannel = null ;
10761114 mEventChannel .setStreamHandler (null );
10771115 mEventChannel = null ;
1116+ mContext = null ;
1117+ mApplication = null ;
10781118 }
10791119
10801120 @ Override
10811121 public void onAttachedToActivity (ActivityPluginBinding binding ) {
10821122 activity = binding .getActivity ();
1083- mIntent = binding .getActivity ().getIntent ();
10841123 mApplication = binding .getActivity ().getApplication ();
10851124 binding .addOnNewIntentListener (onNewIntentListener );
10861125 }
10871126
10881127 @ Override
10891128 public void onDetachedFromActivityForConfigChanges () {
1090-
1129+ this . activity = null ;
10911130 }
10921131
10931132 @ Override
10941133 public void onReattachedToActivityForConfigChanges (ActivityPluginBinding binding ) {
10951134 sendCachedCallbacksToDart ();
10961135 binding .addOnNewIntentListener (onNewIntentListener );
1136+ activity = binding .getActivity ();
10971137 }
10981138
10991139 @ Override
11001140 public void onDetachedFromActivity () {
11011141 activity = null ;
11021142 saveCallbacks = true ;
1143+ AppsFlyerLib .getInstance ().unregisterConversionListener ();
11031144 }
1104-
11051145}
0 commit comments