Search in sources :

Example 1 with BlockchainState

use of de.schildbach.wallet.service.BlockchainState in project bitcoin-wallet by bitcoin-wallet.

the class WalletBalanceFragment method updateView.

private void updateView() {
    final BlockchainState blockchainState = application.blockchainState.getValue();
    final Coin balance = viewModel.getBalance().getValue();
    final boolean showLocalBalance = getResources().getBoolean(R.bool.show_local_balance) && config.isEnableExchangeRates();
    final ExchangeRateEntry exchangeRate = viewModel.getExchangeRate().getValue();
    final boolean showProgress;
    if (blockchainState != null && blockchainState.bestChainDate != null) {
        final long blockchainLag = System.currentTimeMillis() - blockchainState.bestChainDate.getTime();
        final boolean blockchainUptodate = blockchainLag < BLOCKCHAIN_UPTODATE_THRESHOLD_MS;
        final boolean noImpediments = blockchainState.impediments.isEmpty();
        showProgress = !(blockchainUptodate || !blockchainState.replaying);
        final String downloading = getString(noImpediments ? R.string.blockchain_state_progress_downloading : R.string.blockchain_state_progress_stalled);
        if (blockchainLag < 2 * DateUtils.DAY_IN_MILLIS) {
            final long hours = blockchainLag / DateUtils.HOUR_IN_MILLIS;
            viewProgress.setText(getString(R.string.blockchain_state_progress_hours, downloading, hours));
        } else if (blockchainLag < 2 * DateUtils.WEEK_IN_MILLIS) {
            final long days = blockchainLag / DateUtils.DAY_IN_MILLIS;
            viewProgress.setText(getString(R.string.blockchain_state_progress_days, downloading, days));
        } else if (blockchainLag < 90 * DateUtils.DAY_IN_MILLIS) {
            final long weeks = blockchainLag / DateUtils.WEEK_IN_MILLIS;
            viewProgress.setText(getString(R.string.blockchain_state_progress_weeks, downloading, weeks));
        } else {
            final long months = blockchainLag / (30 * DateUtils.DAY_IN_MILLIS);
            viewProgress.setText(getString(R.string.blockchain_state_progress_months, downloading, months));
        }
    } else {
        showProgress = false;
    }
    if (!showProgress) {
        viewBalance.setVisibility(View.VISIBLE);
        if (!showLocalBalance)
            viewBalanceLocal.setVisibility(View.GONE);
        if (balance != null) {
            viewBalanceBtc.setVisibility(View.VISIBLE);
            viewBalanceBtc.setFormat(config.getFormat());
            viewBalanceBtc.setAmount(balance);
            if (showLocalBalance) {
                if (exchangeRate != null) {
                    final Fiat localValue = exchangeRate.exchangeRate().coinToFiat(balance);
                    viewBalanceLocal.setVisibility(View.VISIBLE);
                    viewBalanceLocal.setFormat(Constants.LOCAL_FORMAT.code(0, Constants.PREFIX_ALMOST_EQUAL_TO + exchangeRate.getCurrencyCode()));
                    viewBalanceLocal.setAmount(localValue);
                    viewBalanceLocal.setTextColor(activity.getColor(R.color.fg_less_significant));
                } else {
                    viewBalanceLocal.setVisibility(View.INVISIBLE);
                }
            }
        } else {
            viewBalanceBtc.setVisibility(View.INVISIBLE);
        }
        viewProgress.setVisibility(View.GONE);
    } else {
        viewProgress.setVisibility(View.VISIBLE);
        viewBalance.setVisibility(View.INVISIBLE);
    }
}
Also used : BlockchainState(de.schildbach.wallet.service.BlockchainState) Coin(org.bitcoinj.core.Coin) ExchangeRateEntry(de.schildbach.wallet.exchangerate.ExchangeRateEntry) Fiat(org.bitcoinj.utils.Fiat)

Example 2 with BlockchainState

use of de.schildbach.wallet.service.BlockchainState in project bitcoin-wallet by bitcoin-wallet.

the class MaybeMaintenanceViewModel method maybeShowDialog.

private void maybeShowDialog() {
    final BlockchainState blockchainState = application.blockchainState.getValue();
    final Boolean maintenanceRecommended = MaybeMaintenanceViewModel.this.walletMaintenanceRecommended.getValue();
    if (blockchainState != null && !blockchainState.replaying && maintenanceRecommended != null && maintenanceRecommended)
        showDialog.postValue(null);
}
Also used : BlockchainState(de.schildbach.wallet.service.BlockchainState)

Example 3 with BlockchainState

use of de.schildbach.wallet.service.BlockchainState 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 4 with BlockchainState

use of de.schildbach.wallet.service.BlockchainState in project bitcoin-wallet by bitcoin-wallet.

the class WalletDisclaimerFragment method updateView.

private void updateView() {
    final BlockchainState blockchainState = application.blockchainState.getValue();
    final Boolean disclaimerEnabled = viewModel.getDisclaimerEnabled().getValue();
    final boolean showDisclaimer = disclaimerEnabled != null && disclaimerEnabled;
    int progressResId = 0;
    if (blockchainState != null) {
        final Set<Impediment> impediments = blockchainState.impediments;
        if (impediments.contains(Impediment.STORAGE))
            progressResId = R.string.blockchain_state_progress_problem_storage;
        else if (impediments.contains(Impediment.NETWORK))
            progressResId = R.string.blockchain_state_progress_problem_network;
    }
    final SpannableStringBuilder text = new SpannableStringBuilder();
    if (progressResId != 0)
        text.append(Html.fromHtml("<b>" + getString(progressResId) + "</b>"));
    if (progressResId != 0 && showDisclaimer)
        text.append('\n');
    if (showDisclaimer)
        text.append(Html.fromHtml(getString(R.string.wallet_disclaimer_fragment_remind_safety)));
    messageView.setText(text);
    final View view = getView();
    final ViewParent parent = view.getParent();
    final View fragment = parent instanceof FrameLayout ? (FrameLayout) parent : view;
    fragment.setVisibility(text.length() > 0 ? View.VISIBLE : View.GONE);
}
Also used : Impediment(de.schildbach.wallet.service.BlockchainState.Impediment) BlockchainState(de.schildbach.wallet.service.BlockchainState) ViewParent(android.view.ViewParent) FrameLayout(android.widget.FrameLayout) TextView(android.widget.TextView) View(android.view.View) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

BlockchainState (de.schildbach.wallet.service.BlockchainState)4 Coin (org.bitcoinj.core.Coin)2 SpannableStringBuilder (android.text.SpannableStringBuilder)1 View (android.view.View)1 ViewParent (android.view.ViewParent)1 FrameLayout (android.widget.FrameLayout)1 TextView (android.widget.TextView)1 AddressBookEntry (de.schildbach.wallet.addressbook.AddressBookEntry)1 ExchangeRateEntry (de.schildbach.wallet.exchangerate.ExchangeRateEntry)1 Impediment (de.schildbach.wallet.service.BlockchainState.Impediment)1 InsufficientMoneyException (org.bitcoinj.core.InsufficientMoneyException)1 Transaction (org.bitcoinj.core.Transaction)1 Fiat (org.bitcoinj.utils.Fiat)1 MonetaryFormat (org.bitcoinj.utils.MonetaryFormat)1 Wallet (org.bitcoinj.wallet.Wallet)1 CouldNotAdjustDownwards (org.bitcoinj.wallet.Wallet.CouldNotAdjustDownwards)1 DustySendRequested (org.bitcoinj.wallet.Wallet.DustySendRequested)1