From 1c2dec4cb6ee1ca8d8e751a2c25a963996c9e12b Mon Sep 17 00:00:00 2001 From: Tangem Date: Fri, 19 Oct 2018 13:39:22 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tangem/data/network/ServerApiHelper.java | 13 +- .../com/tangem/domain/wallet/SharedData.java | 44 ---- .../activity/ConfirmPaymentActivity.kt | 192 +++++++++--------- .../res/layout/activity_confirm_payment.xml | 18 +- app/src/main/res/values/strings.xml | 2 +- .../res/values/strings_untranslatable.xml | 1 + 6 files changed, 111 insertions(+), 159 deletions(-) delete mode 100644 app/src/main/java/com/tangem/domain/wallet/SharedData.java diff --git a/app/src/main/java/com/tangem/data/network/ServerApiHelper.java b/app/src/main/java/com/tangem/data/network/ServerApiHelper.java index 520466bc11..262a5d4ddb 100644 --- a/app/src/main/java/com/tangem/data/network/ServerApiHelper.java +++ b/app/src/main/java/com/tangem/data/network/ServerApiHelper.java @@ -37,7 +37,8 @@ public class ServerApiHelper { private EstimateFeeListener estimateFeeListener; public interface EstimateFeeListener { - void onInfuraEthGasPrice(int blockCount, String estimateFeeResponse); + void onSuccess(int blockCount, String estimateFeeResponse); + void onFail(String message); } public void setEstimateFee(EstimateFeeListener listener) { @@ -69,14 +70,16 @@ public class ServerApiHelper { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.code() == 200) { - estimateFeeListener.onInfuraEthGasPrice(blockCount, response.body()); + estimateFeeListener.onSuccess(blockCount, response.body()); Log.i(TAG, "estimateFee onResponse " + response.code() + " " + response.body()); } else - Log.e(TAG, "estimateFee onResponse " + response.code()); + estimateFeeListener.onFail(response.body()); + Log.e(TAG, "estimateFee onResponse " + response.code()); } @Override public void onFailure(@NonNull Call call, @NonNull Throwable t) { + estimateFeeListener.onFail(t.getMessage()); Log.e(TAG, "estimateFee onFailure " + t.getMessage()); } }); @@ -291,7 +294,7 @@ public class ServerApiHelper { private LastVersionListener lastVersionListener; public interface LastVersionListener { - void onLastVersion(String lastVersion); + void onSuccess(String lastVersion); } public void setLastVersionListener(LastVersionListener listener) { @@ -310,7 +313,7 @@ public class ServerApiHelper { String stringResponse; try { stringResponse = response.body() != null ? response.body().string() : null; - lastVersionListener.onLastVersion(stringResponse); + lastVersionListener.onSuccess(stringResponse); } catch (Exception e) { e.printStackTrace(); } diff --git a/app/src/main/java/com/tangem/domain/wallet/SharedData.java b/app/src/main/java/com/tangem/domain/wallet/SharedData.java deleted file mode 100644 index d026e13dd4..0000000000 --- a/app/src/main/java/com/tangem/domain/wallet/SharedData.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.tangem.domain.wallet; - -import java.math.BigDecimal; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * Created by Ilia on 12.04.2018. - */ - -public class SharedData { - public static int COUNT_REQUEST = 3; - public AtomicInteger requestCounter; - public int allRequest; - public AtomicInteger errorRequest; - private BigDecimal payload; - - public SharedData(int requestCount) { - payload = BigDecimal.ZERO; - allRequest = requestCount; - errorRequest = new AtomicInteger(0); - requestCounter = new AtomicInteger(0); - } - - public synchronized boolean updatePayload(BigDecimal value) { - if (payload == null || payload.compareTo(value) != 0) { - boolean isChange = true; - if (payload == null || payload.equals(BigDecimal.ZERO)) - isChange = false; - if (!value.equals(BigDecimal.ZERO)) - payload = value; - else - isChange = false; - - return isChange; - } - - return false; - } - - public void setErrorRequest(int val) { - errorRequest = new AtomicInteger(val); - } - -} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt b/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt index a26e9e2c01..17518b3fbd 100644 --- a/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt +++ b/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.kt @@ -43,11 +43,6 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { private var card: TangemCard? = null private var feeRequestSuccess = false private var balanceRequestSuccess = false - private var etAmount: EditText? = null - private var etFee: EditText? = null - private var rgFee: RadioGroup? = null - private var progressBar: ProgressBar? = null - private var btnSend: Button? = null private var minFee: String? = null private var maxFee: String? = null private var normalFee: String? = null @@ -69,15 +64,10 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { card = TangemCard(intent.getStringExtra("UID")) card!!.loadFromBundle(intent.extras!!.getBundle("Card")) - progressBar = findViewById(R.id.progressBar) - btnSend = findViewById(R.id.btnSend) - etAmount = findViewById(R.id.etAmount) - etFee = findViewById(R.id.etFee) - rgFee = findViewById(R.id.rgFee) - val engine = CoinEngineFactory.create(card!!.blockchain) val balanceWithAlter = engine!!.getBalanceWithAlter(card).replace(",", ".") + if (card!!.blockchain == Blockchain.Token) { val html = Html.fromHtml(balanceWithAlter) tvBalance.text = html @@ -85,60 +75,53 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { tvBalance.text = balanceWithAlter isIncludeFee = intent.getBooleanExtra("IncFee", true) + if (isIncludeFee) tvIncFee.setText(R.string.including_fee) else tvIncFee.setText(R.string.not_including_fee) - if (card!!.blockchain == Blockchain.Token) tvIncFee.visibility = View.INVISIBLE else tvIncFee.visibility = View.VISIBLE - - etAmount!!.setText(intent.getStringExtra(SignPaymentActivity.EXTRA_AMOUNT)) + etAmount.setText(intent.getStringExtra(SignPaymentActivity.EXTRA_AMOUNT)) tvCurrency.text = engine.getBalanceCurrency(card) tvCurrency2.text = engine.feeCurrency - tvCardID.text = card!!.cidDescription - etWallet.setText(intent.getStringExtra("Wallet")) + etFee.setText("") - etFee!!.setText("") - - btnSend!!.visibility = View.INVISIBLE + btnSend.visibility = View.INVISIBLE feeRequestSuccess = false balanceRequestSuccess = false if (card!!.blockchain == Blockchain.Ethereum || card!!.blockchain == Blockchain.EthereumTestNet || card!!.blockchain == Blockchain.Token) { - rgFee!!.isEnabled = false + rgFee.isEnabled = false requestInfura(ServerApiHelper.INFURA_ETH_GAS_PRICE) } else { - rgFee!!.isEnabled = true + rgFee.isEnabled = true requestElectrum(card!!, ElectrumRequest.checkBalance(card!!.wallet)) calcSize = 256 try { - calcSize = buildSize(etWallet!!.text.toString(), "0.00", etAmount!!.text.toString()) + calcSize = buildSize(etWallet!!.text.toString(), "0.00", etAmount.text.toString()) } catch (ex: Exception) { Log.e("Build Fee error", ex.message) } card!!.resetFailedBalanceRequestCounter() - val sharedFee = SharedData(SharedData.COUNT_REQUEST) - progressBar!!.visibility = View.VISIBLE + progressBar.visibility = View.VISIBLE - serverApiHelper.estimateFee(ServerApiHelper.ESTIMATE_FEE_PRIORITY) - serverApiHelper.estimateFee(ServerApiHelper.ESTIMATE_FEE_NORMAL) - serverApiHelper.estimateFee(ServerApiHelper.ESTIMATE_FEE_MINIMAL) + requestEstimateFee() } // set listeners - rgFee!!.setOnCheckedChangeListener { _, checkedId -> doSetFee(checkedId) } - etFee!!.addTextChangedListener(object : TextWatcher { + rgFee.setOnCheckedChangeListener { _, checkedId -> doSetFee(checkedId) } + etFee.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { } @@ -166,7 +149,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { } }) - btnSend!!.setOnClickListener { + btnSend.setOnClickListener { val calendar = Calendar.getInstance() calendar.add(Calendar.MINUTE, -1) @@ -182,8 +165,8 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { return@setOnClickListener } - val txFee = etFee!!.text.toString() - val txAmount = etAmount!!.text.toString() + val txFee = etFee.text.toString() + val txAmount = etAmount.text.toString() if (!engineCoin.hasBalanceInfo(card)) { finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_check_balance_no_connection_with_blockchain_nodes)) @@ -206,8 +189,8 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { requestPIN2Count = 0 val intent = Intent(baseContext, PinRequestActivity::class.java) intent.putExtra("mode", PinRequestActivity.Mode.RequestPIN2.toString()) - intent.putExtra("UID", card!!.uid) - intent.putExtra("Card", card!!.asBundle) + intent.putExtra(TangemCard.EXTRA_UID, card!!.uid) + intent.putExtra(TangemCard.EXTRA_CARD, card!!.asBundle) intent.putExtra("IncFee", isIncludeFee) startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2) } @@ -217,82 +200,33 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { override fun onSuccess(electrumRequest: ElectrumRequest?) { if (electrumRequest!!.isMethod(ElectrumRequest.METHOD_GetBalance)) { try { - etFee!!.setText("--") - if ((electrumRequest.result.getInt("confirmed") + electrumRequest.result.getInt("unconfirmed")) / card!!.blockchain.multiplier * 1000000.0 < java.lang.Float.parseFloat(etAmount!!.text.toString())) { - etFee!!.error = "Not enough funds" + etFee.setText(getString(R.string.empty)) + if ((electrumRequest.result.getInt("confirmed") + electrumRequest.result.getInt("unconfirmed")) / card!!.blockchain.multiplier * 1000000.0 < java.lang.Float.parseFloat(etAmount.text.toString())) { + etFee.error = getString(R.string.not_enough_funds) } else { - etFee!!.error = null + etFee.error = null balanceRequestSuccess = true if (feeRequestSuccess && balanceRequestSuccess) { - btnSend!!.visibility = View.VISIBLE + btnSend.visibility = View.VISIBLE } dtVerified = Date() nodeCheck = true } } catch (e: JSONException) { e.printStackTrace() - finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_check_balance_no_connection_with_blockchain_nodes)) + requestElectrum(card!!, ElectrumRequest.checkBalance(card!!.wallet)) } } } override fun onFail(message: String?) { - + finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_check_balance_no_connection_with_blockchain_nodes)) } } - serverApiHelperElectrum.setElectrumRequestData(electrumBodyListener) - // request estimate fee listener - serverApiHelper.setEstimateFee { blockCount, estimateFeeResponse -> - var fee: BigDecimal? - fee = BigDecimal(estimateFeeResponse) // BTC per 1 kb - - if (fee == BigDecimal.ZERO) { - progressBar!!.visibility = View.INVISIBLE - finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_calculate_fee_wrong_data_received_from_node)) - } - - if (calcSize.toLong() != 0L) { - fee = fee.multiply(BigDecimal(calcSize.toLong())).divide(BigDecimal(1024)) // per Kb -> per byte - } else { - finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_calculate_fee_tx_length_unknown)) - } - - progressBar!!.visibility = View.INVISIBLE - - val df = DecimalFormat() - df.maximumFractionDigits = 7 - df.minimumFractionDigits = 3 - df.isGroupingUsed = false - val strFee = df.format(fee) - - when (blockCount) { - ServerApiHelper.ESTIMATE_FEE_MINIMAL -> { - minFee = strFee - minFeeInInternalUnits = card!!.internalUnitsFromString(strFee) - } - - ServerApiHelper.ESTIMATE_FEE_NORMAL -> { - normalFee = strFee - - doSetFee(rgFee!!.checkedRadioButtonId) - } - - ServerApiHelper.ESTIMATE_FEE_PRIORITY -> { - maxFee = strFee - } - } - - etFee!!.error = null - feeRequestSuccess = true - if (feeRequestSuccess && balanceRequestSuccess) - btnSend!!.visibility = View.VISIBLE - dtVerified = Date() - } - - // request eth gasPrice listener + // request infura eth gasPrice listener val infuraBodyListener: ServerApiHelper.InfuraBodyListener = object : ServerApiHelper.InfuraBodyListener { override fun onSuccess(method: String, infuraResponse: InfuraResponse) { when (method) { @@ -310,15 +244,13 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { minFee = minFeeInGwei normalFee = normalFeeInGwei maxFee = maxFeeInGwei - etFee!!.setText(normalFeeInGwei.replace(',', '.')) - etFee!!.error = null - btnSend!!.visibility = View.VISIBLE + etFee.setText(normalFeeInGwei.replace(',', '.')) + etFee.error = null + btnSend.visibility = View.VISIBLE feeRequestSuccess = true balanceRequestSuccess = true dtVerified = Date() minFeeInInternalUnits = card!!.internalUnitsFromString(normalFeeInGwei) - -// Log.i("eth_gas_price", gasPrice) } } } @@ -331,8 +263,62 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { } } } - serverApiHelper.setInfuraResponse(infuraBodyListener) + + // request estimate fee listener + val estimateFeeListener: ServerApiHelper.EstimateFeeListener = object : ServerApiHelper.EstimateFeeListener { + override fun onSuccess(blockCount: Int, estimateFeeResponse: String?) { + var fee: BigDecimal? + fee = BigDecimal(estimateFeeResponse) // BTC per 1 kb + + if (fee == BigDecimal.ZERO) { + progressBar.visibility = View.INVISIBLE + requestEstimateFee() + } + + if (calcSize.toLong() != 0L) { + fee = fee.multiply(BigDecimal(calcSize.toLong())).divide(BigDecimal(1024)) // per Kb -> per byte + } else { + requestEstimateFee() + } + + progressBar.visibility = View.INVISIBLE + + val df = DecimalFormat() + df.maximumFractionDigits = 7 + df.minimumFractionDigits = 3 + df.isGroupingUsed = false + val strFee = df.format(fee) + + when (blockCount) { + ServerApiHelper.ESTIMATE_FEE_MINIMAL -> { + minFee = strFee + minFeeInInternalUnits = card!!.internalUnitsFromString(strFee) + } + + ServerApiHelper.ESTIMATE_FEE_NORMAL -> { + normalFee = strFee + + doSetFee(rgFee.checkedRadioButtonId) + } + + ServerApiHelper.ESTIMATE_FEE_PRIORITY -> { + maxFee = strFee + } + } + + etFee.error = null + feeRequestSuccess = true + if (feeRequestSuccess && balanceRequestSuccess) + btnSend.visibility = View.VISIBLE + dtVerified = Date() + } + + override fun onFail(message: String?) { + finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_calculate_fee_wrong_data_received_from_node)) + } + } + serverApiHelper.setEstimateFee(estimateFeeListener) } public override fun onResume() { @@ -378,8 +364,8 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { intent.putExtra("UID", card!!.uid) intent.putExtra("Card", card!!.asBundle) intent.putExtra("Wallet", etWallet!!.text.toString()) - intent.putExtra(SignPaymentActivity.EXTRA_AMOUNT, etAmount!!.text.toString()) - intent.putExtra("Fee", etFee!!.text.toString()) + intent.putExtra(SignPaymentActivity.EXTRA_AMOUNT, etAmount.text.toString()) + intent.putExtra("Fee", etFee.text.toString()) intent.putExtra("IncFee", isIncludeFee) startActivityForResult(intent, REQUEST_CODE_SIGN_PAYMENT) } else @@ -485,6 +471,12 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain)) } + private fun requestEstimateFee() { + serverApiHelper.estimateFee(ServerApiHelper.ESTIMATE_FEE_PRIORITY) + serverApiHelper.estimateFee(ServerApiHelper.ESTIMATE_FEE_NORMAL) + serverApiHelper.estimateFee(ServerApiHelper.ESTIMATE_FEE_MINIMAL) + } + private fun doSetFee(checkedRadioButtonId: Int) { var txtFee = "" when (checkedRadioButtonId) { @@ -504,7 +496,7 @@ class ConfirmPaymentActivity : AppCompatActivity(), NfcAdapter.ReaderCallback { else finishWithError(Activity.RESULT_CANCELED, getString(R.string.cannot_obtain_data_from_blockchain)) } - etFee!!.setText(txtFee.replace(',', '.')) + etFee.setText(txtFee.replace(',', '.')) } private fun finishWithError(errorCode: Int, message: String) { diff --git a/app/src/main/res/layout/activity_confirm_payment.xml b/app/src/main/res/layout/activity_confirm_payment.xml index 143db52d65..a662374ce8 100644 --- a/app/src/main/res/layout/activity_confirm_payment.xml +++ b/app/src/main/res/layout/activity_confirm_payment.xml @@ -11,8 +11,8 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:fontFamily="@font/maax" - android:paddingBottom="4dp" android:paddingTop="8dp" + android:paddingBottom="4dp" android:text="@string/send_payment" android:textAlignment="center" android:textColor="@color/primary" @@ -27,10 +27,10 @@ android:layout_height="wrap_content" android:background="@color/btn_light" android:orientation="vertical" - android:paddingBottom="8dp" - android:paddingEnd="5dp" android:paddingStart="5dp" android:paddingTop="8dp" + android:paddingEnd="5dp" + android:paddingBottom="8dp" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" @@ -133,8 +133,8 @@ android:id="@+id/tvIncFee" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginBottom="1dp" android:layout_marginStart="5dp" + android:layout_marginBottom="1dp" android:fontFamily="@font/maax" android:textColor="@color/primary" android:textSize="14sp" @@ -169,10 +169,10 @@ android:id="@+id/tvCurrency" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginBottom="8dp" - android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" + android:layout_marginEnd="8dp" + android:layout_marginBottom="8dp" android:fontFamily="@font/maax" android:text="@string/m_btc" android:textSize="@dimen/text_size_large" @@ -201,9 +201,9 @@ android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginBottom="4dp" android:layout_marginStart="7dp" android:layout_marginTop="16dp" + android:layout_marginBottom="4dp" android:fontFamily="@font/maax" android:text="@string/fee" android:textColor="@color/primary" @@ -281,8 +281,8 @@ android:id="@+id/tvCurrency2" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginBottom="8dp" android:layout_marginTop="8dp" + android:layout_marginBottom="8dp" android:fontFamily="@font/maax" android:text="@string/m_btc" android:textSize="@dimen/text_size_medium" @@ -295,8 +295,8 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/maax" - android:paddingEnd="10dp" android:paddingStart="10dp" + android:paddingEnd="10dp" android:textColor="@android:color/darker_gray" android:textSize="@dimen/text_size_1_small" app:layout_constraintBottom_toBottomOf="parent" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6e5e809db7..2449413874 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -189,7 +189,7 @@ Please wait for confirmation of incoming transaction Not enough ETH funds for gas PIN2 is required to sign the payment - + Not enough funds Wallet hasn\'t been yet created diff --git a/app/src/main/res/values/strings_untranslatable.xml b/app/src/main/res/values/strings_untranslatable.xml index 136b2f86e7..e49e137b5e 100644 --- a/app/src/main/res/values/strings_untranslatable.xml +++ b/app/src/main/res/values/strings_untranslatable.xml @@ -8,6 +8,7 @@ -- -- -- ---- ---- ---- ---- < + -- 1 2 3