Skip to content

Commit 5903ba9

Browse files
[SDK-2459] Implement Consumer Protection Preferences (#1206)
* Initial commit * Changed naming and added testbed button * Updated naming * Updated enum name * Fixed typo * Updated automation testbed * Update Defines.java * Fixed default behavior * Removed line from testbed * Updated to point to correct automation branch * Updated enum and changed request format * Update ServerRequest.java * Testbed update for Mobileboost tests * More testbed edits * Removed Appium testbed changes * Updated deprecation comment * Updated deprecation comment * Updated logic for re-enabling tracking * Updated Toast length * Added more logging * Added callback exposure * Fixed typo
1 parent 50d5b5a commit 5903ba9

File tree

20 files changed

+409
-48
lines changed

20 files changed

+409
-48
lines changed

Branch-SDK-Automation-TestBed/src/main/java/io/branch/branchandroiddemo/BranchWrapper.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package io.branch.branchandroiddemo;
22

3-
import static android.content.Intent.getIntent;
4-
import static androidx.core.content.ContextCompat.startActivity;
5-
6-
import static java.security.AccessController.getContext;
7-
83
import android.app.Activity;
94
import android.content.Context;
105
import android.content.Intent;
116
import android.graphics.Bitmap;
127
import android.graphics.Color;
138
import android.net.Uri;
149
import android.os.Build;
15-
import android.text.TextUtils;
1610
import android.util.Log;
1711

1812
import org.json.JSONObject;

Branch-SDK-Automation-TestBed/src/main/java/io/branch/branchandroiddemo/activities/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void onClick(View view) {
8989
branchWrapper.showLogWindow("",false, this, Constants.LOG_DATA);
9090
} else if (view == btCreateQrCode) {
9191
branchWrapper.getQRCode(this, getIntent(), MainActivity.this);
92-
}else {
92+
} else {
9393
branchWrapper.showLogWindow("",false, this, Constants.UNKNOWN);
9494
}
9595
}

Branch-SDK-Automation-TestBed/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
android:layout_marginBottom="@dimen/_2sdp"
115115
android:text="Send Standard Event"
116116
android:textAllCaps="false" />
117+
117118
<Button
118119
android:id="@+id/bt_Read_Logs"
119120
android:layout_width="match_parent"

Branch-SDK-TestBed/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
</activity>
115115

116116
<activity android:name="io.branch.branchandroidtestbed.SettingsActivity" />
117-
117+
<activity android:name="io.branch.branchandroidtestbed.LogOutputActivity" />
118118
<activity android:name="io.branch.branchandroidtestbed.AutoDeepLinkTestActivity">
119119
<!-- Keys for auto deep linking this activity -->
120120
<meta-data

Branch-SDK-TestBed/src/main/java/io/branch/branchandroidtestbed/CustomBranchApp.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,47 @@
33
import android.app.Application;
44
import android.util.Log;
55

6+
import java.io.File;
7+
import java.io.FileOutputStream;
8+
import java.io.OutputStreamWriter;
9+
610
import io.branch.interfaces.IBranchLoggingCallbacks;
711
import io.branch.referral.Branch;
812
import io.branch.referral.BranchLogger;
13+
import io.branch.referral.Defines;
914

