PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/activities/SupportDevelopmentActivity.kt

232 lines
8.4 KiB
Kotlin
Raw Normal View History

2020-10-06 08:46:04 +00:00
/*
* Copyright (c) 2020 Hemanth Savarla.
*
* 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-04-20 05:29:45 +00:00
package code.name.monkey.retromusic.activities
2018-12-06 08:52:57 +00:00
import android.graphics.Paint
2019-12-01 15:27:01 +00:00
import android.os.Bundle
2018-12-06 08:52:57 +00:00
import android.util.Log
2019-12-01 15:27:01 +00:00
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
2018-12-06 08:52:57 +00:00
import androidx.annotation.LayoutRes
2019-10-12 06:41:48 +00:00
import androidx.appcompat.widget.AppCompatImageView
2021-11-08 05:38:52 +00:00
import androidx.core.view.isVisible
2019-12-01 15:27:01 +00:00
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
2018-12-06 08:52:57 +00:00
import code.name.monkey.appthemehelper.ThemeStore
2019-12-01 15:27:01 +00:00
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.TintHelper
2020-01-03 12:40:42 +00:00
import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper
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
import code.name.monkey.retromusic.databinding.ActivityDonationBinding
2020-02-22 12:10:12 +00:00
import code.name.monkey.retromusic.extensions.textColorPrimary
import code.name.monkey.retromusic.extensions.textColorSecondary
2019-12-01 15:27:01 +00:00
import com.anjlab.android.iab.v3.BillingProcessor
2021-11-08 05:38:52 +00:00
import com.anjlab.android.iab.v3.PurchaseInfo
2019-12-01 15:27:01 +00:00
import com.anjlab.android.iab.v3.SkuDetails
import java.util.*
2018-12-06 08:52:57 +00:00
class SupportDevelopmentActivity : AbsBaseActivity(), BillingProcessor.IBillingHandler {
lateinit var binding: ActivityDonationBinding
2020-01-03 12:40:42 +00:00
companion object {
val TAG: String = SupportDevelopmentActivity::class.java.simpleName
const val DONATION_PRODUCT_IDS = R.array.donation_ids
}
var billingProcessor: BillingProcessor? = null
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
onBackPressed()
return true
}
return super.onOptionsItemSelected(item)
}
fun donate(i: Int) {
val ids = resources.getStringArray(DONATION_PRODUCT_IDS)
billingProcessor?.purchase(this, ids[i])
2020-01-03 12:40:42 +00:00
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDonationBinding.inflate(layoutInflater)
2021-10-24 14:53:52 +00:00
setContentView(binding.root)
2020-01-03 12:40:42 +00:00
setStatusbarColorAuto()
setTaskDescriptionColorAuto()
setupToolbar()
billingProcessor = BillingProcessor(this, BuildConfig.GOOGLE_PLAY_LICENSING_KEY, this)
TintHelper.setTint(binding.progress, ThemeStore.accentColor(this))
binding.donation.setTextColor(ThemeStore.accentColor(this))
2020-01-03 12:40:42 +00:00
}
private fun setupToolbar() {
val toolbarColor = ATHUtil.resolveColor(this, R.attr.colorSurface)
binding.toolbar.setBackgroundColor(toolbarColor)
ToolbarContentTintHelper.colorBackButton(binding.toolbar)
setSupportActionBar(binding.toolbar)
2020-01-03 12:40:42 +00:00
}
override fun onBillingInitialized() {
loadSkuDetails()
}
private fun loadSkuDetails() {
2021-11-08 05:38:52 +00:00
binding.progressContainer.isVisible = true
binding.recyclerView.isVisible = false
val ids =
resources.getStringArray(DONATION_PRODUCT_IDS)
billingProcessor!!.getPurchaseListingDetailsAsync(
ArrayList(listOf(*ids)),
object : BillingProcessor.ISkuDetailsResponseListener {
override fun onSkuDetailsResponse(skuDetails: MutableList<SkuDetails>?) {
if (skuDetails == null || skuDetails.isEmpty()) {
binding.progressContainer.isVisible = false
return
}
binding.progressContainer.isVisible = false
binding.recyclerView.apply {
itemAnimator = DefaultItemAnimator()
layoutManager = GridLayoutManager(this@SupportDevelopmentActivity, 2)
adapter = SkuDetailsAdapter(this@SupportDevelopmentActivity, skuDetails)
isVisible = true
}
}
override fun onSkuDetailsError(error: String?) {
Log.e(TAG, error.toString())
}
})
}
override fun onProductPurchased(productId: String, details: PurchaseInfo?) {
2020-10-06 08:46:04 +00:00
// loadSkuDetails();
2020-01-03 12:40:42 +00:00
Toast.makeText(this, R.string.thank_you, Toast.LENGTH_SHORT).show()
}
override fun onBillingError(errorCode: Int, error: Throwable?) {
Log.e(TAG, "Billing error: code = $errorCode", error)
}
override fun onPurchaseHistoryRestored() {
2020-10-06 08:46:04 +00:00
// loadSkuDetails();
2020-01-03 12:40:42 +00:00
Toast.makeText(this, R.string.restored_previous_purchases, Toast.LENGTH_SHORT).show()
}
override fun onDestroy() {
billingProcessor?.release()
2020-01-03 12:40:42 +00:00
super.onDestroy()
}
2018-12-06 08:52:57 +00:00
}
2019-10-12 06:41:48 +00:00
class SkuDetailsAdapter(
2020-10-06 08:46:04 +00:00
private var donationsDialog: SupportDevelopmentActivity,
objects: List<SkuDetails>
2019-10-12 06:41:48 +00:00
) : RecyclerView.Adapter<SkuDetailsAdapter.ViewHolder>() {
2020-01-03 12:40:42 +00:00
private var skuDetailsList: List<SkuDetails> = ArrayList()
init {
skuDetailsList = objects
}
private fun getIcon(position: Int): Int {
return when (position) {
2020-07-19 21:00:30 +00:00
0 -> R.drawable.ic_cookie
1 -> R.drawable.ic_take_away
2 -> R.drawable.ic_take_away_coffe
3 -> R.drawable.ic_beer
4 -> R.drawable.ic_fast_food_meal
5 -> R.drawable.ic_popcorn
6 -> R.drawable.ic_card_giftcard
7 -> R.drawable.ic_food_croissant
else -> R.drawable.ic_card_giftcard
2020-01-03 12:40:42 +00:00
}
}
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(donationsDialog).inflate(
LAYOUT_RES_ID,
viewGroup,
false
)
)
}
override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
val skuDetails = skuDetailsList[i]
viewHolder.title.text = skuDetails.title.replace("Music Player - MP3 Player - Retro", "")
2020-01-03 12:40:42 +00:00
.trim { it <= ' ' }
viewHolder.text.text = skuDetails.description
2021-11-08 05:38:52 +00:00
viewHolder.text.isVisible = false
2020-01-03 12:40:42 +00:00
viewHolder.price.text = skuDetails.priceText
viewHolder.image.setImageResource(getIcon(i))
val purchased = donationsDialog.billingProcessor!!.isPurchased(skuDetails.productId)
val titleTextColor = if (purchased) ATHUtil.resolveColor(
donationsDialog,
android.R.attr.textColorHint
2020-05-20 20:28:38 +00:00
) else donationsDialog.textColorPrimary()
val contentTextColor =
2020-05-20 20:28:38 +00:00
if (purchased) titleTextColor else donationsDialog.textColorSecondary()
2020-01-03 12:40:42 +00:00
viewHolder.title.setTextColor(titleTextColor)
viewHolder.text.setTextColor(contentTextColor)
viewHolder.price.setTextColor(titleTextColor)
strikeThrough(viewHolder.title, purchased)
strikeThrough(viewHolder.text, purchased)
strikeThrough(viewHolder.price, purchased)
viewHolder.itemView.setOnTouchListener { _, _ -> purchased }
viewHolder.itemView.setOnClickListener { donationsDialog.donate(i) }
}
override fun getItemCount(): Int {
return skuDetailsList.size
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var title: TextView = view.findViewById(R.id.itemTitle)
var text: TextView = view.findViewById(R.id.itemText)
var price: TextView = view.findViewById(R.id.itemPrice)
var image: AppCompatImageView = view.findViewById(R.id.itemImage)
}
companion object {
@LayoutRes
private val LAYOUT_RES_ID = R.layout.item_donation_option
private fun strikeThrough(textView: TextView, strikeThrough: Boolean) {
textView.paintFlags =
if (strikeThrough) textView.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
else textView.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
2020-01-03 12:40:42 +00:00
}
}
2018-12-06 08:52:57 +00:00
}