Clean ups and fixed a NPE in the widget.

This commit is contained in:
Karim Abou Zeid 2015-07-05 15:22:30 +02:00
commit a238d6564e
2 changed files with 24 additions and 10 deletions

View file

@ -67,16 +67,28 @@ public class MusicPlayerWidget extends AppWidgetProvider {
ImageLoader.getInstance().displayImage(currentAlbumArtUri, new NonViewAware(new ImageSize(-1, -1), ViewScaleType.CROP), new SimpleImageLoadingListener() { ImageLoader.getInstance().displayImage(currentAlbumArtUri, new NonViewAware(new ImageSize(-1, -1), ViewScaleType.CROP), new SimpleImageLoadingListener() {
@Override @Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (currentAlbumArtUri.equals(imageUri)) if (currentAlbumArtUri.equals(imageUri)) {
// copy() prevents the original bitmap in the memory cache from being recycled by the remote views 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)); setAlbumArt(context, loadedImage.copy(loadedImage.getConfig(), true));
} else {
setAlbumArt(context, null);
}
}
} }
@Override @Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) { public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
if (currentAlbumArtUri.equals(imageUri)) if (currentAlbumArtUri.equals(imageUri)) {
setAlbumArt(context, null); setAlbumArt(context, null);
} }
}
}); });
} }
} }

View file

@ -417,25 +417,27 @@ public class MusicService extends Service {
@Override @Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (currentAlbumArtUri.equals(imageUri)) { if (currentAlbumArtUri.equals(imageUri)) {
Bitmap albumArt = loadedImage; if (loadedImage != null) {
if (albumArt != null) {
// RemoteControlClient wants to recycle the bitmaps thrown at it, so we need // RemoteControlClient wants to recycle the bitmaps thrown at it, so we need
// to make sure not to hand out our cache copy // to make sure not to hand out our cache copy
Bitmap.Config config = albumArt.getConfig(); Bitmap.Config config = loadedImage.getConfig();
if (config == null) { if (config == null) {
config = Bitmap.Config.ARGB_8888; config = Bitmap.Config.ARGB_8888;
} }
albumArt = albumArt.copy(config, false); loadedImage = loadedImage.copy(config, false);
updateRemoteControlClientBitmap(albumArt.copy(albumArt.getConfig(), true)); updateRemoteControlClientBitmap(loadedImage.copy(loadedImage.getConfig(), true));
} else {
updateRemoteControlClientBitmap(null);
} }
} }
} }
@Override @Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) { public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
if (currentAlbumArtUri.equals(imageUri)) if (currentAlbumArtUri.equals(imageUri)) {
updateRemoteControlClientBitmap(null); updateRemoteControlClientBitmap(null);
} }
}
}); });
} else { } else {
updateRemoteControlClientBitmap(null); updateRemoteControlClientBitmap(null);