Merge pull request #2709 from brad/fix-2359
Display cover art for media in the widget
This commit is contained in:
commit
8380c18543
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 8.0 KiB After Width: | Height: | Size: 19 KiB |
@ -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" >
|
||||
android:gravity="fill_horizontal"
|
||||
android:orientation="horizontal" >
|
||||
|
||||
<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" />
|
||||
<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" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/txtvProgress"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="8dp"
|
||||
android:textColor="@color/white" />
|
||||
<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>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user