Skip to content

Commit a821265

Browse files
Merge pull request #737 from BranchMetrics/Staging
Release v4.0.1
2 parents e6eddb0 + f833cfc commit a821265

File tree

11 files changed

+288
-16
lines changed

11 files changed

+288
-16
lines changed

Branch-SDK/androidTest/io/branch/referral/BranchEventTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ public void testStandardEvent() throws Throwable {
3333
Assert.assertTrue(isStandardEvent(branchEvent));
3434
}
3535

36-
@Test
37-
public void testCustomerEventAlias() throws Throwable {
38-
BRANCH_STANDARD_EVENT eventType = BRANCH_STANDARD_EVENT.CUSTOMER_EVENT_ALIAS;
39-
Assert.assertEquals("CUSTOMER_EVENT_ALIAS", eventType.getName());
40-
41-
BranchEvent branchEvent = new BranchEvent(eventType);
42-
Assert.assertTrue(isStandardEvent(branchEvent));
43-
}
44-
4536
@Test
4637
public void testCustomEvent() throws Throwable {
4738
BranchEvent branchEvent = new BranchEvent("CustomEvent");
@@ -85,6 +76,7 @@ public void testAddAllEventExtras() {
8576
event.setSearchQuery("Love");
8677
event.setShipping(0.001);
8778
event.setTax(10);
79+
event.setCustomerEventAlias("potato_event");
8880

8981
event.addCustomDataProperty("test", "test value");
9082
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package io.branch.referral;
2+
3+
import io.branch.referral.Defines.ModuleNameKeys;
4+
import io.branch.referral.util.CommerceEvent;
5+
import org.json.JSONObject;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
/**
10+
* Created by --vbajpai on --2019-08-29 at --21:56 for --android-branch-deep-linking-attribution
11+
*/
12+
public class BranchModuleInjectionTest extends BranchEventTest {
13+
14+
@Test
15+
public void testResultSuccess() throws Throwable {
16+
Branch branch = Branch.getInstance(getTestContext());
17+
JSONObject branchFileJson = new JSONObject("{\"imei\":\"1234567890\"}");
18+
branch.addModule(branchFileJson);
19+
20+
initQueue(getTestContext());
21+
22+
ServerRequestQueue queue = ServerRequestQueue.getInstance(getTestContext());
23+
Assert.assertEquals(1, queue.getSize());
24+
25+
ServerRequest initRequest = queue.peekAt(0);
26+
doFinalUpdate(initRequest);
27+
doFinalUpdateOnMainThread(initRequest);
28+
29+
Assert.assertTrue(hasV1InstallImeiData(initRequest));
30+
}
31+
32+
@Test
33+
public void testNoModuleAddedWhenModuleNameMismatch() throws Throwable {
34+
Branch branch = Branch.getInstance(getTestContext());
35+
JSONObject branchFileJson = new JSONObject("{\"imei_rouge\":\"1234567890\"}");
36+
branch.addModule(branchFileJson);
37+
38+
initQueue(getTestContext());
39+
40+
ServerRequestQueue queue = ServerRequestQueue.getInstance(getTestContext());
41+
Assert.assertEquals(1, queue.getSize());
42+
43+
ServerRequest initRequest = queue.peekAt(0);
44+
doFinalUpdate(initRequest);
45+
doFinalUpdateOnMainThread(initRequest);
46+
47+
Assert.assertTrue(doesNotHaveV1InstallImeiData(initRequest));
48+
}
49+
50+
@Test
51+
public void testCommerceEventHasImeiData() throws Throwable {
52+
Branch branch = Branch.getInstance(getTestContext());
53+
JSONObject branchFileJson = new JSONObject("{\"imei\":\"1234567890\"}");
54+
branch.addModule(branchFileJson);
55+
56+
initQueue(getTestContext());
57+
58+
CommerceEvent commerceEvent = new CommerceEvent();
59+
commerceEvent.setTransactionID("123XYZ");
60+
commerceEvent.setRevenue(3.14);
61+
commerceEvent.setTax(.314);
62+
commerceEvent.setCoupon("MyCoupon");
63+
64+
Branch.getInstance().sendCommerceEvent(commerceEvent);
65+
ServerRequest serverRequest = findEventOnQueue(getTestContext(), "event", "purchase");
66+
67+
Assert.assertNotNull(serverRequest);
68+
doFinalUpdate(serverRequest);
69+
70+
Assert.assertTrue(hasCommerceImeiData(serverRequest));
71+
}
72+
73+
// Check to see if the module injected imei is in the install request
74+
private boolean hasV1InstallImeiData(ServerRequest request) {
75+
JSONObject jsonObject = request.getGetParams();
76+
String imeiValue = jsonObject.optString(ModuleNameKeys.imei.getKey());
77+
return (imeiValue.equals("1234567890"));
78+
}
79+
80+
// Check for null imei
81+
private boolean doesNotHaveV1InstallImeiData(ServerRequest request) {
82+
JSONObject jsonObject = request.getGetParams();
83+
String imeiValue = jsonObject.optString(ModuleNameKeys.imei.getKey());
84+
return (imeiValue.length()==0);
85+
}
86+
87+
// Check to see if the module injected imei is in the install request
88+
private boolean hasCommerceImeiData(ServerRequest request) {
89+
JSONObject jsonObject = request.getGetParams();
90+
String imeiValue = jsonObject.optString(ModuleNameKeys.imei.getKey());
91+
return (imeiValue.equals("1234567890"));
92+
}
93+
}

Branch-SDK/src/io/branch/referral/Branch.java

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,26 @@ public Branch setPreinstallPartner(@NonNull String preInstallPartner) {
993993
return this;
994994
}
995995

996+
/**
997+
* <p>
998+
* Add key value pairs from the injected modules to all requests
999+
* </p>
1000+
*/
1001+
public void addModule(JSONObject module) {
1002+
if (module!=null) {
1003+
Iterator<String> keys = module.keys();
1004+
while (keys.hasNext()) {
1005+
String key = keys.next();
1006+
if (!TextUtils.isEmpty(key)) {
1007+
try {
1008+
prefHelper_.addSecondaryRequestMetadata(key, module.getString(key));
1009+
} catch (JSONException ignore) {
1010+
}
1011+
}
1012+
}
1013+
}
1014+
}
1015+
9961016
/**
9971017
* <p>Initialises a session with the Branch API, assigning a {@link BranchUniversalReferralInitListener}
9981018
* to perform an action upon successful initialisation.</p>
@@ -1366,7 +1386,52 @@ public boolean initSession(BranchReferralInitListener callback, boolean isReferr
13661386
initUserSessionInternal(callback, activity, isReferrable);
13671387
return true;
13681388
}
1369-
1389+
1390+
/**
1391+
* Re-Initialize a session.
1392+
* This solves a very specific use case, whereas the app is already in the foreground and a new
1393+
* intent with a Uri is delivered to the activity. In this case we want to re-initialize the
1394+
* session and call back with the decoded parameters.
1395+
*
1396+
* @param activity The calling {@link Activity} for context.
1397+
* @param callback A {@link BranchReferralInitListener} instance that will be called
1398+
* following successful (or unsuccessful) initialization of the session
1399+
* with the Branch API.
1400+
* @return A {@link boolean} value that returns <i>false</i> if unsuccessful.
1401+
*/
1402+
public boolean reInitSession(Activity activity, BranchReferralInitListener callback) {
1403+
if (activity == null) {
1404+
return false;
1405+
}
1406+
1407+
Intent intent = activity.getIntent();
1408+
if (intent != null) {
1409+
// Check to see if the intent has Uri data.
1410+
Uri uri = intent.getData();
1411+
if (uri != null) {
1412+
// Here is where it gets interesting. Re-Initializing with a Uri indicates that
1413+
// we want to fetch the data before we return.
1414+
initState_ = SESSION_STATE.INITIALISING;
1415+
1416+
// Let's indicate that the app was initialized with this uri.
1417+
setSessionReferredLink(uri.toString());
1418+
1419+
// We need to set the AndroidAppLinkURL as well
1420+
prefHelper_.setAppLink(uri.toString());
1421+
1422+
// In order to report to the callback a second time, we need to reset this flag.
1423+
isInitReportedThroughCallBack = false;
1424+
1425+
// This will put an open event on the queue
1426+
initializeSession(callback);
1427+
1428+
// Now, actually initialize the new session.
1429+
initSession(callback, uri, activity);
1430+
}
1431+
}
1432+
1433+
return true;
1434+
}
13701435

13711436
private void initUserSessionInternal(BranchUniversalReferralInitListener callback, Activity activity, boolean isReferrable) {
13721437
BranchUniversalReferralInitWrapper branchUniversalReferralInitWrapper = new BranchUniversalReferralInitWrapper(callback);

Branch-SDK/src/io/branch/referral/Defines.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public enum Jsonkey {
146146
InstantApp("INSTANT_APP"),
147147
NativeApp("FULL_APP"),
148148

149+
CustomerEventAlias("customer_event_alias"),
149150
TransactionID("transaction_id"),
150151
Currency("currency"),
151152
Revenue("revenue"),
@@ -304,5 +305,29 @@ public String toString() {
304305
}
305306

306307
}
307-
308+
309+
/**
310+
* <p>
311+
* Defines module name keys
312+
* </p>
313+
*/
314+
public enum ModuleNameKeys {
315+
imei("imei");
316+
317+
private String key = "";
318+
319+
ModuleNameKeys(String key) {
320+
this.key = key;
321+
}
322+
323+
public String getKey() {
324+
return key;
325+
}
326+
327+
@Override
328+
public String toString() {
329+
return key;
330+
}
331+
332+
}
308333
}

Branch-SDK/src/io/branch/referral/DeviceInfo.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.util.DisplayMetrics;
77
import android.webkit.WebSettings;
88

9+
import io.branch.referral.Defines.ModuleNameKeys;
910
import org.json.JSONException;
1011
import org.json.JSONObject;
1112

@@ -105,6 +106,12 @@ void updateRequestWithV1Params(JSONObject requestObj) {
105106
requestObj.put(Defines.Jsonkey.LocalIP.getKey(), localIpAddr);
106107
}
107108

109+
if (PrefHelper.getInstance(context_).shouldAddModules()) {
110+
String imei = SystemObserver.getImei(context_);
111+
if (!isNullOrEmptyOrBlank(imei)) {
112+
requestObj.put(ModuleNameKeys.imei.getKey(), imei);
113+
}
114+
}
108115
} catch (JSONException ignore) {
109116

110117
}
@@ -171,6 +178,13 @@ void updateRequestWithV2Params(Context context, PrefHelper prefHelper, JSONObjec
171178
}
172179
}
173180

