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

373 lines
15 KiB
Kotlin
Raw Normal View History

2022-04-04 14:03:10 +00:00
package com.coldmint.rust.pro
2022-09-04 07:49:29 +00:00
import android.content.Intent
2022-04-04 14:03:10 +00:00
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-09-04 07:49:29 +00:00
import android.text.Editable
2022-04-04 14:03:10 +00:00
import android.text.SpannableString
import android.text.Spanned
2022-09-04 07:49:29 +00:00
import android.text.TextWatcher
2022-04-04 14:03:10 +00:00
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-09-04 07:49:29 +00:00
import com.coldmint.dialog.CoreDialog
2022-04-04 14:03:10 +00:00
import com.coldmint.rust.core.dataBean.OrderDataBean
import com.coldmint.rust.core.debug.LogCat
2022-04-04 14:03:10 +00:00
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-09-04 07:49:29 +00:00
val color by lazy {
GlobalMethod.getColorPrimary(this)
}
2022-04-04 14:03:10 +00:00
override fun whenCreateActivity(savedInstanceState: Bundle?, canUseView: Boolean) {
if (canUseView) {
2022-09-04 07:49:29 +00:00
title = getText(R.string.pay)
2022-04-04 14:03:10 +00:00
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")
2022-09-04 07:49:29 +00:00
Glide.with(this@PayActivity)
.load(hashMap[getString(R.string.wechat_pay)]).apply(GlobalMethod.getRequestOptions())
.into(viewBinding.baseImageView)
viewBinding.typeAutoCompleteTextView.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
2022-04-04 14:03:10 +00:00
2022-09-04 07:49:29 +00:00
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
2022-04-04 14:03:10 +00:00
2022-09-04 07:49:29 +00:00
override fun afterTextChanged(s: Editable?) {
val type = s.toString()
when (type) {
getString(R.string.qq_pay), getString(R.string.alipay), getString(R.string.wechat_pay) -> {
viewBinding.saveCode.text = getString(R.string.sava_code_and_copy_id)
viewBinding.cardView.isVisible = true
Glide.with(this@PayActivity)
.load(hashMap[type]).apply(GlobalMethod.getRequestOptions())
.into(viewBinding.baseImageView)
}
// getString(R.string.paypal) -> {
// viewBinding.cardView.isVisible = false
// viewBinding.saveCode.text = getString(R.string.paypal_payment)
// }
2022-04-04 14:03:10 +00:00
}
}
2022-09-04 07:49:29 +00:00
})
2022-04-04 14:03:10 +00:00
viewBinding.saveCode.setOnClickListener {
2022-09-04 07:49:29 +00:00
val text = viewBinding.saveCode.text.toString()
if (text == getString(R.string.paypal_payment)) {
//启动paypal
// val intent = Intent(this, BrowserActivity::class.java)
// intent.putExtra("link", "https://paypal.me/coldmint")
// startActivity(intent)
} else {
GlobalMethod.copyText(this, uuid)
val type = viewBinding.typeAutoCompleteTextView.text.toString()
val link = hashMap[type]
val appName = when (type) {
getString(R.string.qq_pay) -> {
getString(R.string.qq)
2022-04-04 14:03:10 +00:00
}
2022-09-04 07:49:29 +00:00
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) {
2022-04-04 14:03:10 +00:00
Snackbar.make(
viewBinding.saveCode,
R.string.file_download_fail,
Snackbar.LENGTH_SHORT
).show()
}
2022-09-04 07:49:29 +00:00
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()
2022-04-04 14:03:10 +00:00
}
}
2022-09-04 07:49:29 +00:00
})
}
AppOperator.updateTheAlbum(this, targetFile)
CoreDialog(this).setTitle(R.string.pay).setMessage(String.format(
getString(R.string.pay_tip2),
appName
)).setPositiveButton(R.string.dialog_ok){
val packName = when (type) {
getString(R.string.qq_pay) -> {
"com.tencent.mobileqq"
2022-04-04 14:03:10 +00:00
}
2022-09-04 07:49:29 +00:00
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()
}
}.setNegativeButton(R.string.dialog_cancel){
2022-04-04 14:03:10 +00:00
2022-09-04 07:49:29 +00:00
}.setCancelable(false).show()
}
2022-04-04 14:03:10 +00:00
}
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)
LogCat.d("", timeBuilder.toString())
2022-04-08 07:53:54 +00:00
}
override fun onFinish() {
past = true
2022-09-04 07:49:29 +00:00
viewBinding.typeAutoCompleteTextView.isEnabled = false
2022-04-08 07:53:54 +00:00
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) {
2022-09-04 07:49:29 +00:00
CoreDialog(this).setTitle(R.string.pay).setMessage(R.string.is_paid).setPositiveButton(R.string.paid_yes){
finish()
}.setNegativeButton(R.string.paid_no){
askingQuit()
}.setNeutralButton(R.string.paid_continue){
}.setCancelable(false).show()
2022-04-08 07:53:54 +00:00
}
}
}
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 {
2022-09-04 07:49:29 +00:00
CoreDialog(this).setTitle(R.string.paid_no).setMessage(String.format(
getString(R.string.preferential_price),
difference
)).setPositiveButton(R.string.dialog_ok){
countDownTimer?.cancel()
finish()
}.setNegativeButton(R.string.dialog_cancel){
}.setCancelable(false).show()
2022-04-08 07:53:54 +00:00
}
}
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)
2022-09-04 07:49:29 +00:00
val colorSpan = ForegroundColorSpan(color)
val colorSpan2 = ForegroundColorSpan(color)
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)
}
}