Search in sources :

Example 1 with MonetaryFormat

use of org.bitcoinj.utils.MonetaryFormat in project bitsquare by bitsquare.

the class OfferForJson method setDisplayStrings.

private void setDisplayStrings() {
    try {
        MonetaryFormat fiatFormat = MonetaryFormat.FIAT.repeatOptionalDecimals(0, 0);
        MonetaryFormat coinFormat = MonetaryFormat.BTC.minDecimals(2).repeatOptionalDecimals(1, 6);
        final Fiat priceAsFiat = getPriceAsFiat();
        if (CurrencyUtil.isCryptoCurrency(currencyCode)) {
            primaryMarketDirection = direction == Offer.Direction.BUY ? Offer.Direction.SELL : Offer.Direction.BUY;
            currencyPair = currencyCode + "/" + "BTC";
            DecimalFormat decimalFormat = new DecimalFormat("#.#");
            decimalFormat.setMaximumFractionDigits(8);
            final double value = priceAsFiat.value != 0 ? 10000D / priceAsFiat.value : 0;
            priceDisplayString = decimalFormat.format(MathUtils.roundDouble(value, 8)).replace(",", ".");
            primaryMarketMinAmountDisplayString = fiatFormat.noCode().format(getMinVolumeAsFiat()).toString();
            primaryMarketAmountDisplayString = fiatFormat.noCode().format(getVolumeAsFiat()).toString();
            primaryMarketMinVolumeDisplayString = coinFormat.noCode().format(getMinAmountAsCoin()).toString();
            primaryMarketVolumeDisplayString = coinFormat.noCode().format(getAmountAsCoin()).toString();
            primaryMarketPrice = MathUtils.roundDoubleToLong(MathUtils.scaleUpByPowerOf10(value, 8));
            primaryMarketMinAmount = (long) MathUtils.scaleUpByPowerOf10(getMinVolumeAsFiat().longValue(), 4);
            primaryMarketAmount = (long) MathUtils.scaleUpByPowerOf10(getVolumeAsFiat().longValue(), 4);
            primaryMarketMinVolume = getMinAmountAsCoin().longValue();
            primaryMarketVolume = getAmountAsCoin().longValue();
        } else {
            primaryMarketDirection = direction;
            currencyPair = "BTC/" + currencyCode;
            priceDisplayString = fiatFormat.noCode().format(priceAsFiat).toString();
            primaryMarketMinAmountDisplayString = coinFormat.noCode().format(getMinAmountAsCoin()).toString();
            primaryMarketAmountDisplayString = coinFormat.noCode().format(getAmountAsCoin()).toString();
            primaryMarketMinVolumeDisplayString = fiatFormat.noCode().format(getMinVolumeAsFiat()).toString();
            primaryMarketVolumeDisplayString = fiatFormat.noCode().format(getVolumeAsFiat()).toString();
            primaryMarketPrice = (long) MathUtils.scaleUpByPowerOf10(priceAsFiat.longValue(), 4);
            primaryMarketMinAmount = getMinAmountAsCoin().longValue();
            primaryMarketAmount = getAmountAsCoin().longValue();
            primaryMarketMinVolume = (long) MathUtils.scaleUpByPowerOf10(getMinVolumeAsFiat().longValue(), 4);
            primaryMarketVolume = (long) MathUtils.scaleUpByPowerOf10(getVolumeAsFiat().longValue(), 4);
        }
    } catch (Throwable t) {
        log.error("Error at setDisplayStrings: " + t.getMessage());
    }
}
Also used : MonetaryFormat(org.bitcoinj.utils.MonetaryFormat) Fiat(org.bitcoinj.utils.Fiat) DecimalFormat(java.text.DecimalFormat)

Example 2 with MonetaryFormat

use of org.bitcoinj.utils.MonetaryFormat in project bitcoin-wallet by bitcoin-wallet.

the class InactivityNotificationService method handleMaybeShowNotification.