181+
if (prefHelper != null && prefHelper.shouldAddModules()) {
182+
String imei = SystemObserver.getImei(context_);
183+
if (!isNullOrEmptyOrBlank(imei)) {
184+
requestObj.put(ModuleNameKeys.imei.getKey(), imei);
185+
}
186+
}
187+
174188
requestObj.put(Defines.Jsonkey.AppVersion.getKey(), getAppVersion());
175189
requestObj.put(Defines.Jsonkey.SDK.getKey(), "android");
176190
requestObj.put(Defines.Jsonkey.SdkVersion.getKey(), BuildConfig.VERSION_NAME);

Branch-SDK/src/io/branch/referral/PrefHelper.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.util.Log;
1111
import android.webkit.URLUtil;
1212

13+
import java.util.List;
1314
import org.json.JSONArray;
1415
import org.json.JSONException;
1516
import org.json.JSONObject;
@@ -128,7 +129,15 @@ public class PrefHelper {
128129
* Arbitrary key values added to Install requests.
129130
*/
130131
private final JSONObject installMetadata;
131-
132+
133+
/**
134+
* Module injected key values added to all requests.
135+
*/
136+
private final JSONObject secondaryRequestMetadata;
137+
138+
139+
//private final List<Module> factories = new ArrayList<>();
140+
132141
/**
133142
* Branch Content discovery data
134143
*/
@@ -152,6 +161,7 @@ private PrefHelper(Context context) {
152161
this.prefsEditor_ = this.appSharedPrefs_.edit();
153162
this.requestMetadata = new JSONObject();
154163
this.installMetadata = new JSONObject();
164+
this.secondaryRequestMetadata = new JSONObject();
155165
}
156166

157167
/**
@@ -1188,6 +1198,54 @@ public JSONObject getInstallMetadata() {
11881198
return installMetadata;
11891199
}
11901200

1201+
/**
1202+
* adds the Module injected key-value pairs in the all the requests
1203+
* adds the Module injected key-value pairs in the all the requests
1204+
*
1205+
* @param key A {@link String} value containing the key to reference.
1206+
* @param value A {@link String} value of the specified key to be added in the request
1207+
*/
1208+
void addSecondaryRequestMetadata(String key, String value) {
1209+
if (key == null) {
1210+
return;
1211+
}
1212+
try {
1213+
secondaryRequestMetadata.putOpt(key, value);
1214+
} catch (JSONException ignore) {
1215+
}
1216+
}
1217+
1218+
/**
1219+
* gets the Module injected value
1220+
*
1221+
* @param key A {@link String} value containing the key to reference.
1222+
* @return value A {@link String} value of the specified key to be added in the request
1223+
*/
1224+
String getSecondaryRequestMetaData(String key) {
1225+
if (key == null) {
1226+
return null;
1227+
}
1228+
1229+
try {
1230+
return this.secondaryRequestMetadata.get(key).toString();
1231+
} catch (JSONException ignore) {
1232+
return null;
1233+
}
1234+
}
1235+
1236+
/**
1237+
* helper method to check of the modules need to be added in the requests
1238+
*
1239+
* @return value A {@link Boolean} returns true if the module data is present else false
1240+
*/
1241+
boolean shouldAddModules () {
1242+
try {
1243+
return secondaryRequestMetadata.length() != 0;
1244+
} catch (Exception ignore) {
1245+
return false;
1246+
}
1247+
}
1248+
11911249
/**
11921250
* <p>Creates a <b>Debug</b> message in the debugger. If debugging is disabled, this will fail silently.</p>
11931251
*

Branch-SDK/src/io/branch/referral/SystemObserver.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import android.view.Display;
1616
import android.view.WindowManager;
1717

18+
import io.branch.referral.Defines.ModuleNameKeys;
1819
import java.lang.ref.WeakReference;
1920
import java.lang.reflect.Method;
2021
import java.net.InetAddress;
@@ -572,4 +573,15 @@ int getLATVal() {
572573
return LATVal_;
573574
}
574575

576+
/**
577+
* Get IP address from the Module injected key-value pairs
578+
*/
579+
static String getImei(Context context_) {
580+
String imei = PrefHelper.getInstance(context_)
581+
.getSecondaryRequestMetaData(ModuleNameKeys.imei.getKey());
582+
if (!TextUtils.isEmpty(imei)) {
583+
return imei;
584+
}
585+
return null;
586+
}
575587
}

Branch-SDK/src/io/branch/referral/util/BRANCH_STANDARD_EVENT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public enum BRANCH_STANDARD_EVENT {
3636
SUBSCRIBE("SUBSCRIBE"),
3737
START_TRIAL("START_TRIAL"),
3838
CLICK_AD("CLICK_AD"),
39-
VIEW_AD("VIEW_AD"),
40-
CUSTOMER_EVENT_ALIAS("CUSTOMER_EVENT_ALIAS");
39+
VIEW_AD("VIEW_AD");
4140

4241
private final String name;
4342

0 commit comments

Comments
 (0)