6
6
import android .net .Uri ;
7
7
import android .os .Build ;
8
8
import android .util .Log ;
9
+ import android .util .Base64 ;
9
10
10
11
import org .apache .cordova .CallbackContext ;
11
12
import org .apache .cordova .CordovaPlugin ;
16
17
17
18
import java .util .ArrayList ;
18
19
import java .util .Iterator ;
20
+ import java .io .IOException ;
19
21
20
22
import io .branch .indexing .BranchUniversalObject ;
21
23
import io .branch .referral .Branch ;
24
26
import io .branch .referral .ServerRequestGetCPID .BranchCrossPlatformIdListener ;
25
27
import io .branch .referral .ServerRequestGetLATD .BranchLastAttributedTouchDataListener ;
26
28
import io .branch .referral .SharingHelper ;
29
+ import io .branch .referral .QRCode .BranchQRCode ;
27
30
import io .branch .referral .util .BRANCH_STANDARD_EVENT ;
28
31
import io .branch .referral .util .BranchCPID ;
29
32
import io .branch .referral .util .BranchEvent ;
30
33
import io .branch .referral .util .ContentMetadata ;
31
34
import io .branch .referral .util .CurrencyType ;
32
35
import io .branch .referral .util .ShareSheetStyle ;
33
36
37
+
34
38
public class BranchSDK extends CordovaPlugin {
35
39
36
40
static class BranchLinkProperties extends io .branch .referral .util .LinkProperties {
@@ -137,14 +141,14 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
137
141
return true ;
138
142
} else if (action .equals ("userCompletedAction" )) {
139
143
if (args .length () < 1 && args .length () > 2 ) {
140
- callbackContext .error (String .format ("Parameter mismatched. 1-2 is required but %d is given" , args . length () ));
144
+ callbackContext .error (String .format ("Parameter count mismatch" ));
141
145
return false ;
142
146
}
143
147
cordova .getActivity ().runOnUiThread (r );
144
148
return true ;
145
149
} else if (action .equals ("sendBranchEvent" )) {
146
150
if (args .length () < 1 && args .length () > 2 ) {
147
- callbackContext .error (String .format ("Parameter mismatched. 1-2 is required but %d is given" , args . length () ));
151
+ callbackContext .error (String .format ("Parameter count mismatch" ));
148
152
return false ;
149
153
}
150
154
cordova .getActivity ().runOnUiThread (r );
@@ -160,7 +164,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
160
164
return true ;
161
165
} else if (action .equals ("createBranchUniversalObject" )) {
162
166
if (args .length () != 1 ) {
163
- callbackContext .error (String .format ("Parameter mismatched. 1 is required but %d is given" , args . length () ));
167
+ callbackContext .error (String .format ("Parameter count mismatch" ));
164
168
return false ;
165
169
}
166
170
cordova .getActivity ().runOnUiThread (r );
@@ -176,21 +180,21 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
176
180
177
181
} else if (action .equals (("generateShortUrl" ))) {
178
182
if (args .length () != 3 ) {
179
- callbackContext .error (String .format ("Parameter mismatched. 3 is required but %d is given" , args . length () ));
183
+ callbackContext .error (String .format ("Parameter count mismatch" ));
180
184
return false ;
181
185
}
182
186
cordova .getActivity ().runOnUiThread (r );
183
187
return true ;
184
188
} else if (action .equals ("registerView" )) {
185
189
if (args .length () != 1 ) {
186
- callbackContext .error (String .format ("Parameter mismatched. 1 is required but %d is given" , args . length () ));
190
+ callbackContext .error (String .format ("Parameter count mismatch" ));
187
191
return false ;
188
192
}
189
193
cordova .getActivity ().runOnUiThread (r );
190
194
return true ;
191
195
} else if (action .equals ("showShareSheet" )) {
192
196
if (args .length () < 3 ) {
193
- callbackContext .error (String .format ("Parameter mismatched. 3 is required but %d is given" , args . length () ));
197
+ callbackContext .error (String .format ("Parameter count mismatch" ));
194
198
return false ;
195
199
}
196
200
cordova .getActivity ().runOnUiThread (r );
@@ -223,6 +227,13 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
223
227
224
228
branchObjectWrappers .set (args .getInt (0 ), branchObjWrapper );
225
229
230
+ } else if (action .equals ("getBranchQRCode" )) {
231
+ if (args .length () != 4 ) {
232
+ callbackContext .error (String .format ("Parameter count mismatch" ));
233
+ return false ;
234
+ }
235
+ cordova .getActivity ().runOnUiThread (r );
236
+ return true ;
226
237
}
227
238
228
239
return true ;
@@ -508,6 +519,70 @@ private void generateShortUrl(int instanceIdx, JSONObject options, JSONObject co
508
519
509
520
}
510
521
522
+ /**
523
+ * Generate a QR code.
524
+ *
525
+ * @param qrCodeSettings A {@link JSONObject} value to set QR cide options.
526
+ * @param instanceIdx The instance index from branchObjects array
527
+ * @param options A {@link JSONObject} value to set URL options.
528
+ * @param controlParams A {@link JSONObject} value to set the URL control parameters.
529
+ */
530
+ private void getBranchQRCode (JSONObject qrCodeSettings , int instanceIdx , JSONObject options , JSONObject controlParams , CallbackContext callbackContext ) throws JSONException {
531
+
532
+ BranchLinkProperties linkProperties = createLinkProperties (options , controlParams );
533
+
534
+ BranchUniversalObjectWrapper branchUniversalWrapper = (BranchUniversalObjectWrapper ) this .branchObjectWrappers .get (instanceIdx );
535
+ BranchUniversalObject buo = branchUniversalWrapper .branchUniversalObj ;
536
+
537
+ BranchQRCode branchQRCode = new BranchQRCode ();
538
+ if (qrCodeSettings .has ("codeColor" )) {
539
+ branchQRCode .setCodeColor (qrCodeSettings .getString ("codeColor" ));
540
+ }
541
+ if (qrCodeSettings .has ("backgroundColor" )) {
542
+ branchQRCode .setBackgroundColor (qrCodeSettings .getString ("backgroundColor" ));
543
+ }
544
+ if (qrCodeSettings .has ("centerLogo" )) {
545
+ branchQRCode .setCenterLogo (qrCodeSettings .getString ("centerLogo" ));
546
+ }
547
+ if (qrCodeSettings .has ("width" )) {
548
+ branchQRCode .setWidth (qrCodeSettings .getInt ("width" ));
549
+ }
550
+ if (qrCodeSettings .has ("margin" )) {
551
+ branchQRCode .setMargin (qrCodeSettings .getInt ("margin" ));
552
+ }
553
+ if (qrCodeSettings .has ("imageFormat" )) {
554
+ String imageFormat = qrCodeSettings .getString ("imageFormat" );
555
+ if (imageFormat != null ) {
556
+ if (imageFormat .equals ("JPEG" )) {
557
+ branchQRCode .setImageFormat (BranchQRCode .BranchImageFormat .JPEG );
558
+ } else {
559
+ branchQRCode .setImageFormat (BranchQRCode .BranchImageFormat .PNG );
560
+ }
561
+ }
562
+ }
563
+
564
+ try {
565
+ branchQRCode .getQRCodeAsData (this .activity , buo , linkProperties , new BranchQRCode .BranchQRCodeDataHandler () {
566
+ @ Override
567
+ public void onSuccess (byte [] qrCodeData ) {
568
+ String qrCodeString = Base64 .encodeToString (qrCodeData , Base64 .DEFAULT );
569
+ Log .d (LCAT , qrCodeString );
570
+ callbackContext .success (qrCodeString );
571
+ }
572
+
573
+ @ Override
574
+ public void onFailure (Exception e ) {
575
+ Log .d (LCAT , e .getMessage ());
576
+ callbackContext .error (e .getMessage ());
577
+ }
578
+ });
579
+ } catch (IOException e ) {
580
+ e .printStackTrace ();
581
+ Log .d (LCAT , e .getMessage ());
582
+ callbackContext .error (e .getMessage ());
583
+ }
584
+ }
585
+
511
586
/**
512
587
* <p>Sets the cookie based matching for all incoming requests.</p>
513
588
* <p>If you want cookie based matching, call this <b>before</b> initUserSession</p>
@@ -1017,7 +1092,7 @@ public void onLinkCreate(String url, BranchError error) {
1017
1092
}
1018
1093
1019
1094
}
1020
-
1095
+
1021
1096
protected class ShowShareSheetListener implements Branch .BranchLinkShareListener {
1022
1097
1023
1098
private CallbackContext _onShareLinkDialogLaunched ;
@@ -1197,6 +1272,8 @@ public void run() {
1197
1272
lastAttributedTouchData (this .callbackContext );
1198
1273
} else if (this .action .equals (("generateShortUrl" ))) {
1199
1274
generateShortUrl (this .args .getInt (0 ), this .args .getJSONObject (1 ), this .args .getJSONObject (2 ), this .callbackContext );
1275
+ } else if (this .action .equals (("getBranchQRCode" ))) {
1276
+ getBranchQRCode (this .args .getJSONObject (0 ), this .args .getInt (1 ), this .args .getJSONObject (2 ), this .args .getJSONObject (3 ), this .callbackContext );
1200
1277
} else if (this .action .equals ("registerView" )) {
1201
1278
registerView (this .args .getInt (0 ), this .callbackContext );
1202
1279
} else if (this .action .equals ("showShareSheet" )) {
0 commit comments