101 lines
3.8 KiB
Kotlin
101 lines
3.8 KiB
Kotlin
/*
|
|
* 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.
|
|
*/
|
|
|
|
package code.name.monkey.retromusic.util
|
|
|
|
import android.app.Activity
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.content.SharedPreferences
|
|
import android.net.Uri
|
|
import code.name.monkey.retromusic.R
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
import com.google.android.play.core.review.ReviewManagerFactory
|
|
|
|
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
|
|
|
|
@JvmStatic
|
|
fun appLaunched(context: Context) {
|
|
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) {
|
|
//showRateDialog(context, editor)
|
|
showPlayStoreReviewDialog(context)
|
|
}
|
|
}
|
|
|
|
editor.commit()
|
|
}
|
|
|
|
private fun showPlayStoreReviewDialog(context: Context) {
|
|
val manager = ReviewManagerFactory.create(context)
|
|
manager.requestReviewFlow().addOnCompleteListener { request ->
|
|
if (request.isSuccessful) {
|
|
val reviewInfo = request.result
|
|
manager.launchReviewFlow(context as Activity, reviewInfo).addOnCompleteListener {
|
|
if (it.isSuccessful) {
|
|
//Toast.makeText(context, "Thanks for the feedback", Toast.LENGTH_SHORT).show()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun showRateDialog(context: Context, editor: SharedPreferences.Editor) {
|
|
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}")
|
|
)
|
|
)
|
|
editor.putBoolean(DO_NOT_SHOW_AGAIN, true)
|
|
editor.commit()
|
|
}
|
|
.setNeutralButton("Not now", null)
|
|
.setNegativeButton("No thanks") { _, _ ->
|
|
editor.putBoolean(DO_NOT_SHOW_AGAIN, true)
|
|
editor.commit()
|
|
}
|
|
.show()
|
|
}
|
|
} |