From 41db90d3338c1ff9269078db6efedbe9b6595a44 Mon Sep 17 00:00:00 2001 From: jameskeith Date: Thu, 21 Mar 2019 16:21:02 +0000 Subject: [PATCH] 1.0.4 --- CHANGELOG.md | 1 + TealiumSampleApp/android/app/build.gradle | 2 +- TealiumSampleApp/android/build.gradle | 8 +- .../com/tealiumreactnative/TealiumModule.java | 113 +++++++++++++----- npm-package/package.json | 2 +- 5 files changed, 89 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddfd426c..eeae3a95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Change Log +- 1.0.4 - 1.0.3 - Bug fix - Handling of possible null Activity from ReactApplicationContext diff --git a/TealiumSampleApp/android/app/build.gradle b/TealiumSampleApp/android/app/build.gradle index 6c07660b..7530bd22 100644 --- a/TealiumSampleApp/android/app/build.gradle +++ b/TealiumSampleApp/android/app/build.gradle @@ -137,7 +137,7 @@ android { } dependencies { - compile project(':tealium-react-native') + implementation project(':tealium-react-native') implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}" implementation "com.facebook.react:react-native:+" // From node_modules diff --git a/TealiumSampleApp/android/build.gradle b/TealiumSampleApp/android/build.gradle index 105950f8..12aac236 100644 --- a/TealiumSampleApp/android/build.gradle +++ b/TealiumSampleApp/android/build.gradle @@ -2,11 +2,11 @@ buildscript { ext { - buildToolsVersion = "27.0.3" + buildToolsVersion = "28.0.1" minSdkVersion = 16 - compileSdkVersion = 27 - targetSdkVersion = 26 - supportLibVersion = "27.1.1" + compileSdkVersion = 28 + targetSdkVersion = 28 + supportLibVersion = "28.0.0" } repositories { google() diff --git a/npm-package/android/src/main/java/com/tealiumreactnative/TealiumModule.java b/npm-package/android/src/main/java/com/tealiumreactnative/TealiumModule.java index 02561cb0..9751b433 100644 --- a/npm-package/android/src/main/java/com/tealiumreactnative/TealiumModule.java +++ b/npm-package/android/src/main/java/com/tealiumreactnative/TealiumModule.java @@ -1,5 +1,6 @@ package com.tealiumreactnative; +import android.app.Application; import android.content.SharedPreferences; import android.os.Handler; import android.util.Log; @@ -35,15 +36,12 @@ public class TealiumModule extends ReactContextBaseJavaModule { - private static ReactApplicationContext mReactApplicationContext; - private static String mTealiumInstanceName; private static boolean mIsLifecycleAutotracking = false; private boolean mDidTrackInitialLaunch = false; public TealiumModule(ReactApplicationContext reactContext) { super(reactContext); - mReactApplicationContext = reactContext; } @@ -65,7 +63,7 @@ public void initialize(String account, throw new IllegalArgumentException("Account, profile, and environment parameters must be provided and non-null"); } - final Tealium.Config config = Tealium.Config.create(mReactApplicationContext.getCurrentActivity().getApplication(), account, profile, environment); + final Tealium.Config config = Tealium.Config.create(getApplication(), account, profile, environment); if (androidDatasource != null) { config.setDatasourceId(androidDatasource); } @@ -75,7 +73,7 @@ public void initialize(String account, final boolean isAutoTracking = false; LifeCycle.setupInstance(mTealiumInstanceName, config, isAutoTracking); mIsLifecycleAutotracking = isLifecycleEnabled; - mReactApplicationContext.addLifecycleEventListener(createLifecycleEventListener(mTealiumInstanceName)); + getReactApplicationContext().addLifecycleEventListener(createLifecycleEventListener(mTealiumInstanceName)); } Tealium.createInstance(instance, config); @@ -94,7 +92,7 @@ public void initializeWithConsentManager(String account, throw new IllegalArgumentException("Account, profile, and environment parameters must be provided and non-null"); } - final Tealium.Config config = Tealium.Config.create(mReactApplicationContext.getCurrentActivity().getApplication(), account, profile, environment); + final Tealium.Config config = Tealium.Config.create(getApplication(), account, profile, environment); if (androidDatasource != null) { config.setDatasourceId(androidDatasource); } @@ -105,7 +103,7 @@ public void initializeWithConsentManager(String account, final boolean isAutoTracking = false; LifeCycle.setupInstance(mTealiumInstanceName, config, isAutoTracking); mIsLifecycleAutotracking = isLifecycleEnabled; - mReactApplicationContext.addLifecycleEventListener(createLifecycleEventListener(mTealiumInstanceName)); + getReactApplicationContext().addLifecycleEventListener(createLifecycleEventListener(mTealiumInstanceName)); } Tealium.createInstance(instance, config); @@ -128,7 +126,7 @@ public void initializeCustom(String account, throw new IllegalArgumentException("Account, profile, and environment parameters must be provided and non-null"); } - final Tealium.Config config = Tealium.Config.create(mReactApplicationContext.getCurrentActivity().getApplication(), account, profile, environment); + final Tealium.Config config = Tealium.Config.create(getApplication(), account, profile, environment); if (androidDatasource != null) { config.setDatasourceId(androidDatasource); } @@ -149,7 +147,7 @@ public void initializeCustom(String account, final boolean isAutoTracking = false; LifeCycle.setupInstance(instance, config, isAutoTracking); mIsLifecycleAutotracking = isLifecycleEnabled; - mReactApplicationContext.addLifecycleEventListener(createLifecycleEventListener(instance)); + getReactApplicationContext().addLifecycleEventListener(createLifecycleEventListener(instance)); } Tealium.createInstance(instance, config); } @@ -402,7 +400,12 @@ public void getVisitorID(Callback callback) { @ReactMethod public void getVisitorIDForInstance(String instanceName, Callback callback) { - callback.invoke(Tealium.getInstance(instanceName).getDataSources().getVisitorId()); + final Tealium instance = Tealium.getInstance(instanceName); + if (instance == null) { + Log.e(BuildConfig.TAG, "GetVisitorID attempted, but Tealium not enabled for instance name: " + instanceName); + return; + } + callback.invoke(instance.getDataSources().getVisitorId()); } @ReactMethod @@ -412,9 +415,13 @@ public void getUserConsentStatus(Callback callback) { @ReactMethod public void getUserConsentStatusForInstance(String instanceName, Callback callback) { - Tealium tealium = Tealium.getInstance(instanceName); - if (tealium.getConsentManager() != null) { - tealium.getConsentManager().getUserConsentStatus(); + final Tealium instance = Tealium.getInstance(instanceName); + if (instance == null) { + Log.e(BuildConfig.TAG, "GetUserConsentStatus attempted, but Tealium not enabled for instance name: " + instanceName); + return; + } + if (instance.getConsentManager() != null) { + instance.getConsentManager().getUserConsentStatus(); } } @@ -426,9 +433,13 @@ public void setUserConsentStatus(int userConsentStatus) { @ReactMethod public void setUserConsentStatusForInstance(String instanceName, int userConsentStatus) { String consentStatus = mapUserConsentStatus(userConsentStatus); - Tealium tealium = Tealium.getInstance(instanceName); - if (tealium.getConsentManager() != null) { - tealium.getConsentManager().setUserConsentStatus(consentStatus); + final Tealium instance = Tealium.getInstance(instanceName); + if (instance == null) { + Log.e(BuildConfig.TAG, "SetUserConsentStatus attempted, but Tealium not enabled for instance name: " + instanceName); + return; + } + if (instance.getConsentManager() != null) { + instance.getConsentManager().setUserConsentStatus(consentStatus); } } @@ -439,9 +450,13 @@ public void getUserConsentCategories(Callback callback) { @ReactMethod public void getUserConsentCategoriesForInstance(String instanceName, Callback callback) { - Tealium tealium = Tealium.getInstance(instanceName); - if (tealium.getConsentManager() != null) { - callback.invoke(tealium.getConsentManager().getUserConsentCategories()); + final Tealium instance = Tealium.getInstance(instanceName); + if (instance == null) { + Log.e(BuildConfig.TAG, "GetUserConsentCategories attempted, but Tealium not enabled for instance name: " + instanceName); + return; + } + if (instance.getConsentManager() != null) { + callback.invoke(instance.getConsentManager().getUserConsentCategories()); } } @@ -452,8 +467,12 @@ public void setUserConsentCategories(ReadableArray categories) { @ReactMethod public void setUserConsentCategoriesForInstance(String instanceName, ReadableArray categories) { - Tealium tealium = Tealium.getInstance(instanceName); - if (tealium.getConsentManager() != null) { + final Tealium instance = Tealium.getInstance(instanceName); + if (instance == null) { + Log.e(BuildConfig.TAG, "SetUserConsentCategories attempted, but Tealium not enabled for instance name: " + instanceName); + return; + } + if (instance.getConsentManager() != null) { String[] userConsentCategories = new String[categories.toArrayList().size()]; for (int i = 0; i < categories.size(); i++) { ReadableType type = categories.getType(i); @@ -466,7 +485,7 @@ public void setUserConsentCategoriesForInstance(String instanceName, ReadableArr break; } } - tealium.getConsentManager().setUserConsentCategories(userConsentCategories); + instance.getConsentManager().setUserConsentCategories(userConsentCategories); } } @@ -477,9 +496,13 @@ public void resetUserConsentPreferences() { @ReactMethod public void resetUserConsentPreferencesForInstance(String instanceName) { - Tealium tealium = Tealium.getInstance(instanceName); - if (tealium.getConsentManager() != null) { - tealium.getConsentManager().getUserConsentCategories(); + final Tealium instance = Tealium.getInstance(instanceName); + if (instance == null) { + Log.e(BuildConfig.TAG, "ResetUserConsentPreferences attempted, but Tealium not enabled for instance name: " + instanceName); + return; + } + if (instance.getConsentManager() != null) { + instance.getConsentManager().resetUserConsentPreferences(); } } @@ -490,9 +513,13 @@ public void setConsentLoggingEnabled(boolean isLogging) { @ReactMethod public void setConsentLoggingEnabledForInstance(String instanceName, boolean isLogging) { - Tealium tealium = Tealium.getInstance(instanceName); - if (tealium.getConsentManager() != null) { - tealium.getConsentManager().setConsentLoggingEnabled(isLogging); + final Tealium instance = Tealium.getInstance(instanceName); + if (instance == null) { + Log.e(BuildConfig.TAG, "SetConsentLoggingEnabled attempted, but Tealium not enabled for instance name: " + instanceName); + return; + } + if (instance.getConsentManager() != null) { + instance.getConsentManager().setConsentLoggingEnabled(isLogging); } } @@ -503,9 +530,13 @@ public void isConsentLoggingEnabled(Callback callback) { @ReactMethod public void isConsentLoggingEnabledForInstance(String instanceName, Callback callback) { - Tealium tealium = Tealium.getInstance(instanceName); - if (tealium.getConsentManager() != null) { - callback.invoke(tealium.getConsentManager().isConsentLogging()); + final Tealium instance = Tealium.getInstance(instanceName); + if (instance == null) { + Log.e(BuildConfig.TAG, "ResetUserConsentPreferences attempted, but Tealium not enabled for instance name: " + instanceName); + return; + } + if (instance.getConsentManager() != null) { + callback.invoke(instance.getConsentManager().isConsentLogging()); } } @@ -547,4 +578,24 @@ private String mapUserConsentStatus(int userConsentStatus) { } } + private Application getApplication() { + Application app = null; + //ReactApplicationContext only holds a weak reference to the Current Activity so we need to + //handle the case where it is null properly to avoid an unhandled exception. + try { + if (getReactApplicationContext().hasCurrentActivity()) { + app = getReactApplicationContext().getCurrentActivity().getApplication(); + } else if (getCurrentActivity() != null){ + app = getCurrentActivity().getApplication(); + } else { + app = (Application) getReactApplicationContext().getApplicationContext(); + } + } catch (NullPointerException ex) { + Log.d(BuildConfig.TAG, "getApplication: method called on null object. ", ex); + } catch (ClassCastException ex) { + Log.d(BuildConfig.TAG, "getApplication: failed to cast to Application. ", ex); + } + return app; + } + } \ No newline at end of file diff --git a/npm-package/package.json b/npm-package/package.json index 04da8878..b6102155 100644 --- a/npm-package/package.json +++ b/npm-package/package.json @@ -1,6 +1,6 @@ { "name": "tealium-react-native", - "version": "1.0.3", + "version": "1.0.4", "description": "A native module for using Tealium's Android and iOS libraries from a React Native app.", "main": "index.js", "homepage": "https://github.com/Tealium/tealium-react-native",