From 0c8157a659f9eb892e6e9146c0ba654fe9790c57 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Wed, 21 Nov 2018 21:49:44 -0400 Subject: [PATCH] - Improve the speed of the qr code generation on receivefragment - The feedback is better now in the receivefragment --- app/build.gradle | 4 + .../fragments/ReceiveTransactionFragment.java | 229 +++++++++--------- .../manager/GeneralAccountManager.java | 2 +- .../main/res/layout/receive_transaction.xml | 18 ++ 4 files changed, 142 insertions(+), 111 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 111fdd0..96c8483 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -87,6 +87,7 @@ dependencies { implementation 'com.jakewharton:butterknife:8.8.1' implementation 'com.github.bilthon:graphenej:0.4.6' implementation 'me.dm7.barcodescanner:zxing:1.9.8' + implementation 'com.github.sjaramillo10:AnimatedTabLayout:1.0.3' implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0' @@ -125,4 +126,7 @@ dependencies { annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' debugImplementation 'com.amitshekhar.android:debug-db:1.0.4' + + implementation 'com.google.zxing:core:3.2.1' + implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar' } diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java index 8a78d5a..4a0c493 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/ReceiveTransactionFragment.java @@ -27,6 +27,7 @@ import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; +import android.widget.ProgressBar; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; @@ -35,6 +36,7 @@ import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; +import com.journeyapps.barcodescanner.BarcodeEncoder; import butterknife.OnClick; import cy.agorise.crystalwallet.enums.CryptoCoin; @@ -84,6 +86,8 @@ public class ReceiveTransactionFragment extends DialogFragment implements UIVali TextView tvAssetError; @BindView(R.id.ivQrCode) ImageView ivQrCode; + @BindView(R.id.pbQrCode) + ProgressBar pbQrCode; @BindView(R.id.tvCancel) TextView tvCancel; @@ -106,6 +110,8 @@ public class ReceiveTransactionFragment extends DialogFragment implements UIVali private AsyncTask qrCodeTask; + private Double lastAmount = -1.0; + public static ReceiveTransactionFragment newInstance(long cryptoNetAccountId) { ReceiveTransactionFragment f = new ReceiveTransactionFragment(); @@ -337,144 +343,147 @@ public class ReceiveTransactionFragment extends DialogFragment implements UIVali } public void createQrCode(){ - Double amount = 0.0; + final Double amount; try{ amount = Double.valueOf(this.etAmount.getText().toString()); + } catch(NumberFormatException e){ + lastAmount = -1.0; Log.e("ReceiveFragment","Amount casting error."); + return; } - CryptoNetAccount toAccountSelected = (CryptoNetAccount) spTo.getSelectedItem(); - - if (this.cryptoNetAccount.getCryptoNet() == CryptoNet.BITSHARES) { - /* - * this is only for graphene accounts. - * - **/ - GrapheneAccount grapheneAccountSelected = new GrapheneAccount(toAccountSelected); - grapheneAccountSelected.loadInfo(db.grapheneAccountInfoDao().getByAccountId(toAccountSelected.getId())); - - - this.invoiceItems.clear(); - this.invoiceItems.add( - new LineItem("transfer", 1, amount) - ); - - LineItem items[] = new LineItem[this.invoiceItems.size()]; - items = this.invoiceItems.toArray(items); - this.invoice.setLineItems(items); - this.invoice.setTo(grapheneAccountSelected.getName()); - this.invoice.setCurrency(this.cryptoCurrency.getName()); + if (!amount.equals(lastAmount)) { + pbQrCode.setVisibility(View.VISIBLE); + lastAmount = amount; + CryptoNetAccount toAccountSelected = (CryptoNetAccount) spTo.getSelectedItem(); if (this.qrCodeTask != null) { this.qrCodeTask.cancel(true); } - this.qrCodeTask = new AsyncTask() { + if (this.cryptoNetAccount.getCryptoNet() == CryptoNet.BITSHARES) { + /* + * this is only for graphene accounts. + * + **/ + GrapheneAccount grapheneAccountSelected = new GrapheneAccount(toAccountSelected); + grapheneAccountSelected.loadInfo(db.grapheneAccountInfoDao().getByAccountId(toAccountSelected.getId())); - @Override - protected Void doInBackground(Object... voids) { - try { - final Bitmap bitmap = textToImageEncode(Invoice.toQrCode(invoice)); - if (!this.isCancelled()) { - ReceiveTransactionFragment.this.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - ivQrCode.setImageBitmap(bitmap); - } - }); - } - } catch (WriterException e) { - Log.e("ReceiveFragment", "Error creating QrCode"); - } - return null; - } - }; + this.invoiceItems.clear(); + this.invoiceItems.add( + new LineItem("transfer", 1, amount) + ); - this.qrCodeTask.execute(null, null, null); - } else { - final CryptoCoin cryptoCoin = CryptoCoin.getByCryptoNet(this.cryptoNetAccount.getCryptoNet()).get(0); + LineItem items[] = new LineItem[this.invoiceItems.size()]; + items = this.invoiceItems.toArray(items); + this.invoice.setLineItems(items); + this.invoice.setTo(grapheneAccountSelected.getName()); + this.invoice.setCurrency(this.cryptoCurrency.getName()); - //final NextBitcoinAccountAddressRequest addressRequest = new NextBitcoinAccountAddressRequest(this.cryptoNetAccount, cryptoCoin, getContext()); + //if (this.qrCodeTask != null) { + // this.qrCodeTask.cancel(true); + //} - //addressRequest.setListener(new CryptoNetInfoRequestListener() { - // @Override - // public void onCarryOut() { - // if (addressRequest.getStatus() == NextBitcoinAccountAddressRequest.StatusCode.SUCCEEDED){ - final CalculateBitcoinUriRequest uriRequest = new CalculateBitcoinUriRequest(cryptoCoin, cryptoNetAccount, getContext(), amount); + this.qrCodeTask = new AsyncTask() { - uriRequest.setListener(new CryptoNetInfoRequestListener(){ - @Override - public void onCarryOut(){ - if (uriRequest.getUri() != null){ - qrCodeTask = new AsyncTask() { + @Override + protected Void doInBackground(Object... voids) { + try { + final Bitmap bitmap = textToImageEncode(Invoice.toQrCode(invoice)); - @Override - protected Void doInBackground(Object... voids) { - try { - final Bitmap bitmap = textToImageEncode(uriRequest.getUri()); - - if (!this.isCancelled()) { - ReceiveTransactionFragment.this.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - ivQrCode.setImageBitmap(bitmap); - } - }); - } - } catch (WriterException e) { - Log.e("ReceiveFragment", "Error creating QrCode"); - } - return null; - } - }; - - qrCodeTask.execute(null, null, null); - } else { - Log.e("ReceiveFragment", "Error obtaining the uri"); - } + if (!this.isCancelled()) { + ReceiveTransactionFragment.this.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + ivQrCode.setImageBitmap(bitmap); + pbQrCode.setVisibility(View.GONE); + } + }); } - }); + } catch (WriterException e) { + Log.e("ReceiveFragment", "Error creating QrCode"); + } + return null; + } + }; + + this.qrCodeTask.execute(null, null, null); + } else { + final CryptoCoin cryptoCoin = CryptoCoin.getByCryptoNet(this.cryptoNetAccount.getCryptoNet()).get(0); + + final CalculateBitcoinUriRequest uriRequest = new CalculateBitcoinUriRequest(cryptoCoin, cryptoNetAccount, getContext(), amount); + + uriRequest.setListener(new CryptoNetInfoRequestListener() { + @Override + public void onCarryOut() { + if (uriRequest.getUri() != null) { + qrCodeTask = new AsyncTask() { + + @Override + protected Void doInBackground(Object... voids) { + try { + final Bitmap bitmap = textToImageEncode(uriRequest.getUri()); + + if (!this.isCancelled()) { + ReceiveTransactionFragment.this.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + //Double amountNow = -1.0; + //try{ + // amountNow = Double.valueOf(etAmount.getText().toString()); + //} catch(NumberFormatException e){ + //} + //if (amountNow >= 0) { + if (amount.equals(lastAmount)) { + if (!isCancelled()) { + ivQrCode.setImageBitmap(bitmap); + pbQrCode.setVisibility(View.GONE); + } + } + //} + } + }); + } + } catch (WriterException e) { + Log.e("ReceiveFragment", "Error creating QrCode"); + } + + return null; + } + }; + + qrCodeTask.execute(null, null, null); + } else { + Log.e("ReceiveFragment", "Error obtaining the uri"); + } + } + }); + + Thread thread = new Thread(new Runnable() { + @Override + public void run() { CryptoNetInfoRequests.getInstance().addRequest(uriRequest); - // } else { - // Toast.makeText(getContext(),"Error creating address",Toast.LENGTH_SHORT); - // } - // } - //}); + } + }); + thread.start(); + } } } Bitmap textToImageEncode(String Value) throws WriterException { - //TODO: do this in another thread - - BitMatrix bitMatrix; + Bitmap bitmap = null; + MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); try { - bitMatrix = new MultiFormatWriter().encode( - Value, - BarcodeFormat.DATA_MATRIX.QR_CODE, - ivQrCode.getWidth(), ivQrCode.getHeight(), null - ); - - } catch (IllegalArgumentException Illegalargumentexception) { - return null; - } - int bitMatrixWidth = bitMatrix.getWidth(); - int bitMatrixHeight = bitMatrix.getHeight(); - int[] pixels = new int[bitMatrixWidth * bitMatrixHeight]; - - for (int y = 0; y < bitMatrixHeight; y++) { - int offset = y * bitMatrixWidth; - - for (int x = 0; x < bitMatrixWidth; x++) { - pixels[offset + x] = bitMatrix.get(x, y) ? - getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor); - } + BitMatrix bitMatrix = multiFormatWriter.encode(Value, BarcodeFormat.QR_CODE, ivQrCode.getWidth(), ivQrCode.getHeight()); + BarcodeEncoder barcodeEncoder = new BarcodeEncoder(); + bitmap = barcodeEncoder.createBitmap(bitMatrix); + } catch (WriterException e) { + e.printStackTrace(); } - Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444); - bitmap.setPixels(pixels, 0, ivQrCode.getWidth(), 0, 0, bitMatrixWidth, bitMatrixHeight); return bitmap; } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java index 4065186..fa6a6b8 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java +++ b/app/src/main/java/cy/agorise/crystalwallet/manager/GeneralAccountManager.java @@ -542,7 +542,7 @@ public class GeneralAccountManager implements CryptoAccountManager, CryptoNetInf System.out.println("GeneralAccountMAnager uri calculated : " + uri.toString()); request.setUri(uri.toString()); - request.validate(); + //request.validate(); } private void parseUri(BitcoinUriParseRequest request){ diff --git a/app/src/main/res/layout/receive_transaction.xml b/app/src/main/res/layout/receive_transaction.xml index 38ee0ff..5a11900 100644 --- a/app/src/main/res/layout/receive_transaction.xml +++ b/app/src/main/res/layout/receive_transaction.xml @@ -103,6 +103,8 @@ android:textColor="@color/red" android:layout_below="@+id/spAsset"/> + + + +