Initial commit retro music app
This commit is contained in:
parent
ab332473bc
commit
fe890632fd
932 changed files with 83126 additions and 0 deletions
|
@ -0,0 +1,171 @@
|
|||
package code.name.monkey.retromusic.appwidgets;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.RemoteViews;
|
||||
import code.name.monkey.appthemehelper.util.MaterialValueHelper;
|
||||
import code.name.monkey.retromusic.Constants;
|
||||
import code.name.monkey.retromusic.R;
|
||||
import code.name.monkey.retromusic.appwidgets.base.BaseAppWidget;
|
||||
import code.name.monkey.retromusic.glide.SongGlideRequest;
|
||||
import code.name.monkey.retromusic.model.Song;
|
||||
import code.name.monkey.retromusic.service.MusicService;
|
||||
import code.name.monkey.retromusic.ui.activities.MainActivity;
|
||||
import code.name.monkey.retromusic.util.RetroUtil;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.animation.GlideAnimation;
|
||||
import com.bumptech.glide.request.target.SimpleTarget;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
|
||||
public class AppWidgetBig extends BaseAppWidget {
|
||||
|
||||
public static final String NAME = "app_widget_big";
|
||||
|
||||
private static AppWidgetBig mInstance;
|
||||
private Target<Bitmap> target; // for cancellation
|
||||
|
||||
public static synchronized AppWidgetBig getInstance() {
|
||||
if (mInstance == null) {
|
||||
mInstance = new AppWidgetBig();
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize given widgets to default state, where we launch Music on default click and hide
|
||||
* actions if service not running.
|
||||
*/
|
||||
protected void defaultAppWidget(final Context context, final int[] appWidgetIds) {
|
||||
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(),
|
||||
R.layout.app_widget_big);
|
||||
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||
appWidgetView.setImageViewResource(R.id.image, R.drawable.default_album_art);
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_next_white_24dp,
|
||||
MaterialValueHelper.getPrimaryTextColor(context, false)), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_previous_white_24dp,
|
||||
MaterialValueHelper.getPrimaryTextColor(context, false)), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_play_arrow_white_24dp,
|
||||
MaterialValueHelper.getPrimaryTextColor(context, false)), 1f));
|
||||
|
||||
linkButtons(context, appWidgetView);
|
||||
pushUpdate(context, appWidgetIds, appWidgetView);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update all active widget instances by pushing changes
|
||||
*/
|
||||
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
|
||||
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(),
|
||||
R.layout.app_widget_big);
|
||||
|
||||
final boolean isPlaying = service.isPlaying();
|
||||
final Song song = service.getCurrentSong();
|
||||
|
||||
// Set the titles and artwork
|
||||
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||
} else {
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE);
|
||||
appWidgetView.setTextViewText(R.id.title, song.title);
|
||||
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
|
||||
}
|
||||
|
||||
// Set correct drawable for pause state
|
||||
int playPauseRes =
|
||||
isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp;
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, playPauseRes,
|
||||
MaterialValueHelper.getPrimaryTextColor(service, false)), 1f));
|
||||
|
||||
// Set prev/next button drawables
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp,
|
||||
MaterialValueHelper.getPrimaryTextColor(service, false)), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp,
|
||||
MaterialValueHelper.getPrimaryTextColor(service, false)), 1f));
|
||||
|
||||
// Link actions buttons to intents
|
||||
linkButtons(service, appWidgetView);
|
||||
|
||||
// Load the album cover async and push the update on completion
|
||||
Point p = RetroUtil.getScreenSize(service);
|
||||
final int widgetImageSize = Math.min(p.x, p.y);
|
||||
final Context appContext = service.getApplicationContext();
|
||||
service.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (target != null) {
|
||||
Glide.clear(target);
|
||||
}
|
||||
target = SongGlideRequest.Builder.from(Glide.with(appContext), song)
|
||||
.checkIgnoreMediaStore(appContext)
|
||||
.asBitmap().build()
|
||||
.into(new SimpleTarget<Bitmap>(widgetImageSize, widgetImageSize) {
|
||||
@Override
|
||||
public void onResourceReady(Bitmap resource,
|
||||
GlideAnimation<? super Bitmap> glideAnimation) {
|
||||
update(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFailed(Exception e, Drawable errorDrawable) {
|
||||
super.onLoadFailed(e, errorDrawable);
|
||||
update(null);
|
||||
}
|
||||
|
||||
private void update(@Nullable Bitmap bitmap) {
|
||||
if (bitmap == null) {
|
||||
appWidgetView.setImageViewResource(R.id.image, R.drawable.default_album_art);
|
||||
} else {
|
||||
appWidgetView.setImageViewBitmap(R.id.image, bitmap);
|
||||
}
|
||||
pushUpdate(appContext, appWidgetIds, appWidgetView);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Link up various button actions using {@link PendingIntent}.
|
||||
*/
|
||||
private void linkButtons(final Context context, final RemoteViews views) {
|
||||
Intent action;
|
||||
PendingIntent pendingIntent;
|
||||
|
||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||
|
||||
// Home
|
||||
action = new Intent(context, MainActivity.class);
|
||||
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
|
||||
views.setOnClickPendingIntent(R.id.clickable_area, pendingIntent);
|
||||
|
||||
// Previous track
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_REWIND, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||
|
||||
// Play and pause
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_TOGGLE_PAUSE, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||
|
||||
// Next track
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_SKIP, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,194 @@
|
|||
package code.name.monkey.retromusic.appwidgets;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.graphics.Palette;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.RemoteViews;
|
||||
import code.name.monkey.appthemehelper.util.MaterialValueHelper;
|
||||
import code.name.monkey.retromusic.Constants;
|
||||
import code.name.monkey.retromusic.R;
|
||||
import code.name.monkey.retromusic.appwidgets.base.BaseAppWidget;
|
||||
import code.name.monkey.retromusic.glide.SongGlideRequest;
|
||||
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper;
|
||||
import code.name.monkey.retromusic.model.Song;
|
||||
import code.name.monkey.retromusic.service.MusicService;
|
||||
import code.name.monkey.retromusic.ui.activities.MainActivity;
|
||||
import code.name.monkey.retromusic.util.RetroUtil;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.animation.GlideAnimation;
|
||||
import com.bumptech.glide.request.target.SimpleTarget;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
|
||||
public class AppWidgetCard extends BaseAppWidget {
|
||||
|
||||
public static final String NAME = "app_widget_card";
|
||||
|
||||
private static AppWidgetCard mInstance;
|
||||
private static int imageSize = 0;
|
||||
private static float cardRadius = 0f;
|
||||
private Target<BitmapPaletteWrapper> target; // for cancellation
|
||||
|
||||
public static synchronized AppWidgetCard getInstance() {
|
||||
if (mInstance == null) {
|
||||
mInstance = new AppWidgetCard();
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize given widgets to default state, where we launch Music on default click and hide
|
||||
* actions if service not running.
|
||||
*/
|
||||
protected void defaultAppWidget(final Context context, final int[] appWidgetIds) {
|
||||
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(),
|
||||
R.layout.app_widget_card);
|
||||
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||
appWidgetView.setImageViewResource(R.id.image, R.drawable.default_album_art);
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_next_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_previous_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_play_arrow_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
|
||||
|
||||
linkButtons(context, appWidgetView);
|
||||
pushUpdate(context, appWidgetIds, appWidgetView);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update all active widget instances by pushing changes
|
||||
*/
|
||||
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
|
||||
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(),
|
||||
R.layout.app_widget_card);
|
||||
|
||||
final boolean isPlaying = service.isPlaying();
|
||||
final Song song = service.getCurrentSong();
|
||||
|
||||
// Set the titles and artwork
|
||||
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||
} else {
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE);
|
||||
appWidgetView.setTextViewText(R.id.title, song.title);
|
||||
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
|
||||
}
|
||||
|
||||
// Set correct drawable for pause state
|
||||
int playPauseRes =
|
||||
isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp;
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, playPauseRes,
|
||||
MaterialValueHelper.getSecondaryTextColor(service, true)), 1f));
|
||||
|
||||
// Set prev/next button drawables
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(service, true)), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(service, true)), 1f));
|
||||
|
||||
// Link actions buttons to intents
|
||||
linkButtons(service, appWidgetView);
|
||||
|
||||
if (imageSize == 0) {
|
||||
imageSize = service.getResources().getDimensionPixelSize(R.dimen.app_widget_card_image_size);
|
||||
}
|
||||
if (cardRadius == 0f) {
|
||||
cardRadius = service.getResources().getDimension(R.dimen.app_widget_card_radius);
|
||||
}
|
||||
|
||||
// Load the album cover async and push the update on completion
|
||||
service.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (target != null) {
|
||||
Glide.clear(target);
|
||||
}
|
||||
target = SongGlideRequest.Builder.from(Glide.with(service), song)
|
||||
.checkIgnoreMediaStore(service)
|
||||
.generatePalette(service).build()
|
||||
.centerCrop()
|
||||
.into(new SimpleTarget<BitmapPaletteWrapper>(imageSize, imageSize) {
|
||||
@Override
|
||||
public void onResourceReady(BitmapPaletteWrapper resource,
|
||||
GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
|
||||
Palette palette = resource.getPalette();
|
||||
update(resource.getBitmap(), palette.getVibrantColor(palette
|
||||
.getMutedColor(MaterialValueHelper.getSecondaryTextColor(service, true))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFailed(Exception e, Drawable errorDrawable) {
|
||||
super.onLoadFailed(e, errorDrawable);
|
||||
update(null, MaterialValueHelper.getSecondaryTextColor(service, true));
|
||||
}
|
||||
|
||||
private void update(@Nullable Bitmap bitmap, int color) {
|
||||
// Set correct drawable for pause state
|
||||
int playPauseRes = isPlaying ? R.drawable.ic_pause_white_24dp
|
||||
: R.drawable.ic_play_arrow_white_24dp;
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause,
|
||||
createBitmap(RetroUtil.getTintedVectorDrawable(service, playPauseRes, color), 1f));
|
||||
|
||||
// Set prev/next button drawables
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp,
|
||||
color), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp,
|
||||
color), 1f));
|
||||
|
||||
final Drawable image = getAlbumArtDrawable(service.getResources(), bitmap);
|
||||
final Bitmap roundedBitmap = createRoundedBitmap(image, imageSize, imageSize,
|
||||
cardRadius, 0, cardRadius, 0);
|
||||
appWidgetView.setImageViewBitmap(R.id.image, roundedBitmap);
|
||||
|
||||
pushUpdate(service, appWidgetIds, appWidgetView);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Link up various button actions using {@link PendingIntent}.
|
||||
*/
|
||||
private void linkButtons(final Context context, final RemoteViews views) {
|
||||
Intent action;
|
||||
PendingIntent pendingIntent;
|
||||
|
||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||
|
||||
// Home
|
||||
action = new Intent(context, MainActivity.class);
|
||||
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
|
||||
views.setOnClickPendingIntent(R.id.image, pendingIntent);
|
||||
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent);
|
||||
|
||||
// Previous track
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_REWIND, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||
|
||||
// Play and pause
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_TOGGLE_PAUSE, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||
|
||||
// Next track
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_SKIP, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,181 @@
|
|||
package code.name.monkey.retromusic.appwidgets;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.graphics.Palette;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.RemoteViews;
|
||||
import code.name.monkey.appthemehelper.util.MaterialValueHelper;
|
||||
import code.name.monkey.retromusic.Constants;
|
||||
import code.name.monkey.retromusic.R;
|
||||
import code.name.monkey.retromusic.appwidgets.base.BaseAppWidget;
|
||||
import code.name.monkey.retromusic.glide.SongGlideRequest;
|
||||
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper;
|
||||
import code.name.monkey.retromusic.model.Song;
|
||||
import code.name.monkey.retromusic.service.MusicService;
|
||||
import code.name.monkey.retromusic.ui.activities.MainActivity;
|
||||
import code.name.monkey.retromusic.util.RetroUtil;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.animation.GlideAnimation;
|
||||
import com.bumptech.glide.request.target.SimpleTarget;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
|
||||
public class AppWidgetClassic extends BaseAppWidget {
|
||||
|
||||
public static final String NAME = "app_widget_classic";
|
||||
|
||||
private static AppWidgetClassic mInstance;
|
||||
private static int imageSize = 0;
|
||||
private static float cardRadius = 0f;
|
||||
private Target<BitmapPaletteWrapper> target; // for cancellation
|
||||
|
||||
public static synchronized AppWidgetClassic getInstance() {
|
||||
if (mInstance == null) {
|
||||
mInstance = new AppWidgetClassic();
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize given widgets to default state, where we launch Music on default click and hide
|
||||
* actions if service not running.
|
||||
*/
|
||||
protected void defaultAppWidget(final Context context, final int[] appWidgetIds) {
|
||||
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(),
|
||||
R.layout.app_widget_classic);
|
||||
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||
appWidgetView.setImageViewResource(R.id.image, R.drawable.default_album_art);
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_next_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_previous_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_play_arrow_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
|
||||
|
||||
linkButtons(context, appWidgetView);
|
||||
pushUpdate(context, appWidgetIds, appWidgetView);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update all active widget instances by pushing changes
|
||||
*/
|
||||
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
|
||||
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(),
|
||||
R.layout.app_widget_classic);
|
||||
|
||||
final boolean isPlaying = service.isPlaying();
|
||||
final Song song = service.getCurrentSong();
|
||||
|
||||
// Set the titles and artwork
|
||||
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||
} else {
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE);
|
||||
appWidgetView.setTextViewText(R.id.title, song.title);
|
||||
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
|
||||
}
|
||||
|
||||
// Link actions buttons to intents
|
||||
linkButtons(service, appWidgetView);
|
||||
|
||||
if (imageSize == 0) {
|
||||
imageSize = service.getResources()
|
||||
.getDimensionPixelSize(R.dimen.app_widget_classic_image_size);
|
||||
}
|
||||
if (cardRadius == 0f) {
|
||||
cardRadius = service.getResources().getDimension(R.dimen.app_widget_card_radius);
|
||||
}
|
||||
|
||||
// Load the album cover async and push the update on completion
|
||||
final Context appContext = service.getApplicationContext();
|
||||
service.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (target != null) {
|
||||
Glide.clear(target);
|
||||
}
|
||||
target = SongGlideRequest.Builder.from(Glide.with(appContext), song)
|
||||
.checkIgnoreMediaStore(appContext)
|
||||
.generatePalette(service).build()
|
||||
.centerCrop()
|
||||
.into(new SimpleTarget<BitmapPaletteWrapper>(imageSize, imageSize) {
|
||||
@Override
|
||||
public void onResourceReady(BitmapPaletteWrapper resource,
|
||||
GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
|
||||
Palette palette = resource.getPalette();
|
||||
update(resource.getBitmap(), palette.getVibrantColor(palette
|
||||
.getMutedColor(MaterialValueHelper.getSecondaryTextColor(appContext, true))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFailed(Exception e, Drawable errorDrawable) {
|
||||
super.onLoadFailed(e, errorDrawable);
|
||||
update(null, MaterialValueHelper.getSecondaryTextColor(appContext, true));
|
||||
}
|
||||
|
||||
private void update(@Nullable Bitmap bitmap, int color) {
|
||||
// Set correct drawable for pause state
|
||||
int playPauseRes = isPlaying ? R.drawable.ic_pause_white_24dp
|
||||
: R.drawable.ic_play_arrow_white_24dp;
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause,
|
||||
createBitmap(RetroUtil.getTintedVectorDrawable(service, playPauseRes, color), 1f));
|
||||
|
||||
// Set prev/next button drawables
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp,
|
||||
color), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp,
|
||||
color), 1f));
|
||||
|
||||
final Drawable image = getAlbumArtDrawable(service.getResources(), bitmap);
|
||||
final Bitmap roundedBitmap = createRoundedBitmap(image, imageSize, imageSize,
|
||||
cardRadius, 0, cardRadius, 0);
|
||||
appWidgetView.setImageViewBitmap(R.id.image, roundedBitmap);
|
||||
|
||||
pushUpdate(appContext, appWidgetIds, appWidgetView);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Link up various button actions using {@link PendingIntent}.
|
||||
*/
|
||||
private void linkButtons(final Context context, final RemoteViews views) {
|
||||
Intent action;
|
||||
PendingIntent pendingIntent;
|
||||
|
||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||
|
||||
// Home
|
||||
action = new Intent(context, MainActivity.class);
|
||||
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
|
||||
views.setOnClickPendingIntent(R.id.image, pendingIntent);
|
||||
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent);
|
||||
|
||||
// Previous track
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_REWIND, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||
|
||||
// Play and pause
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_TOGGLE_PAUSE, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||
|
||||
// Next track
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_SKIP, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,186 @@
|
|||
package code.name.monkey.retromusic.appwidgets;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.graphics.Palette;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.RemoteViews;
|
||||
import code.name.monkey.appthemehelper.util.MaterialValueHelper;
|
||||
import code.name.monkey.retromusic.Constants;
|
||||
import code.name.monkey.retromusic.R;
|
||||
import code.name.monkey.retromusic.appwidgets.base.BaseAppWidget;
|
||||
import code.name.monkey.retromusic.glide.SongGlideRequest;
|
||||
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper;
|
||||
import code.name.monkey.retromusic.model.Song;
|
||||
import code.name.monkey.retromusic.service.MusicService;
|
||||
import code.name.monkey.retromusic.ui.activities.MainActivity;
|
||||
import code.name.monkey.retromusic.util.RetroUtil;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.animation.GlideAnimation;
|
||||
import com.bumptech.glide.request.target.SimpleTarget;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
|
||||
public class AppWidgetSmall extends BaseAppWidget {
|
||||
|
||||
public static final String NAME = "app_widget_small";
|
||||
|
||||
private static AppWidgetSmall mInstance;
|
||||
private static int imageSize = 0;
|
||||
private static float cardRadius = 0f;
|
||||
private Target<BitmapPaletteWrapper> target; // for cancellation
|
||||
|
||||
public static synchronized AppWidgetSmall getInstance() {
|
||||
if (mInstance == null) {
|
||||
mInstance = new AppWidgetSmall();
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize given widgets to default state, where we launch Music on default click and hide
|
||||
* actions if service not running.
|
||||
*/
|
||||
protected void defaultAppWidget(final Context context, final int[] appWidgetIds) {
|
||||
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(),
|
||||
R.layout.app_widget_small);
|
||||
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||
appWidgetView.setImageViewResource(R.id.image, R.drawable.default_album_art);
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_next_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_previous_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_play_arrow_white_24dp,
|
||||
MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
|
||||
|
||||
linkButtons(context, appWidgetView);
|
||||
pushUpdate(context, appWidgetIds, appWidgetView);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update all active widget instances by pushing changes
|
||||
*/
|
||||
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
|
||||
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(),
|
||||
R.layout.app_widget_small);
|
||||
|
||||
final boolean isPlaying = service.isPlaying();
|
||||
final Song song = service.getCurrentSong();
|
||||
|
||||
// Set the titles and artwork
|
||||
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||
} else {
|
||||
if (TextUtils.isEmpty(song.title) || TextUtils.isEmpty(song.artistName)) {
|
||||
appWidgetView.setTextViewText(R.id.text_separator, "");
|
||||
} else {
|
||||
appWidgetView.setTextViewText(R.id.text_separator, "•");
|
||||
}
|
||||
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE);
|
||||
appWidgetView.setTextViewText(R.id.title, song.title);
|
||||
appWidgetView.setTextViewText(R.id.text, song.artistName);
|
||||
}
|
||||
|
||||
// Link actions buttons to intents
|
||||
linkButtons(service, appWidgetView);
|
||||
|
||||
if (imageSize == 0) {
|
||||
imageSize = service.getResources().getDimensionPixelSize(R.dimen.app_widget_small_image_size);
|
||||
}
|
||||
if (cardRadius == 0f) {
|
||||
cardRadius = service.getResources().getDimension(R.dimen.app_widget_card_radius);
|
||||
}
|
||||
|
||||
// Load the album cover async and push the update on completion
|
||||
final Context appContext = service.getApplicationContext();
|
||||
service.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (target != null) {
|
||||
Glide.clear(target);
|
||||
}
|
||||
target = SongGlideRequest.Builder.from(Glide.with(appContext), song)
|
||||
.checkIgnoreMediaStore(appContext)
|
||||
.generatePalette(service).build()
|
||||
.centerCrop()
|
||||
.into(new SimpleTarget<BitmapPaletteWrapper>(imageSize, imageSize) {
|
||||
@Override
|
||||
public void onResourceReady(BitmapPaletteWrapper resource,
|
||||
GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
|
||||
Palette palette = resource.getPalette();
|
||||
update(resource.getBitmap(), palette.getVibrantColor(palette
|
||||
.getMutedColor(MaterialValueHelper.getSecondaryTextColor(appContext, true))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFailed(Exception e, Drawable errorDrawable) {
|
||||
super.onLoadFailed(e, errorDrawable);
|
||||
update(null, MaterialValueHelper.getSecondaryTextColor(appContext, true));
|
||||
}
|
||||
|
||||
private void update(@Nullable Bitmap bitmap, int color) {
|
||||
// Set correct drawable for pause state
|
||||
int playPauseRes = isPlaying ? R.drawable.ic_pause_white_24dp
|
||||
: R.drawable.ic_play_arrow_white_24dp;
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause,
|
||||
createBitmap(RetroUtil.getTintedVectorDrawable(service, playPauseRes, color), 1f));
|
||||
|
||||
// Set prev/next button drawables
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp,
|
||||
color), 1f));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(
|
||||
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp,
|
||||
color), 1f));
|
||||
|
||||
final Drawable image = getAlbumArtDrawable(service.getResources(), bitmap);
|
||||
final Bitmap roundedBitmap = createRoundedBitmap(image, imageSize, imageSize,
|
||||
cardRadius, 0, 0, 0);
|
||||
appWidgetView.setImageViewBitmap(R.id.image, roundedBitmap);
|
||||
|
||||
pushUpdate(appContext, appWidgetIds, appWidgetView);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Link up various button actions using {@link PendingIntent}.
|
||||
*/
|
||||
private void linkButtons(final Context context, final RemoteViews views) {
|
||||
Intent action;
|
||||
PendingIntent pendingIntent;
|
||||
|
||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||
|
||||
// Home
|
||||
action = new Intent(context, MainActivity.class);
|
||||
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
|
||||
views.setOnClickPendingIntent(R.id.image, pendingIntent);
|
||||
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent);
|
||||
|
||||
// Previous track
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_REWIND, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||
|
||||
// Play and pause
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_TOGGLE_PAUSE, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||
|
||||
// Next track
|
||||
pendingIntent = buildPendingIntent(context, Constants.ACTION_SKIP, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package code.name.monkey.retromusic.appwidgets;
|
||||
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
|
||||
import code.name.monkey.retromusic.service.MusicService;
|
||||
|
||||
|
||||
/**
|
||||
* @author Eugene Cheung (arkon)
|
||||
*/
|
||||
public class BootReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
final AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
|
||||
|
||||
// Start music service if there are any existing widgets
|
||||
if (widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetBig.class)).length > 0 ||
|
||||
widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetClassic.class)).length > 0 ||
|
||||
widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetSmall.class)).length > 0 ||
|
||||
widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetCard.class)).length > 0) {
|
||||
final Intent serviceIntent = new Intent(context, MusicService.class);
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { // not allowed on Oreo
|
||||
context.startService(serviceIntent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,162 @@
|
|||
package code.name.monkey.retromusic.appwidgets.base;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProvider;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapShader;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Path;
|
||||
import android.graphics.RectF;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.widget.RemoteViews;
|
||||
import code.name.monkey.retromusic.Constants;
|
||||
import code.name.monkey.retromusic.R;
|
||||
import code.name.monkey.retromusic.model.Song;
|
||||
import code.name.monkey.retromusic.service.MusicService;
|
||||
|
||||
public abstract class BaseAppWidget extends AppWidgetProvider {
|
||||
|
||||
public static final String NAME = "app_widget";
|
||||
|
||||
protected static Bitmap createBitmap(Drawable drawable, float sizeMultiplier) {
|
||||
Bitmap bitmap = Bitmap.createBitmap((int) (drawable.getIntrinsicWidth() * sizeMultiplier),
|
||||
(int) (drawable.getIntrinsicHeight() * sizeMultiplier), Bitmap.Config.ARGB_8888);
|
||||
Canvas c = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, c.getWidth(), c.getHeight());
|
||||
drawable.draw(c);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
protected static Bitmap createRoundedBitmap(Drawable drawable, int width, int height, float tl,
|
||||
float tr, float bl, float br) {
|
||||
if (drawable == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
Canvas c = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, width, height);
|
||||
drawable.draw(c);
|
||||
|
||||
Bitmap rounded = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
|
||||
Canvas canvas = new Canvas(rounded);
|
||||
Paint paint = new Paint();
|
||||
paint.setShader(
|
||||
new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
|
||||
paint.setAntiAlias(true);
|
||||
canvas.drawPath(composeRoundedRectPath(new RectF(0, 0, width, height), tl, tr, bl, br), paint);
|
||||
|
||||
return rounded;
|
||||
}
|
||||
|
||||
protected static Path composeRoundedRectPath(RectF rect, float tl, float tr, float bl, float br) {
|
||||
Path path = new Path();
|
||||
tl = tl < 0 ? 0 : tl;
|
||||
tr = tr < 0 ? 0 : tr;
|
||||
bl = bl < 0 ? 0 : bl;
|
||||
br = br < 0 ? 0 : br;
|
||||
|
||||
path.moveTo(rect.left + tl, rect.top);
|
||||
path.lineTo(rect.right - tr, rect.top);
|
||||
path.quadTo(rect.right, rect.top, rect.right, rect.top + tr);
|
||||
path.lineTo(rect.right, rect.bottom - br);
|
||||
path.quadTo(rect.right, rect.bottom, rect.right - br, rect.bottom);
|
||||
path.lineTo(rect.left + bl, rect.bottom);
|
||||
path.quadTo(rect.left, rect.bottom, rect.left, rect.bottom - bl);
|
||||
path.lineTo(rect.left, rect.top + tl);
|
||||
path.quadTo(rect.left, rect.top, rect.left + tl, rect.top);
|
||||
path.close();
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager,
|
||||
final int[] appWidgetIds) {
|
||||
defaultAppWidget(context, appWidgetIds);
|
||||
final Intent updateIntent = new Intent(Constants.APP_WIDGET_UPDATE);
|
||||
updateIntent.putExtra(Constants.EXTRA_APP_WIDGET_NAME, NAME);
|
||||
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
|
||||
updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
|
||||
context.sendBroadcast(updateIntent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a change notification coming over from {@link MusicService}
|
||||
*/
|
||||
public void notifyChange(final MusicService service, final String what) {
|
||||
if (hasInstances(service)) {
|
||||
if (Constants.META_CHANGED.equals(what) || Constants.PLAY_STATE_CHANGED.equals(what)) {
|
||||
performUpdate(service, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void pushUpdate(final Context context, final int[] appWidgetIds,
|
||||
final RemoteViews views) {
|
||||
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||
if (appWidgetIds != null) {
|
||||
appWidgetManager.updateAppWidget(appWidgetIds, views);
|
||||
} else {
|
||||
appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check against {@link AppWidgetManager} if there are any instances of this widget.
|
||||
*/
|
||||
protected boolean hasInstances(final Context context) {
|
||||
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||
final int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context,
|
||||
getClass()));
|
||||
return mAppWidgetIds.length > 0;
|
||||
}
|
||||
|
||||
protected PendingIntent buildPendingIntent(Context context, final String action,
|
||||
final ComponentName serviceName) {
|
||||
Intent intent = new Intent(action);
|
||||
intent.setComponent(serviceName);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
return PendingIntent.getForegroundService(context, 0, intent, 0);
|
||||
} else {
|
||||
return PendingIntent.getService(context, 0, intent, 0);
|
||||
}
|
||||
}
|
||||
|
||||
abstract protected void defaultAppWidget(final Context context, final int[] appWidgetIds);
|
||||
|
||||
abstract public void performUpdate(final MusicService service, final int[] appWidgetIds);
|
||||
|
||||
protected Drawable getAlbumArtDrawable(final Resources resources, final Bitmap bitmap) {
|
||||
Drawable image;
|
||||
if (bitmap == null) {
|
||||
image = resources.getDrawable(R.drawable.default_album_art);
|
||||
} else {
|
||||
image = new BitmapDrawable(resources, bitmap);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
protected String getSongArtistAndAlbum(final Song song) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append(song.artistName);
|
||||
if (!TextUtils.isEmpty(song.artistName) && !TextUtils.isEmpty(song.albumName)) {
|
||||
builder.append(" • ");
|
||||
}
|
||||
builder.append(song.albumName);
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue