Created
February 1, 2018 12:02
-
-
Save 4xes/4a5eba524944a9737aeac06bbaa48687 to your computer and use it in GitHub Desktop.
ReviewUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Переход на ссылку через браузер минуя приложения, если на устройстве есть Chrome, то запуск будет через него.