Skip to content

Commit

Permalink
Merge pull request #20 from Tealium/rn-context-fix2
Browse files Browse the repository at this point in the history
1.0.4
  • Loading branch information
jonathanswong authored Mar 21, 2019
2 parents 7ba588c + 41db90d commit e310956
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Change Log

- 1.0.4
- 1.0.3
- Bug fix
- Handling of possible null Activity from ReactApplicationContext
Expand Down
2 changes: 1 addition & 1 deletion TealiumSampleApp/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions TealiumSampleApp/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tealiumreactnative;

import android.app.Application;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;
Expand Down Expand Up @@ -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;

}

Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
}

Expand All @@ -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);
}
}

Expand All @@ -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());
}
}

Expand All @@ -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);
Expand All @@ -466,7 +485,7 @@ public void setUserConsentCategoriesForInstance(String instanceName, ReadableArr
break;
}
}
tealium.getConsentManager().setUserConsentCategories(userConsentCategories);
instance.getConsentManager().setUserConsentCategories(userConsentCategories);
}
}

Expand All @@ -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();
}
}

Expand All @@ -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);
}
}

Expand All @@ -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());
}
}

Expand Down Expand Up @@ -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;
}

}
2 changes: 1 addition & 1 deletion npm-package/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit e310956

Please sign in to comment.