Skip to content

Instantly share code, notes, and snippets.

@4xes
Created February 1, 2018 12:02
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 4xes/4a5eba524944a9737aeac06bbaa48687 to your computer and use it in GitHub Desktop.
Save 4xes/4a5eba524944a9737aeac06bbaa48687 to your computer and use it in GitHub Desktop.
ReviewUtils.kt
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Parcelable
import android.util.Log
import android.widget.Toast
object ReviewUtils {
fun openStoreReviewInChrome(activity: Activity, urlReview: String) {
val uriStore = Uri.parse(urlReview)
val uriBrowser = Uri.parse("http://www.example.com")
val intent = Intent(Intent.ACTION_VIEW, uriBrowser)
val ignoreStorePackage = "com.android.vending"
val recommendPackage = "com.android.chrome"
openUriExceptPackage(activity, intent, uriStore, ignoreStorePackage, recommendPackage)
}
private fun openUriExceptPackage(activity: Activity, intent: Intent, openUri: Uri, ignorePackage: String, recommendPackage: String? = null) {
val activities = activity.packageManager.queryIntentActivities(intent, 0)
var targetIntents = ArrayList<Intent>()
for (currentInfo in activities) {
val packageName = currentInfo.activityInfo.packageName
Log.d("TAG", packageName)
if (ignorePackage != packageName) {
val targetIntent = Intent(Intent.ACTION_VIEW)
targetIntent.data = openUri
targetIntent.`package` = packageName
targetIntents.add(targetIntent)
}
}
if(targetIntents.size > 0) {
if (recommendPackage != null) {
val recommendTarget = targetIntents.find { it.`package` == recommendPackage }
if (recommendTarget != null) {
val emptyChooser = Intent.createChooser(recommendTarget, "Open review in: ")
emptyChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, emptyArray<Parcelable>())
activity.startActivity(emptyChooser)
return
}
}
val chooserIntent = Intent.createChooser(targetIntents.removeAt(0), "Open review in: ")
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(emptyArray<Parcelable>()))
activity.startActivity(chooserIntent)
}
else {
Toast.makeText(activity, "No app found", Toast.LENGTH_SHORT).show();
}
}
@4xes
Copy link
Author

4xes commented Feb 1, 2018

Переход на ссылку через браузер минуя приложения, если на устройстве есть Chrome, то запуск будет через него.

val urlReview = "https://play.google.com/store/apps/details?id={package}&hl=ru&reviewId={id}&hl=ru"
ReviewUtils.openStoreReviewInChrome(activity, urlReview)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment