RustAssistant/app/src/main/java/com/coldmint/rust/pro/PayActivity.kt

351 lines
14 KiB
Kotlin
Raw Normal View History

2022-04-04 14:03:10 +00:00
package com.coldmint.rust.pro
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
2022-04-08 07:53:54 +00:00
import android.os.CountDownTimer
2022-04-04 14:03:10 +00:00
import android.text.SpannableString
import android.text.Spanned
import android.view.View
import android.widget.AdapterView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.coldmint.rust.core.web.ServerConfiguration
import com.coldmint.rust.pro.base.BaseActivity
import com.coldmint.rust.pro.databinding.ActivityPayBinding
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
2022-04-08 07:53:54 +00:00
import android.util.Log
import android.view.KeyEvent
2022-08-14 02:43:53 +00:00
import android.view.LayoutInflater
2022-04-08 07:53:54 +00:00
import android.view.MenuItem
import androidx.core.view.isVisible
2022-04-04 14:03:10 +00:00
import com.afollestad.materialdialogs.MaterialDialog
import com.coldmint.rust.core.dataBean.OrderDataBean
import com.coldmint.rust.core.interfaces.ApiCallBack
import com.coldmint.rust.core.tool.AppOperator
import com.coldmint.rust.core.tool.FileOperator
import com.coldmint.rust.core.web.ActivationApp
import com.coldmint.rust.pro.tool.AppSettings
import com.coldmint.rust.pro.tool.GlobalMethod
import com.google.android.material.snackbar.Snackbar
import okhttp3.*
import java.io.File
import java.io.IOException
/**
* @author Cold Mint
* @date 2022/1/11 13:38
*/
class PayActivity : BaseActivity<ActivityPayBinding>() {
val hashMap: HashMap<String, String> = HashMap()
2022-04-08 07:53:54 +00:00
//是否超时了
var past: Boolean = false
//是否第一次启动
var first = true
//优惠的价格(用于询问退出时使用)
var difference = 0.toDouble()
//倒计时器
var countDownTimer: CountDownTimer? = null
2022-04-04 14:03:10 +00:00
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
if (canUseView) {
viewBinding.toolbar.title = getText(R.string.pay)
setReturnButton()
val thisIntent = intent
val uuid = thisIntent.getStringExtra("uuid")
val account = thisIntent.getStringExtra("account")
if (uuid == null || account == null) {
showError("启动错误")
return
}
hashMap[getString(R.string.qq_pay)] =
ServerConfiguration.getRealLink("/resources/image/Payment/QQ.png")
hashMap[getString(R.string.alipay)] =
ServerConfiguration.getRealLink("/resources/image/Payment/Alipay.png")
hashMap[getString(R.string.wechat_pay)] =
ServerConfiguration.getRealLink("/resources/image/Payment/WeChat.png")
val array = resources.getStringArray(R.array.pay_type_entries)
viewBinding.typeSpinner.onItemSelectedListener =
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
when (val type = array[position]) {
getString(R.string.qq_pay), getString(R.string.alipay), getString(R.string.wechat_pay) -> {
Glide.with(this@PayActivity)
.load(hashMap[type]).apply(GlobalMethod.getRequestOptions())
.into(viewBinding.baseImageView)
}
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
viewBinding.saveCode.setOnClickListener {
GlobalMethod.copyText(this, uuid)
val type = array[viewBinding.typeSpinner.selectedItemPosition]
val link = hashMap[type]
val appName = when (type) {
getString(R.string.qq_pay) -> {
getString(R.string.qq)
}
getString(R.string.wechat_pay) -> {
getString(R.string.wechat)
}
else -> {
getString(R.string.alipay)
}
}
val targetFile = File(AppSettings.dataRootDirectory + "/pay/" + type + ".png")
//获取下载链接,保存二维码(如果二维码存在则不会保存)
if (link != null && !targetFile.exists()) {
val okHttpClient = ServerConfiguration.initOkHttpClient()
val request = Request.Builder()
.url(link).build()
val call = okHttpClient.newCall(request)
call.enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Snackbar.make(
viewBinding.saveCode,
R.string.file_download_fail,
Snackbar.LENGTH_SHORT
).show()
}
override fun onResponse(call: Call, response: Response) {
val body = response.body
if (body != null) {
val inputStream = body.byteStream()
FileOperator.copyFile(
inputStream,
targetFile
)
} else {
Snackbar.make(
viewBinding.saveCode,
R.string.file_download_fail,
Snackbar.LENGTH_SHORT
).show()
}
}
})
}
AppOperator.updateTheAlbum(this, targetFile)
MaterialDialog(this).show {
title(R.string.pay).message(
text = String.format(
getString(R.string.pay_tip2),
appName
)
)
.positiveButton(R.string.dialog_ok).positiveButton {
val packName = when (type) {
getString(R.string.qq_pay) -> {
"com.tencent.mobileqq"
}
getString(R.string.wechat_pay) -> {
"com.tencent.mm"
}
else -> {
"com.eg.android.AlipayGphone"
}
}
if (AppOperator.isAppInstalled(this@PayActivity, packName)) {
AppOperator.openApp(this@PayActivity, packName)
} else {
Snackbar.make(
viewBinding.saveCode,
String.format(getString(R.string.no_app_installed), appName),
Snackbar.LENGTH_SHORT
).show()
}
}.negativeButton(R.string.dialog_cancel).cancelable(false)
}
}
ActivationApp.instance.getOrderInfo(account, uuid, object : ApiCallBack<OrderDataBean> {
override fun onResponse(t: OrderDataBean) {
if (t.code == ServerConfiguration.Success_Code) {
val data = t.data
val stringBuilder = StringBuilder()
stringBuilder.append("订单名:")
stringBuilder.append(data.name)
stringBuilder.append("\n订单号:")
stringBuilder.append(data.flag)
stringBuilder.append("\n创建日期:")
stringBuilder.append(data.createTime)
stringBuilder.append("\n应付款:")
stringBuilder.append(data.price)
stringBuilder.append("")
if (data.originalPrice != data.price) {
stringBuilder.append("\n原价:")
stringBuilder.append(data.originalPrice)
stringBuilder.append("")
2022-04-08 07:53:54 +00:00
difference = data.originalPrice - data.price
2022-04-04 14:03:10 +00:00
}
viewBinding.info.text = stringBuilder.toString()
2022-04-08 07:53:54 +00:00
val timeBuilder = StringBuilder()
countDownTimer = object : CountDownTimer(600000, 1000) {
override fun onTick(p0: Long) {
//秒
timeBuilder.clear()
val second = p0 / 1000
if (second >= 60) {
val minute = second / 60
timeBuilder.append(minute)
timeBuilder.append("分钟")
timeBuilder.append(second % 60)
timeBuilder.append("")
} else {
timeBuilder.append(second)
timeBuilder.append("")
}
setMoney(timeBuilder.toString(), t.data.price)
Log.d("", timeBuilder.toString())
}
override fun onFinish() {
past = true
viewBinding.typeSpinner.isEnabled = false
viewBinding.baseImageView.isVisible = false
viewBinding.payMoneyView.text = getString(R.string.order_timeout)
viewBinding.saveCode.isEnabled = false
}
}
countDownTimer!!.start()
2022-04-04 14:03:10 +00:00
} else {
showError(t.message)
}
}
override fun onFailure(e: Exception) {
showInternetError(viewBinding.saveCode, e)
}
})
}
}
2022-04-08 07:53:54 +00:00
override fun onResume() {
super.onResume()
if (first) {
first = false
} else {
if (!past) {
MaterialDialog(this).show {
title(R.string.pay).message(R.string.is_paid).positiveButton(R.string.paid_yes)
.positiveButton {
finish()
}
.negativeButton(R.string.paid_no).negativeButton {
askingQuit()
}.cancelable(false).neutralButton(R.string.paid_continue)
}
}
}
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
askingQuit()
return true
}
return super.onKeyDown(keyCode, event)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == android.R.id.home) {
askingQuit()
return true
}
return super.onOptionsItemSelected(item)
}
/**
* 询问是否退出
*/
fun askingQuit() {
if (difference == 0.toDouble()) {
countDownTimer?.cancel()
finish()
} else {
MaterialDialog(this).show {
title(R.string.paid_no).message(
text = String.format(
getString(R.string.preferential_price),
difference
)
).positiveButton(R.string.dialog_ok).positiveButton {
countDownTimer?.cancel()
finish()
}.negativeButton(R.string.dialog_cancel).cancelable(false)
}
}
}
2022-04-04 14:03:10 +00:00
/**
2022-04-08 07:53:54 +00:00
* 设置显示的钱数以及剩余时间
* @param time String
2022-04-04 14:03:10 +00:00
* @param money Double
*/
2022-04-08 07:53:54 +00:00
fun setMoney(time: String, money: Double) {
2022-04-04 14:03:10 +00:00
val payMoney = getString(R.string.pay_tip)
val tipMoney = money.toString()
2022-04-08 07:53:54 +00:00
val tip = String.format(payMoney, time, tipMoney)
val start = tip.indexOf("") + 1
val end = tip.indexOf("")
val start2 = tip.indexOf("") + 1
val end2 = tip.indexOf("")
2022-04-04 14:03:10 +00:00
val spannableString = SpannableString(tip)
val colorSpan = ForegroundColorSpan(Color.parseColor("#0099EE"))
2022-04-08 07:53:54 +00:00
val colorSpan2 = ForegroundColorSpan(Color.parseColor("#0099EE"))
2022-04-04 14:03:10 +00:00
spannableString.setSpan(
colorSpan,
start,
2022-04-08 07:53:54 +00:00
end,
2022-04-04 14:03:10 +00:00
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(
StyleSpan(Typeface.BOLD),
start,
2022-04-08 07:53:54 +00:00
end,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(
colorSpan2,
start2,
end2,
2022-04-04 14:03:10 +00:00
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
2022-04-08 07:53:54 +00:00
)
spannableString.setSpan(
StyleSpan(Typeface.BOLD),
start2,
end2,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
2022-04-04 14:03:10 +00:00
viewBinding.payMoneyView.text = spannableString
}
2022-08-14 02:43:53 +00:00
override fun getViewBindingObject(layoutInflater: LayoutInflater): ActivityPayBinding {
2022-04-04 14:03:10 +00:00
return ActivityPayBinding.inflate(layoutInflater)
}
}