Search in sources :

Example 1 with CouldNotAdjustDownwards

use of org.bitcoinj.wallet.Wallet.CouldNotAdjustDownwards in project bitcoin-wallet by bitcoin-wallet.

the class SendCoinsFragment method updateView.

private void updateView() {
    final Wallet wallet = walletActivityViewModel.wallet.getValue();
    final Map<FeeCategory, Coin> fees = viewModel.dynamicFees.getValue();
    final BlockchainState blockchainState = application.blockchainState.getValue();
    final Map<String, AddressBookEntry> addressBook = AddressBookEntry.asMap(viewModel.addressBook.getValue());
    if (viewModel.paymentIntent != null) {
        final MonetaryFormat btcFormat = config.getFormat();
        getView().setVisibility(View.VISIBLE);
        if (viewModel.paymentIntent.hasPayee()) {
            payeeNameView.setVisibility(View.VISIBLE);
            payeeNameView.setText(viewModel.paymentIntent.payeeName);
            payeeVerifiedByView.setVisibility(View.VISIBLE);
            final String verifiedBy = viewModel.paymentIntent.payeeVerifiedBy != null ? viewModel.paymentIntent.payeeVerifiedBy : getString(R.string.send_coins_fragment_payee_verified_by_unknown);
            payeeVerifiedByView.setText(Constants.CHAR_CHECKMARK + String.format(getString(R.string.send_coins_fragment_payee_verified_by), verifiedBy));
        } else {
            payeeNameView.setVisibility(View.GONE);
            payeeVerifiedByView.setVisibility(View.GONE);
        }
        if (viewModel.paymentIntent.hasOutputs()) {
            payeeGroup.setVisibility(View.VISIBLE);
            receivingAddressView.setVisibility(View.GONE);
            receivingStaticView.setVisibility(!viewModel.paymentIntent.hasPayee() || viewModel.paymentIntent.payeeVerifiedBy == null ? View.VISIBLE : View.GONE);
            receivingStaticLabelView.setText(viewModel.paymentIntent.memo);
            if (viewModel.paymentIntent.hasAddress())
                receivingStaticAddressView.setText(WalletUtils.formatAddress(viewModel.paymentIntent.getAddress(), Constants.ADDRESS_FORMAT_GROUP_SIZE, Constants.ADDRESS_FORMAT_LINE_SIZE));
            else
                receivingStaticAddressView.setText(R.string.send_coins_fragment_receiving_address_complex);
        } else if (viewModel.validatedAddress != null) {
            payeeGroup.setVisibility(View.VISIBLE);
            receivingAddressView.setVisibility(View.GONE);
            receivingStaticView.setVisibility(View.VISIBLE);
            receivingStaticAddressView.setText(WalletUtils.formatAddress(viewModel.validatedAddress.address, Constants.ADDRESS_FORMAT_GROUP_SIZE, Constants.ADDRESS_FORMAT_LINE_SIZE));
            final String addressBookLabel = addressBookDao.resolveLabel(viewModel.validatedAddress.address.toString());
            final String staticLabel;
            if (addressBookLabel != null)
                staticLabel = addressBookLabel;
            else if (viewModel.validatedAddress.label != null)
                staticLabel = viewModel.validatedAddress.label;
            else
                staticLabel = getString(R.string.address_unlabeled);
            receivingStaticLabelView.setText(staticLabel);
            receivingStaticLabelView.setTextColor(activity.getColor(viewModel.validatedAddress.label != null ? R.color.fg_significant : R.color.fg_insignificant));
        } else if (viewModel.paymentIntent.standard == null) {
            payeeGroup.setVisibility(View.VISIBLE);
            receivingStaticView.setVisibility(View.GONE);
            receivingAddressView.setVisibility(View.VISIBLE);
        } else {
            payeeGroup.setVisibility(View.GONE);
        }
        receivingAddressView.setEnabled(viewModel.state == SendCoinsViewModel.State.INPUT);
        amountGroup.setVisibility(viewModel.paymentIntent.hasAmount() || (viewModel.state != null && viewModel.state.compareTo(SendCoinsViewModel.State.INPUT) >= 0) ? View.VISIBLE : View.GONE);
        amountCalculatorLink.setEnabled(viewModel.state == SendCoinsViewModel.State.INPUT && viewModel.paymentIntent.mayEditAmount());
        final boolean directPaymentVisible;
        if (viewModel.paymentIntent.hasPaymentUrl()) {
            if (viewModel.paymentIntent.isBluetoothPaymentUrl())
                directPaymentVisible = bluetoothAdapter != null;
            else
                directPaymentVisible = true;
        } else {
            directPaymentVisible = false;
        }
        directPaymentEnableView.setVisibility(directPaymentVisible ? View.VISIBLE : View.GONE);
        directPaymentEnableView.setEnabled(viewModel.state == SendCoinsViewModel.State.INPUT);
        hintView.setVisibility(View.GONE);
        if (viewModel.state == SendCoinsViewModel.State.INPUT) {
            if (blockchainState != null && blockchainState.replaying) {
                hintView.setTextColor(activity.getColor(R.color.fg_error));
                hintView.setVisibility(View.VISIBLE);
                hintView.setText(R.string.send_coins_fragment_hint_replaying);
            } else if (viewModel.paymentIntent.mayEditAddress() && viewModel.validatedAddress == null && !receivingAddressView.getText().toString().trim().isEmpty()) {
                hintView.setTextColor(activity.getColor(R.color.fg_error));
                hintView.setVisibility(View.VISIBLE);
                hintView.setText(R.string.send_coins_fragment_receiving_address_error);
            } else if (viewModel.dryrunException != null) {
                hintView.setTextColor(activity.getColor(R.color.fg_error));
                hintView.setVisibility(View.VISIBLE);
                if (viewModel.dryrunException instanceof DustySendRequested)
                    hintView.setText(getString(R.string.send_coins_fragment_hint_dusty_send));
                else if (viewModel.dryrunException instanceof InsufficientMoneyException)
                    hintView.setText(getString(R.string.send_coins_fragment_hint_insufficient_money, btcFormat.format(((InsufficientMoneyException) viewModel.dryrunException).missing)));
                else if (viewModel.dryrunException instanceof CouldNotAdjustDownwards)
                    hintView.setText(getString(R.string.send_coins_fragment_hint_empty_wallet_failed));
                else
                    hintView.setText(viewModel.dryrunException.toString());
            } else if (viewModel.dryrunTransaction != null && viewModel.dryrunTransaction.getFee() != null) {
                hintView.setVisibility(View.VISIBLE);
                final int hintResId;
                final int colorResId;
                if (viewModel.feeCategory == FeeCategory.ECONOMIC) {
                    hintResId = R.string.send_coins_fragment_hint_fee_economic;
                    colorResId = R.color.fg_less_significant;
                } else if (viewModel.feeCategory == FeeCategory.PRIORITY) {
                    hintResId = R.string.send_coins_fragment_hint_fee_priority;
                    colorResId = R.color.fg_less_significant;
                } else {
                    hintResId = R.string.send_coins_fragment_hint_fee;
                    colorResId = R.color.fg_insignificant;
                }
                hintView.setTextColor(activity.getColor(colorResId));
                hintView.setText(getString(hintResId, btcFormat.format(viewModel.dryrunTransaction.getFee())));
            } else if (viewModel.paymentIntent.mayEditAddress() && viewModel.validatedAddress != null && wallet != null && wallet.isAddressMine(viewModel.validatedAddress.address)) {
                hintView.setTextColor(activity.getColor(R.color.fg_insignificant));
                hintView.setVisibility(View.VISIBLE);
                hintView.setText(R.string.send_coins_fragment_receiving_address_own);
            }
        }
        final Transaction sentTransaction = viewModel.sentTransaction.getValue();
        if (sentTransaction != null && wallet != null) {
            sentTransactionView.setVisibility(View.VISIBLE);
            sentTransactionViewHolder.fullBind(new TransactionsAdapter.ListItem.TransactionItem(activity, sentTransaction, wallet, addressBook, btcFormat, application.maxConnectedPeers()));
        } else {
            sentTransactionView.setVisibility(View.GONE);
        }
        if (viewModel.directPaymentAck != null) {
            directPaymentMessageView.setVisibility(View.VISIBLE);
            directPaymentMessageView.setText(viewModel.directPaymentAck ? R.string.send_coins_fragment_direct_payment_ack : R.string.send_coins_fragment_direct_payment_nack);
        } else {
            directPaymentMessageView.setVisibility(View.GONE);
        }
        viewCancel.setEnabled(viewModel.state != SendCoinsViewModel.State.REQUEST_PAYMENT_REQUEST && viewModel.state != SendCoinsViewModel.State.DECRYPTING && viewModel.state != SendCoinsViewModel.State.SIGNING);
        viewGo.setEnabled(everythingPlausible() && viewModel.dryrunTransaction != null && wallet != null && fees != null && (blockchainState == null || !blockchainState.replaying));
        if (viewModel.state == null || viewModel.state == SendCoinsViewModel.State.REQUEST_PAYMENT_REQUEST) {
            viewCancel.setText(R.string.button_cancel);
            viewGo.setText(null);
        } else if (viewModel.state == SendCoinsViewModel.State.INPUT) {
            viewCancel.setText(R.string.button_cancel);
            viewGo.setText(R.string.send_coins_fragment_button_send);
        } else if (viewModel.state == SendCoinsViewModel.State.DECRYPTING) {
            viewCancel.setText(R.string.button_cancel);
            viewGo.setText(R.string.send_coins_fragment_state_decrypting);
        } else if (viewModel.state == SendCoinsViewModel.State.SIGNING) {
            viewCancel.setText(R.string.button_cancel);
            viewGo.setText(R.string.send_coins_preparation_msg);
        } else if (viewModel.state == SendCoinsViewModel.State.SENDING) {
            viewCancel.setText(R.string.send_coins_fragment_button_back);
            viewGo.setText(R.string.send_coins_sending_msg);
        } else if (viewModel.state == SendCoinsViewModel.State.SENT) {
            viewCancel.setText(R.string.send_coins_fragment_button_back);
            viewGo.setText(R.string.send_coins_sent_msg);
        } else if (viewModel.state == SendCoinsViewModel.State.FAILED) {
            viewCancel.setText(R.string.send_coins_fragment_button_back);
            viewGo.setText(R.string.send_coins_failed_msg);
        }
        final boolean privateKeyPasswordViewVisible = (viewModel.state == SendCoinsViewModel.State.INPUT || viewModel.state == SendCoinsViewModel.State.DECRYPTING) && wallet != null && wallet.isEncrypted();
        privateKeyPasswordViewGroup.setVisibility(privateKeyPasswordViewVisible ? View.VISIBLE : View.GONE);
        privateKeyPasswordView.setEnabled(viewModel.state == SendCoinsViewModel.State.INPUT);
        // focus linking
        final int activeAmountViewId = amountCalculatorLink.activeTextView().getId();
        receivingAddressView.setNextFocusDownId(activeAmountViewId);
        receivingAddressView.setNextFocusForwardId(activeAmountViewId);
        amountCalculatorLink.setNextFocusId(privateKeyPasswordViewVisible ? R.id.send_coins_private_key_password : R.id.send_coins_go);
        privateKeyPasswordView.setNextFocusUpId(activeAmountViewId);
        privateKeyPasswordView.setNextFocusDownId(R.id.send_coins_go);
        privateKeyPasswordView.setNextFocusForwardId(R.id.send_coins_go);
        viewGo.setNextFocusUpId(privateKeyPasswordViewVisible ? R.id.send_coins_private_key_password : activeAmountViewId);
    } else {
        getView().setVisibility(View.GONE);
    }
}
Also used : DustySendRequested(org.bitcoinj.wallet.Wallet.DustySendRequested) MonetaryFormat(org.bitcoinj.utils.MonetaryFormat) Wallet(org.bitcoinj.wallet.Wallet) AddressBookEntry(de.schildbach.wallet.addressbook.AddressBookEntry) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) CouldNotAdjustDownwards(org.bitcoinj.wallet.Wallet.CouldNotAdjustDownwards) Coin(org.bitcoinj.core.Coin) BlockchainState(de.schildbach.wallet.service.BlockchainState) Transaction(org.bitcoinj.core.Transaction)

