From 0fab3ec90ef91dd934eac77ccb75a8729d60bfdc Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 27 Jun 2018 23:04:50 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tangem/data/nfc/CreateNewWalletTask.java | 100 ++++++ .../java/com/tangem/data/nfc/PurgeTask.java | 101 ++++++ .../data/{task => nfc}/ReadCardInfoTask.java | 6 +- .../com/tangem/data/nfc/SignPaymentTask.java | 150 +++++++++ .../java/com/tangem/data/nfc/SwapPINTask.java | 105 ++++++ .../data/{task => nfc}/VerifyCardTask.java | 6 +- .../activity/CreateNewWalletActivity.java | 172 +++++----- .../activity/EmptyWalletActivity.java | 2 +- .../presentation/activity/PurgeActivity.java | 167 +++++----- .../activity/SignPaymentActivity.java | 2 +- .../activity/SwapPINActivity.java | 313 ++++++++---------- .../presentation/fragment/LoadedWallet.java | 2 +- .../tangem/presentation/fragment/Main.java | 2 +- 13 files changed, 777 insertions(+), 351 deletions(-) create mode 100644 app/src/main/java/com/tangem/data/nfc/CreateNewWalletTask.java create mode 100644 app/src/main/java/com/tangem/data/nfc/PurgeTask.java rename app/src/main/java/com/tangem/data/{task => nfc}/ReadCardInfoTask.java (98%) create mode 100644 app/src/main/java/com/tangem/data/nfc/SignPaymentTask.java create mode 100644 app/src/main/java/com/tangem/data/nfc/SwapPINTask.java rename app/src/main/java/com/tangem/data/{task => nfc}/VerifyCardTask.java (96%) diff --git a/app/src/main/java/com/tangem/data/nfc/CreateNewWalletTask.java b/app/src/main/java/com/tangem/data/nfc/CreateNewWalletTask.java new file mode 100644 index 0000000000..bdb5de913f --- /dev/null +++ b/app/src/main/java/com/tangem/data/nfc/CreateNewWalletTask.java @@ -0,0 +1,100 @@ +package com.tangem.data.nfc; + +import android.content.Context; +import android.nfc.tech.IsoDep; +import android.util.Log; + +import com.tangem.domain.cardReader.CardProtocol; +import com.tangem.domain.cardReader.NfcManager; +import com.tangem.domain.wallet.PINStorage; +import com.tangem.domain.wallet.TangemCard; + +public class CreateNewWalletTask extends Thread { + public static final String TAG = CreateNewWalletTask.class.getSimpleName(); + + private Context mContext; + private TangemCard mCard; + private NfcManager mNfcManager; + private IsoDep mIsoDep; + private CardProtocol.Notifications mNotifications; + private boolean isCancelled = false; + + public CreateNewWalletTask(Context context, TangemCard card, NfcManager nfcManager, IsoDep isoDep, CardProtocol.Notifications notifications) { + mCard = card; + mContext = context; + mNfcManager = nfcManager; + mIsoDep = isoDep; + mNotifications = notifications; + } + + @Override + public void run() { + if (mIsoDep == null) { + return; + } + CardProtocol protocol = new CardProtocol(mContext, mIsoDep, mCard, mNotifications); + mNotifications.OnReadStart(protocol); + try { + // for Samsung's bugs - + // Workaround for the Samsung Galaxy S5 (since the + // first connection always hangs on transceive). + int timeout = mIsoDep.getTimeout(); + mIsoDep.connect(); + mIsoDep.close(); + mIsoDep.connect(); + mIsoDep.setTimeout(timeout); + try { + mNotifications.OnReadProgress(protocol, 5); + + Log.i(TAG, "[-- Start create new wallet --]"); + + if (isCancelled) return; + protocol.run_VerifyCard(); + + Log.i(TAG, "Manufacturer: " + protocol.getCard().getManufacturer().getOfficialName()); + + mNotifications.OnReadProgress(protocol, 30); + if (isCancelled) return; + +// if (mCard.getPauseBeforePIN2() > 0) { +// mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); +// } +// try { + protocol.run_CreateWallet(PINStorage.getPIN2()); +// } finally { +// mNotifications.OnReadWait(0); +// } + mNotifications.OnReadProgress(protocol, 60); + if (isCancelled) return; + + protocol.run_Read(); + + } finally { + mNfcManager.ignoreTag(mIsoDep.getTag()); + } + } catch (Exception e) { + e.printStackTrace(); + protocol.setError(e); + + } finally { + Log.i(TAG, "[-- Finish create new wallet --]"); + mNotifications.OnReadFinish(protocol); + } + } + + public void cancel(Boolean AllowInterrupt) { + try { + if (isAlive()) { + isCancelled = true; + join(500); + } + if (isAlive() && AllowInterrupt) { + interrupt(); + mNotifications.OnReadCancel(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/data/nfc/PurgeTask.java b/app/src/main/java/com/tangem/data/nfc/PurgeTask.java new file mode 100644 index 0000000000..e95b385e41 --- /dev/null +++ b/app/src/main/java/com/tangem/data/nfc/PurgeTask.java @@ -0,0 +1,101 @@ +package com.tangem.data.nfc; + +import android.content.Context; +import android.nfc.tech.IsoDep; +import android.util.Log; + +import com.tangem.domain.cardReader.CardProtocol; +import com.tangem.domain.cardReader.NfcManager; +import com.tangem.domain.wallet.PINStorage; +import com.tangem.domain.wallet.TangemCard; + +public class PurgeTask extends Thread { + public static final String TAG = PurgeTask.class.getSimpleName(); + + private String txOutAddress; + private Context mContext; + private TangemCard mCard; + private NfcManager mNfcManager; + private IsoDep mIsoDep; + private CardProtocol.Notifications mNotifications; + private boolean isCancelled = false; + + public PurgeTask(Context context, TangemCard card, NfcManager nfcManager, IsoDep isoDep, CardProtocol.Notifications notifications) { + mCard = card; + mContext = context; + mNfcManager = nfcManager; + mIsoDep = isoDep; + mNotifications = notifications; + } + + @Override + public void run() { + if (mIsoDep == null) { + return; + } + CardProtocol protocol = new CardProtocol(mContext, mIsoDep, mCard, mNotifications); + + mNotifications.OnReadStart(protocol); + try { + + // for Samsung's bugs - Workaround for the Samsung Galaxy S5 (since the first connection always hangs on transceive). + int timeout = mIsoDep.getTimeout(); + mIsoDep.connect(); + mIsoDep.close(); + mIsoDep.connect(); + mIsoDep.setTimeout(timeout); + try { + mNotifications.OnReadProgress(protocol, 5); + + Log.i(TAG, "[-- Start purge --]"); + + if (isCancelled) return; + + if (mCard.getPauseBeforePIN2() > 0) { + mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); + } + +// try { + protocol.run_PurgeWallet(PINStorage.getPIN2()); +// } finally { +// mNotifications.OnReadWait(0); +// } + + mNotifications.OnReadProgress(protocol, 50); + + protocol.run_Read(); + + mNotifications.OnReadProgress(protocol, 100); + + if (isCancelled) + return; + + } finally { + mNfcManager.ignoreTag(mIsoDep.getTag()); + } + } catch (Exception e) { + e.printStackTrace(); + protocol.setError(e); + + } finally { + Log.i(TAG, "[-- Finish purge --]"); + mNotifications.OnReadFinish(protocol); + } + } + + public void cancel(Boolean AllowInterrupt) { + try { + if (isAlive()) { + isCancelled = true; + join(500); + } + if (isAlive() && AllowInterrupt) { + interrupt(); + mNotifications.OnReadCancel(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/data/task/ReadCardInfoTask.java b/app/src/main/java/com/tangem/data/nfc/ReadCardInfoTask.java similarity index 98% rename from app/src/main/java/com/tangem/data/task/ReadCardInfoTask.java rename to app/src/main/java/com/tangem/data/nfc/ReadCardInfoTask.java index 0e3c070512..34819b715f 100644 --- a/app/src/main/java/com/tangem/data/task/ReadCardInfoTask.java +++ b/app/src/main/java/com/tangem/data/nfc/ReadCardInfoTask.java @@ -1,4 +1,4 @@ -package com.tangem.data.task; +package com.tangem.data.nfc; import android.content.Context; import android.nfc.tech.IsoDep; @@ -151,11 +151,11 @@ public class ReadCardInfoTask extends Thread { public void cancel(Boolean AllowInterrupt) { try { - if (this.isAlive()) { + if (isAlive()) { isCancelled = true; join(500); } - if (this.isAlive() && AllowInterrupt) { + if (isAlive() && AllowInterrupt) { interrupt(); mNotifications.OnReadCancel(); } diff --git a/app/src/main/java/com/tangem/data/nfc/SignPaymentTask.java b/app/src/main/java/com/tangem/data/nfc/SignPaymentTask.java new file mode 100644 index 0000000000..d72ddc9397 --- /dev/null +++ b/app/src/main/java/com/tangem/data/nfc/SignPaymentTask.java @@ -0,0 +1,150 @@ +package com.tangem.data.nfc; + +import android.app.Activity; +import android.content.Intent; +import android.nfc.tech.IsoDep; +import android.util.Log; + +import com.tangem.domain.cardReader.CardProtocol; +import com.tangem.domain.cardReader.NfcManager; +import com.tangem.domain.wallet.Blockchain; +import com.tangem.domain.wallet.CoinEngine; +import com.tangem.domain.wallet.CoinEngineFactory; +import com.tangem.domain.wallet.LastSignStorage; +import com.tangem.domain.wallet.TangemCard; +import com.tangem.presentation.activity.SendTransactionActivity; +import com.tangem.presentation.activity.SignPaymentActivity; +import com.tangem.util.BTCUtils; + +public class SignPaymentTask extends Thread { + public static final String TAG = SignPaymentTask.class.getSimpleName(); + + private String txAmount = ""; + private String txFee = ""; + + public void SetTransactionValue(String amount, String fee) { + txAmount = amount; + txFee = fee; + } + + private String txOutAddress; + private Activity mContext; + private TangemCard mCard; + private NfcManager mNfcManager; + private IsoDep mIsoDep; + private CardProtocol.Notifications mNotifications; + private boolean isCancelled = false; + + public SignPaymentTask(Activity context, TangemCard card, NfcManager nfcManager, IsoDep isoDep, CardProtocol.Notifications notifications, String amount, String fee, String outAddress) { + mCard = card; + mContext = context; + mNfcManager = nfcManager; + mIsoDep = isoDep; + mNotifications = notifications; + txOutAddress = outAddress; + SetTransactionValue(amount, fee); + } + + @Override + public void run() { + if (mIsoDep == null) { + return; + } + CardProtocol protocol = new CardProtocol(mContext, mIsoDep, mCard, mNotifications); + + mNotifications.OnReadStart(protocol); + try { + + // for Samsung's bugs - + // Workaround for the Samsung Galaxy S5 (since the + // first connection always hangs on transceive). + int timeout = mIsoDep.getTimeout(); + mIsoDep.connect(); + mIsoDep.close(); + mIsoDep.connect(); + mIsoDep.setTimeout(timeout); + try { + mNotifications.OnReadProgress(protocol, 5); + + Log.i(TAG, "[-- Start sign payment --]"); + + if (isCancelled) return; + protocol.run_VerifyCard(); + + Log.i(TAG, "Manufacturer: " + protocol.getCard().getManufacturer().getOfficialName()); + + mNotifications.OnReadProgress(protocol, 30); + if (isCancelled) return; +// +// if (mCard.getBlockchain() == Blockchain.Ethereum) { +// SignETH_TX(protocol); +// } else { +// SignBTC_TX(protocol); +// } + + CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); + if (engine != null) { + if (mCard.getPauseBeforePIN2() > 0) { + mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); + } + + byte[] tx; +// try { + tx = engine.Sign(txFee, txAmount, txOutAddress, mCard, protocol); +// } +// finally { +// mNotifications.OnReadWait(0); +// } + + if (tx != null) { + String txStr = BTCUtils.toHex(tx); + if (mCard.getBlockchain() == Blockchain.Ethereum || mCard.getBlockchain() == Blockchain.EthereumTestNet || mCard.getBlockchain() == Blockchain.Token) { + txStr = String.format("0x%s", txStr); + } + + LastSignStorage.setLastTX(mCard.getWallet(), txStr); + + Intent intent = new Intent(mContext, SendTransactionActivity.class); + intent.putExtra("UID", mCard.getUID()); + intent.putExtra("Card", mCard.getAsBundle()); + intent.putExtra("TX", txStr); + mContext.startActivityForResult(intent, SignPaymentActivity.REQUEST_CODE_SEND_PAYMENT); + } + } + mNotifications.OnReadProgress(protocol, 100); + + + if (isCancelled) return; + + } finally { + mNfcManager.ignoreTag(mIsoDep.getTag()); + } + } catch (CardProtocol.TangemException_InvalidPIN e) { + e.printStackTrace(); + protocol.setError(e); + } catch (Exception e) { + e.printStackTrace(); + protocol.setError(e); + + } finally { + Log.i(TAG, "[-- Finish sign payment --]"); + mNotifications.OnReadFinish(protocol); + } + } + + public void cancel(Boolean AllowInterrupt) { + try { + if (isAlive()) { + isCancelled = true; + join(500); + } + if (isAlive() && AllowInterrupt) { + interrupt(); + mNotifications.OnReadCancel(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/data/nfc/SwapPINTask.java b/app/src/main/java/com/tangem/data/nfc/SwapPINTask.java new file mode 100644 index 0000000000..a0bd0c2f6f --- /dev/null +++ b/app/src/main/java/com/tangem/data/nfc/SwapPINTask.java @@ -0,0 +1,105 @@ +package com.tangem.data.nfc; + +import android.content.Context; +import android.nfc.tech.IsoDep; +import android.util.Log; + +import com.tangem.domain.cardReader.CardProtocol; +import com.tangem.domain.cardReader.NfcManager; +import com.tangem.domain.wallet.PINStorage; +import com.tangem.domain.wallet.TangemCard; + +public class SwapPINTask extends Thread { + public static final String TAG = SwapPINTask.class.getSimpleName(); + + private Context mContext; + private TangemCard mCard; + private NfcManager mNfcManager; + private String newPIN, newPIN2; + private IsoDep mIsoDep; + private CardProtocol.Notifications mNotifications; + private boolean isCancelled = false; + + public SwapPINTask(Context context, TangemCard card, NfcManager nfcManager, String newPIN, String newPIN2, IsoDep isoDep, CardProtocol.Notifications notifications) { + this.newPIN = newPIN; + this.newPIN2 = newPIN2; + mCard = card; + mContext = context; + mNfcManager = nfcManager; + mIsoDep = isoDep; + mNotifications = notifications; + } + + @Override + public void run() { + if (mIsoDep == null) { + return; + } + CardProtocol protocol = new CardProtocol(mContext, mIsoDep, mCard, mNotifications); + + mNotifications.OnReadStart(protocol); + try { + + // for Samsung's bugs - + // Workaround for the Samsung Galaxy S5 (since the + // first connection always hangs on transceive). + int timeout = mIsoDep.getTimeout(); + mIsoDep.connect(); + mIsoDep.close(); + mIsoDep.connect(); + mIsoDep.setTimeout(timeout); + try { + + mNotifications.OnReadProgress(protocol, 5); + + Log.i(TAG, "[-- Start swap pin --]"); + + if (isCancelled) return; + + if (mCard.getPauseBeforePIN2() > 0) { + mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); + } + +// try { + protocol.run_SwapPIN(PINStorage.getPIN2(), newPIN, newPIN2, false); + protocol.setPIN(newPIN); + mCard.setPIN(newPIN); +// } finally { +// mNotifications.OnReadWait(0); +// } + + mNotifications.OnReadProgress(protocol, 50); + + protocol.run_Read(); + + mNotifications.OnReadProgress(protocol, 100); + + } finally { + mNfcManager.ignoreTag(mIsoDep.getTag()); + } + } catch (Exception e) { + e.printStackTrace(); + protocol.setError(e); + + } finally { + Log.i(TAG, "[-- Finish purge --]"); + mNotifications.OnReadFinish(protocol); + } + } + + public void cancel(Boolean AllowInterrupt) { + try { + if (isAlive()) { + isCancelled = true; + join(500); + } + if (isAlive() && AllowInterrupt) { + interrupt(); + mNotifications.OnReadCancel(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/data/task/VerifyCardTask.java b/app/src/main/java/com/tangem/data/nfc/VerifyCardTask.java similarity index 96% rename from app/src/main/java/com/tangem/data/task/VerifyCardTask.java rename to app/src/main/java/com/tangem/data/nfc/VerifyCardTask.java index 966f410e5d..894cc73af5 100644 --- a/app/src/main/java/com/tangem/data/task/VerifyCardTask.java +++ b/app/src/main/java/com/tangem/data/nfc/VerifyCardTask.java @@ -1,4 +1,4 @@ -package com.tangem.data.task; +package com.tangem.data.nfc; import android.content.Context; import android.nfc.tech.IsoDep; @@ -94,11 +94,11 @@ public class VerifyCardTask extends Thread { public void cancel(Boolean AllowInterrupt) { try { - if (this.isAlive()) { + if (isAlive()) { isCancelled = true; join(500); } - if (this.isAlive() && AllowInterrupt) { + if (isAlive() && AllowInterrupt) { interrupt(); mNotifications.OnReadCancel(); } diff --git a/app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.java b/app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.java index 56dd3b4095..9f5e6ee8ac 100644 --- a/app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.java @@ -15,14 +15,14 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; +import com.tangem.data.nfc.CreateNewWalletTask; import com.tangem.domain.cardReader.CardProtocol; import com.tangem.domain.cardReader.NfcManager; import com.tangem.domain.wallet.TangemCard; -import com.tangem.util.Util; import com.tangem.presentation.dialog.NoExtendedLengthSupportDialog; -import com.tangem.domain.wallet.PINStorage; -import com.tangem.wallet.R; import com.tangem.presentation.dialog.WaitSecurityDelayDialog; +import com.tangem.util.Util; +import com.tangem.wallet.R; public class CreateNewWalletActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { public static final String TAG = CreateNewWalletActivity.class.getSimpleName(); @@ -35,88 +35,88 @@ public class CreateNewWalletActivity extends AppCompatActivity implements NfcAda private CreateNewWalletTask createNewWalletTask; private boolean lastReadSuccess = true; - private class CreateNewWalletTask extends Thread { - - private IsoDep mIsoDep; - private CardProtocol.Notifications mNotifications; - private boolean isCancelled = false; - - public CreateNewWalletTask(IsoDep isoDep, CardProtocol.Notifications notifications) { - mIsoDep = isoDep; - mNotifications = notifications; - } - - @Override - public void run() { - if (mIsoDep == null) { - return; - } - CardProtocol protocol = new CardProtocol(getBaseContext(), mIsoDep, mCard, mNotifications); - mNotifications.OnReadStart(protocol); - try { - // for Samsung's bugs - - // Workaround for the Samsung Galaxy S5 (since the - // first connection always hangs on transceive). - int timeout = mIsoDep.getTimeout(); - mIsoDep.connect(); - mIsoDep.close(); - mIsoDep.connect(); - mIsoDep.setTimeout(timeout); - try { - mNotifications.OnReadProgress(protocol, 5); - - Log.i("CreateNewWalletTask", "[-- Start create new wallet --]"); - - if (isCancelled) return; - protocol.run_VerifyCard(); - - Log.i("CreateNewWalletTask", "Manufacturer: " + protocol.getCard().getManufacturer().getOfficialName()); - - mNotifications.OnReadProgress(protocol, 30); - if (isCancelled) return; - -// if (mCard.getPauseBeforePIN2() > 0) { -// mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); -// } -// try { - protocol.run_CreateWallet(PINStorage.getPIN2()); -// } finally { -// mNotifications.OnReadWait(0); -// } - mNotifications.OnReadProgress(protocol, 60); - if (isCancelled) return; - - protocol.run_Read(); - - } finally { - mNfcManager.ignoreTag(mIsoDep.getTag()); - } - } catch (Exception e) { - e.printStackTrace(); - protocol.setError(e); - - } finally { - Log.i("CreateNewWalletTask", "[-- Finish create new wallet --]"); - mNotifications.OnReadFinish(protocol); - } - } - - public void cancel(Boolean AllowInterrupt) { - try { - if (this.isAlive()) { - isCancelled = true; - join(500); - } - if (this.isAlive() && AllowInterrupt) { - interrupt(); - mNotifications.OnReadCancel(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - } +// private class CreateNewWalletTask extends Thread { +// +// private IsoDep mIsoDep; +// private CardProtocol.Notifications mNotifications; +// private boolean isCancelled = false; +// +// public CreateNewWalletTask(IsoDep isoDep, CardProtocol.Notifications notifications) { +// mIsoDep = isoDep; +// mNotifications = notifications; +// } +// +// @Override +// public void run() { +// if (mIsoDep == null) { +// return; +// } +// CardProtocol protocol = new CardProtocol(getBaseContext(), mIsoDep, mCard, mNotifications); +// mNotifications.OnReadStart(protocol); +// try { +// // for Samsung's bugs - +// // Workaround for the Samsung Galaxy S5 (since the +// // first connection always hangs on transceive). +// int timeout = mIsoDep.getTimeout(); +// mIsoDep.connect(); +// mIsoDep.close(); +// mIsoDep.connect(); +// mIsoDep.setTimeout(timeout); +// try { +// mNotifications.OnReadProgress(protocol, 5); +// +// Log.i("CreateNewWalletTask", "[-- Start create new wallet --]"); +// +// if (isCancelled) return; +// protocol.run_VerifyCard(); +// +// Log.i("CreateNewWalletTask", "Manufacturer: " + protocol.getCard().getManufacturer().getOfficialName()); +// +// mNotifications.OnReadProgress(protocol, 30); +// if (isCancelled) return; +// +//// if (mCard.getPauseBeforePIN2() > 0) { +//// mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); +//// } +//// try { +// protocol.run_CreateWallet(PINStorage.getPIN2()); +//// } finally { +//// mNotifications.OnReadWait(0); +//// } +// mNotifications.OnReadProgress(protocol, 60); +// if (isCancelled) return; +// +// protocol.run_Read(); +// +// } finally { +// mNfcManager.ignoreTag(mIsoDep.getTag()); +// } +// } catch (Exception e) { +// e.printStackTrace(); +// protocol.setError(e); +// +// } finally { +// Log.i("CreateNewWalletTask", "[-- Finish create new wallet --]"); +// mNotifications.OnReadFinish(protocol); +// } +// } +// +// public void cancel(Boolean AllowInterrupt) { +// try { +// if (this.isAlive()) { +// isCancelled = true; +// join(500); +// } +// if (this.isAlive() && AllowInterrupt) { +// interrupt(); +// mNotifications.OnReadCancel(); +// } +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } +// +// } @Override protected void onCreate(Bundle savedInstanceState) { @@ -155,7 +155,7 @@ public class CreateNewWalletActivity extends AppCompatActivity implements NfcAda } else { isoDep.setTimeout(mCard.getPauseBeforePIN2() + 65000); } - createNewWalletTask = new CreateNewWalletTask(isoDep, this); + createNewWalletTask = new CreateNewWalletTask(this, mCard, mNfcManager, isoDep, this); createNewWalletTask.start(); } else { Log.d(TAG, "Mismatch card UID (" + sUID + " instead of " + mCard.getUID() + ")"); diff --git a/app/src/main/java/com/tangem/presentation/activity/EmptyWalletActivity.java b/app/src/main/java/com/tangem/presentation/activity/EmptyWalletActivity.java index cd797910dc..8c34ce6b72 100644 --- a/app/src/main/java/com/tangem/presentation/activity/EmptyWalletActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/EmptyWalletActivity.java @@ -23,7 +23,7 @@ import com.tangem.domain.wallet.TangemCard; import com.tangem.util.Util; import com.tangem.presentation.dialog.NoExtendedLengthSupportDialog; import com.tangem.wallet.R; -import com.tangem.data.task.VerifyCardTask; +import com.tangem.data.nfc.VerifyCardTask; import com.tangem.presentation.dialog.WaitSecurityDelayDialog; public class EmptyWalletActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { diff --git a/app/src/main/java/com/tangem/presentation/activity/PurgeActivity.java b/app/src/main/java/com/tangem/presentation/activity/PurgeActivity.java index a23441675e..7578d18193 100644 --- a/app/src/main/java/com/tangem/presentation/activity/PurgeActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/PurgeActivity.java @@ -15,9 +15,9 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; +import com.tangem.data.nfc.PurgeTask; import com.tangem.domain.cardReader.CardProtocol; import com.tangem.domain.cardReader.NfcManager; -import com.tangem.domain.wallet.PINStorage; import com.tangem.domain.wallet.TangemCard; import com.tangem.presentation.dialog.NoExtendedLengthSupportDialog; import com.tangem.presentation.dialog.WaitSecurityDelayDialog; @@ -34,88 +34,88 @@ public class PurgeActivity extends AppCompatActivity implements NfcAdapter.Reade private ProgressBar progressBar; private PurgeTask purgeTask; - private class PurgeTask extends Thread { - private String txOutAddress; - - IsoDep mIsoDep; - CardProtocol.Notifications mNotifications; - private boolean isCancelled = false; - - PurgeTask(IsoDep isoDep, CardProtocol.Notifications notifications) { - mIsoDep = isoDep; - mNotifications = notifications; - } - - @Override - public void run() { - if (mIsoDep == null) { - return; - } - CardProtocol protocol = new CardProtocol(getBaseContext(), mIsoDep, mCard, mNotifications); - - mNotifications.OnReadStart(protocol); - try { - - // for Samsung's bugs - Workaround for the Samsung Galaxy S5 (since the first connection always hangs on transceive). - int timeout = mIsoDep.getTimeout(); - mIsoDep.connect(); - mIsoDep.close(); - mIsoDep.connect(); - mIsoDep.setTimeout(timeout); - try { - mNotifications.OnReadProgress(protocol, 5); - - Log.i(TAG, "[-- Start purge --]"); - - if (isCancelled) return; - - if (mCard.getPauseBeforePIN2() > 0) { - mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); - } - -// try { - protocol.run_PurgeWallet(PINStorage.getPIN2()); -// } finally { -// mNotifications.OnReadWait(0); +// private class PurgeTask extends Thread { +// private String txOutAddress; +// +// IsoDep mIsoDep; +// CardProtocol.Notifications mNotifications; +// private boolean isCancelled = false; +// +// PurgeTask(IsoDep isoDep, CardProtocol.Notifications notifications) { +// mIsoDep = isoDep; +// mNotifications = notifications; +// } +// +// @Override +// public void run() { +// if (mIsoDep == null) { +// return; +// } +// CardProtocol protocol = new CardProtocol(getBaseContext(), mIsoDep, mCard, mNotifications); +// +// mNotifications.OnReadStart(protocol); +// try { +// +// // for Samsung's bugs - Workaround for the Samsung Galaxy S5 (since the first connection always hangs on transceive). +// int timeout = mIsoDep.getTimeout(); +// mIsoDep.connect(); +// mIsoDep.close(); +// mIsoDep.connect(); +// mIsoDep.setTimeout(timeout); +// try { +// mNotifications.OnReadProgress(protocol, 5); +// +// Log.i(TAG, "[-- Start purge --]"); +// +// if (isCancelled) return; +// +// if (mCard.getPauseBeforePIN2() > 0) { +// mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); // } - - mNotifications.OnReadProgress(protocol, 50); - - protocol.run_Read(); - - mNotifications.OnReadProgress(protocol, 100); - - if (isCancelled) return; - - } finally { - mNfcManager.ignoreTag(mIsoDep.getTag()); - } - } catch (Exception e) { - e.printStackTrace(); - protocol.setError(e); - - } finally { - Log.i(TAG, "[-- Finish purge --]"); - mNotifications.OnReadFinish(protocol); - } - } - - public void cancel(Boolean AllowInterrupt) { - try { - if (this.isAlive()) { - isCancelled = true; - join(500); - } - if (this.isAlive() && AllowInterrupt) { - interrupt(); - mNotifications.OnReadCancel(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - } +// +//// try { +// protocol.run_PurgeWallet(PINStorage.getPIN2()); +//// } finally { +//// mNotifications.OnReadWait(0); +//// } +// +// mNotifications.OnReadProgress(protocol, 50); +// +// protocol.run_Read(); +// +// mNotifications.OnReadProgress(protocol, 100); +// +// if (isCancelled) return; +// +// } finally { +// mNfcManager.ignoreTag(mIsoDep.getTag()); +// } +// } catch (Exception e) { +// e.printStackTrace(); +// protocol.setError(e); +// +// } finally { +// Log.i(TAG, "[-- Finish purge --]"); +// mNotifications.OnReadFinish(protocol); +// } +// } +// +// public void cancel(Boolean AllowInterrupt) { +// try { +// if (this.isAlive()) { +// isCancelled = true; +// join(500); +// } +// if (this.isAlive() && AllowInterrupt) { +// interrupt(); +// mNotifications.OnReadCancel(); +// } +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } +// +// } @Override protected void onCreate(Bundle savedInstanceState) { @@ -176,8 +176,7 @@ public class PurgeActivity extends AppCompatActivity implements NfcAdapter.Reade if (sUID.equals(mCard.getUID())) { isoDep.setTimeout(mCard.getPauseBeforePIN2() + 65000); - purgeTask = new PurgeTask(isoDep, this); - + purgeTask = new PurgeTask(this, mCard, mNfcManager, isoDep, this); purgeTask.start(); } else { Log.d(TAG, "Mismatch card UID (" + sUID + " instead of " + mCard.getUID() + ")"); diff --git a/app/src/main/java/com/tangem/presentation/activity/SignPaymentActivity.java b/app/src/main/java/com/tangem/presentation/activity/SignPaymentActivity.java index 0fc059862d..dbe19a41e2 100644 --- a/app/src/main/java/com/tangem/presentation/activity/SignPaymentActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/SignPaymentActivity.java @@ -1 +1 @@ -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.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.tangem.domain.cardReader.CardProtocol; import com.tangem.domain.cardReader.NfcManager; import com.tangem.domain.wallet.TangemCard; import com.tangem.util.Util; import com.tangem.util.BTCUtils; import com.tangem.domain.wallet.Blockchain; import com.tangem.domain.wallet.CoinEngine; import com.tangem.domain.wallet.CoinEngineFactory; import com.tangem.domain.wallet.LastSignStorage; import com.tangem.presentation.dialog.NoExtendedLengthSupportDialog; import com.tangem.wallet.R; import com.tangem.presentation.dialog.WaitSecurityDelayDialog; public class SignPaymentActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { private static final int REQUEST_CODE_SEND_PAYMENT = 1; public static final int RESULT_INVALID_PIN = Activity.RESULT_FIRST_USER; private TangemCard mCard; private TextView tvCardID; private NfcManager mNfcManager; private static final String logTag = "SignPayment"; private ProgressBar progressBar; private SignPaymentTask signPaymentTask; private String amountStr; private String feeStr; private String outAddressStr; private boolean lastReadSuccess = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_payment); MainActivity.commonInit(getApplicationContext()); mCard = new TangemCard(getIntent().getStringExtra("UID")); mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); amountStr = getIntent().getStringExtra("Amount"); feeStr = getIntent().getStringExtra("Fee"); outAddressStr = getIntent().getStringExtra("Wallet"); tvCardID = findViewById(R.id.tvCardID); tvCardID.setText(mCard.getCIDDescription()); mNfcManager = new NfcManager(this, this); progressBar = findViewById(R.id.progressBar); progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); progressBar.setVisibility(View.INVISIBLE); } @Override public void onTagDiscovered(Tag tag) { try { // get IsoDep handle and run cardReader thread final IsoDep isoDep = IsoDep.get(tag); if (isoDep == null) { throw new CardProtocol.TangemException(getString(R.string.wrong_tag_err)); } byte UID[] = tag.getId(); String sUID = Util.byteArrayToHexString(UID); Log.v(logTag, "UID: " + sUID); if (sUID.equals(mCard.getUID())) { if (lastReadSuccess) { isoDep.setTimeout(mCard.getPauseBeforePIN2() + 5000); } else { isoDep.setTimeout(mCard.getPauseBeforePIN2() + 65000); } signPaymentTask = new SignPaymentTask(isoDep, this, amountStr, feeStr, outAddressStr); signPaymentTask.start(); } else { Log.d(logTag, "Mismatch card UID (" + sUID + " instead of " + mCard.getUID() + ")"); mNfcManager.ignoreTag(isoDep.getTag()); return; } } catch (Exception e) { e.printStackTrace(); } } @Override public void onResume() { super.onResume(); mNfcManager.onResume(); } @Override public void onPause() { mNfcManager.onPause(); if (signPaymentTask != null) { signPaymentTask.cancel(true); } super.onPause(); } @Override public void onStop() { // dismiss enable NFC dialog mNfcManager.onStop(); if (signPaymentTask != null) { signPaymentTask.cancel(true); } super.onStop(); } // @Override // public Dialog CreateNFCDialog(int id, AlertDialogWrapper.Builder builder, LayoutInflater li) { // return mNfcManager.onCreateDialog(id, builder, li); // } private class SignPaymentTask extends Thread { String txAmount = ""; String txFee = ""; public void SetTransactionValue(String amount, String fee) { txAmount = amount; txFee = fee; } private String txOutAddress; IsoDep mIsoDep; CardProtocol.Notifications mNotifications; private boolean isCancelled = false; public SignPaymentTask(IsoDep isoDep, CardProtocol.Notifications notifications, String amount, String fee, String outAddress) { mIsoDep = isoDep; mNotifications = notifications; txOutAddress = outAddress; SetTransactionValue(amount, fee); } @Override public void run() { if (mIsoDep == null) { return; } CardProtocol protocol = new CardProtocol(getBaseContext(), mIsoDep, mCard, mNotifications); mNotifications.OnReadStart(protocol); try { // for Samsung's bugs - // Workaround for the Samsung Galaxy S5 (since the // first connection always hangs on transceive). int timeout = mIsoDep.getTimeout(); mIsoDep.connect(); mIsoDep.close(); mIsoDep.connect(); mIsoDep.setTimeout(timeout); try { mNotifications.OnReadProgress(protocol, 5); Log.i("SignPaymentTask", "[-- Start sign payment --]"); if (isCancelled) return; protocol.run_VerifyCard(); Log.i("SignPaymentTask", "Manufacturer: " + protocol.getCard().getManufacturer().getOfficialName()); mNotifications.OnReadProgress(protocol, 30); if (isCancelled) return; // // if (mCard.getBlockchain() == Blockchain.Ethereum) { // SignETH_TX(protocol); // } else { // SignBTC_TX(protocol); // } CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); if (engine != null) { if (mCard.getPauseBeforePIN2() > 0) { mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); } byte[] tx; // try { tx = engine.Sign(txFee, txAmount, txOutAddress, mCard, protocol); // } // finally { // mNotifications.OnReadWait(0); // } if (tx != null) { String txStr = BTCUtils.toHex(tx); if (mCard.getBlockchain() == Blockchain.Ethereum || mCard.getBlockchain() == Blockchain.EthereumTestNet || mCard.getBlockchain() == Blockchain.Token) { txStr = String.format("0x%s", txStr); } LastSignStorage.setLastTX(mCard.getWallet(), txStr); Intent intent = new Intent(getBaseContext(), SendTransactionActivity.class); intent.putExtra("UID", mCard.getUID()); intent.putExtra("Card", mCard.getAsBundle()); intent.putExtra("TX", txStr); startActivityForResult(intent, REQUEST_CODE_SEND_PAYMENT); } } mNotifications.OnReadProgress(protocol, 100); if (isCancelled) return; } finally { mNfcManager.ignoreTag(mIsoDep.getTag()); } } catch (CardProtocol.TangemException_InvalidPIN e) { e.printStackTrace(); protocol.setError(e); } catch (Exception e) { e.printStackTrace(); protocol.setError(e); } finally { Log.i("SignPaymentTask", "[-- Finish sign payment --]"); mNotifications.OnReadFinish(protocol); } } public void cancel(Boolean AllowInterrupt) { try { if (this.isAlive()) { isCancelled = true; join(500); } if (this.isAlive() && AllowInterrupt) { interrupt(); mNotifications.OnReadCancel(); } } catch (Exception e) { e.printStackTrace(); } } } public void OnReadStart(CardProtocol cardProtocol) { progressBar.post(new Runnable() { @Override public void run() { progressBar.setVisibility(View.VISIBLE); progressBar.setProgress(5); } }); } public void OnReadFinish(final CardProtocol cardProtocol) { signPaymentTask = null; if (cardProtocol != null) { if (cardProtocol.getError() == null) { progressBar.post(new Runnable() { @Override public void run() { progressBar.setProgress(100); progressBar.setProgressTintList(ColorStateList.valueOf(Color.GREEN)); } }); } else { lastReadSuccess = false; if (cardProtocol.getError().getClass().equals(CardProtocol.TangemException_InvalidPIN.class)) { progressBar.post(new Runnable() { @Override public void run() { progressBar.setProgress(100); progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED)); } }); progressBar.postDelayed(new Runnable() { @Override public void run() { try { progressBar.setProgress(0); progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); progressBar.setVisibility(View.INVISIBLE); Intent intent = new Intent(); intent.putExtra("message", "Cannot sign transaction. Make sure you enter correct PIN2!"); intent.putExtra("UID", cardProtocol.getCard().getUID()); intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); setResult(RESULT_INVALID_PIN, intent); finish(); } catch (Exception e) { e.printStackTrace(); } } }, 500); return; } else { progressBar.post(new Runnable() { @Override public void run() { if (cardProtocol.getError() instanceof CardProtocol.TangemException_ExtendedLengthNotSupported) { if (!NoExtendedLengthSupportDialog.allreadyShowed) { new NoExtendedLengthSupportDialog().show(getFragmentManager(), "NoExtendedLengthSupportDialog"); } } else { Toast.makeText(getBaseContext(), "Try to scan again", Toast.LENGTH_LONG).show(); } progressBar.setProgress(100); progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED)); } }); } } } progressBar.postDelayed(new Runnable() { @Override public void run() { try { progressBar.setProgress(0); progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); progressBar.setVisibility(View.INVISIBLE); } catch (Exception e) { e.printStackTrace(); } } }, 500); } public void OnReadProgress(CardProtocol protocol, final int progress) { progressBar.post(new Runnable() { @Override public void run() { progressBar.setProgress(progress); } }); } public void OnReadCancel() { signPaymentTask = null; progressBar.postDelayed(new Runnable() { @Override public void run() { try { progressBar.setProgress(0); progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); progressBar.setVisibility(View.INVISIBLE); } catch (Exception e) { e.printStackTrace(); } } }, 500); } @Override public void OnReadWait(final int msec) { WaitSecurityDelayDialog.OnReadWait(this, msec); } @Override public void OnReadBeforeRequest(int timeout) { WaitSecurityDelayDialog.onReadBeforeRequest(this, timeout); } @Override public void OnReadAfterRequest() { WaitSecurityDelayDialog.onReadAfterRequest(this); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: Intent intent = new Intent(); intent.putExtra("message", "Operation canceled by user"); setResult(Activity.RESULT_CANCELED, intent); finish(); return true; } return super.onKeyDown(keyCode, event); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_SEND_PAYMENT) { setResult(resultCode, data); finish(); return; } super.onActivityResult(requestCode, resultCode, data); } } \ No newline at end of file +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.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; 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; public class SignPaymentActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { public static final int REQUEST_CODE_SEND_PAYMENT = 1; public static final int RESULT_INVALID_PIN = Activity.RESULT_FIRST_USER; private TangemCard mCard; private TextView tvCardID; private NfcManager mNfcManager; private static final String logTag = "SignPayment"; private ProgressBar progressBar; private SignPaymentTask signPaymentTask; private String amountStr; private String feeStr; private String outAddressStr; private boolean lastReadSuccess = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_payment); MainActivity.commonInit(getApplicationContext()); mCard = new TangemCard(getIntent().getStringExtra("UID")); mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); amountStr = getIntent().getStringExtra("Amount"); feeStr = getIntent().getStringExtra("Fee"); outAddressStr = getIntent().getStringExtra("Wallet"); tvCardID = findViewById(R.id.tvCardID); tvCardID.setText(mCard.getCIDDescription()); mNfcManager = new NfcManager(this, this); progressBar = findViewById(R.id.progressBar); progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); progressBar.setVisibility(View.INVISIBLE); } @Override public void onTagDiscovered(Tag tag) { try { // get IsoDep handle and run cardReader thread final IsoDep isoDep = IsoDep.get(tag); if (isoDep == null) { throw new CardProtocol.TangemException(getString(R.string.wrong_tag_err)); } byte UID[] = tag.getId(); String sUID = Util.byteArrayToHexString(UID); Log.v(logTag, "UID: " + sUID); if (sUID.equals(mCard.getUID())) { if (lastReadSuccess) { isoDep.setTimeout(mCard.getPauseBeforePIN2() + 5000); } else { isoDep.setTimeout(mCard.getPauseBeforePIN2() + 65000); } signPaymentTask = new SignPaymentTask(this, mCard, mNfcManager, isoDep, this, amountStr, feeStr, outAddressStr); signPaymentTask.start(); } else { Log.d(logTag, "Mismatch card UID (" + sUID + " instead of " + mCard.getUID() + ")"); mNfcManager.ignoreTag(isoDep.getTag()); return; } } catch (Exception e) { e.printStackTrace(); } } @Override public void onResume() { super.onResume(); mNfcManager.onResume(); } @Override public void onPause() { mNfcManager.onPause(); if (signPaymentTask != null) { signPaymentTask.cancel(true); } super.onPause(); } @Override public void onStop() { // dismiss enable NFC dialog mNfcManager.onStop(); if (signPaymentTask != null) { signPaymentTask.cancel(true); } super.onStop(); } // @Override // public Dialog CreateNFCDialog(int id, AlertDialogWrapper.Builder builder, LayoutInflater li) { // return mNfcManager.onCreateDialog(id, builder, li); // } // private class SignPaymentTask extends Thread { // // String txAmount = ""; // String txFee = ""; // // public void SetTransactionValue(String amount, String fee) { // txAmount = amount; // txFee = fee; // } // // private String txOutAddress; // // IsoDep mIsoDep; // CardProtocol.Notifications mNotifications; // private boolean isCancelled = false; // // public SignPaymentTask(IsoDep isoDep, CardProtocol.Notifications notifications, String amount, String fee, String outAddress) { // mIsoDep = isoDep; // mNotifications = notifications; // txOutAddress = outAddress; // SetTransactionValue(amount, fee); // } // // @Override // public void run() { // if (mIsoDep == null) { // return; // } // CardProtocol protocol = new CardProtocol(getBaseContext(), mIsoDep, mCard, mNotifications); // // mNotifications.OnReadStart(protocol); // try { // // // for Samsung's bugs - // // Workaround for the Samsung Galaxy S5 (since the // // first connection always hangs on transceive). // int timeout = mIsoDep.getTimeout(); // mIsoDep.connect(); // mIsoDep.close(); // mIsoDep.connect(); // mIsoDep.setTimeout(timeout); // try { // mNotifications.OnReadProgress(protocol, 5); // // Log.i("SignPaymentTask", "[-- Start sign payment --]"); // // if (isCancelled) return; // protocol.run_VerifyCard(); // // Log.i("SignPaymentTask", "Manufacturer: " + protocol.getCard().getManufacturer().getOfficialName()); // // mNotifications.OnReadProgress(protocol, 30); // if (isCancelled) return; //// //// if (mCard.getBlockchain() == Blockchain.Ethereum) { //// SignETH_TX(protocol); //// } else { //// SignBTC_TX(protocol); //// } // // CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); // if (engine != null) { // if (mCard.getPauseBeforePIN2() > 0) { // mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); // } // // byte[] tx; //// try { // tx = engine.Sign(txFee, txAmount, txOutAddress, mCard, protocol); //// } //// finally { //// mNotifications.OnReadWait(0); //// } // // if (tx != null) { // String txStr = BTCUtils.toHex(tx); // if (mCard.getBlockchain() == Blockchain.Ethereum || mCard.getBlockchain() == Blockchain.EthereumTestNet || mCard.getBlockchain() == Blockchain.Token) { // txStr = String.format("0x%s", txStr); // } // // LastSignStorage.setLastTX(mCard.getWallet(), txStr); // // Intent intent = new Intent(getBaseContext(), SendTransactionActivity.class); // intent.putExtra("UID", mCard.getUID()); // intent.putExtra("Card", mCard.getAsBundle()); // intent.putExtra("TX", txStr); // startActivityForResult(intent, REQUEST_CODE_SEND_PAYMENT); // } // } // mNotifications.OnReadProgress(protocol, 100); // // // if (isCancelled) return; // // } finally { // mNfcManager.ignoreTag(mIsoDep.getTag()); // } // } catch (CardProtocol.TangemException_InvalidPIN e) { // e.printStackTrace(); // protocol.setError(e); // } catch (Exception e) { // e.printStackTrace(); // protocol.setError(e); // // } finally { // Log.i("SignPaymentTask", "[-- Finish sign payment --]"); // mNotifications.OnReadFinish(protocol); // } // } // // public void cancel(Boolean AllowInterrupt) { // try { // if (this.isAlive()) { // isCancelled = true; // join(500); // } // if (this.isAlive() && AllowInterrupt) { // interrupt(); // mNotifications.OnReadCancel(); // } // } catch (Exception e) { // e.printStackTrace(); // } // } // // } public void OnReadStart(CardProtocol cardProtocol) { progressBar.post(() -> { progressBar.setVisibility(View.VISIBLE); progressBar.setProgress(5); }); } public void OnReadFinish(final CardProtocol cardProtocol) { signPaymentTask = null; if (cardProtocol != null) { if (cardProtocol.getError() == null) { progressBar.post(() -> { progressBar.setProgress(100); progressBar.setProgressTintList(ColorStateList.valueOf(Color.GREEN)); }); } else { lastReadSuccess = false; if (cardProtocol.getError().getClass().equals(CardProtocol.TangemException_InvalidPIN.class)) { progressBar.post(() -> { progressBar.setProgress(100); progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED)); }); progressBar.postDelayed(() -> { try { progressBar.setProgress(0); progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); progressBar.setVisibility(View.INVISIBLE); Intent intent = new Intent(); intent.putExtra("message", "Cannot sign transaction. Make sure you enter correct PIN2!"); intent.putExtra("UID", cardProtocol.getCard().getUID()); intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); setResult(RESULT_INVALID_PIN, intent); finish(); } catch (Exception e) { e.printStackTrace(); } }, 500); return; } else { progressBar.post(() -> { if (cardProtocol.getError() instanceof CardProtocol.TangemException_ExtendedLengthNotSupported) { if (!NoExtendedLengthSupportDialog.allreadyShowed) { new NoExtendedLengthSupportDialog().show(getFragmentManager(), NoExtendedLengthSupportDialog.TAG); } } else { Toast.makeText(getBaseContext(), R.string.try_to_scan_again, Toast.LENGTH_LONG).show(); } progressBar.setProgress(100); progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED)); }); } } } progressBar.postDelayed(() -> { try { progressBar.setProgress(0); progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); progressBar.setVisibility(View.INVISIBLE); } catch (Exception e) { e.printStackTrace(); } }, 500); } public void OnReadProgress(CardProtocol protocol, final int progress) { progressBar.post(() -> progressBar.setProgress(progress)); } public void OnReadCancel() { signPaymentTask = null; progressBar.postDelayed(() -> { try { progressBar.setProgress(0); progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); progressBar.setVisibility(View.INVISIBLE); } catch (Exception e) { e.printStackTrace(); } }, 500); } @Override public void OnReadWait(final int msec) { WaitSecurityDelayDialog.OnReadWait(this, msec); } @Override public void OnReadBeforeRequest(int timeout) { WaitSecurityDelayDialog.onReadBeforeRequest(this, timeout); } @Override public void OnReadAfterRequest() { WaitSecurityDelayDialog.onReadAfterRequest(this); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: Intent intent = new Intent(); intent.putExtra("message", "Operation canceled by user"); setResult(Activity.RESULT_CANCELED, intent); finish(); return true; } return super.onKeyDown(keyCode, event); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_SEND_PAYMENT) { setResult(resultCode, data); finish(); return; } super.onActivityResult(requestCode, resultCode, data); } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/presentation/activity/SwapPINActivity.java b/app/src/main/java/com/tangem/presentation/activity/SwapPINActivity.java index 57c4010616..aa29eb6dcc 100644 --- a/app/src/main/java/com/tangem/presentation/activity/SwapPINActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/SwapPINActivity.java @@ -15,14 +15,14 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; +import com.tangem.data.nfc.SwapPINTask; import com.tangem.domain.cardReader.CardProtocol; import com.tangem.domain.cardReader.NfcManager; import com.tangem.domain.wallet.TangemCard; -import com.tangem.util.Util; import com.tangem.presentation.dialog.NoExtendedLengthSupportDialog; -import com.tangem.domain.wallet.PINStorage; -import com.tangem.wallet.R; import com.tangem.presentation.dialog.WaitSecurityDelayDialog; +import com.tangem.util.Util; +import com.tangem.wallet.R; public class SwapPINActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { @@ -72,7 +72,7 @@ public class SwapPINActivity extends AppCompatActivity implements NfcAdapter.Rea if (sUID.equals(mCard.getUID())) { isoDep.setTimeout(mCard.getPauseBeforePIN2() + 65000); - swapPinTask = new SwapPINTask(isoDep, this); + swapPinTask = new SwapPINTask(this, mCard, mNfcManager, newPIN, newPIN2, isoDep, this); swapPinTask.start(); } else { Log.d(logTag, "Mismatch card UID (" + sUID + " instead of " + mCard.getUID() + ")"); @@ -110,99 +110,95 @@ public class SwapPINActivity extends AppCompatActivity implements NfcAdapter.Rea super.onStop(); } - private class SwapPINTask extends Thread { - - IsoDep mIsoDep; - CardProtocol.Notifications mNotifications; - private boolean isCancelled = false; - - SwapPINTask(IsoDep isoDep, CardProtocol.Notifications notifications) { - mIsoDep = isoDep; - mNotifications = notifications; - } - - @Override - public void run() { - if (mIsoDep == null) { - return; - } - CardProtocol protocol = new CardProtocol(getBaseContext(), mIsoDep, mCard, mNotifications); - - mNotifications.OnReadStart(protocol); - try { - - // for Samsung's bugs - - // Workaround for the Samsung Galaxy S5 (since the - // first connection always hangs on transceive). - int timeout = mIsoDep.getTimeout(); - mIsoDep.connect(); - mIsoDep.close(); - mIsoDep.connect(); - mIsoDep.setTimeout(timeout); - try { - - mNotifications.OnReadProgress(protocol, 5); - - Log.i("SwapTask", "[-- Start swap pin --]"); - - if (isCancelled) return; - - if (mCard.getPauseBeforePIN2() > 0) { - mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); - } - -// try { - protocol.run_SwapPIN(PINStorage.getPIN2(), newPIN, newPIN2, false); - protocol.setPIN(newPIN); - mCard.setPIN(newPIN); -// } finally { -// mNotifications.OnReadWait(0); +// private class SwapPINTask extends Thread { +// +// IsoDep mIsoDep; +// CardProtocol.Notifications mNotifications; +// private boolean isCancelled = false; +// +// SwapPINTask(IsoDep isoDep, CardProtocol.Notifications notifications) { +// mIsoDep = isoDep; +// mNotifications = notifications; +// } +// +// @Override +// public void run() { +// if (mIsoDep == null) { +// return; +// } +// CardProtocol protocol = new CardProtocol(getBaseContext(), mIsoDep, mCard, mNotifications); +// +// mNotifications.OnReadStart(protocol); +// try { +// +// // for Samsung's bugs - +// // Workaround for the Samsung Galaxy S5 (since the +// // first connection always hangs on transceive). +// int timeout = mIsoDep.getTimeout(); +// mIsoDep.connect(); +// mIsoDep.close(); +// mIsoDep.connect(); +// mIsoDep.setTimeout(timeout); +// try { +// +// mNotifications.OnReadProgress(protocol, 5); +// +// Log.i("SwapTask", "[-- Start swap pin --]"); +// +// if (isCancelled) return; +// +// if (mCard.getPauseBeforePIN2() > 0) { +// mNotifications.OnReadWait(mCard.getPauseBeforePIN2()); // } - - mNotifications.OnReadProgress(protocol, 50); - - protocol.run_Read(); - - mNotifications.OnReadProgress(protocol, 100); - - } finally { - mNfcManager.ignoreTag(mIsoDep.getTag()); - } - } catch (Exception e) { - e.printStackTrace(); - protocol.setError(e); - - } finally { - Log.i("SwapPINTask", "[-- Finish purge --]"); - mNotifications.OnReadFinish(protocol); - } - } - - public void cancel(Boolean AllowInterrupt) { - try { - if (this.isAlive()) { - isCancelled = true; - join(500); - } - if (this.isAlive() && AllowInterrupt) { - interrupt(); - mNotifications.OnReadCancel(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - } - +// +//// try { +// protocol.run_SwapPIN(PINStorage.getPIN2(), newPIN, newPIN2, false); +// protocol.setPIN(newPIN); +// mCard.setPIN(newPIN); +//// } finally { +//// mNotifications.OnReadWait(0); +//// } +// +// mNotifications.OnReadProgress(protocol, 50); +// +// protocol.run_Read(); +// +// mNotifications.OnReadProgress(protocol, 100); +// +// } finally { +// mNfcManager.ignoreTag(mIsoDep.getTag()); +// } +// } catch (Exception e) { +// e.printStackTrace(); +// protocol.setError(e); +// +// } finally { +// Log.i("SwapPINTask", "[-- Finish purge --]"); +// mNotifications.OnReadFinish(protocol); +// } +// } +// +// public void cancel(Boolean AllowInterrupt) { +// try { +// if (this.isAlive()) { +// isCancelled = true; +// join(500); +// } +// if (this.isAlive() && AllowInterrupt) { +// interrupt(); +// mNotifications.OnReadCancel(); +// } +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } +// +// } public void OnReadStart(CardProtocol cardProtocol) { - progressBar.post(new Runnable() { - @Override - public void run() { - progressBar.setVisibility(View.VISIBLE); - progressBar.setProgress(5); - } + progressBar.post(() -> { + progressBar.setVisibility(View.VISIBLE); + progressBar.setProgress(5); }); } @@ -212,95 +208,53 @@ public class SwapPINActivity extends AppCompatActivity implements NfcAdapter.Rea if (cardProtocol != null) { if (cardProtocol.getError() == null) { - progressBar.post(new Runnable() { - @Override - public void run() { - progressBar.setProgress(100); - progressBar.setProgressTintList(ColorStateList.valueOf(Color.GREEN)); - Intent intent = new Intent(); - intent.putExtra("UID", cardProtocol.getCard().getUID()); - intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); - setResult(Activity.RESULT_OK, intent); - finish(); - } + progressBar.post(() -> { + progressBar.setProgress(100); + progressBar.setProgressTintList(ColorStateList.valueOf(Color.GREEN)); + Intent intent = new Intent(); + intent.putExtra("UID", cardProtocol.getCard().getUID()); + intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); + setResult(Activity.RESULT_OK, intent); + finish(); }); } else if (cardProtocol.getError() instanceof CardProtocol.TangemException_InvalidPIN) { - progressBar.post(new Runnable() { - @Override - public void run() { - progressBar.setProgress(100); - progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED)); - } + progressBar.post(() -> { + progressBar.setProgress(100); + progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED)); }); - progressBar.postDelayed(new Runnable() { - @Override - public void run() { - try { - progressBar.setProgress(0); - progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); - progressBar.setVisibility(View.INVISIBLE); - Intent intent = new Intent(); - intent.putExtra("message", "Cannot change PIN(s). Make sure you enter correct PIN2!"); - intent.putExtra("UID", cardProtocol.getCard().getUID()); - intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); - setResult(RESULT_INVALID_PIN, intent); - finish(); - } catch (Exception e) { - e.printStackTrace(); - } + progressBar.postDelayed(() -> { + try { + progressBar.setProgress(0); + progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); + progressBar.setVisibility(View.INVISIBLE); + Intent intent = new Intent(); + intent.putExtra("message", "Cannot change PIN(s). Make sure you enter correct PIN2!"); + intent.putExtra("UID", cardProtocol.getCard().getUID()); + intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); + setResult(RESULT_INVALID_PIN, intent); + finish(); + } catch (Exception e) { + e.printStackTrace(); } }, 500); return; } else { - progressBar.post(new Runnable() { - @Override - public void run() { - if (cardProtocol.getError() instanceof CardProtocol.TangemException_ExtendedLengthNotSupported) { - if (!NoExtendedLengthSupportDialog.allreadyShowed) { - new NoExtendedLengthSupportDialog().show(getFragmentManager(), "NoExtendedLengthSupportDialog"); - } - } else { - Toast.makeText(getBaseContext(), "Try to scan again", Toast.LENGTH_LONG).show(); + progressBar.post(() -> { + if (cardProtocol.getError() instanceof CardProtocol.TangemException_ExtendedLengthNotSupported) { + if (!NoExtendedLengthSupportDialog.allreadyShowed) { + new NoExtendedLengthSupportDialog().show(getFragmentManager(), NoExtendedLengthSupportDialog.TAG); } - progressBar.setProgress(100); - progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED)); + } else { + Toast.makeText(getBaseContext(), R.string.try_to_scan_again, Toast.LENGTH_LONG).show(); } + progressBar.setProgress(100); + progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED)); }); } - progressBar.postDelayed(new Runnable() { - @Override - public void run() { - try { - progressBar.setProgress(0); - progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); - progressBar.setVisibility(View.INVISIBLE); - } catch (Exception e) { - e.printStackTrace(); - } - } - }, 500); - } - } - - public void OnReadProgress(CardProtocol protocol, final int progress) { - progressBar.post(new Runnable() { - @Override - public void run() { - progressBar.setProgress(progress); - } - }); - } - - public void OnReadCancel() { - - swapPinTask = null; - - progressBar.postDelayed(new Runnable() { - @Override - public void run() { + progressBar.postDelayed(() -> { try { progressBar.setProgress(0); progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); @@ -308,6 +262,24 @@ public class SwapPINActivity extends AppCompatActivity implements NfcAdapter.Rea } catch (Exception e) { e.printStackTrace(); } + }, 500); + } + } + + public void OnReadProgress(CardProtocol protocol, final int progress) { + progressBar.post(() -> progressBar.setProgress(progress)); + } + + public void OnReadCancel() { + swapPinTask = null; + + progressBar.postDelayed(() -> { + try { + progressBar.setProgress(0); + progressBar.setProgressTintList(ColorStateList.valueOf(Color.DKGRAY)); + progressBar.setVisibility(View.INVISIBLE); + } catch (Exception e) { + e.printStackTrace(); } }, 500); } @@ -327,5 +299,4 @@ public class SwapPINActivity extends AppCompatActivity implements NfcAdapter.Rea WaitSecurityDelayDialog.onReadAfterRequest(this); } -} - +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.java b/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.java index 073d8241ba..e53ffc360e 100644 --- a/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.java +++ b/app/src/main/java/com/tangem/presentation/fragment/LoadedWallet.java @@ -44,7 +44,7 @@ import com.tangem.data.network.request.InfuraRequest; import com.tangem.data.network.task.ElectrumTask; import com.tangem.data.network.task.ExchangeTask; import com.tangem.data.network.task.InfuraTask; -import com.tangem.data.task.VerifyCardTask; +import com.tangem.data.nfc.VerifyCardTask; import com.tangem.domain.cardReader.CardProtocol; import com.tangem.domain.cardReader.NfcManager; import com.tangem.domain.wallet.BitcoinException; diff --git a/app/src/main/java/com/tangem/presentation/fragment/Main.java b/app/src/main/java/com/tangem/presentation/fragment/Main.java index 21256089a9..171b9a2338 100644 --- a/app/src/main/java/com/tangem/presentation/fragment/Main.java +++ b/app/src/main/java/com/tangem/presentation/fragment/Main.java @@ -30,7 +30,7 @@ import com.tangem.data.network.request.InfuraRequest; import com.tangem.data.network.task.ElectrumTask; import com.tangem.data.network.task.ExchangeTask; import com.tangem.data.network.task.InfuraTask; -import com.tangem.data.task.ReadCardInfoTask; +import com.tangem.data.nfc.ReadCardInfoTask; import com.tangem.domain.cardReader.CardProtocol; import com.tangem.domain.cardReader.NfcManager; import com.tangem.domain.wallet.Blockchain;