Updated on 2026-08-14

This commit is contained in:
Tangem 2018-08-07 19:55:14 +03:00
parent 4e1fb11d29
commit 0e0075a7e0
4 changed files with 239 additions and 22 deletions

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,232 @@
package com.tangem.presentation.activity
import android.app.Activity
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.nfc.NfcAdapter
import android.nfc.Tag
import android.nfc.tech.IsoDep
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.KeyEvent
import android.view.View
import android.widget.ProgressBar
import android.widget.Toast
import com.tangem.data.nfc.SignPaymentTask
import com.tangem.domain.cardReader.CardProtocol
import com.tangem.domain.cardReader.NfcManager
import com.tangem.domain.wallet.TangemCard
import com.tangem.presentation.dialog.NoExtendedLengthSupportDialog
import com.tangem.presentation.dialog.WaitSecurityDelayDialog
import com.tangem.util.Util
import com.tangem.wallet.R
import kotlinx.android.synthetic.main.activity_sign_payment.*
class SignPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback, CardProtocol.Notifications {
companion object {
val TAG: String = SignPaymentActivity::class.java.simpleName
const val EXTRA_AMOUNT = "Amount"
const val REQUEST_CODE_SEND_PAYMENT = 1
const val RESULT_INVALID_PIN = Activity.RESULT_FIRST_USER
}
private var nfcManager: NfcManager? = null
private var mCard: TangemCard? = null
private var signPaymentTask: SignPaymentTask? = null
private var amountStr: String? = null
private var feeStr: String? = null
private var outAddressStr: String? = null
private var lastReadSuccess = true
private var progressBar: ProgressBar? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sign_payment)
MainActivity.commonInit(applicationContext)
nfcManager = NfcManager(this, this)
mCard = TangemCard(intent.getStringExtra("UID"))
mCard!!.LoadFromBundle(intent.extras!!.getBundle("Card"))
amountStr = intent.getStringExtra(EXTRA_AMOUNT)
feeStr = intent.getStringExtra("Fee")
outAddressStr = intent.getStringExtra("Wallet")
tvCardID.text = mCard!!.cidDescription
progressBar = findViewById(R.id.progressBar)
progressBar!!.progressTintList = ColorStateList.valueOf(Color.DKGRAY)
progressBar!!.visibility = View.INVISIBLE
}
override fun onTagDiscovered(tag: Tag) {
try {
// get IsoDep handle and run cardReader thread
val isoDep = IsoDep.get(tag)
?: throw CardProtocol.TangemException(getString(R.string.wrong_tag_err))
val uid = tag.id
val sUID = Util.byteArrayToHexString(uid)
// Log.v(TAG, "UID: $sUID")
if (sUID == mCard!!.uid) {
if (lastReadSuccess) {
isoDep.timeout = mCard!!.pauseBeforePIN2 + 5000
} else {
isoDep.timeout = mCard!!.pauseBeforePIN2 + 65000
}
signPaymentTask = SignPaymentTask(this, mCard, nfcManager, isoDep, this, amountStr, feeStr, outAddressStr)
signPaymentTask!!.start()
} else {
// Log.d(TAG, "Mismatch card UID (" + sUID + " instead of " + mCard!!.uid + ")")
nfcManager!!.ignoreTag(isoDep.tag)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
public override fun onResume() {
super.onResume()
nfcManager!!.onResume()
}
public override fun onPause() {
nfcManager!!.onPause()
if (signPaymentTask != null)
signPaymentTask!!.cancel(true)
super.onPause()
}
public override fun onStop() {
// dismiss enable NFC dialog
nfcManager!!.onStop()
if (signPaymentTask != null)
signPaymentTask!!.cancel(true)
super.onStop()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (requestCode == REQUEST_CODE_SEND_PAYMENT) {
setResult(resultCode, data)
finish()
return
}
super.onActivityResult(requestCode, resultCode, data)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
when (keyCode) {
KeyEvent.KEYCODE_BACK -> {
val intent = Intent()
setResult(Activity.RESULT_CANCELED, intent)
finish()
return true
}
}
return super.onKeyDown(keyCode, event)
}
override fun OnReadStart(cardProtocol: CardProtocol) {
progressBar!!.post {
progressBar!!.visibility = View.VISIBLE
progressBar!!.progress = 5
}
}
override fun OnReadFinish(cardProtocol: CardProtocol?) {
signPaymentTask = null
if (cardProtocol != null) {
if (cardProtocol.error == null) {
progressBar!!.post {
progressBar!!.progress = 100
progressBar!!.progressTintList = ColorStateList.valueOf(Color.GREEN)
}
} else {
lastReadSuccess = false
if (cardProtocol.error.javaClass == CardProtocol.TangemException_InvalidPIN::class.java) {
progressBar!!.post {
progressBar!!.progress = 100
progressBar!!.progressTintList = ColorStateList.valueOf(Color.RED)
}
progressBar!!.postDelayed({
try {
progressBar!!.progress = 0
progressBar!!.progressTintList = ColorStateList.valueOf(Color.DKGRAY)
progressBar!!.visibility = View.INVISIBLE
val intent = Intent()
intent.putExtra("message", "Cannot sign transaction. Make sure you enter correct PIN2!")
intent.putExtra("UID", cardProtocol.card.uid)
intent.putExtra("Card", cardProtocol.card.asBundle)
setResult(RESULT_INVALID_PIN, intent)
finish()
} catch (e: Exception) {
e.printStackTrace()
}
}, 500)
} else {
progressBar!!.post {
if (cardProtocol.error is CardProtocol.TangemException_ExtendedLengthNotSupported) {
if (!NoExtendedLengthSupportDialog.allReadyShowed) {
NoExtendedLengthSupportDialog().show(fragmentManager, NoExtendedLengthSupportDialog.TAG)
}
} else {
Toast.makeText(baseContext, R.string.try_to_scan_again, Toast.LENGTH_LONG).show()
}
progressBar!!.progress = 100
progressBar!!.progressTintList = ColorStateList.valueOf(Color.RED)
}
}
}
}
progressBar!!.postDelayed({
try {
progressBar!!.progress = 0
progressBar!!.progressTintList = ColorStateList.valueOf(Color.DKGRAY)
progressBar!!.visibility = View.INVISIBLE
} catch (e: Exception) {
e.printStackTrace()
}
}, 500)
}
override fun OnReadProgress(protocol: CardProtocol, progress: Int) {
progressBar!!.post { progressBar!!.progress = progress }
}
override fun OnReadCancel() {
signPaymentTask = null
progressBar!!.postDelayed({
try {
progressBar!!.progress = 0
progressBar!!.progressTintList = ColorStateList.valueOf(Color.DKGRAY)
progressBar!!.visibility = View.INVISIBLE
} catch (e: Exception) {
e.printStackTrace()
}
}, 500)
}
override fun OnReadWait(msec: Int) {
WaitSecurityDelayDialog.OnReadWait(this, msec)
}
override fun OnReadBeforeRequest(timeout: Int) {
WaitSecurityDelayDialog.onReadBeforeRequest(this, timeout)
}
override fun OnReadAfterRequest() {
WaitSecurityDelayDialog.onReadAfterRequest(this)
}
}

View file

@ -91,18 +91,16 @@ class SwapPINActivity : AppCompatActivity(), NfcAdapter.ReaderCallback, CardProt
public override fun onPause() {
nfcManager!!.onPause()
if (swapPinTask != null) {
if (swapPinTask != null)
swapPinTask!!.cancel(true)
}
super.onPause()
}
public override fun onStop() {
// dismiss enable NFC dialog
nfcManager!!.onStop()
if (swapPinTask != null) {
if (swapPinTask != null)
swapPinTask!!.cancel(true)
}
super.onStop()
}

View file

@ -1,7 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
@ -14,13 +12,12 @@
android:orientation="vertical">
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/maax"
android:text="@string/now_touch_the_banknote_with_id"
android:textAlignment="center"
android:textSize="@dimen/text_size_medium"/>
android:textSize="@dimen/text_size_medium" />
<TextView
android:id="@+id/tvCardID"
@ -32,27 +29,18 @@
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="@dimen/text_size_large"
tools:text="24234234"/>
tools:text="CB02 0000 0002 5000" />
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/maax"
android:text="@string/to_sign_the_payment"
android:textAlignment="center"
android:textSize="@dimen/text_size_medium"/>
android:textSize="@dimen/text_size_medium" />
</LinearLayout>
<ProgressBar
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="16dp"
android:layout_alignParentBottom="true"
android:alpha="0.5"
android:elevation="1dp"
app:layout_constraintBottom_toBottomOf="parent"/>
<include layout="@layout/layout_progress_horizontal" />
</RelativeLayout>