Permalink
Browse files

Ensures the example-contract-widget does not crash with high res images.

When the image is exactly square, using the DisplayMetrics largest size leads to too large of an image being passed in. Instead, we should use the widget's actual size to scale the image.
  • Loading branch information...
1 parent a1fb9a2 commit 57538717113d8e5c713c36ec1eadcef244ba1103 @ianhanniballake ianhanniballake committed Oct 18, 2016
@@ -23,9 +23,11 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
+import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
+import android.util.TypedValue;
import android.widget.RemoteViews;
import com.google.android.apps.muzei.api.Artwork;
@@ -64,10 +66,6 @@ protected void onHandleIntent(final Intent intent) {
Bitmap image;
try {
image = MuzeiContract.Artwork.getCurrentArtworkBitmap(this);
- // getCurrentArtworkBitmap is a naive method so we manually reduce the size based on the screen size
- // Consider using BitmapFactory.decodeStream(contentResolver.openInputStream(CONTENT_URI), options)
- // where options subsamples the image to the appropriate size if you don't need to full size image
- image = scaleBitmap(image);
} catch (FileNotFoundException e) {
Log.w(TAG, "Could not find current artwork image", e);
return;
@@ -81,18 +79,28 @@ protected void onHandleIntent(final Intent intent) {
Intent launchIntent = packageManager.getLaunchIntentForPackage("net.nurik.roman.muzei");
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+ DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
for (int widgetId : appWidgetIds) {
+ Bundle extras = appWidgetManager.getAppWidgetOptions(widgetId);
+ int widgetWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
+ extras.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH), displayMetrics);
+ int widgetHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
+ extras.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT), displayMetrics);
+ // getCurrentArtworkBitmap is a naive method so we manually reduce the size based on the
+ // widget's actual size. Consider using
+ // BitmapFactory.decodeStream(contentResolver.openInputStream(CONTENT_URI), options)
+ // where options subsamples the image to the appropriate size
+ Bitmap scaledImage = scaleBitmap(image, widgetWidth, widgetHeight);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
remoteViews.setContentDescription(R.id.background, contentDescription);
- remoteViews.setImageViewBitmap(R.id.background, image);
+ remoteViews.setImageViewBitmap(R.id.background, scaledImage);
remoteViews.setOnClickPendingIntent(R.id.background, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
- private Bitmap scaleBitmap(Bitmap image) {
- DisplayMetrics metrics = getResources().getDisplayMetrics();
- int largestDimension = Math.max(metrics.widthPixels, metrics.heightPixels);
+ private Bitmap scaleBitmap(Bitmap image, int widgetWidth, int widgetHeight) {
+ int largestDimension = Math.max(widgetWidth, widgetHeight);
int width = image.getWidth();
int height = image.getHeight();
if (width > height) {

0 comments on commit 5753871

Please sign in to comment.