Skip to content

Commit

Permalink
Merge branch 'master' into fix/issue-89-non-branch-links-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aaustin committed Apr 16, 2016
2 parents ead6abf + 6702d55 commit 4e0edf4
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 51 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ The Branch iOS SDK includes a wrapper on the UIActivityViewController, that will
| $blackberry_url | `string` | The URL for Blackberry
| $windows_phone_url | `string` | The URL for Windows phone

**shareText**: `string` - Custom share text

##### Usage
```js
branchUniversalObj.showShareSheet({
Expand All @@ -471,6 +473,8 @@ branchUniversalObj.showShareSheet({
}, {
// put your control parameters here
"$desktop_url" : "http://desktop-url.com",
}, {
'Custom share text: '
});
```

Expand Down
5 changes: 5 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ SOFTWARE.
<keywords>cordova,branch</keywords>
<repo>https://github.com/BranchMetrics/Cordova-Ionic-PhoneGap-Deferred-Deep-Linking-SDK.git</repo>
<issue>https://github.com/BranchMetrics/Cordova-Ionic-PhoneGap-Deferred-Deep-Linking-SDK/issues</issue>

<dependency
id="es6-promise-plugin"
url="https://github.com/vstirbu/PromisesPlugin.git">
</dependency>

<js-module src="www/branch.js" name="Branch">
<clobbers target="Branch" />
Expand Down
150 changes: 102 additions & 48 deletions src/android/io/branch/BranchSDK.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,102 +100,83 @@ public void onStop()
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException
{

Runnable r = new RunnableThread(action, args, callbackContext);

if (action.equals("setDebug")) {
if (args.length() == 1) {
this.setDebug(args.getBoolean(0), callbackContext);
cordova.getThreadPool().execute(r);
}
return true;
} else if (action.equals("initSession")) {
this.initSession(callbackContext);
cordova.getThreadPool().execute(r);
return true;
} else {
if (this.instance != null) {
if (action.equals("setIdentity")) {
this.setIdentity(args.getString(0), callbackContext);

cordova.getThreadPool().execute(r);
return true;
} else if (action.equals("userCompletedAction")) {
if (args.length() < 1 && args.length() > 2) {
callbackContext.error(String.format("Parameter mismatched. 1-2 is required but %d is given", args.length()));
return false;
}
if (args.length() == 2) {
this.userCompletedAction(args.getString(0), args.getJSONObject(1), callbackContext);
} else if (args.length() == 1) {
this.userCompletedAction(args.getString(0), callbackContext);
}

cordova.getThreadPool().execute(r);
return true;
} else if (action.equals("getFirstReferringParams")) {
this.getFirstReferringParams(callbackContext);
cordova.getThreadPool().execute(r);
return true;
} else if (action.equals("getLatestReferringParams")) {
this.getLatestReferringParams(callbackContext);
cordova.getThreadPool().execute(r);
return true;
} else if (action.equals("logout")) {
this.logout(callbackContext);
cordova.getThreadPool().execute(r);
return true;
} else if (action.equals("loadRewards")) {
this.loadRewards(callbackContext);
cordova.getThreadPool().execute(r);
return true;
} else if (action.equals("redeemRewards")) {
if (args.length() < 1 && args.length() > 2) {
callbackContext.error(String.format("Parameter mismatched. 1-2 is required but %d is given", args.length()));

return false;
}
if (args.length() == 1) {
this.redeemRewards(args.getInt(0), callbackContext);
} else if (args.length() == 2) {
this.redeemRewards(args.getInt(0), args.getString(1), callbackContext);
}

cordova.getThreadPool().execute(r);
return true;
} else if (action.equals("getCreditHistory")) {
this.getCreditHistory(callbackContext);

cordova.getThreadPool().execute(r);
return true;
} else if (action.equals("createBranchUniversalObject")) {
if (args.length() == 1) {
this.createBranchUniversalObject(args.getJSONObject(0), callbackContext);

return true;
} else {
if (args.length() != 1) {
callbackContext.error(String.format("Parameter mismatched. 1 is required but %d is given", args.length()));

return false;
}
cordova.getThreadPool().execute(r);
return true;
} else if (action.equals(("generateShortUrl"))) {
if (args.length() == 3) {
this.generateShortUrl(args.getInt(0), args.getJSONObject(1), args.getJSONObject(2), callbackContext);

return true;
} else {
if (args.length() != 3) {
callbackContext.error(String.format("Parameter mismatched. 3 is required but %d is given", args.length()));

return false;
}
cordova.getThreadPool().execute(r);
return true;
} else if (action.equals("registerView")) {
if (args.length() == 1) {
this.registerView(args.getInt(0), callbackContext);

return true;
} else {
if (args.length() != 1) {
callbackContext.error(String.format("Parameter mismatched. 1 is required but %d is given", args.length()));

return false;
}
cordova.getThreadPool().execute(r);
return true;
} else if (action.equals("showShareSheet")) {
if (args.length() == 3) {
this.showShareSheet(args.getInt(0), args.getJSONObject(1), args.getJSONObject(2));
if (args.length() == 4) {
this.showShareSheet(args.getInt(0), args.getJSONObject(1), args.getJSONObject(2), args.getString(3));

return true;
} else {
callbackContext.error(String.format("Parameter mismatched. 3 is required but %d is given", args.length()));

return false;
}
cordova.getActivity().runOnUiThread(r);
return true;
} else if (action.equals("onShareLinkDialogLaunched")) {

BranchUniversalObjectWrapper branchObjWrapper = (BranchUniversalObjectWrapper)branchObjectWrappers.get(args.getInt(0));
Expand Down Expand Up @@ -254,7 +235,13 @@ private void initSession(CallbackContext callbackContext)
this.activity = this.cordova.getActivity();

this.instance = Branch.getAutoInstance(this.activity.getApplicationContext());
this.instance.initSession(new SessionListener(callbackContext), activity.getIntent().getData(), activity);

if (callbackContext == null) {
this.instance.initSession(new SessionListener(), activity.getIntent().getData(), activity);
} else {
this.instance.initSession(new SessionListener(callbackContext), activity.getIntent().getData(), activity);
}


}

Expand Down Expand Up @@ -466,12 +453,12 @@ private void createBranchUniversalObject(JSONObject options, CallbackContext cal
* @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(int instanceIdx, JSONObject options, JSONObject controlParams) throws JSONException
private void showShareSheet(int instanceIdx, JSONObject options, JSONObject controlParams, String shareText) throws JSONException
{

Log.d(LCAT, "start showShareSheet()");

ShareSheetStyle shareSheetStyle = new ShareSheetStyle(this.activity, "Check this out!", "This stuff is awesome: ")
ShareSheetStyle shareSheetStyle = new ShareSheetStyle(this.activity, "Check this out!", shareText)
.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)
Expand Down Expand Up @@ -776,11 +763,14 @@ protected class SessionListener implements Branch.BranchReferralInitListener
{
private CallbackContext _callbackContext;

// Constructor that takes in a required callbackContext object
public SessionListener(CallbackContext callbackContext) {
this._callbackContext = callbackContext;
}

public SessionListener() {
this._callbackContext = null;
}

//Listener that implements BranchReferralInitListener for initSession
@Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
Expand All @@ -791,6 +781,10 @@ public void onInitFinished(JSONObject referringParams, BranchError error) {

webView.sendJavascript(out);

if (this._callbackContext == null) {
return;
}

if (error == null) {

// params are the deep linked params associated with the link that the user clicked -> was re-directed to this app
Expand Down Expand Up @@ -1188,4 +1182,64 @@ public void onReceivingResponse(JSONArray list, BranchError error) {
}
}

protected class RunnableThread implements Runnable {

private String action;
private JSONArray args;
private CallbackContext callbackContext;

public RunnableThread(String action, JSONArray args, CallbackContext callbackContext) {
this.callbackContext = callbackContext;
this.action = action;
this.args = args;
}

public void run() {
try {
if (this.action.equals("setDebug")) {
setDebug(this.args.getBoolean(0), this.callbackContext);
} else if (this.action.equals("initSession")) {
initSession(this.callbackContext);
} else {
if (this.action.equals("setIdentity")) {
setIdentity(this.args.getString(0), this.callbackContext);
} else if (this.action.equals("userCompletedAction")) {
if (this.args.length() == 2) {
userCompletedAction(this.args.getString(0), this.args.getJSONObject(1), this.callbackContext);
} else if (this.args.length() == 1) {
userCompletedAction(this.args.getString(0), this.callbackContext);
}
} else if (this.action.equals("getFirstReferringParams")) {
getFirstReferringParams(this.callbackContext);
} else if (this.action.equals("getLatestReferringParams")) {
getLatestReferringParams(this.callbackContext);
} else if (this.action.equals("logout")) {
logout(this.callbackContext);
} else if (this.action.equals("loadRewards")) {
loadRewards(this.callbackContext);
} else if (this.action.equals("redeemRewards")) {
if (this.args.length() == 1) {
redeemRewards(this.args.getInt(0), this.callbackContext);
} else if (this.args.length() == 2) {
redeemRewards(this.args.getInt(0), this.args.getString(1), this.callbackContext);
}
} else if (this.action.equals("getCreditHistory")) {
getCreditHistory(this.callbackContext);
} else if (this.action.equals("createBranchUniversalObject")) {
createBranchUniversalObject(this.args.getJSONObject(0), this.callbackContext);
} else if (this.action.equals(("generateShortUrl"))) {
generateShortUrl(this.args.getInt(0), this.args.getJSONObject(1), this.args.getJSONObject(2), this.callbackContext);
} else if (this.action.equals("registerView")) {
registerView(this.args.getInt(0), this.callbackContext);
} else if (this.action.equals("showShareSheet")) {
showShareSheet(this.args.getInt(0), this.args.getJSONObject(1), this.args.getJSONObject(2));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}

}

}
2 changes: 1 addition & 1 deletion testbed/www/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function ShowShareSheet()
console.log('Channel selected: ' + JSON.stringify(res));
});

branchUniversalObj.showShareSheet(properties, controlParams);
branchUniversalObj.showShareSheet(properties, controlParams, 'Custom Text');

}

Expand Down
11 changes: 9 additions & 2 deletions www/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ Branch.prototype.createBranchUniversalObject = function (options) {
*
* @param (Object) options
* @param (Object) controlParameters
* @param (String) shareText [OPTIONAL]
*
* @return (Promise)
*
Expand Down Expand Up @@ -276,9 +277,13 @@ Branch.prototype.createBranchUniversalObject = function (options) {
* | $windows_phone_url | String | Kindle Fire URL |
* -------------------------------------------------------
*/
obj.showShareSheet = function (options, controlParameters) {
obj.showShareSheet = function (options, controlParameters, shareText) {

return execute('showShareSheet', [obj.instanceId, options, controlParameters]);
if ( ! shareText) {
shareText = 'This stuff is awesome: ';
}

return execute('showShareSheet', [obj.instanceId, options, controlParameters, shareText]);

};

Expand Down Expand Up @@ -333,7 +338,9 @@ Branch.prototype.createBranchUniversalObject = function (options) {
* List item on Spotlight (iOS Only).
*/
obj.listOnSpotlight = function () {

return execute('listOnSpotlight');

};

resolve(obj);
Expand Down

0 comments on commit 4e0edf4

Please sign in to comment.