1015
public final class CustomBranchApp extends Application {
1116
@Override
1217
public void onCreate() {
1318
super.onCreate();
14-
15-
IBranchLoggingCallbacks iBranchLoggingCallbacks = new IBranchLoggingCallbacks() {
16-
@Override
17-
public void onBranchLog(String logMessage, String severityConstantName) {
18-
Log.v( "CustomTag", logMessage);
19-
}
19+
20+
IBranchLoggingCallbacks loggingCallbacks = (message, tag) -> {
21+
Log.d("BranchTestbed", message);
22+
saveLogToFile(message);
2023
};
21-
Branch.enableLogging(BranchLogger.BranchLogLevel.VERBOSE); // Pass in iBranchLoggingCallbacks to enable logging redirects
24+
Branch.enableLogging(loggingCallbacks);
25+
2226
Branch.getAutoInstance(this);
2327
}
28+
29+
private void saveLogToFile(String logMessage) {
30+
File logFile = new File(getFilesDir(), "branchlogs.txt");
31+
32+
try {
33+
if (!logFile.exists()) {
34+
boolean fileCreated = logFile.createNewFile();
35+
Log.d("BranchTestbed", "Log file created: " + fileCreated);
36+
}
37+
38+
try (FileOutputStream fos = new FileOutputStream(logFile, true);
39+
OutputStreamWriter writer = new OutputStreamWriter(fos)) {
40+
writer.write(logMessage + "\n");
41+
}
42+
43+
} catch (Exception e) {
44+
Log.e("BranchTestbed", "Error writing to log file", e);
45+
}
46+
}
47+
48+
2449
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io.branch.branchandroidtestbed;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.view.Menu;
6+
import android.view.MenuItem;
7+
import android.widget.TextView;
8+
import android.widget.Toast;
9+
10+
import java.io.BufferedReader;
11+
import java.io.File;
12+
import java.io.FileInputStream;
13+
import java.io.InputStreamReader;
14+
15+
public class LogOutputActivity extends Activity {
16+
private TextView logOutputTextView;
17+
private File logFile;
18+
19+
@Override
20+
protected void onCreate(Bundle savedInstanceState) {
21+
super.onCreate(savedInstanceState);
22+
setContentView(R.layout.activity_log_output);
23+
24+
logOutputTextView = findViewById(R.id.logOutputTextView);
25+
logFile = new File(getFilesDir(), "branchlogs.txt");
26+
displayLogs();
27+
}
28+
29+
@Override
30+
public boolean onCreateOptionsMenu(Menu menu) {
31+
getMenuInflater().inflate(R.menu.menu_log_output, menu);
32+
return true;
33+
}
34+
35+
@Override
36+
public boolean onOptionsItemSelected(MenuItem item) {
37+
if (item.getItemId() == R.id.action_clear_logs) {
38+
clearLogs();
39+
return true;
40+
}
41+
return super.onOptionsItemSelected(item);
42+
}
43+
44+
private void displayLogs() {
45+
if (logFile.exists()) {
46+
StringBuilder logContent = new StringBuilder();
47+
48+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(logFile)))) {
49+
String line;
50+
while ((line = reader.readLine()) != null) {
51+
logContent.append(line).append("\n");
52+
}
53+
} catch (Exception e) {
54+
e.printStackTrace();
55+
logContent.append("Error reading log file.");
56+
}
57+
58+
logOutputTextView.setText(logContent.toString());
59+
} else {
60+
logOutputTextView.setText("Log file not found.");
61+
}
62+
}
63+
64+
private void clearLogs() {
65+
if (logFile.exists()) {
66+
if (logFile.delete()) {
67+
Toast.makeText(this, "Logs cleared.", Toast.LENGTH_SHORT).show();
68+
logOutputTextView.setText("Logs cleared.");
69+
finish();
70+
} else {
71+
Toast.makeText(this, "Failed to clear logs.", Toast.LENGTH_SHORT).show();
72+
}
73+
} else {
74+
Toast.makeText(this, "No log file to clear.", Toast.LENGTH_SHORT).show();
75+
}
76+
}
77+
}

Branch-SDK-TestBed/src/main/java/io/branch/branchandroidtestbed/MainActivity.java

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import android.util.Log;
1414
import android.view.View;
1515
import android.view.View.OnClickListener;
16-
import android.widget.CompoundButton;
1716
import android.widget.EditText;
1817
import android.widget.ImageView;
1918
import android.widget.Toast;
@@ -128,7 +127,7 @@ public void onInitFinished(JSONObject referringParams, BranchError error) {
128127
if (error != null) {
129128
Log.e("BranchSDK_Tester", "branch set Identity failed. Caused by -" + error.getMessage());
130129
}
131-
Toast.makeText(getApplicationContext(), "Set Identity to " + userID, Toast.LENGTH_SHORT).show();
130+
Toast.makeText(getApplicationContext(), "Set Identity to " + userID, Toast.LENGTH_LONG).show();
132131

133132

134133
}
@@ -154,10 +153,10 @@ public void onClick(View v) {
154153
public void onLogoutFinished(boolean loggedOut, BranchError error) {
155154
if (error != null) {
156155
Log.e("BranchSDK_Tester", "onLogoutFinished Error: " + error);
157-
Toast.makeText(getApplicationContext(), "Error Logging Out: " + error.getMessage(), Toast.LENGTH_SHORT).show();
156+
Toast.makeText(getApplicationContext(), "Error Logging Out: " + error.getMessage(), Toast.LENGTH_LONG).show();
158157
} else {
159158
Log.d("BranchSDK_Tester", "onLogoutFinished succeeded: " + loggedOut);
160-
Toast.makeText(getApplicationContext(), "Cleared User ID: " + currentUserId, Toast.LENGTH_SHORT).show();
159+
Toast.makeText(getApplicationContext(), "Cleared User ID: " + currentUserId, Toast.LENGTH_LONG).show();
161160
}
162161
}
163162
});
@@ -415,6 +414,13 @@ public void onChannelSelected(String channelName) {
415414
}
416415
});
417416

