From 25fb530b85c2dd46e5fdcbac85784067f89514f4 Mon Sep 17 00:00:00 2001 From: Dani-Koza-AF Date: Wed, 30 Oct 2024 14:26:26 +0200 Subject: [PATCH] This should fix the NullPointerException --- .../appsflyersdk/AppsFlyerConstants.java | 52 +++++++++-------- .../appsflyersdk/AppsflyerSdkPlugin.java | 57 ++++++++++--------- .../appsflyer/appsflyersdk/LogMessages.java | 12 ++++ 3 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 android/src/main/java/com/appsflyer/appsflyersdk/LogMessages.java diff --git a/android/src/main/java/com/appsflyer/appsflyersdk/AppsFlyerConstants.java b/android/src/main/java/com/appsflyer/appsflyersdk/AppsFlyerConstants.java index b6f419c..c8a7847 100644 --- a/android/src/main/java/com/appsflyer/appsflyersdk/AppsFlyerConstants.java +++ b/android/src/main/java/com/appsflyer/appsflyersdk/AppsFlyerConstants.java @@ -1,30 +1,32 @@ package com.appsflyer.appsflyersdk; -public class AppsFlyerConstants { - final static String PLUGIN_VERSION = "6.15.1"; - final static String AF_APP_INVITE_ONE_LINK = "appInviteOneLink"; - final static String AF_HOST_PREFIX = "hostPrefix"; - final static String AF_HOST_NAME = "hostName"; - final static String AF_IS_DEBUG = "isDebug"; - final static String AF_MANUAL_START = "manualStart"; - final static String AF_DEV_KEY = "afDevKey"; - final static String AF_EVENT_NAME = "eventName"; - final static String AF_EVENT_VALUES = "eventValues"; - final static String AF_ON_INSTALL_CONVERSION_DATA_LOADED = "onInstallConversionDataLoaded"; - final static String AF_ON_APP_OPEN_ATTRIBUTION = "onAppOpenAttribution"; - final static String AF_SUCCESS = "success"; - final static String AF_FAILURE = "failure"; - final static String AF_GCD = "GCD"; - final static String AF_UDL = "UDL"; - final static String AF_VALIDATE_PURCHASE = "validatePurchase"; - final static String AF_GCD_CALLBACK = "onInstallConversionData"; - final static String AF_OAOA_CALLBACK = "onAppOpenAttribution"; - final static String AF_UDL_CALLBACK = "onDeepLinking"; - final static String DISABLE_ADVERTISING_IDENTIFIER = "disableAdvertisingIdentifier"; +public final class AppsFlyerConstants { + final static String PLUGIN_VERSION = "6.15.1"; + final static String AF_APP_INVITE_ONE_LINK = "appInviteOneLink"; + final static String AF_HOST_PREFIX = "hostPrefix"; + final static String AF_HOST_NAME = "hostName"; + final static String AF_IS_DEBUG = "isDebug"; + final static String AF_MANUAL_START = "manualStart"; + final static String AF_DEV_KEY = "afDevKey"; + final static String AF_EVENT_NAME = "eventName"; + final static String AF_EVENT_VALUES = "eventValues"; + final static String AF_ON_INSTALL_CONVERSION_DATA_LOADED = "onInstallConversionDataLoaded"; + final static String AF_ON_APP_OPEN_ATTRIBUTION = "onAppOpenAttribution"; + final static String AF_SUCCESS = "success"; + final static String AF_FAILURE = "failure"; + final static String AF_GCD = "GCD"; + final static String AF_UDL = "UDL"; + final static String AF_VALIDATE_PURCHASE = "validatePurchase"; + final static String AF_GCD_CALLBACK = "onInstallConversionData"; + final static String AF_OAOA_CALLBACK = "onAppOpenAttribution"; + final static String AF_UDL_CALLBACK = "onDeepLinking"; + final static String DISABLE_ADVERTISING_IDENTIFIER = "disableAdvertisingIdentifier"; - final static String AF_EVENTS_CHANNEL = "af-events"; - final static String AF_METHOD_CHANNEL = "af-api"; - final static String AF_CALLBACK_CHANNEL = "callbacks"; + final static String AF_EVENTS_CHANNEL = "af-events"; + final static String AF_METHOD_CHANNEL = "af-api"; + final static String AF_CALLBACK_CHANNEL = "callbacks"; - final static String AF_BROADCAST_ACTION_NAME = "com.appsflyer.appsflyersdk"; + final static String AF_BROADCAST_ACTION_NAME = "com.appsflyer.appsflyersdk"; + + final static String AF_PLUGIN_TAG = "AppsFlyer_FlutterPlugin"; } diff --git a/android/src/main/java/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.java b/android/src/main/java/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.java index bc66b4a..be1d7e6 100644 --- a/android/src/main/java/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.java +++ b/android/src/main/java/com/appsflyer/appsflyersdk/AppsflyerSdkPlugin.java @@ -49,6 +49,7 @@ import static com.appsflyer.appsflyersdk.AppsFlyerConstants.AF_EVENTS_CHANNEL; import static com.appsflyer.appsflyersdk.AppsFlyerConstants.AF_FAILURE; +import static com.appsflyer.appsflyersdk.AppsFlyerConstants.AF_PLUGIN_TAG; import static com.appsflyer.appsflyersdk.AppsFlyerConstants.AF_SUCCESS; /** @@ -203,7 +204,7 @@ private void startListening(Object arguments, Result rawResult) { @Override public void onMethodCall(MethodCall call, Result result) { if (activity == null) { - Log.d("AppsFlyer", "Activity isn't attached to the flutter engine"); + Log.d(AF_PLUGIN_TAG, LogMessages.ACTIVITY_NOT_ATTACHED_TO_ENGINE); return; } final String method = call.method; @@ -362,11 +363,11 @@ private void performOnDeepLinking(MethodCall call, Result result) { AppsFlyerLib.getInstance().performOnDeepLinking(intent, mApplication); result.success(null); } else { - Log.d("AppsFlyer", "performOnDeepLinking: intent is null!"); + Log.d(AF_PLUGIN_TAG, "performOnDeepLinking: intent is null!"); result.error("NO_INTENT", "The intent is null", null); } } else { - Log.d("AppsFlyer", "performOnDeepLinking: activity is null!"); + Log.d(AF_PLUGIN_TAG, "performOnDeepLinking: activity is null!"); result.error("NO_ACTIVITY", "The current activity is null", null); } } @@ -379,34 +380,37 @@ private void anonymizeUser(MethodCall call, Result result) { private void startSDKwithHandler(MethodCall call, final Result result) { try { - final AppsFlyerLib instance = AppsFlyerLib.getInstance(); - instance.start(activity, null, new AppsFlyerRequestListener() { + final AppsFlyerLib appsFlyerLib = AppsFlyerLib.getInstance(); + + appsFlyerLib.start(activity, null, new AppsFlyerRequestListener() { @Override public void onSuccess() { - uiThreadHandler.post(new Runnable() { - @Override - public void run() { - mMethodChannel.invokeMethod("onSuccess", null); - } - }); + if (mMethodChannel != null) { + uiThreadHandler.post(() -> mMethodChannel.invokeMethod("onSuccess", null)); + } else { + Log.e(AF_PLUGIN_TAG, LogMessages.METHOD_CHANNEL_IS_NULL); + result.error("NULL_OBJECT", LogMessages.METHOD_CHANNEL_IS_NULL, null); + } } @Override public void onError(final int errorCode, final String errorMessage) { - uiThreadHandler.post(new Runnable() { - @Override - public void run() { + if (mMethodChannel != null) { + uiThreadHandler.post(() -> { HashMap errorDetails = new HashMap<>(); errorDetails.put("errorCode", errorCode); errorDetails.put("errorMessage", errorMessage); mMethodChannel.invokeMethod("onError", errorDetails); - } - }); + }); + } else { + Log.e(AF_PLUGIN_TAG, LogMessages.METHOD_CHANNEL_IS_NULL); + result.error("NULL_OBJECT", LogMessages.METHOD_CHANNEL_IS_NULL, null); + } } }); result.success(null); - } catch (Exception e) { - result.error("UNEXPECTED_ERROR", e.getMessage(), null); + } catch (Throwable t) { + result.error("UNEXPECTED_ERROR", t.getMessage(), null); } } @@ -532,14 +536,14 @@ private void sendPushNotificationData(MethodCall call, Result result) { Bundle bundle; if (pushPayload == null) { - Log.d("AppsFlyer", "Push payload is null"); + Log.d(AF_PLUGIN_TAG, "Push payload is null"); return; } try { bundle = this.jsonToBundle(new JSONObject(pushPayload)); } catch (JSONException e) { - Log.d("AppsFlyer", "Can't parse pushPayload to bundle"); + Log.d(AF_PLUGIN_TAG, "Can't parse pushPayload to bundle"); return; } @@ -557,7 +561,7 @@ private void sendPushNotificationData(MethodCall call, Result result) { } if (errorMsg != null) { - Log.d("AppsFlyer", errorMsg); + Log.d(AF_PLUGIN_TAG, errorMsg); return; } @@ -963,8 +967,8 @@ private void logAdRevenue(MethodCall call, Result result) { try { String monetizationNetwork = requireNonNullArgument(call, "monetizationNetwork"); String currencyIso4217Code = requireNonNullArgument(call, "currencyIso4217Code"); - double revenue = requireNonNullArgument(call,"revenue"); - String mediationNetworkString = requireNonNullArgument(call,"mediationNetwork"); + double revenue = requireNonNullArgument(call, "revenue"); + String mediationNetworkString = requireNonNullArgument(call, "mediationNetwork"); MediationNetwork mediationNetwork = MediationNetwork.valueOf(mediationNetworkString.toUpperCase()); @@ -984,10 +988,9 @@ private void logAdRevenue(MethodCall call, Result result) { } catch (IllegalArgumentException e) { // The IllegalArgumentException could come from either requireNonNullArgument or valueOf methods. result.error("INVALID_ARGUMENT_PROVIDED", e.getMessage(), null); - } - catch (Throwable t) { + } catch (Throwable t) { result.error("UNEXPECTED_ERROR", "[logAdRevenue]: An unexpected error occurred: " + t.getMessage(), null); - Log.e("AppsFlyer", "Unexpected exception occurred: [logAdRevenue]", t); + Log.e(AF_PLUGIN_TAG, "Unexpected exception occurred: [logAdRevenue]", t); } } @@ -1004,7 +1007,7 @@ private void logAdRevenue(MethodCall call, Result result) { private T requireNonNullArgument(MethodCall call, String argumentName) throws IllegalArgumentException { T argument = call.argument(argumentName); if (argument == null) { - Log.e("AppsFlyer", "Exception occurred when trying to: " + call.method + "->" + argumentName + " must not be null"); + Log.e(AF_PLUGIN_TAG, "Exception occurred when trying to: " + call.method + "->" + argumentName + " must not be null"); throw new IllegalArgumentException("[" + call.method + "]: " + argumentName + " must not be null"); } return argument; diff --git a/android/src/main/java/com/appsflyer/appsflyersdk/LogMessages.java b/android/src/main/java/com/appsflyer/appsflyersdk/LogMessages.java new file mode 100644 index 0000000..cc363ef --- /dev/null +++ b/android/src/main/java/com/appsflyer/appsflyersdk/LogMessages.java @@ -0,0 +1,12 @@ +package com.appsflyer.appsflyersdk; + +public final class LogMessages { + + // Prevent the instantiation of this utilities class. + private LogMessages() { + throw new IllegalStateException("LogMessages class should not be instantiated"); + } + + public static final String METHOD_CHANNEL_IS_NULL = "mMethodChannel is null, cannot invoke the callback"; + public static final String ACTIVITY_NOT_ATTACHED_TO_ENGINE = "Activity isn't attached to the flutter engine"; +}