Minor refactoring of widget code

This commit is contained in:
Eugene Cheung 2017-08-02 20:11:53 -04:00
commit 5d94555afe
No known key found for this signature in database
GPG key ID: E1FD745328866B0A
8 changed files with 121 additions and 153 deletions

View file

@ -18,6 +18,7 @@ import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.kabouzeid.appthemehelper.util.MaterialValueHelper;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.appwidgets.base.BaseAppWidget;
import com.kabouzeid.gramophone.glide.SongGlideRequest;
import com.kabouzeid.gramophone.model.Song;
import com.kabouzeid.gramophone.service.MusicService;
@ -69,7 +70,7 @@ public class AppWidgetBig extends BaseAppWidget {
} else {
appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE);
appWidgetView.setTextViewText(R.id.title, song.title);
appWidgetView.setTextViewText(R.id.text, song.artistName + "" +song.albumName);
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
}
// Set correct drawable for pause state
@ -109,7 +110,6 @@ public class AppWidgetBig extends BaseAppWidget {
}
private void update(@Nullable Bitmap bitmap) {
appWidgetView.setViewVisibility(R.id.image, View.VISIBLE);
if (bitmap == null) {
appWidgetView.setImageViewResource(R.id.image, R.drawable.default_album_art);
} else {

View file

@ -19,6 +19,7 @@ import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.kabouzeid.appthemehelper.util.MaterialValueHelper;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.appwidgets.base.BaseAppWidget;
import com.kabouzeid.gramophone.glide.SongGlideRequest;
import com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper;
import com.kabouzeid.gramophone.model.Song;
@ -49,7 +50,7 @@ public class AppWidgetCard extends BaseAppWidget {
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.app_widget_card);
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
appWidgetView.setViewVisibility(R.id.image, View.INVISIBLE);
appWidgetView.setImageViewResource(R.id.image, R.drawable.default_album_art);
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(Util.getTintedVectorDrawable(context, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(Util.getTintedVectorDrawable(context, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(Util.getTintedVectorDrawable(context, R.drawable.ic_play_arrow_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true)), 1f));
@ -71,16 +72,9 @@ public class AppWidgetCard extends BaseAppWidget {
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
} else {
if (TextUtils.isEmpty(song.artistName) || TextUtils.isEmpty(song.albumName)) {
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.artist, song.artistName);
appWidgetView.setTextViewText(R.id.album, song.albumName);
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
}
// Set correct drawable for pause state
@ -124,8 +118,6 @@ public class AppWidgetCard extends BaseAppWidget {
}
private void update(@Nullable Bitmap bitmap, int color) {
appWidgetView.setViewVisibility(R.id.image, View.VISIBLE);
// 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(Util.getTintedVectorDrawable(service, playPauseRes, color), 1f));
@ -134,15 +126,9 @@ public class AppWidgetCard extends BaseAppWidget {
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, color), 1f));
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, color), 1f));
Drawable image;
if (bitmap == null) {
image = service.getResources().getDrawable(R.drawable.default_album_art);
} else {
image = new BitmapDrawable(bitmap);
}
appWidgetView.setImageViewBitmap(R.id.image, createRoundedBitmap(image, imageSize, imageSize, cardRadius, 0, cardRadius, 0));
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);
}

View file

@ -19,6 +19,7 @@ import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.kabouzeid.appthemehelper.util.MaterialValueHelper;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.appwidgets.base.BaseAppWidget;
import com.kabouzeid.gramophone.glide.SongGlideRequest;
import com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper;
import com.kabouzeid.gramophone.model.Song;
@ -73,7 +74,7 @@ public class AppWidgetClassic extends BaseAppWidget {
} else {
appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE);
appWidgetView.setTextViewText(R.id.title, song.title);
appWidgetView.setTextViewText(R.id.text, song.artistName + "" +song.albumName);
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song));
}
// Link actions buttons to intents
@ -109,9 +110,7 @@ public class AppWidgetClassic extends BaseAppWidget {
update(null, MaterialValueHelper.getSecondaryTextColor(appContext, true));
}
private void update(@Nullable Bitmap bitmap,int color) {
appWidgetView.setViewVisibility(R.id.image, View.VISIBLE);
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(Util.getTintedVectorDrawable(service, playPauseRes, color), 1f));
@ -120,13 +119,9 @@ public class AppWidgetClassic extends BaseAppWidget {
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, color), 1f));
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, color), 1f));
Drawable image;
if (bitmap == null) {
image = service.getResources().getDrawable(R.drawable.default_album_art);
} else {
image = new BitmapDrawable(bitmap);
}
appWidgetView.setImageViewBitmap(R.id.image, createRoundedBitmap(image, imageSize, imageSize, cardRadius, 0, cardRadius, 0));
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);
}

View file

