Skip to content

Commit

Permalink
Merge pull request #45 from BranchMetrics/staging
Browse files Browse the repository at this point in the history
Prep release
  • Loading branch information
echo-branch authored Jul 15, 2021
2 parents 07969c3 + 1e16076 commit 252a70f
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 3,872 deletions.
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ repositories {


dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':capacitor-android')
implementation 'androidx.annotation:annotation:1.1.0'
Expand Down
134 changes: 104 additions & 30 deletions android/src/main/java/co/boundstate/BranchDeepLinks.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,84 @@
package co.boundstate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.os.UserManager;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import com.getcapacitor.JSArray;
import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import io.branch.indexing.BranchUniversalObject;
import io.branch.referral.Branch;
import io.branch.referral.BranchError;
import io.branch.referral.BranchShareSheetBuilder;
import io.branch.referral.BranchShortLinkBuilder;
import io.branch.referral.BranchUtil;
import io.branch.referral.SharingHelper;
import io.branch.referral.util.BRANCH_STANDARD_EVENT;
import io.branch.referral.util.BranchEvent;
import io.branch.referral.util.ContentMetadata;
import io.branch.referral.util.CurrencyType;
import io.branch.referral.util.ShareSheetStyle;
import java.util.Iterator;
import java.util.Objects;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@NativePlugin
@CapacitorPlugin(name = "BranchDeepLinks")
public class BranchDeepLinks extends Plugin {
private static final String EVENT_INIT = "init";
private static final String EVENT_INIT_ERROR = "initError";

@Nullable
private Uri mData;
public static Branch getBranchInstance(@NonNull Context context) {
boolean isUnlocked = isUserUnlocked(context);

private Activity activity;
if (isUnlocked) {
// Branch object initialization
return Branch.getAutoInstance(context);
}

return null;
}

private static boolean isUserUnlocked(@NonNull Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
return ((UserManager) Objects.requireNonNull(context.getSystemService(Context.USER_SERVICE))).isUserUnlocked();
} else {
return true;
}
}

@Override
protected void handleOnNewIntent(Intent intent) {
super.handleOnNewIntent(intent);
mData = intent.getData();
getActivity().setIntent(intent);
// if activity is in foreground (or in backstack but partially visible) launching the same
// activity will skip onStart, handle this case with sessionBuilder()...reInit()
// will re-initialize only if ""branch_force_new_session=true"" intent extra is set
Branch.sessionBuilder(getActivity()).withCallback(callback).reInit();
Branch branch = getBranchInstance(getContext());
if (branch != null) {
// if activity is in foreground (or in backstack but partially visible) launching the same
// activity will skip onStart, handle this case with sessionBuilder()...reInit()
// will re-initialize only if ""branch_force_new_session=true"" intent extra is set
Branch.sessionBuilder(getActivity()).withCallback(callback).reInit();
}
}

@Override
protected void handleOnStart() {
this.activity = getActivity();

super.handleOnStart();
Branch.sessionBuilder(getActivity()).withCallback(callback).withData(mData).init();
Uri data = getActivity().getIntent() != null ? getActivity().getIntent().getData() : null;
Branch branch = getBranchInstance(getContext());
if (branch != null) {
Branch.sessionBuilder(getActivity()).withCallback(callback).withData(data).init();
}
}

private Branch.BranchReferralInitListener callback = new Branch.BranchReferralInitListener() {

private final Branch.BranchReferralInitListener callback = new Branch.BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject referringParams, BranchError error) {
if (error == null) {
Expand Down Expand Up @@ -105,7 +129,7 @@ public void onLinkCreate(String url, BranchError error) {
if (error == null) {
JSObject ret = new JSObject();
ret.put("url", url);
call.success(ret);
call.resolve(ret);
} else {
call.reject(error.getMessage());
}
Expand All @@ -125,7 +149,7 @@ public void showShareSheet(PluginCall call) throws JSONException {
BranchShareSheetBuilder shareLinkBuilder = getShareLinkBuilder(shortLinkBuilder, shareSheetStyle);
shareLinkBuilder.shareLink();

call.success();
call.resolve();
}

@PluginMethod
Expand All @@ -138,7 +162,7 @@ public void getStandardEvents(PluginCall call) {

JSObject ret = new JSObject();
ret.put("branch_standard_events", events);
call.success(ret);
call.resolve(ret);
}

@PluginMethod
Expand Down Expand Up @@ -195,23 +219,31 @@ public void sendBranchEvent(PluginCall call) throws JSONException {
String keyValue = (String) keys.next();
event.addCustomDataProperty(keyValue, customData.getString(keyValue));
}
} else if (key.equals("contentMetadata")) {
JSONArray contentMetadata = metaData.getJSONArray("contentMetadata");

for (int i = 0; i < contentMetadata.length(); i++) {
JSONObject item = contentMetadata.getJSONObject(i);

BranchUniversalObject universalObject = this.getContentItem(item);
event.addContentItems(universalObject);
}
}
}

event.logEvent(activity);
event.logEvent(getActivity());

call.success();
call.resolve();
}

@PluginMethod
public void disableTracking(PluginCall call) {
this.activity = getActivity();
Boolean isEnabled = call.getBoolean("isEnabled", false);
Branch.getInstance().disableTracking(isEnabled);

JSObject ret = new JSObject();
ret.put("is_enabled", isEnabled);
call.success(ret);
call.resolve(ret);
}

@PluginMethod
Expand All @@ -229,7 +261,7 @@ public void onInitFinished(JSONObject referringParams, BranchError error) {
if (error == null) {
JSObject ret = new JSObject();
ret.put("referringParams", referringParams);
call.success(ret);
call.resolve(ret);
} else {
call.reject(error.getMessage());
}
Expand All @@ -250,7 +282,7 @@ public void onLogoutFinished(boolean loggedOut, BranchError error) {
if (error == null) {
JSObject ret = new JSObject();
ret.put("logged_out", loggedOut);
call.success(ret);
call.resolve(ret);
} else {
call.reject(error.getMessage());
}
Expand All @@ -266,9 +298,9 @@ private ShareSheetStyle getShareSheetStyle(String shareText) {
String more = "Show More";
String shareWith = "Share With";

ShareSheetStyle shareSheetStyle = new ShareSheetStyle(activity, shareTitle, shareText)
.setCopyUrlStyle(activity.getResources().getDrawable(android.R.drawable.ic_menu_send), copyToClipboard, clipboardSuccess)
.setMoreOptionStyle(activity.getResources().getDrawable(android.R.drawable.ic_menu_search), more)
ShareSheetStyle shareSheetStyle = new ShareSheetStyle(getActivity(), shareTitle, shareText)
.setCopyUrlStyle(getActivity().getResources().getDrawable(android.R.drawable.ic_menu_send), copyToClipboard, clipboardSuccess)
.setMoreOptionStyle(getActivity().getResources().getDrawable(android.R.drawable.ic_menu_search), more)
.addPreferredSharingOption(SharingHelper.SHARE_WITH.FACEBOOK)
.addPreferredSharingOption(SharingHelper.SHARE_WITH.EMAIL)
.addPreferredSharingOption(SharingHelper.SHARE_WITH.MESSAGE)
Expand All @@ -279,8 +311,50 @@ private ShareSheetStyle getShareSheetStyle(String shareText) {
return shareSheetStyle;
}

private BranchUniversalObject getContentItem(JSONObject item) throws JSONException {
BranchUniversalObject universalObject = new BranchUniversalObject();
ContentMetadata contentMetadata = new ContentMetadata();
Iterator<String> keys = item.keys();

while (keys.hasNext()) {
String key = keys.next();

switch (key) {
case "quantity":
contentMetadata.setQuantity(Double.parseDouble(item.getString(key)));
break;
case "price":
contentMetadata.price = Double.parseDouble(item.getString(key));
break;
case "currency":
String currencyString = item.getString(key);
CurrencyType currency = CurrencyType.getValue(currencyString);
if (currency != null) {
contentMetadata.currencyType = currency;
}
break;
case "productName":
contentMetadata.setProductName(item.getString(key));
break;
case "productBrand":
contentMetadata.setProductBrand(item.getString(key));
break;
case "sku":
contentMetadata.setSku(item.getString(key));
break;
default:
contentMetadata.addCustomMetadata(key, item.getString(key));
break;
}
}

universalObject.setContentMetadata(contentMetadata);

return universalObject;
}

private BranchShortLinkBuilder getShortLinkBuilder(JSObject analytics, JSObject properties) throws JSONException {
BranchShortLinkBuilder shortLinkBuilder = new BranchShortLinkBuilder(activity);
BranchShortLinkBuilder shortLinkBuilder = new BranchShortLinkBuilder(getActivity());

// Add analytics properties
if (analytics.has("feature")) {
Expand Down Expand Up @@ -320,7 +394,7 @@ private BranchShortLinkBuilder getShortLinkBuilder(JSObject analytics, JSObject
}

private BranchShareSheetBuilder getShareLinkBuilder(BranchShortLinkBuilder shortLinkBuilder, ShareSheetStyle shareSheetStyle) {
BranchShareSheetBuilder shareLinkBuilder = new BranchShareSheetBuilder(activity, shortLinkBuilder);
BranchShareSheetBuilder shareLinkBuilder = new BranchShareSheetBuilder(getActivity(), shortLinkBuilder);

shareLinkBuilder.setSubject(shareSheetStyle.getMessageTitle()).setMessage(shareSheetStyle.getMessageBody());

Expand Down
14 changes: 14 additions & 0 deletions android/src/main/java/co/boundstate/BranchMultiDexApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package co.boundstate;

import static co.boundstate.BranchDeepLinks.getBranchInstance;

import androidx.multidex.MultiDexApplication;

public class BranchMultiDexApp extends MultiDexApplication {

@Override
public void onCreate() {
super.onCreate();
getBranchInstance(this);
}
}
Loading

0 comments on commit 252a70f

Please sign in to comment.