PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/util/AppRater.kt

104 lines
3.9 KiB
Kotlin
Raw Normal View History

2019-09-22 18:56:40 +00:00
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
2019-10-26 13:46:01 +00:00
package code.name.monkey.retromusic.util
2019-09-22 18:56:40 +00:00
2020-08-11 18:29:44 +00:00
import android.app.Activity
2019-09-22 18:56:40 +00:00
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import code.name.monkey.retromusic.R
2020-05-24 18:04:50 +00:00
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2020-08-11 18:29:44 +00:00
import com.google.android.play.core.review.ReviewManagerFactory
2019-09-22 18:56:40 +00:00
object AppRater {
private const val DO_NOT_SHOW_AGAIN = "do_not_show_again"// Package Name
private const val APP_RATING = "app_rating"// Package Name
private const val LAUNCH_COUNT = "launch_count"// Package Name
private const val DATE_FIRST_LAUNCH = "date_first_launch"// Package Name
private const val DAYS_UNTIL_PROMPT = 3//Min number of days
private const val LAUNCHES_UNTIL_PROMPT = 5//Min number of launches
2020-05-23 13:53:10 +00:00
2020-02-19 07:08:12 +00:00
@JvmStatic
2020-10-02 12:48:57 +00:00
fun appLaunched(context: Activity) {
2019-09-22 18:56:40 +00:00
val prefs = context.getSharedPreferences(APP_RATING, 0)
if (prefs.getBoolean(DO_NOT_SHOW_AGAIN, false)) {
return
}
val editor = prefs.edit()
// Increment launch counter
val launchCount = prefs.getLong(LAUNCH_COUNT, 0) + 1
editor.putLong(LAUNCH_COUNT, launchCount)
// Get date of first launch
var dateFirstLaunch = prefs.getLong(DATE_FIRST_LAUNCH, 0)
if (dateFirstLaunch == 0L) {
dateFirstLaunch = System.currentTimeMillis()
editor.putLong(DATE_FIRST_LAUNCH, dateFirstLaunch)
}
// Wait at least n days before opening
if (launchCount >= LAUNCHES_UNTIL_PROMPT) {
if (System.currentTimeMillis() >= dateFirstLaunch + DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000) {
2020-08-11 18:29:44 +00:00
//showRateDialog(context, editor)
2020-10-02 12:48:57 +00:00
showPlayStoreReviewDialog(context, editor)
2019-09-22 18:56:40 +00:00
}
}
editor.commit()
}
2020-10-02 12:48:57 +00:00
private fun showPlayStoreReviewDialog(context: Activity, editor: SharedPreferences.Editor) {
2020-08-11 18:29:44 +00:00
val manager = ReviewManagerFactory.create(context)
2020-10-02 12:48:57 +00:00
val flow = manager.requestReviewFlow()
flow.addOnCompleteListener { request ->
2020-08-11 18:29:44 +00:00
if (request.isSuccessful) {
val reviewInfo = request.result
2020-10-02 12:48:57 +00:00
val flowManager = manager.launchReviewFlow(context, reviewInfo)
flowManager.addOnCompleteListener {
2020-08-11 18:29:44 +00:00
if (it.isSuccessful) {
2020-10-02 12:48:57 +00:00
editor.putBoolean(DO_NOT_SHOW_AGAIN, true)
editor.commit()
2020-08-11 18:29:44 +00:00
}
}
}
}
}
2019-09-22 18:56:40 +00:00
private fun showRateDialog(context: Context, editor: SharedPreferences.Editor) {
2020-05-24 18:04:50 +00:00
MaterialAlertDialogBuilder(context)
.setTitle("Rate this App")
.setMessage("If you enjoy using Retro Music, please take a moment to rate it. Thanks for your support!")
.setPositiveButton(R.string.app_name) { _, _ ->
context.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=${context.packageName}")
2020-02-19 07:08:12 +00:00
)
2020-05-24 18:04:50 +00:00
)
editor.putBoolean(DO_NOT_SHOW_AGAIN, true)
editor.commit()
2020-02-19 07:08:12 +00:00
}
2020-05-24 18:04:50 +00:00
.setNeutralButton("Not now", null)
.setNegativeButton("No thanks") { _, _ ->
editor.putBoolean(DO_NOT_SHOW_AGAIN, true)
editor.commit()
}
.show()
2019-09-22 18:56:40 +00:00
}
}