Skip to content

Commit

Permalink
Added some extra debugging info.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyost committed Aug 22, 2020
1 parent c794ad4 commit 6091d4e
Showing 1 changed file with 85 additions and 80 deletions.
165 changes: 85 additions & 80 deletions android/src/main/kotlin/fr/skyost/rate_my_app/RateMyAppPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,103 +19,108 @@ import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar

public class RateMyAppPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "rate_my_app")
channel.setMethodCallHandler(RateMyAppPlugin())
public class RateMyAppPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "rate_my_app")
channel.setMethodCallHandler(RateMyAppPlugin())
}
}
}

private var activity: Activity? = null
private lateinit var channel : MethodChannel

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, "rate_my_app")
channel.setMethodCallHandler(this);
}
private var activity: Activity? = null
private lateinit var channel: MethodChannel

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if(activity == null) {
result.error("activity_is_null", "Activity is null.", null);
return;
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, "rate_my_app")
channel.setMethodCallHandler(this);
}

val activity: Activity = this.activity!!
when(call.method) {
"launchNativeReviewDialog" -> {
val manager: ReviewManager = ReviewManagerFactory.create(activity)
val request: Task<ReviewInfo> = manager.requestReviewFlow()
request.addOnCompleteListener { task ->
if (task.isSuccessful) {
val reviewInfo: ReviewInfo = task.result
val flow: Task<Void> = manager.launchReviewFlow(activity, reviewInfo)
flow.addOnCompleteListener { result.success(true) }
} else {
result.success(false)
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (activity == null) {
result.error("activity_is_null", "Activity is null.", null);
return;
}

val activity: Activity = this.activity!!
when (call.method) {
"launchNativeReviewDialog" -> {
val manager: ReviewManager = ReviewManagerFactory.create(activity)
val request: Task<ReviewInfo> = manager.requestReviewFlow()
request.addOnCompleteListener { reviewTask ->
when {
reviewTask.isSuccessful -> {
val reviewInfo: ReviewInfo = reviewTask.result
val flow: Task<Void> = manager.launchReviewFlow(activity, reviewInfo)
flow.addOnCompleteListener { task -> result.success(task.exception != null) }
}
reviewTask.exception != null -> {
result.error(reviewTask.exception!!.javaClass.name, reviewTask.exception!!.localizedMessage, null)
}
else -> {
result.success(false)
}
}
}
}
"isNativeDialogSupported" -> result.success(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && isPlayStoreInstalled(activity))
"launchStore" -> {
goToPlayStore(activity, if (call.hasArgument("appId")) call.argument<String>("appId")!! else activity.applicationContext.packageName)
result.success(true)
}
else -> result.notImplemented()
}
}
"isNativeDialogSupported" -> result.success(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && isPlayStoreInstalled(activity))
"launchStore" -> {
goToPlayStore(activity, if (call.hasArgument("appId")) call.argument<String>("appId")!! else activity.applicationContext.packageName)
result.success(true)
}
else -> result.notImplemented()
}
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
}

override fun onDetachedFromActivityForConfigChanges() {
onDetachedFromActivity()
}
override fun onDetachedFromActivityForConfigChanges() {
onDetachedFromActivity()
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
onAttachedToActivity(binding)
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
onAttachedToActivity(binding)
}

override fun onDetachedFromActivity() {
activity = null
}
override fun onDetachedFromActivity() {
activity = null
}

/**
* Returns whether the Play Store is installed on the current device.
*
* @param activity The activity.
*
* @return Whether the Play Store is installed on the current device.
*/
/**
* Returns whether the Play Store is installed on the current device.
*
* @param activity The activity.
*
* @return Whether the Play Store is installed on the current device.
*/

private fun isPlayStoreInstalled(activity: Activity): Boolean {
return try {
activity.packageManager.getPackageInfo("com.android.vending", 0);
true
} catch (ex: PackageManager.NameNotFoundException) {
false
private fun isPlayStoreInstalled(activity: Activity): Boolean {
return try {
activity.packageManager.getPackageInfo("com.android.vending", 0);
true
} catch (ex: PackageManager.NameNotFoundException) {
false
}
}
}

/**
* Launches a Play Store instance.
*
* @param activity The activity.
* @param applicationId The application ID.
*/
/**
* Launches a Play Store instance.
*
* @param activity The activity.
* @param applicationId The application ID.
*/

private fun goToPlayStore(activity: Activity, applicationId: String) {
try {
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market:https://details?id=$applicationId")));
}
catch(ex: android.content.ActivityNotFoundException) {
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$applicationId")));
private fun goToPlayStore(activity: Activity, applicationId: String) {
try {
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market:https://details?id=$applicationId")));
} catch (ex: android.content.ActivityNotFoundException) {
activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$applicationId")));
}
}
}
}

0 comments on commit 6091d4e

Please sign in to comment.