From fee133cc78cb79e5d2e055e40fd4d184535ad969 Mon Sep 17 00:00:00 2001
From: pragunvohra Initialises a session with the Branch API, assigning a {@link BranchUniversalReferralInitListener}
+ * Initialises a session with the Branch API, assigning a {@link Branch.BranchUniversalReferralInitListener}
* to perform an action upon successful initialisation. This method should be called if you know that a different person is about to use the app. For example,
* if you allow users to log out and let their friend use the app, you should call this to notify Branch
* to create a new user for this device. This will clear the first and latest params, as a new session is created.
Retrieves rewards for the current session, with a callback to perform a predefined * action following successful report of state change. You'll then need to call getCredits * in the callback to update the credit totals in your UX.
+ * + * @param callbackContext A callback to execute at the end of this method */ - private void loadRewards() + private void loadRewards(CallbackContext callbackContext) { Log.d(LCAT, "start loadRewards()"); - - this.instance.loadRewards(new LoadRewardsListener()); + this.instance.loadRewards(new LoadRewardsListener(callbackContext)); } @@ -272,10 +277,12 @@ private void loadRewards() * sessions persist for the duration of time that the app is in focus. For example, if you * minimize the app, these parameters will be cleared when closeSession is called. * + * @param callbackContext A callback to execute at the end of this method + * * @return A {@link JSONObject} containing the latest referring parameters as * configured locally. */ - private void getLatestReferringParams() + private void getLatestReferringParams(CallbackContext callbackContext) { Log.d(LCAT, "start getLatestReferringParams()"); @@ -289,7 +296,7 @@ private void getLatestReferringParams() Log.d(LCAT, sessionParams.toString()); } - this.callbackContext.success(sessionParams); + callbackContext.success(sessionParams); } @@ -299,8 +306,10 @@ private void getLatestReferringParams() * It is also only set if isReferrable is equal to true, which by default is only true * on a fresh install (not upgrade or reinstall). This will change on setIdentity (if the * user already exists from a previous device) and logout. + * + * @param callbackContext A callback to execute at the end of this method */ - private void getFirstReferringParams() + private void getFirstReferringParams(CallbackContext callbackContext) { Log.d(LCAT, "start getFirstReferringParams()"); @@ -314,7 +323,7 @@ private void getFirstReferringParams() Log.d(LCAT, installParams.toString()); } - this.callbackContext.success(installParams); + callbackContext.success(installParams); } @@ -327,7 +336,7 @@ private void getFirstReferringParams() * * @return A {@link JSONObject} value of BranchUniversalObject instance. */ - private void createBranchUniversalObject(JSONObject options) throws JSONException + private void createBranchUniversalObject(JSONObject options, CallbackContext callbackContext) throws JSONException { Log.d(LCAT, "start createBranchUniversalObject()"); @@ -381,8 +390,7 @@ private void createBranchUniversalObject(JSONObject options) throws JSONExceptio } } - this.callbackContext.success("Success"); - + callbackContext.success("Success"); } /** @@ -483,12 +491,12 @@ private BranchLinkProperties createLinkProperties(JSONObject options, JSONObject /** * Mark the content referred by this object as viewed. This increment the view count of the contents referred by this object. */ - private void registerView() + private void registerView(CallbackContext callbackContext) { Log.d(LCAT, "start registerView()"); - this.branchObj.registerView(); + this.branchObj.registerView(new RegisterViewStatusListener(callbackContext)); } @@ -498,7 +506,7 @@ private void registerView() * @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 + private void generateShortUrl(JSONObject options, JSONObject controlParams, CallbackContext callbackContext) throws JSONException { Log.d(LCAT, "start generateShortUrl()"); @@ -558,7 +566,7 @@ private void generateShortUrl(JSONObject options, JSONObject controlParams) thro linkProperties.addControlParameter("$windows_phone_url", controlParams.getString("$windows_phone_url")); } - this.branchObj.generateShortUrl(this.activity, linkProperties, new GenerateShortUrlListener()); + this.branchObj.generateShortUrl(this.activity, linkProperties, new GenerateShortUrlListener(callbackContext)); } @@ -575,7 +583,6 @@ private void setDebug(boolean isEnable) this.activity = this.cordova.getActivity(); - Branch debugInstance = Branch.getAutoInstance(this.activity.getApplicationContext()); if (isEnable) { @@ -589,14 +596,14 @@ private void setDebug(boolean isEnable) * identifier as a {@link String} value. * * @param newIdentity A {@link String} value containing the unique identifier of the user. + * @param callbackContext A callback to execute at the end of this method */ - private void setIdentity(String newIdentity) + private void setIdentity(String newIdentity, CallbackContext callbackContext) { Log.d(LCAT, "start setIdentity()"); - this.instance.setIdentity(newIdentity); - this.callbackContext.success("Success"); + this.instance.setIdentity(newIdentity, new SetIdentityListener(callbackContext)); } @@ -606,13 +613,15 @@ private void setIdentity(String newIdentity) * * @param action A {@link String} value to be passed as an action that the user has carried out. * For example "logged in" or "registered". + * @param callbackContext A callback to execute at the end of this method */ - private void userCompletedAction(String action) + private void userCompletedAction(String action, CallbackContext callbackContext) { Log.d(LCAT, "start userCompletedAction()"); this.instance.userCompletedAction(action); + callbackContext.success("Success"); } @@ -624,27 +633,30 @@ private void userCompletedAction(String action) * out. For example "logged in" or "registered". * @param metaData A {@link JSONObject} containing app-defined meta-data to be attached to a * user action that has just been completed. + * @param callbackContext A callback to execute at the end of this method */ - private void userCompletedAction(String action, JSONObject metaData) + private void userCompletedAction(String action, JSONObject metaData, CallbackContext callbackContext) { Log.d(LCAT, "start userCompletedAction()"); this.instance.userCompletedAction(action, metaData); - this.callbackContext.success("Success"); + callbackContext.success("Success"); } /** *Gets the credit history of the specified bucket and triggers a callback to handle the * response.
+ * + * @param callbackContext A callback to execute at the end of this method */ - private void getCreditHistory() + private void getCreditHistory(CallbackContext callbackContext) { Log.d(LCAT, "start creditHistory()"); - this.instance.getCreditHistory(new CreditHistoryListener()); + this.instance.getCreditHistory(new CreditHistoryListener(callbackContext)); } @@ -654,6 +666,12 @@ private void getCreditHistory() protected class SessionListener implements Branch.BranchReferralInitListener { + private CallbackContext _callbackContext; + + // Constructor that takes in a required callbackContext object + public SessionListener(CallbackContext callbackContext) { + this._callbackContext = callbackContext; + } //Listener that implements BranchReferralInitListener for initSession @Override @@ -676,7 +694,9 @@ public void onInitFinished(JSONObject referringParams, BranchError error) { String out = String.format("DeepLinkHandler(%s)", referringParams.toString()); webView.sendJavascript(out); - callbackContext.success(referringParams); + if (this._callbackContext != null) { + this._callbackContext.success(referringParams); + } } else { String errorMessage = error.getMessage(); @@ -686,16 +706,111 @@ public void onInitFinished(JSONObject referringParams, BranchError error) { String out = String.format("DeepLinkHandler(%s)", referringParams.toString()); webView.sendJavascript(out); - callbackContext.error(errorMessage); + if (this._callbackContext != null) { + this._callbackContext.error(errorMessage); + } + } + + } + + } + + protected class LogoutStatusListener implements Branch.LogoutStatusListener + { + private CallbackContext _callbackContext; + + // Constructor that takes in a required callbackContext object + public LogoutStatusListener (CallbackContext callbackContext) { + this._callbackContext = callbackContext; + } + + /** + * Called on finishing the the logout process + * + * @param loggedOut A {@link Boolean} which is set to true if logout succeeded + * @param error An instance of {@link BranchError} to notify any error occurred during logout. + * A null value is set if logout succeeded. + */ + @Override + public void onLogoutFinished(boolean loggedOut, BranchError error) { + if (error == null) { + Log.d(LCAT, "no error on logout"); + this._callbackContext.success(Boolean.toString(loggedOut)); + } else { + Log.d(LCAT, "error on logout"); + this._callbackContext.error(error.getMessage()); + } + } + } + + protected class SetIdentityListener implements Branch.BranchReferralInitListener + { + private CallbackContext _callbackContext; + + public SetIdentityListener (CallbackContext callbackContext) { + this._callbackContext = callbackContext; + } + + //Listener that implements BranchReferralInitListener for setIdentity + @Override + public void onInitFinished(JSONObject referringParams, BranchError error) { + + Log.d(LCAT, "SessionListener onSetIdentityFinished()"); + + if (error == null) { + + this._callbackContext.success("Success"); + + } else { + + String errorMessage = error.getMessage(); + + Log.d(LCAT, errorMessage); + + this._callbackContext.error(errorMessage); } } + + } + + protected class RegisterViewStatusListener implements BranchUniversalObject.RegisterViewStatusListener + { + + private CallbackContext _callbackContext; + + public RegisterViewStatusListener(CallbackContext callbackContext) { + this._callbackContext = callbackContext; + } + + @Override + public void onRegisterViewFinished(boolean registered, BranchError error) { + + Log.d(LCAT, "RegisterViewStatusListener registerViewFinished()"); + + if (error == null) { + this._callbackContext.success(Boolean.toString(registered)); + } else { + + String errorMessage = error.getMessage(); + + Log.d(LCAT, errorMessage); + + this._callbackContext.error(errorMessage); + } + } } protected class LoadRewardsListener implements Branch.BranchReferralStateChangedListener { + private CallbackContext _callbackContext; + + // Constructor that takes in a required callbackContext object + public LoadRewardsListener(CallbackContext callbackContext) { + this._callbackContext = callbackContext; + } // Listener that implements BranchReferralStateChangedListener for loadRewards @Override @@ -709,7 +824,7 @@ public void onStateChanged(boolean changed, BranchError error) { Log.d(LCAT, "LoadRewards success"); - callbackContext.success(credits); + this._callbackContext.success(credits); } else { @@ -717,7 +832,7 @@ public void onStateChanged(boolean changed, BranchError error) { Log.d(LCAT, errorMessage); - callbackContext.error(errorMessage); + this._callbackContext.error(errorMessage); } @@ -726,6 +841,13 @@ public void onStateChanged(boolean changed, BranchError error) { protected class GenerateShortUrlListener implements Branch.BranchLinkCreateListener { + private CallbackContext _callbackContext; + + // Constructor that takes in a required callbackContext object + public GenerateShortUrlListener(CallbackContext callbackContext) { + this._callbackContext = callbackContext; + } + @Override public void onLinkCreate(String url, BranchError error) { @@ -745,7 +867,7 @@ public void onLinkCreate(String url, BranchError error) { } Log.d(LCAT, response.toString()); - callbackContext.success(response); + this._callbackContext.success(response); } else { @@ -761,7 +883,7 @@ public void onLinkCreate(String url, BranchError error) { } Log.d(LCAT, response.toString()); - callbackContext.error(response); + this._callbackContext.error(response); } @@ -873,13 +995,19 @@ public void onChannelSelected(String channelName) { protected class CreditHistoryListener implements Branch.BranchListResponseListener { + private CallbackContext _callbackContext; + + // Constructor that takes in a required callbackContext object + public CreditHistoryListener(CallbackContext callbackContext) { + this._callbackContext = callbackContext; + } + // Listener that implements BranchListResponseListener for getCreditHistory() @Override public void onReceivingResponse(JSONArray list, BranchError error) { Log.d(LCAT, "inside onReceivingResponse"); - - JSONObject response = new JSONObject(); + ArrayList