|
|
|
|
@ -8,6 +8,7 @@ import android.graphics.Bitmap; |
|
|
|
|
import android.os.Build; |
|
|
|
|
import android.os.IBinder; |
|
|
|
|
import android.support.v4.media.MediaMetadataCompat; |
|
|
|
|
import android.util.LruCache; |
|
|
|
|
|
|
|
|
|
import androidx.annotation.DrawableRes; |
|
|
|
|
import androidx.annotation.Nullable; |
|
|
|
|
@ -18,6 +19,7 @@ import androidx.core.app.ServiceCompat; |
|
|
|
|
import androidx.core.content.ContextCompat; |
|
|
|
|
import androidx.media.app.NotificationCompat.MediaStyle; |
|
|
|
|
import androidx.media.session.MediaButtonReceiver; |
|
|
|
|
import androidx.palette.graphics.Palette; |
|
|
|
|
|
|
|
|
|
import com.bumptech.glide.Glide; |
|
|
|
|
import com.fongmi.android.tv.App; |
|
|
|
|
@ -32,13 +34,12 @@ import org.greenrobot.eventbus.EventBus; |
|
|
|
|
import org.greenrobot.eventbus.Subscribe; |
|
|
|
|
import org.greenrobot.eventbus.ThreadMode; |
|
|
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
|
|
public class PlaybackService extends Service { |
|
|
|
|
|
|
|
|
|
private Map<String, Bitmap> cache; |
|
|
|
|
private static final int MAX_CACHE_SIZE_BYTES = 10 * 1024 * 1024; |
|
|
|
|
private LruCache<String, Bitmap> cache; |
|
|
|
|
private static Players player; |
|
|
|
|
|
|
|
|
|
public static void start(Players player) { |
|
|
|
|
@ -87,15 +88,6 @@ public class PlaybackService extends Service { |
|
|
|
|
return getMetadata() == null ? "" : getMetadata().getString(MediaMetadataCompat.METADATA_KEY_ART_URI); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setLargeIcon(NotificationCompat.Builder builder, Bitmap art) { |
|
|
|
|
Bitmap b1 = Bitmap.createScaledBitmap(art, 16, 16, true); |
|
|
|
|
Bitmap b2 = Bitmap.createScaledBitmap(b1, 1, 1, true); |
|
|
|
|
builder.setColor(b2.getPixel(0, 0)); |
|
|
|
|
builder.setLargeIcon(art); |
|
|
|
|
b2.recycle(); |
|
|
|
|
b1.recycle(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void addAction(NotificationCompat.Builder builder) { |
|
|
|
|
builder.addAction(buildNotificationAction(androidx.media3.ui.R.drawable.exo_icon_previous, androidx.media3.ui.R.string.exo_controls_previous_description, ActionEvent.PREV)); |
|
|
|
|
builder.addAction(getPlayPauseAction()); |
|
|
|
|
@ -120,8 +112,9 @@ public class PlaybackService extends Service { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setArtwork(NotificationCompat.Builder builder) { |
|
|
|
|
if (cache.containsKey(getArtUri())) { |
|
|
|
|
setLargeIcon(builder, cache.get(getArtUri())); |
|
|
|
|
Bitmap bitmap = cache.get(getArtUri()); |
|
|
|
|
if (bitmap != null) { |
|
|
|
|
setLargeIcon(builder, bitmap); |
|
|
|
|
} else if (!getArtUri().isEmpty()) { |
|
|
|
|
App.execute(() -> glide(builder)); |
|
|
|
|
} |
|
|
|
|
@ -129,14 +122,24 @@ public class PlaybackService extends Service { |
|
|
|
|
|
|
|
|
|
private void glide(NotificationCompat.Builder builder) { |
|
|
|
|
try { |
|
|
|
|
cache.put(getArtUri(), Glide.with(this).asBitmap().load(ImgUtil.getUrl(getArtUri())).error(R.drawable.artwork).submit().get()); |
|
|
|
|
setLargeIcon(builder, cache.get(getArtUri())); |
|
|
|
|
Notify.show(builder.build()); |
|
|
|
|
Bitmap bitmap = Glide.with(this).asBitmap().load(ImgUtil.getUrl(getArtUri())).error(R.drawable.artwork).override(256, 256).submit().get(); |
|
|
|
|
cache.put(getArtUri(), bitmap); |
|
|
|
|
setLargeIcon(builder, bitmap); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setLargeIcon(NotificationCompat.Builder builder, Bitmap art) { |
|
|
|
|
builder.setLargeIcon(art); |
|
|
|
|
Palette.from(art).generate(palette -> { |
|
|
|
|
int white = ContextCompat.getColor(this, R.color.white); |
|
|
|
|
int color = palette != null ? palette.getMutedColor(palette.getVibrantColor(white)) : white; |
|
|
|
|
builder.setColor(color); |
|
|
|
|
Notify.show(builder.build()); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN) |
|
|
|
|
public void onActionEvent(ActionEvent event) { |
|
|
|
|
if (event.isUpdate()) Notify.show(buildNotification()); |
|
|
|
|
@ -145,8 +148,13 @@ public class PlaybackService extends Service { |
|
|
|
|
@Override |
|
|
|
|
public void onCreate() { |
|
|
|
|
super.onCreate(); |
|
|
|
|
cache = new HashMap<>(); |
|
|
|
|
EventBus.getDefault().register(this); |
|
|
|
|
cache = new LruCache<>(MAX_CACHE_SIZE_BYTES) { |
|
|
|
|
@Override |
|
|
|
|
protected int sizeOf(String key, Bitmap bitmap) { |
|
|
|
|
return bitmap.getByteCount(); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|