Skip to content

Commit 5ec4eaa

Browse files
Merge pull request #13 from BranchMetrics/INTENG-9038/cleanup-initialization-flow
INTENG-9038
2 parents b319588 + c3de9ec commit 5ec4eaa

File tree

9 files changed

+103
-98
lines changed

9 files changed

+103
-98
lines changed

AdobeBranchExample/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1+
*.iml
2+
/local.properties
3+
.DS_Store
14
/build
5+
/captures
6+
7+
/.idea/
8+
/.gradle/
9+
gradle/
10+
gradlew
11+
gradlew.bat

AdobeBranchExample/src/main/AndroidManifest.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,6 @@
6060
android:name="io.branch.sdk.BranchKey"
6161
android:value="key_live_nbB0KZ4UGOKaHEWCjQI2ThncEAeRJmhy" />
6262

63-
<!-- Branch install referrer tracking (optional) -->
64-
<receiver
65-
android:name="io.branch.referral.InstallListener"
66-
android:exported="true">
67-
<intent-filter>
68-
<action android:name="com.android.vending.INSTALL_REFERRER" />
69-
</intent-filter>
70-
</receiver>
71-
7263
</application>
7364

7465
</manifest>

AdobeBranchExample/src/main/java/io/branch/adobe/demo/DemoApplication.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.adobe.marketing.mobile.AdobeCallback;
44
import com.adobe.marketing.mobile.Analytics;
5-
import com.adobe.marketing.mobile.ExtensionError;
6-
import com.adobe.marketing.mobile.ExtensionErrorCallback;
75
import com.adobe.marketing.mobile.Identity;
86
import com.adobe.marketing.mobile.InvalidInitException;
97
import com.adobe.marketing.mobile.Lifecycle;
@@ -13,38 +11,24 @@
1311
import com.adobe.marketing.mobile.UserProfile;
1412

1513
import android.app.Application;
16-
import io.branch.adobe.extension.AdobeBranch;
1714
import io.branch.adobe.extension.AdobeBranchExtension;
1815
import io.branch.referral.*;
1916

2017
public class DemoApplication extends Application {
21-
private static final String TAG = "DemoApplication::";
2218
private static final String ADOBE_APP_ID = "d10f76259195/b0503e1a5dce/launch-9948a3b3a89d-development";
2319

2420
@Override
2521
public void onCreate() {
2622
super.onCreate();
2723

2824
// Initialize
29-
initBranch();
3025
initAdobeBranch();
31-
registerAdobeBranchExtension();
32-
// Analytics.setVisitorIdentifier("custom_identifier_123"); // to test custom visitor ID (key: "vid")
33-
}
34-
35-
private void initBranch() {
36-
Branch.enableDebugMode();
37-
38-
// TODO: Revisit. This is how we should encourage customers to initialize Branch using Branch.
39-
// Branch.getAutoInstance(this);
26+
Analytics.setVisitorIdentifier("custom_identifier_1234"); // to test custom visitor ID (key: "vid")
4027
}
4128

4229
private void initAdobeBranch() {
4330
PrefHelper.Debug("initAdobeBranch()");
4431

45-
// TODO: Revisit. We should encourage customers to initialize Branch using Branch.
46-
AdobeBranch.getAutoInstance(this);
47-
4832
MobileCore.setApplication(this);
4933
MobileCore.setLogLevel(LoggingMode.DEBUG);
5034

@@ -54,28 +38,14 @@ private void initAdobeBranch() {
5438
Identity.registerExtension();
5539
Lifecycle.registerExtension();
5640
Signal.registerExtension();
41+
AdobeBranchExtension.registerExtension(this, true);
5742
MobileCore.start(new AdobeCallback () {
58-
@Override
59-
public void call(Object o) {
43+
@Override public void call(Object o) {
6044
MobileCore.configureWithAppID(ADOBE_APP_ID);
6145
}
6246
});
6347
} catch (InvalidInitException e) {
6448
PrefHelper.Debug("InitException: " + e.getLocalizedMessage());
6549
}
6650
}
67-
68-
private void registerAdobeBranchExtension() {
69-
ExtensionErrorCallback<ExtensionError> errorCallback = new ExtensionErrorCallback<ExtensionError>() {
70-
@Override
71-
public void error(final ExtensionError extensionError) {
72-
PrefHelper.Debug(String.format("An error occurred while registering the AdobeBranchExtension %d %s", extensionError.getErrorCode(), extensionError.getErrorName()));
73-
}
74-
};
75-
76-
if (!MobileCore.registerExtension(AdobeBranchExtension.class, errorCallback)) {
77-
PrefHelper.Debug("Failed to register the AdobeBranchExtension extension");
78-
}
79-
}
80-
8151
}

AdobeBranchExtension/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ dependencies {
3434
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
3535

3636
// Branch
37-
api 'io.branch.sdk.android:library:4.2.0'
38-
javadocDeps 'io.branch.sdk.android:library:4.2.0'
37+
api 'io.branch.sdk.android:library:4.3.2'
38+
javadocDeps 'io.branch.sdk.android:library:4.3.2'
3939

4040
// Adobe
4141
androidTestImplementation 'com.adobe.marketing.mobile:userprofile:1.0.1'
42-
implementation 'com.adobe.marketing.mobile:sdk-core:1.4.6'
42+
implementation 'com.adobe.marketing.mobile:sdk-core:1.5.0'
4343
}
4444

4545
//------------- Javadocs ---------------//

AdobeBranchExtension/src/androidTest/java/io/branch/adobe/extension/test/TestApplication.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import android.util.Log;
55

66
import com.adobe.marketing.mobile.AdobeCallback;
7-
import com.adobe.marketing.mobile.ExtensionError;
8-
import com.adobe.marketing.mobile.ExtensionErrorCallback;
97
import com.adobe.marketing.mobile.Identity;
108
import com.adobe.marketing.mobile.InvalidInitException;
119
import com.adobe.marketing.mobile.Lifecycle;
@@ -16,7 +14,6 @@
1614

1715
import io.branch.adobe.extension.AdobeBranch;
1816
import io.branch.adobe.extension.AdobeBranchExtension;
19-
import io.branch.referral.*;
2017

2118
public class TestApplication extends Application {
2219
private static final String TAG = "Branch::TestApplication::";
@@ -27,13 +24,7 @@ public void onCreate() {
2724
super.onCreate();
2825

2926
// Initialize
30-
initBranch();
3127
initAdobeBranch();
32-
registerAdobeBranchExtension();
33-
}
34-
35-
private void initBranch() {
36-
Branch.enableDebugMode();
3728
}
3829

3930
private void initAdobeBranch() {
@@ -45,6 +36,7 @@ private void initAdobeBranch() {
4536
MobileCore.setLogLevel(LoggingMode.VERBOSE);
4637

4738
try {
39+
AdobeBranchExtension.registerExtension(this, true);
4840
UserProfile.registerExtension();
4941
Identity.registerExtension();
5042
Lifecycle.registerExtension();
@@ -59,20 +51,4 @@ public void call(Object o) {
5951
Log.e(TAG, "InitException", e);
6052
}
6153
}
62-
63-
private void registerAdobeBranchExtension() {
64-
MobileCore.setApplication(this);
65-
66-
ExtensionErrorCallback<ExtensionError> errorCallback = new ExtensionErrorCallback<ExtensionError>() {
67-
@Override
68-
public void error(final ExtensionError extensionError) {
69-
Log.e(TAG, String.format("An error occurred while registering the AdobeBranchExtension %d %s", extensionError.getErrorCode(), extensionError.getErrorName()));
70-
}
71-
};
72-
73-
if (!MobileCore.registerExtension(AdobeBranchExtension.class, errorCallback)) {
74-
Log.e(TAG, "Failed to register the AdobeBranchExtension extension");
75-
}
76-
}
77-
7854
}

AdobeBranchExtension/src/main/java/io/branch/adobe/extension/AdobeBranch.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.Activity;
44
import android.content.Context;
55
import android.net.Uri;
6+
import android.os.Handler;
67
import android.util.Pair;
78

89
import androidx.annotation.NonNull;
@@ -33,6 +34,7 @@ public class AdobeBranch {
3334

3435
// Package Private Configuration Event
3536
static final String KEY_APICONFIGURATION = "branch_api_configuration";
37+
static final int INIT_SESSION_DELAY_MILLIS = 750;
3638

3739
/**
3840
* Singleton method to return the pre-initialized, or newly initialize and return, a singleton
@@ -47,19 +49,47 @@ public static Branch getAutoInstance(@NonNull Context context) {
4749
}
4850

4951
/**
50-
* <p>Initializes a session with the Branch API.
52+
* <p>Initializes Branch session after the default 750 millisecond delay needed to collect Adobe IDs.
53+
* To initialize without delay, use initSession(callback, data, activity, delay) passing in 0 as the delay parameter.
54+
*
5155
* @param callback A listener that will be called following successful (or unsuccessful)
5256
* initialization of the session with the Branch API.
5357
* @param data A {@link Uri} variable containing the details of the source link that
5458
* led to this initialization action.
5559
* @param activity The calling {@link Activity} for context.
56-
* @return A {@link Boolean} value that will return <i>false</i> if the supplied <i>data</i>
57-
* parameter cannot be handled successfully - i.e. is not of a valid URI format.
60+
* @return A {@link Boolean} value that will return <i>false</i> if the supplied <i>data</i>
61+
* parameter cannot be handled successfully - i.e. is not of a valid URI format.
5862
*/
5963
public static boolean initSession(Branch.BranchReferralInitListener callback, Uri data, Activity activity) {
60-
Branch branch = Branch.getInstance();
64+
return initSession(callback, data, activity, INIT_SESSION_DELAY_MILLIS);
65+
}
66+
67+
/**
68+
* <p>Initializes Branch session after the chosen delay in milliseconds (needed to collect Adobe IDs).
69+
* To initialize without delay pass 0 as the delay parameter.
70+
*
71+
* @param callback A listener that will be called following successful (or unsuccessful)
72+
* initialization of the session with the Branch API.
73+
* @param data A {@link Uri} variable containing the details of the source link that
74+
* led to this initialization action.
75+
* @param activity The calling {@link Activity} for context.
76+
* @param delay An {@link Integer} to set session initialization delay in millis (delay is needed to collect Adobe IDs).
77+
* @return A {@link Boolean} value that will return <i>false</i> if the supplied <i>data</i>
78+
* parameter cannot be handled successfully - i.e. is not of a valid URI format.
79+
*/
80+
public static boolean initSession(final Branch.BranchReferralInitListener callback, final Uri data, final Activity activity, int delay) {
81+
final Branch branch = Branch.getInstance();
6182
if (branch != null) {
62-
return branch.initSession(callback, data, activity);
83+
if (delay == 0) {
84+
branch.initSession(callback, data, activity);
85+
} else {
86+
new Handler().postDelayed(new Runnable() {
87+
@Override public void run() {
88+
branch.initSession(callback, data, activity);
89+
}
90+
}, delay);
91+
}
92+
return true;
6393
}
6494
return false;
6595
}

AdobeBranchExtension/src/main/java/io/branch/adobe/extension/AdobeBranchExtension.java

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
import android.content.Context;
44
import android.text.TextUtils;
55

6+
import androidx.annotation.NonNull;
7+
68
import com.adobe.marketing.mobile.Event;
79
import com.adobe.marketing.mobile.Extension;
810
import com.adobe.marketing.mobile.ExtensionApi;
911
import com.adobe.marketing.mobile.ExtensionError;
1012
import com.adobe.marketing.mobile.ExtensionErrorCallback;
13+
import com.adobe.marketing.mobile.MobileCore;
1114

1215
import org.json.JSONObject;
1316

1417
import java.lang.reflect.Field;
1518
import java.util.HashMap;
1619
import java.util.List;
20+
import java.util.Locale;
1721
import java.util.Map;
1822

1923
import io.branch.referral.Branch;
@@ -42,41 +46,59 @@ public class AdobeBranchExtension extends Extension implements ExtensionErrorCal
4246

4347
public AdobeBranchExtension(final ExtensionApi extensionApi) {
4448
super(extensionApi);
45-
4649
initExtension();
4750
}
4851

49-
@Override
50-
protected String getName() {
52+
public static void registerExtension(@NonNull Context context) {
53+
registerExtension(context, false);
54+
}
55+
56+
@SuppressWarnings("WeakerAccess")
57+
public static void registerExtension(@NonNull Context context, boolean debugMode) {
58+
if (debugMode) {
59+
Branch.enableDebugMode();
60+
}
61+
AdobeBranch.getAutoInstance(context.getApplicationContext());
62+
boolean successfulRegistration = MobileCore.registerExtension(AdobeBranchExtension.class, new ExtensionErrorCallback<ExtensionError>() {
63+
@Override public void error(final ExtensionError extensionError) {
64+
PrefHelper.Debug(String.format(Locale.getDefault(),
65+
"An error occurred while registering the AdobeBranchExtension %d %s",
66+
extensionError.getErrorCode(), extensionError.getErrorName()));
67+
}
68+
});
69+
if (!successfulRegistration) {
70+
PrefHelper.Debug("Failed to register the AdobeBranchExtension extension");
71+
}
72+
}
73+
74+
@Override protected String getName() {
5175
return "io.branch";
5276
}
5377

54-
@Override
55-
public final String getVersion() {
78+
@Override public final String getVersion() {
5679
return BuildConfig.VERSION_NAME;
5780
}
5881

59-
@Override
60-
public void error(ExtensionError extensionError) {
61-
// something went wrong...
62-
// TODO: What to do about it.
63-
PrefHelper.Debug(TAG + String.format("An error occurred in the AdobeBranchExtension %d %s", extensionError.getErrorCode(), extensionError.getErrorName()));
82+
@Override public void error(ExtensionError extensionError) {
83+
// error callback when registering event listeners
84+
PrefHelper.Debug(TAG + String.format(Locale.getDefault(),
85+
"An error occurred in the AdobeBranchExtension %d %s",
86+
extensionError.getErrorCode(), extensionError.getErrorName()));
6487
}
6588

6689
private void initExtension() {
6790
// Register default Event Listeners
68-
registerExtension(ADOBE_TRACK_EVENT, ADOBE_EVENT_SOURCE);
69-
registerExtension(BRANCH_CONFIGURATION_EVENT, BRANCH_EVENT_SOURCE);
70-
registerExtension(ADOBE_HUB_EVENT_TYPE, ADOBE_SHARED_STATE_EVENT_SOURCE);
71-
}
72-
73-
private void registerExtension(String eventType, String eventSource) {
74-
getApi().registerEventListener(eventType, eventSource, AdobeBranchExtensionListener.class, this);
91+
ExtensionApi api = getApi();
92+
if (api != null) {
93+
api.registerEventListener(ADOBE_TRACK_EVENT, ADOBE_EVENT_SOURCE, AdobeBranchExtensionListener.class, this);
94+
api.registerEventListener(BRANCH_CONFIGURATION_EVENT, BRANCH_EVENT_SOURCE, AdobeBranchExtensionListener.class, this);
95+
api.registerEventListener(ADOBE_HUB_EVENT_TYPE, ADOBE_SHARED_STATE_EVENT_SOURCE, AdobeBranchExtensionListener.class, this);
96+
}
7597
}
7698

77-
// Package Private
7899
void handleAdobeEvent(final Event event) {
79-
PrefHelper.Debug(TAG + String.format("Started processing new event [%s] of type [%s] and source [%s]", event.getName(), event.getType(), event.getSource()));
100+
PrefHelper.Debug(TAG + String.format("Started processing new event [%s] of type [%s] and source [%s]",
101+
event.getName(), event.getType(), event.getSource()));
80102

81103
if (Branch.getInstance() == null) {
82104
// Branch is not initialized.
@@ -128,15 +150,16 @@ private void handleBranchConfigurationEvent(final Event event) {
128150
PrefHelper.Debug("Configuring AdobeBranch");
129151

130152
Object object = eventData.get(AdobeBranch.KEY_APICONFIGURATION);
153+
ExtensionApi api = getApi();
131154

132155
// We expect this to be a List of Strings.
133-
if (object instanceof List<?>) {
156+
if (object instanceof List<?> && api != null) {
134157
try {
135158
apiWhitelist = (List<AdobeBranch.EventTypeSource>)object;
136159

137160
// For each pair in the whitelist, register the extension
138161
for (AdobeBranch.EventTypeSource pair : apiWhitelist) {
139-
registerExtension(pair.getType(), pair.getSource());
162+
api.registerEventListener(pair.getType(), pair.getSource(), AdobeBranchExtensionListener.class, this);
140163
}
141164

142165
} catch (Exception e) {

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Adobe Branch SDK Extension change log
2+
- v1.2.0
3+
* _*Master Release*_ - November 19, 2019
4+
* Minor: delay session initialization to allow enough time to collect Adobe IDs
5+
* Clean up extension registration flow
6+
27
- v1.1.3
38
* _*Master Release*_ - November 19, 2019
49
* Patch: automatically pick up all Adobe IDs and pass it to Branch

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ org.gradle.jvmargs=-Xmx1536m
1111
# This option should only be used with decoupled projects. More details, visit
1212
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1313
# org.gradle.parallel=true
14-
VERSION_NAME=1.1.3
15-
VERSION_CODE=010103
14+
VERSION_NAME=1.2.0
15+
VERSION_CODE=010200
1616
GROUP=io.branch.sdk.android
1717

1818
POM_NAME=Branch Adobe Android SDK

0 commit comments

Comments
 (0)