private void handleMaybeShowNotification() {
    final Coin estimatedBalance = wallet.getBalance(BalanceType.ESTIMATED_SPENDABLE);
    if (estimatedBalance.isPositive()) {
        log.info("detected balance, showing inactivity notification");
        final Coin availableBalance = wallet.getBalance(BalanceType.AVAILABLE_SPENDABLE);
        final boolean canDonate = Constants.DONATION_ADDRESS != null && availableBalance.isPositive();
        final MonetaryFormat btcFormat = config.getFormat();
        final String title = getString(R.string.notification_inactivity_title);
        final StringBuilder text = new StringBuilder(getString(R.string.notification_inactivity_message, btcFormat.format(estimatedBalance)));
        if (canDonate)
            text.append("\n\n").append(getString(R.string.notification_inactivity_message_donate));
        final Intent dismissIntent = new Intent(this, InactivityNotificationService.class);
        dismissIntent.setAction(ACTION_DISMISS);
        final Intent dismissForeverIntent = new Intent(this, InactivityNotificationService.class);
        dismissForeverIntent.setAction(ACTION_DISMISS_FOREVER);
        final Intent donateIntent = new Intent(this, InactivityNotificationService.class);
        donateIntent.setAction(ACTION_DONATE);
        final NotificationCompat.Builder notification = new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID_IMPORTANT);
        notification.setStyle(new NotificationCompat.BigTextStyle().bigText(text));
        notification.setSmallIcon(R.drawable.stat_notify_received_24dp);
        notification.setContentTitle(title);
        notification.setContentText(text);
        notification.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, WalletActivity.class), 0));
        notification.setAutoCancel(true);
        if (!canDonate)
            notification.addAction(new NotificationCompat.Action.Builder(0, getString(R.string.notification_inactivity_action_dismiss), PendingIntent.getService(this, 0, dismissIntent, 0)).build());
        notification.addAction(new NotificationCompat.Action.Builder(0, getString(R.string.notification_inactivity_action_dismiss_forever), PendingIntent.getService(this, 0, dismissForeverIntent, 0)).build());
        if (canDonate)
            notification.addAction(new NotificationCompat.Action.Builder(0, getString(R.string.wallet_options_donate), PendingIntent.getService(this, 0, donateIntent, 0)).build());
        nm.notify(Constants.NOTIFICATION_ID_INACTIVITY, notification.build());
    }
}
Also used : Coin(org.bitcoinj.core.Coin) MonetaryFormat(org.bitcoinj.utils.MonetaryFormat) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent)

Example 3 with MonetaryFormat

use of org.bitcoinj.utils.MonetaryFormat in project bitcoin-wallet by bitcoin-wallet.

the class WalletBalanceWidgetProvider method updateWidget.

private static void updateWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId, final Bundle appWidgetOptions, final Coin balance) {
    final WalletApplication application = (WalletApplication) context.getApplicationContext();
    final Configuration config = application.getConfiguration();
    final MonetaryFormat btcFormat = config.getFormat();
    final Spannable balanceStr = new MonetarySpannable(btcFormat.noCode(), balance).applyMarkup(null, MonetarySpannable.STANDARD_INSIGNIFICANT_SPANS);
    final Cursor data = context.getContentResolver().query(ExchangeRatesProvider.contentUri(context.getPackageName(), true), null, ExchangeRatesProvider.KEY_CURRENCY_CODE, new String[] { config.getExchangeCurrencyCode() }, null);
    final Spannable localBalanceStr;
    if (data != null) {
        if (data.moveToFirst()) {
            final ExchangeRate exchangeRate = ExchangeRatesProvider.getExchangeRate(data);
            final Fiat localBalance = exchangeRate.rate.coinToFiat(balance);
            final MonetaryFormat localFormat = Constants.LOCAL_FORMAT.code(0, Constants.PREFIX_ALMOST_EQUAL_TO + GenericUtils.currencySymbol(exchangeRate.getCurrencyCode()));
            final Object[] prefixSpans = new Object[] { MonetarySpannable.SMALLER_SPAN, new ForegroundColorSpan(context.getResources().getColor(R.color.fg_less_significant)) };
            localBalanceStr = new MonetarySpannable(localFormat, localBalance).applyMarkup(prefixSpans, MonetarySpannable.STANDARD_INSIGNIFICANT_SPANS);
        } else {
            localBalanceStr = null;
        }
        data.close();
    } else {
        localBalanceStr = null;
    }
    final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wallet_balance_widget_content);
    final String currencyCode = btcFormat.code();
    if (MonetaryFormat.CODE_BTC.equals(currencyCode))
        views.setImageViewResource(R.id.widget_wallet_prefix, R.drawable.currency_symbol_btc);
    else if (MonetaryFormat.CODE_MBTC.equals(currencyCode))
        views.setImageViewResource(R.id.widget_wallet_prefix, R.drawable.currency_symbol_mbtc);
    else if (MonetaryFormat.CODE_UBTC.equals(currencyCode))
        views.setImageViewResource(R.id.widget_wallet_prefix, R.drawable.currency_symbol_ubtc);
    views.setTextViewText(R.id.widget_wallet_balance_btc, balanceStr);
    views.setViewVisibility(R.id.widget_wallet_balance_local, localBalanceStr != null ? View.VISIBLE : View.GONE);
    views.setTextViewText(R.id.widget_wallet_balance_local, localBalanceStr);
    if (appWidgetOptions != null) {
        final int minWidth = appWidgetOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
        views.setViewVisibility(R.id.widget_app_icon, minWidth > 400 ? View.VISIBLE : View.GONE);
        views.setViewVisibility(R.id.widget_button_request, minWidth > 300 ? View.VISIBLE : View.GONE);
        views.setViewVisibility(R.id.widget_button_send, minWidth > 300 ? View.VISIBLE : View.GONE);
        views.setViewVisibility(R.id.widget_button_send_qr, minWidth > 200 ? View.VISIBLE : View.GONE);
    }
    views.setOnClickPendingIntent(R.id.widget_button_balance, PendingIntent.getActivity(context, 0, new Intent(context, WalletActivity.class), 0));
    views.setOnClickPendingIntent(R.id.widget_button_request, PendingIntent.getActivity(context, 0, new Intent(context, RequestCoinsActivity.class), 0));
    views.setOnClickPendingIntent(R.id.widget_button_send, PendingIntent.getActivity(context, 0, new Intent(context, SendCoinsActivity.class), 0));
    views.setOnClickPendingIntent(R.id.widget_button_send_qr, PendingIntent.getActivity(context, 0, new Intent(context, SendCoinsQrActivity.class), 0));
    appWidgetManager.updateAppWidget(appWidgetId, views);
}
Also used : ExchangeRate(de.schildbach.wallet.data.ExchangeRate) MonetaryFormat(org.bitcoinj.utils.MonetaryFormat) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Cursor(android.database.Cursor) MonetarySpannable(de.schildbach.wallet.util.MonetarySpannable) RemoteViews(android.widget.RemoteViews) Fiat(org.bitcoinj.utils.Fiat) MonetarySpannable(de.schildbach.wallet.util.MonetarySpannable) Spannable(android.text.Spannable)

Example 4 with MonetaryFormat

use of org.bitcoinj.utils.MonetaryFormat in project bitcoin-wallet by bitcoin-wallet.

the class BlockchainService method notifyCoinsReceived.

private void notifyCoinsReceived(@Nullable final Address address, final Coin amount, final Sha256Hash transactionHash) {
    notificationCount++;
    notificationAccumulatedAmount = notificationAccumulatedAmount.add(amount);
    if (address != null && !notificationAddresses.contains(address))
        notificationAddresses.add(address);
    final MonetaryFormat btcFormat = config.getFormat();
    final String packageFlavor = application.applicationPackageFlavor();
    final String msgSuffix = packageFlavor != null ? " [" + packageFlavor + "]" : "";
    // summary notification
    final NotificationCompat.Builder summaryNotification = new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID_RECEIVED);
    summaryNotification.setGroup(Constants.NOTIFICATION_GROUP_KEY_RECEIVED);
    summaryNotification.setGroupSummary(true);
    summaryNotification.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
    summaryNotification.setWhen(System.currentTimeMillis());
    summaryNotification.setSmallIcon(R.drawable.stat_notify_received_24dp);
    summaryNotification.setContentTitle(getString(R.string.notification_coins_received_msg, btcFormat.format(notificationAccumulatedAmount)) + msgSuffix);
    if (!notificationAddresses.isEmpty()) {
        final StringBuilder text = new StringBuilder();
        for (final Address notificationAddress : notificationAddresses) {
            if (text.length() > 0)
                text.append(", ");
            final String addressStr = notificationAddress.toString();
            final String label = addressBookDao.resolveLabel(addressStr);
            text.append(label != null ? label : addressStr);
        }
        summaryNotification.setContentText(text);
    }
    summaryNotification.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, WalletActivity.class), 0));
    nm.notify(Constants.NOTIFICATION_ID_COINS_RECEIVED, summaryNotification.build());
    // child notification
    final NotificationCompat.Builder childNotification = new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID_RECEIVED);
    childNotification.setGroup(Constants.NOTIFICATION_GROUP_KEY_RECEIVED);
    childNotification.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
    childNotification.setWhen(System.currentTimeMillis());
    childNotification.setColor(getColor(R.color.fg_network_significant));
    childNotification.setSmallIcon(R.drawable.stat_notify_received_24dp);
    final String msg = getString(R.string.notification_coins_received_msg, btcFormat.format(amount)) + msgSuffix;
    childNotification.setTicker(msg);
    childNotification.setContentTitle(msg);
    if (address != null) {
        final String addressStr = address.toString();
        final String addressLabel = addressBookDao.resolveLabel(addressStr);
        if (addressLabel != null)
            childNotification.setContentText(addressLabel);
        else
            childNotification.setContentText(addressStr);
    }
    childNotification.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, WalletActivity.class), 0));
    childNotification.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.coins_received));
    nm.notify(transactionHash.toString(), Constants.NOTIFICATION_ID_COINS_RECEIVED, childNotification.build());
}
Also used : MonetaryFormat(org.bitcoinj.utils.MonetaryFormat) InetSocketAddress(java.net.InetSocketAddress) Address(org.bitcoinj.core.Address) PeerAddress(org.bitcoinj.core.PeerAddress) NotificationCompat(androidx.core.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent)

Example 5 with MonetaryFormat

use of org.bitcoinj.utils.MonetaryFormat in project bitcoin-wallet by bitcoin-wallet.

the class SendCoinsFragment method signAndSendPayment.

private void signAndSendPayment(final KeyParameter encryptionKey) {
    setState(SendCoinsViewModel.State.SIGNING);
    // final payment intent
    final PaymentIntent finalPaymentIntent = viewModel.paymentIntent.mergeWithEditedValues(amountCalculatorLink.getAmount(), viewModel.validatedAddress != null ? viewModel.validatedAddress.address : null);
    final Coin finalAmount = finalPaymentIntent.getAmount();
    // prepare send request
    final Map<FeeCategory, Coin> fees = viewModel.dynamicFees.getValue();
    final Wallet wallet = walletActivityViewModel.wallet.getValue();
    final SendRequest sendRequest = finalPaymentIntent.toSendRequest();
    sendRequest.emptyWallet = viewModel.paymentIntent.mayEditAmount() && finalAmount.equals(wallet.getBalance(BalanceType.AVAILABLE));
    sendRequest.feePerKb = fees.get(viewModel.feeCategory);
    sendRequest.memo = viewModel.paymentIntent.memo;
    sendRequest.exchangeRate = amountCalculatorLink.getExchangeRate();
    sendRequest.aesKey = encryptionKey;
    final Coin fee = viewModel.dryrunTransaction.getFee();
    if (fee.isGreaterThan(finalAmount)) {
        setState(SendCoinsViewModel.State.INPUT);
        final MonetaryFormat btcFormat = config.getFormat();
        final DialogBuilder dialog = DialogBuilder.warn(activity, R.string.send_coins_fragment_significant_fee_title, R.string.send_coins_fragment_significant_fee_message, btcFormat.format(fee), btcFormat.format(finalAmount));
        dialog.setPositiveButton(R.string.send_coins_fragment_button_send, (d, which) -> sendPayment(sendRequest, finalAmount));
        dialog.setNegativeButton(R.string.button_cancel, null);
        dialog.show();
    } else {
        sendPayment(sendRequest, finalAmount);
    }
}
Also used : Coin(org.bitcoinj.core.Coin) SendRequest(org.bitcoinj.wallet.SendRequest) MonetaryFormat(org.bitcoinj.utils.MonetaryFormat) Wallet(org.bitcoinj.wallet.Wallet) PaymentIntent(de.schildbach.wallet.data.PaymentIntent) DialogBuilder(de.schildbach.wallet.ui.DialogBuilder)

Aggregations

MonetaryFormat (org.bitcoinj.utils.MonetaryFormat)15 Coin (org.bitcoinj.core.Coin)8 Intent (android.content.Intent)7 Transaction (org.bitcoinj.core.Transaction)7 Wallet (org.bitcoinj.wallet.Wallet)7 PendingIntent (android.app.PendingIntent)5 DialogBuilder (de.schildbach.wallet.ui.DialogBuilder)4 View (android.view.View)3 TextView (android.widget.TextView)3 Activity (android.app.Activity)2 Context (android.content.Context)2 PackageManager (android.content.pm.PackageManager)2 Bundle (android.os.Bundle)2 Handler (android.os.Handler)2 HandlerThread (android.os.HandlerThread)2 Process (android.os.Process)2 Spannable (android.text.Spannable)2 ForegroundColorSpan (android.text.style.ForegroundColorSpan)2 LayoutInflater (android.view.LayoutInflater)2 Menu (android.view.Menu)2