@ -19,6 +19,7 @@ import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.kabouzeid.appthemehelper.util.MaterialValueHelper;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.appwidgets.base.BaseAppWidget;
import com.kabouzeid.gramophone.glide.SongGlideRequest;
import com.kabouzeid.gramophone.glide.palette.BitmapPaletteWrapper;
import com.kabouzeid.gramophone.model.Song;
@ -116,8 +117,6 @@ public class AppWidgetSmall extends BaseAppWidget {
}
private void update(@Nullable Bitmap bitmap, int color) {
appWidgetView.setViewVisibility(R.id.image, View.VISIBLE);
// 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(Util.getTintedVectorDrawable(service, playPauseRes, color), 1f));
@ -126,13 +125,9 @@ public class AppWidgetSmall extends BaseAppWidget {
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, color), 1f));
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(Util.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, color), 1f));
Drawable image;
if (bitmap == null) {
image = service.getResources().getDrawable(R.drawable.default_album_art);
} else {
image = new BitmapDrawable(bitmap);
}
appWidgetView.setImageViewBitmap(R.id.image, createRoundedBitmap(image, imageSize, imageSize, cardRadius, 0, 0, 0));
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);
}

View file

@ -8,9 +8,6 @@ import android.content.Intent;
import com.kabouzeid.gramophone.service.MusicService;
/**
* @author Eugene Cheung (arkon)
*/
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

View file

@ -1,4 +1,4 @@
package com.kabouzeid.gramophone.appwidgets;
package com.kabouzeid.gramophone.appwidgets.base;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
@ -6,20 +6,77 @@ 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.text.TextUtils;
import android.widget.RemoteViews;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.model.Song;
import com.kabouzeid.gramophone.service.MusicService;
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) {
defaultAppWidget(context, appWidgetIds);
final Intent updateIntent = new Intent(MusicService.APP_WIDGET_UPDATE);
updateIntent.putExtra(MusicService.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 (MusicService.META_CHANGED.equals(what) || MusicService.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);
return PendingIntent.getService(context, 0, intent, 0);
}
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);
@ -72,55 +129,23 @@ public abstract class BaseAppWidget extends AppWidgetProvider {
abstract public void performUpdate(final MusicService service, final int[] appWidgetIds);
/**
* {@inheritDoc}
*/
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager,
final int[] appWidgetIds) {
defaultAppWidget(context, appWidgetIds);
final Intent updateIntent = new Intent(MusicService.APP_WIDGET_UPDATE);
updateIntent.putExtra(MusicService.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 (MusicService.META_CHANGED.equals(what) || MusicService.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);
protected Drawable getAlbumArtDrawable(final Resources resources, final Bitmap bitmap) {
Drawable image;
if (bitmap == null) {
image = resources.getDrawable(R.drawable.default_album_art);
} else {
appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
image = new BitmapDrawable(resources, bitmap);
}
return image;
}
/**
* 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);
return PendingIntent.getService(context, 0, intent, 0);
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();
}
}

View file

@ -1,84 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:background="@drawable/card">
android:background="@drawable/card"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/app_widget_card_image_size"
android:layout_height="@dimen/app_widget_card_image_size"
android:scaleType="centerInside"
tools:src="@drawable/default_album_art"
tools:ignore="ContentDescription"/>
tools:ignore="ContentDescription"
tools:src="@drawable/default_album_art" />
<LinearLayout
android:id="@+id/media_titles"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical"
android:gravity="center">
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@color/ate_primary_text_light"
android:singleLine="true"
tools:text="Title"/>
tools:text="Title" />
<LinearLayout
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<TextView
android:id="@+id/artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@color/ate_secondary_text_light"
android:singleLine="true"
tools:text="Artist"/>
<TextView
android:id="@+id/text_separator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@color/ate_secondary_text_light"
android:singleLine="true"
tools:ignore="HardcodedText"
tools:text="•"/>
<TextView
android:id="@+id/album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@color/ate_secondary_text_light"
android:singleLine="true"
tools:text="Album"/>
</LinearLayout>
android:gravity="center_vertical"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
android:textColor="@color/ate_secondary_text_light"
tools:text="Text" />
</LinearLayout>
@ -101,7 +71,7 @@
android:background="@drawable/widget_selector_light"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_skip_previous_white_24dp"
tools:tint="@color/ate_secondary_text_light"/>
tools:tint="@color/ate_secondary_text_light" />
<ImageButton
android:id="@+id/button_toggle_play_pause"
@ -111,7 +81,7 @@
android:background="@drawable/widget_selector_light"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_play_arrow_white_24dp"
tools:tint="@color/ate_secondary_text_light"/>
tools:tint="@color/ate_secondary_text_light" />
<ImageButton
android:id="@+id/button_next"
@ -121,7 +91,7 @@
android:background="@drawable/widget_selector_light"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_skip_next_white_24dp"
tools:tint="@color/ate_secondary_text_light"/>
tools:tint="@color/ate_secondary_text_light" />
</LinearLayout>

View file

@ -12,8 +12,8 @@
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
tools:ignore="ContentDescription"
tools:src="@drawable/default_album_art" />
@ -25,13 +25,13 @@
android:id="@+id/media_actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layoutDirection="ltr"
android:orientation="horizontal"
android:layout_marginBottom="8dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
android:layout_alignParentStart="true"
android:layout_gravity="bottom"
android:layout_marginBottom="8dp"
android:layoutDirection="ltr"
android:orientation="horizontal">
<ImageButton
android:id="@+id/button_prev"
@ -68,10 +68,10 @@
android:layout_height="wrap_content"
android:layout_above="@+id/media_actions"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp">
android:layout_marginRight="8dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/title"