refactor widget classes
This commit is contained in:
parent
c9054f6b0c
commit
76b5c99f7f
7 changed files with 11 additions and 84 deletions
|
|
@ -41,10 +41,6 @@ public class AppWidgetAlbum extends BaseAppWidget {
|
||||||
return mInstance;
|
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) {
|
protected void defaultAppWidget(final Context context, final int[] appWidgetIds) {
|
||||||
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.app_widget_album);
|
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.app_widget_album);
|
||||||
|
|
||||||
|
|
@ -58,16 +54,12 @@ public class AppWidgetAlbum extends BaseAppWidget {
|
||||||
pushUpdate(context, appWidgetIds, appWidgetView);
|
pushUpdate(context, appWidgetIds, appWidgetView);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Update all active widget instances by pushing changes
|
|
||||||
*/
|
|
||||||
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
|
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
|
||||||
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(), R.layout.app_widget_album);
|
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(), R.layout.app_widget_album);
|
||||||
|
|
||||||
final boolean isPlaying = service.isPlaying();
|
final boolean isPlaying = service.isPlaying();
|
||||||
final Song song = service.getCurrentSong();
|
final Song song = service.getCurrentSong();
|
||||||
|
|
||||||
// Set the titles and artwork
|
|
||||||
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
||||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -76,18 +68,14 @@ public class AppWidgetAlbum extends BaseAppWidget {
|
||||||
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
|
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;
|
int playPauseRes = isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp;
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, playPauseRes, MaterialValueHelper.getPrimaryTextColor(service, false))));
|
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, playPauseRes, MaterialValueHelper.getPrimaryTextColor(service, false))));
|
||||||
|
|
||||||
// Set prev/next button drawables
|
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getPrimaryTextColor(service, false))));
|
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getPrimaryTextColor(service, false))));
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getPrimaryTextColor(service, false))));
|
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getPrimaryTextColor(service, false))));
|
||||||
|
|
||||||
// Link actions buttons to intents
|
|
||||||
linkButtons(service, appWidgetView);
|
linkButtons(service, appWidgetView);
|
||||||
|
|
||||||
// Load the album cover async and push the update on completion
|
|
||||||
Point p = Util.getScreenSize(service);
|
Point p = Util.getScreenSize(service);
|
||||||
final int widgetImageSize = Math.min(p.x, p.y);
|
final int widgetImageSize = Math.min(p.x, p.y);
|
||||||
final Context appContext = service.getApplicationContext();
|
final Context appContext = service.getApplicationContext();
|
||||||
|
|
@ -133,30 +121,23 @@ public class AppWidgetAlbum extends BaseAppWidget {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Link up various button actions using {@link PendingIntent}.
|
|
||||||
*/
|
|
||||||
private void linkButtons(final Context context, final RemoteViews views) {
|
private void linkButtons(final Context context, final RemoteViews views) {
|
||||||
Intent action;
|
Intent action;
|
||||||
PendingIntent pendingIntent;
|
PendingIntent pendingIntent;
|
||||||
|
|
||||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||||
|
|
||||||
// Home
|
|
||||||
action = new Intent(context, MainActivity.class);
|
action = new Intent(context, MainActivity.class);
|
||||||
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
|
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
|
||||||
views.setOnClickPendingIntent(R.id.clickable_area, pendingIntent);
|
views.setOnClickPendingIntent(R.id.clickable_area, pendingIntent);
|
||||||
|
|
||||||
// Previous track
|
|
||||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_REWIND, serviceName);
|
pendingIntent = buildPendingIntent(context, MusicService.ACTION_REWIND, serviceName);
|
||||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||||
|
|
||||||
// Play and pause
|
|
||||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
||||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||||
|
|
||||||
// Next track
|
|
||||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
||||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,9 +31,10 @@ public class AppWidgetCard extends BaseAppWidget {
|
||||||
public static final String NAME = "app_widget_card";
|
public static final String NAME = "app_widget_card";
|
||||||
|
|
||||||
private static AppWidgetCard mInstance;
|
private static AppWidgetCard mInstance;
|
||||||
|
private Target<BitmapPaletteWrapper> target;
|
||||||
|
|
||||||
private static int imageSize = 0;
|
private static int imageSize = 0;
|
||||||
private static float cardRadius = 0f;
|
private static float cardRadius = 0f;
|
||||||
private Target<BitmapPaletteWrapper> target;
|
|
||||||
|
|
||||||
public static synchronized AppWidgetCard getInstance() {
|
public static synchronized AppWidgetCard getInstance() {
|
||||||
if (mInstance == null) {
|
if (mInstance == null) {
|
||||||
|
|
@ -43,10 +44,6 @@ public class AppWidgetCard extends BaseAppWidget {
|
||||||
return mInstance;
|
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) {
|
protected void defaultAppWidget(final Context context, final int[] appWidgetIds) {
|
||||||
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.app_widget_card);
|
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.app_widget_card);
|
||||||
|
|
||||||
|
|
@ -60,16 +57,12 @@ public class AppWidgetCard extends BaseAppWidget {
|
||||||
pushUpdate(context, appWidgetIds, appWidgetView);
|
pushUpdate(context, appWidgetIds, appWidgetView);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Update all active widget instances by pushing changes
|
|
||||||
*/
|
|
||||||
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
|
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
|
||||||
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(), R.layout.app_widget_card);
|
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(), R.layout.app_widget_card);
|
||||||
|
|
||||||
final boolean isPlaying = service.isPlaying();
|
final boolean isPlaying = service.isPlaying();
|
||||||
final Song song = service.getCurrentSong();
|
final Song song = service.getCurrentSong();
|
||||||
|
|
||||||
// Set the titles and artwork
|
|
||||||
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
||||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -78,15 +71,12 @@ public class AppWidgetCard extends BaseAppWidget {
|
||||||
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
|
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;
|
int playPauseRes = isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp;
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, playPauseRes, MaterialValueHelper.getSecondaryTextColor(service, true))));
|
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, playPauseRes, MaterialValueHelper.getSecondaryTextColor(service, true))));
|
||||||
|
|
||||||
// Set prev/next button drawables
|
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getSecondaryTextColor(service, true))));
|
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getSecondaryTextColor(service, true))));
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getSecondaryTextColor(service, true))));
|
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getSecondaryTextColor(service, true))));
|
||||||
|
|
||||||
// Link actions buttons to intents
|
|
||||||
linkButtons(service, appWidgetView);
|
linkButtons(service, appWidgetView);
|
||||||
|
|
||||||
if (imageSize == 0)
|
if (imageSize == 0)
|
||||||
|
|
@ -94,7 +84,6 @@ public class AppWidgetCard extends BaseAppWidget {
|
||||||
if (cardRadius == 0f)
|
if (cardRadius == 0f)
|
||||||
cardRadius = service.getResources().getDimension(R.dimen.app_widget_card_radius);
|
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() {
|
service.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
@ -125,11 +114,9 @@ public class AppWidgetCard extends BaseAppWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void update(@Nullable Bitmap bitmap, int color) {
|
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;
|
int playPauseRes = isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp;
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, playPauseRes, color)));
|
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, playPauseRes, color)));
|
||||||
|
|
||||||
// Set prev/next button drawables
|
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, color)));
|
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, color)));
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, color)));
|
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, color)));
|
||||||
|
|
||||||
|
|
@ -144,31 +131,24 @@ public class AppWidgetCard extends BaseAppWidget {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Link up various button actions using {@link PendingIntent}.
|
|
||||||
*/
|
|
||||||
private void linkButtons(final Context context, final RemoteViews views) {
|
private void linkButtons(final Context context, final RemoteViews views) {
|
||||||
Intent action;
|
Intent action;
|
||||||
PendingIntent pendingIntent;
|
PendingIntent pendingIntent;
|
||||||
|
|
||||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||||
|
|
||||||
// Home
|
|
||||||
action = new Intent(context, MainActivity.class);
|
action = new Intent(context, MainActivity.class);
|
||||||
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
|
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
|
||||||
views.setOnClickPendingIntent(R.id.image, pendingIntent);
|
views.setOnClickPendingIntent(R.id.image, pendingIntent);
|
||||||
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent);
|
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent);
|
||||||
|
|
||||||
// Previous track
|
|
||||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_REWIND, serviceName);
|
pendingIntent = buildPendingIntent(context, MusicService.ACTION_REWIND, serviceName);
|
||||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||||
|
|
||||||
// Play and pause
|
|
||||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
||||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||||
|
|
||||||
// Next track
|
|
||||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
||||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,9 +31,10 @@ public class AppWidgetClassic extends BaseAppWidget {
|
||||||
public static final String NAME = "app_widget_classic";
|
public static final String NAME = "app_widget_classic";
|
||||||
|
|
||||||
private static AppWidgetClassic mInstance;
|
private static AppWidgetClassic mInstance;
|
||||||
|
private Target<BitmapPaletteWrapper> target;
|
||||||
|
|
||||||
private static int imageSize = 0;
|
private static int imageSize = 0;
|
||||||
private static float cardRadius = 0f;
|
private static float cardRadius = 0f;
|
||||||
private Target<BitmapPaletteWrapper> target;
|
|
||||||
|
|
||||||
public static synchronized AppWidgetClassic getInstance() {
|
public static synchronized AppWidgetClassic getInstance() {
|
||||||
if (mInstance == null) {
|
if (mInstance == null) {
|
||||||
|
|
@ -43,10 +44,6 @@ public class AppWidgetClassic extends BaseAppWidget {
|
||||||
return mInstance;
|
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) {
|
protected void defaultAppWidget(final Context context, final int[] appWidgetIds) {
|
||||||
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.app_widget_classic);
|
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.app_widget_classic);
|
||||||
|
|
||||||
|
|
@ -60,16 +57,12 @@ public class AppWidgetClassic extends BaseAppWidget {
|
||||||
pushUpdate(context, appWidgetIds, appWidgetView);
|
pushUpdate(context, appWidgetIds, appWidgetView);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Update all active widget instances by pushing changes
|
|
||||||
*/
|
|
||||||
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
|
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
|
||||||
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(), R.layout.app_widget_classic);
|
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(), R.layout.app_widget_classic);
|
||||||
|
|
||||||
final boolean isPlaying = service.isPlaying();
|
final boolean isPlaying = service.isPlaying();
|
||||||
final Song song = service.getCurrentSong();
|
final Song song = service.getCurrentSong();
|
||||||
|
|
||||||
// Set the titles and artwork
|
|
||||||
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
||||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -78,7 +71,6 @@ public class AppWidgetClassic extends BaseAppWidget {
|
||||||
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
|
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Link actions buttons to intents
|
|
||||||
linkButtons(service, appWidgetView);
|
linkButtons(service, appWidgetView);
|
||||||
|
|
||||||
if (imageSize == 0)
|
if (imageSize == 0)
|
||||||
|
|
@ -86,7 +78,6 @@ public class AppWidgetClassic extends BaseAppWidget {
|
||||||
if (cardRadius == 0f)
|
if (cardRadius == 0f)
|
||||||
cardRadius = service.getResources().getDimension(R.dimen.app_widget_card_radius);
|
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();
|
final Context appContext = service.getApplicationContext();
|
||||||
service.runOnUiThread(new Runnable() {
|
service.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -118,11 +109,9 @@ public class AppWidgetClassic extends BaseAppWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void update(@Nullable Bitmap bitmap, int color) {
|
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;
|
int playPauseRes = isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp;
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, playPauseRes, color)));
|
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, playPauseRes, color)));
|
||||||
|
|
||||||
// Set prev/next button drawables
|
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, color)));
|
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, color)));
|
||||||
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, color)));
|
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, color)));
|
||||||
|
|
||||||
|
|
@ -137,31 +126,24 @@ public class AppWidgetClassic extends BaseAppWidget {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Link up various button actions using {@link PendingIntent}.
|
|
||||||
*/
|
|
||||||
private void linkButtons(final Context context, final RemoteViews views) {
|
private void linkButtons(final Context context, final RemoteViews views) {
|
||||||
Intent action;
|
Intent action;
|
||||||
PendingIntent pendingIntent;
|
PendingIntent pendingIntent;
|
||||||
|
|
||||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||||
|
|
||||||
// Home
|
|
||||||
action = new Intent(context, MainActivity.class);
|
action = new Intent(context, MainActivity.class);
|
||||||
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
|
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
|
||||||
views.setOnClickPendingIntent(R.id.image, pendingIntent);
|
views.setOnClickPendingIntent(R.id.image, pendingIntent);
|
||||||
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent);
|
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent);
|
||||||
|
|
||||||
// Previous track
|
|
||||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_REWIND, serviceName);
|
pendingIntent = buildPendingIntent(context, MusicService.ACTION_REWIND, serviceName);
|
||||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||||
|
|
||||||
// Play and pause
|
|
||||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
||||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||||
|
|
||||||
// Next track
|
|
||||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
||||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@ import android.graphics.drawable.Drawable;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.widget.RemoteViews;
|
import android.widget.RemoteViews;
|
||||||
|
|
||||||
|
import androidx.core.content.res.ResourcesCompat;
|
||||||
|
|
||||||
import com.dkanada.gramophone.R;
|
import com.dkanada.gramophone.R;
|
||||||
import com.dkanada.gramophone.model.Song;
|
import com.dkanada.gramophone.model.Song;
|
||||||
import com.dkanada.gramophone.service.MusicService;
|
import com.dkanada.gramophone.service.MusicService;
|
||||||
|
|
@ -26,12 +28,8 @@ import com.dkanada.gramophone.util.MusicUtil;
|
||||||
public abstract class BaseAppWidget extends AppWidgetProvider {
|
public abstract class BaseAppWidget extends AppWidgetProvider {
|
||||||
public static final String NAME = "app_widget";
|
public static final String NAME = "app_widget";
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager,
|
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
|
||||||
final int[] appWidgetIds) {
|
|
||||||
defaultAppWidget(context, appWidgetIds);
|
defaultAppWidget(context, appWidgetIds);
|
||||||
final Intent updateIntent = new Intent(MusicService.INTENT_EXTRA_WIDGET_UPDATE);
|
final Intent updateIntent = new Intent(MusicService.INTENT_EXTRA_WIDGET_UPDATE);
|
||||||
updateIntent.putExtra(MusicService.INTENT_EXTRA_WIDGET_NAME, NAME);
|
updateIntent.putExtra(MusicService.INTENT_EXTRA_WIDGET_NAME, NAME);
|
||||||
|
|
@ -40,10 +38,6 @@ public abstract class BaseAppWidget extends AppWidgetProvider {
|
||||||
context.sendBroadcast(updateIntent);
|
context.sendBroadcast(updateIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle a change notification coming over from
|
|
||||||
* {@link MusicService}
|
|
||||||
*/
|
|
||||||
public void notifyChange(final MusicService service, final String what) {
|
public void notifyChange(final MusicService service, final String what) {
|
||||||
if (hasInstances(service)) {
|
if (hasInstances(service)) {
|
||||||
if (MusicService.META_CHANGED.equals(what) || MusicService.STATE_CHANGED.equals(what)) {
|
if (MusicService.META_CHANGED.equals(what) || MusicService.STATE_CHANGED.equals(what)) {
|
||||||
|
|
@ -61,18 +55,16 @@ public abstract class BaseAppWidget extends AppWidgetProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check against {@link AppWidgetManager} if there are any instances of this
|
|
||||||
* widget.
|
|
||||||
*/
|
|
||||||
protected boolean hasInstances(final Context context) {
|
protected boolean hasInstances(final Context context) {
|
||||||
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||||
final int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));
|
final int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getClass()));
|
||||||
|
|
||||||
return mAppWidgetIds.length > 0;
|
return mAppWidgetIds.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected PendingIntent buildPendingIntent(Context context, final String action, final ComponentName serviceName) {
|
protected PendingIntent buildPendingIntent(Context context, final String action, final ComponentName serviceName) {
|
||||||
Intent intent = new Intent(action);
|
Intent intent = new Intent(action);
|
||||||
|
|
||||||
intent.setComponent(serviceName);
|
intent.setComponent(serviceName);
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
return PendingIntent.getForegroundService(context, 0, intent, 0);
|
return PendingIntent.getForegroundService(context, 0, intent, 0);
|
||||||
|
|
@ -126,13 +118,11 @@ public abstract class BaseAppWidget extends AppWidgetProvider {
|
||||||
abstract public void performUpdate(final MusicService service, final int[] appWidgetIds);
|
abstract public void performUpdate(final MusicService service, final int[] appWidgetIds);
|
||||||
|
|
||||||
protected Drawable getAlbumArtDrawable(final Resources resources, final Bitmap bitmap) {
|
protected Drawable getAlbumArtDrawable(final Resources resources, final Bitmap bitmap) {
|
||||||
Drawable image;
|
|
||||||
if (bitmap == null) {
|
if (bitmap == null) {
|
||||||
image = resources.getDrawable(R.drawable.default_album_art);
|
return ResourcesCompat.getDrawable(resources, R.drawable.default_album_art, null);
|
||||||
} else {
|
} else {
|
||||||
image = new BitmapDrawable(resources, bitmap);
|
return new BitmapDrawable(resources, bitmap);
|
||||||
}
|
}
|
||||||
return image;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getSongArtistAndAlbum(final Song song) {
|
protected String getSongArtistAndAlbum(final Song song) {
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
|
||||||
android:initialLayout="@layout/app_widget_album"
|
android:initialLayout="@layout/app_widget_album"
|
||||||
android:minHeight="@dimen/app_widget_big_min_height"
|
android:minHeight="@dimen/app_widget_big_min_height"
|
||||||
android:minWidth="@dimen/app_widget_big_min_width"
|
android:minWidth="@dimen/app_widget_big_min_width"
|
||||||
android:resizeMode="horizontal|vertical"
|
android:resizeMode="horizontal|vertical"
|
||||||
android:updatePeriodMillis="0"
|
android:updatePeriodMillis="0"
|
||||||
android:widgetCategory="keyguard|home_screen"
|
android:widgetCategory="keyguard|home_screen"
|
||||||
tools:ignore="UnusedAttribute"
|
|
||||||
android:previewImage="@drawable/app_widget_album"/>
|
android:previewImage="@drawable/app_widget_album"/>
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
|
||||||
android:initialLayout="@layout/app_widget_card"
|
android:initialLayout="@layout/app_widget_card"
|
||||||
android:minHeight="@dimen/app_widget_card_min_height"
|
android:minHeight="@dimen/app_widget_card_min_height"
|
||||||
android:minWidth="@dimen/app_widget_card_min_width"
|
android:minWidth="@dimen/app_widget_card_min_width"
|
||||||
android:resizeMode="horizontal|vertical"
|
android:resizeMode="horizontal|vertical"
|
||||||
android:updatePeriodMillis="0"
|
android:updatePeriodMillis="0"
|
||||||
android:widgetCategory="keyguard|home_screen"
|
android:widgetCategory="keyguard|home_screen"
|
||||||
tools:ignore="UnusedAttribute"
|
|
||||||
android:previewImage="@drawable/app_widget_card"/>
|
android:previewImage="@drawable/app_widget_card"/>
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
|
||||||
android:initialLayout="@layout/app_widget_classic"
|
android:initialLayout="@layout/app_widget_classic"
|
||||||
android:minHeight="@dimen/app_widget_classic_min_height"
|
android:minHeight="@dimen/app_widget_classic_min_height"
|
||||||
android:minWidth="@dimen/app_widget_classic_min_width"
|
android:minWidth="@dimen/app_widget_classic_min_width"
|
||||||
android:resizeMode="horizontal|vertical"
|
android:resizeMode="horizontal|vertical"
|
||||||
android:updatePeriodMillis="0"
|
android:updatePeriodMillis="0"
|
||||||
android:widgetCategory="keyguard|home_screen"
|
android:widgetCategory="keyguard|home_screen"
|
||||||
tools:ignore="UnusedAttribute"
|
|
||||||
android:previewImage="@drawable/app_widget_classic" />
|
android:previewImage="@drawable/app_widget_classic" />
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue