Skip to content

Commit c2b7bd7

Browse files
Merge pull request #14 from BranchMetrics/SDK-874/broken-auto-init
Sdk 874/broken auto init
2 parents 5ec4eaa + 217fee9 commit c2b7bd7

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99
gradle/
1010
gradlew
1111
gradlew.bat
12+
13+
AdobeBranchExample/.idea
14+
AdobeBranchExample/local.properties

AdobeBranchExample/src/main/AndroidManifest.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<activity
1717
android:name=".ProductActivity"
1818
android:label="@string/app_name"
19+
android:launchMode="singleTask"
1920
android:theme="@style/AppTheme.NoActionBar">
2021
<intent-filter>
2122
<action android:name="android.intent.action.MAIN" />
@@ -25,9 +26,7 @@
2526

2627
<!-- Branch URI Scheme -->
2728
<intent-filter>
28-
<data
29-
android:host="open"
30-
android:scheme="adobe-branch" />
29+
<data android:scheme="adobe-branch" />
3130

3231
<action android:name="android.intent.action.VIEW" />
3332

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

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

33
import android.content.Intent;
44
import android.os.Bundle;
5-
import android.os.Handler;
5+
import android.text.TextUtils;
66
import android.view.View;
77
import android.widget.AdapterView;
88
import android.widget.ListAdapter;
@@ -57,13 +57,14 @@ public void onClick(View view) {
5757
@Override
5858
protected void onStart() {
5959
super.onStart();
60-
initBranchSession();
60+
AdobeBranch.initSession(branchInitSessionCallback, getIntent().getData(), this);
6161
}
6262

6363
@Override
6464
public void onNewIntent(Intent intent) {
6565
super.onNewIntent(intent);
6666
this.setIntent(intent);
67+
AdobeBranch.reInitSession(this, branchInitSessionCallback);
6768
}
6869

6970
private void initList() {
@@ -88,16 +89,16 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
8889
}
8990
}
9091

91-
private void initBranchSession() {
92-
AdobeBranch.initSession(new Branch.BranchReferralInitListener() {
92+
private Branch.BranchReferralInitListener branchInitSessionCallback = new Branch.BranchReferralInitListener() {
9393
@Override
9494
public void onInitFinished(JSONObject referringParams, BranchError error) {
95+
PrefHelper.Debug("initBranchSession, referringParams = " + referringParams + ", error = " + error);
9596
if (referringParams == null) return;
9697
try {
9798
// You would think that there was an easier way to figure this out than looking at LinkProperties code
9899
if (referringParams.has("+clicked_branch_link") && referringParams.getBoolean("+clicked_branch_link")) {
99100
String idString = referringParams.optString(SwagActivity.SWAG_ID);
100-
if (idString != null) {
101+
if (!TextUtils.isEmpty(idString)) {
101102
int swagId = Integer.parseInt(idString);
102103

103104
// Launch the Swag Activity
@@ -111,8 +112,8 @@ public void onInitFinished(JSONObject referringParams, BranchError error) {
111112
// internal error; id is not a number.
112113
}
113114
}
114-
}, getIntent().getData(), this);
115-
}
115+
};
116+
116117

117118
private void shareProduct() {
118119
BranchUniversalObject buo = new BranchUniversalObject();

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import io.branch.referral.Branch;
1818
import io.branch.referral.BranchUtil;
19+
import io.branch.referral.Defines;
1920

2021
/**
2122
* AdobeBranch Extension.
@@ -43,8 +44,6 @@ public class AdobeBranch {
4344
* @return An initialized {@link Branch} object
4445
*/
4546
public static Branch getAutoInstance(@NonNull Context context) {
46-
BranchUtil.setPluginType(BranchUtil.PluginType.AdobeLaunch);
47-
BranchUtil.setPluginVersion(BuildConfig.VERSION_NAME);
4847
return Branch.getAutoInstance(context);
4948
}
5049

@@ -80,11 +79,13 @@ public static boolean initSession(Branch.BranchReferralInitListener callback, Ur
8079
public static boolean initSession(final Branch.BranchReferralInitListener callback, final Uri data, final Activity activity, int delay) {
8180
final Branch branch = Branch.getInstance();
8281
if (branch != null) {
82+
BranchUtil.setPluginType(BranchUtil.PluginType.AdobeLaunch);// prevents early auto-initialization
8383
if (delay == 0) {
8484
branch.initSession(callback, data, activity);
8585
} else {
8686
new Handler().postDelayed(new Runnable() {
8787
@Override public void run() {
88+
BranchUtil.setPluginType(null);// re-enables auto-initialization for when app is launched from recent apps list
8889
branch.initSession(callback, data, activity);
8990
}
9091
}, delay);
@@ -94,6 +95,21 @@ public static boolean initSession(final Branch.BranchReferralInitListener callba
9495
return false;
9596
}
9697

98+
/**
99+
* ReInitialize session. Called from onNewIntent, will only reInitialize if the intent contains a boolean extra "branch_force_new_session"=true
100+
*/
101+
public static boolean reInitSession(@NonNull Activity activity, Branch.BranchReferralInitListener callback) {
102+
Branch branch = Branch.getInstance();
103+
if (branch != null && branch.reInitSession(activity, callback)) {
104+
// this ensures that if user intra-app links from SomeActivity to LauncherActivity,
105+
// and both, initSession and reInitSession, are used in LauncherActivity, then only the
106+
// reInitSession callback returns referring params while the other one returns error, "SDK already initialized".
107+
activity.getIntent().removeExtra(Defines.Jsonkey.ForceNewBranchSession.getKey());
108+
return true;
109+
}
110+
return false;
111+
}
112+
97113
/**
98114
* Register a whitelist of Event Types and Event Sources to send to Branch.
99115
* @param additionalEvents Additional events to listen for.

0 commit comments

Comments
 (0)