Skip to content

Commit f509324

Browse files
Merge pull request #990 from BranchMetrics/SDK-1403
[SDK-1403] Added QR code methods
2 parents 8ec5baa + df12eb7 commit f509324

File tree

14 files changed

+1108
-21
lines changed

14 files changed

+1108
-21
lines changed

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

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package io.branch.branchandroiddemo;
22

33
import android.app.Activity;
4+
import android.app.AlertDialog;
45
import android.app.NotificationChannel;
56
import android.app.NotificationManager;
67
import android.app.PendingIntent;
8+
import android.content.DialogInterface;
79
import android.content.Intent;
8-
import android.net.Uri;
10+
import android.graphics.Bitmap;
11+
import android.graphics.Color;
912
import android.os.Build;
1013
import android.os.Bundle;
11-
import android.os.Handler;
1214
import android.util.Log;
1315
import android.view.View;
1416
import android.view.View.OnClickListener;
1517
import android.widget.CompoundButton;
1618
import android.widget.EditText;
19+
import android.widget.ImageView;
1720
import android.widget.TextView;
1821
import android.widget.ToggleButton;
1922

@@ -24,20 +27,18 @@
2427
import org.json.JSONException;
2528
import org.json.JSONObject;
2629

30+
import java.io.IOException;
2731
import java.nio.charset.Charset;
28-
import java.nio.charset.StandardCharsets;
2932
import java.security.MessageDigest;
30-
import java.security.NoSuchAlgorithmException;
3133
import java.util.Date;
3234

3335
import io.branch.indexing.BranchUniversalObject;
3436
import io.branch.referral.Branch;
3537
import io.branch.referral.Branch.BranchReferralInitListener;
36-
import io.branch.referral.Branch.BranchReferralStateChangedListener;
3738
import io.branch.referral.BranchError;
39+
import io.branch.referral.QRCode.BranchQRCode;
3840
import io.branch.referral.BranchViewHandler;
3941
import io.branch.referral.Defines;
40-
import io.branch.referral.PrefHelper;
4142
import io.branch.referral.SharingHelper;
4243
import io.branch.referral.util.BRANCH_STANDARD_EVENT;
4344
import io.branch.referral.util.BranchContentSchema;
@@ -48,8 +49,6 @@
4849
import io.branch.referral.util.ProductCategory;
4950
import io.branch.referral.util.ShareSheetStyle;
5051

51-
import static java.nio.charset.StandardCharsets.UTF_8;
52-
5352

5453
public class MainActivity extends Activity {
5554
private EditText txtShortUrl;
@@ -373,6 +372,65 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
373372
}
374373
});
375374

375+
findViewById(R.id.qrCode_btn).setOnClickListener(new OnClickListener() {
376+
@Override
377+
public void onClick(View view) {
378+
BranchQRCode qrCode = new BranchQRCode()
379+
.setCodeColor("#57dbe0")
380+
.setBackgroundColor("#2a2e2e")
381+
.setMargin(2)
382+
.setWidth(512)
383+
.setImageFormat(BranchQRCode.BranchImageFormat.PNG)
384+
.setCenterLogo("https://cdn.branch.io/branch-assets/1598575682753-og_image.png");
385+
386+
BranchUniversalObject buo = new BranchUniversalObject()
387+
.setCanonicalIdentifier("content/12345")
388+
.setTitle("My Content Title")
389+
.setContentDescription("My Content Description")
390+
.setContentImageUrl("https://lorempixel.com/400/400");
391+
392+
LinkProperties lp = new LinkProperties()
393+
.setChannel("facebook")
394+
.setFeature("sharing")
395+
.setCampaign("content 123 launch")
396+
.setStage("new user");
397+
398+
try {
399+
qrCode.getQRCodeAsImage(MainActivity.this, buo, lp, new BranchQRCode.BranchQRCodeImageHandler() {
400+
@Override
401+
public void onSuccess(Bitmap qrCodeImage) {
402+
try {
403+
AlertDialog.Builder ImageDialog = new AlertDialog.Builder(MainActivity.this);
404+
ImageDialog.setTitle("Your QR Code");
405+
ImageView showImage = new ImageView(MainActivity.this);
406+
407+
showImage.setImageBitmap(qrCodeImage);
408+
ImageDialog.setView(showImage);
409+
410+
ImageDialog.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
411+
public void onClick(DialogInterface arg0, int arg1) {
412+
}
413+
});
414+
ImageDialog.show();
415+
416+
} catch (Exception e) {
417+
Log.d("Adding Image to Alert", "Failed");
418+
e.printStackTrace();
419+
}
420+
421+
}
422+
423+
@Override
424+
public void onFailure(Exception e) {
425+
Log.d("Fail in main activity", String.valueOf(e));
426+
427+
}
428+
});
429+
} catch (IOException e) {
430+
e.printStackTrace();
431+
}
432+
}
433+
});
376434
}
377435

