diff --git a/src/android/BranchSDK.java b/src/android/BranchSDK.java index 0c6956fe..61332c13 100644 --- a/src/android/BranchSDK.java +++ b/src/android/BranchSDK.java @@ -3,29 +3,37 @@ import android.app.Activity; import android.util.Log; -import io.branch.referral.Branch; -import io.branch.referral.BranchError; - -import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CallbackContext; -import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaInterface; - +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaWebView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import java.lang.Override; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Iterator; import java.util.Map; +import io.branch.indexing.BranchUniversalObject; +import io.branch.referral.Branch; +import io.branch.referral.BranchError; +import io.branch.referral.BranchShortLinkBuilder; +import io.branch.referral.Defines; +import io.branch.referral.SharingHelper; +import io.branch.referral.util.ShareSheetStyle; + public class BranchSDK extends CordovaPlugin { + static class BranchLinkProperties extends io.branch.referral.util.LinkProperties {} + // Standard Debugging Variables private static final String LCAT = "CordovaBranchSDK"; // Private Method Properties + private BranchUniversalObject branchObj; private CallbackContext callbackContext; private Activity activity; private Branch instance; @@ -92,6 +100,27 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo this.redeemRewards(args.getInt(0)); } return true; + } else if (action.equals("creditHistory")) { + this.creditHistory(); + return true; + } else if (action.equals("createBranchUniversalObject")) { + if (args.length() == 1) { + this.createBranchUniversalObject(args.getJSONObject(0)); + } + return true; + } else if (action.equals(("generateShortUrl"))) { + if (args.length() == 2) { + this.generateShortUrl(args.getJSONObject(0), args.getJSONObject(1)); + } + return true; + } else if (action.equals("registerView")) { + this.registerView(); + return true; + } else if (action.equals("showShareSheet")) { + if (args.length() == 2) { + this.showShareSheet(args.getJSONObject(0), args.getJSONObject(1)); + } + return true; } return false; @@ -172,20 +201,23 @@ private void loadRewards() * @return A {@link JSONObject} containing the latest referring parameters as * configured locally. */ - private void getLatestReferringParams() - { + private void getLatestReferringParams() throws JSONException { Log.d(LCAT, "start getLatestReferringParams()"); + JSONObject result = new JSONObject(); JSONObject sessionParams = this.instance.getLatestReferringParams(); if (sessionParams == null) { Log.d(LCAT, "return is null"); } else { + Log.d(LCAT, "return is not null"); Log.d(LCAT, sessionParams.toString()); } - this.callbackContext.success(sessionParams); + result.put("data", sessionParams); + + this.callbackContext.success(result); } @@ -196,20 +228,266 @@ private void getLatestReferringParams() * on a fresh install (not upgrade or reinstall). This will change on setIdentity (if the * user already exists from a previous device) and logout.

*/ - private void getFirstReferringParams() + private void getFirstReferringParams() throws JSONException { Log.d(LCAT, "start getFirstReferringParams()"); + JSONObject result = new JSONObject(); JSONObject installParams = this.instance.getFirstReferringParams(); if (installParams == null) { Log.d(LCAT, "return is null"); } else { + Log.d(LCAT, "return is not null"); Log.d(LCAT, installParams.toString()); } - this.callbackContext.success(installParams.toString()); + result.put("data", installParams); + + this.callbackContext.success(installParams); + + } + + /** + *

+ * Create a BranchUniversalObject with the given content. + *

+ * + * @param options A {@link JSONObject} value to set for the branch universal object properties. + * + * @return A {@link JSONObject} value of BranchUniversalObject instance. + */ + private void createBranchUniversalObject(JSONObject options) throws JSONException { + + Log.d(LCAT, "start createBranchUniversalObject()"); + + this.branchObj = new BranchUniversalObject(); + + // Set object properties + // Facebook Properties + if (options.has("canonicalIdentifier")) { + Log.d(LCAT, "set canonical identifier"); + this.branchObj.setCanonicalIdentifier(options.getString("canonicalIdentifier")); + } + if (options.has("title")) { + Log.d(LCAT, "set title"); + this.branchObj.setTitle(options.getString("title")); + } + if (options.has("contentDescription")) { + Log.d(LCAT, "set content description"); + this.branchObj.setContentDescription(options.getString("contentDescription")); + } + if (options.has("contentImageUrl")) { + Log.d(LCAT, "set content image url"); + this.branchObj.setContentImageUrl(options.getString("contentImageUrl")); + } + + // Set content visibility + if (options.has("contentIndexingMode")) { + Log.d(LCAT, "set content indexing mode"); + + if (options.getString("contentIndexingMode").equals("private")) { + Log.d(LCAT, "set private"); + this.branchObj.setContentIndexingMode(BranchUniversalObject.CONTENT_INDEX_MODE.PRIVATE); + } else { + Log.d(LCAT, "set public"); + this.branchObj.setContentIndexingMode(BranchUniversalObject.CONTENT_INDEX_MODE.PUBLIC); + } + } + + // Add custom keys/values to the deep link data + if (options.has("contentMetadata")) { + Log.d(LCAT, "add content meta data"); + + JSONObject contentMetaData = options.getJSONObject("contentMetadata"); + Iterator keys = contentMetaData.keys(); + + while (keys.hasNext()) { + String key = (String) keys.next(); + Log.d(LCAT, contentMetaData.getString(key)); + } + } + + this.callbackContext.success(this.branchObj.convertToJson()); + + } + + /** + * Display a popup of the share sheet. + * + * @param options A {@link JSONObject} value to set URL options. + * @param controlParams A {@link JSONObject} value to set the URL control parameters. + */ + private void showShareSheet(JSONObject options, JSONObject controlParams) throws JSONException + { + + Log.d(LCAT, "start showShareSheet()"); + + ShareSheetStyle shareSheetStyle = new ShareSheetStyle(this.activity, "Check this out!", "This stuff is awesome: ") + .setCopyUrlStyle(this.activity.getResources().getDrawable(android.R.drawable.ic_menu_send), "Copy", "Added to clipboard") + .setMoreOptionStyle(this.activity.getResources().getDrawable(android.R.drawable.ic_menu_search), "Show More") + .addPreferredSharingOption(SharingHelper.SHARE_WITH.FACEBOOK) + .addPreferredSharingOption(SharingHelper.SHARE_WITH.EMAIL); + + BranchLinkProperties linkProperties = createLinkProperties(options, controlParams); + + this.branchObj.showShareSheet(this.activity, linkProperties, shareSheetStyle, new ShowShareSheetListener()); + + } + + /** + * Generates a share link. + * + * @param options A {@link JSONObject} value to set URL options. + * @param controlParams A {@link JSONObject} value to set the URL control parameters. + * + * @return A {@link io.branch.referral.util.LinkProperties} value. + */ + private BranchLinkProperties createLinkProperties(JSONObject options, JSONObject controlParams) throws JSONException + { + + Log.d(LCAT, "start createLinkProperties()"); + + BranchLinkProperties linkProperties = new BranchLinkProperties(); + + if (options.has("feature")) { + linkProperties.setFeature(options.getString("feature")); + } else if (options.has("alias")) { + linkProperties.setFeature(options.getString("alias")); + } else if (options.has("channel")) { + linkProperties.setFeature(options.getString("channel")); + } else if (options.has("stage")) { + linkProperties.setFeature(options.getString("stage")); + } else if (options.has("duration")) { + linkProperties.setFeature(options.getString("duration")); + } + + if (options.has("tags")) { + ArrayList tags = (ArrayList) options.get("tags"); + + for (String tag : tags) { + linkProperties.addTag(tag); + } + } + + if (controlParams.has("$fallback_url")) { + Log.d(LCAT, "addControlParameter $fallback_url"); + linkProperties.addControlParameter("$fallback_url", controlParams.getString("$fallback_url")); + } + if (controlParams.has("$desktop_url")) { + Log.d(LCAT, "addControlParameter $desktop_url"); + linkProperties.addControlParameter("$desktop_url", controlParams.getString("$desktop_url")); + } + if (controlParams.has("$android_url")) { + Log.d(LCAT, "addControlParameter $android_url"); + linkProperties.addControlParameter("$android_url", controlParams.getString("$android_url")); + } + if (controlParams.has("$ios_url")) { + Log.d(LCAT, "addControlParameter $ios_url"); + linkProperties.addControlParameter("$ios_url", controlParams.getString("$ios_url")); + } + if (controlParams.has("$ipad_url")) { + Log.d(LCAT, "addControlParameter $ipad_url"); + linkProperties.addControlParameter("$ipad_url", controlParams.getString("$ipad_url")); + } + if (controlParams.has("$fire_url")) { + Log.d(LCAT, "addControlParameter $fire_url"); + linkProperties.addControlParameter("$fire_url", controlParams.getString("$fire_url")); + } + if (controlParams.has("$blackberry_url")) { + Log.d(LCAT, "addControlParameter $blackberry_url"); + linkProperties.addControlParameter("$blackberry_url", controlParams.getString("$blackberry_url")); + } + if (controlParams.has("$windows_phone_url")) { + Log.d(LCAT, "addControlParameter $windows_phone_url"); + linkProperties.addControlParameter("$windows_phone_url", controlParams.getString("$windows_phone_url")); + } + + return linkProperties; + + } + + /** + * Mark the content referred by this object as viewed. This increment the view count of the contents referred by this object. + */ + private void registerView() + { + + Log.d(LCAT, "start registerView()"); + + this.branchObj.registerView(); + this.callbackContext.success("Success"); + + } + + /** + * Generate a URL. + * + * @param options A {@link JSONObject} value to set URL options. + * @param controlParams A {@link JSONObject} value to set the URL control parameters. + */ + private void generateShortUrl(JSONObject options, JSONObject controlParams) throws JSONException + { + + Log.d(LCAT, "start generateShortUrl()"); + + BranchLinkProperties linkProperties = new BranchLinkProperties(); + + if (options.has("feature")) { + linkProperties.setFeature(options.getString("feature")); + } else if (options.has("alias")) { + linkProperties.setAlias(options.getString("alias")); + } else if (options.has("channel")) { + linkProperties.setChannel(options.getString("channel")); + } else if (options.has("stage")) { + linkProperties.setStage(options.getString("stage")); + } else if (options.has("duration")) { + linkProperties.setDuration(options.getInt("duration")); + } + + if (options.has("tags")) { + ArrayList tags = (ArrayList) options.get("tags"); + + for (String tag : tags) { + linkProperties.addTag(tag); + } + } + + if (controlParams.has("$fallback_url")) { + Log.d(LCAT, "addControlParameter $fallback_url"); + linkProperties.addControlParameter("$fallback_url", controlParams.getString("$fallback_url")); + } + if (controlParams.has("$desktop_url")) { + Log.d(LCAT, "addControlParameter $desktop_url"); + linkProperties.addControlParameter("$desktop_url", controlParams.getString("$desktop_url")); + } + if (controlParams.has("$android_url")) { + Log.d(LCAT, "addControlParameter $android_url"); + linkProperties.addControlParameter("$android_url", controlParams.getString("$android_url")); + } + if (controlParams.has("$ios_url")) { + Log.d(LCAT, "addControlParameter $ios_url"); + linkProperties.addControlParameter("$ios_url", controlParams.getString("$ios_url")); + } + if (controlParams.has("$ipad_url")) { + Log.d(LCAT, "addControlParameter $ipad_url"); + linkProperties.addControlParameter("$ipad_url", controlParams.getString("$ipad_url")); + } + if (controlParams.has("$fire_url")) { + Log.d(LCAT, "addControlParameter $fire_url"); + linkProperties.addControlParameter("$fire_url", controlParams.getString("$fire_url")); + } + if (controlParams.has("$blackberry_url")) { + Log.d(LCAT, "addControlParameter $blackberry_url"); + linkProperties.addControlParameter("$blackberry_url", controlParams.getString("$blackberry_url")); + } + if (controlParams.has("$windows_phone_url")) { + Log.d(LCAT, "addControlParameter $windows_phone_url"); + linkProperties.addControlParameter("$windows_phone_url", controlParams.getString("$windows_phone_url")); + } + + this.branchObj.generateShortUrl(this.activity, linkProperties, new GenerateShortUrlListener()); } @@ -283,54 +561,15 @@ private void userCompletedAction(String action, JSONObject metaData) } /** - *

- * Creates a dictionary for session returns. - *

+ *

Gets the credit history of the specified bucket and triggers a callback to handle the + * response.

*/ - private Map createSessionDict(JSONObject data) + private void creditHistory() { - Log.d(LCAT, "start createSessionDict()"); - Map sessionDict = new HashMap(); + Log.d(LCAT, "start creditHistory()"); - if (data.has("~channel")) { - sessionDict.put("~channel", data.optString("~channel")); - } - if (data.has("~feature")) { - sessionDict.put("~feature", data.optString("~feature")); - } - if (data.has("~tags")) { - sessionDict.put("~tags", data.optString("~tags")); - } - if (data.has("~campaign")) { - sessionDict.put("~campaign", data.optString("~campaign")); - } - if (data.has("~stage")) { - sessionDict.put("~stage", data.optString("~stage")); - } - if (data.has("~creation_source")) { - sessionDict.put("~creation_source", data.optString("~creation_source")); - } - if (data.has("+match_guaranteed")) { - sessionDict.put("+match_guaranteed", data.optString("+match_guaranteed")); - } - if (data.has("+referrer")) { - sessionDict.put("+referrer", data.optString("+referrer")); - } - if (data.has("+phone_number")) { - sessionDict.put("+phone_number", data.optString("+phone_number")); - } - if (data.has("+is_first_session")) { - sessionDict.put("+is_first_session", data.optString("+is_first_session")); - } - if (data.has("+clicked_branch_link")) { - sessionDict.put("+clicked_branch_link", data.optString("+clicked_branch_link")); - } - if (data.has("+click_timestamp")) { - sessionDict.put("+click_timestamp", data.optString("+click_timestamp")); - } - - return sessionDict; + this.instance.getCreditHistory(new CreditHistoryListener()); } @@ -341,14 +580,14 @@ private Map createSessionDict(JSONObject data) protected class SessionListener implements Branch.BranchReferralInitListener { - /** - * Listener that implements BranchReferralInitListener for initSession - */ + //Listener that implements BranchReferralInitListener for initSession @Override public void onInitFinished(JSONObject referringParams, BranchError error) { Log.d(LCAT, "SessionListener onInitFinished()"); + JSONObject result = new JSONObject(); + if (error == null) { // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app @@ -361,14 +600,28 @@ public void onInitFinished(JSONObject referringParams, BranchError error) { Log.d(LCAT, referringParams.toString()); } - callbackContext.success("initialize success"); + try { + result.put("data", referringParams); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + + callbackContext.success(result); } else { String errorMessage = error.getMessage(); + try { + result.put("error", errorMessage); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + Log.d(LCAT, errorMessage); - callbackContext.error(errorMessage); + callbackContext.error(result); } } @@ -387,6 +640,7 @@ public void onStateChanged(boolean changed, BranchError error) { JSONObject result = new JSONObject(); if (error == null) { + int credits = instance.getCredits(); try { @@ -396,7 +650,9 @@ public void onStateChanged(boolean changed, BranchError error) { } callbackContext.success(result); + } else { + String errorMessage = error.getMessage(); Log.d(LCAT, errorMessage); @@ -405,11 +661,126 @@ public void onStateChanged(boolean changed, BranchError error) { result.put("error", errorMessage); } catch (JSONException e) { e.printStackTrace(); + return; } callbackContext.error(result); + + } + + } + } + + protected class GenerateShortUrlListener implements Branch.BranchLinkCreateListener + { + @Override + public void onLinkCreate(String url, BranchError error) { + + Log.d(LCAT, "inside onLinkCreate"); + + JSONObject response = new JSONObject(); + + if (error == null) { + + Log.d(LCAT, "link to share: " + url); + + try { + response.put("generatedLink", url); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + + callbackContext.success(response); + } else { + + String errorMessage = error.getMessage(); + + Log.d(LCAT, errorMessage); + + try { + response.put("error", errorMessage); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + + callbackContext.error(response); + } + + } + + } + + protected class ShowShareSheetListener implements Branch.BranchLinkShareListener + { + @Override + public void onShareLinkDialogLaunched() { + Log.d(LCAT, "inside onShareLinkDialogLaunched"); + + callbackContext.success("Success"); + } + + @Override + public void onShareLinkDialogDismissed() { + Log.d(LCAT, "inside onShareLinkDialogDismissed"); + + callbackContext.success("Success"); + } + + @Override + public void onLinkShareResponse(String sharedLink, String sharedChannel, BranchError error) { + + Log.d(LCAT, "inside onLinkCreate"); + + JSONObject response = new JSONObject(); + + if (error == null) { + + try { + response.put("sharedLink", sharedLink); + response.put("sharedChannel", sharedChannel); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + + Log.d(LCAT, "sharedLink: " + sharedLink); + Log.d(LCAT, "sharedChannel: " + sharedChannel); + } else { + String errorMessage = error.getMessage(); + + Log.d(LCAT, errorMessage); + + try { + response.put("error", errorMessage); + } catch (JSONException e) { + e.printStackTrace(); + return; + } } + callbackContext.success(response); + + } + + @Override + public void onChannelSelected(String channelName) { + + Log.d(LCAT, "inside onChannelSelected"); + Log.d(LCAT, "channelName: " + channelName); + + JSONObject response = new JSONObject(); + + try { + response.put("channelName", channelName); + } catch (JSONException e) { + e.printStackTrace(); + return; + } + + callbackContext.success(response); + } } @@ -418,24 +789,46 @@ protected class CreditHistoryListener implements Branch.BranchListResponseListen // Listener that implements BranchListResponseListener for getCreditHistory() @Override public void onReceivingResponse(JSONArray list, BranchError error) { + Log.d(LCAT, "inside onReceivingResponse"); JSONObject response = new JSONObject(); if (error == null) { - ArrayList data = new ArrayList(); - /*if (list != null) { + JSONArray data = new JSONArray(); + + if (list != null) { + for (int i = 0, limit = list.length(); i < limit; ++i) { + + JSONObject entry; + try { - JSONObject entry = list.getJSONObject(i); - Map dict = parse + entry = list.getJSONObject(i); + data.put(entry); + } catch (JSONException e) { + e.printStackTrace(); + return; } + } - }*/ + + } + + try { + response.put("data", data); + } catch (JSONException e) { + e.printStackTrace(); + return; + } callbackContext.success(response); + } else { + + response = new JSONObject(); + String errorMessage = error.getMessage(); Log.d(LCAT, errorMessage); @@ -444,9 +837,11 @@ public void onReceivingResponse(JSONArray list, BranchError error) { response.put("error", errorMessage); } catch (JSONException e) { e.printStackTrace(); + return; } callbackContext.error(response); + } } }