[Android] Make AppWidgetProvider load covers itself

Geoffrey Métais git at videolan.org
Thu Jul 6 12:40:03 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jul  6 12:39:07 2017 +0200| [376cd51618578d64b6c2376da1f1510ef50e6580] | committer: Geoffrey Métais

Make AppWidgetProvider load covers itself

> https://code.videolan.org/videolan/vlc-android/commit/376cd51618578d64b6c2376da1f1510ef50e6580
---

 .../src/org/videolan/vlc/PlaybackService.java      | 13 +-----
 .../videolan/vlc/widget/VLCAppWidgetProvider.java  | 53 ++++++++++++++++------
 2 files changed, 40 insertions(+), 26 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index 79e6d1ce7..6e1816190 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -1492,17 +1492,8 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
     }
 
     private void updateWidgetCover() {
-        if (!hasCurrentMedia())
-            return;
-        final String artworkMrl = getCurrentMedia().getArtworkMrl();
-        VLCApplication.runBackground(new Runnable() {
-            @Override
-            public void run() {
-                Bitmap cover = hasCurrentMedia()? AudioUtil.readCoverBitmap(Uri.decode(artworkMrl), 320) : null;
-                sendBroadcast(new Intent(VLCAppWidgetProvider.ACTION_WIDGET_UPDATE_COVER)
-                        .putExtra("cover", cover));
-            }
-        });
+        sendBroadcast(new Intent(VLCAppWidgetProvider.ACTION_WIDGET_UPDATE_COVER)
+                        .putExtra("artworkMrl", hasCurrentMedia() ? getCurrentMedia().getArtworkMrl() : null));
     }
 
     private void updateWidgetPosition(final float pos) {
diff --git a/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java b/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java
index 70d749746..e052b79d3 100644
--- a/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java
+++ b/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.java
@@ -28,6 +28,7 @@ import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
+import android.net.Uri;
 import android.os.Build;
 import android.text.TextUtils;
 import android.view.View;
@@ -38,6 +39,7 @@ import org.videolan.vlc.BuildConfig;
 import org.videolan.vlc.R;
 import org.videolan.vlc.StartActivity;
 import org.videolan.vlc.VLCApplication;
+import org.videolan.vlc.gui.helpers.AudioUtil;
 import org.videolan.vlc.util.Strings;
 
 import java.util.Locale;
@@ -53,6 +55,7 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
     public static final String ACTION_WIDGET_UPDATE = ACTION_WIDGET_PREFIX+"UPDATE";
     public static final String ACTION_WIDGET_UPDATE_COVER = ACTION_WIDGET_PREFIX+"UPDATE_COVER";
     public static final String ACTION_WIDGET_UPDATE_POSITION = ACTION_WIDGET_PREFIX+"UPDATE_POSITION";
+    private static String sCurrentArtworkMrl;
 
     @Override
     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
@@ -70,17 +73,17 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
 
     @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
     @Override
-    public void onReceive(Context context, Intent intent) {
+    public void onReceive(final Context context, final Intent intent) {
         String action = intent.getAction();
         if (!action.startsWith(ACTION_WIDGET_PREFIX)) {
             super.onReceive(context, intent);
             return;
         }
 
-        RemoteViews views = new RemoteViews(BuildConfig.APPLICATION_ID, getLayout());
-        boolean partial = AndroidUtil.isHoneycombOrLater;
+        final RemoteViews views = new RemoteViews(BuildConfig.APPLICATION_ID, getLayout());
+        final boolean partial = !ACTION_WIDGET_INIT.equals(action) && AndroidUtil.isHoneycombOrLater;
 
-        if (ACTION_WIDGET_INIT.equals(action) || !partial) {
+        if (!partial) {
             /* commands */
             Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD);
             Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE);
@@ -99,7 +102,6 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
             views.setOnClickPendingIntent(R.id.stop, piStop);
             views.setOnClickPendingIntent(R.id.forward, piForward);
             views.setOnClickPendingIntent(R.id.cover, piVlc);
-            partial = false;
             if (AndroidUtil.isJellyBeanMR1OrLater && TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_RTL) {
                 boolean black = this instanceof VLCAppWidgetProviderBlack;
                 views.setImageViewResource(R.id.forward, black ? R.drawable.ic_widget_previous_w : R.drawable.ic_widget_previous);
@@ -115,20 +117,41 @@ abstract public class VLCAppWidgetProvider extends AppWidgetProvider {
             views.setTextViewText(R.id.songName, title);
             views.setTextViewText(R.id.artist, artist);
             views.setImageViewResource(R.id.play_pause, getPlayPauseImage(isplaying));
-        }
-        else if (ACTION_WIDGET_UPDATE_COVER.equals(action)) {
-            Bitmap cover = intent.getParcelableExtra("cover");
-            if (cover != null)
-                views.setImageViewBitmap(R.id.cover, cover);
-            else
-                views.setImageViewResource(R.id.cover, R.drawable.icon);
-            views.setProgressBar(R.id.timeline, 100, 0, false);
-        }
-        else if (ACTION_WIDGET_UPDATE_POSITION.equals(action)) {
+        } else if (ACTION_WIDGET_UPDATE_COVER.equals(action)) {
+            final String artworkMrl = intent.getStringExtra("artworkMrl");
+            if (!TextUtils.equals(sCurrentArtworkMrl, artworkMrl)) {
+                sCurrentArtworkMrl = ""+artworkMrl;
+                if (!TextUtils.isEmpty(artworkMrl)) {
+                    VLCApplication.runBackground(new Runnable() {
+                        @Override
+                        public void run() {
+                            final Bitmap cover = AudioUtil.readCoverBitmap(Uri.decode(artworkMrl), 320);
+                            VLCApplication.runOnMainThread(new Runnable() {
+                                @Override
+                                public void run() {
+                                    if (cover != null)
+                                        views.setImageViewBitmap(R.id.cover, cover);
+                                    else
+                                        views.setImageViewResource(R.id.cover, R.drawable.icon);
+                                    views.setProgressBar(R.id.timeline, 100, 0, false);
+                                    applyUpdate(context, views, partial);
+                                }
+                            });
+                        }
+                    });
+                } else
+                    views.setImageViewResource(R.id.cover, R.drawable.icon);
+                views.setProgressBar(R.id.timeline, 100, 0, false);
+            }
+        } else if (ACTION_WIDGET_UPDATE_POSITION.equals(action)) {
             float pos = intent.getFloatExtra("position", 0f);
             views.setProgressBar(R.id.timeline, 100, (int) (100 * pos), false);
         }
 
+        applyUpdate(context, views, partial);
+    }
+
+    private void applyUpdate(Context context, RemoteViews views, boolean partial) {
         ComponentName widget = new ComponentName(context, this.getClass());
         AppWidgetManager manager = AppWidgetManager.getInstance(context);
         if (partial)



More information about the Android mailing list