From 9d4ea9911f37d5c8e044cae199ff89d8afd63564 Mon Sep 17 00:00:00 2001 From: Karim Abou Zeid Date: Sat, 11 Jul 2015 00:34:25 +0200 Subject: [PATCH] Improve image loading in the notification, the widget and the lockscreen album art by moving stuff to worker thread and adjusting the bitmap size which should also prevent the to large bitmap exception currently occurring in the widget for some devices. --- .../gramophone/appwidget/WidgetMedium.java | 55 +++++++++-------- .../helper/PlayingNotificationHelper.java | 37 +++++++----- .../gramophone/service/MusicService.java | 59 +++++++++++-------- .../layout/notification_controller_big.xml | 4 +- .../notification_controller_big_colored.xml | 4 +- app/src/main/res/layout/widget_medium.xml | 4 +- app/src/main/res/values/dimens.xml | 12 +--- 7 files changed, 97 insertions(+), 78 deletions(-) diff --git a/app/src/main/java/com/kabouzeid/gramophone/appwidget/WidgetMedium.java b/app/src/main/java/com/kabouzeid/gramophone/appwidget/WidgetMedium.java index 26f14166..567129f6 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/appwidget/WidgetMedium.java +++ b/app/src/main/java/com/kabouzeid/gramophone/appwidget/WidgetMedium.java @@ -18,12 +18,14 @@ import com.kabouzeid.gramophone.model.Song; import com.kabouzeid.gramophone.service.MusicService; import com.kabouzeid.gramophone.ui.activities.MainActivity; import com.kabouzeid.gramophone.util.MusicUtil; +import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.FailReason; import com.nostra13.universalimageloader.core.assist.ImageSize; import com.nostra13.universalimageloader.core.assist.ViewScaleType; import com.nostra13.universalimageloader.core.imageaware.NonViewAware; import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener; +import com.nostra13.universalimageloader.core.process.BitmapProcessor; public class WidgetMedium extends AppWidgetProvider { private static RemoteViews widgetLayout; @@ -66,33 +68,40 @@ public class WidgetMedium extends AppWidgetProvider { private static void loadAlbumArt(@NonNull final Context context, @Nullable final Song song) { if (song != null) { + int widgetImageSize = context.getResources().getDimensionPixelSize(R.dimen.widget_medium_image_size); currentAlbumArtUri = MusicUtil.getSongImageLoaderString(song); - ImageLoader.getInstance().displayImage(currentAlbumArtUri, new NonViewAware(new ImageSize(-1, -1), ViewScaleType.CROP), new SimpleImageLoadingListener() { - @Override - public void onLoadingComplete(String imageUri, View view, @Nullable Bitmap loadedImage) { - 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; + ImageLoader.getInstance().displayImage( + currentAlbumArtUri, + new NonViewAware(new ImageSize(widgetImageSize, widgetImageSize), ViewScaleType.CROP), + new DisplayImageOptions.Builder() + .postProcessor(new BitmapProcessor() { + @Override + public Bitmap process(Bitmap bitmap) { + // 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 = bitmap.getConfig(); + if (config == null) { + config = Bitmap.Config.ARGB_8888; + } + bitmap = bitmap.copy(config, false); + return bitmap.copy(bitmap.getConfig(), true); + } + }).build(), + new SimpleImageLoadingListener() { + @Override + public void onLoadingComplete(String imageUri, View view, @Nullable Bitmap loadedImage) { + if (currentAlbumArtUri.equals(imageUri)) { + setAlbumArt(context, loadedImage); } - 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)) { - setAlbumArt(context, null); - } - } - }); + @Override + public void onLoadingFailed(String imageUri, View view, FailReason failReason) { + if (currentAlbumArtUri.equals(imageUri)) { + setAlbumArt(context, null); + } + } + }); } } diff --git a/app/src/main/java/com/kabouzeid/gramophone/helper/PlayingNotificationHelper.java b/app/src/main/java/com/kabouzeid/gramophone/helper/PlayingNotificationHelper.java index 887f5844..f3d7b189 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/helper/PlayingNotificationHelper.java +++ b/app/src/main/java/com/kabouzeid/gramophone/helper/PlayingNotificationHelper.java @@ -46,7 +46,7 @@ public class PlayingNotificationHelper { @NonNull private final NotificationManager notificationManager; @Nullable - private Notification notification = null; + private Notification notification; private RemoteViews notificationLayout; private RemoteViews notificationLayoutExpanded; @@ -59,6 +59,8 @@ public class PlayingNotificationHelper { private boolean isReceiverRegistered; private boolean isNotificationShown; + private int bigNotificationImageSize; + @NonNull final IntentFilter intentFilter; @@ -69,6 +71,8 @@ public class PlayingNotificationHelper { intentFilter = new IntentFilter(); intentFilter.addAction(ACTION_NOTIFICATION_COLOR_PREFERENCE_CHANGED); + + bigNotificationImageSize = service.getResources().getDimensionPixelSize(R.dimen.notification_big_image_size); } @NonNull @@ -211,19 +215,22 @@ public class PlayingNotificationHelper { private void loadAlbumArt() { currentAlbumArtUri = MusicUtil.getSongImageLoaderString(currentSong); - 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)) - setAlbumArt(loadedImage); - } + ImageLoader.getInstance().displayImage( + currentAlbumArtUri, + new NonViewAware(new ImageSize(bigNotificationImageSize, bigNotificationImageSize), ViewScaleType.CROP), + new SimpleImageLoadingListener() { + @Override + public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { + if (currentAlbumArtUri.equals(imageUri)) + setAlbumArt(loadedImage); + } - @Override - public void onLoadingFailed(String imageUri, View view, FailReason failReason) { - if (currentAlbumArtUri.equals(imageUri)) - setAlbumArt(null); - } - }); + @Override + public void onLoadingFailed(String imageUri, View view, FailReason failReason) { + if (currentAlbumArtUri.equals(imageUri)) + setAlbumArt(null); + } + }); } private void setAlbumArt(@Nullable Bitmap albumArt) { @@ -244,7 +251,9 @@ public class PlayingNotificationHelper { } } - notificationManager.notify(NOTIFICATION_ID, notification); + if (notification != null) { + notificationManager.notify(NOTIFICATION_ID, notification); + } } private void setBackgroundColor(int color) { 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 5d299252..5436e96c 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java +++ b/app/src/main/java/com/kabouzeid/gramophone/service/MusicService.java @@ -38,12 +38,14 @@ import com.kabouzeid.gramophone.provider.RecentlyPlayedStore; import com.kabouzeid.gramophone.provider.SongPlayCountStore; import com.kabouzeid.gramophone.util.MusicUtil; import com.kabouzeid.gramophone.util.PreferenceUtils; +import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.FailReason; import com.nostra13.universalimageloader.core.assist.ImageSize; import com.nostra13.universalimageloader.core.assist.ViewScaleType; import com.nostra13.universalimageloader.core.imageaware.NonViewAware; import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener; +import com.nostra13.universalimageloader.core.process.BitmapProcessor; import java.lang.ref.WeakReference; import java.util.ArrayList; @@ -438,33 +440,38 @@ public class MusicService extends Service { .apply(); if (showAlbumArt) { final String currentAlbumArtUri = MusicUtil.getSongImageLoaderString(song); - ImageLoader.getInstance().displayImage(currentAlbumArtUri, new NonViewAware(new ImageSize(-1, -1), ViewScaleType.CROP), new SimpleImageLoadingListener() { - @Override - public void onLoadingComplete(String imageUri, View view, @Nullable Bitmap loadedImage) { - if (!currentAlbumArtUri.equals(imageUri)) { - return; - } - if (loadedImage == null) { - onLoadingFailed(imageUri, view, null); - return; - } - // 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 = loadedImage.getConfig(); - if (config == null) { - config = Bitmap.Config.ARGB_8888; - } - loadedImage = loadedImage.copy(config, false); - updateRemoteControlClientBitmap(loadedImage.copy(loadedImage.getConfig(), true)); - } + ImageLoader.getInstance().displayImage( + currentAlbumArtUri, + new NonViewAware(new ImageSize(-1, -1), ViewScaleType.CROP), + new DisplayImageOptions.Builder() + .postProcessor(new BitmapProcessor() { + @Override + public Bitmap process(Bitmap bitmap) { + // 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 = bitmap.getConfig(); + if (config == null) { + config = Bitmap.Config.ARGB_8888; + } + bitmap = bitmap.copy(config, false); + return bitmap.copy(bitmap.getConfig(), true); + } + }).build(), + new SimpleImageLoadingListener() { + @Override + public void onLoadingComplete(String imageUri, View view, @Nullable Bitmap loadedImage) { + if (currentAlbumArtUri.equals(imageUri)) { + updateRemoteControlClientBitmap(loadedImage); + } + } - @Override - public void onLoadingFailed(String imageUri, View view, FailReason failReason) { - if (currentAlbumArtUri.equals(imageUri)) { - updateRemoteControlClientBitmap(null); - } - } - }); + @Override + public void onLoadingFailed(String imageUri, View view, FailReason failReason) { + if (currentAlbumArtUri.equals(imageUri)) { + updateRemoteControlClientBitmap(null); + } + } + }); } else { updateRemoteControlClientBitmap(null); } diff --git a/app/src/main/res/layout/notification_controller_big.xml b/app/src/main/res/layout/notification_controller_big.xml index 0e28f98c..fdbaaeb3 100644 --- a/app/src/main/res/layout/notification_controller_big.xml +++ b/app/src/main/res/layout/notification_controller_big.xml @@ -23,8 +23,8 @@ diff --git a/app/src/main/res/layout/notification_controller_big_colored.xml b/app/src/main/res/layout/notification_controller_big_colored.xml index 854efffc..bfd9a571 100644 --- a/app/src/main/res/layout/notification_controller_big_colored.xml +++ b/app/src/main/res/layout/notification_controller_big_colored.xml @@ -24,8 +24,8 @@ diff --git a/app/src/main/res/layout/widget_medium.xml b/app/src/main/res/layout/widget_medium.xml index 27b2d59b..c8f1a15d 100644 --- a/app/src/main/res/layout/widget_medium.xml +++ b/app/src/main/res/layout/widget_medium.xml @@ -7,8 +7,8 @@ diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 18702535..41d947c8 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -17,12 +17,6 @@ 0dp - - 64.0dip - 64.0dip - 8.0dip - 4.0dip - 48dp 100dp @@ -31,8 +25,6 @@ Refer to App Widget Documentation for margin information http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout --> - 64dp - 5dp 4dp 14dp @@ -48,7 +40,9 @@ http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout -17dp 2dp - 128dp + 96dp + 128dp + 86dp 8dp