From 8c17a8d4f072f2ba08117cd489979ade62c61ba7 Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 29 May 2018 13:28:36 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .gitignore | 1 + .idea/caches/build_file_checksums.ser | Bin 536 -> 536 bytes app/src/main/AndroidManifest.xml | 161 +- .../main/java/com/tangem/domain/Logic.java | 4 + .../java/com/tangem/presentation/Screen.java | 4 + .../activity}/CardInfoActivity.java | 248 +-- .../activity}/ConfirmPaymentActivity.java | 1455 ++++++++--------- .../activity}/CreateNewWalletActivity.java | 664 ++++---- .../activity}/EmptyWalletActivity.java | 717 ++++---- .../activity}/LoadedWalletActivity.java | 77 +- .../activity}/LogoActivity.java | 139 +- .../activity}/MainActivity.java | 817 ++++----- .../activity}/PreparePaymentActivity.java | 462 +++--- .../activity}/PurgeActivity.java | 671 ++++---- .../activity}/QRScanActivity.java | 180 +- .../activity}/RequestPINActivity.java | 6 +- .../activity}/SavePINActivity.java | 6 +- .../activity}/SelectBlockchainActivity.java | 64 +- .../activity}/SendTransactionActivity.java | 427 ++--- .../activity/SignPaymentActivity.java | 1 + .../activity}/SwapPINActivity.java | 657 ++++---- .../activity}/VerifyCardActivity.java | 58 +- .../java/com/tangem/wallet/Electrum_Task.java | 2 +- .../main/java/com/tangem/wallet/Fee_Task.java | 2 +- .../com/tangem/wallet/FingerprintHelper.java | 2 +- .../com/tangem/wallet/LastSignStorage.java | 3 +- .../wallet/LoadedWalletActivityFragment.java | 6 + .../tangem/wallet/MainActivityFragment.java | 4 + .../java/com/tangem/wallet/PINStorage.java | 33 +- .../java/com/tangem/wallet/SharedData.java | 1 + .../tangem/wallet/SignPaymentActivity.java | 1 - .../com/tangem/wallet/VerifyCardTask.java | 2 +- .../main/res/layout/activity_card_info.xml | 2 +- .../res/layout/activity_confirm_payment.xml | 2 +- .../res/layout/activity_create_new_wallet.xml | 2 +- .../main/res/layout/activity_empty_wallet.xml | 2 +- .../res/layout/activity_loaded_wallet.xml | 2 +- app/src/main/res/layout/activity_logo.xml | 2 +- app/src/main/res/layout/activity_main.xml | 2 +- .../res/layout/activity_prepare_payment.xml | 2 +- app/src/main/res/layout/activity_purge.xml | 2 +- app/src/main/res/layout/activity_qrscan.xml | 2 +- .../main/res/layout/activity_request_pin.xml | 2 +- app/src/main/res/layout/activity_save_pin.xml | 2 +- .../res/layout/activity_send_transaction.xml | 2 +- .../main/res/layout/activity_sign_payment.xml | 2 +- app/src/main/res/layout/activity_swap_pin.xml | 2 +- .../main/res/layout/activity_verify_card.xml | 2 +- 48 files changed, 3490 insertions(+), 3417 deletions(-) create mode 100644 app/src/main/java/com/tangem/domain/Logic.java create mode 100644 app/src/main/java/com/tangem/presentation/Screen.java rename app/src/main/java/com/tangem/{wallet => presentation/activity}/CardInfoActivity.java (96%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/ConfirmPaymentActivity.java (87%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/CreateNewWalletActivity.java (97%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/EmptyWalletActivity.java (97%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/LoadedWalletActivity.java (91%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/LogoActivity.java (94%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/MainActivity.java (97%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/PreparePaymentActivity.java (80%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/PurgeActivity.java (96%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/QRScanActivity.java (96%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/RequestPINActivity.java (98%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/SavePINActivity.java (99%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/SelectBlockchainActivity.java (94%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/SendTransactionActivity.java (94%) create mode 100644 app/src/main/java/com/tangem/presentation/activity/SignPaymentActivity.java rename app/src/main/java/com/tangem/{wallet => presentation/activity}/SwapPINActivity.java (96%) rename app/src/main/java/com/tangem/{wallet => presentation/activity}/VerifyCardActivity.java (63%) delete mode 100644 app/src/main/java/com/tangem/wallet/SignPaymentActivity.java diff --git a/.gitignore b/.gitignore index c7079ecd52..9a981e6c12 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ .externalNativeBuild app/libs/ .idea/vcs.xml +.idea/caches diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 0fabde5fae072e79b8193b6c8984621a77c4e9c7..9389fce7cb7f70f0695edf4207d9dbf9e9891a8e 100644 GIT binary patch delta 34 scmV+-0NwwX1egSnm;|dqaZ8b$%@5ib4sSp~q3VA2R~L4 + package="com.tangem.wallet"> + android:required="true"/> - - - - - - + + + + + - - + + + android:grantUriPermissions="true"/> + - - + + - + - - - - - - - - + + + android:scheme="http"/> - + + android:resource="@xml/nfc_tech_filter"/> + - - - + android:theme="@style/AppTheme.NoActionBar"/> + + android:theme="@style/AppTheme.NoActionBar"/> + + android:theme="@style/AppTheme.NoActionBar"/> + + android:theme="@style/AppTheme.NoActionBar"/> + + android:theme="@style/AppTheme.NoActionBar"/> + + android:theme="@style/AppTheme.NoActionBar"/> + + android:theme="@style/AppTheme.NoActionBar"/> + + android:theme="@style/AppTheme.NoActionBar"/> + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/tangem/domain/Logic.java b/app/src/main/java/com/tangem/domain/Logic.java new file mode 100644 index 0000000000..b222756f36 --- /dev/null +++ b/app/src/main/java/com/tangem/domain/Logic.java @@ -0,0 +1,4 @@ +package com.tangem.domain; + +public class Logic { +} diff --git a/app/src/main/java/com/tangem/presentation/Screen.java b/app/src/main/java/com/tangem/presentation/Screen.java new file mode 100644 index 0000000000..4e389bb6b7 --- /dev/null +++ b/app/src/main/java/com/tangem/presentation/Screen.java @@ -0,0 +1,4 @@ +package com.tangem.presentation; + +public class Screen { +} diff --git a/app/src/main/java/com/tangem/wallet/CardInfoActivity.java b/app/src/main/java/com/tangem/presentation/activity/CardInfoActivity.java similarity index 96% rename from app/src/main/java/com/tangem/wallet/CardInfoActivity.java rename to app/src/main/java/com/tangem/presentation/activity/CardInfoActivity.java index d80b2a306b..9b10052280 100644 --- a/app/src/main/java/com/tangem/wallet/CardInfoActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/CardInfoActivity.java @@ -1,122 +1,126 @@ -package com.tangem.wallet; - -import android.os.Bundle; -import android.support.design.widget.TabLayout; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.view.ViewPager; -import android.support.v7.app.AppCompatActivity; -import android.view.Menu; -import android.view.MenuItem; - -public class CardInfoActivity extends AppCompatActivity implements WalletInfoFragment.OnFragmentInteractionListener { - - /** - * The {@link android.support.v4.view.PagerAdapter} that will provide - * fragments for each of the sections. We use a - * {@link FragmentPagerAdapter} derivative, which will keep every - * loaded fragment in memory. If this becomes too memory intensive, it - * may be best to switch to a - * {@link android.support.v4.app.FragmentStatePagerAdapter}. - */ - private SectionsPagerAdapter mSectionsPagerAdapter; - - /** - * The {@link ViewPager} that will host the section contents. - */ - private ViewPager mViewPager; - - - private Tangem_Card mCard; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_card_info); - - // Create the adapter that will return a fragment for each of the three - // primary sections of the activity. - mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); - - // Set up the ViewPager with the sections adapter. - mViewPager = (ViewPager) findViewById(R.id.container); - mViewPager.setAdapter(mSectionsPagerAdapter); - - TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); - tabLayout.setupWithViewPager(mViewPager); - - String UID = getIntent().getStringExtra("UID"); - mCard = new Tangem_Card(UID); - mCard.LoadFromBundle(getIntent().getBundleExtra("Card")); - - } - - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_card_info, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { - return true; - } - - return super.onOptionsItemSelected(item); - } - - - /** - * A {@link FragmentPagerAdapter} that returns a fragment corresponding to - * one of the sections/tabs/pages. - */ - public class SectionsPagerAdapter extends FragmentPagerAdapter { - - public SectionsPagerAdapter(FragmentManager fm) { - super(fm); - } - - @Override - public Fragment getItem(int position) { - // getItem is called to instantiate the fragment for the given page. - // Return a PlaceholderFragment (defined as a static inner class below). - if (position == 0) { - return WalletInfoFragment.newInstance(mCard); - } /*else if (position == 1) { - return WalletUnspentFragment.newInstance(mCard); - } else if (position == 2) { - return WalletHistoryFragment.newInstance(mCard); - }*/ - return null; - } - - @Override - public int getCount() { - // Show 3 total pages. - return 3; - } - - @Override - public CharSequence getPageTitle(int position) { - switch (position) { - case 0: - return "Wallet info"; - case 1: - return "Unspent"; - case 2: - return "History"; - } - return null; - } - } -} +package com.tangem.presentation.activity; + +import android.os.Bundle; +import android.support.design.widget.TabLayout; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; +import android.support.v7.app.AppCompatActivity; +import android.view.Menu; +import android.view.MenuItem; + +import com.tangem.wallet.R; +import com.tangem.wallet.Tangem_Card; +import com.tangem.wallet.WalletInfoFragment; + +public class CardInfoActivity extends AppCompatActivity implements WalletInfoFragment.OnFragmentInteractionListener { + + /** + * The {@link android.support.v4.view.PagerAdapter} that will provide + * fragments for each of the sections. We use a + * {@link FragmentPagerAdapter} derivative, which will keep every + * loaded fragment in memory. If this becomes too memory intensive, it + * may be best to switch to a + * {@link android.support.v4.app.FragmentStatePagerAdapter}. + */ + private SectionsPagerAdapter mSectionsPagerAdapter; + + /** + * The {@link ViewPager} that will host the section contents. + */ + private ViewPager mViewPager; + + + private Tangem_Card mCard; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_card_info); + + // Create the adapter that will return a fragment for each of the three + // primary sections of the activity. + mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); + + // Set up the ViewPager with the sections adapter. + mViewPager = (ViewPager) findViewById(R.id.container); + mViewPager.setAdapter(mSectionsPagerAdapter); + + TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); + tabLayout.setupWithViewPager(mViewPager); + + String UID = getIntent().getStringExtra("UID"); + mCard = new Tangem_Card(UID); + mCard.LoadFromBundle(getIntent().getBundleExtra("Card")); + + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_card_info, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } + + return super.onOptionsItemSelected(item); + } + + + /** + * A {@link FragmentPagerAdapter} that returns a fragment corresponding to + * one of the sections/tabs/pages. + */ + public class SectionsPagerAdapter extends FragmentPagerAdapter { + + public SectionsPagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int position) { + // getItem is called to instantiate the fragment for the given page. + // Return a PlaceholderFragment (defined as a static inner class below). + if (position == 0) { + return WalletInfoFragment.newInstance(mCard); + } /*else if (position == 1) { + return WalletUnspentFragment.newInstance(mCard); + } else if (position == 2) { + return WalletHistoryFragment.newInstance(mCard); + }*/ + return null; + } + + @Override + public int getCount() { + // Show 3 total pages. + return 3; + } + + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case 0: + return "Wallet info"; + case 1: + return "Unspent"; + case 2: + return "History"; + } + return null; + } + } +} diff --git a/app/src/main/java/com/tangem/wallet/ConfirmPaymentActivity.java b/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.java similarity index 87% rename from app/src/main/java/com/tangem/wallet/ConfirmPaymentActivity.java rename to app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.java index 9697c35770..b28f9b915c 100644 --- a/app/src/main/java/com/tangem/wallet/ConfirmPaymentActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/ConfirmPaymentActivity.java @@ -1,732 +1,723 @@ -package com.tangem.wallet; - -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.os.AsyncTask; -import android.os.Bundle; -import android.support.v4.widget.SwipeRefreshLayout; -import android.support.v7.app.AppCompatActivity; -import android.text.Editable; -import android.text.Html; -import android.text.Spanned; -import android.text.TextWatcher; -import android.util.Log; -import android.view.KeyEvent; -import android.view.View; -import android.widget.Button; -import android.widget.EditText; -import android.widget.ImageView; -import android.widget.ProgressBar; -import android.widget.RadioGroup; -import android.widget.TextView; -import android.widget.Toast; - -import com.tangem.cardReader.NfcManager; -import com.tangem.cardReader.Util; - -import org.json.JSONException; - -import java.io.IOException; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Date; -import java.util.List; - -public class ConfirmPaymentActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback { - - private static final int REQUEST_CODE_SIGN_PAYMENT = 1; - private static final int REQUEST_CODE_REQUEST_PIN2 = 2; - Button btnSend; - boolean feeRequestSuccess = false; - boolean balanceRequestSuccess = false; - EditText etWallet; - TextView tvCardID, tvBalance, tvCurrency, tvCurrency2, tvBalanceEquivalent, tvAmountEquivalent, tvFeeEquivalent; - EditText etAmount; - EditText etFee; - ImageView ivCamera; - Tangem_Card mCard; - RadioGroup rgFee; - String minFee = null, maxFee = null, normalFee = null; - Long minFeeInInternalUnits = 0L; - private NfcManager mNfcManager; - int requestPIN2Count = 0; - ProgressBar progressBar; - boolean nodeCheck = false; - Date dtVerifyed=null; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_confirm_payment); - - MainActivity.commonInit(getApplicationContext()); - mNfcManager = new NfcManager(this, this); - - mCard = new Tangem_Card(getIntent().getStringExtra("UID")); - mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); - - progressBar = findViewById(R.id.progressBar); - - btnSend = findViewById(R.id.btnSend); - etWallet = findViewById(R.id.etWallet); - tvCardID = findViewById(R.id.tvCardID); - tvBalance = findViewById(R.id.tvBalance); - tvCurrency = findViewById(R.id.tvCurrency); - tvCurrency2 = findViewById(R.id.tvCurrency2); - etAmount = findViewById(R.id.etAmount); - etFee = findViewById(R.id.etFee); - tvBalanceEquivalent = findViewById(R.id.tvBalanceEquivalent); - tvAmountEquivalent = findViewById(R.id.tvAmountEquivalent); - - tvFeeEquivalent = findViewById(R.id.tvFeeEquivalent); - ivCamera = findViewById(R.id.ivCamera); - - rgFee = findViewById(R.id.rgFee); - rgFee.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(RadioGroup group, int checkedId) { - doSetFee(checkedId); - } - }); - - etAmount.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - try { - - CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); - tvAmountEquivalent.setText(engine.GetAmountEqualentDescriptor(mCard, etAmount.getText().toString())); - if (!mCard.getAmountEquivalentDescriptionAvailable()) { - tvAmountEquivalent.setError("Service unavailable"); - } else { - tvAmountEquivalent.setError(null); - } - } catch (Exception e) { - e.printStackTrace(); - tvAmountEquivalent.setText(""); - } - } - - @Override - public void afterTextChanged(Editable s) { - - } - }); - - etFee.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - try { - - CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); - String eqFee = engine.EvaluteFeeEquivalent(mCard, etFee.getText().toString()); - tvFeeEquivalent.setText(eqFee); - - if (!mCard.getAmountEquivalentDescriptionAvailable()) { - tvFeeEquivalent.setError("Service unavailable"); - } else { - tvFeeEquivalent.setError(null); - } - } catch (Exception e) { - e.printStackTrace(); - tvFeeEquivalent.setText(""); - } - } - - @Override - public void afterTextChanged(Editable s) { - - } - }); - - CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); - //tvBalance.setText(engine.GetBalanceWithAlter(mCard)); - if (mCard.getBlockchain() == Blockchain.Token) { - Spanned html = Html.fromHtml(engine.GetBalanceWithAlter(mCard)); - tvBalance.setText(html); - } else { - tvBalance.setText(engine.GetBalanceWithAlter(mCard)); - } - etAmount.setText(getIntent().getStringExtra("Amount")); - tvCurrency.setText(engine.GetBalanceCurrency(mCard)); - tvCurrency2.setText(engine.GetFeeCurrency()); - - tvCardID.setText(mCard.getCIDDescription()); - - //tvBalanceEquivalent.setText(mCard.getBalanceEquivalentDescription()); - tvBalanceEquivalent.setText(engine.GetBalanceEquivalent(mCard)); - if (!mCard.getAmountEquivalentDescriptionAvailable()) { - tvBalanceEquivalent.setError("Service unavailable"); - } else { - tvBalanceEquivalent.setError(null); - } - - etWallet.setText(getIntent().getStringExtra("Wallet")); - - etFee.setText("?"); - - btnSend.setVisibility(View.INVISIBLE); - feeRequestSuccess = false; - balanceRequestSuccess = false; - btnSend.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - - Calendar calendar=Calendar.getInstance(); - calendar.add(Calendar.MINUTE,-1); - - if( dtVerifyed==null || dtVerifyed.before(calendar.getTime()) ) { - FinishActivityWithError(Activity.RESULT_CANCELED, "The obtained data is outdated! Try again"); - return; - } - - CoinEngine engineCoin = CoinEngineFactory.Create(mCard.getBlockchain()); - - if (engineCoin.IsNeedCheckNode() && !nodeCheck) { - Toast.makeText(getBaseContext(), "Cannot reach current active blockchain node. Try again", Toast.LENGTH_LONG).show(); - return; - } - String txFee = etFee.getText().toString(); - String txAmount = etAmount.getText().toString(); - - - if (!engineCoin.HasBalanceInfo(mCard)) { - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot check balance! No connection with blockchain nodes"); - return; - } else if (!engineCoin.IsBalanceNotZero(mCard)) { - FinishActivityWithError(Activity.RESULT_CANCELED, "The wallet is empty"); - return; - } else if(!engineCoin.CheckUnspentTransaction(mCard)) { - //else if (mCard.getUnspentTransactions().size() == 0 && mCard.getBlockchain() != Blockchain.Ethereum) { - FinishActivityWithError(Activity.RESULT_CANCELED, "Please wait for confirmation of incoming transaction"); - return; - } - - if(!engineCoin.CheckAmountValie(mCard, txAmount, txFee, minFeeInInternalUnits)) - { - FinishActivityWithError(Activity.RESULT_CANCELED, "Fee exceeds payment amount. Enter correct value and repeat sending."); - return; - } - - requestPIN2Count = 0; - Intent intent = new Intent(getBaseContext(), RequestPINActivity.class); - intent.putExtra("mode", RequestPINActivity.Mode.RequestPIN2.toString()); - intent.putExtra("UID", mCard.getUID()); - intent.putExtra("Card", mCard.getAsBundle()); - startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2); - } - }); - - if (mCard.getBlockchain() == Blockchain.Ethereum || mCard.getBlockchain() == Blockchain.EthereumTestNet || mCard.getBlockchain() == Blockchain.Token) { - ETHRequestTask task = new ETHRequestTask(mCard.getBlockchain()); - Infura_Request req = Infura_Request.GetGasPrise(mCard.getWallet()); - req.setID(67); - req.setBlockchain(mCard.getBlockchain()); - rgFee.setEnabled(false); - task.execute(req); - } else { - - rgFee.setEnabled(true); - - SharedData data = new SharedData(SharedData.COUNT_REQUEST); - - CoinEngine engineCoin = CoinEngineFactory.Create(mCard.getBlockchain()); - - for(int i =0 ; i < data.allRequest; ++i) { - - String nodeAddress = engineCoin.GetNextNode(mCard); - int nodePort = engineCoin.GetNextNodePort(mCard); - //ConnectTask connectTaskEx = new ConnectTask(Blockchain.getNextServiceHost(mCard), Blockchain.getNextServicePort(mCard), data); - ConnectTask connectTaskEx = new ConnectTask(nodeAddress, nodePort, data); - - //connectTaskEx.execute(Electrum_Request.CheckBalance(mCard.getWallet())); - connectTaskEx.executeOnExecutor( AsyncTask.THREAD_POOL_EXECUTOR,Electrum_Request.CheckBalance(mCard.getWallet())); - } - - String nodeAddress = engineCoin.GetNode(mCard); - int nodePort = engineCoin.GetNodePort(mCard); - ConnectTask connectTask = new ConnectTask(nodeAddress, nodePort, data); - - //ConnectTask connectTask = new ConnectTask(Blockchain.getServiceHost(mCard), Blockchain.getServicePort(mCard)); - - connectTask.execute(/*Electrum_Request.CheckBalance(mCard.getWallet()), */Electrum_Request.GetFee(mCard.getWallet())); - - int calcSize = 256; - try { - calcSize = BuildSize(etWallet.getText().toString(), "0.00", etAmount.getText().toString()); - } catch (Exception ex) { - Log.e("Build Fee error", ex.getMessage()); - } - - SharedData sharedFee = new SharedData(SharedData.COUNT_REQUEST); - - progressBar.setVisibility(View.VISIBLE); - for(int i = 0; i < SharedData.COUNT_REQUEST; ++i) - { - - ConnectFeeTask feeTask = new ConnectFeeTask(sharedFee); - - feeTask.execute(Fee_Request.GetFee(mCard.getWallet(), calcSize, Fee_Request.NORMAL), - Fee_Request.GetFee(mCard.getWallet(), calcSize, Fee_Request.MINIMAL), - Fee_Request.GetFee(mCard.getWallet(), calcSize, Fee_Request.PRIORITY)); - } - } - - } - - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) { - switch (keyCode) { - case KeyEvent.KEYCODE_BACK: - Intent intent = new Intent(); - intent.putExtra("message", "Operation canceled"); - setResult(Activity.RESULT_CANCELED, intent); - finish(); - return true; - } - return super.onKeyDown(keyCode, event); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (requestCode == REQUEST_CODE_SIGN_PAYMENT) { - if (data != null && data.getExtras().containsKey("UID") && data.getExtras().containsKey("Card")) { - Tangem_Card updatedCard=new Tangem_Card(data.getStringExtra("UID")); - updatedCard.LoadFromBundle(data.getBundleExtra("Card")); - mCard=updatedCard; - } - if (resultCode == SignPaymentActivity.RESULT_INVALID_PIN && requestPIN2Count < 2) { - requestPIN2Count++; - Intent intent = new Intent(getBaseContext(), RequestPINActivity.class); - intent.putExtra("mode", RequestPINActivity.Mode.RequestPIN2.toString()); - intent.putExtra("UID", mCard.getUID()); - intent.putExtra("Card", mCard.getAsBundle()); - startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2); - return; - } - setResult(resultCode, data); - finish(); - } else if (requestCode == REQUEST_CODE_REQUEST_PIN2) { - if (resultCode == Activity.RESULT_OK) { - Intent intent = new Intent(getBaseContext(), SignPaymentActivity.class); - intent.putExtra("UID", mCard.getUID()); - intent.putExtra("Card", mCard.getAsBundle()); - intent.putExtra("Wallet", etWallet.getText().toString()); - intent.putExtra("Amount", etAmount.getText().toString()); - intent.putExtra("Fee", etFee.getText().toString()); - startActivityForResult(intent, REQUEST_CODE_SIGN_PAYMENT); - } else { - Toast.makeText(getBaseContext(), "PIN2 is required to sign the payment", Toast.LENGTH_LONG).show(); - } - } - } - - void FinishActivityWithError(int errorCode, String message) { - //Snackbar.make(etFee, message, Snackbar.LENGTH_LONG).show(); - Intent intent = new Intent(); - intent.putExtra("message", message); - setResult(errorCode, intent); - finish(); - } - - @Override - public void onTagDiscovered(Tag tag) { - try { - Log.w(getClass().getName(), "Ignore discovered tag!"); - mNfcManager.IgnoreTag(tag); - } catch (IOException e) { - e.printStackTrace(); - } - } - - int BuildSize(String outputAddress, String outFee, String outAmount) throws Exception { - String myAddress = mCard.getWallet(); - String changeAddress = myAddress; //"n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi"; - byte[] pbKey = mCard.getWalletPublicKey(); - byte[] pbComprKey = mCard.getWalletPublicKeyRar(); - - // Build script for our address - List rawTxList = mCard.getUnspentTransactions(); - byte[] outputScriptWeAreAbleToSpend = Transaction.Script.buildOutput(myAddress).bytes; - - // Collect unspent - ArrayList unspentOutputs = BTCUtils.getOutputs(rawTxList, outputScriptWeAreAbleToSpend); - - long fullAmount = 0; - for (int i = 0; i < unspentOutputs.size(); ++i) { - fullAmount += unspentOutputs.get(i).value; - } - - // Get first unspent - UnspentOutputInfo outPut = unspentOutputs.get(0); - int outPutIndex = outPut.outputIndex; - - // get prev TX id; - String prevTXID = rawTxList.get(0).txID;//"f67b838d6e2c0c587f476f583843e93ff20368eaf96a798bdc25e01f53f8f5d2"; - - long fees = FormatUtil.ConvertStringToLong(outFee); - long amount = FormatUtil.ConvertStringToLong(outAmount); - amount = amount - fees; - - long change = fullAmount - fees - amount; - - if (amount + fees > fullAmount) { - throw new Exception(String.format("Balance (%d) < amount (%d) + (%d)", fullAmount, change, amount)); - } - - byte[][] hashesForSign = new byte[unspentOutputs.size()][]; - - for (int i = 0; i < unspentOutputs.size(); ++i) { - byte[] newTX = BTCUtils.buildTXForSign(myAddress, outputAddress, changeAddress, unspentOutputs, i, amount, change); - - byte[] hashData = Util.calculateSHA256(newTX); - byte[] doubleHashData = Util.calculateSHA256(hashData); - - Log.e("TX_BODY_1", BTCUtils.toHex(newTX)); - Log.e("TX_HASH_1", BTCUtils.toHex(hashData)); - Log.e("TX_HASH_2", BTCUtils.toHex(doubleHashData)); - - unspentOutputs.get(i).bodyDoubleHash = doubleHashData; - unspentOutputs.get(i).bodyHash = hashData; - - hashesForSign[i] = doubleHashData; - } - - byte[] signFromCard = new byte[64 * unspentOutputs.size()]; - - for (int i = 0; i < unspentOutputs.size(); ++i) { - BigInteger r = new BigInteger(1, Arrays.copyOfRange(signFromCard, 0 + i * 64, 32 + i * 64)); - BigInteger s = new BigInteger(1, Arrays.copyOfRange(signFromCard, 32 + i * 64, 64 + i * 64)); - byte[] encodingSign = DerEncodingUtil.packSignDer(r, s, pbKey); - unspentOutputs.get(i).scriptForBuild = encodingSign; - } - - byte[] realTX = BTCUtils.buildTXForSend(outputAddress, changeAddress, unspentOutputs, amount, change); - - return realTX.length; - } - - private class ConnectTask extends Electrum_Task { - public ConnectTask(String host, int port) { - super(host, port); - } - - public ConnectTask(String host, int port, SharedData sharedData) { - super(host, port, sharedData); - } - @Override - protected void onProgressUpdate(Integer... values) { - super.onProgressUpdate(values); - } - - @Override - protected void onPostExecute(List requests) { - super.onPostExecute(requests); - for (Electrum_Request request : requests) { - try { - if (request.error == null) { - if (request.isMethod(Electrum_Request.METHOD_GetBalance)) { - try { - etFee.setText("--"); - - //String mWalletAddress = request.getParams().getString(0); - if ((request.getResult().getInt("confirmed") + request.getResult().getInt("unconfirmed")) / mCard.getBlockchain().getMultiplier() * 1000000.0 < Float.parseFloat(etAmount.getText().toString())) { - etFee.setError("Not enough funds"); - balanceRequestSuccess = false; - btnSend.setVisibility(View.INVISIBLE); - dtVerifyed=null; - nodeCheck = false; - } else { - etFee.setError(null); - balanceRequestSuccess = true; - if(feeRequestSuccess && balanceRequestSuccess) { - btnSend.setVisibility(View.VISIBLE); - - } - dtVerifyed=new Date(); - nodeCheck = true; - } - } catch (JSONException e) { - if(sharedCounter != null) - { - int errCounter = sharedCounter.errorRequest.incrementAndGet(); - if(errCounter >= sharedCounter.allRequest) - { - e.printStackTrace(); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot check balance! No connection with blockchain nodes"); - } - } - else - { - e.printStackTrace(); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot check balance! No connection with blockchain nodes"); - } - } - } else if (request.isMethod(Electrum_Request.METHOD_GetFee)) { - if (request.getResultString() == "-1") { - etFee.setText("3"); - } - } - } else { -// etFee.setError(request.error); -// btnSend.setVisibility(View.INVISIBLE); - if(sharedCounter != null) - { - int errCounter = sharedCounter.errorRequest.incrementAndGet(); - if(errCounter >= sharedCounter.allRequest) - { - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - } - } - else - { - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - } - return; - } - } catch (JSONException e) { - if(sharedCounter != null) - { - int errCounter = sharedCounter.errorRequest.incrementAndGet(); - if(errCounter >= sharedCounter.allRequest) - { - e.printStackTrace(); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - - } - } - else - { - e.printStackTrace(); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - } - } - } - - } - } - - private class ETHRequestTask extends Infura_Task { - ETHRequestTask(Blockchain blockchain){ - super(blockchain); - } - @Override - protected void onPostExecute(List requests) { - super.onPostExecute(requests); - for (Infura_Request request : requests) { - try { - Long price = 0L; - if (request.error == null) { - - if (request.isMethod(Infura_Request.METHOD_ETH_GetGasPrice)) { - try { - String gasPrice = request.getResultString(); - gasPrice = gasPrice.substring(2); - BigInteger l = new BigInteger(gasPrice, 16); - - BigInteger m = mCard.getBlockchain() == Blockchain.Token ? BigInteger.valueOf(55000) : BigInteger.valueOf(21000); - l = l.multiply(m); - String feeInGwei = mCard.getAmountInGwei(String.valueOf(l)); - - minFee=feeInGwei; - maxFee=feeInGwei; - normalFee=feeInGwei; - etFee.setText(feeInGwei); - etFee.setError(null); - btnSend.setVisibility(View.VISIBLE); - feeRequestSuccess = true; - balanceRequestSuccess = true; - - dtVerifyed=new Date(); - minFeeInInternalUnits = mCard.InternalUnitsFromString(feeInGwei); - - } catch (JSONException e) { - e.printStackTrace(); - FinishActivityWithError(Activity.RESULT_CANCELED, "Can't calculate fee! No connection with blockchain nodes"); - } - } - } else { - FinishActivityWithError(Activity.RESULT_CANCELED, "Can't calculate fee! No connection with blockchain nodes"); - } - } catch (JSONException e) { - e.printStackTrace(); - FinishActivityWithError(Activity.RESULT_CANCELED, "Can't calculate fee! No connection with blockchain nodes"); - } - } - } - } - - private class ConnectFeeTask extends Fee_Task { - public ConnectFeeTask(SharedData sharedData) { - super(sharedData); - } - @Override - protected void onPostExecute(List requests) { - super.onPostExecute(requests); - for (Fee_Request request : requests) { - if (request.error == null) { - long minFeeRate = 0; - - try { - - try { - String tmpAnswer = request.getAsString(); - BigDecimal minFeeBD = new BigDecimal(tmpAnswer); - BigDecimal multiplicator = new BigDecimal("100000000"); - minFeeBD = minFeeBD.multiply(multiplicator); - BigInteger minFeeBI = minFeeBD.toBigInteger(); - minFeeRate = minFeeBI.longValue(); - } catch (Exception e) { - - if(sharedCounter != null) - { - int errCounter = sharedCounter.errorRequest.incrementAndGet(); - - - if(errCounter >= sharedCounter.allRequest) - { - progressBar.setVisibility(View.INVISIBLE); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - } - } - else - { - progressBar.setVisibility(View.INVISIBLE); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - } - - //FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - return; - } - - if (minFeeRate == 0) { - progressBar.setVisibility(View.INVISIBLE); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! Wrong data received from the node"); - return; - } - - long inputCount = request.txSize; - - if (inputCount != 0) { - minFeeRate = minFeeRate * inputCount; - } else { - minFeeRate = minFeeRate * 256; - } - } catch (Exception e) { - e.printStackTrace(); - if(sharedCounter != null) - { - int errCounter = sharedCounter.errorRequest.incrementAndGet(); - if(errCounter >= sharedCounter.allRequest) - { - progressBar.setVisibility(View.INVISIBLE); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - } - } - else - { - progressBar.setVisibility(View.INVISIBLE); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - } - return; - } - - progressBar.setVisibility(View.INVISIBLE); - - float finalFee = (float) minFeeRate / (float) 10000; - - finalFee = Math.round(finalFee) / (float) 10000; - - if (request.getBlockCount() == Fee_Request.MINIMAL) { - minFee = String.valueOf(finalFee); - minFeeInInternalUnits = mCard.InternalUnitsFromString(String.valueOf(finalFee)); - } else if (request.getBlockCount() == Fee_Request.NORMAL) { - normalFee = String.valueOf(finalFee); - } else if (request.getBlockCount() == Fee_Request.PRIORITY) { - maxFee = String.valueOf(finalFee); - } - - doSetFee(rgFee.getCheckedRadioButtonId()); - - etFee.setError(null); - feeRequestSuccess = true; - if(feeRequestSuccess && balanceRequestSuccess) { - btnSend.setVisibility(View.VISIBLE); - } - dtVerifyed=new Date(); - - } else { - - if(sharedCounter != null) - { - int errCounter = sharedCounter.errorRequest.incrementAndGet(); - if(errCounter >= sharedCounter.allRequest) - { - progressBar.setVisibility(View.INVISIBLE); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - } - } - else - { - progressBar.setVisibility(View.INVISIBLE); - FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); - } - } - } - } - } - - private void doSetFee(int checkedRadioButtonId) { - switch (checkedRadioButtonId) { - case R.id.rbMinimalFee: - if (minFee != null) etFee.setText(minFee); - else etFee.setText("?"); - break; - case R.id.rbNormalFee: - if (normalFee != null) etFee.setText(normalFee); - else etFee.setText("?"); - break; - case R.id.rbMaximumFee: - if (maxFee != null) etFee.setText(maxFee); - else etFee.setText("?"); - break; - } - } - - @Override - public void onResume() { - super.onResume(); - mNfcManager.onResume(); - } - - @Override - public void onPause() { - super.onPause(); - mNfcManager.onPause(); - } - - @Override - public void onStop() { - super.onStop(); - mNfcManager.onStop(); - } -} +package com.tangem.presentation.activity; + +import android.app.Activity; +import android.content.Intent; +import android.nfc.NfcAdapter; +import android.nfc.Tag; +import android.os.AsyncTask; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.text.Editable; +import android.text.Html; +import android.text.Spanned; +import android.text.TextWatcher; +import android.util.Log; +import android.view.KeyEvent; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.ProgressBar; +import android.widget.RadioGroup; +import android.widget.TextView; +import android.widget.Toast; + +import com.tangem.cardReader.NfcManager; +import com.tangem.cardReader.Util; +import com.tangem.wallet.BTCUtils; +import com.tangem.wallet.Blockchain; +import com.tangem.wallet.CoinEngine; +import com.tangem.wallet.CoinEngineFactory; +import com.tangem.wallet.DerEncodingUtil; +import com.tangem.wallet.Electrum_Request; +import com.tangem.wallet.Electrum_Task; +import com.tangem.wallet.Fee_Request; +import com.tangem.wallet.Fee_Task; +import com.tangem.wallet.FormatUtil; +import com.tangem.wallet.Infura_Request; +import com.tangem.wallet.Infura_Task; +import com.tangem.wallet.R; +import com.tangem.wallet.SharedData; +import com.tangem.wallet.Tangem_Card; +import com.tangem.wallet.Transaction; +import com.tangem.wallet.UnspentOutputInfo; + +import org.json.JSONException; + +import java.io.IOException; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +public class ConfirmPaymentActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback { + + private static final int REQUEST_CODE_SIGN_PAYMENT = 1; + private static final int REQUEST_CODE_REQUEST_PIN2 = 2; + Button btnSend; + boolean feeRequestSuccess = false; + boolean balanceRequestSuccess = false; + EditText etWallet; + TextView tvCardID, tvBalance, tvCurrency, tvCurrency2, tvBalanceEquivalent, tvAmountEquivalent, tvFeeEquivalent; + EditText etAmount; + EditText etFee; + ImageView ivCamera; + Tangem_Card mCard; + RadioGroup rgFee; + String minFee = null, maxFee = null, normalFee = null; + Long minFeeInInternalUnits = 0L; + private NfcManager mNfcManager; + int requestPIN2Count = 0; + ProgressBar progressBar; + boolean nodeCheck = false; + Date dtVerifyed = null; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_confirm_payment); + + MainActivity.commonInit(getApplicationContext()); + mNfcManager = new NfcManager(this, this); + + mCard = new Tangem_Card(getIntent().getStringExtra("UID")); + mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); + + progressBar = findViewById(R.id.progressBar); + + btnSend = findViewById(R.id.btnSend); + etWallet = findViewById(R.id.etWallet); + tvCardID = findViewById(R.id.tvCardID); + tvBalance = findViewById(R.id.tvBalance); + tvCurrency = findViewById(R.id.tvCurrency); + tvCurrency2 = findViewById(R.id.tvCurrency2); + etAmount = findViewById(R.id.etAmount); + etFee = findViewById(R.id.etFee); + tvBalanceEquivalent = findViewById(R.id.tvBalanceEquivalent); + tvAmountEquivalent = findViewById(R.id.tvAmountEquivalent); + + tvFeeEquivalent = findViewById(R.id.tvFeeEquivalent); + ivCamera = findViewById(R.id.ivCamera); + + rgFee = findViewById(R.id.rgFee); + rgFee.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(RadioGroup group, int checkedId) { + doSetFee(checkedId); + } + }); + + etAmount.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + try { + + CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); + tvAmountEquivalent.setText(engine.GetAmountEqualentDescriptor(mCard, etAmount.getText().toString())); + if (!mCard.getAmountEquivalentDescriptionAvailable()) { + tvAmountEquivalent.setError("Service unavailable"); + } else { + tvAmountEquivalent.setError(null); + } + } catch (Exception e) { + e.printStackTrace(); + tvAmountEquivalent.setText(""); + } + } + + @Override + public void afterTextChanged(Editable s) { + + } + }); + + etFee.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + try { + + CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); + String eqFee = engine.EvaluteFeeEquivalent(mCard, etFee.getText().toString()); + tvFeeEquivalent.setText(eqFee); + + if (!mCard.getAmountEquivalentDescriptionAvailable()) { + tvFeeEquivalent.setError("Service unavailable"); + } else { + tvFeeEquivalent.setError(null); + } + } catch (Exception e) { + e.printStackTrace(); + tvFeeEquivalent.setText(""); + } + } + + @Override + public void afterTextChanged(Editable s) { + + } + }); + + CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); + //tvBalance.setText(engine.GetBalanceWithAlter(mCard)); + if (mCard.getBlockchain() == Blockchain.Token) { + Spanned html = Html.fromHtml(engine.GetBalanceWithAlter(mCard)); + tvBalance.setText(html); + } else { + tvBalance.setText(engine.GetBalanceWithAlter(mCard)); + } + etAmount.setText(getIntent().getStringExtra("Amount")); + tvCurrency.setText(engine.GetBalanceCurrency(mCard)); + tvCurrency2.setText(engine.GetFeeCurrency()); + + tvCardID.setText(mCard.getCIDDescription()); + + //tvBalanceEquivalent.setText(mCard.getBalanceEquivalentDescription()); + tvBalanceEquivalent.setText(engine.GetBalanceEquivalent(mCard)); + if (!mCard.getAmountEquivalentDescriptionAvailable()) { + tvBalanceEquivalent.setError("Service unavailable"); + } else { + tvBalanceEquivalent.setError(null); + } + + etWallet.setText(getIntent().getStringExtra("Wallet")); + + etFee.setText("?"); + + btnSend.setVisibility(View.INVISIBLE); + feeRequestSuccess = false; + balanceRequestSuccess = false; + btnSend.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.MINUTE, -1); + + if (dtVerifyed == null || dtVerifyed.before(calendar.getTime())) { + FinishActivityWithError(Activity.RESULT_CANCELED, "The obtained data is outdated! Try again"); + return; + } + + CoinEngine engineCoin = CoinEngineFactory.Create(mCard.getBlockchain()); + + if (engineCoin.IsNeedCheckNode() && !nodeCheck) { + Toast.makeText(getBaseContext(), "Cannot reach current active blockchain node. Try again", Toast.LENGTH_LONG).show(); + return; + } + String txFee = etFee.getText().toString(); + String txAmount = etAmount.getText().toString(); + + + if (!engineCoin.HasBalanceInfo(mCard)) { + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot check balance! No connection with blockchain nodes"); + return; + } else if (!engineCoin.IsBalanceNotZero(mCard)) { + FinishActivityWithError(Activity.RESULT_CANCELED, "The wallet is empty"); + return; + } else if (!engineCoin.CheckUnspentTransaction(mCard)) { + //else if (mCard.getUnspentTransactions().size() == 0 && mCard.getBlockchain() != Blockchain.Ethereum) { + FinishActivityWithError(Activity.RESULT_CANCELED, "Please wait for confirmation of incoming transaction"); + return; + } + + if (!engineCoin.CheckAmountValie(mCard, txAmount, txFee, minFeeInInternalUnits)) { + FinishActivityWithError(Activity.RESULT_CANCELED, "Fee exceeds payment amount. Enter correct value and repeat sending."); + return; + } + + requestPIN2Count = 0; + Intent intent = new Intent(getBaseContext(), RequestPINActivity.class); + intent.putExtra("mode", RequestPINActivity.Mode.RequestPIN2.toString()); + intent.putExtra("UID", mCard.getUID()); + intent.putExtra("Card", mCard.getAsBundle()); + startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2); + } + }); + + if (mCard.getBlockchain() == Blockchain.Ethereum || mCard.getBlockchain() == Blockchain.EthereumTestNet || mCard.getBlockchain() == Blockchain.Token) { + ETHRequestTask task = new ETHRequestTask(mCard.getBlockchain()); + Infura_Request req = Infura_Request.GetGasPrise(mCard.getWallet()); + req.setID(67); + req.setBlockchain(mCard.getBlockchain()); + rgFee.setEnabled(false); + task.execute(req); + } else { + + rgFee.setEnabled(true); + + SharedData data = new SharedData(SharedData.COUNT_REQUEST); + + CoinEngine engineCoin = CoinEngineFactory.Create(mCard.getBlockchain()); + + for (int i = 0; i < data.allRequest; ++i) { + + String nodeAddress = engineCoin.GetNextNode(mCard); + int nodePort = engineCoin.GetNextNodePort(mCard); + //ConnectTask connectTaskEx = new ConnectTask(Blockchain.getNextServiceHost(mCard), Blockchain.getNextServicePort(mCard), data); + ConnectTask connectTaskEx = new ConnectTask(nodeAddress, nodePort, data); + + //connectTaskEx.execute(Electrum_Request.CheckBalance(mCard.getWallet())); + connectTaskEx.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, Electrum_Request.CheckBalance(mCard.getWallet())); + } + + String nodeAddress = engineCoin.GetNode(mCard); + int nodePort = engineCoin.GetNodePort(mCard); + ConnectTask connectTask = new ConnectTask(nodeAddress, nodePort, data); + + //ConnectTask connectTask = new ConnectTask(Blockchain.getServiceHost(mCard), Blockchain.getServicePort(mCard)); + + connectTask.execute(/*Electrum_Request.CheckBalance(mCard.getWallet()), */Electrum_Request.GetFee(mCard.getWallet())); + + int calcSize = 256; + try { + calcSize = BuildSize(etWallet.getText().toString(), "0.00", etAmount.getText().toString()); + } catch (Exception ex) { + Log.e("Build Fee error", ex.getMessage()); + } + + SharedData sharedFee = new SharedData(SharedData.COUNT_REQUEST); + + progressBar.setVisibility(View.VISIBLE); + for (int i = 0; i < SharedData.COUNT_REQUEST; ++i) { + + ConnectFeeTask feeTask = new ConnectFeeTask(sharedFee); + + feeTask.execute(Fee_Request.GetFee(mCard.getWallet(), calcSize, Fee_Request.NORMAL), + Fee_Request.GetFee(mCard.getWallet(), calcSize, Fee_Request.MINIMAL), + Fee_Request.GetFee(mCard.getWallet(), calcSize, Fee_Request.PRIORITY)); + } + } + + } + + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + switch (keyCode) { + case KeyEvent.KEYCODE_BACK: + Intent intent = new Intent(); + intent.putExtra("message", "Operation canceled"); + setResult(Activity.RESULT_CANCELED, intent); + finish(); + return true; + } + return super.onKeyDown(keyCode, event); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == REQUEST_CODE_SIGN_PAYMENT) { + if (data != null && data.getExtras().containsKey("UID") && data.getExtras().containsKey("Card")) { + Tangem_Card updatedCard = new Tangem_Card(data.getStringExtra("UID")); + updatedCard.LoadFromBundle(data.getBundleExtra("Card")); + mCard = updatedCard; + } + if (resultCode == SignPaymentActivity.RESULT_INVALID_PIN && requestPIN2Count < 2) { + requestPIN2Count++; + Intent intent = new Intent(getBaseContext(), RequestPINActivity.class); + intent.putExtra("mode", RequestPINActivity.Mode.RequestPIN2.toString()); + intent.putExtra("UID", mCard.getUID()); + intent.putExtra("Card", mCard.getAsBundle()); + startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2); + return; + } + setResult(resultCode, data); + finish(); + } else if (requestCode == REQUEST_CODE_REQUEST_PIN2) { + if (resultCode == Activity.RESULT_OK) { + Intent intent = new Intent(getBaseContext(), SignPaymentActivity.class); + intent.putExtra("UID", mCard.getUID()); + intent.putExtra("Card", mCard.getAsBundle()); + intent.putExtra("Wallet", etWallet.getText().toString()); + intent.putExtra("Amount", etAmount.getText().toString()); + intent.putExtra("Fee", etFee.getText().toString()); + startActivityForResult(intent, REQUEST_CODE_SIGN_PAYMENT); + } else { + Toast.makeText(getBaseContext(), "PIN2 is required to sign the payment", Toast.LENGTH_LONG).show(); + } + } + } + + void FinishActivityWithError(int errorCode, String message) { + //Snackbar.make(etFee, message, Snackbar.LENGTH_LONG).show(); + Intent intent = new Intent(); + intent.putExtra("message", message); + setResult(errorCode, intent); + finish(); + } + + @Override + public void onTagDiscovered(Tag tag) { + try { + Log.w(getClass().getName(), "Ignore discovered tag!"); + mNfcManager.IgnoreTag(tag); + } catch (IOException e) { + e.printStackTrace(); + } + } + + int BuildSize(String outputAddress, String outFee, String outAmount) throws Exception { + String myAddress = mCard.getWallet(); + String changeAddress = myAddress; //"n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi"; + byte[] pbKey = mCard.getWalletPublicKey(); + byte[] pbComprKey = mCard.getWalletPublicKeyRar(); + + // Build script for our address + List rawTxList = mCard.getUnspentTransactions(); + byte[] outputScriptWeAreAbleToSpend = Transaction.Script.buildOutput(myAddress).bytes; + + // Collect unspent + ArrayList unspentOutputs = BTCUtils.getOutputs(rawTxList, outputScriptWeAreAbleToSpend); + + long fullAmount = 0; + for (int i = 0; i < unspentOutputs.size(); ++i) { + fullAmount += unspentOutputs.get(i).value; + } + + // Get first unspent + UnspentOutputInfo outPut = unspentOutputs.get(0); + int outPutIndex = outPut.outputIndex; + + // get prev TX id; + String prevTXID = rawTxList.get(0).txID;//"f67b838d6e2c0c587f476f583843e93ff20368eaf96a798bdc25e01f53f8f5d2"; + + long fees = FormatUtil.ConvertStringToLong(outFee); + long amount = FormatUtil.ConvertStringToLong(outAmount); + amount = amount - fees; + + long change = fullAmount - fees - amount; + + if (amount + fees > fullAmount) { + throw new Exception(String.format("Balance (%d) < amount (%d) + (%d)", fullAmount, change, amount)); + } + + byte[][] hashesForSign = new byte[unspentOutputs.size()][]; + + for (int i = 0; i < unspentOutputs.size(); ++i) { + byte[] newTX = BTCUtils.buildTXForSign(myAddress, outputAddress, changeAddress, unspentOutputs, i, amount, change); + + byte[] hashData = Util.calculateSHA256(newTX); + byte[] doubleHashData = Util.calculateSHA256(hashData); + + Log.e("TX_BODY_1", BTCUtils.toHex(newTX)); + Log.e("TX_HASH_1", BTCUtils.toHex(hashData)); + Log.e("TX_HASH_2", BTCUtils.toHex(doubleHashData)); + + unspentOutputs.get(i).bodyDoubleHash = doubleHashData; + unspentOutputs.get(i).bodyHash = hashData; + + hashesForSign[i] = doubleHashData; + } + + byte[] signFromCard = new byte[64 * unspentOutputs.size()]; + + for (int i = 0; i < unspentOutputs.size(); ++i) { + BigInteger r = new BigInteger(1, Arrays.copyOfRange(signFromCard, 0 + i * 64, 32 + i * 64)); + BigInteger s = new BigInteger(1, Arrays.copyOfRange(signFromCard, 32 + i * 64, 64 + i * 64)); + byte[] encodingSign = DerEncodingUtil.packSignDer(r, s, pbKey); + unspentOutputs.get(i).scriptForBuild = encodingSign; + } + + byte[] realTX = BTCUtils.buildTXForSend(outputAddress, changeAddress, unspentOutputs, amount, change); + + return realTX.length; + } + + private class ConnectTask extends Electrum_Task { + public ConnectTask(String host, int port) { + super(host, port); + } + + public ConnectTask(String host, int port, SharedData sharedData) { + super(host, port, sharedData); + } + + @Override + protected void onProgressUpdate(Integer... values) { + super.onProgressUpdate(values); + } + + @Override + protected void onPostExecute(List requests) { + super.onPostExecute(requests); + for (Electrum_Request request : requests) { + try { + if (request.error == null) { + if (request.isMethod(Electrum_Request.METHOD_GetBalance)) { + try { + etFee.setText("--"); + + //String mWalletAddress = request.getParams().getString(0); + if ((request.getResult().getInt("confirmed") + request.getResult().getInt("unconfirmed")) / mCard.getBlockchain().getMultiplier() * 1000000.0 < Float.parseFloat(etAmount.getText().toString())) { + etFee.setError("Not enough funds"); + balanceRequestSuccess = false; + btnSend.setVisibility(View.INVISIBLE); + dtVerifyed = null; + nodeCheck = false; + } else { + etFee.setError(null); + balanceRequestSuccess = true; + if (feeRequestSuccess && balanceRequestSuccess) { + btnSend.setVisibility(View.VISIBLE); + + } + dtVerifyed = new Date(); + nodeCheck = true; + } + } catch (JSONException e) { + if (sharedCounter != null) { + int errCounter = sharedCounter.errorRequest.incrementAndGet(); + if (errCounter >= sharedCounter.allRequest) { + e.printStackTrace(); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot check balance! No connection with blockchain nodes"); + } + } else { + e.printStackTrace(); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot check balance! No connection with blockchain nodes"); + } + } + } else if (request.isMethod(Electrum_Request.METHOD_GetFee)) { + if (request.getResultString() == "-1") { + etFee.setText("3"); + } + } + } else { +// etFee.setError(request.error); +// btnSend.setVisibility(View.INVISIBLE); + if (sharedCounter != null) { + int errCounter = sharedCounter.errorRequest.incrementAndGet(); + if (errCounter >= sharedCounter.allRequest) { + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + } + } else { + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + } + return; + } + } catch (JSONException e) { + if (sharedCounter != null) { + int errCounter = sharedCounter.errorRequest.incrementAndGet(); + if (errCounter >= sharedCounter.allRequest) { + e.printStackTrace(); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + + } + } else { + e.printStackTrace(); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + } + } + } + + } + } + + private class ETHRequestTask extends Infura_Task { + ETHRequestTask(Blockchain blockchain) { + super(blockchain); + } + + @Override + protected void onPostExecute(List requests) { + super.onPostExecute(requests); + for (Infura_Request request : requests) { + try { + Long price = 0L; + if (request.error == null) { + + if (request.isMethod(Infura_Request.METHOD_ETH_GetGasPrice)) { + try { + String gasPrice = request.getResultString(); + gasPrice = gasPrice.substring(2); + BigInteger l = new BigInteger(gasPrice, 16); + + BigInteger m = mCard.getBlockchain() == Blockchain.Token ? BigInteger.valueOf(55000) : BigInteger.valueOf(21000); + l = l.multiply(m); + String feeInGwei = mCard.getAmountInGwei(String.valueOf(l)); + + minFee = feeInGwei; + maxFee = feeInGwei; + normalFee = feeInGwei; + etFee.setText(feeInGwei); + etFee.setError(null); + btnSend.setVisibility(View.VISIBLE); + feeRequestSuccess = true; + balanceRequestSuccess = true; + + dtVerifyed = new Date(); + minFeeInInternalUnits = mCard.InternalUnitsFromString(feeInGwei); + + } catch (JSONException e) { + e.printStackTrace(); + FinishActivityWithError(Activity.RESULT_CANCELED, "Can't calculate fee! No connection with blockchain nodes"); + } + } + } else { + FinishActivityWithError(Activity.RESULT_CANCELED, "Can't calculate fee! No connection with blockchain nodes"); + } + } catch (JSONException e) { + e.printStackTrace(); + FinishActivityWithError(Activity.RESULT_CANCELED, "Can't calculate fee! No connection with blockchain nodes"); + } + } + } + } + + private class ConnectFeeTask extends Fee_Task { + public ConnectFeeTask(SharedData sharedData) { + super(sharedData); + } + + @Override + protected void onPostExecute(List requests) { + super.onPostExecute(requests); + for (Fee_Request request : requests) { + if (request.error == null) { + long minFeeRate = 0; + + try { + + try { + String tmpAnswer = request.getAsString(); + BigDecimal minFeeBD = new BigDecimal(tmpAnswer); + BigDecimal multiplicator = new BigDecimal("100000000"); + minFeeBD = minFeeBD.multiply(multiplicator); + BigInteger minFeeBI = minFeeBD.toBigInteger(); + minFeeRate = minFeeBI.longValue(); + } catch (Exception e) { + + if (sharedCounter != null) { + int errCounter = sharedCounter.errorRequest.incrementAndGet(); + + + if (errCounter >= sharedCounter.allRequest) { + progressBar.setVisibility(View.INVISIBLE); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + } + } else { + progressBar.setVisibility(View.INVISIBLE); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + } + + //FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + return; + } + + if (minFeeRate == 0) { + progressBar.setVisibility(View.INVISIBLE); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! Wrong data received from the node"); + return; + } + + long inputCount = request.txSize; + + if (inputCount != 0) { + minFeeRate = minFeeRate * inputCount; + } else { + minFeeRate = minFeeRate * 256; + } + } catch (Exception e) { + e.printStackTrace(); + if (sharedCounter != null) { + int errCounter = sharedCounter.errorRequest.incrementAndGet(); + if (errCounter >= sharedCounter.allRequest) { + progressBar.setVisibility(View.INVISIBLE); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + } + } else { + progressBar.setVisibility(View.INVISIBLE); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + } + return; + } + + progressBar.setVisibility(View.INVISIBLE); + + float finalFee = (float) minFeeRate / (float) 10000; + + finalFee = Math.round(finalFee) / (float) 10000; + + if (request.getBlockCount() == Fee_Request.MINIMAL) { + minFee = String.valueOf(finalFee); + minFeeInInternalUnits = mCard.InternalUnitsFromString(String.valueOf(finalFee)); + } else if (request.getBlockCount() == Fee_Request.NORMAL) { + normalFee = String.valueOf(finalFee); + } else if (request.getBlockCount() == Fee_Request.PRIORITY) { + maxFee = String.valueOf(finalFee); + } + + doSetFee(rgFee.getCheckedRadioButtonId()); + + etFee.setError(null); + feeRequestSuccess = true; + if (feeRequestSuccess && balanceRequestSuccess) { + btnSend.setVisibility(View.VISIBLE); + } + dtVerifyed = new Date(); + + } else { + + if (sharedCounter != null) { + int errCounter = sharedCounter.errorRequest.incrementAndGet(); + if (errCounter >= sharedCounter.allRequest) { + progressBar.setVisibility(View.INVISIBLE); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + } + } else { + progressBar.setVisibility(View.INVISIBLE); + FinishActivityWithError(Activity.RESULT_CANCELED, "Cannot calculate fee! No connection with blockchain nodes"); + } + } + } + } + } + + private void doSetFee(int checkedRadioButtonId) { + switch (checkedRadioButtonId) { + case R.id.rbMinimalFee: + if (minFee != null) etFee.setText(minFee); + else etFee.setText("?"); + break; + case R.id.rbNormalFee: + if (normalFee != null) etFee.setText(normalFee); + else etFee.setText("?"); + break; + case R.id.rbMaximumFee: + if (maxFee != null) etFee.setText(maxFee); + else etFee.setText("?"); + break; + } + } + + @Override + public void onResume() { + super.onResume(); + mNfcManager.onResume(); + } + + @Override + public void onPause() { + super.onPause(); + mNfcManager.onPause(); + } + + @Override + public void onStop() { + super.onStop(); + mNfcManager.onStop(); + } +} diff --git a/app/src/main/java/com/tangem/wallet/CreateNewWalletActivity.java b/app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.java similarity index 97% rename from app/src/main/java/com/tangem/wallet/CreateNewWalletActivity.java rename to app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.java index d9da77718f..0aae506554 100644 --- a/app/src/main/java/com/tangem/wallet/CreateNewWalletActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/CreateNewWalletActivity.java @@ -1,330 +1,334 @@ -package com.tangem.wallet; - -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.View; -import android.widget.ProgressBar; -import android.widget.TextView; -import android.widget.Toast; - -import com.tangem.cardReader.CardProtocol; -import com.tangem.cardReader.NfcManager; -import com.tangem.cardReader.Util; - -public class CreateNewWalletActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { - - public static final int RESULT_INVALID_PIN = Activity.RESULT_FIRST_USER; - private Tangem_Card mCard; - private TextView tvCardID; - private NfcManager mNfcManager; - private static final String logTag = "CreateNewActivity"; - private ProgressBar progressBar; - private CreateNewWalletTask createNewWalletTask; - private boolean lastReadSuccess = true; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_create_new_wallet); - - MainActivity.commonInit(getApplicationContext()); - mCard = new Tangem_Card(getIntent().getStringExtra("UID")); - mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); - - tvCardID = (TextView) findViewById(R.id.tvCardID); - tvCardID.setText(mCard.getCIDDescription()); - - mNfcManager = new NfcManager(this, this); - - progressBar = (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); - } - createNewWalletTask = new CreateNewWalletTask(isoDep, this); - createNewWalletTask.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 (createNewWalletTask != null) { - createNewWalletTask.cancel(true); - } - super.onPause(); - } - - @Override - public void onStop() { - // dismiss enable NFC dialog - mNfcManager.onStop(); - if (createNewWalletTask != null) { - createNewWalletTask.cancel(true); - } - super.onStop(); - } - -// @Override -// public Dialog CreateNFCDialog(int id, AlertDialogWrapper.Builder builder, LayoutInflater li) { -// return mNfcManager.ShowNFCEnableDialog(); //onCreateDialog(id, builder, li); -// } - - private class CreateNewWalletTask extends Thread { - - IsoDep mIsoDep; - 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(); - } - } - - } - - 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) { - - createNewWalletTask = 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)); - Intent intent = new Intent(); - intent.putExtra("UID", cardProtocol.getCard().getUID()); - intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); - setResult(Activity.RESULT_OK, intent); - finish(); - } - }); - } else { - lastReadSuccess = false; - 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.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 create wallet. 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() { - - createNewWalletTask = 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); - } -} - +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.View; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; + +import com.tangem.cardReader.CardProtocol; +import com.tangem.cardReader.NfcManager; +import com.tangem.cardReader.Util; +import com.tangem.wallet.NoExtendedLengthSupportDialog; +import com.tangem.wallet.PINStorage; +import com.tangem.wallet.R; +import com.tangem.wallet.Tangem_Card; +import com.tangem.wallet.WaitSecurityDelayDialog; + +public class CreateNewWalletActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { + + public static final int RESULT_INVALID_PIN = Activity.RESULT_FIRST_USER; + private Tangem_Card mCard; + private TextView tvCardID; + private NfcManager mNfcManager; + private static final String logTag = "CreateNewActivity"; + private ProgressBar progressBar; + private CreateNewWalletTask createNewWalletTask; + private boolean lastReadSuccess = true; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_create_new_wallet); + + MainActivity.commonInit(getApplicationContext()); + mCard = new Tangem_Card(getIntent().getStringExtra("UID")); + mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); + + tvCardID = (TextView) findViewById(R.id.tvCardID); + tvCardID.setText(mCard.getCIDDescription()); + + mNfcManager = new NfcManager(this, this); + + progressBar = (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); + } + createNewWalletTask = new CreateNewWalletTask(isoDep, this); + createNewWalletTask.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 (createNewWalletTask != null) { + createNewWalletTask.cancel(true); + } + super.onPause(); + } + + @Override + public void onStop() { + // dismiss enable NFC dialog + mNfcManager.onStop(); + if (createNewWalletTask != null) { + createNewWalletTask.cancel(true); + } + super.onStop(); + } + +// @Override +// public Dialog CreateNFCDialog(int id, AlertDialogWrapper.Builder builder, LayoutInflater li) { +// return mNfcManager.ShowNFCEnableDialog(); //onCreateDialog(id, builder, li); +// } + + private class CreateNewWalletTask extends Thread { + + IsoDep mIsoDep; + 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(); + } + } + + } + + 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) { + + createNewWalletTask = 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)); + Intent intent = new Intent(); + intent.putExtra("UID", cardProtocol.getCard().getUID()); + intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); + setResult(Activity.RESULT_OK, intent); + finish(); + } + }); + } else { + lastReadSuccess = false; + 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.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 create wallet. 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() { + + createNewWalletTask = 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); + } +} + diff --git a/app/src/main/java/com/tangem/wallet/EmptyWalletActivity.java b/app/src/main/java/com/tangem/presentation/activity/EmptyWalletActivity.java similarity index 97% rename from app/src/main/java/com/tangem/wallet/EmptyWalletActivity.java rename to app/src/main/java/com/tangem/presentation/activity/EmptyWalletActivity.java index 4b5db2dc7a..5e27be5e8a 100644 --- a/app/src/main/java/com/tangem/wallet/EmptyWalletActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/EmptyWalletActivity.java @@ -1,356 +1,361 @@ -package com.tangem.wallet; - -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.View; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.ProgressBar; -import android.widget.TextView; -import android.widget.Toast; - -import com.tangem.cardReader.CardProtocol; -import com.tangem.cardReader.NfcManager; -import com.tangem.cardReader.Util; - -public class EmptyWalletActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { - - private static final int REQUEST_CODE_CREATE_NEW_WALLET_ACTIVITY = 2; - private static final int REQUEST_CODE_REQUEST_PIN2 = 3; - private static final int REQUEST_CODE_VERIFY_CARD = 4; - Tangem_Card mCard; - TextView tvCardID, tvIssuer, tvIssuerData, tvBlockchain; - ProgressBar progressBar; - ImageView ivBlockchain, ivPIN, ivPIN2orSecurityDelay, ivDeveloperVersion; - - private NfcManager mNfcManager; - private final String logTag = "EmptyWalletActivity"; - private boolean lastReadSuccess = true; - private VerifyCardTask verifyCardTask = null; - private int requestPIN2Count = 0; - - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_empty_wallet); - - MainActivity.commonInit(getApplicationContext()); - mNfcManager = new NfcManager(this, this); - - mCard = new Tangem_Card(getIntent().getStringExtra("UID")); - mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); - - tvCardID = findViewById(R.id.tvCardID); - tvCardID.setText(mCard.getCIDDescription()); - - tvIssuer = findViewById(R.id.tvIssuer); - tvIssuerData = findViewById(R.id.tvIssuerData); - tvBlockchain = findViewById(R.id.tvBlockchain); - - tvIssuer.setText(mCard.getIssuerDescription()); - tvIssuerData.setText(mCard.getIssuerDataDescription()); - - //tvBlockchain.setText(mCard.getBlockchain().getOfficialName()); - tvBlockchain.setText(mCard.getBlockchainName()); - progressBar = findViewById(R.id.progressBar); - - ivBlockchain = findViewById(R.id.imgBlockchain); - ivPIN = findViewById(R.id.imgPIN); - ivPIN2orSecurityDelay = findViewById(R.id.imgPIN2orSecurityDelay); - ivDeveloperVersion = findViewById(R.id.imgDeveloperVersion); - - ivBlockchain.setImageResource(mCard.getBlockchain().getImageResource(this, mCard.getTokenSymbol())); - - if (mCard.useDefaultPIN1()) { - ivPIN.setImageResource(R.drawable.unlock_pin1); - ivPIN.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(EmptyWalletActivity.this, "This banknote is protected by default PIN1 code", Toast.LENGTH_LONG).show(); - } - }); - } else { - ivPIN.setImageResource(R.drawable.lock_pin1); - ivPIN.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(EmptyWalletActivity.this, "This banknote is protected by user's PIN1 code", Toast.LENGTH_LONG).show(); - } - }); - } - - if (mCard.getPauseBeforePIN2() > 0 && (mCard.useDefaultPIN2() || !mCard.useSmartSecurityDelay())) { - ivPIN2orSecurityDelay.setImageResource(R.drawable.timer); - ivPIN2orSecurityDelay.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(EmptyWalletActivity.this, String.format("This banknote will enforce %.0f seconds security delay for all operations requiring PIN2 code", mCard.getPauseBeforePIN2() / 1000.0), Toast.LENGTH_LONG).show(); - } - }); - - } else if (mCard.useDefaultPIN2()) { - ivPIN2orSecurityDelay.setImageResource(R.drawable.unlock_pin2); - ivPIN2orSecurityDelay.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(EmptyWalletActivity.this, "This banknote is protected by default PIN2 code", Toast.LENGTH_LONG).show(); - } - }); - } else { - ivPIN2orSecurityDelay.setImageResource(R.drawable.lock_pin2); - ivPIN2orSecurityDelay.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(EmptyWalletActivity.this, "This banknote is protected by user's PIN2 code", Toast.LENGTH_LONG).show(); - } - }); - } - - - if (mCard.useDevelopersFirmware()) { - ivDeveloperVersion.setImageResource(R.drawable.ic_developer_version); - ivDeveloperVersion.setVisibility(View.VISIBLE); - ivDeveloperVersion.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Toast.makeText(EmptyWalletActivity.this, "Unlocked banknote, only for development use", Toast.LENGTH_LONG).show(); - } - }); - } else { - ivDeveloperVersion.setVisibility(View.INVISIBLE); - } - - Button btnNewWallet = findViewById(R.id.btnNewWallet); - btnNewWallet.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - //CreateSelectBlockchainDialog(); - requestPIN2Count = 0; - Intent intent = new Intent(getBaseContext(), RequestPINActivity.class); - intent.putExtra("mode", RequestPINActivity.Mode.RequestPIN2.toString()); - intent.putExtra("UID", mCard.getUID()); - intent.putExtra("Card", mCard.getAsBundle()); - startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2); - } - }); - - if (getIntent().getExtras().containsKey(NfcAdapter.EXTRA_TAG)) { - Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG); - if (tag != null) { - onTagDiscovered(tag); - } - } - } - - - private void doCreateNewWallet() { - Intent intent = new Intent(this, CreateNewWalletActivity.class); - - intent.putExtra("UID", mCard.getUID()); - intent.putExtra("Card", mCard.getAsBundle()); - -// intent.putExtra("newPIN",mCard.getPIN()); -// intent.putExtra("newPIN2","12345678"); - startActivityForResult(intent, REQUEST_CODE_CREATE_NEW_WALLET_ACTIVITY); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == REQUEST_CODE_CREATE_NEW_WALLET_ACTIVITY) { - if (resultCode == Activity.RESULT_OK) { - - if (data != null) { - data.putExtra("modification", "updateAndViewCard"); - data.putExtra("updateDelay", 0); - setResult(Activity.RESULT_OK, data); - } - finish(); - } else { - if (data != null && data.getExtras().containsKey("UID") && data.getExtras().containsKey("Card")) { - Tangem_Card updatedCard = new Tangem_Card(data.getStringExtra("UID")); - updatedCard.LoadFromBundle(data.getBundleExtra("Card")); - mCard = updatedCard; - } - if (resultCode == CreateNewWalletActivity.RESULT_INVALID_PIN && requestPIN2Count < 2) { - requestPIN2Count++; - Intent intent = new Intent(getBaseContext(), RequestPINActivity.class); - intent.putExtra("mode", RequestPINActivity.Mode.RequestPIN2.toString()); - intent.putExtra("UID", mCard.getUID()); - intent.putExtra("Card", mCard.getAsBundle()); - startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2); - return; - } - } - setResult(resultCode, data); - finish(); - } else if (requestCode == REQUEST_CODE_REQUEST_PIN2) { - if (resultCode == Activity.RESULT_OK) { - doCreateNewWallet(); - } - } - - } - - @Override - public void onTagDiscovered(Tag tag) { - try { - 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); - if (!mCard.getUID().equals(sUID)) { - Log.d(logTag, "Invalid UID: " + sUID); - mNfcManager.IgnoreTag(isoDep.getTag()); - return; - } else { - Log.v(logTag, "UID: " + sUID); - } - - if (lastReadSuccess) { - isoDep.setTimeout(1000); - } else { - isoDep.setTimeout(65000); - } - //lastTag = tag; - verifyCardTask = new VerifyCardTask(this, mCard, mNfcManager, isoDep, this); - verifyCardTask.start(); - } 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) { - - verifyCardTask = 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)); - Intent intent = new Intent(EmptyWalletActivity.this, VerifyCardActivity.class); - // TODO обновить карту mCard - intent.putExtra("UID", cardProtocol.getCard().getUID()); - intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); - startActivityForResult(intent, REQUEST_CODE_VERIFY_CARD); - //addCard(cardProtocol.getCard()); - } - }); - } else { - // remove last UIDs because of error and no card read - progressBar.post(new Runnable() { - @Override - public void run() { - lastReadSuccess = false; - if (cardProtocol.getError() instanceof CardProtocol.TangemException_ExtendedLengthNotSupported) { - if (!NoExtendedLengthSupportDialog.allreadyShowed) { - new NoExtendedLengthSupportDialog().show(getFragmentManager(), "NoExtendedLengthSupportDialog"); - } - } else { - Toast.makeText(EmptyWalletActivity.this, "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() { - - verifyCardTask = 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(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 void onResume() { - super.onResume(); - mNfcManager.onResume(); - } - - @Override - public void onPause() { - super.onPause(); - mNfcManager.onPause(); - } - - @Override - public void onStop() { - super.onStop(); - mNfcManager.onStop(); - } -} +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.View; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; + +import com.tangem.cardReader.CardProtocol; +import com.tangem.cardReader.NfcManager; +import com.tangem.cardReader.Util; +import com.tangem.wallet.NoExtendedLengthSupportDialog; +import com.tangem.wallet.R; +import com.tangem.wallet.Tangem_Card; +import com.tangem.wallet.VerifyCardTask; +import com.tangem.wallet.WaitSecurityDelayDialog; + +public class EmptyWalletActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { + + private static final int REQUEST_CODE_CREATE_NEW_WALLET_ACTIVITY = 2; + private static final int REQUEST_CODE_REQUEST_PIN2 = 3; + private static final int REQUEST_CODE_VERIFY_CARD = 4; + Tangem_Card mCard; + TextView tvCardID, tvIssuer, tvIssuerData, tvBlockchain; + ProgressBar progressBar; + ImageView ivBlockchain, ivPIN, ivPIN2orSecurityDelay, ivDeveloperVersion; + + private NfcManager mNfcManager; + private final String logTag = "EmptyWalletActivity"; + private boolean lastReadSuccess = true; + private VerifyCardTask verifyCardTask = null; + private int requestPIN2Count = 0; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_empty_wallet); + + MainActivity.commonInit(getApplicationContext()); + mNfcManager = new NfcManager(this, this); + + mCard = new Tangem_Card(getIntent().getStringExtra("UID")); + mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); + + tvCardID = findViewById(R.id.tvCardID); + tvCardID.setText(mCard.getCIDDescription()); + + tvIssuer = findViewById(R.id.tvIssuer); + tvIssuerData = findViewById(R.id.tvIssuerData); + tvBlockchain = findViewById(R.id.tvBlockchain); + + tvIssuer.setText(mCard.getIssuerDescription()); + tvIssuerData.setText(mCard.getIssuerDataDescription()); + + //tvBlockchain.setText(mCard.getBlockchain().getOfficialName()); + tvBlockchain.setText(mCard.getBlockchainName()); + progressBar = findViewById(R.id.progressBar); + + ivBlockchain = findViewById(R.id.imgBlockchain); + ivPIN = findViewById(R.id.imgPIN); + ivPIN2orSecurityDelay = findViewById(R.id.imgPIN2orSecurityDelay); + ivDeveloperVersion = findViewById(R.id.imgDeveloperVersion); + + ivBlockchain.setImageResource(mCard.getBlockchain().getImageResource(this, mCard.getTokenSymbol())); + + if (mCard.useDefaultPIN1()) { + ivPIN.setImageResource(R.drawable.unlock_pin1); + ivPIN.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(EmptyWalletActivity.this, "This banknote is protected by default PIN1 code", Toast.LENGTH_LONG).show(); + } + }); + } else { + ivPIN.setImageResource(R.drawable.lock_pin1); + ivPIN.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(EmptyWalletActivity.this, "This banknote is protected by user's PIN1 code", Toast.LENGTH_LONG).show(); + } + }); + } + + if (mCard.getPauseBeforePIN2() > 0 && (mCard.useDefaultPIN2() || !mCard.useSmartSecurityDelay())) { + ivPIN2orSecurityDelay.setImageResource(R.drawable.timer); + ivPIN2orSecurityDelay.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(EmptyWalletActivity.this, String.format("This banknote will enforce %.0f seconds security delay for all operations requiring PIN2 code", mCard.getPauseBeforePIN2() / 1000.0), Toast.LENGTH_LONG).show(); + } + }); + + } else if (mCard.useDefaultPIN2()) { + ivPIN2orSecurityDelay.setImageResource(R.drawable.unlock_pin2); + ivPIN2orSecurityDelay.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(EmptyWalletActivity.this, "This banknote is protected by default PIN2 code", Toast.LENGTH_LONG).show(); + } + }); + } else { + ivPIN2orSecurityDelay.setImageResource(R.drawable.lock_pin2); + ivPIN2orSecurityDelay.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(EmptyWalletActivity.this, "This banknote is protected by user's PIN2 code", Toast.LENGTH_LONG).show(); + } + }); + } + + + if (mCard.useDevelopersFirmware()) { + ivDeveloperVersion.setImageResource(R.drawable.ic_developer_version); + ivDeveloperVersion.setVisibility(View.VISIBLE); + ivDeveloperVersion.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(EmptyWalletActivity.this, "Unlocked banknote, only for development use", Toast.LENGTH_LONG).show(); + } + }); + } else { + ivDeveloperVersion.setVisibility(View.INVISIBLE); + } + + Button btnNewWallet = findViewById(R.id.btnNewWallet); + btnNewWallet.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + //CreateSelectBlockchainDialog(); + requestPIN2Count = 0; + Intent intent = new Intent(getBaseContext(), RequestPINActivity.class); + intent.putExtra("mode", RequestPINActivity.Mode.RequestPIN2.toString()); + intent.putExtra("UID", mCard.getUID()); + intent.putExtra("Card", mCard.getAsBundle()); + startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2); + } + }); + + if (getIntent().getExtras().containsKey(NfcAdapter.EXTRA_TAG)) { + Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG); + if (tag != null) { + onTagDiscovered(tag); + } + } + } + + + private void doCreateNewWallet() { + Intent intent = new Intent(this, CreateNewWalletActivity.class); + + intent.putExtra("UID", mCard.getUID()); + intent.putExtra("Card", mCard.getAsBundle()); + +// intent.putExtra("newPIN",mCard.getPIN()); +// intent.putExtra("newPIN2","12345678"); + startActivityForResult(intent, REQUEST_CODE_CREATE_NEW_WALLET_ACTIVITY); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_CODE_CREATE_NEW_WALLET_ACTIVITY) { + if (resultCode == Activity.RESULT_OK) { + + if (data != null) { + data.putExtra("modification", "updateAndViewCard"); + data.putExtra("updateDelay", 0); + setResult(Activity.RESULT_OK, data); + } + finish(); + } else { + if (data != null && data.getExtras().containsKey("UID") && data.getExtras().containsKey("Card")) { + Tangem_Card updatedCard = new Tangem_Card(data.getStringExtra("UID")); + updatedCard.LoadFromBundle(data.getBundleExtra("Card")); + mCard = updatedCard; + } + if (resultCode == CreateNewWalletActivity.RESULT_INVALID_PIN && requestPIN2Count < 2) { + requestPIN2Count++; + Intent intent = new Intent(getBaseContext(), RequestPINActivity.class); + intent.putExtra("mode", RequestPINActivity.Mode.RequestPIN2.toString()); + intent.putExtra("UID", mCard.getUID()); + intent.putExtra("Card", mCard.getAsBundle()); + startActivityForResult(intent, REQUEST_CODE_REQUEST_PIN2); + return; + } + } + setResult(resultCode, data); + finish(); + } else if (requestCode == REQUEST_CODE_REQUEST_PIN2) { + if (resultCode == Activity.RESULT_OK) { + doCreateNewWallet(); + } + } + + } + + @Override + public void onTagDiscovered(Tag tag) { + try { + 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); + if (!mCard.getUID().equals(sUID)) { + Log.d(logTag, "Invalid UID: " + sUID); + mNfcManager.IgnoreTag(isoDep.getTag()); + return; + } else { + Log.v(logTag, "UID: " + sUID); + } + + if (lastReadSuccess) { + isoDep.setTimeout(1000); + } else { + isoDep.setTimeout(65000); + } + //lastTag = tag; + verifyCardTask = new VerifyCardTask(this, mCard, mNfcManager, isoDep, this); + verifyCardTask.start(); + } 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) { + + verifyCardTask = 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)); + Intent intent = new Intent(EmptyWalletActivity.this, VerifyCardActivity.class); + // TODO обновить карту mCard + intent.putExtra("UID", cardProtocol.getCard().getUID()); + intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); + startActivityForResult(intent, REQUEST_CODE_VERIFY_CARD); + //addCard(cardProtocol.getCard()); + } + }); + } else { + // remove last UIDs because of error and no card read + progressBar.post(new Runnable() { + @Override + public void run() { + lastReadSuccess = false; + if (cardProtocol.getError() instanceof CardProtocol.TangemException_ExtendedLengthNotSupported) { + if (!NoExtendedLengthSupportDialog.allreadyShowed) { + new NoExtendedLengthSupportDialog().show(getFragmentManager(), "NoExtendedLengthSupportDialog"); + } + } else { + Toast.makeText(EmptyWalletActivity.this, "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() { + + verifyCardTask = 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(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 void onResume() { + super.onResume(); + mNfcManager.onResume(); + } + + @Override + public void onPause() { + super.onPause(); + mNfcManager.onPause(); + } + + @Override + public void onStop() { + super.onStop(); + mNfcManager.onStop(); + } +} diff --git a/app/src/main/java/com/tangem/wallet/LoadedWalletActivity.java b/app/src/main/java/com/tangem/presentation/activity/LoadedWalletActivity.java similarity index 91% rename from app/src/main/java/com/tangem/wallet/LoadedWalletActivity.java rename to app/src/main/java/com/tangem/presentation/activity/LoadedWalletActivity.java index 3021483689..b2c1f2c7dc 100644 --- a/app/src/main/java/com/tangem/wallet/LoadedWalletActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/LoadedWalletActivity.java @@ -1,37 +1,40 @@ -package com.tangem.wallet; - -import android.app.Activity; -import android.content.Intent; -import android.nfc.NfcAdapter; -import android.nfc.Tag; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; - - -public class LoadedWalletActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_loaded_wallet); - MainActivity.commonInit(getApplicationContext()); - - if( getIntent().getExtras().containsKey(NfcAdapter.EXTRA_TAG) ) - { - Tag tag=getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG); - if (tag != null ) { - LoadedWalletActivityFragment fragment=(LoadedWalletActivityFragment)(getSupportFragmentManager().findFragmentById(R.id.loaded_wallet_fragment)); - fragment.onTagDiscovered(tag); - } - } - } - - @Override - public void onBackPressed() { - LoadedWalletActivityFragment loadedWalletActivityFragment=(LoadedWalletActivityFragment) getSupportFragmentManager().findFragmentById(R.id.loaded_wallet_fragment); - Intent data= loadedWalletActivityFragment.prepareResultIntent(); - data.putExtra("modification", "update"); - setResult(Activity.RESULT_OK, data); - finish(); - } -} +package com.tangem.presentation.activity; + +import android.app.Activity; +import android.content.Intent; +import android.nfc.NfcAdapter; +import android.nfc.Tag; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; + +import com.tangem.wallet.LoadedWalletActivityFragment; +import com.tangem.wallet.R; + + +public class LoadedWalletActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_loaded_wallet); + MainActivity.commonInit(getApplicationContext()); + + if( getIntent().getExtras().containsKey(NfcAdapter.EXTRA_TAG) ) + { + Tag tag=getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG); + if (tag != null ) { + LoadedWalletActivityFragment fragment=(LoadedWalletActivityFragment)(getSupportFragmentManager().findFragmentById(R.id.loaded_wallet_fragment)); + fragment.onTagDiscovered(tag); + } + } + } + + @Override + public void onBackPressed() { + LoadedWalletActivityFragment loadedWalletActivityFragment=(LoadedWalletActivityFragment) getSupportFragmentManager().findFragmentById(R.id.loaded_wallet_fragment); + Intent data= loadedWalletActivityFragment.prepareResultIntent(); + data.putExtra("modification", "update"); + setResult(Activity.RESULT_OK, data); + finish(); + } +} diff --git a/app/src/main/java/com/tangem/wallet/LogoActivity.java b/app/src/main/java/com/tangem/presentation/activity/LogoActivity.java similarity index 94% rename from app/src/main/java/com/tangem/wallet/LogoActivity.java rename to app/src/main/java/com/tangem/presentation/activity/LogoActivity.java index 7764ba0048..cd5f60b0d2 100644 --- a/app/src/main/java/com/tangem/wallet/LogoActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/LogoActivity.java @@ -1,68 +1,71 @@ -package com.tangem.wallet; - -import android.content.Intent; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; - -/** - * An example full-screen activity that shows and hides the system UI (i.e. - * status bar and navigation/system bar) with user interaction. - */ -public class LogoActivity extends AppCompatActivity { - - private final Runnable mHideRunnable = new Runnable() { - @Override - public void run() { - hide(); - } - }; - - ImageView imgLogo; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.activity_logo); - - imgLogo= (ImageView) findViewById(R.id.imgLogo); - // Set up the user interaction to manually show or hide the system UI. - imgLogo.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) {hide(); - } - }); - } - - @Override - protected void onPostCreate(Bundle savedInstanceState) { - super.onPostCreate(savedInstanceState); - - // Trigger the initial hide() shortly after the activity has been - // created, to briefly hint to the user that UI controls - // are available. - TextView AppVersion = (TextView) findViewById(R.id.AppVersion); - AppVersion.setText("BETA v." + BuildConfig.VERSION_NAME); - if( !getIntent().getBooleanExtra("skipAutoHide",false)) { - delayedHide(1000); - } - } - - private void hide() { - Intent intent=new Intent(getBaseContext(),MainActivity.class); - startActivity(intent); - finish(); - } - - /** - * Schedules a call to hide() in [delay] milliseconds, canceling any - * previously scheduled calls. - */ - private void delayedHide(int delayMillis) { - //imgLogo.removeCallbacks(mHideRunnable); - imgLogo.postDelayed(mHideRunnable, delayMillis); - } -} +package com.tangem.presentation.activity; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import com.tangem.wallet.BuildConfig; +import com.tangem.wallet.R; + +/** + * An example full-screen activity that shows and hides the system UI (i.e. + * status bar and navigation/system bar) with user interaction. + */ +public class LogoActivity extends AppCompatActivity { + + private final Runnable mHideRunnable = new Runnable() { + @Override + public void run() { + hide(); + } + }; + + ImageView imgLogo; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.activity_logo); + + imgLogo= (ImageView) findViewById(R.id.imgLogo); + // Set up the user interaction to manually show or hide the system UI. + imgLogo.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) {hide(); + } + }); + } + + @Override + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + + // Trigger the initial hide() shortly after the activity has been + // created, to briefly hint to the user that UI controls + // are available. + TextView AppVersion = (TextView) findViewById(R.id.AppVersion); + AppVersion.setText("BETA v." + BuildConfig.VERSION_NAME); + if( !getIntent().getBooleanExtra("skipAutoHide",false)) { + delayedHide(1000); + } + } + + private void hide() { + Intent intent=new Intent(getBaseContext(),MainActivity.class); + startActivity(intent); + finish(); + } + + /** + * Schedules a call to hide() in [delay] milliseconds, canceling any + * previously scheduled calls. + */ + private void delayedHide(int delayMillis) { + //imgLogo.removeCallbacks(mHideRunnable); + imgLogo.postDelayed(mHideRunnable, delayMillis); + } +} diff --git a/app/src/main/java/com/tangem/wallet/MainActivity.java b/app/src/main/java/com/tangem/presentation/activity/MainActivity.java similarity index 97% rename from app/src/main/java/com/tangem/wallet/MainActivity.java rename to app/src/main/java/com/tangem/presentation/activity/MainActivity.java index 7d7b2001ed..4bc9231baa 100644 --- a/app/src/main/java/com/tangem/wallet/MainActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/MainActivity.java @@ -1,404 +1,413 @@ -package com.tangem.wallet; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.app.DialogFragment; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.pm.ActivityInfo; -import android.content.pm.ResolveInfo; -import android.net.Uri; -import android.nfc.NfcAdapter; -import android.nfc.Tag; -import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.PopupMenu; -import android.util.Log; -import android.view.KeyEvent; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.animation.Animation; -import android.view.animation.DecelerateInterpolator; -import android.view.animation.Transformation; -import android.widget.LinearLayout; -import android.widget.RelativeLayout; -import android.widget.TextView; - -import com.scottyab.rootbeer.RootBeer; -import com.skyfishjy.library.RippleBackground; - -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - - -public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener { - - public static final int DIALOG_ENABLE_INTERNET = 1; - private static final int REQUEST_CODE_SEND_EMAIL = 2; - private String logTag = "MainActivity"; - - public interface OnCardsClean { - void doClean(); - } - -// public interface OnCreateNFCDialog { -// Dialog CreateNFCDialog(int id, AlertDialogWrapper.Builder builder, LayoutInflater li); -// } - - OnCardsClean onCardsClean; -// OnCreateNFCDialog onCreateNFCDialog; - NfcAdapter.ReaderCallback onNFCReaderCallback; - FloatingActionButton fab; - - - public void setOnCardsClean(OnCardsClean onCardsClean) { - this.onCardsClean = onCardsClean; - } - -// public void setOnCreateNFCDialog(OnCreateNFCDialog onCreateNFCDialog) { -// this.onCreateNFCDialog = onCreateNFCDialog; -// } - - public void setNfcAdapterReaderCallback(NfcAdapter.ReaderCallback callback) { - this.onNFCReaderCallback = callback; - } - - public void showCleanButton() { - - findViewById(R.id.tvTapPrompt).setVisibility(View.INVISIBLE); - } - - public void hideCleanButton() { - findViewById(R.id.tvTapPrompt).setVisibility(View.VISIBLE); - } - - public static class RootFoundDialog extends DialogFragment { - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - - return new AlertDialog.Builder(getActivity()) - .setIcon(R.drawable.tangem_logo_small_new) - .setTitle("Your Android device is rooted. Security at risk!") - .setCancelable(false) - .setPositiveButton("Got it", - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - } - } - ) - .create(); - } - - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - RootBeer rootBeer = new RootBeer(this); - if (rootBeer.isRootedWithoutBusyBoxCheck()) { - //we found indication of root - new RootFoundDialog().show(getFragmentManager(), "RootFoundDialog"); - } - - setContentView(R.layout.activity_main); - setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); - - commonInit(getApplicationContext()); - - TextView tvNFCHint = findViewById(R.id.tvNFCHint); - if(tvNFCHint != null) - { -// tvNFCHint.setText("Scan a banknote with your\n" + PhoneUtility.GetPhoneName() + "\nas shown above"); - tvNFCHint.setText("Scan a banknote with your\n smartphone as shown above"); - } - - DeviceNFCAntennaLocation antenna = new DeviceNFCAntennaLocation(); - antenna.getAntennaLocation(); - final LinearLayout hand = findViewById(R.id.llHand); - final LinearLayout nfc = findViewById(R.id.llNFC); - final RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) hand.getLayoutParams(); - final RelativeLayout.LayoutParams lp2 = (RelativeLayout.LayoutParams) nfc.getLayoutParams(); - final float dp = getResources().getDisplayMetrics().density; - final float lm = dp*(69 + antenna.X * 75); - lp.topMargin = (int) (dp*(-100 + antenna.Y * 250)); - lp2.topMargin = (int) (dp*(-125 + antenna.Y * 250)); - nfc.setLayoutParams(lp2); - - Animation a = new Animation() { - - @Override - protected void applyTransformation(float interpolatedTime, Transformation t) { - lp.leftMargin = (int)(lm * interpolatedTime); - hand.setLayoutParams(lp); - } - }; - a.setDuration(2000); // in ms - a.setInterpolator(new DecelerateInterpolator()); - hand.startAnimation(a); - - fab = findViewById(R.id.fab); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - - showMenu(view); - } - }); - - MainActivityFragment mainActivityFragment=(MainActivityFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentMain); - if( mainActivityFragment.getCardListAdapter().getItemCount()>0 ) - { - showCleanButton(); - }else { - hideCleanButton(); - } - - final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.imNFC); - rippleBackground.startRippleAnimation(); - - - Intent intent = getIntent(); - if (intent != null && (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))) { - Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); - if (tag != null && onNFCReaderCallback != null) { - onNFCReaderCallback.onTagDiscovered(tag); - } - } - } - - public static void commonInit(Context context) - { - if( PINStorage.needInit() ) { - PINStorage.Init(context); - } - if( LastSignStorage.needInit() ) { - LastSignStorage.Init(context); - } - } - - @Override - protected void onDestroy() { -// Logger.StopSaveToFile(getApplicationContext()); - super.onDestroy(); - } - - @Override - protected void onNewIntent(Intent intent) { - super.onNewIntent(intent); - if (intent != null && (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))) { - Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); - if (tag != null && onNFCReaderCallback != null) { - onNFCReaderCallback.onTagDiscovered(tag); - } - } - - } - - @Override - public boolean onKeyDown(int keycode, KeyEvent e) { - switch (keycode) { - case KeyEvent.KEYCODE_MENU: - fab.requestFocus(); - showMenu(fab); - return true; - } - - return super.onKeyDown(keycode, e); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_main, menu); - if( BuildConfig.DEBUG ) { - for(int i=0; i 0) { - String[] fileNames = new String[filelocations.length]; - for (int i = 0; i < filelocations.length; i++) - fileNames[i] = filelocations[i].getAbsolutePath(); - zipFile = File.createTempFile("tangemLogs", ".zip", filelocations[0].getParentFile()); - Compress compress = new Compress(fileNames, zipFile.getAbsolutePath()); - compress.zip(); - Log.e(logTag, String.format("Send %d bytes zip with logs", zipFile.length())); - Uri attachment = Uri.parse("content://" + LogFileProvider.AUTHORITY + "/" - + zipFile.getName()); - - intent.putExtra(Intent.EXTRA_STREAM, attachment); - zipFile.deleteOnExit(); - } - - List activities = getPackageManager().queryIntentActivities(intent, 0); - boolean isIntentSafe = activities.size() > 0; - - if (isIntentSafe) { - startActivity(intent); - return; - } - } catch (Exception e) { - e.printStackTrace(); - } - - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == REQUEST_CODE_SEND_EMAIL) { - if (zipFile != null) { - zipFile.delete(); - zipFile = null; - } - } - super.onActivityResult(requestCode, resultCode, data); - } - - public void showMenu(View v) { - PopupMenu popup = new PopupMenu(this, v); - MenuInflater inflater = popup.getMenuInflater(); - inflater.inflate(R.menu.menu_main, popup.getMenu()); - if( BuildConfig.DEBUG ) { - for(int i=0; i0 ) + { + showCleanButton(); + }else { + hideCleanButton(); + } + + final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.imNFC); + rippleBackground.startRippleAnimation(); + + + Intent intent = getIntent(); + if (intent != null && (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))) { + Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); + if (tag != null && onNFCReaderCallback != null) { + onNFCReaderCallback.onTagDiscovered(tag); + } + } + } + + public static void commonInit(Context context) + { + if( PINStorage.needInit() ) { + PINStorage.Init(context); + } + if( LastSignStorage.needInit() ) { + LastSignStorage.Init(context); + } + } + + @Override + protected void onDestroy() { +// Logger.StopSaveToFile(getApplicationContext()); + super.onDestroy(); + } + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + if (intent != null && (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))) { + Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); + if (tag != null && onNFCReaderCallback != null) { + onNFCReaderCallback.onTagDiscovered(tag); + } + } + + } + + @Override + public boolean onKeyDown(int keycode, KeyEvent e) { + switch (keycode) { + case KeyEvent.KEYCODE_MENU: + fab.requestFocus(); + showMenu(fab); + return true; + } + + return super.onKeyDown(keycode, e); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_main, menu); + if( BuildConfig.DEBUG ) { + for(int i=0; i 0) { + String[] fileNames = new String[filelocations.length]; + for (int i = 0; i < filelocations.length; i++) + fileNames[i] = filelocations[i].getAbsolutePath(); + zipFile = File.createTempFile("tangemLogs", ".zip", filelocations[0].getParentFile()); + Compress compress = new Compress(fileNames, zipFile.getAbsolutePath()); + compress.zip(); + Log.e(logTag, String.format("Send %d bytes zip with logs", zipFile.length())); + Uri attachment = Uri.parse("content://" + LogFileProvider.AUTHORITY + "/" + + zipFile.getName()); + + intent.putExtra(Intent.EXTRA_STREAM, attachment); + zipFile.deleteOnExit(); + } + + List activities = getPackageManager().queryIntentActivities(intent, 0); + boolean isIntentSafe = activities.size() > 0; + + if (isIntentSafe) { + startActivity(intent); + return; + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_CODE_SEND_EMAIL) { + if (zipFile != null) { + zipFile.delete(); + zipFile = null; + } + } + super.onActivityResult(requestCode, resultCode, data); + } + + public void showMenu(View v) { + PopupMenu popup = new PopupMenu(this, v); + MenuInflater inflater = popup.getMenuInflater(); + inflater.inflate(R.menu.menu_main, popup.getMenu()); + if( BuildConfig.DEBUG ) { + for(int i=0; i 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("PurgeTask", "[-- 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); - } - }); - } - - public void OnReadFinish(final CardProtocol cardProtocol) { - - purgeTask = 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)); - 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.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("UID", cardProtocol.getCard().getUID()); - intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); - intent.putExtra("message", "Cannot erase wallet. Make sure you enter correct PIN2!"); - 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() { - - purgeTask = 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); - } - -} - +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.View; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; + +import com.tangem.cardReader.CardProtocol; +import com.tangem.cardReader.NfcManager; +import com.tangem.cardReader.Util; +import com.tangem.wallet.NoExtendedLengthSupportDialog; +import com.tangem.wallet.PINStorage; +import com.tangem.wallet.R; +import com.tangem.wallet.Tangem_Card; +import com.tangem.wallet.WaitSecurityDelayDialog; + +public class PurgeActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { + + public static final int RESULT_INVALID_PIN = Activity.RESULT_FIRST_USER; + + private Tangem_Card mCard; + private TextView tvCardID; + private NfcManager mNfcManager; + private static final String logTag = "Purge"; + private ProgressBar progressBar; + private PurgeTask purgeTask; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_purge); + + MainActivity.commonInit(getApplicationContext()); + + mCard = new Tangem_Card(getIntent().getStringExtra("UID")); + mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); + + tvCardID = (TextView) findViewById(R.id.tvCardID); + tvCardID.setText(mCard.getCIDDescription()); + + mNfcManager = new NfcManager(this, this); + + progressBar = (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())) { + isoDep.setTimeout(mCard.getPauseBeforePIN2() + 65000); + purgeTask = new PurgeTask(isoDep, this); + + purgeTask.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 (purgeTask != null) { + purgeTask.cancel(true); + } + super.onPause(); + } + + @Override + public void onStop() { + // dismiss enable NFC dialog + mNfcManager.onStop(); + if (purgeTask != null) { + purgeTask.cancel(true); + } + super.onStop(); + } + +// @Override +// public Dialog CreateNFCDialog(int id, AlertDialogWrapper.Builder builder, LayoutInflater li) { +// return mNfcManager.onCreateDialog(id, builder, li); +// } + + private class PurgeTask extends Thread { + + + private String txOutAddress; + + IsoDep mIsoDep; + CardProtocol.Notifications mNotifications; + private boolean isCancelled = false; + + public 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("PurgeTask", "[-- 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("PurgeTask", "[-- 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); + } + }); + } + + public void OnReadFinish(final CardProtocol cardProtocol) { + + purgeTask = 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)); + 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.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("UID", cardProtocol.getCard().getUID()); + intent.putExtra("Card", cardProtocol.getCard().getAsBundle()); + intent.putExtra("message", "Cannot erase wallet. Make sure you enter correct PIN2!"); + 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() { + + purgeTask = 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); + } + +} + diff --git a/app/src/main/java/com/tangem/wallet/QRScanActivity.java b/app/src/main/java/com/tangem/presentation/activity/QRScanActivity.java similarity index 96% rename from app/src/main/java/com/tangem/wallet/QRScanActivity.java rename to app/src/main/java/com/tangem/presentation/activity/QRScanActivity.java index 52e658869d..249316b107 100644 --- a/app/src/main/java/com/tangem/wallet/QRScanActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/QRScanActivity.java @@ -1,90 +1,90 @@ -package com.tangem.wallet; - -import android.Manifest; -import android.app.Activity; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.os.Bundle; -import android.support.v4.app.ActivityCompat; -import android.support.v7.app.AppCompatActivity; -import android.util.Log; - -import com.google.zxing.Result; - -import me.dm7.barcodescanner.zxing.ZXingScannerView; - -public class QRScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{ - - private ZXingScannerView mScannerView; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - //setContentView(R.layout.activity_qrscan); - if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { - Log.e("QRScanActivity","User hasn't granted permission to use camera"); - ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA}, 1); - }else { - runScanner(); - } - } - - void runScanner() - { - mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view - setContentView(mScannerView); - mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results. - mScannerView.startCamera(); - } - - @Override - public void onRequestPermissionsResult(int requestCode, - String permissions[], int[] grantResults) { - switch (requestCode) { - case 1: { - // If request is cancelled, the result arrays are empty. - if (grantResults.length > 0 - && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - - Log.i("QRScanActivity","permission was granted"); - // permission was granted, yay! Do the - // contacts-related task you need to do. - runScanner(); - - } else { - Log.e("QRScanActivity","permission denied"); - setResult(Activity.RESULT_CANCELED); - finish(); - - // permission denied, boo! Disable the - // functionality that depends on this permission. - } - return; - } - - // other 'case' lines to check for other - // permissions this app might request - } - } - - - @Override - public void handleResult(Result result) { - Intent data=new Intent(); - data.putExtra("QRCode", result.getText()); - setResult(Activity.RESULT_OK, data); - finish(); - } - - @Override - protected void onPause() { - super.onPause(); - if( mScannerView!=null ) mScannerView.stopCamera(); // Stop camera on pause - } - - @Override - protected void onResume() { - super.onResume(); - if( mScannerView!=null ) mScannerView.startCamera(); - } -} +package com.tangem.presentation.activity; + +import android.Manifest; +import android.app.Activity; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.support.v4.app.ActivityCompat; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; + +import com.google.zxing.Result; + +import me.dm7.barcodescanner.zxing.ZXingScannerView; + +public class QRScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{ + + private ZXingScannerView mScannerView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + //setContentView(R.layout.activity_qrscan); + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { + Log.e("QRScanActivity","User hasn't granted permission to use camera"); + ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA}, 1); + }else { + runScanner(); + } + } + + void runScanner() + { + mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view + setContentView(mScannerView); + mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results. + mScannerView.startCamera(); + } + + @Override + public void onRequestPermissionsResult(int requestCode, + String permissions[], int[] grantResults) { + switch (requestCode) { + case 1: { + // If request is cancelled, the result arrays are empty. + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + + Log.i("QRScanActivity","permission was granted"); + // permission was granted, yay! Do the + // contacts-related task you need to do. + runScanner(); + + } else { + Log.e("QRScanActivity","permission denied"); + setResult(Activity.RESULT_CANCELED); + finish(); + + // permission denied, boo! Disable the + // functionality that depends on this permission. + } + return; + } + + // other 'case' lines to check for other + // permissions this app might request + } + } + + + @Override + public void handleResult(Result result) { + Intent data=new Intent(); + data.putExtra("QRCode", result.getText()); + setResult(Activity.RESULT_OK, data); + finish(); + } + + @Override + protected void onPause() { + super.onPause(); + if( mScannerView!=null ) mScannerView.stopCamera(); // Stop camera on pause + } + + @Override + protected void onResume() { + super.onResume(); + if( mScannerView!=null ) mScannerView.startCamera(); + } +} diff --git a/app/src/main/java/com/tangem/wallet/RequestPINActivity.java b/app/src/main/java/com/tangem/presentation/activity/RequestPINActivity.java similarity index 98% rename from app/src/main/java/com/tangem/wallet/RequestPINActivity.java rename to app/src/main/java/com/tangem/presentation/activity/RequestPINActivity.java index b35a4ec71e..9570babb18 100644 --- a/app/src/main/java/com/tangem/wallet/RequestPINActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/RequestPINActivity.java @@ -1,4 +1,4 @@ -package com.tangem.wallet; +package com.tangem.presentation.activity; import android.Manifest; import android.annotation.SuppressLint; @@ -27,6 +27,10 @@ import android.widget.ImageView; import android.widget.TextView; import com.tangem.cardReader.NfcManager; +import com.tangem.wallet.FingerprintHelper; +import com.tangem.wallet.PINStorage; +import com.tangem.wallet.R; +import com.tangem.wallet.Tangem_Card; import java.io.IOException; import java.security.KeyStore; diff --git a/app/src/main/java/com/tangem/wallet/SavePINActivity.java b/app/src/main/java/com/tangem/presentation/activity/SavePINActivity.java similarity index 99% rename from app/src/main/java/com/tangem/wallet/SavePINActivity.java rename to app/src/main/java/com/tangem/presentation/activity/SavePINActivity.java index 02f519364b..b2172ed82d 100644 --- a/app/src/main/java/com/tangem/wallet/SavePINActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/SavePINActivity.java @@ -1,4 +1,4 @@ -package com.tangem.wallet; +package com.tangem.presentation.activity; import android.Manifest; import android.annotation.SuppressLint; @@ -23,6 +23,10 @@ import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; +import com.tangem.wallet.FingerprintHelper; +import com.tangem.wallet.PINStorage; +import com.tangem.wallet.R; + import java.io.IOException; import java.security.KeyStore; import java.security.KeyStoreException; diff --git a/app/src/main/java/com/tangem/wallet/SelectBlockchainActivity.java b/app/src/main/java/com/tangem/presentation/activity/SelectBlockchainActivity.java similarity index 94% rename from app/src/main/java/com/tangem/wallet/SelectBlockchainActivity.java rename to app/src/main/java/com/tangem/presentation/activity/SelectBlockchainActivity.java index baf77bbb62..5900e94d77 100644 --- a/app/src/main/java/com/tangem/wallet/SelectBlockchainActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/SelectBlockchainActivity.java @@ -1,31 +1,33 @@ -package com.tangem.wallet; - -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; - -public class SelectBlockchainActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_select_blockchain); - -// Spinner spBlockchain = (Spinner) findViewById(R.id.spBlockchain); -// ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, Blockchain.values()); -// spBlockchain.setAdapter(adapter); -// spBlockchain.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { -// @Override -// public void onItemSelected(AdapterView parent, View view, int position, long id) { -// Intent intent = new Intent(); -// intent.putExtra("blockchain", Blockchain.values()[position].toString()); -// setResult(RESULT_OK, intent); -// finish(); -// } -// -// @Override -// public void onNothingSelected(AdapterView parent) { -// -// } -// }); - } -} +package com.tangem.presentation.activity; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; + +import com.tangem.wallet.R; + +public class SelectBlockchainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_select_blockchain); + +// Spinner spBlockchain = (Spinner) findViewById(R.id.spBlockchain); +// ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, Blockchain.values()); +// spBlockchain.setAdapter(adapter); +// spBlockchain.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { +// @Override +// public void onItemSelected(AdapterView parent, View view, int position, long id) { +// Intent intent = new Intent(); +// intent.putExtra("blockchain", Blockchain.values()[position].toString()); +// setResult(RESULT_OK, intent); +// finish(); +// } +// +// @Override +// public void onNothingSelected(AdapterView parent) { +// +// } +// }); + } +} diff --git a/app/src/main/java/com/tangem/wallet/SendTransactionActivity.java b/app/src/main/java/com/tangem/presentation/activity/SendTransactionActivity.java similarity index 94% rename from app/src/main/java/com/tangem/wallet/SendTransactionActivity.java rename to app/src/main/java/com/tangem/presentation/activity/SendTransactionActivity.java index 33c8d46d65..bec0ff1104 100644 --- a/app/src/main/java/com/tangem/wallet/SendTransactionActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/SendTransactionActivity.java @@ -1,208 +1,219 @@ -package com.tangem.wallet; - -import android.content.Intent; -import android.os.AsyncTask; -import android.support.v7.app.AppCompatActivity; -import android.os.Bundle; -import android.util.Log; -import android.view.KeyEvent; -import android.widget.ProgressBar; -import android.widget.Toast; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.math.BigInteger; -import java.util.List; - -public class SendTransactionActivity extends AppCompatActivity { - - ProgressBar progressBar; - private Tangem_Card mCard; - private String tx; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_send_transaction); - - MainActivity.commonInit(getApplicationContext()); - - progressBar = findViewById(R.id.progressBar); - - Intent intent = getIntent(); - mCard = new Tangem_Card(getIntent().getStringExtra("UID")); - mCard.LoadFromBundle(intent.getExtras().getBundle("Card")); - tx = intent.getStringExtra("TX"); - - CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); - if (mCard.getBlockchain() == Blockchain.Ethereum || mCard.getBlockchain() == Blockchain.EthereumTestNet || mCard.getBlockchain() == Blockchain.Token) { - ETHRequestTask task = new ETHRequestTask(mCard.getBlockchain()); - Infura_Request req = Infura_Request.SendTransaction(mCard.getWallet(), tx); - req.setID(67); - req.setBlockchain(mCard.getBlockchain()); - task.execute(req); - } else if (mCard.getBlockchain() == Blockchain.Bitcoin || mCard.getBlockchain() == Blockchain.BitcoinTestNet ) { - String nodeAddress = engine.GetNode(mCard); - int nodePort = engine.GetNodePort(mCard); - ConnectTask connectTask = new ConnectTask(nodeAddress, nodePort); - connectTask.execute(Electrum_Request.Broadcast(mCard.getWallet(), tx)); - } - else if (mCard.getBlockchain() == Blockchain.BitcoinCash || mCard.getBlockchain() == Blockchain.BitcoinCashTestNet ) { - String nodeAddress = engine.GetNode(mCard); - int nodePort = engine.GetNodePort(mCard); - ConnectTask connectTask = new ConnectTask(nodeAddress, nodePort); - connectTask.execute(Electrum_Request.Broadcast(mCard.getWallet(), tx)); - } - - } - - @Override - public boolean onKeyDown(int keycode, KeyEvent e) { - switch (keycode) { - case KeyEvent.KEYCODE_BACK: - Toast.makeText(getBaseContext(),"Please wait while the payment is sent...",Toast.LENGTH_LONG).show(); - return true; - } - - return super.onKeyDown(keycode, e); - } - - void FinishWithError(String Message) { - Intent intent = new Intent(); - intent.putExtra("message", "Failed to send transaction. Try again."); - setResult(MainActivity.RESULT_CANCELED, intent); - finish(); - } - - void FinishWithSuccess() { - Intent intent = new Intent(); - intent.putExtra("message", "Transaction has been successfully signed and sent to blockchain node. Wallet balance will be updated in a while"); - setResult(MainActivity.RESULT_OK, intent); - finish(); - } - - private class ETHRequestTask extends Infura_Task { - ETHRequestTask(Blockchain blockchain){ - super(blockchain); - } - @Override - protected void onPostExecute(List requests) { - super.onPostExecute(requests); - for (Infura_Request request : requests) { - try { - if (request.error == null) { - if (request.isMethod(Infura_Request.METHOD_ETH_SendRawTransaction)) { - try { - String hashTX = ""; - try { - String tmp = request.getResultString(); - hashTX = tmp; - }catch(JSONException e) - { - JSONObject msg = request.getAnswer(); - JSONObject err = msg.getJSONObject("error"); - hashTX = err.getString("message"); - LastSignStorage.setLastMessage(mCard.getWallet(), hashTX); - FinishWithError(hashTX); - return; - } - - try { - if (hashTX.startsWith("0x") || hashTX.startsWith("0X")) { - hashTX = hashTX.substring(2); - } - BigInteger bigInt = new BigInteger(hashTX, 16); //TODO: очень плохой способ - LastSignStorage.setTxWasSend(mCard.getWallet()); - LastSignStorage.setLastMessage(mCard.getWallet(), ""); - BigInteger nonce = mCard.GetConfirmTXCount(); - nonce.add(BigInteger.valueOf(1)); - mCard.SetConfirmTXCount(nonce); - Log.e("TX_RESULT", hashTX); - FinishWithSuccess(); - }catch(Exception e) - { - FinishWithError(hashTX); - } - - } catch (JSONException e) { - e.printStackTrace(); - FinishWithError(e.toString()); - } - } - } else if (request.error != null) { - FinishWithError(request.error); - } - } catch (JSONException e) { - e.printStackTrace(); - FinishWithError(e.toString()); - } - } - } - } - - private class ConnectTask extends Electrum_Task { - public ConnectTask(String host, int port) { - super(host, port); - } - - public ConnectTask(String host, int port, SharedData sharedData) { - super(host, port, sharedData); - } - - @Override - protected void onProgressUpdate(Integer... values) { - super.onProgressUpdate(values); - } - - @Override - protected void onPostExecute(List requests) { - super.onPostExecute(requests); - CoinEngine engine = CoinEngineFactory.Create(Blockchain.Bitcoin); - - for (Electrum_Request request : requests) { - try { - if (request.error == null) { - if (request.isMethod(Electrum_Request.METHOD_SendTransaction)) { - try { - String hashTX = request.getResultString(); - - try - { - LastSignStorage.setLastMessage(mCard.getWallet(), hashTX); - if (hashTX.startsWith("0x") || hashTX.startsWith("0X")) { - hashTX = hashTX.substring(2); - } - BigInteger bigInt = new BigInteger(hashTX, 16); //TODO: очень плохой способ - LastSignStorage.setTxWasSend(mCard.getWallet()); - LastSignStorage.setLastMessage(mCard.getWallet(), ""); - Log.e("TX_RESULT", hashTX); - FinishWithSuccess(); - }catch(Exception e) - { - engine.SwitchNode(null); - FinishWithError(hashTX); - return; - } - - } catch (JSONException e) { - e.printStackTrace(); - engine.SwitchNode(null); - FinishWithError(e.toString()); - } - } - } else if (request.error != null) { - engine.SwitchNode(null); - FinishWithError(request.error); - } - } catch (JSONException e) { - e.printStackTrace(); - engine.SwitchNode(null); - FinishWithError(e.toString()); - } - } - - } - } - -} +package com.tangem.presentation.activity; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.KeyEvent; +import android.widget.ProgressBar; +import android.widget.Toast; + +import com.tangem.wallet.Blockchain; +import com.tangem.wallet.CoinEngine; +import com.tangem.wallet.CoinEngineFactory; +import com.tangem.wallet.Electrum_Request; +import com.tangem.wallet.Electrum_Task; +import com.tangem.wallet.Infura_Request; +import com.tangem.wallet.Infura_Task; +import com.tangem.wallet.LastSignStorage; +import com.tangem.wallet.R; +import com.tangem.wallet.SharedData; +import com.tangem.wallet.Tangem_Card; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.math.BigInteger; +import java.util.List; + +public class SendTransactionActivity extends AppCompatActivity { + + ProgressBar progressBar; + private Tangem_Card mCard; + private String tx; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_send_transaction); + + MainActivity.commonInit(getApplicationContext()); + + progressBar = findViewById(R.id.progressBar); + + Intent intent = getIntent(); + mCard = new Tangem_Card(getIntent().getStringExtra("UID")); + mCard.LoadFromBundle(intent.getExtras().getBundle("Card")); + tx = intent.getStringExtra("TX"); + + CoinEngine engine = CoinEngineFactory.Create(mCard.getBlockchain()); + if (mCard.getBlockchain() == Blockchain.Ethereum || mCard.getBlockchain() == Blockchain.EthereumTestNet || mCard.getBlockchain() == Blockchain.Token) { + ETHRequestTask task = new ETHRequestTask(mCard.getBlockchain()); + Infura_Request req = Infura_Request.SendTransaction(mCard.getWallet(), tx); + req.setID(67); + req.setBlockchain(mCard.getBlockchain()); + task.execute(req); + } else if (mCard.getBlockchain() == Blockchain.Bitcoin || mCard.getBlockchain() == Blockchain.BitcoinTestNet ) { + String nodeAddress = engine.GetNode(mCard); + int nodePort = engine.GetNodePort(mCard); + ConnectTask connectTask = new ConnectTask(nodeAddress, nodePort); + connectTask.execute(Electrum_Request.Broadcast(mCard.getWallet(), tx)); + } + else if (mCard.getBlockchain() == Blockchain.BitcoinCash || mCard.getBlockchain() == Blockchain.BitcoinCashTestNet ) { + String nodeAddress = engine.GetNode(mCard); + int nodePort = engine.GetNodePort(mCard); + ConnectTask connectTask = new ConnectTask(nodeAddress, nodePort); + connectTask.execute(Electrum_Request.Broadcast(mCard.getWallet(), tx)); + } + + } + + @Override + public boolean onKeyDown(int keycode, KeyEvent e) { + switch (keycode) { + case KeyEvent.KEYCODE_BACK: + Toast.makeText(getBaseContext(),"Please wait while the payment is sent...",Toast.LENGTH_LONG).show(); + return true; + } + + return super.onKeyDown(keycode, e); + } + + void FinishWithError(String Message) { + Intent intent = new Intent(); + intent.putExtra("message", "Failed to send transaction. Try again."); + setResult(MainActivity.RESULT_CANCELED, intent); + finish(); + } + + void FinishWithSuccess() { + Intent intent = new Intent(); + intent.putExtra("message", "Transaction has been successfully signed and sent to blockchain node. Wallet balance will be updated in a while"); + setResult(MainActivity.RESULT_OK, intent); + finish(); + } + + private class ETHRequestTask extends Infura_Task { + ETHRequestTask(Blockchain blockchain){ + super(blockchain); + } + @Override + protected void onPostExecute(List requests) { + super.onPostExecute(requests); + for (Infura_Request request : requests) { + try { + if (request.error == null) { + if (request.isMethod(Infura_Request.METHOD_ETH_SendRawTransaction)) { + try { + String hashTX = ""; + try { + String tmp = request.getResultString(); + hashTX = tmp; + }catch(JSONException e) + { + JSONObject msg = request.getAnswer(); + JSONObject err = msg.getJSONObject("error"); + hashTX = err.getString("message"); + LastSignStorage.setLastMessage(mCard.getWallet(), hashTX); + FinishWithError(hashTX); + return; + } + + try { + if (hashTX.startsWith("0x") || hashTX.startsWith("0X")) { + hashTX = hashTX.substring(2); + } + BigInteger bigInt = new BigInteger(hashTX, 16); //TODO: очень плохой способ + LastSignStorage.setTxWasSend(mCard.getWallet()); + LastSignStorage.setLastMessage(mCard.getWallet(), ""); + BigInteger nonce = mCard.GetConfirmTXCount(); + nonce.add(BigInteger.valueOf(1)); + mCard.SetConfirmTXCount(nonce); + Log.e("TX_RESULT", hashTX); + FinishWithSuccess(); + }catch(Exception e) + { + FinishWithError(hashTX); + } + + } catch (JSONException e) { + e.printStackTrace(); + FinishWithError(e.toString()); + } + } + } else if (request.error != null) { + FinishWithError(request.error); + } + } catch (JSONException e) { + e.printStackTrace(); + FinishWithError(e.toString()); + } + } + } + } + + private class ConnectTask extends Electrum_Task { + public ConnectTask(String host, int port) { + super(host, port); + } + + public ConnectTask(String host, int port, SharedData sharedData) { + super(host, port, sharedData); + } + + @Override + protected void onProgressUpdate(Integer... values) { + super.onProgressUpdate(values); + } + + @Override + protected void onPostExecute(List requests) { + super.onPostExecute(requests); + CoinEngine engine = CoinEngineFactory.Create(Blockchain.Bitcoin); + + for (Electrum_Request request : requests) { + try { + if (request.error == null) { + if (request.isMethod(Electrum_Request.METHOD_SendTransaction)) { + try { + String hashTX = request.getResultString(); + + try + { + LastSignStorage.setLastMessage(mCard.getWallet(), hashTX); + if (hashTX.startsWith("0x") || hashTX.startsWith("0X")) { + hashTX = hashTX.substring(2); + } + BigInteger bigInt = new BigInteger(hashTX, 16); //TODO: очень плохой способ + LastSignStorage.setTxWasSend(mCard.getWallet()); + LastSignStorage.setLastMessage(mCard.getWallet(), ""); + Log.e("TX_RESULT", hashTX); + FinishWithSuccess(); + }catch(Exception e) + { + engine.SwitchNode(null); + FinishWithError(hashTX); + return; + } + + } catch (JSONException e) { + e.printStackTrace(); + engine.SwitchNode(null); + FinishWithError(e.toString()); + } + } + } else if (request.error != null) { + engine.SwitchNode(null); + FinishWithError(request.error); + } + } catch (JSONException e) { + e.printStackTrace(); + engine.SwitchNode(null); + FinishWithError(e.toString()); + } + } + + } + } + +} diff --git a/app/src/main/java/com/tangem/presentation/activity/SignPaymentActivity.java b/app/src/main/java/com/tangem/presentation/activity/SignPaymentActivity.java new file mode 100644 index 0000000000..d01ac47897 --- /dev/null +++ b/app/src/main/java/com/tangem/presentation/activity/SignPaymentActivity.java @@ -0,0 +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.cardReader.CardProtocol; import com.tangem.cardReader.NfcManager; import com.tangem.cardReader.Util; import com.tangem.wallet.BTCUtils; import com.tangem.wallet.Blockchain; import com.tangem.wallet.CoinEngine; import com.tangem.wallet.CoinEngineFactory; import com.tangem.wallet.LastSignStorage; import com.tangem.wallet.NoExtendedLengthSupportDialog; import com.tangem.wallet.R; import com.tangem.wallet.Tangem_Card; import com.tangem.wallet.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 Tangem_Card 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 Tangem_Card(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 diff --git a/app/src/main/java/com/tangem/wallet/SwapPINActivity.java b/app/src/main/java/com/tangem/presentation/activity/SwapPINActivity.java similarity index 96% rename from app/src/main/java/com/tangem/wallet/SwapPINActivity.java rename to app/src/main/java/com/tangem/presentation/activity/SwapPINActivity.java index 67910d1505..32a16c9739 100644 --- a/app/src/main/java/com/tangem/wallet/SwapPINActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/SwapPINActivity.java @@ -1,326 +1,331 @@ -package com.tangem.wallet; - -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.View; -import android.widget.ProgressBar; -import android.widget.TextView; -import android.widget.Toast; - -import com.tangem.cardReader.CardProtocol; -import com.tangem.cardReader.NfcManager; -import com.tangem.cardReader.Util; - -public class SwapPINActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { - - public static final int RESULT_INVALID_PIN = Activity.RESULT_FIRST_USER; - private Tangem_Card mCard; - private NfcManager mNfcManager; - private static final String logTag = "SwapPIN"; - private ProgressBar progressBar; - private SwapPINTask swapPinTask; - - private String newPIN, newPIN2; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_swap_pin); - - MainActivity.commonInit(getApplicationContext()); - - mCard = new Tangem_Card(getIntent().getStringExtra("UID")); - mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); - - newPIN = getIntent().getStringExtra("newPIN"); - newPIN2 = getIntent().getStringExtra("newPIN2"); - - TextView 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())) { - isoDep.setTimeout(mCard.getPauseBeforePIN2() + 65000); - swapPinTask = new SwapPINTask(isoDep, this); - swapPinTask.start(); - } else { - Log.d(logTag, "Mismatch card UID (" + sUID + " instead of " + mCard.getUID() + ")"); - mNfcManager.IgnoreTag(isoDep.getTag()); - } - - } catch (Exception e) { - e.printStackTrace(); - } - - } - - @Override - public void onResume() { - super.onResume(); - mNfcManager.onResume(); - } - - @Override - public void onPause() { - mNfcManager.onPause(); - if (swapPinTask != null) { - swapPinTask.cancel(true); - } - super.onPause(); - } - - @Override - public void onStop() { - // dismiss enable NFC dialog - mNfcManager.onStop(); - if (swapPinTask != null) { - swapPinTask.cancel(true); - } - 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); -// } - - 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); - } - }); - } - - public void OnReadFinish(final CardProtocol cardProtocol) { - - swapPinTask = 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)); - 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.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(); - } - } - }, 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() { - - swapPinTask = 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); - } - -} - +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.View; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; + +import com.tangem.cardReader.CardProtocol; +import com.tangem.cardReader.NfcManager; +import com.tangem.cardReader.Util; +import com.tangem.wallet.NoExtendedLengthSupportDialog; +import com.tangem.wallet.PINStorage; +import com.tangem.wallet.R; +import com.tangem.wallet.Tangem_Card; +import com.tangem.wallet.WaitSecurityDelayDialog; + +public class SwapPINActivity extends AppCompatActivity implements NfcAdapter.ReaderCallback, CardProtocol.Notifications { + + public static final int RESULT_INVALID_PIN = Activity.RESULT_FIRST_USER; + private Tangem_Card mCard; + private NfcManager mNfcManager; + private static final String logTag = "SwapPIN"; + private ProgressBar progressBar; + private SwapPINTask swapPinTask; + + private String newPIN, newPIN2; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_swap_pin); + + MainActivity.commonInit(getApplicationContext()); + + mCard = new Tangem_Card(getIntent().getStringExtra("UID")); + mCard.LoadFromBundle(getIntent().getExtras().getBundle("Card")); + + newPIN = getIntent().getStringExtra("newPIN"); + newPIN2 = getIntent().getStringExtra("newPIN2"); + + TextView 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())) { + isoDep.setTimeout(mCard.getPauseBeforePIN2() + 65000); + swapPinTask = new SwapPINTask(isoDep, this); + swapPinTask.start(); + } else { + Log.d(logTag, "Mismatch card UID (" + sUID + " instead of " + mCard.getUID() + ")"); + mNfcManager.IgnoreTag(isoDep.getTag()); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + } + + @Override + public void onResume() { + super.onResume(); + mNfcManager.onResume(); + } + + @Override + public void onPause() { + mNfcManager.onPause(); + if (swapPinTask != null) { + swapPinTask.cancel(true); + } + super.onPause(); + } + + @Override + public void onStop() { + // dismiss enable NFC dialog + mNfcManager.onStop(); + if (swapPinTask != null) { + swapPinTask.cancel(true); + } + 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); +// } + + 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); + } + }); + } + + public void OnReadFinish(final CardProtocol cardProtocol) { + + swapPinTask = 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)); + 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.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(); + } + } + }, 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() { + + swapPinTask = 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); + } + +} + diff --git a/app/src/main/java/com/tangem/wallet/VerifyCardActivity.java b/app/src/main/java/com/tangem/presentation/activity/VerifyCardActivity.java similarity index 63% rename from app/src/main/java/com/tangem/wallet/VerifyCardActivity.java rename to app/src/main/java/com/tangem/presentation/activity/VerifyCardActivity.java index 29113fdb47..7af9846019 100644 --- a/app/src/main/java/com/tangem/wallet/VerifyCardActivity.java +++ b/app/src/main/java/com/tangem/presentation/activity/VerifyCardActivity.java @@ -1,28 +1,30 @@ -package com.tangem.wallet; - -import android.app.Activity; -import android.content.Intent; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; - - -public class VerifyCardActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_verify_card); - - MainActivity.commonInit(getApplicationContext()); - } - - @Override - public void onBackPressed() { - //super.onBackPressed(); - VerifyCardActivityFragment verifyCardActivityFragment= (VerifyCardActivityFragment) getSupportFragmentManager().findFragmentById(R.id.verify_card_fragment); - Intent data= verifyCardActivityFragment.prepareResultIntent(); - data.putExtra("modification", "update"); - setResult(Activity.RESULT_OK, data); - finish(); - } -} +package com.tangem.presentation.activity; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; + +import com.tangem.wallet.R; +import com.tangem.wallet.VerifyCardActivityFragment; + +public class VerifyCardActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_verify_card); + + MainActivity.commonInit(getApplicationContext()); + } + + @Override + public void onBackPressed() { + //super.onBackPressed(); + VerifyCardActivityFragment verifyCardActivityFragment = (VerifyCardActivityFragment) getSupportFragmentManager().findFragmentById(R.id.verify_card_fragment); + Intent data = verifyCardActivityFragment.prepareResultIntent(); + data.putExtra("modification", "update"); + setResult(Activity.RESULT_OK, data); + finish(); + } +} diff --git a/app/src/main/java/com/tangem/wallet/Electrum_Task.java b/app/src/main/java/com/tangem/wallet/Electrum_Task.java index 2475277d68..8aa9609371 100644 --- a/app/src/main/java/com/tangem/wallet/Electrum_Task.java +++ b/app/src/main/java/com/tangem/wallet/Electrum_Task.java @@ -27,7 +27,7 @@ public class Electrum_Task extends AsyncTask> { - SharedData sharedCounter = null; + public SharedData sharedCounter = null; public Fee_Task(SharedData sharedData) { diff --git a/app/src/main/java/com/tangem/wallet/FingerprintHelper.java b/app/src/main/java/com/tangem/wallet/FingerprintHelper.java index b4fb689afe..0f61ac1853 100644 --- a/app/src/main/java/com/tangem/wallet/FingerprintHelper.java +++ b/app/src/main/java/com/tangem/wallet/FingerprintHelper.java @@ -35,7 +35,7 @@ public class FingerprintHelper extends FingerprintManager.AuthenticationCallback cancellationSignal.cancel(); } - interface FingerprintHelperListener { + public interface FingerprintHelperListener { public void authenticationFailed(String error); public void authenticationSucceeded(FingerprintManager.AuthenticationResult result); } diff --git a/app/src/main/java/com/tangem/wallet/LastSignStorage.java b/app/src/main/java/com/tangem/wallet/LastSignStorage.java index b12d6df8de..66dc1add79 100644 --- a/app/src/main/java/com/tangem/wallet/LastSignStorage.java +++ b/app/src/main/java/com/tangem/wallet/LastSignStorage.java @@ -6,7 +6,6 @@ import android.preference.PreferenceManager; import android.util.ArrayMap; import android.util.ArraySet; -import java.lang.reflect.Array; import java.util.Date; import java.util.Map; import java.util.Set; @@ -24,7 +23,7 @@ public class LastSignStorage { private static Map txCol = new ArrayMap<>(); private static Map txCompleteCol = new ArrayMap<>(); - static void Init(Context context) { + public static void Init(Context context) { sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); cards=sharedPreferences.getStringSet("LastSign_Cards", cards); for (int i = 0; i < cards.size(); i++) { diff --git a/app/src/main/java/com/tangem/wallet/LoadedWalletActivityFragment.java b/app/src/main/java/com/tangem/wallet/LoadedWalletActivityFragment.java index 67103317c6..5d1c32bcb8 100644 --- a/app/src/main/java/com/tangem/wallet/LoadedWalletActivityFragment.java +++ b/app/src/main/java/com/tangem/wallet/LoadedWalletActivityFragment.java @@ -42,6 +42,12 @@ import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.tangem.cardReader.CardProtocol; import com.tangem.cardReader.NfcManager; import com.tangem.cardReader.Util; +import com.tangem.presentation.activity.CreateNewWalletActivity; +import com.tangem.presentation.activity.PreparePaymentActivity; +import com.tangem.presentation.activity.PurgeActivity; +import com.tangem.presentation.activity.RequestPINActivity; +import com.tangem.presentation.activity.SwapPINActivity; +import com.tangem.presentation.activity.VerifyCardActivity; import org.json.JSONArray; import org.json.JSONException; diff --git a/app/src/main/java/com/tangem/wallet/MainActivityFragment.java b/app/src/main/java/com/tangem/wallet/MainActivityFragment.java index 95e61e3ce1..4bf58483a0 100644 --- a/app/src/main/java/com/tangem/wallet/MainActivityFragment.java +++ b/app/src/main/java/com/tangem/wallet/MainActivityFragment.java @@ -27,6 +27,10 @@ import android.widget.Toast; import com.tangem.cardReader.CardProtocol; import com.tangem.cardReader.NfcManager; import com.tangem.cardReader.Util; +import com.tangem.presentation.activity.EmptyWalletActivity; +import com.tangem.presentation.activity.LoadedWalletActivity; +import com.tangem.presentation.activity.MainActivity; +import com.tangem.presentation.activity.RequestPINActivity; import org.json.JSONArray; import org.json.JSONException; diff --git a/app/src/main/java/com/tangem/wallet/PINStorage.java b/app/src/main/java/com/tangem/wallet/PINStorage.java index f16194d5fb..bd4c9836e4 100644 --- a/app/src/main/java/com/tangem/wallet/PINStorage.java +++ b/app/src/main/java/com/tangem/wallet/PINStorage.java @@ -4,7 +4,6 @@ import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Base64; -import android.util.Log; import com.tangem.cardReader.CardProtocol; @@ -18,11 +17,11 @@ import javax.crypto.Cipher; * Global PIN Storage */ -class PINStorage { +public class PINStorage { private static String mSavedPIN, mUserPIN, mLastUsedPIN, mEncryptedPIN, mPIN2; private static SharedPreferences sharedPreferences=null; - static void Init(Context context) { + public static void Init(Context context) { sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); mSavedPIN = sharedPreferences.getString("SavedPIN", null); mUserPIN = null; @@ -46,15 +45,15 @@ class PINStorage { mLastUsedPIN = PIN; } - static void setUserPIN(String PIN) { + public static void setUserPIN(String PIN) { mUserPIN = PIN; } - static void setPIN2(String PIN) { + public static void setPIN2(String PIN) { mPIN2 = PIN; } - static void savePIN(String PIN) { + public static void savePIN(String PIN) { mSavedPIN = PIN; if (mSavedPIN != null && !mSavedPIN.isEmpty()) { SharedPreferences.Editor editor = sharedPreferences.edit(); @@ -65,7 +64,7 @@ class PINStorage { } } - static void deletePIN() { + public static void deletePIN() { SharedPreferences.Editor editor = sharedPreferences.edit(); if (mSavedPIN != null && mLastUsedPIN != null && mSavedPIN.equals(mLastUsedPIN)) { mLastUsedPIN = null; @@ -75,7 +74,7 @@ class PINStorage { editor.apply(); } - static void saveEncryptedPIN(Cipher cipher, String PIN) { + public static void saveEncryptedPIN(Cipher cipher, String PIN) { try { byte[] iv = cipher.getIV(); byte[] bytes = cipher.doFinal(PIN.getBytes()); @@ -93,14 +92,14 @@ class PINStorage { } } - static byte[] loadEncryptedIV() { + public static byte[] loadEncryptedIV() { String sIV = sharedPreferences.getString("EncryptedIV", ""); // Log.d("PINStorage", String.format("loadEncryptedIV: %s",sIV)); return Base64.decode(sIV, Base64.NO_WRAP); } - static String loadEncryptedPIN(Cipher cipher) { + public static String loadEncryptedPIN(Cipher cipher) { String encryptedPIN = sharedPreferences.getString("EncryptedPIN", null); try { @@ -114,11 +113,11 @@ class PINStorage { return mEncryptedPIN; } - static boolean haveEncryptedPIN() { + public static boolean haveEncryptedPIN() { return sharedPreferences.getString("EncryptedPIN", null) != null; } - static void deleteEncryptedPIN() { + public static void deleteEncryptedPIN() { if (mEncryptedPIN != null && mLastUsedPIN != null && mEncryptedPIN.equals(mLastUsedPIN)) { mLastUsedPIN = null; } @@ -129,7 +128,7 @@ class PINStorage { editor.apply(); } - static void saveEncryptedPIN2(Cipher cipher, String PIN) { + public static void saveEncryptedPIN2(Cipher cipher, String PIN) { try { byte[] iv = cipher.getIV(); byte[] bytes = cipher.doFinal(PIN.getBytes()); @@ -147,14 +146,14 @@ class PINStorage { } } - static byte[] loadEncryptedIV2() { + public static byte[] loadEncryptedIV2() { String sIV = sharedPreferences.getString("EncryptedIV2", ""); // Log.d("PINStorage", String.format("loadEncryptedIV: %s",sIV)); return Base64.decode(sIV, Base64.NO_WRAP); } - static String loadEncryptedPIN2(Cipher cipher) { + public static String loadEncryptedPIN2(Cipher cipher) { String encryptedPIN = sharedPreferences.getString("EncryptedPIN2", null); try { @@ -168,11 +167,11 @@ class PINStorage { return mPIN2; } - static boolean haveEncryptedPIN2() { + public static boolean haveEncryptedPIN2() { return sharedPreferences.getString("EncryptedPIN2", null) != null; } - static void deleteEncryptedPIN2() { + public static void deleteEncryptedPIN2() { mPIN2 = null; SharedPreferences.Editor editor = sharedPreferences.edit(); editor.remove("EncryptedPIN2"); diff --git a/app/src/main/java/com/tangem/wallet/SharedData.java b/app/src/main/java/com/tangem/wallet/SharedData.java index 92e9fbd8ef..20bbf5942d 100644 --- a/app/src/main/java/com/tangem/wallet/SharedData.java +++ b/app/src/main/java/com/tangem/wallet/SharedData.java @@ -13,6 +13,7 @@ public class SharedData public AtomicInteger requestCounter; public int allRequest; public AtomicInteger errorRequest; + public SharedData(int requstCount) { allRequest = requstCount; diff --git a/app/src/main/java/com/tangem/wallet/SignPaymentActivity.java b/app/src/main/java/com/tangem/wallet/SignPaymentActivity.java deleted file mode 100644 index 1f5c582659..0000000000 --- a/app/src/main/java/com/tangem/wallet/SignPaymentActivity.java +++ /dev/null @@ -1 +0,0 @@ -package com.tangem.wallet; 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.cardReader.CardProtocol; import com.tangem.cardReader.NfcManager; import com.tangem.cardReader.Util; 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 Tangem_Card 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 Tangem_Card(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 diff --git a/app/src/main/java/com/tangem/wallet/VerifyCardTask.java b/app/src/main/java/com/tangem/wallet/VerifyCardTask.java index c18cce6c89..3b827b4419 100644 --- a/app/src/main/java/com/tangem/wallet/VerifyCardTask.java +++ b/app/src/main/java/com/tangem/wallet/VerifyCardTask.java @@ -21,7 +21,7 @@ public class VerifyCardTask extends Thread { private Tangem_Card mCard; private NfcManager mNfcManager; - VerifyCardTask(Context context, Tangem_Card card, NfcManager nfcManager, IsoDep isoDep, CardProtocol.Notifications notifications) { + public VerifyCardTask(Context context, Tangem_Card card, NfcManager nfcManager, IsoDep isoDep, CardProtocol.Notifications notifications) { mCard = card; mContext = context; mIsoDep = isoDep; diff --git a/app/src/main/res/layout/activity_card_info.xml b/app/src/main/res/layout/activity_card_info.xml index 436175cfff..228a73d245 100644 --- a/app/src/main/res/layout/activity_card_info.xml +++ b/app/src/main/res/layout/activity_card_info.xml @@ -6,7 +6,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" - tools:context="com.tangem.wallet.CardInfoActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.CardInfoActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.ConfirmPaymentActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.CreateNewWalletActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.EmptyWalletActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.LoadedWalletActivity"> diff --git a/app/src/main/res/layout/activity_logo.xml b/app/src/main/res/layout/activity_logo.xml index f1b2d5babc..333c6ca2ad 100644 --- a/app/src/main/res/layout/activity_logo.xml +++ b/app/src/main/res/layout/activity_logo.xml @@ -5,7 +5,7 @@ android:background="#FFFFFF" android:gravity="bottom" android:orientation="vertical" - tools:context="com.tangem.wallet.LogoActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.LogoActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.MainActivity"> diff --git a/app/src/main/res/layout/activity_prepare_payment.xml b/app/src/main/res/layout/activity_prepare_payment.xml index 068ff29789..145ac93b95 100644 --- a/app/src/main/res/layout/activity_prepare_payment.xml +++ b/app/src/main/res/layout/activity_prepare_payment.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="com.tangem.wallet.PreparePaymentActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.PreparePaymentActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.PurgeActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.QRScanActivity"> diff --git a/app/src/main/res/layout/activity_request_pin.xml b/app/src/main/res/layout/activity_request_pin.xml index e06cbfa7fa..ea51f41082 100644 --- a/app/src/main/res/layout/activity_request_pin.xml +++ b/app/src/main/res/layout/activity_request_pin.xml @@ -4,7 +4,7 @@ android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" - tools:context="com.tangem.wallet.RequestPINActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.RequestPINActivity"> diff --git a/app/src/main/res/layout/activity_save_pin.xml b/app/src/main/res/layout/activity_save_pin.xml index 4fd06ad4cb..73a66b5432 100644 --- a/app/src/main/res/layout/activity_save_pin.xml +++ b/app/src/main/res/layout/activity_save_pin.xml @@ -4,7 +4,7 @@ android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" - tools:context="com.tangem.wallet.SavePINActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.SavePINActivity"> diff --git a/app/src/main/res/layout/activity_send_transaction.xml b/app/src/main/res/layout/activity_send_transaction.xml index d166ee7f5b..f30b96bf39 100644 --- a/app/src/main/res/layout/activity_send_transaction.xml +++ b/app/src/main/res/layout/activity_send_transaction.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="com.tangem.wallet.SendTransactionActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.SendTransactionActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.SignPaymentActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.PurgeActivity"> + tools:context="com.tangem.wallet.com.tangem.presentation.activity.VerifyCardActivity">