reduce number of folders in main directory
This commit is contained in:
parent
971735b0b9
commit
2c15037bfb
24 changed files with 55 additions and 55 deletions
|
|
@ -0,0 +1,67 @@
|
|||
package com.dkanada.gramophone.views.shortcuts;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.AdaptiveIconDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.graphics.drawable.LayerDrawable;
|
||||
import android.os.Build;
|
||||
import android.util.TypedValue;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
|
||||
import com.kabouzeid.appthemehelper.ThemeStore;
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.util.ImageUtil;
|
||||
import com.dkanada.gramophone.util.PreferenceUtil;
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.N_MR1)
|
||||
public final class AppShortcutIconGenerator {
|
||||
|
||||
public static Icon generateThemedIcon(Context context, int iconId) {
|
||||
if (PreferenceUtil.getInstance(context).getColoredShortcuts()) {
|
||||
return generateUserThemedIcon(context, iconId).toIcon();
|
||||
} else {
|
||||
return generateDefaultThemedIcon(context, iconId).toIcon();
|
||||
}
|
||||
}
|
||||
|
||||
private static IconCompat generateDefaultThemedIcon(Context context, int iconId) {
|
||||
// Return an Icon of iconId with default colors
|
||||
return generateThemedIcon(context, iconId,
|
||||
context.getColor(R.color.app_shortcut_default_foreground),
|
||||
context.getColor(R.color.app_shortcut_default_background)
|
||||
);
|
||||
}
|
||||
|
||||
private static IconCompat generateUserThemedIcon(Context context, int iconId) {
|
||||
// Get background color from context's theme
|
||||
final TypedValue typedColorBackground = new TypedValue();
|
||||
context.getTheme().resolveAttribute(android.R.attr.colorBackground, typedColorBackground, true);
|
||||
|
||||
// Return an Icon of iconId with those colors
|
||||
return generateThemedIcon(context, iconId,
|
||||
ThemeStore.primaryColor(context),
|
||||
typedColorBackground.data
|
||||
);
|
||||
}
|
||||
|
||||
private static IconCompat generateThemedIcon(Context context, int iconId, int foregroundColor, int backgroundColor) {
|
||||
// Get and tint foreground and background drawables
|
||||
Drawable vectorDrawable = ImageUtil.getTintedVectorDrawable(context, iconId, foregroundColor);
|
||||
Drawable backgroundDrawable = ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_app_shortcut_background, backgroundColor);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
AdaptiveIconDrawable adaptiveIconDrawable = new AdaptiveIconDrawable(backgroundDrawable, vectorDrawable);
|
||||
return IconCompat.createWithAdaptiveBitmap(ImageUtil.createBitmap(adaptiveIconDrawable));
|
||||
} else {
|
||||
// Squash the two drawables together
|
||||
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{backgroundDrawable, vectorDrawable});
|
||||
|
||||
// Return as an Icon
|
||||
return IconCompat.createWithBitmap(ImageUtil.createBitmap(layerDrawable));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
package com.dkanada.gramophone.views.shortcuts;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.dkanada.gramophone.views.shortcuts.shortcuttype.LatestShortcutType;
|
||||
import com.dkanada.gramophone.views.shortcuts.shortcuttype.ShuffleShortcutType;
|
||||
import com.dkanada.gramophone.views.shortcuts.shortcuttype.FrequentShortcutType;
|
||||
import com.dkanada.gramophone.model.Playlist;
|
||||
import com.dkanada.gramophone.service.MusicService;
|
||||
|
||||
public class AppShortcutLauncherActivity extends Activity {
|
||||
public static final String KEY_SHORTCUT_TYPE = "com.dkanada.gramophone.views.shortcuts.ShortcutType";
|
||||
|
||||
public static final int SHORTCUT_TYPE_SHUFFLE = 0;
|
||||
public static final int SHORTCUT_TYPE_FREQUENT = 1;
|
||||
public static final int SHORTCUT_TYPE_LATEST = 2;
|
||||
public static final int SHORTCUT_TYPE_NONE = 3;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
int shortcutType = SHORTCUT_TYPE_NONE;
|
||||
|
||||
// Set shortcutType from the intent extras
|
||||
Bundle extras = getIntent().getExtras();
|
||||
if (extras != null) {
|
||||
// noinspection WrongConstant
|
||||
shortcutType = extras.getInt(KEY_SHORTCUT_TYPE, SHORTCUT_TYPE_NONE);
|
||||
}
|
||||
|
||||
switch (shortcutType) {
|
||||
case SHORTCUT_TYPE_SHUFFLE:
|
||||
DynamicShortcutManager.reportShortcutUsed(this, ShuffleShortcutType.getId());
|
||||
break;
|
||||
case SHORTCUT_TYPE_FREQUENT:
|
||||
DynamicShortcutManager.reportShortcutUsed(this, FrequentShortcutType.getId());
|
||||
break;
|
||||
case SHORTCUT_TYPE_LATEST:
|
||||
DynamicShortcutManager.reportShortcutUsed(this, LatestShortcutType.getId());
|
||||
break;
|
||||
}
|
||||
|
||||
finish();
|
||||
}
|
||||
|
||||
private void startServiceWithPlaylist(int shuffleMode, Playlist playlist) {
|
||||
Intent intent = new Intent(this, MusicService.class);
|
||||
intent.setAction(MusicService.ACTION_PLAY_PLAYLIST);
|
||||
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putParcelable(MusicService.INTENT_EXTRA_PLAYLIST, playlist);
|
||||
bundle.putInt(MusicService.INTENT_EXTRA_SHUFFLE, shuffleMode);
|
||||
|
||||
intent.putExtras(bundle);
|
||||
|
||||
startService(intent);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package com.dkanada.gramophone.views.shortcuts;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ShortcutInfo;
|
||||
import android.content.pm.ShortcutManager;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.os.Build;
|
||||
|
||||
import com.dkanada.gramophone.views.shortcuts.shortcuttype.LatestShortcutType;
|
||||
import com.dkanada.gramophone.views.shortcuts.shortcuttype.ShuffleShortcutType;
|
||||
import com.dkanada.gramophone.views.shortcuts.shortcuttype.FrequentShortcutType;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.N_MR1)
|
||||
public class DynamicShortcutManager {
|
||||
|
||||
private Context context;
|
||||
private ShortcutManager shortcutManager;
|
||||
|
||||
public DynamicShortcutManager(Context context) {
|
||||
this.context = context;
|
||||
shortcutManager = this.context.getSystemService(ShortcutManager.class);
|
||||
}
|
||||
|
||||
public static ShortcutInfo createShortcut(Context context, String id, String shortLabel, String longLabel, Icon icon, Intent intent) {
|
||||
return new ShortcutInfo.Builder(context, id)
|
||||
.setShortLabel(shortLabel)
|
||||
.setLongLabel(longLabel)
|
||||
.setIcon(icon)
|
||||
.setIntent(intent)
|
||||
.build();
|
||||
}
|
||||
|
||||
public void initDynamicShortcuts() {
|
||||
if (shortcutManager.getDynamicShortcuts().size() == 0) {
|
||||
shortcutManager.setDynamicShortcuts(getDefaultShortcuts());
|
||||
}
|
||||
}
|
||||
|
||||
public void updateDynamicShortcuts() {
|
||||
shortcutManager.updateShortcuts(getDefaultShortcuts());
|
||||
}
|
||||
|
||||
public List<ShortcutInfo> getDefaultShortcuts() {
|
||||
return (Arrays.asList(
|
||||
new ShuffleShortcutType(context).getShortcutInfo(),
|
||||
new FrequentShortcutType(context).getShortcutInfo(),
|
||||
new LatestShortcutType(context).getShortcutInfo()
|
||||
));
|
||||
}
|
||||
|
||||
public static void reportShortcutUsed(Context context, String shortcutId){
|
||||
context.getSystemService(ShortcutManager.class).reportShortcutUsed(shortcutId);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package com.dkanada.gramophone.views.shortcuts.shortcuttype;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ShortcutInfo;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.dkanada.gramophone.views.shortcuts.AppShortcutLauncherActivity;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.N_MR1)
|
||||
public abstract class BaseShortcutType {
|
||||
|
||||
static final String ID_PREFIX = "com.dkanada.gramophone.views.shortcuts.id.";
|
||||
|
||||
Context context;
|
||||
|
||||
public BaseShortcutType(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
static public String getId() {
|
||||
return ID_PREFIX + "invalid";
|
||||
}
|
||||
|
||||
abstract ShortcutInfo getShortcutInfo();
|
||||
|
||||
/**
|
||||
* Creates an Intent that will launch MainActivtiy and immediately play {@param songs} in either shuffle or normal mode
|
||||
*
|
||||
* @param shortcutType Describes the type of shortcut to create (ShuffleAll, TopTracks, custom playlist, etc.)
|
||||
* @return
|
||||
*/
|
||||
Intent getPlaySongsIntent(int shortcutType) {
|
||||
Intent intent = new Intent(context, AppShortcutLauncherActivity.class);
|
||||
intent.setAction(Intent.ACTION_VIEW);
|
||||
|
||||
Bundle b = new Bundle();
|
||||
b.putInt(AppShortcutLauncherActivity.KEY_SHORTCUT_TYPE, shortcutType);
|
||||
|
||||
intent.putExtras(b);
|
||||
|
||||
return intent;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package com.dkanada.gramophone.views.shortcuts.shortcuttype;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ShortcutInfo;
|
||||
import android.os.Build;
|
||||
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.views.shortcuts.AppShortcutIconGenerator;
|
||||
import com.dkanada.gramophone.views.shortcuts.AppShortcutLauncherActivity;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.N_MR1)
|
||||
public final class FrequentShortcutType extends BaseShortcutType {
|
||||
public FrequentShortcutType(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public static String getId() {
|
||||
return ID_PREFIX + "top_tracks";
|
||||
}
|
||||
|
||||
public ShortcutInfo getShortcutInfo() {
|
||||
return new ShortcutInfo.Builder(context, getId())
|
||||
.setShortLabel(context.getString(R.string.my_top_tracks))
|
||||
.setIcon(AppShortcutIconGenerator.generateThemedIcon(context, R.drawable.ic_app_shortcut_top_tracks))
|
||||
.setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.SHORTCUT_TYPE_FREQUENT))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package com.dkanada.gramophone.views.shortcuts.shortcuttype;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ShortcutInfo;
|
||||
import android.os.Build;
|
||||
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.views.shortcuts.AppShortcutIconGenerator;
|
||||
import com.dkanada.gramophone.views.shortcuts.AppShortcutLauncherActivity;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.N_MR1)
|
||||
public final class LatestShortcutType extends BaseShortcutType {
|
||||
public LatestShortcutType(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public static String getId() {
|
||||
return ID_PREFIX + "last_added";
|
||||
}
|
||||
|
||||
public ShortcutInfo getShortcutInfo() {
|
||||
return new ShortcutInfo.Builder(context, getId())
|
||||
.setShortLabel(context.getString(R.string.last_added))
|
||||
.setIcon(AppShortcutIconGenerator.generateThemedIcon(context, R.drawable.ic_app_shortcut_last_added))
|
||||
.setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.SHORTCUT_TYPE_LATEST))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package com.dkanada.gramophone.views.shortcuts.shortcuttype;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ShortcutInfo;
|
||||
import android.os.Build;
|
||||
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.views.shortcuts.AppShortcutIconGenerator;
|
||||
import com.dkanada.gramophone.views.shortcuts.AppShortcutLauncherActivity;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.N_MR1)
|
||||
public final class ShuffleShortcutType extends BaseShortcutType {
|
||||
public ShuffleShortcutType(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public static String getId() {
|
||||
return ID_PREFIX + "shuffle_all";
|
||||
}
|
||||
|
||||
public ShortcutInfo getShortcutInfo() {
|
||||
return new ShortcutInfo.Builder(context, getId())
|
||||
.setShortLabel(context.getString(R.string.action_shuffle))
|
||||
.setIcon(AppShortcutIconGenerator.generateThemedIcon(context, R.drawable.ic_app_shortcut_shuffle_all))
|
||||
.setIntent(getPlaySongsIntent(AppShortcutLauncherActivity.SHORTCUT_TYPE_SHUFFLE))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,155 @@
|
|||
package com.dkanada.gramophone.views.widgets;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.target.CustomTarget;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
import com.bumptech.glide.request.transition.Transition;
|
||||
import com.kabouzeid.appthemehelper.util.MaterialValueHelper;
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.glide.CustomGlideRequest;
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
import com.dkanada.gramophone.service.MusicService;
|
||||
import com.dkanada.gramophone.activities.MainActivity;
|
||||
import com.dkanada.gramophone.util.ImageUtil;
|
||||
import com.dkanada.gramophone.util.Util;
|
||||
|
||||
public class AppWidgetAlbum extends BaseAppWidget {
|
||||
public static final String NAME = "app_widget_album";
|
||||
|
||||
private static AppWidgetAlbum mInstance;
|
||||
private Target<Bitmap> target;
|
||||
|
||||
public static synchronized AppWidgetAlbum getInstance() {
|
||||
if (mInstance == null) {
|
||||
mInstance = new AppWidgetAlbum();
|
||||
}
|
||||
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
Point p = Util.getScreenSize(context);
|
||||
|
||||
imageSize = Math.min(p.x, p.y);
|
||||
cardRadius = context.getResources().getDimension(R.dimen.app_widget_card_radius);
|
||||
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||
}
|
||||
|
||||
protected void defaultAppWidget(final Context context, final int[] appWidgetIds) {
|
||||
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.app_widget_album);
|
||||
|
||||
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
|
||||
appWidgetView.setImageViewBitmap(R.id.image, createRoundedBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.default_album_art), imageSize, imageSize, cardRadius, cardRadius, cardRadius, cardRadius));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getPrimaryTextColor(context, false))));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getPrimaryTextColor(context, false))));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_play_arrow_white_24dp, MaterialValueHelper.getPrimaryTextColor(context, false))));
|
||||
|
||||
linkButtons(context, appWidgetView);
|
||||
pushUpdate(context, appWidgetIds, appWidgetView);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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_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))));
|
||||
|
||||
linkButtons(service, appWidgetView);
|
||||
|
||||
Point p = Util.getScreenSize(service);
|
||||
imageSize = Math.min(p.x, p.y);
|
||||
cardRadius = service.getResources().getDimension(R.dimen.app_widget_card_radius);
|
||||
|
||||
service.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (target != null) {
|
||||
Glide.with(service).clear(target);
|
||||
}
|
||||
|
||||
target = CustomGlideRequest.Builder
|
||||
.from(service, song.primary, song.blurHash)
|
||||
.bitmap().build()
|
||||
.into(new CustomTarget<Bitmap>(imageSize, imageSize) {
|
||||
@Override
|
||||
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> glideAnimation) {
|
||||
update(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFailed(Drawable drawable) {
|
||||
super.onLoadFailed(drawable);
|
||||
update(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadCleared(Drawable drawable) {
|
||||
super.onLoadFailed(drawable);
|
||||
update(null);
|
||||
}
|
||||
|
||||
private void update(@Nullable Bitmap bitmap) {
|
||||
final Drawable image = getAlbumArtDrawable(service.getResources(), bitmap);
|
||||
final Bitmap roundedBitmap = createRoundedBitmap(image, imageSize, imageSize, cardRadius, cardRadius, cardRadius, cardRadius);
|
||||
|
||||
appWidgetView.setImageViewBitmap(R.id.image, roundedBitmap);
|
||||
pushUpdate(service, appWidgetIds, appWidgetView);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void linkButtons(final Context context, final RemoteViews views) {
|
||||
Intent action;
|
||||
PendingIntent pendingIntent;
|
||||
|
||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||
|
||||
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);
|
||||
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_REWIND, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,160 @@
|
|||
package com.dkanada.gramophone.views.widgets;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.palette.graphics.Palette;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.target.CustomTarget;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
import com.bumptech.glide.request.transition.Transition;
|
||||
import com.kabouzeid.appthemehelper.util.MaterialValueHelper;
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.glide.CustomGlideRequest;
|
||||
import com.dkanada.gramophone.glide.palette.BitmapPaletteWrapper;
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
import com.dkanada.gramophone.service.MusicService;
|
||||
import com.dkanada.gramophone.activities.MainActivity;
|
||||
import com.dkanada.gramophone.util.ImageUtil;
|
||||
|
||||
public class AppWidgetCard extends BaseAppWidget {
|
||||
public static final String NAME = "app_widget_card";
|
||||
|
||||
private static AppWidgetCard mInstance;
|
||||
private Target<BitmapPaletteWrapper> target;
|
||||
|
||||
public static synchronized AppWidgetCard getInstance() {
|
||||
if (mInstance == null) {
|
||||
mInstance = new AppWidgetCard();
|
||||
}
|
||||
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
imageSize = context.getResources().getDimensionPixelSize(R.dimen.app_widget_card_image_size);
|
||||
cardRadius = context.getResources().getDimension(R.dimen.app_widget_card_radius);
|
||||
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||
}
|
||||
|
||||
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.setImageViewBitmap(R.id.image, createRoundedBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.default_album_art), imageSize, imageSize, cardRadius, 0, cardRadius, 0));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true))));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true))));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_play_arrow_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true))));
|
||||
|
||||
linkButtons(context, appWidgetView);
|
||||
pushUpdate(context, appWidgetIds, appWidgetView);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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_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))));
|
||||
|
||||
linkButtons(service, appWidgetView);
|
||||
|
||||
imageSize = service.getResources().getDimensionPixelSize(R.dimen.app_widget_card_image_size);
|
||||
cardRadius = service.getResources().getDimension(R.dimen.app_widget_card_radius) / 2;
|
||||
|
||||
service.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (target != null) {
|
||||
Glide.with(service).clear(target);
|
||||
}
|
||||
|
||||
target = CustomGlideRequest.Builder
|
||||
.from(service, song.primary, song.blurHash)
|
||||
.palette().build()
|
||||
.into(new CustomTarget<BitmapPaletteWrapper>(imageSize, imageSize) {
|
||||
@Override
|
||||
public void onResourceReady(@NonNull BitmapPaletteWrapper resource, Transition<? super BitmapPaletteWrapper> glideAnimation) {
|
||||
Palette palette = resource.getPalette();
|
||||
update(resource.getBitmap(), palette.getVibrantColor(palette.getMutedColor(MaterialValueHelper.getSecondaryTextColor(service, true))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFailed(Drawable drawable) {
|
||||
super.onLoadFailed(drawable);
|
||||
update(null, MaterialValueHelper.getSecondaryTextColor(service, true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadCleared(Drawable drawable) {
|
||||
super.onLoadFailed(drawable);
|
||||
update(null, MaterialValueHelper.getSecondaryTextColor(service, true));
|
||||
}
|
||||
|
||||
private void update(@Nullable Bitmap bitmap, int color) {
|
||||
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_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)));
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void linkButtons(final Context context, final RemoteViews views) {
|
||||
Intent action;
|
||||
PendingIntent pendingIntent;
|
||||
|
||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||
|
||||
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);
|
||||
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_REWIND, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,154 @@
|
|||
package com.dkanada.gramophone.views.widgets;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.palette.graphics.Palette;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.request.target.CustomTarget;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
import com.bumptech.glide.request.transition.Transition;
|
||||
import com.kabouzeid.appthemehelper.util.MaterialValueHelper;
|
||||
import com.dkanada.gramophone.R;
|
||||
import com.dkanada.gramophone.glide.CustomGlideRequest;
|
||||
import com.dkanada.gramophone.glide.palette.BitmapPaletteWrapper;
|
||||
import com.dkanada.gramophone.model.Song;
|
||||
import com.dkanada.gramophone.service.MusicService;
|
||||
import com.dkanada.gramophone.activities.MainActivity;
|
||||
import com.dkanada.gramophone.util.ImageUtil;
|
||||
|
||||
public class AppWidgetClassic extends BaseAppWidget {
|
||||
public static final String NAME = "app_widget_classic";
|
||||
|
||||
private static AppWidgetClassic mInstance;
|
||||
private Target<BitmapPaletteWrapper> target;
|
||||
|
||||
public static synchronized AppWidgetClassic getInstance() {
|
||||
if (mInstance == null) {
|
||||
mInstance = new AppWidgetClassic();
|
||||
}
|
||||
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
imageSize = context.getResources().getDimensionPixelSize(R.dimen.app_widget_classic_image_size);
|
||||
cardRadius = context.getResources().getDimension(R.dimen.app_widget_card_radius);
|
||||
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||
}
|
||||
|
||||
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.setImageViewBitmap(R.id.image, createRoundedBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.default_album_art), imageSize, imageSize, cardRadius, 0, cardRadius, 0));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true))));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true))));
|
||||
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(context, R.drawable.ic_play_arrow_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true))));
|
||||
|
||||
linkButtons(context, appWidgetView);
|
||||
pushUpdate(context, appWidgetIds, appWidgetView);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
linkButtons(service, appWidgetView);
|
||||
|
||||
imageSize = service.getResources().getDimensionPixelSize(R.dimen.app_widget_classic_image_size);
|
||||
cardRadius = service.getResources().getDimension(R.dimen.app_widget_card_radius);
|
||||
|
||||
service.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (target != null) {
|
||||
Glide.with(service).clear(target);
|
||||
}
|
||||
|
||||
target = CustomGlideRequest.Builder
|
||||
.from(service, song.primary, song.blurHash)
|
||||
.palette().build()
|
||||
.into(new CustomTarget<BitmapPaletteWrapper>(imageSize, imageSize) {
|
||||
@Override
|
||||
public void onResourceReady(@NonNull BitmapPaletteWrapper resource, Transition<? super BitmapPaletteWrapper> glideAnimation) {
|
||||
Palette palette = resource.getPalette();
|
||||
update(resource.getBitmap(), palette.getVibrantColor(palette.getMutedColor(MaterialValueHelper.getSecondaryTextColor(service, true))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFailed(Drawable drawable) {
|
||||
super.onLoadFailed(drawable);
|
||||
update(null, MaterialValueHelper.getSecondaryTextColor(service, true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadCleared(Drawable drawable) {
|
||||
super.onLoadFailed(drawable);
|
||||
update(null, MaterialValueHelper.getSecondaryTextColor(service, true));
|
||||
}
|
||||
|
||||
private void update(@Nullable Bitmap bitmap, int color) {
|
||||
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_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)));
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void linkButtons(final Context context, final RemoteViews views) {
|
||||
Intent action;
|
||||
PendingIntent pendingIntent;
|
||||
|
||||
final ComponentName serviceName = new ComponentName(context, MusicService.class);
|
||||
|
||||
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);
|
||||
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_REWIND, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
|
||||
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
|
||||
|
||||
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
|
||||
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
package com.dkanada.gramophone.views.widgets;
|
||||
|
||||
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.util.Log;
|
||||
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;
|
||||
import com.dkanada.gramophone.util.MusicUtil;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public abstract class BaseAppWidget extends AppWidgetProvider {
|
||||
public static final String NAME = "app_widget";
|
||||
|
||||
public int imageSize = 0;
|
||||
public float cardRadius = 0f;
|
||||
|
||||
@Override
|
||||
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
|
||||
Log.d(NAME, String.format("onUpdate: %s", Arrays.toString(appWidgetIds)));
|
||||
defaultAppWidget(context, appWidgetIds);
|
||||
|
||||
final Intent updateIntent = new Intent(MusicService.INTENT_EXTRA_WIDGET_UPDATE);
|
||||
|
||||
updateIntent.putExtra(MusicService.INTENT_EXTRA_WIDGET_NAME, NAME);
|
||||
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
|
||||
updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
|
||||
|
||||
context.sendBroadcast(updateIntent);
|
||||
}
|
||||
|
||||
public void notifyChange(final MusicService service, final String what) {
|
||||
if (hasInstances(service)) {
|
||||
if (MusicService.META_CHANGED.equals(what) || MusicService.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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
protected static Bitmap createRoundedBitmap(Bitmap bitmap, int width, int height, float tl, float tr, float bl, float br) {
|
||||
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 Bitmap createRoundedBitmap(Drawable drawable, int width, int height, float tl, float tr, float bl, float br) {
|
||||
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
|
||||
drawable.setBounds(0, 0, width, height);
|
||||
drawable.draw(canvas);
|
||||
|
||||
return createRoundedBitmap(bitmap, width, height, tl, tr, bl, br);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (bitmap == null) {
|
||||
return ResourcesCompat.getDrawable(resources, R.drawable.default_album_art, null);
|
||||
} else {
|
||||
return new BitmapDrawable(resources, bitmap);
|
||||
}
|
||||
}
|
||||
|
||||
protected String getSongArtistAndAlbum(final Song song) {
|
||||
return MusicUtil.getSongInfoString(song);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package com.dkanada.gramophone.views.widgets;
|
||||
|
||||
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 com.dkanada.gramophone.service.MusicService;
|
||||
|
||||
public class BootReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(final 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, AppWidgetAlbum.class)).length > 0
|
||||
|| widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetClassic.class)).length > 0
|
||||
|| widgetManager.getAppWidgetIds(new ComponentName(context, AppWidgetCard.class)).length > 0) {
|
||||
final Intent serviceIntent = new Intent(context, MusicService.class);
|
||||
|
||||
// not allowed on oreo
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
||||
context.startService(serviceIntent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue