From a238d6564e9f71ab57e80c035a1bddd27dd26c68 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Sun, 5 Jul 2015 15:22:30 +0200 Subject: [PATCH] Clean ups and fixed a NPE in the widget. --- .../appwidget/MusicPlayerWidget.java | 20 +++++++++++++++---- .../gramophone/service/MusicService.java | 14 +++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/kabouzeid/gramophone/appwidget/MusicPlayerWidget.java b/app/src/main/java/com/kabouzeid/gramophone/appwidget/MusicPlayerWidget.java index 38db31c5..96c7f7d3 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/appwidget/MusicPlayerWidget.java +++ b/app/src/main/java/com/kabouzeid/gramophone/appwidget/MusicPlayerWidget.java @@ -67,15 +67,27 @@ public class MusicPlayerWidget extends AppWidgetProvider { ImageLoader.getInstance().displayImage(currentAlbumArtUri, new NonViewAware(new ImageSize(-1, -1), ViewScaleType.CROP), new SimpleImageLoadingListener() { @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { - if (currentAlbumArtUri.equals(imageUri)) - // copy() prevents the original bitmap in the memory cache from being recycled by the remote views - setAlbumArt(context, loadedImage.copy(loadedImage.getConfig(), true)); + if (currentAlbumArtUri.equals(imageUri)) { + if (loadedImage != null) { + // The RemoteViews might wants to recycle the bitmaps thrown at it, so we need + // to make sure not to hand out our cache copy + Bitmap.Config config = loadedImage.getConfig(); + if (config == null) { + config = Bitmap.Config.ARGB_8888; + } + loadedImage = loadedImage.copy(config, false); + setAlbumArt(context, loadedImage.copy(loadedImage.getConfig(), true)); + } else { + setAlbumArt(context, null); + } + } } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { - if (currentAlbumArtUri.equals(imageUri)) + if (currentAlbumArtUri.equals(imageUri)) { setAlbumArt(context, null); + } } }); } diff --git a/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java b/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java index 32b64f9c..03b857a4 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java +++ b/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java @@ -417,24 +417,26 @@ public class MusicService extends Service { @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { if (currentAlbumArtUri.equals(imageUri)) { - Bitmap albumArt = loadedImage; - if (albumArt != null) { + if (loadedImage != null) { // RemoteControlClient wants to recycle the bitmaps thrown at it, so we need // to make sure not to hand out our cache copy - Bitmap.Config config = albumArt.getConfig(); + Bitmap.Config config = loadedImage.getConfig(); if (config == null) { config = Bitmap.Config.ARGB_8888; } - albumArt = albumArt.copy(config, false); - updateRemoteControlClientBitmap(albumArt.copy(albumArt.getConfig(), true)); + loadedImage = loadedImage.copy(config, false); + updateRemoteControlClientBitmap(loadedImage.copy(loadedImage.getConfig(), true)); + } else { + updateRemoteControlClientBitmap(null); } } } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { - if (currentAlbumArtUri.equals(imageUri)) + if (currentAlbumArtUri.equals(imageUri)) { updateRemoteControlClientBitmap(null); + } } }); } else {