Minor refactoring
This commit is contained in:
parent
4fddddec9e
commit
c06a17f411
1 changed files with 16 additions and 15 deletions
|
|
@ -27,15 +27,15 @@ import com.kabouzeid.gramophone.service.MusicService;
|
||||||
import com.kabouzeid.gramophone.ui.activities.MainActivity;
|
import com.kabouzeid.gramophone.ui.activities.MainActivity;
|
||||||
import com.kabouzeid.gramophone.util.Util;
|
import com.kabouzeid.gramophone.util.Util;
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Karim Abou Zeid (kabouzeid)
|
|
||||||
*/
|
|
||||||
public class AppWidgetCard extends BaseAppWidget {
|
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; // for cancellation
|
private Target<BitmapPaletteWrapper> target; // for cancellation
|
||||||
|
|
||||||
|
private static int imageSize = 0;
|
||||||
|
private static float cardRadius = 0f;
|
||||||
|
|
||||||
public static synchronized AppWidgetCard getInstance() {
|
public static synchronized AppWidgetCard getInstance() {
|
||||||
if (mInstance == null) {
|
if (mInstance == null) {
|
||||||
mInstance = new AppWidgetCard();
|
mInstance = new AppWidgetCard();
|
||||||
|
|
@ -142,29 +142,32 @@ public class AppWidgetCard extends BaseAppWidget {
|
||||||
// Link actions buttons to intents
|
// Link actions buttons to intents
|
||||||
linkButtons(service, appWidgetView);
|
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
|
// Load the album cover async and push the update on completion
|
||||||
final Context appContext = service.getApplicationContext();
|
|
||||||
final int widgetImageSize = service.getResources().getDimensionPixelSize(R.dimen.app_widget_small_image_size);
|
|
||||||
service.runOnUiThread(new Runnable() {
|
service.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (target != null) {
|
if (target != null) {
|
||||||
Glide.clear(target);
|
Glide.clear(target);
|
||||||
}
|
}
|
||||||
target = SongGlideRequest.Builder.from(Glide.with(appContext), song)
|
target = SongGlideRequest.Builder.from(Glide.with(service), song)
|
||||||
.checkIgnoreMediaStore(appContext)
|
.checkIgnoreMediaStore(service)
|
||||||
.generatePalette(service).build()
|
.generatePalette(service).build()
|
||||||
.into(new SimpleTarget<BitmapPaletteWrapper>(widgetImageSize, widgetImageSize) {
|
.into(new SimpleTarget<BitmapPaletteWrapper>(imageSize, imageSize) {
|
||||||
@Override
|
@Override
|
||||||
public void onResourceReady(BitmapPaletteWrapper resource, GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
|
public void onResourceReady(BitmapPaletteWrapper resource, GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
|
||||||
Palette palette = resource.getPalette();
|
Palette palette = resource.getPalette();
|
||||||
update(resource.getBitmap(), palette.getVibrantColor(palette.getMutedColor(MaterialValueHelper.getSecondaryTextColor(appContext, true))));
|
update(resource.getBitmap(), palette.getVibrantColor(palette.getMutedColor(MaterialValueHelper.getSecondaryTextColor(service, true))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoadFailed(Exception e, Drawable errorDrawable) {
|
public void onLoadFailed(Exception e, Drawable errorDrawable) {
|
||||||
super.onLoadFailed(e, errorDrawable);
|
super.onLoadFailed(e, errorDrawable);
|
||||||
update(null, MaterialValueHelper.getSecondaryTextColor(appContext, true));
|
update(null, MaterialValueHelper.getSecondaryTextColor(service, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void update(@Nullable Bitmap bitmap, int color) {
|
private void update(@Nullable Bitmap bitmap, int color) {
|
||||||
|
|
@ -181,16 +184,14 @@ public class AppWidgetCard extends BaseAppWidget {
|
||||||
Drawable image;
|
Drawable image;
|
||||||
|
|
||||||
if (bitmap == null) {
|
if (bitmap == null) {
|
||||||
image = appContext.getResources().getDrawable(R.drawable.default_album_art);
|
image = service.getResources().getDrawable(R.drawable.default_album_art);
|
||||||
} else {
|
} else {
|
||||||
image = new BitmapDrawable(bitmap);
|
image = new BitmapDrawable(bitmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
float radius = appContext.getResources().getDimension(R.dimen.app_widget_card_radius);
|
appWidgetView.setImageViewBitmap(R.id.image, createRoundedBitmap(image, imageSize, imageSize, cardRadius, 0, cardRadius, 0));
|
||||||
int size = appContext.getResources().getDimensionPixelSize(R.dimen.app_widget_card_image_size);
|
|
||||||
appWidgetView.setImageViewBitmap(R.id.image, createRoundedBitmap(image, size, size, radius, 0, radius, 0));
|
|
||||||
|
|
||||||
pushUpdate(appContext, appWidgetIds, appWidgetView);
|
pushUpdate(service, appWidgetIds, appWidgetView);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue