Skip to content

Instantly share code, notes, and snippets.

@henrytao-me
Last active October 12, 2015 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrytao-me/7985eda1387a5d9fc13c to your computer and use it in GitHub Desktop.
Save henrytao-me/7985eda1387a5d9fc13c to your computer and use it in GitHub Desktop.
public class BranchUtils {
public static final String BRANCH_KEY_ANDROID_DEEPVIEW = "$android_deepview";
public static final String BRANCH_KEY_CLICKED_BRANCH_LINK = "+clicked_branch_link";
public static final String BRANCH_KEY_OG_DESCRIPTION = "$og_description";
public static final String BRANCH_KEY_OG_IMAGE_URL = "$og_image_url";
public static final String BRANCH_KEY_OG_TITLE = "$og_title";
public static final String DEFAULT_URL = "http://mychat.mysquar.com/";
public static BranchShortLinkBuilder getShortLinkBuilder(Context context, Post post) {
return new BranchShortLinkBuilder(context)
.setChannel("facebook")
.setFeature("test")
.addTag("hello")
.addTag("moto")
.setParameters(getParameters(context, post));
}
public static void showBranchShareLinkDialogAsync(Activity activity, Post post) {
IProgressDialog dialog = activity instanceof IProgressDialog ? (IProgressDialog) activity : null;
if (dialog != null) {
dialog.showProgressDialog(activity.getString(R.string.loading), false);
}
getShortLinkBuilder(activity, post).generateShortUrl((url, error) -> {
if (dialog != null) {
dialog.dismissProgressDialog();
}
if (!dialog.isPaused() && !activity.isFinishing()) {
if (error == null) {
url = DEFAULT_URL;
} else {
String title = activity.getString(R.string.share_branch_subject);
String description = activity.getString(R.string.share_branch_content, url);
Intent shareIntent = UiUtils.getSharingIntent(activity, title, description);
if (shareIntent != null) {
activity.startActivity(shareIntent);
}
Ln.i(String.format("custom | %s", url));
}
}
});
}
public static void showBranchShareLinkDialogSync(Activity activity, Post post) {
String url = getShortLinkBuilder(activity, post).getShortUrl();
String title = activity.getString(R.string.share_branch_subject);
String description = activity.getString(R.string.share_branch_content, url);
Intent shareIntent = UiUtils.getSharingIntent(activity, title, description);
if (shareIntent != null) {
activity.startActivity(shareIntent);
}
Ln.i(String.format("custom | %s", url));
}
protected static JSONObject getParameters(Context context, Post post) {
JSONObject object = new JSONObject();
try {
if (post != null) {
object.put(BRANCH_KEY_ANDROID_DEEPVIEW, "default_template");
object.put(BRANCH_KEY_OG_TITLE, post != null ? post.getUserDisplayName() : "");
object.put(BRANCH_KEY_OG_DESCRIPTION, post != null ? post.getMessage() : "");
object.put(BRANCH_KEY_OG_IMAGE_URL, post != null ? post.getPhotoUrl() : "");
object.put("_type", post.getClass().getName());
object.put("_id", post.getId());
}
} catch (JSONException ignore) {
}
return object;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment