Search in sources :

Example 1 with ExchangeRate

use of de.schildbach.wallet.data.ExchangeRate in project bitcoin-wallet by bitcoin-wallet.

the class Configuration method getCachedExchangeRate.

public ExchangeRate getCachedExchangeRate() {
    if (prefs.contains(PREFS_KEY_CACHED_EXCHANGE_CURRENCY) && prefs.contains(PREFS_KEY_CACHED_EXCHANGE_RATE_COIN) && prefs.contains(PREFS_KEY_CACHED_EXCHANGE_RATE_FIAT)) {
        final String cachedExchangeCurrency = prefs.getString(PREFS_KEY_CACHED_EXCHANGE_CURRENCY, null);
        final Coin cachedExchangeRateCoin = Coin.valueOf(prefs.getLong(PREFS_KEY_CACHED_EXCHANGE_RATE_COIN, 0));
        final Fiat cachedExchangeRateFiat = Fiat.valueOf(cachedExchangeCurrency, prefs.getLong(PREFS_KEY_CACHED_EXCHANGE_RATE_FIAT, 0));
        return new ExchangeRate(new org.bitcoinj.utils.ExchangeRate(cachedExchangeRateCoin, cachedExchangeRateFiat), null);
    } else {
        return null;
    }
}
Also used : Coin(org.bitcoinj.core.Coin) ExchangeRate(de.schildbach.wallet.data.ExchangeRate) Fiat(org.bitcoinj.utils.Fiat)

Example 2 with ExchangeRate

use of de.schildbach.wallet.data.ExchangeRate 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)

Aggregations

ExchangeRate (de.schildbach.wallet.data.ExchangeRate)2 Fiat (org.bitcoinj.utils.Fiat)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Spannable (android.text.Spannable)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 RemoteViews (android.widget.RemoteViews)1 MonetarySpannable (de.schildbach.wallet.util.MonetarySpannable)1 Coin (org.bitcoinj.core.Coin)1 MonetaryFormat (org.bitcoinj.utils.MonetaryFormat)1