2019-04-20 05:29:45 +00:00
|
|
|
package code.name.monkey.retromusic.activities
|
2018-12-06 08:52:57 +00:00
|
|
|
|
|
|
|
import android.content.Intent
|
2020-04-30 20:48:14 +00:00
|
|
|
import android.content.res.ColorStateList
|
|
|
|
import android.graphics.Color
|
2019-12-01 15:27:01 +00:00
|
|
|
import android.os.AsyncTask
|
|
|
|
import android.os.Bundle
|
2018-12-06 08:52:57 +00:00
|
|
|
import android.util.Log
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.widget.Toast
|
2020-04-30 20:48:14 +00:00
|
|
|
import code.name.monkey.appthemehelper.ThemeStore
|
2019-12-01 15:27:01 +00:00
|
|
|
import code.name.monkey.appthemehelper.util.MaterialUtil
|
|
|
|
import code.name.monkey.retromusic.App
|
2018-12-06 08:52:57 +00:00
|
|
|
import code.name.monkey.retromusic.BuildConfig
|
2019-12-01 15:27:01 +00:00
|
|
|
import code.name.monkey.retromusic.R
|
2019-04-20 05:29:45 +00:00
|
|
|
import code.name.monkey.retromusic.activities.base.AbsBaseActivity
|
2019-12-01 15:27:01 +00:00
|
|
|
import com.anjlab.android.iab.v3.BillingProcessor
|
|
|
|
import com.anjlab.android.iab.v3.TransactionDetails
|
2020-02-26 16:49:24 +00:00
|
|
|
import kotlinx.android.synthetic.main.activity_pro_version.*
|
2018-12-06 08:52:57 +00:00
|
|
|
import java.lang.ref.WeakReference
|
|
|
|
|
2018-12-09 06:38:34 +00:00
|
|
|
class PurchaseActivity : AbsBaseActivity(), BillingProcessor.IBillingHandler {
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2019-12-30 11:01:50 +00:00
|
|
|
private lateinit var billingProcessor: BillingProcessor
|
|
|
|
private var restorePurchaseAsyncTask: AsyncTask<*, *, *>? = null
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
setDrawUnderStatusBar()
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_pro_version)
|
2020-04-30 20:48:14 +00:00
|
|
|
setStatusbarColor(Color.TRANSPARENT)
|
|
|
|
setLightStatusbar(false)
|
|
|
|
setNavigationbarColor(Color.BLACK)
|
|
|
|
setLightNavigationBar(false)
|
|
|
|
toolbar.navigationIcon?.setTint(Color.WHITE)
|
|
|
|
toolbar.setNavigationOnClickListener { onBackPressed() }
|
2019-12-30 11:01:50 +00:00
|
|
|
|
|
|
|
restoreButton.isEnabled = false
|
|
|
|
purchaseButton.isEnabled = false
|
|
|
|
|
|
|
|
billingProcessor = BillingProcessor(this, BuildConfig.GOOGLE_PLAY_LICENSING_KEY, this)
|
|
|
|
|
|
|
|
MaterialUtil.setTint(purchaseButton, true)
|
|
|
|
|
|
|
|
restoreButton.setOnClickListener {
|
|
|
|
if (restorePurchaseAsyncTask == null || restorePurchaseAsyncTask!!.status != AsyncTask.Status.RUNNING) {
|
|
|
|
restorePurchase()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
purchaseButton.setOnClickListener {
|
|
|
|
billingProcessor.purchase(this@PurchaseActivity, App.PRO_VERSION_PRODUCT_ID)
|
|
|
|
}
|
2020-04-30 20:48:14 +00:00
|
|
|
bannerContainer.backgroundTintList =
|
|
|
|
ColorStateList.valueOf(ThemeStore.accentColor(this))
|
2019-12-30 11:01:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun restorePurchase() {
|
|
|
|
if (restorePurchaseAsyncTask != null) {
|
|
|
|
restorePurchaseAsyncTask!!.cancel(false)
|
|
|
|
}
|
|
|
|
restorePurchaseAsyncTask = RestorePurchaseAsyncTask(this).execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onProductPurchased(productId: String, details: TransactionDetails?) {
|
|
|
|
Toast.makeText(this, R.string.thank_you, Toast.LENGTH_SHORT).show()
|
|
|
|
setResult(RESULT_OK)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPurchaseHistoryRestored() {
|
|
|
|
if (App.isProVersion()) {
|
|
|
|
Toast.makeText(
|
|
|
|
this,
|
|
|
|
R.string.restored_previous_purchase_please_restart,
|
|
|
|
Toast.LENGTH_LONG
|
|
|
|
).show()
|
|
|
|
setResult(RESULT_OK)
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, R.string.no_purchase_found, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBillingError(errorCode: Int, error: Throwable?) {
|
|
|
|
Log.e(TAG, "Billing error: code = $errorCode", error)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBillingInitialized() {
|
|
|
|
restoreButton.isEnabled = true
|
|
|
|
purchaseButton.isEnabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
if (!billingProcessor.handleActivityResult(requestCode, resultCode, data)) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
when (item.itemId) {
|
|
|
|
android.R.id.home -> finish()
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
billingProcessor.release()
|
|
|
|
super.onDestroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
private class RestorePurchaseAsyncTask internal constructor(purchaseActivity: PurchaseActivity) :
|
|
|
|
AsyncTask<Void, Void, Boolean>() {
|
|
|
|
|
|
|
|
private val buyActivityWeakReference: WeakReference<PurchaseActivity> = WeakReference(
|
|
|
|
purchaseActivity
|
|
|
|
)
|
|
|
|
|
|
|
|
override fun onPreExecute() {
|
|
|
|
super.onPreExecute()
|
|
|
|
val purchaseActivity = buyActivityWeakReference.get()
|
|
|
|
if (purchaseActivity != null) {
|
|
|
|
Toast.makeText(purchaseActivity, R.string.restoring_purchase, Toast.LENGTH_SHORT)
|
|
|
|
.show()
|
|
|
|
} else {
|
|
|
|
cancel(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun doInBackground(vararg params: Void): Boolean? {
|
|
|
|
val purchaseActivity = buyActivityWeakReference.get()
|
|
|
|
if (purchaseActivity != null) {
|
|
|
|
return purchaseActivity.billingProcessor.loadOwnedPurchasesFromGoogle()
|
|
|
|
}
|
|
|
|
cancel(false)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPostExecute(b: Boolean?) {
|
|
|
|
super.onPostExecute(b)
|
|
|
|
val purchaseActivity = buyActivityWeakReference.get()
|
|
|
|
if (purchaseActivity == null || b == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b) {
|
|
|
|
purchaseActivity.onPurchaseHistoryRestored()
|
|
|
|
} else {
|
|
|
|
Toast.makeText(
|
|
|
|
purchaseActivity,
|
|
|
|
R.string.could_not_restore_purchase,
|
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val TAG: String = "PurchaseActivity"
|
|
|
|
}
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|