Methods to open social media apps using deep links
fun Context.openUserFacebook(facebookUrl: String) {
val facebookIntent = Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=$facebookUrl"))
val isValidFacebookIntent = facebookIntent.resolveActivity(packageManager) != null
if (isValidFacebookIntent) startActivity(facebookIntent)
else browse(facebookUrl) // Needs Anko
}
fun Context.openUserTwitter(twitterUrl: String) {
val twitterIntent = Intent(Intent.ACTION_VIEW, Uri.parse(twitterUrl))
val isValidTwitterIntent = twitterIntent.resolveActivity(packageManager) != null
if (isValidTwitterIntent) startActivity(twitterIntent)
else browse(twitterUrl) // Needs Anko
}
fun Context.openUserLinkedIn(linkedinUrl: String) {
val linkedinIntent = Intent(Intent.ACTION_VIEW, Uri.parse("linkedin://$linkedinUrl"))
val isValidLinkedinIntent = linkedinIntent.resolveActivity(packageManager) != null
if (isValidLinkedinIntent) startActivity(linkedinIntent)
else browse(linkedinUrl) // Needs Anko
}
fun Context.openEventBlog(url: String) { // Medium or Tumblr
if (url.contains("medium")) {
val mediumIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
val isValidMediumIntent = mediumIntent.resolveActivity(packageManager) != null
if (isValidMediumIntent) {
startActivity(mediumIntent)
return
}
} else {
val blogName = url.split('.')[0]
var postLink = ""
if (url.contains("/post/")) {
val linkInfo = url.split('/')
val postId = linkInfo[linkInfo.indexOf("post") + 1]
postLink = "&page=permalink&postID=$postId#_=_"
}
val tumblrIntent = Intent(Intent.ACTION_VIEW, Uri.parse("tumblr://x-callback-url/blog?blogName=$blogName$postLink"))
val isValidTumblrIntent = tumblrIntent.resolveActivity(packageManager) != null
if (isValidTumblrIntent) {
startActivity(tumblrIntent)
return
}
}
openWebURL(url) // Needs Anko
}
fun Context.openUserYouTube(youtubeUrl: String) {
val youtubeIntent = Intent(Intent.ACTION_VIEW, Uri.parse(youtubeUrl))
val isValidYoutubeIntent = youtubeIntent.resolveActivity(packageManager) != null
if (isValidYoutubeIntent) startActivity(youtubeIntent)
else browse(youtubeUrl) // Needs Anko
}
fun Context.openUserInstagram(instagramUrl: String) {
val instagramUser = instagramUrl.split("/").last()
val instagramIntent = Intent(Intent.ACTION_VIEW, Uri.parse("instagram://user?username=$instagramUser"))
val isValidInstagramIntent = instagramIntent.resolveActivity(packageManager) != null
if (isValidInstagramIntent) startActivity(instagramIntent)
else browse(instagramUrl) // Needs Anko
}