Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid storing reference to a particular Appboy instance #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.segment.analytics.android.integrations.appboy;

import com.appboy.IAppboy;

public interface AppboyInstanceProvider {
IAppboy getInstance();
}

Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public Integration<?> create(ValueMap settings, Analytics analytics) {

final Context applicationContext = analytics.getApplication().getApplicationContext();
Appboy.configure(applicationContext, builder.build());
Appboy appboy = Appboy.getInstance(applicationContext);
Appboy.getInstance(applicationContext);
logger.verbose("Configured Braze+Segment integration and initialized Appboy.");
return new AppboyIntegration(appboy, apiKey, logger, inAppMessageRegistrationEnabled);
return new AppboyIntegration(applicationContext, apiKey, logger, inAppMessageRegistrationEnabled);
}

@Override
Expand All @@ -94,16 +94,16 @@ public String key() {
}
};

private final IAppboy mAppboy;
private final AppboyInstanceProvider mAppboy;
private final String mToken;
private final Logger mLogger;
private final boolean mAutomaticInAppMessageRegistrationEnabled;

public AppboyIntegration(Appboy appboy,
public AppboyIntegration(Context context,
String token,
Logger logger,
boolean automaticInAppMessageRegistrationEnabled) {
mAppboy = appboy;
mAppboy = new FromContextAppboyInstanceProvider(context);
mToken = token;
mLogger = logger;
mAutomaticInAppMessageRegistrationEnabled = automaticInAppMessageRegistrationEnabled;
Expand All @@ -114,7 +114,7 @@ public AppboyIntegration(IAppboy appboy,
String token,
Logger logger,
boolean automaticInAppMessageRegistrationEnabled) {
mAppboy = appboy;
mAppboy = new DirectAppboyInstanceProvider(appboy);
mToken = token;
mLogger = logger;
mAutomaticInAppMessageRegistrationEnabled = automaticInAppMessageRegistrationEnabled;
Expand All @@ -126,7 +126,7 @@ public String getToken() {

@Override
public Appboy getUnderlyingInstance() {
return (Appboy) mAppboy;
return (Appboy) mAppboy.getInstance();
}

@Override
Expand All @@ -135,12 +135,12 @@ public void identify(IdentifyPayload identify) {

String userId = identify.userId();
if (!StringUtils.isNullOrBlank(userId)) {
mAppboy.changeUser(identify.userId());
mAppboy.getInstance().changeUser(identify.userId());
}

Traits traits = identify.traits();

BrazeUser currentUser = mAppboy.getCurrentUser();
BrazeUser currentUser = mAppboy.getInstance().getCurrentUser();
if (currentUser == null) {
mLogger.info("Appboy.getCurrentUser() was null, aborting identify");
return;
Expand Down Expand Up @@ -243,7 +243,7 @@ public void identify(IdentifyPayload identify) {
public void flush() {
super.flush();
mLogger.verbose("Calling appboy.requestImmediateDataFlush().");
mAppboy.requestImmediateDataFlush();
mAppboy.getInstance().requestImmediateDataFlush();
}

@Override
Expand All @@ -257,7 +257,7 @@ public void track(TrackPayload track) {
try {
if (event.equals("Install Attributed")) {
ValueMap campaignProps = (ValueMap) properties.get("campaign");
BrazeUser currentUser = mAppboy.getCurrentUser();
BrazeUser currentUser = mAppboy.getInstance().getCurrentUser();
if (campaignProps != null && currentUser != null) {
currentUser.setAttributionData(new AttributionData(
campaignProps.getString("source"),
Expand Down Expand Up @@ -289,25 +289,25 @@ public void track(TrackPayload track) {
} else {
if (propertiesJson == null || propertiesJson.length() == 0) {
mLogger.verbose("Calling appboy.logCustomEvent for event %s", event);
mAppboy.logCustomEvent(event);
mAppboy.getInstance().logCustomEvent(event);
} else {
mLogger.verbose("Calling appboy.logCustomEvent for event %s with properties %s.",
event, propertiesJson.toString());
mAppboy.logCustomEvent(event, new BrazeProperties(propertiesJson));
mAppboy.getInstance().logCustomEvent(event, new BrazeProperties(propertiesJson));
}
}
}

@Override
public void onActivityStarted(Activity activity) {
super.onActivityStarted(activity);
mAppboy.openSession(activity);
mAppboy.getInstance().openSession(activity);
}

@Override
public void onActivityStopped(Activity activity) {
super.onActivityStopped(activity);
mAppboy.closeSession(activity);
mAppboy.getInstance().closeSession(activity);
}

@Override
Expand All @@ -334,11 +334,11 @@ void logPurchaseForSingleItem(String productId,
if (propertiesJson == null || propertiesJson.length() == 0) {
mLogger.verbose("Calling appboy.logPurchase for purchase %s for %.02f %s with no"
+ " properties.", productId, price, currencyCode);
mAppboy.logPurchase(productId, currencyCode, price);
mAppboy.getInstance().logPurchase(productId, currencyCode, price);
} else {
mLogger.verbose("Calling appboy.logPurchase for purchase %s for %.02f %s with properties"
+ " %s.", productId, price, currencyCode, propertiesJson.toString());
mAppboy.logPurchase(productId, currencyCode, price, new BrazeProperties(propertiesJson));
mAppboy.getInstance().logPurchase(productId, currencyCode, price, new BrazeProperties(propertiesJson));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.segment.analytics.android.integrations.appboy;

import com.appboy.IAppboy;

public class DirectAppboyInstanceProvider implements AppboyInstanceProvider {

private IAppboy appboy;

public DirectAppboyInstanceProvider(IAppboy appboy) {
this.appboy = appboy;
}

@Override
public IAppboy getInstance() {
return appboy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.segment.analytics.android.integrations.appboy;

import android.content.Context;
import com.appboy.Appboy;

public class FromContextAppboyInstanceProvider implements AppboyInstanceProvider {

private final Context mContext;

public FromContextAppboyInstanceProvider(Context context) {
mContext = context;
}

@Override
public Appboy getInstance() {
return Appboy.getInstance(mContext);
}
}