Skip to content

Commit

Permalink
Merge pull request #210 from BranchMetrics/SDK-1409-qr-code-creation
Browse files Browse the repository at this point in the history
SDK 1409 [Unity] Expose QR code creation methods
  • Loading branch information
rob-gioia-branch authored Jan 23, 2024
2 parents 1487ac4 + a0bdc73 commit a0af262
Show file tree
Hide file tree
Showing 52 changed files with 3,052 additions and 2,665 deletions.
1,718 changes: 883 additions & 835 deletions BranchUnityTestBed/Assembly-CSharp-Editor.csproj

Large diffs are not rendered by default.

1,604 changes: 812 additions & 792 deletions BranchUnityTestBed/Assembly-CSharp.csproj

Large diffs are not rendered by default.

63 changes: 52 additions & 11 deletions BranchUnityTestBed/Assets/Branch/Branch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ public class Branch : MonoBehaviour {
public delegate void BranchCallbackWithStatus(bool changed, string error);
public delegate void BranchCallbackWithList(List<object> list, string error);
public delegate void BranchCallbackWithBranchUniversalObject(BranchUniversalObject universalObject, BranchLinkProperties linkProperties, string error);
public delegate void BranchCallbackWithData(string data, string error);

#region Public methods
#region Public methods

#region InitSession methods
#region InitSession methods


/**
* Initialize session and receive information about how it opened.
*/
public static void initSession(BranchCallbackWithParams callback) {
public static void initSession(BranchCallbackWithParams callback) {
if (_sessionCounter == 0) {
++_sessionCounter;
_isFirstSessionInited = true;
Expand Down Expand Up @@ -260,20 +261,35 @@ public static void shareLink(BranchUniversalObject universalObject, BranchLinkPr
* Get a short url given a BranchUniversalObject, BranchLinkProperties
*/
public static void getShortURL(BranchUniversalObject universalObject, BranchLinkProperties linkProperties, BranchCallbackWithUrl callback) {

var callbackId = _getNextCallbackId();

_branchCallbacks[callbackId] = callback;

_getShortURLWithBranchUniversalObjectAndCallback(universalObject.ToJsonString(), linkProperties.ToJsonString(), callbackId);
}

#endregion
#endregion

#endregion
#region QR Code methods

/**
* Generate a QR Code
*/
public static void generateQRCode(BranchUniversalObject universalObject, BranchLinkProperties linkProperties, BranchQRCode branchQRCode, BranchCallbackWithData callback)
{
var callbackId = _getNextCallbackId();
_branchCallbacks[callbackId] = callback;
_generateBranchQRCode(universalObject.ToJsonString(), linkProperties.ToJsonString(), branchQRCode.ToJsonString(), callbackId);
}

#endregion

#endregion

#region Singleton

public void Awake() {
public void Awake() {

// make sure there's only a single Branch instance
var olderBranches = FindObjectsOfType<Branch>();
Expand Down Expand Up @@ -313,11 +329,11 @@ void OnApplicationPause(bool pauseStatus) {
}
}

#endregion
#endregion

#region Private methods
#region Private methods

#region Platform Loading Methods
#region Platform Loading Methods

#if (UNITY_IOS || UNITY_IPHONE) && !UNITY_EDITOR

Expand Down Expand Up @@ -392,6 +408,9 @@ void OnApplicationPause(bool pauseStatus) {

[DllImport ("__Internal")]
private static extern void _shareLinkWithLinkProperties(string universalObject, string linkProperties, string message, string callbackId);

[DllImport ("__Internal")]
private static extern void _generateBranchQRCode(string universalObject, string linkProperties, string branchQRCode, string callbackId);

#elif UNITY_ANDROID && !UNITY_EDITOR

Expand Down Expand Up @@ -491,9 +510,13 @@ private static void _getShortURLWithBranchUniversalObjectAndCallback(string univ
BranchAndroidWrapper.getShortURLWithBranchUniversalObjectAndCallback(universalObject, linkProperties, callbackId);
}

private static void _generateBranchQRCode(string universalObject, string linkProperties, string branchQRCode, string callbackId) {
BranchAndroidWrapper.generateBranchQRCode(universalObject, linkProperties, branchQRCode, callbackId);
}

#else

private static void _setBranchKey(string branchKey, string sdkVersion) { }
private static void _setBranchKey(string branchKey, string sdkVersion) { }

private static void _initSessionWithCallback(string callbackId) {
callNotImplementedCallbackForParamCallback(callbackId);
Expand Down Expand Up @@ -558,7 +581,11 @@ private static void _shareLinkWithLinkProperties(string universalObject, string
private static void _getShortURLWithBranchUniversalObjectAndCallback(string universalObject, string linkProperties, string callbackId) {
callNotImplementedCallbackForUrlCallback(callbackId);
}


private static void _generateBranchQRCode(string universalObject, string linkProperties, string branchQRCode, string callbackId)
{
callNotImplementedCallbackForUrlCallback(callbackId);
}

private static void callNotImplementedCallbackForParamCallback(string callbackId) {
var callback = _branchCallbacks[callbackId] as BranchCallbackWithParams;
Expand Down Expand Up @@ -639,6 +666,20 @@ public void _asyncCallbackWithUrl(string callbackDictString) {
}
}

public void _asyncCallbackWithData(string callbackDictString)
{
var callbackDict = BranchThirdParty_MiniJSON.Json.Deserialize(callbackDictString) as Dictionary<string, object>;
var callbackId = callbackDict["callbackId"] as string;
string data = callbackDict.ContainsKey("data") ? callbackDict["data"] as string : null;
string error = callbackDict.ContainsKey("error") ? callbackDict["error"] as string : null;

var callback = _branchCallbacks[callbackId] as BranchCallbackWithData;
if (callback != null)
{
callback(data, error);
}
}

public void _asyncCallbackWithBranchUniversalObject(string callbackDictString) {

Debug.Log ("callbackDictString: \n\n" + callbackDictString + "\n\n");
Expand Down
15 changes: 13 additions & 2 deletions BranchUnityTestBed/Assets/Branch/BranchAndroidWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,20 @@ public static void getShortURLWithBranchUniversalObjectAndCallback(string univer
}

#endregion


#region QR Code Generation methods

public static void generateBranchQRCode(string universalObject, string linkProperties, string branchQRCode, string callbackId)
{
_runBlockOnThread(() => {
_getBranchClass().CallStatic("generateBranchQRCode", universalObject, linkProperties, branchQRCode, callbackId);
});
}

#endregion

#region Utility methods

private static AndroidJavaClass _getBranchClass() {
if (_branchClass == null) {
_branchClass = new AndroidJavaClass("io/branch/unity/BranchUnityWrapper");
Expand Down
24 changes: 24 additions & 0 deletions BranchUnityTestBed/Assets/Branch/BranchEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,28 @@ public static BranchProductCategory parse(this string category)

return BranchProductCategory.NONE;
}
}

public enum BranchQRCodePattern
{
Default, //1
Squares, //2
Circles, //3
Triangles, //4
Pentagons, //5
Hexagons, //6
Octagons //7
}

public enum BranchQRCodeFinderPattern
{
Square, //1
RoundedSquare, //2
Circle, //3
}

public enum BranchImageFormat
{
JPEG, //QR code is returned as a JPEG
PNG //QR code is returned as a PNG
}
113 changes: 113 additions & 0 deletions BranchUnityTestBed/Assets/Branch/BranchQRCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;

public class BranchQRCode
{
//Primary color of the generated QR code itself
private string codeColor;

//Secondary color used as the QR Code background
private string backgroundColor;

//The number of pixels for the QR code's border. Min 1px. Max 20px
private int margin;

//Output size of QR Code image. Min 300px. Max 2000px
private int width;

//Image Format of the returned QR code. Can be a JPEG or PNG
private BranchImageFormat imageFormat;

//A URL of an image that will be added to the center of the QR code. Must be a PNG or JPEG
private string centerLogoUrl;

//The style of code pattern used to generate the QR-Code
private BranchQRCodePattern codePattern;

// The style of finder pattern used to generate the QR-Code
private BranchQRCodeFinderPattern finderPattern;

// Hex string used to change the color of the Qr-Code’s finder pattern
private string finderPatternColor;

// Direct link to an image to be used as the background image that is set behind the QR Code
private string backgroundImageUrl;

// Adjusts the opacity of the background image
private int backgroundImageOpacity;

// Direct link to an image to be used as the code-pattern itself on the QR Code
private string codePatternUrl;

// Hex string used to change the color of the interior part of a Qr-Code’s finder pattern
private string finderEyeColor;

public BranchQRCode(string codeColor = "#000000", string backgroundColor = "#FFFFFF", int margin = 1, int width = 512, BranchImageFormat imageFormat = BranchImageFormat.PNG, string centerLogoUrl = "")
{
Init(codeColor, backgroundColor, margin, width, imageFormat, centerLogoUrl);
}

private void Init(string codeColor, string backgroundColor, int margin, int width, BranchImageFormat imageFormat, string centerLogoUrl)
{
this.codeColor = codeColor;
this.backgroundColor = backgroundColor;
this.margin = margin;
this.width = width;
this.imageFormat = imageFormat;
this.centerLogoUrl = centerLogoUrl;
}

public void loadFromJson(string json)
{
if (string.IsNullOrEmpty(json))
return;

var data = BranchThirdParty_MiniJSON.Json.Deserialize(json) as Dictionary<string, object>;
loadFromDictionary(data);
}

public void loadFromDictionary(Dictionary<string, object> data)
{
if (data == null)
return;

if (data.ContainsKey("code_color") && data["code_color"] != null)
{
codeColor = data["code_color"].ToString();
}
if (data.ContainsKey("background_color") && data["background_color"] != null)
{
backgroundColor = data["background_color"].ToString();
}
if (data.ContainsKey("margin"))
{
margin = Convert.ToInt32(data["margin"].ToString());
}
if (data.ContainsKey("width"))
{
width = Convert.ToInt32(data["width"].ToString());
}
if (data.ContainsKey("image_format"))
{
imageFormat = (BranchImageFormat) Convert.ToInt32(data["imageFormat"].ToString());
}
if (data.ContainsKey("center_logo_url") && data["center_logo_url"] != null)
{
centerLogoUrl = data["center_logo_url"].ToString();
}
}

public string ToJsonString()
{
var data = new Dictionary<string, object>();

data.Add("code_color", codeColor);
data.Add("background_color", backgroundColor);
data.Add("margin", margin.ToString());
data.Add("width", width.ToString());
data.Add("image_format", imageFormat.ToString());
data.Add("center_logo_url", centerLogoUrl);

return BranchThirdParty_MiniJSON.Json.Serialize(data);
}
}
11 changes: 11 additions & 0 deletions BranchUnityTestBed/Assets/Branch/BranchQRCode.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
33 changes: 33 additions & 0 deletions BranchUnityTestBed/Assets/Branch/Demo/Scripts/BranchDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public class BranchDemo : MonoBehaviour {
public GameObject rewardsHistoryPanel;
public GameObject logPanel;
public GameObject quitButton;
public RawImage qrCodeRawImage;

private BranchUniversalObject universalObject = null;
private BranchLinkProperties linkProperties = null;
private BranchQRCode qrCode = null;
private string logString = "";

#region Init
Expand Down Expand Up @@ -138,6 +140,37 @@ public void OnBtn_CreateBranchLink() {
}
}

public void OnBtn_GenerateQRCode()
{
try
{
universalObject = new BranchUniversalObject();
universalObject.canonicalIdentifier = "qrcodeid12345";
linkProperties = new BranchLinkProperties();
qrCode = new BranchQRCode("#FF0000", "#00FF00", 2, 1024, BranchImageFormat.JPEG, "https://play-lh.googleusercontent.com/gJ22vsKfh-dU592AI9GzI4OX9dkyzYPlsGSyr019dQv6cyAvfuRkUtzl9KJADGdTIlQ");
Branch.generateQRCode(universalObject, linkProperties, qrCode, (data, error) =>
{
if (error != null)
{
Debug.LogError("Branch.generateQRCode failed: " + error);
}
else
{
Debug.Log("QR Code Successfully Generated!");
byte[] decodedBytes = Convert.FromBase64String(data);
Texture2D texture2D = new Texture2D(1, 1);
texture2D.LoadImage(decodedBytes);
texture2D.Apply();
qrCodeRawImage.texture = texture2D;
}
});
}
catch (Exception e)
{
Debug.Log("QR Code Generating Error" + e);
}
}

public void OnBtn_SendBuyEvent() {
BranchEvent e = new BranchEvent (BranchEventType.PURCHASE);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<dependencies>
<androidPackages>
<androidPackage spec="io.branch.sdk.android:library:5.3.0" />
<androidPackage spec="io.branch.sdk.android:library:5.8.0" />
<androidPackage spec="com.google.android.gms:play-services-ads-identifier:18+" />
</androidPackages>
<iosPods>
<iosPod name="BranchSDK" version="~>2.1.0" bitcodeEnabled="false" minTargetSdk="11.0" addToAllTargets="false" />
<iosPod name="BranchSDK" version="~>3.0.0" bitcodeEnabled="false" minTargetSdk="14.0" addToAllTargets="false" />
</iosPods>
</dependencies>
Binary file modified BranchUnityTestBed/Assets/Branch/Resources/BranchData.asset
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Loading

0 comments on commit a0af262

Please sign in to comment.