From 9efe44d76a7178751469611b2dc846bf845cfd24 Mon Sep 17 00:00:00 2001 From: pokkst Date: Thu, 6 Oct 2022 00:39:40 -0500 Subject: [PATCH] copy some logic from monerujo. add new "fresh address" feature --- .../dialog/ReceiveBottomSheetDialog.java | 37 +++++++++++++++---- .../mynero/wallet/service/AddressService.java | 11 ++++++ .../layout/receive_bottom_sheet_dialog.xml | 14 ++++++- app/src/main/res/values/strings.xml | 1 + 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/net/mynero/wallet/fragment/dialog/ReceiveBottomSheetDialog.java b/app/src/main/java/net/mynero/wallet/fragment/dialog/ReceiveBottomSheetDialog.java index 82801b1..a787c20 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/dialog/ReceiveBottomSheetDialog.java +++ b/app/src/main/java/net/mynero/wallet/fragment/dialog/ReceiveBottomSheetDialog.java @@ -8,6 +8,7 @@ import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -22,7 +23,11 @@ import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import net.mynero.wallet.R; import net.mynero.wallet.data.Subaddress; +import net.mynero.wallet.model.TransactionInfo; +import net.mynero.wallet.model.Wallet; +import net.mynero.wallet.model.WalletManager; import net.mynero.wallet.service.AddressService; +import net.mynero.wallet.service.HistoryService; import net.mynero.wallet.util.DayNightMode; import net.mynero.wallet.util.Helper; import net.mynero.wallet.util.NightmodeHelper; @@ -33,6 +38,9 @@ import java.util.Map; import timber.log.Timber; public class ReceiveBottomSheetDialog extends BottomSheetDialogFragment { + private TextView addressTextView = null; + private ImageView addressImageView = null; + private ImageButton copyAddressImageButton = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -42,14 +50,29 @@ public class ReceiveBottomSheetDialog extends BottomSheetDialogFragment { @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - ImageView addressImageView = view.findViewById(R.id.monero_qr_imageview); - TextView addressTextView = view.findViewById(R.id.address_textview); - ImageButton copyAddressImageButton = view.findViewById(R.id.copy_address_imagebutton); + addressImageView = view.findViewById(R.id.monero_qr_imageview); + addressTextView = view.findViewById(R.id.address_textview); + copyAddressImageButton = view.findViewById(R.id.copy_address_imagebutton); + ImageView freshAddressImageView = view.findViewById(R.id.fresh_address_imageview); + Wallet wallet = WalletManager.getInstance().getWallet(); + AddressService addressService = AddressService.getInstance(); - Subaddress addr = AddressService.getInstance().currentSubaddress(); - addressTextView.setText(addr.getAddress()); - addressImageView.setImageBitmap(generate(addr.getAddress(), 256, 256)); - copyAddressImageButton.setOnClickListener(view1 -> Helper.clipBoardCopy(getContext(), "address", addr.getAddress())); + Subaddress addr = addressService.currentSubaddress(); + setAddress(addr); + freshAddressImageView.setOnClickListener(view1 -> { + final int maxSubaddresses = addressService.getLastUsedSubaddress() + wallet.getDeviceType().getSubaddressLookahead(); + if(wallet.getNumSubaddresses() < maxSubaddresses) { + setAddress(AddressService.getInstance().freshSubaddress()); + } else { + Toast.makeText(getContext(), getResources().getString(R.string.max_subaddresses_warning), Toast.LENGTH_SHORT).show(); + } + }); + } + + private void setAddress(Subaddress subaddress) { + addressTextView.setText(subaddress.getAddress()); + addressImageView.setImageBitmap(generate(subaddress.getAddress(), 256, 256)); + copyAddressImageButton.setOnClickListener(view1 -> Helper.clipBoardCopy(getContext(), "address", subaddress.getAddress())); } public Bitmap generate(String text, int width, int height) { diff --git a/app/src/main/java/net/mynero/wallet/service/AddressService.java b/app/src/main/java/net/mynero/wallet/service/AddressService.java index d452c4d..b51301a 100644 --- a/app/src/main/java/net/mynero/wallet/service/AddressService.java +++ b/app/src/main/java/net/mynero/wallet/service/AddressService.java @@ -1,5 +1,6 @@ package net.mynero.wallet.service; +import net.mynero.wallet.MoneroApplication; import net.mynero.wallet.data.Subaddress; import net.mynero.wallet.model.TransactionInfo; import net.mynero.wallet.model.Wallet; @@ -14,6 +15,7 @@ import java.util.Locale; public class AddressService extends ServiceBase { public static AddressService instance = null; private int latestAddressIndex = 1; + private int lastUsedSubaddress = 0; public AddressService(MoneroHandlerThread thread) { super(thread); @@ -25,9 +27,18 @@ public class AddressService extends ServiceBase { } public void refreshAddresses() { + List localTransactionList = new ArrayList<>(HistoryService.getInstance().getHistory()); + for (TransactionInfo info : localTransactionList) { + if (info.addressIndex > lastUsedSubaddress) + lastUsedSubaddress = info.addressIndex; + } latestAddressIndex = WalletManager.getInstance().getWallet().getNumSubaddresses(); } + public int getLastUsedSubaddress() { + return lastUsedSubaddress; + } + public String getPrimaryAddress() { return WalletManager.getInstance().getWallet().getAddress(); } diff --git a/app/src/main/res/layout/receive_bottom_sheet_dialog.xml b/app/src/main/res/layout/receive_bottom_sheet_dialog.xml index f3469d5..c12f973 100644 --- a/app/src/main/res/layout/receive_bottom_sheet_dialog.xml +++ b/app/src/main/res/layout/receive_bottom_sheet_dialog.xml @@ -15,17 +15,27 @@ + + Outpoint: %1$s Create wallet failed: %1$s Wallet Keys + Max subaddresses. Please receive funds first.