Example 2 with CouldNotAdjustDownwards

use of org.bitcoinj.wallet.Wallet.CouldNotAdjustDownwards in project bitcoin-wallet by bitcoin-wallet.

the class SendCoinsOfflineTask method sendCoinsOffline.

public final void sendCoinsOffline(final SendRequest sendRequest) {
    backgroundHandler.post(() -> {
        org.bitcoinj.core.Context.propagate(Constants.CONTEXT);
        try {
            log.info("sending: {}", sendRequest);
            // can take long
            final Transaction transaction = wallet.sendCoinsOffline(sendRequest);
            log.info("send successful, transaction committed: {}", transaction.getTxId());
            callbackHandler.post(() -> onSuccess(transaction));
        } catch (final InsufficientMoneyException x) {
            final Coin missing = x.missing;
            if (missing != null)
                log.info("send failed, {} missing", missing.toFriendlyString());
            else
                log.info("send failed, insufficient coins");
            callbackHandler.post(() -> onInsufficientMoney(x.missing));
        } catch (final ECKey.KeyIsEncryptedException x) {
            log.info("send failed, key is encrypted: {}", x.getMessage());
            callbackHandler.post(() -> onFailure(x));
        } catch (final Wallet.BadWalletEncryptionKeyException x) {
            log.info("send failed, bad spending password: {}", x.getMessage());
            final boolean isEncrypted = wallet.isEncrypted();
            callbackHandler.post(() -> {
                if (isEncrypted)
                    onInvalidEncryptionKey();
                else
                    onFailure(x);
            });
        } catch (final CouldNotAdjustDownwards x) {
            log.info("send failed, could not adjust downwards: {}", x.getMessage());
            callbackHandler.post(() -> onEmptyWalletFailed());
        } catch (final CompletionException x) {
            log.info("send failed, cannot complete: {}", x.getMessage());
            callbackHandler.post(() -> onFailure(x));
        }
    });
}
Also used : Coin(org.bitcoinj.core.Coin) Transaction(org.bitcoinj.core.Transaction) Wallet(org.bitcoinj.wallet.Wallet) CompletionException(org.bitcoinj.wallet.Wallet.CompletionException) InsufficientMoneyException(org.bitcoinj.core.InsufficientMoneyException) ECKey(org.bitcoinj.core.ECKey) CouldNotAdjustDownwards(org.bitcoinj.wallet.Wallet.CouldNotAdjustDownwards)

Aggregations

Coin (org.bitcoinj.core.Coin)2 InsufficientMoneyException (org.bitcoinj.core.InsufficientMoneyException)2 Transaction (org.bitcoinj.core.Transaction)2 Wallet (org.bitcoinj.wallet.Wallet)2 CouldNotAdjustDownwards (org.bitcoinj.wallet.Wallet.CouldNotAdjustDownwards)2 AddressBookEntry (de.schildbach.wallet.addressbook.AddressBookEntry)1 BlockchainState (de.schildbach.wallet.service.BlockchainState)1 ECKey (org.bitcoinj.core.ECKey)1 MonetaryFormat (org.bitcoinj.utils.MonetaryFormat)1 CompletionException (org.bitcoinj.wallet.Wallet.CompletionException)1 DustySendRequested (org.bitcoinj.wallet.Wallet.DustySendRequested)1