417+
findViewById(R.id.viewLogsButton).setOnClickListener(new View.OnClickListener() {
418+
@Override
419+
public void onClick(View v) {
420+
Intent intent = new Intent(MainActivity.this, LogOutputActivity.class);
421+
startActivity(intent);
422+
}
423+
});
418424

419425
findViewById(R.id.notif_btn).setOnClickListener(new OnClickListener() {
420426
@Override
@@ -454,13 +460,40 @@ public void onClick(View v) {
454460
((ToggleButton) findViewById(R.id.tracking_cntrl_btn)).setOnCheckedChangeListener((buttonView, isChecked) -> {
455461
Branch.getInstance().disableTracking(isChecked, (trackingDisabled, referringParams, error) -> {
456462
if (trackingDisabled) {
457-
Toast.makeText(getApplicationContext(), "Disabled Tracking", Toast.LENGTH_SHORT).show();
463+
Toast.makeText(getApplicationContext(), "Disabled Tracking", Toast.LENGTH_LONG).show();
458464
} else {
459-
Toast.makeText(getApplicationContext(), "Enabled Tracking", Toast.LENGTH_SHORT).show();
465+
Toast.makeText(getApplicationContext(), "Enabled Tracking", Toast.LENGTH_LONG).show();
460466
}
461467
});
462468
});
463469

470+
findViewById(R.id.cmdConsumerProtectionPreference).setOnClickListener(v -> {
471+
final String[] options = {"Full", "Reduced", "Minimal", "None"};
472+
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
473+
builder.setTitle("Select Consumer Protection Attribution Level")
474+
.setItems(options, (dialog, which) -> {
475+
Defines.BranchAttributionLevel preference;
476+
switch (which) {
477+
case 1:
478+
preference = Defines.BranchAttributionLevel.REDUCED;
479+
break;
480+
case 2:
481+
preference = Defines.BranchAttributionLevel.MINIMAL;
482+
break;
483+
case 3:
484+
preference = Defines.BranchAttributionLevel.NONE;
485+
break;
486+
case 0:
487+
default:
488+
preference = Defines.BranchAttributionLevel.FULL;
489+
break;
490+
}
491+
Branch.getInstance().setConsumerProtectionAttributionLevel(preference);
492+
Toast.makeText(MainActivity.this, "Consumer Protection Preference set to " + options[which], Toast.LENGTH_LONG).show();
493+
});
494+
builder.create().show();
495+
});
496+
464497
findViewById(R.id.qrCode_btn).setOnClickListener(new OnClickListener() {
465498
@Override
466499
public void onClick(View view) {
@@ -541,13 +574,13 @@ public void onClick(View v) {
541574
.logEvent(MainActivity.this, new BranchEvent.BranchLogEventCallback() {
542575
@Override
543576
public void onSuccess(int responseCode) {
544-
Toast.makeText(getApplicationContext(), "Sent Branch Commerce Event: " + responseCode, Toast.LENGTH_SHORT).show();
577+
Toast.makeText(getApplicationContext(), "Sent Branch Commerce Event: " + responseCode, Toast.LENGTH_LONG).show();
545578
}
546579

547580
@Override
548581
public void onFailure(Exception e) {
549582
Log.d("BranchSDK_Tester", e.toString());
550-
Toast.makeText(getApplicationContext(), "Error sending Branch Commerce Event: " + e.toString(), Toast.LENGTH_SHORT).show();
583+
Toast.makeText(getApplicationContext(), "Error sending Branch Commerce Event: " + e.toString(), Toast.LENGTH_LONG).show();
551584
}
552585
});
553586

@@ -565,8 +598,18 @@ public void onClick(View v) {
565598
.setSearchQuery("product name")
566599
.addCustomDataProperty("Custom_Event_Property_Key1", "Custom_Event_Property_val1")
567600
.addContentItems(branchUniversalObject)
568-
.logEvent(MainActivity.this);
569-
Toast.makeText(getApplicationContext(), "Sent Branch Content Event", Toast.LENGTH_SHORT).show();
601+
.logEvent(MainActivity.this, new BranchEvent.BranchLogEventCallback() {
602+
@Override
603+
public void onSuccess(int responseCode) {
604+
Toast.makeText(getApplicationContext(), "Sent Branch Content Event: " + responseCode, Toast.LENGTH_LONG).show();
605+
}
606+
607+
@Override
608+
public void onFailure(Exception e) {
609+
Log.d("BranchSDK_Tester", e.toString());
610+
Toast.makeText(getApplicationContext(), "Error sending Branch Content Event: " + e.toString(), Toast.LENGTH_LONG).show();
611+
}
612+
});
570613
}
571614
});
572615

@@ -580,8 +623,18 @@ public void onClick(View v) {
580623
.setDescription("User created an account")
581624
.addCustomDataProperty("registrationID", "12345")
582625
.addContentItems(branchUniversalObject)
583-
.logEvent(MainActivity.this);
584-
Toast.makeText(getApplicationContext(), "Sent Branch Lifecycle Event", Toast.LENGTH_SHORT).show();
626+
.logEvent(MainActivity.this, new BranchEvent.BranchLogEventCallback() {
627+
@Override
628+
public void onSuccess(int responseCode) {
629+
Toast.makeText(getApplicationContext(), "Sent Branch Lifecycle Event: " + responseCode, Toast.LENGTH_LONG).show();
630+
}
631+
632+
@Override
633+
public void onFailure(Exception e) {
634+
Log.d("BranchSDK_Tester", e.toString());
635+
Toast.makeText(getApplicationContext(), "Error sending Branch Lifecycle Event: " + e, Toast.LENGTH_LONG).show();
636+
}
637+
});
585638
}
586639
});
587640

@@ -592,7 +645,7 @@ public void onClick(View v) {
592645
@Override
593646
public void onLogoutFinished(boolean loggedOut, BranchError error) {
594647
Log.d("BranchSDK_Tester", "onLogoutFinished " + loggedOut + " errorMessage " + error);
595-
Toast.makeText(getApplicationContext(), "Logged Out", Toast.LENGTH_SHORT).show();
648+
Toast.makeText(getApplicationContext(), "Logged Out", Toast.LENGTH_LONG).show();
596649
}
597650
});
598651

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#FFFFFF" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
2+
3+
<path android:fillColor="@android:color/white" android:pathData="M7,3H4v3H2V1h5V3zM22,6V1h-5v2h3v3H22zM7,21H4v-3H2v5h5V21zM20,18v3h-3v2h5v-5H20zM19,18c0,1.1 -0.9,2 -2,2H7c-1.1,0 -2,-0.9 -2,-2V6c0,-1.1 0.9,-2 2,-2h10c1.1,0 2,0.9 2,2V18zM15,8H9v2h6V8zM15,11H9v2h6V11zM15,14H9v2h6V14z"/>
4+
5+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#FFFFFF" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
2+
3+
<path android:fillColor="@android:color/white" android:pathData="M12,1L3,5v6c0,5.55 3.84,10.74 9,12 5.16,-1.26 9,-6.45 9,-12L21,5l-9,-4zM12,11.99h7c-0.53,4.12 -3.28,7.79 -7,8.94L12,12L5,12L5,6.3l7,-3.11v8.8z"/>
4+
5+
</vector>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<ScrollView
7+
android:layout_width="match_parent"
8+
android:layout_height="match_parent"
9+
android:padding="16dp">
10+
11+
<TextView
12+
android:id="@+id/logOutputTextView"
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:textSize="14sp"
16+
android:text="Logs will appear here" />
17+
</ScrollView>
18+
</RelativeLayout>

0 commit comments

Comments
 (0)