66import android .net .Uri ;
77import android .os .Build ;
88import android .util .Log ;
9+ import android .util .Base64 ;
910
1011import org .apache .cordova .CallbackContext ;
1112import org .apache .cordova .CordovaPlugin ;
1617
1718import java .util .ArrayList ;
1819import java .util .Iterator ;
20+ import java .io .IOException ;
1921
2022import io .branch .indexing .BranchUniversalObject ;
2123import io .branch .referral .Branch ;
2426import io .branch .referral .ServerRequestGetCPID .BranchCrossPlatformIdListener ;
2527import io .branch .referral .ServerRequestGetLATD .BranchLastAttributedTouchDataListener ;
2628import io .branch .referral .SharingHelper ;
29+ import io .branch .referral .QRCode .BranchQRCode ;
2730import io .branch .referral .util .BRANCH_STANDARD_EVENT ;
2831import io .branch .referral .util .BranchCPID ;
2932import io .branch .referral .util .BranchEvent ;
3033import io .branch .referral .util .ContentMetadata ;
3134import io .branch .referral .util .CurrencyType ;
3235import io .branch .referral .util .ShareSheetStyle ;
3336
37+
3438public class BranchSDK extends CordovaPlugin {
3539
3640 static class BranchLinkProperties extends io .branch .referral .util .LinkProperties {
@@ -137,14 +141,14 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
137141 return true ;
138142 } else if (action .equals ("userCompletedAction" )) {
139143 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" ));
141145 return false ;
142146 }
143147 cordova .getActivity ().runOnUiThread (r );
144148 return true ;
145149 } else if (action .equals ("sendBranchEvent" )) {
146150 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" ));
148152 return false ;
149153 }
150154 cordova .getActivity ().runOnUiThread (r );
@@ -160,7 +164,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
160164 return true ;
161165 } else if (action .equals ("createBranchUniversalObject" )) {
162166 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" ));
164168 return false ;
165169 }
166170 cordova .getActivity ().runOnUiThread (r );
@@ -176,21 +180,21 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
176180
177181 } else if (action .equals (("generateShortUrl" ))) {
178182 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" ));
180184 return false ;
181185 }
182186 cordova .getActivity ().runOnUiThread (r );
183187 return true ;
184188 } else if (action .equals ("registerView" )) {
185189 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" ));
187191 return false ;
188192 }
189193 cordova .getActivity ().runOnUiThread (r );
190194 return true ;
191195 } else if (action .equals ("showShareSheet" )) {
192196 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" ));
194198 return false ;
195199 }
196200 cordova .getActivity ().runOnUiThread (r );
@@ -223,6 +227,13 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
223227
224228 branchObjectWrappers .set (args .getInt (0 ), branchObjWrapper );
225229
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 ;
226237 }
227238
228239 return true ;
@@ -508,6 +519,70 @@ private void generateShortUrl(int instanceIdx, JSONObject options, JSONObject co
508519
509520 }
510521
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+
511586 /**
512587 * <p>Sets the cookie based matching for all incoming requests.</p>
513588 * <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) {
10171092 }
10181093
10191094 }
1020-
1095+
10211096 protected class ShowShareSheetListener implements Branch .BranchLinkShareListener {
10221097
10231098 private CallbackContext _onShareLinkDialogLaunched ;
@@ -1197,6 +1272,8 @@ public void run() {
11971272 lastAttributedTouchData (this .callbackContext );
11981273 } else if (this .action .equals (("generateShortUrl" ))) {
11991274 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 );
12001277 } else if (this .action .equals ("registerView" )) {
12011278 registerView (this .args .getInt (0 ), this .callbackContext );
12021279 } else if (this .action .equals ("showShareSheet" )) {
0 commit comments