378436
private void createNotificationChannel() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
android:layout_width="match_parent"
99
android:layout_height="wrap_content">
1010

11+
<Button
12+
android:id="@+id/qrCode_btn"
13+
style="@style/testbed_button_style"
14+
android:layout_width="180dp"
15+
android:layout_height="wrap_content"
16+
android:layout_below="@id/cmdPrintInstallParam"
17+
android:layout_alignParentRight="true"
18+
android:layout_marginTop="5dp"
19+
android:text="Create QR Code" />
20+
1121
<EditText
1222
android:id="@+id/editReferralShortUrl"
1323
android:layout_width="260dp"
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package io.branch.referral;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.Color;
5+
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.json.JSONException;
9+
import org.json.JSONObject;
10+
import org.junit.Assert;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
15+
import java.io.IOException;
16+
import java.util.concurrent.CountDownLatch;
17+
18+
import io.branch.indexing.BranchUniversalObject;
19+
import io.branch.referral.QRCode.BranchQRCode;
20+
import io.branch.referral.util.LinkProperties;
21+
22+
@RunWith(AndroidJUnit4.class)
23+
public class BranchQRCodeTests extends BranchTest {
24+
25+
@Before
26+
public void initializeValues(){
27+
initBranchInstance();
28+
}
29+
30+
@Test
31+
public void testQRCodeWithSettings() {
32+
initSessionResumeActivity(null, new Runnable() {
33+
@Override
34+
public void run() {
35+
BranchQRCode qrCode = new BranchQRCode()
36+
.setCodeColor("#a4c639")
37+
.setBackgroundColor(Color.WHITE)
38+
.setMargin(1)
39+
.setWidth(512)
40+
.setImageFormat(BranchQRCode.BranchImageFormat.PNG)
41+
.setCenterLogo("https://cdn.branch.io/branch-assets/1598575682753-og_image.png");
42+
43+
BranchUniversalObject buo = new BranchUniversalObject()
44+
.setCanonicalIdentifier("test/123")
45+
.setTitle("My Test Title")
46+
.setContentDescription("My Test Description")
47+
.setContentImageUrl("https://lorempixel.com/400/400");
48+
49+
LinkProperties lp = new LinkProperties()
50+
.setChannel("facebook")
51+
.setFeature("sharing")
52+
.setCampaign("test 123 launch")
53+
.setStage("test");
54+
55+
final CountDownLatch lock = new CountDownLatch(1);
56+
57+
try {
58+
qrCode.getQRCodeAsData(getTestContext(), buo, lp, new BranchQRCode.BranchQRCodeDataHandler() {
59+
@Override
60+
public void onSuccess(byte[] qrCodeData) {
61+
Assert.assertNotNull(qrCodeData);
62+
lock.countDown();
63+
}
64+
65+
@Override
66+
public void onFailure(Exception e) {
67+
e.printStackTrace();
68+
Assert.fail();
69+
}
70+
});
71+
} catch (IOException e) {
72+
e.printStackTrace();
73+
Assert.fail();
74+
}
75+
}
76+
});
77+
}
78+
79+
@Test
80+
public void testQRCodeWithNoSettings() {
81+
initSessionResumeActivity(null, new Runnable() {
82+
@Override
83+
public void run() {
84+
BranchQRCode qrCode = new BranchQRCode();
85+
86+
BranchUniversalObject buo = new BranchUniversalObject()
87+
.setCanonicalIdentifier("test/123")
88+
.setTitle("My Test Title")
89+
.setContentDescription("My Test Description")
90+
.setContentImageUrl("https://lorempixel.com/400/400");
91+
92+
LinkProperties lp = new LinkProperties()
93+
.setChannel("facebook")
94+
.setFeature("sharing")
95+
.setCampaign("test 123 launch")
96+
.setStage("test");
97+
98+
final CountDownLatch lock = new CountDownLatch(1);
99+
100+
try {
101+
qrCode.getQRCodeAsData(getTestContext(), buo, lp, new BranchQRCode.BranchQRCodeDataHandler() {
102+
@Override
103+
public void onSuccess(byte[] qrCodeData) {
104+
Assert.assertNotNull(qrCodeData);
105+
lock.countDown();
106+
}
107+
108+
@Override
109+
public void onFailure(Exception e) {
110+
e.printStackTrace();
111+
Assert.fail();
112+
}
113+
});
114+
} catch (IOException e) {
115+
e.printStackTrace();
116+
Assert.fail();
117+
}
118+
}
119+
});
120+
}
121+
122+
@Test
123+
public void testQRCodeAsImage() {
124+
initSessionResumeActivity(null, new Runnable() {
125+
@Override
126+
public void run() {
127+
BranchQRCode qrCode = new BranchQRCode();
128+
BranchUniversalObject buo = new BranchUniversalObject();
129+
LinkProperties lp = new LinkProperties();
130+
131+
final CountDownLatch lock = new CountDownLatch(1);
132+
133+
try {
134+
qrCode.getQRCodeAsImage(branch.getCurrentActivity(), buo, lp, new BranchQRCode.BranchQRCodeImageHandler() {
135+
@Override
136+
public void onSuccess(Bitmap qrCodeImage) {
137+
Assert.assertNotNull(qrCodeImage);
138+
lock.countDown();
139+
}
140+
141+
@Override
142+
public void onFailure(Exception e) {
143+
e.printStackTrace();
144+
Assert.fail();
145+
}
146+
});
147+
} catch (IOException e) {
148+
e.printStackTrace();
149+
Assert.fail();
150+
}
151+
}
152+
});
153+
}
154+
155+
@Test
156+
public void testQRCodeCache() {
157+
initSessionResumeActivity(null, new Runnable() {
158+
@Override
159+
public void run() {
160+
final BranchQRCode qrCode = new BranchQRCode();
161+
BranchUniversalObject buo = new BranchUniversalObject();
162+
LinkProperties lp = new LinkProperties();
163+
164+
final CountDownLatch lock = new CountDownLatch(1);
165+
try {
166+
167+
qrCode.getQRCodeAsData(getTestContext(), buo, lp, new BranchQRCode.BranchQRCodeDataHandler() {
168+
@Override
169+
public void onSuccess(byte[] qrCodeData) {
170+
try {
171+
JSONObject expectedCachedParams = new JSONObject("{\"feature\":\"Share\",\"stage\":\"\",\"data\":{\"$publicly_indexable\":true,\"$locally_indexable\":true},\"channel\":\"\",\"qr_code_settings\":{\"image_format\":\"PNG\"},\"campaign\":\"\",\"branch_key\":\"key_live_testing_only\",\"tags\":[]}");
172+
byte[] cachedQRCodeData = BranchQRCodeCache.getInstance().checkQRCodeCache(expectedCachedParams);
173+
174+
Assert.assertEquals(qrCodeData, cachedQRCodeData);
175+
lock.countDown();
176+
} catch (JSONException e) {
177+
e.printStackTrace();
178+
}
179+
}
180+
181+
@Override
182+
public void onFailure(Exception e) {
183+
e.printStackTrace();
184+
Assert.fail();
185+
}
186+
});
187+
} catch (IOException e) {
188+
e.printStackTrace();
189+
Assert.fail();
190+
}
191+
}
192+
});
193+
194+
195+
}
196+
197+
198+
}

0 commit comments

Comments
 (0)