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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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_album);
|
||||
|
||||
|
|
@ -58,16 +54,12 @@ public class AppWidgetAlbum extends BaseAppWidget {
|
|||
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_album);
|
||||
|
||||
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 {
|
||||
|
|
@ -76,18 +68,14 @@ public class AppWidgetAlbum extends BaseAppWidget {
|
|||
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, 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_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);
|
||||
|
||||
// Load the album cover async and push the update on completion
|
||||
Point p = Util.getScreenSize(service);
|
||||
final int widgetImageSize = Math.min(p.x, p.y);
|
||||
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) {
|
||||
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, MusicService.ACTION_REWIND, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||
|
||||
// Play and pause
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||
|
||||
// Next track
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,9 +31,10 @@ public class AppWidgetCard extends BaseAppWidget {
|
|||
public static final String NAME = "app_widget_card";
|
||||
|
||||
private static AppWidgetCard mInstance;
|
||||
private Target<BitmapPaletteWrapper> target;
|
||||
|
||||
private static int imageSize = 0;
|
||||
private static float cardRadius = 0f;
|
||||
private Target<BitmapPaletteWrapper> target;
|
||||
|
||||
public static synchronized AppWidgetCard getInstance() {
|
||||
if (mInstance == null) {
|
||||
|
|
@ -43,10 +44,6 @@ public class AppWidgetCard extends BaseAppWidget {
|
|||
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);
|
||||
|
||||
|
|
@ -60,16 +57,12 @@ public class AppWidgetCard extends BaseAppWidget {
|
|||
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 {
|
||||
|
|
@ -78,15 +71,12 @@ public class AppWidgetCard extends BaseAppWidget {
|
|||
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, 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_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);
|
||||
|
||||
if (imageSize == 0)
|
||||
|
|
@ -94,7 +84,6 @@ public class AppWidgetCard extends BaseAppWidget {
|
|||
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() {
|
||||
|
|
@ -125,11 +114,9 @@ public class AppWidgetCard extends BaseAppWidget {
|
|||
}
|
||||
|
||||
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, 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_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) {
|
||||
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, MusicService.ACTION_REWIND, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||
|
||||
// Play and pause
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||
|
||||
// Next track
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,9 +31,10 @@ public class AppWidgetClassic extends BaseAppWidget {
|
|||
public static final String NAME = "app_widget_classic";
|
||||
|
||||
private static AppWidgetClassic mInstance;
|
||||
private Target<BitmapPaletteWrapper> target;
|
||||
|
||||
private static int imageSize = 0;
|
||||
private static float cardRadius = 0f;
|
||||
private Target<BitmapPaletteWrapper> target;
|
||||
|
||||
public static synchronized AppWidgetClassic getInstance() {
|
||||
if (mInstance == null) {
|
||||
|
|
@ -43,10 +44,6 @@ public class AppWidgetClassic extends BaseAppWidget {
|
|||
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);
|
||||
|
||||
|
|
@ -60,16 +57,12 @@ public class AppWidgetClassic extends BaseAppWidget {
|
|||
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 {
|
||||
|
|
@ -78,7 +71,6 @@ public class AppWidgetClassic extends BaseAppWidget {
|
|||
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
|
||||
}
|
||||
|
||||
// Link actions buttons to intents
|
||||
linkButtons(service, appWidgetView);
|
||||
|
||||
if (imageSize == 0)
|
||||
|
|
@ -86,7 +78,6 @@ public class AppWidgetClassic extends BaseAppWidget {
|
|||
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
|
||||
|
|
@ -118,11 +109,9 @@ public class AppWidgetClassic extends BaseAppWidget {
|
|||
}
|
||||
|
||||
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, 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_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) {
|
||||
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, MusicService.ACTION_REWIND, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||
|
||||
// Play and pause
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||
|
||||
// Next track
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ import android.graphics.drawable.Drawable;
|
|||
import android.os.Build;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import androidx.core.content.res.ResourcesCompat;
|
||||
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
import com.dkanada.gramophone.service.MusicService;
|
||||
|
|
@ -26,12 +28,8 @@ import com.dkanada.gramophone.util.MusicUtil;
|
|||
public abstract class BaseAppWidget extends AppWidgetProvider {
|
||||
public static final String NAME = "app_widget";
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager,
|
||||
final int[] appWidgetIds) {
|
||||
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
|
||||
defaultAppWidget(context, appWidgetIds);
|
||||
final Intent updateIntent = new Intent(MusicService.INTENT_EXTRA_WIDGET_UPDATE);
|
||||
updateIntent.putExtra(MusicService.INTENT_EXTRA_WIDGET_NAME, NAME);
|
||||
|
|
@ -40,10 +38,6 @@ public abstract class BaseAppWidget extends AppWidgetProvider {
|
|||
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 (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) {
|
||||
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);
|
||||
|
|
@ -126,13 +118,11 @@ public abstract class BaseAppWidget extends AppWidgetProvider {
|
|||
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);
|
||||
return ResourcesCompat.getDrawable(resources, R.drawable.default_album_art, null);
|
||||
} else {
|
||||
image = new BitmapDrawable(resources, bitmap);
|
||||
return new BitmapDrawable(resources, bitmap);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
protected String getSongArtistAndAlbum(final Song song) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue