diff options
author | H. Lehmann <ByteHamster@users.noreply.github.com> | 2019-05-28 16:29:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-28 16:29:43 +0200 |
commit | 8380c1854333c86d8677ad0197e42762b80d6c18 (patch) | |
tree | 7f1014ba44fc1624554a46071761469e4f1a755b | |
parent | d81cb2ff398229024f71ca719ba2fbb708899fcb (diff) | |
parent | 88854ea6ba68354e55686f6ad98e09384e6acab3 (diff) | |
download | AntennaPod-8380c1854333c86d8677ad0197e42762b80d6c18.zip |
Merge pull request #2709 from brad/fix-2359
Display cover art for media in the widget
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/service/PlayerWidgetJobService.java | 63 | ||||
-rw-r--r-- | core/src/main/res/drawable-hdpi/ic_widget_preview.png | bin | 8210 -> 19686 bytes | |||
-rw-r--r-- | core/src/main/res/layout/player_widget.xml | 71 |
3 files changed, 109 insertions, 25 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/PlayerWidgetJobService.java b/core/src/main/java/de/danoeh/antennapod/core/service/PlayerWidgetJobService.java index 773a1f6e1..1165d689a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/PlayerWidgetJobService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/PlayerWidgetJobService.java @@ -6,7 +6,9 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.graphics.Bitmap; import android.os.Build; +import android.os.Bundle; import android.os.IBinder; import android.support.annotation.NonNull; import android.support.v4.app.SafeJobIntentService; @@ -15,7 +17,13 @@ import android.view.KeyEvent; import android.view.View; import android.widget.RemoteViews; +import com.bumptech.glide.Glide; +import com.bumptech.glide.request.RequestOptions; + +import java.util.concurrent.TimeUnit; + import de.danoeh.antennapod.core.R; +import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.receiver.MediaButtonReceiver; import de.danoeh.antennapod.core.receiver.PlayerWidget; import de.danoeh.antennapod.core.service.playback.PlaybackService; @@ -70,10 +78,25 @@ public class PlayerWidgetJobService extends SafeJobIntentService { } } + /** + * Returns number of cells needed for given size of the widget. + * + * @param size Widget size in dp. + * @return Size in number of cells. + */ + private static int getCellsForSize(int size) { + int n = 2; + while (70 * n - 30 < size) { + ++n; + } + return n - 1; + } + private void updateViews() { ComponentName playerWidget = new ComponentName(this, PlayerWidget.class); AppWidgetManager manager = AppWidgetManager.getInstance(this); + int[] widgetIds = manager.getAppWidgetIds(playerWidget); RemoteViews views = new RemoteViews(getPackageName(), R.layout.player_widget); PendingIntent startMediaplayer = PendingIntent.getActivity(this, 0, PlaybackService.getPlayerActivityIntent(this), 0); @@ -96,7 +119,24 @@ public class PlayerWidgetJobService extends SafeJobIntentService { if (media != null) { views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer); + try { + Bitmap icon = null; + int iconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size); + icon = Glide.with(PlayerWidgetJobService.this) + .asBitmap() + .load(media.getImageLocation()) + .apply(RequestOptions.diskCacheStrategyOf(ApGlideSettings.AP_DISK_CACHE_STRATEGY)) + .submit(iconSize, iconSize) + .get(500, TimeUnit.MILLISECONDS); + views.setImageViewBitmap(R.id.imgvCover, icon); + } catch (Throwable tr) { + Log.e(TAG, "Error loading the media icon for the widget", tr); + views.setImageViewResource(R.id.imgvCover, R.mipmap.ic_launcher_foreground); + } + views.setTextViewText(R.id.txtvTitle, media.getEpisodeTitle()); + views.setViewVisibility(R.id.txtvTitle, View.VISIBLE); + views.setViewVisibility(R.id.txtNoPlaying, View.GONE); String progressString; if (playbackService != null) { @@ -130,13 +170,28 @@ public class PlayerWidgetJobService extends SafeJobIntentService { // start the app if they click anything views.setOnClickPendingIntent(R.id.layout_left, startAppPending); views.setOnClickPendingIntent(R.id.butPlay, startAppPending); - views.setViewVisibility(R.id.txtvProgress, View.INVISIBLE); - views.setTextViewText(R.id.txtvTitle, - this.getString(R.string.no_media_playing_label)); + views.setViewVisibility(R.id.txtvProgress, View.GONE); + views.setViewVisibility(R.id.txtvTitle, View.GONE); + views.setViewVisibility(R.id.txtNoPlaying, View.VISIBLE); + views.setImageViewResource(R.id.imgvCover, R.mipmap.ic_launcher_foreground); views.setImageViewResource(R.id.butPlay, R.drawable.ic_play_arrow_white_24dp); } - manager.updateAppWidget(playerWidget, views); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { + for (int id : widgetIds) { + Bundle options = manager.getAppWidgetOptions(id); + int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH); + int columns = getCellsForSize(minWidth); + if (columns < 3) { + views.setViewVisibility(R.id.layout_center, View.INVISIBLE); + } else { + views.setViewVisibility(R.id.layout_center, View.VISIBLE); + } + manager.updateAppWidget(id, views); + } + } else { + manager.updateAppWidget(playerWidget, views); + } } /** diff --git a/core/src/main/res/drawable-hdpi/ic_widget_preview.png b/core/src/main/res/drawable-hdpi/ic_widget_preview.png Binary files differindex 85a537154..3c1e08a31 100644 --- a/core/src/main/res/drawable-hdpi/ic_widget_preview.png +++ b/core/src/main/res/drawable-hdpi/ic_widget_preview.png diff --git a/core/src/main/res/layout/player_widget.xml b/core/src/main/res/layout/player_widget.xml index 4c98895a0..daf661002 100644 --- a/core/src/main/res/layout/player_widget.xml +++ b/core/src/main/res/layout/player_widget.xml @@ -12,7 +12,7 @@ <ImageButton android:id="@+id/butPlay" android:contentDescription="@string/play_label" - android:layout_width="56dp" + android:layout_width="@android:dimen/app_icon_size" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="12dp" @@ -26,26 +26,55 @@ android:layout_alignParentLeft="true" android:layout_toLeftOf="@id/butPlay" android:background="@drawable/borderless_button_dark" - android:gravity="center_vertical" - android:orientation="vertical" > - - <TextView - android:id="@+id/txtvTitle" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_margin="8dp" - android:maxLines="1" - android:text="@string/no_media_playing_label" - android:textColor="@color/white" - android:textSize="@dimen/text_size_medium" - android:textStyle="bold" /> - - <TextView - android:id="@+id/txtvProgress" - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:layout_margin="8dp" - android:textColor="@color/white" /> + android:gravity="fill_horizontal" + android:orientation="horizontal" > + + <ImageView + android:id="@+id/imgvCover" + android:layout_width="@android:dimen/app_icon_size" + android:layout_height="match_parent" + android:layout_alignParentLeft="true" + android:layout_margin="12dp" + android:layout_toLeftOf="@id/layout_center" /> + + <LinearLayout + android:id="@+id/layout_center" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_alignParentRight="true" + android:gravity="center_vertical" + android:orientation="vertical" > + + <TextView + android:id="@+id/txtNoPlaying" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginVertical="12dp" + android:maxLines="3" + android:text="@string/no_media_playing_label" + android:textColor="@color/white" + android:textSize="@dimen/text_size_medium" + android:textStyle="bold" /> + + <TextView + android:id="@+id/txtvTitle" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginVertical="8dp" + android:maxLines="1" + android:textColor="@color/white" + android:textSize="@dimen/text_size_medium" + android:textStyle="bold" + android:visibility="gone" /> + + <TextView + android:id="@+id/txtvProgress" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginVertical="8dp" + android:textColor="@color/white" + android:visibility="gone" /> + </LinearLayout> </LinearLayout> </RelativeLayout> |