Skip to content

Commit

Permalink
Horizontally resizable widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
schildbach committed Oct 2, 2014
1 parent 795a448 commit 6db0d19
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 34 deletions.
Binary file modified wallet/res/drawable-xhdpi/widget_preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions wallet/res/layout/wallet_balance_widget_content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
android:orientation="horizontal"
android:showDividers="middle" >

<ImageView
android:id="@+id/widget_app_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="12dp"
android:contentDescription="@string/app_name"
android:src="@drawable/app_icon"
android:visibility="gone" />

<LinearLayout
android:id="@+id/widget_button_balance"
android:layout_width="0px"
Expand Down
9 changes: 9 additions & 0 deletions wallet/res/xml-v16/wallet_balance_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/wallet_balance_widget_content"
android:minHeight="64dp"
android:minResizeWidth="132dp"
android:minWidth="294dp"
android:previewImage="@drawable/widget_preview"
android:resizeMode="horizontal"
android:updatePeriodMillis="1800000" />
103 changes: 69 additions & 34 deletions wallet/src/de/schildbach/wallet/WalletBalanceWidgetProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package de.schildbach.wallet;

import javax.annotation.Nonnull;
import java.lang.reflect.Method;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Wallet;
Expand All @@ -34,6 +34,7 @@
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
Expand All @@ -59,16 +60,24 @@ public class WalletBalanceWidgetProvider extends AppWidgetProvider
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds)
{
final WalletApplication application = (WalletApplication) context.getApplicationContext();
final Wallet wallet = application.getWallet();
final Coin balance = wallet.getBalance(BalanceType.ESTIMATED);
final Coin balance = application.getWallet().getBalance(BalanceType.ESTIMATED);

updateWidgets(context, appWidgetManager, appWidgetIds, balance);
}

@Override
public void onAppWidgetOptionsChanged(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId,
final Bundle newOptions)
{
final WalletApplication application = (WalletApplication) context.getApplicationContext();
final Coin balance = application.getWallet().getBalance(BalanceType.ESTIMATED);

updateWidget(context, appWidgetManager, appWidgetId, newOptions, balance);
}

public static void updateWidgets(final Context context, final Wallet wallet)
{
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

final ComponentName providerName = new ComponentName(context, WalletBalanceWidgetProvider.class);

try
Expand All @@ -78,7 +87,6 @@ public static void updateWidgets(final Context context, final Wallet wallet)
if (appWidgetIds.length > 0)
{
final Coin balance = wallet.getBalance(BalanceType.ESTIMATED);

WalletBalanceWidgetProvider.updateWidgets(context, appWidgetManager, appWidgetIds, balance);
}
}
Expand All @@ -88,10 +96,22 @@ public static void updateWidgets(final Context context, final Wallet wallet)
}
}

private static void updateWidgets(final Context context, @Nonnull final AppWidgetManager appWidgetManager, @Nonnull final int[] appWidgetIds,
@Nonnull final Coin balance)
private static void updateWidgets(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds, final Coin balance)
{
for (final int appWidgetId : appWidgetIds)
{
final Bundle options = getAppWidgetOptions(appWidgetManager, appWidgetId);
updateWidget(context, appWidgetManager, appWidgetId, options, balance);
}
}

private static void updateWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId,
final Bundle appWidgetOptions, final Coin balance)
{
final Configuration config = new Configuration(PreferenceManager.getDefaultSharedPreferences(context));
final MonetaryFormat btcFormat = config.getFormat();

final Spannable balanceStr = new MonetarySpannable(btcFormat.noCode(), balance).applyMarkup(null, null, MonetarySpannable.SMALLER_SPAN);

final Cursor data = context.getContentResolver().query(ExchangeRatesProvider.contentUri(context.getPackageName(), true), null,
ExchangeRatesProvider.KEY_CURRENCY_CODE, new String[] { config.getExchangeCurrencyCode() }, null);
Expand Down Expand Up @@ -119,35 +139,50 @@ private static void updateWidgets(final Context context, @Nonnull final AppWidge
localBalanceStr = null;
}

final MonetaryFormat btcFormat = config.getFormat();
final Spannable balanceStr = new MonetarySpannable(btcFormat.noCode(), balance).applyMarkup(null, null, MonetarySpannable.SMALLER_SPAN);
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wallet_balance_widget_content);

for (final int appWidgetId : appWidgetIds)
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);
}

private static Bundle getAppWidgetOptions(final AppWidgetManager appWidgetManager, final int appWidgetId)
{
try
{
final Method getAppWidgetOptions = AppWidgetManager.class.getMethod("getAppWidgetOptions", Integer.TYPE);
final Bundle options = (Bundle) getAppWidgetOptions.invoke(appWidgetManager, appWidgetId);
return options;
}
catch (final Exception x)
{
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);

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);
return null;
}
}
}

0 comments on commit 6db0d19

Please sign in to comment.