Skip to content

Instantly share code, notes, and snippets.

@KryptKode
Last active May 29, 2020 06:38
Show Gist options
  • Save KryptKode/96554fcd1b856976d7b50a7706392843 to your computer and use it in GitHub Desktop.
Save KryptKode/96554fcd1b856976d7b50a7706392843 to your computer and use it in GitHub Desktop.
Android app share helper
import android.content.ComponentName
import android.content.Intent
import android.content.pm.ActivityInfo
import android.net.Uri
import androidx.fragment.app.FragmentActivity
import com.allwishes.wishesapp.R
class PlayStoreUtils(private val context: FragmentActivity) {
fun openPlayStore(appPackageName: String = getAppPackageName()) {
val marketIntent = createPlayStoreMarketIntent(appPackageName)
val playStoreInfo = resolvePlayStore(marketIntent)
if (playStoreInfo != null) {
val component = getComponent(playStoreInfo)
addComponentAndFlagsToIntent(marketIntent, component)
openPlayStoreDirectly(marketIntent)
} else {
openPlayStoreInBrowser(appPackageName)
}
}
fun openPlayStoreForDeveloper(developerId: String = DEVELOPER_ID) {
/* val marketIntent = createPlayStoreDevMarketIntent(developerId)
val playStoreInfo = resolvePlayStore(marketIntent)
if (playStoreInfo != null) {
val component = getComponent(playStoreInfo)
addComponentAndFlagsToIntent(marketIntent, component)
openPlayStoreDirectly(marketIntent)
} else {
openPlayStoreDevInBrowser(developerId)
}*/
openPlayStoreDevInBrowser(developerId)
}
private fun openPlayStoreDirectly(intent: Intent) {
context.startActivity(intent)
}
private fun openPlayStoreInBrowser(appPackageName: String) {
val intent = createPlayStoreBrowserIntent(appPackageName)
context.startActivity(
Intent.createChooser(
intent,
context.getString(R.string.open_playstore_with)
)
)
}
private fun openPlayStoreDevInBrowser(developerId: String) {
val intent = createPlayStoreDevBrowserIntent(developerId)
context.startActivity(
Intent.createChooser(
intent,
context.getString(R.string.open_playstore_with)
)
)
}
private fun createPlayStoreMarketIntent(appPackageName: String): Intent {
val marketUrl = getMarketUrl(appPackageName)
val marketUri = convertToUri(marketUrl)
return Intent(Intent.ACTION_VIEW, marketUri)
}
private fun createPlayStoreDevMarketIntent(appPackageName: String): Intent {
val marketUrl = getMarketUrl(appPackageName)
val marketUri = convertToUri(marketUrl)
return Intent(Intent.ACTION_VIEW, marketUri)
}
private fun addComponentAndFlagsToIntent(intent: Intent, componentName: ComponentName) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.component = componentName
}
private fun createPlayStoreDevBrowserIntent(developerId: String): Intent {
val playStoreUrl = getPlayStoreDevUrl(developerId)
val playStoreBrowserUri = convertToUri(playStoreUrl)
return Intent(Intent.ACTION_VIEW, playStoreBrowserUri)
}
private fun createPlayStoreBrowserIntent(appPackageName: String): Intent {
val playStoreUrl = getPlayStoreUrl(appPackageName)
val playStoreBrowserUri = convertToUri(playStoreUrl)
return Intent(Intent.ACTION_VIEW, playStoreBrowserUri)
}
private fun resolvePlayStore(intent: Intent): ActivityInfo? {
val otherAppsResolveInfo = context.packageManager
.queryIntentActivities(intent, 0)
for (resolveInfo in otherAppsResolveInfo) {
if (resolveInfo.activityInfo.applicationInfo.packageName == PLAY_STORE_PACKAGE_NAME) {
return resolveInfo.activityInfo
}
}
return null
}
private fun getComponent(activityInfo: ActivityInfo): ComponentName {
return ComponentName(
activityInfo.applicationInfo.packageName,
activityInfo.name
)
}
private fun convertToUri(uri: String): Uri? {
return Uri.parse(uri)
}
private fun getAppPackageName(): String {
return context.packageName
}
private fun getMarketUrl(appPackageName: String): String {
return "$MARKET_URI_BASE$appPackageName"
}
private fun getPlayStoreUrl(appPackageName: String): String {
return "$PLAY_STORE_URL_BASE$appPackageName"
}
private fun getMarketDevUrl(developerId: String): String {
return "$MARKET_DEV_URI_BASE$developerId"
}
private fun getPlayStoreDevUrl(developerId: String): String {
return "$PLAY_STORE__DEV_URL_BASE$developerId"
}
companion object {
const val PLAY_STORE_URL_BASE = "https://play.google.com/store/apps/details?id="
const val PLAY_STORE__DEV_URL_BASE = "https://play.google.com/store/apps/developer?id="
const val MARKET_URI_BASE = "market://details?id="
const val MARKET_DEV_URI_BASE = "market://dev?id="
const val PLAY_STORE_PACKAGE_NAME = "com.android.vending"
const val DEVELOPER_ID = "Handikapp"
}
}
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.core.content.FileProvider
import com.allwishes.wishesapp.R
import com.allwishes.wishesapp.app.utils.rating.UriHelper
import timber.log.Timber
import java.io.File
/**
* Created by kryptkode on 2/11/2020.
*/
class ShareUtils(private val context: Context) {
companion object {
const val PLAY_STORE_URL_BASE = "https://play.google.com/store/apps/details?id="
const val IMAGE_MIME_TYPE = "image/*"
const val TEXT_MIME_TYPE = "text/*"
const val PROVIDER = ".provider"
const val WHATS_APP_PACKAGE_NAME = "com.whatsapp"
const val WHATS_APP_BUSINESS_PACKAGE_NAME = "com.whatsapp.w4b"
const val FACEBOOK_PACKAGE_NAME = "com.facebook.katana"
const val TWITTER_PACKAGE_NAME = "com.twitter.android"
const val GOOGLE_PLAY_PACKAGE_NAME = "com.android.vending"
}
fun createShareAppIntent(): Intent? {
val intent = Intent(Intent.ACTION_SEND)
val text = context.getString(R.string.share_text, getPlayStoreUrl())
intent.putExtra(Intent.EXTRA_TEXT, text)
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
intent.type =
TEXT_MIME_TYPE
return Intent.createChooser(intent, context.getString(R.string.share_msg))
}
fun createRateIntentForGooglePlay(): Intent {
val packageName = context.packageName
val intent = Intent(Intent.ACTION_VIEW, UriHelper.getGooglePlay(packageName))
if (UriHelper.isPackageExists(context, GOOGLE_PLAY_PACKAGE_NAME)) {
intent.setPackage(GOOGLE_PLAY_PACKAGE_NAME)
}
return intent
}
fun createRateIntentForAmazonAppstore(): Intent {
val packageName = context.packageName
return Intent(Intent.ACTION_VIEW, UriHelper.getAmazonAppstore(packageName))
}
var listener: ShareListener? = null
private fun getPlayStoreUrl(): String {
return "${PlayStoreUtils.PLAY_STORE_URL_BASE}${context.packageName}"
}
private fun getUriForFile(file: File): Uri {
return FileProvider.getUriForFile(
context,
"${context.packageName}$PROVIDER",
file
)
}
fun shareFileOthers(file: File) {
shareOthers(getUriForFile(file))
}
fun shareOthers(uri: Uri) {
val intent = Intent(Intent.ACTION_SEND)
val text = getShareText()
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.putExtra(Intent.EXTRA_STREAM, uri)
intent.putExtra(Intent.EXTRA_TEXT, text)
intent.type = IMAGE_MIME_TYPE
context.startActivity(
Intent.createChooser(
intent,
context.getString(R.string.share_with)
)
)
}
fun shareFileByPackageName(uri: Uri, packageName: String) {
try {
val intent = Intent(Intent.ACTION_SEND)
val text = getShareText()
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.putExtra(Intent.EXTRA_STREAM, uri)
intent.putExtra(Intent.EXTRA_TEXT, text)
intent.type = IMAGE_MIME_TYPE
intent.setPackage(packageName)
context.startActivity(intent)
} catch (e: ActivityNotFoundException) {
Timber.e(e)
listener?.onShareError(errorMessageFromPackageName(packageName))
}
}
private fun getShareText() =
context.getString(
R.string.share_image_text,
context.getString(R.string.app_name),
getPlayStoreUrl()
)
@Throws(ActivityNotFoundException::class)
fun shareFileByPackageNameThrows(uri: Uri, packageName: String) {
val intent = Intent(Intent.ACTION_SEND)
val text = getShareText()
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.putExtra(Intent.EXTRA_STREAM, uri)
intent.putExtra(Intent.EXTRA_TEXT, text)
intent.type = IMAGE_MIME_TYPE
intent.setPackage(packageName)
context.startActivity(intent)
}
fun shareViaWhatsApp(file: File) {
try {
shareFileByPackageNameThrows(getUriForFile(file), WHATS_APP_PACKAGE_NAME)
} catch (e: ActivityNotFoundException) {
Timber.e(e)
shareFileByPackageName(getUriForFile(file), WHATS_APP_BUSINESS_PACKAGE_NAME)
}
}
fun shareViaFacebook(file: File) {
shareFileByPackageName(getUriForFile(file), FACEBOOK_PACKAGE_NAME)
}
fun shareViaTwitter(file: File) {
shareFileByPackageName(getUriForFile(file), TWITTER_PACKAGE_NAME)
}
private fun errorMessageFromPackageName(packageName: String): String {
return when (packageName) {
WHATS_APP_PACKAGE_NAME -> context.getString(R.string.whats_app_not_installed_error_message)
FACEBOOK_PACKAGE_NAME -> context.getString(R.string.facebook_not_installed_error_message)
TWITTER_PACKAGE_NAME -> context.getString(R.string.twitter_not_installed_error_message)
else -> context.getString(R.string.other_share_error_message)
}
}
interface ShareListener {
fun onShareError(message: String)
}
}
<string name="share_msg">Share with…</string>
<string name="share_text">Hey, check out this awesome app!\n%s</string>
<string name="whats_app_not_installed_error_message">Whatsapp was not found, please install and try again</string>
<string name="facebook_not_installed_error_message">Facebook app was not found, please install and try again</string>
<string name="twitter_not_installed_error_message">Twitter app was not found, please install and try again</string>
<string name="other_share_error_message">An error occurred while sharing, please try again</string>
<string name="share_with">Share with…</string>
<string name="open_playstore_with">Open with</string>
<string name="share_image_text">Download %1$s for more %2$s</string>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment