improved notification style

This commit is contained in:
h4h13 2018-07-29 23:05:44 +05:30
parent 29e2a3958f
commit d8fc32ff30
3 changed files with 295 additions and 295 deletions

View file

@ -14,7 +14,8 @@ import code.name.monkey.retromusic.service.MusicService;
public abstract class PlayingNotification { public abstract class PlayingNotification {
static final String NOTIFICATION_CHANNEL_ID = "playing_notification"; protected static final float NOTIFICATION_CONTROLS_SIZE_MULTIPLIER = 1.0f;
static final String NOTIFICATION_CHANNEL_ID = "playing_notification";
private static final int NOTIFICATION_ID = 1; private static final int NOTIFICATION_ID = 1;
private static final int NOTIFY_MODE_FOREGROUND = 1; private static final int NOTIFY_MODE_FOREGROUND = 1;
private static final int NOTIFY_MODE_BACKGROUND = 0; private static final int NOTIFY_MODE_BACKGROUND = 0;

View file

@ -1,5 +1,10 @@
package code.name.monkey.retromusic.service.notification; package code.name.monkey.retromusic.service.notification;
import static code.name.monkey.retromusic.Constants.ACTION_QUIT;
import static code.name.monkey.retromusic.Constants.ACTION_REWIND;
import static code.name.monkey.retromusic.Constants.ACTION_SKIP;
import static code.name.monkey.retromusic.Constants.ACTION_TOGGLE_PAUSE;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Intent; import android.content.Intent;
@ -11,11 +16,6 @@ import android.os.Build;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.media.app.NotificationCompat.MediaStyle; import android.support.v4.media.app.NotificationCompat.MediaStyle;
import android.text.Html; import android.text.Html;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import code.name.monkey.retromusic.Constants; import code.name.monkey.retromusic.Constants;
import code.name.monkey.retromusic.R; import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.RetroApplication; import code.name.monkey.retromusic.RetroApplication;
@ -26,121 +26,118 @@ import code.name.monkey.retromusic.service.MusicService;
import code.name.monkey.retromusic.ui.activities.MainActivity; import code.name.monkey.retromusic.ui.activities.MainActivity;
import code.name.monkey.retromusic.util.PreferenceUtil; import code.name.monkey.retromusic.util.PreferenceUtil;
import code.name.monkey.retromusic.util.RetroColorUtil; import code.name.monkey.retromusic.util.RetroColorUtil;
import com.bumptech.glide.Glide;
import static code.name.monkey.retromusic.Constants.ACTION_QUIT; import com.bumptech.glide.request.animation.GlideAnimation;
import static code.name.monkey.retromusic.Constants.ACTION_REWIND; import com.bumptech.glide.request.target.SimpleTarget;
import static code.name.monkey.retromusic.Constants.ACTION_SKIP;
import static code.name.monkey.retromusic.Constants.ACTION_TOGGLE_PAUSE;
public class PlayingNotificationImpl24 extends PlayingNotification { public class PlayingNotificationImpl24 extends PlayingNotification {
@Override @Override
public synchronized void update() { public synchronized void update() {
stopped = false; stopped = false;
final Song song = service.getCurrentSong(); final Song song = service.getCurrentSong();
final boolean isPlaying = service.isPlaying(); final boolean isPlaying = service.isPlaying();
final int playButtonResId = isPlaying ? R.drawable.ic_pause_white_24dp :
R.drawable.ic_play_arrow_white_24dp;
final int playButtonResId = isPlaying ? R.drawable.ic_pause_white_24dp : Intent action = new Intent(service, MainActivity.class);
R.drawable.ic_play_arrow_white_24dp; action.putExtra("expand", true);
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
final PendingIntent clickIntent = PendingIntent
.getActivity(service, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
Intent action = new Intent(service, MainActivity.class); final ComponentName serviceName = new ComponentName(service, MusicService.class);
action.putExtra("expand", true); Intent intent = new Intent(Constants.ACTION_QUIT);
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setComponent(serviceName);
final PendingIntent clickIntent = PendingIntent final PendingIntent deleteIntent = PendingIntent.getService(service, 0, intent, 0);
.getActivity(service, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
final ComponentName serviceName = new ComponentName(service, MusicService.class); final int bigNotificationImageSize = service.getResources()
Intent intent = new Intent(Constants.ACTION_QUIT); .getDimensionPixelSize(R.dimen.notification_big_image_size);
intent.setComponent(serviceName); service.runOnUiThread(() -> SongGlideRequest.Builder.from(Glide.with(service), song)
final PendingIntent deleteIntent = PendingIntent.getService(service, 0, intent, 0); .checkIgnoreMediaStore(service)
.generatePalette(service).build()
.into(new SimpleTarget<BitmapPaletteWrapper>(bigNotificationImageSize,
bigNotificationImageSize) {
@Override
public void onResourceReady(BitmapPaletteWrapper resource,
GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
update(resource.getBitmap(),
PreferenceUtil.getInstance(RetroApplication.getInstance()).isDominantColor() ?
RetroColorUtil.getDominantColor(resource.getBitmap(), Color.TRANSPARENT) :
RetroColorUtil.getColor(resource.getPalette(), Color.TRANSPARENT));
}
final int bigNotificationImageSize = service.getResources() @Override
.getDimensionPixelSize(R.dimen.notification_big_image_size); public void onLoadFailed(Exception e, Drawable errorDrawable) {
service.runOnUiThread(() -> SongGlideRequest.Builder.from(Glide.with(service), song) update(null, Color.TRANSPARENT);
.checkIgnoreMediaStore(service) }
.generatePalette(service).build()
.into(new SimpleTarget<BitmapPaletteWrapper>(bigNotificationImageSize,
bigNotificationImageSize) {
@Override
public void onResourceReady(BitmapPaletteWrapper resource,
GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
update(resource.getBitmap(),
PreferenceUtil.getInstance(RetroApplication.getInstance()).isDominantColor() ?
RetroColorUtil.getDominantColor(resource.getBitmap(), Color.TRANSPARENT) :
RetroColorUtil.getColor(resource.getPalette(), Color.TRANSPARENT));
}
@Override void update(Bitmap bitmap, int color) {
public void onLoadFailed(Exception e, Drawable errorDrawable) { if (bitmap == null) {
update(null, Color.TRANSPARENT); bitmap = BitmapFactory
} .decodeResource(service.getResources(), R.drawable.default_album_art);
}
NotificationCompat.Action playPauseAction = new NotificationCompat.Action(
playButtonResId,
service.getString(R.string.action_play_pause),
retrievePlaybackAction(ACTION_TOGGLE_PAUSE));
void update(Bitmap bitmap, int color) { NotificationCompat.Action closeAction = new NotificationCompat.Action(
if (bitmap == null) { R.drawable.ic_close_white_24dp,
bitmap = BitmapFactory service.getString(R.string.close_notification),
.decodeResource(service.getResources(), R.drawable.default_album_art); retrievePlaybackAction(ACTION_QUIT));
}
NotificationCompat.Action playPauseAction = new NotificationCompat.Action(
playButtonResId,
service.getString(R.string.action_play_pause),
retrievePlaybackAction(ACTION_TOGGLE_PAUSE));
NotificationCompat.Action closeAction = new NotificationCompat.Action( NotificationCompat.Action previousAction = new NotificationCompat.Action(
R.drawable.ic_close_white_24dp, R.drawable.ic_skip_previous_white_24dp,
service.getString(R.string.close_notification), service.getString(R.string.action_previous),
retrievePlaybackAction(ACTION_QUIT)); retrievePlaybackAction(ACTION_REWIND));
NotificationCompat.Action previousAction = new NotificationCompat.Action( NotificationCompat.Action nextAction = new NotificationCompat.Action(
R.drawable.ic_skip_previous_white_24dp, R.drawable.ic_skip_next_white_24dp,
service.getString(R.string.action_previous), service.getString(R.string.action_next),
retrievePlaybackAction(ACTION_REWIND)); retrievePlaybackAction(ACTION_SKIP));
NotificationCompat.Action nextAction = new NotificationCompat.Action( NotificationCompat.Builder builder = new NotificationCompat.Builder(service,
R.drawable.ic_skip_next_white_24dp, NOTIFICATION_CHANNEL_ID)
service.getString(R.string.action_next), .setSmallIcon(R.drawable.ic_notification)
retrievePlaybackAction(ACTION_SKIP)); .setLargeIcon(bitmap)
.setContentIntent(clickIntent)
.setDeleteIntent(deleteIntent)
.setContentTitle(Html.fromHtml("<b>" + song.title + "</b>"))
.setContentText(song.artistName)
.setSubText(Html.fromHtml("<b>" + song.albumName + "</b>"))
.setOngoing(isPlaying)
.setShowWhen(false)
.addAction(previousAction)
.addAction(playPauseAction)
.addAction(nextAction)
.addAction(closeAction);
NotificationCompat.Builder builder = new NotificationCompat.Builder(service, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
NOTIFICATION_CHANNEL_ID) builder.setStyle(new MediaStyle()
.setSmallIcon(R.drawable.ic_notification) .setMediaSession(service.getMediaSession().getSessionToken())
.setLargeIcon(bitmap) .setShowActionsInCompactView(0, 1, 2, 3, 4))
.setContentIntent(clickIntent) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
.setDeleteIntent(deleteIntent) if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O && PreferenceUtil
.setContentTitle(Html.fromHtml("<b>" + song.title + "</b>")) .getInstance(service).coloredNotification()) {
.setContentText(song.artistName) builder.setColor(color);
.setSubText(Html.fromHtml("<b>" + song.albumName + "</b>")) }
.setOngoing(isPlaying) }
.setShowWhen(false)
.addAction(previousAction)
.addAction(playPauseAction)
.addAction(nextAction)
.addAction(closeAction);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (stopped) {
builder.setStyle(new MediaStyle() return; // notification has been stopped before loading was finished
.setMediaSession(service.getMediaSession().getSessionToken()) }
.setShowActionsInCompactView(0, 1, 2, 3, 4)) updateNotifyModeAndPostNotification(builder.build());
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); }
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O && PreferenceUtil }));
.getInstance(service).coloredNotification()) { }
builder.setColor(color);
}
}
if (stopped) { private PendingIntent retrievePlaybackAction(final String action) {
return; // notification has been stopped before loading was finished final ComponentName serviceName = new ComponentName(service, MusicService.class);
} Intent intent = new Intent(action);
updateNotifyModeAndPostNotification(builder.build()); intent.setComponent(serviceName);
} return PendingIntent.getService(service, 0, intent, 0);
})); }
}
private PendingIntent retrievePlaybackAction(final String action) {
final ComponentName serviceName = new ComponentName(service, MusicService.class);
Intent intent = new Intent(action);
intent.setComponent(serviceName);
return PendingIntent.getService(service, 0, intent, 0);
}
} }

View file

@ -1,5 +1,11 @@
package code.name.monkey.retromusic.service.notification; package code.name.monkey.retromusic.service.notification;
import static code.name.monkey.retromusic.Constants.ACTION_QUIT;
import static code.name.monkey.retromusic.Constants.ACTION_REWIND;
import static code.name.monkey.retromusic.Constants.ACTION_SKIP;
import static code.name.monkey.retromusic.Constants.ACTION_TOGGLE_PAUSE;
import static code.name.monkey.retromusic.util.RetroUtil.createBitmap;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
@ -11,12 +17,6 @@ import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import code.name.monkey.appthemehelper.util.ColorUtil; import code.name.monkey.appthemehelper.util.ColorUtil;
import code.name.monkey.appthemehelper.util.MaterialValueHelper; import code.name.monkey.appthemehelper.util.MaterialValueHelper;
import code.name.monkey.retromusic.R; import code.name.monkey.retromusic.R;
@ -28,217 +28,219 @@ import code.name.monkey.retromusic.ui.activities.MainActivity;
import code.name.monkey.retromusic.util.PreferenceUtil; import code.name.monkey.retromusic.util.PreferenceUtil;
import code.name.monkey.retromusic.util.RetroUtil; import code.name.monkey.retromusic.util.RetroUtil;
import code.name.monkey.retromusic.util.color.MediaNotificationProcessor; import code.name.monkey.retromusic.util.color.MediaNotificationProcessor;
import com.bumptech.glide.Glide;
import static code.name.monkey.retromusic.Constants.ACTION_QUIT; import com.bumptech.glide.request.animation.GlideAnimation;
import static code.name.monkey.retromusic.Constants.ACTION_REWIND; import com.bumptech.glide.request.target.SimpleTarget;
import static code.name.monkey.retromusic.Constants.ACTION_SKIP; import com.bumptech.glide.request.target.Target;
import static code.name.monkey.retromusic.Constants.ACTION_TOGGLE_PAUSE;
import static code.name.monkey.retromusic.util.RetroUtil.createBitmap;
/** /**
* @author Hemanth S (h4h13). * @author Hemanth S (h4h13).
*/ */
public class PlayingNotificationOreo extends PlayingNotification { public class PlayingNotificationOreo extends PlayingNotification {
private Target<BitmapPaletteWrapper> target; private Target<BitmapPaletteWrapper> target;
private RemoteViews getCombinedRemoteViews(boolean collapsed, Song song) { private RemoteViews getCombinedRemoteViews(boolean collapsed, Song song) {
RemoteViews remoteViews = new RemoteViews(service.getPackageName(), RemoteViews remoteViews = new RemoteViews(service.getPackageName(),
collapsed ? R.layout.layout_notification_collapsed : R.layout.layout_notification_expanded); collapsed ? R.layout.layout_notification_collapsed : R.layout.layout_notification_expanded);
remoteViews.setTextViewText(R.id.appName, service.getString(R.string.app_name) + "" + song.albumName); remoteViews.setTextViewText(R.id.appName,
remoteViews.setTextViewText(R.id.title, song.title); service.getString(R.string.app_name) + "" + song.albumName);
remoteViews.setTextViewText(R.id.subtitle, song.artistName); remoteViews.setTextViewText(R.id.title, song.title);
remoteViews.setTextViewText(R.id.subtitle, song.artistName);
TypedArray typedArray = service TypedArray typedArray = service
.obtainStyledAttributes(new int[]{android.R.attr.selectableItemBackground}); .obtainStyledAttributes(new int[]{android.R.attr.selectableItemBackground});
int selectableItemBackground = typedArray.getResourceId(0, 0); int selectableItemBackground = typedArray.getResourceId(0, 0);
typedArray.recycle(); typedArray.recycle();
remoteViews.setInt(R.id.content, "setBackgroundResource", selectableItemBackground); remoteViews.setInt(R.id.content, "setBackgroundResource", selectableItemBackground);
linkButtons(remoteViews); linkButtons(remoteViews);
//setNotificationContent(remoteViews, ColorUtil.isColorLight(backgroundColor)); //setNotificationContent(remoteViews, ColorUtil.isColorLight(backgroundColor));
return remoteViews; return remoteViews;
} }
@Override @Override
public void update() { public void update() {
stopped = false; stopped = false;
final Song song = service.getCurrentSong(); final Song song = service.getCurrentSong();
final boolean isPlaying = service.isPlaying(); final boolean isPlaying = service.isPlaying();
final RemoteViews notificationLayout = getCombinedRemoteViews(true, song); final RemoteViews notificationLayout = getCombinedRemoteViews(true, song);
final RemoteViews notificationLayoutBig = getCombinedRemoteViews(false, song); final RemoteViews notificationLayoutBig = getCombinedRemoteViews(false, song);
Intent action = new Intent(service, MainActivity.class); Intent action = new Intent(service, MainActivity.class);
action.putExtra("expand", true); action.putExtra("expand", true);
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
final PendingIntent clickIntent = PendingIntent final PendingIntent clickIntent = PendingIntent
.getActivity(service, 0, action, PendingIntent.FLAG_UPDATE_CURRENT); .getActivity(service, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent deleteIntent = buildPendingIntent(service, ACTION_QUIT, null); final PendingIntent deleteIntent = buildPendingIntent(service, ACTION_QUIT, null);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(service, final NotificationCompat.Builder builder = new NotificationCompat.Builder(service,
NOTIFICATION_CHANNEL_ID) NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setContentIntent(clickIntent) .setContentIntent(clickIntent)
.setDeleteIntent(deleteIntent) .setDeleteIntent(deleteIntent)
.setCategory(NotificationCompat.CATEGORY_SERVICE) .setCategory(NotificationCompat.CATEGORY_SERVICE)
.setPriority(NotificationCompat.PRIORITY_MAX) .setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setCustomContentView(notificationLayout) .setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutBig) .setCustomBigContentView(notificationLayoutBig)
.setOngoing(isPlaying); .setOngoing(isPlaying);
final int bigNotificationImageSize = service.getResources() final int bigNotificationImageSize = service.getResources()
.getDimensionPixelSize(R.dimen.notification_big_image_size); .getDimensionPixelSize(R.dimen.notification_big_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(service), song)
.checkIgnoreMediaStore(service)
.generatePalette(service).build()
.into(new SimpleTarget<BitmapPaletteWrapper>(bigNotificationImageSize,
bigNotificationImageSize) {
@Override
public void onResourceReady(BitmapPaletteWrapper resource,
GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
MediaNotificationProcessor mediaNotificationProcessor = new MediaNotificationProcessor(service, service, new MediaNotificationProcessor.onColorThing() {
@Override
public void bothColor(int i, int i2) {
update(resource.getBitmap(), i, i2);
}
});
mediaNotificationProcessor.processNotification(resource.getBitmap());
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
update(null, Color.WHITE, Color.BLACK);
}
private void update(@Nullable Bitmap bitmap, int bgColor, int textColor) {
if (bitmap != null) {
notificationLayout.setImageViewBitmap(R.id.largeIcon, bitmap);
notificationLayoutBig.setImageViewBitmap(R.id.largeIcon, bitmap);
} else {
notificationLayout
.setImageViewResource(R.id.largeIcon, R.drawable.default_album_art);
notificationLayoutBig
.setImageViewResource(R.id.largeIcon, R.drawable.default_album_art);
}
if (!PreferenceUtil.getInstance(service).coloredNotification()) {
bgColor = Color.WHITE;
}
setBackgroundColor(bgColor);
setNotificationContent(ColorUtil.isColorLight(bgColor));
if (stopped) {
return; // notification has been stopped before loading was finished
}
updateNotifyModeAndPostNotification(builder.build());
}
private void setBackgroundColor(int color) {
notificationLayout.setInt(R.id.image, "setBackgroundColor", color);
notificationLayoutBig.setInt(R.id.image, "setBackgroundColor", color);
notificationLayout.setInt(R.id.foregroundImage, "setColorFilter", color);
notificationLayoutBig.setInt(R.id.foregroundImage, "setColorFilter", color);
}
private void setNotificationContent(boolean dark) {
int primary = MaterialValueHelper.getPrimaryTextColor(service, dark);
int secondary = MaterialValueHelper.getSecondaryTextColor(service, dark);
Bitmap close = createBitmap(
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_close_white_24dp, primary),
1.3f);
Bitmap prev = createBitmap(
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp,
primary), 1.3f);
Bitmap next = createBitmap(
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp,
primary), 1.3f);
Bitmap playPause = createBitmap(RetroUtil.getTintedVectorDrawable(service,
isPlaying ? R.drawable.ic_pause_white_24dp
: R.drawable.ic_play_arrow_white_24dp, primary), 1.3f);
notificationLayout.setTextColor(R.id.title, primary);
notificationLayout.setTextColor(R.id.subtitle, secondary);
notificationLayout.setTextColor(R.id.appName, secondary);
notificationLayout.setImageViewBitmap(R.id.action_prev, prev);
notificationLayout.setImageViewBitmap(R.id.action_next, next);
notificationLayout.setImageViewBitmap(R.id.action_play_pause, playPause);
notificationLayoutBig.setTextColor(R.id.title, primary);
notificationLayoutBig.setTextColor(R.id.subtitle, secondary);
notificationLayoutBig.setTextColor(R.id.appName, secondary);
notificationLayoutBig.setImageViewBitmap(R.id.action_quit, close);
notificationLayoutBig.setImageViewBitmap(R.id.action_prev, prev);
notificationLayoutBig.setImageViewBitmap(R.id.action_next, next);
notificationLayoutBig.setImageViewBitmap(R.id.action_play_pause, playPause);
notificationLayout.setImageViewBitmap(R.id.smallIcon,
createBitmap(RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_notification, secondary), 0.6f));
notificationLayoutBig.setImageViewBitmap(R.id.smallIcon,
createBitmap(RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_notification, secondary), 0.6f));
notificationLayout.setInt(R.id.arrow, "setColorFilter", secondary);
notificationLayoutBig.setInt(R.id.arrow, "setColorFilter", secondary);
}
});
}
});
if (stopped) {
return; // notification has been stopped before loading was finished
} }
updateNotifyModeAndPostNotification(builder.build()); target = SongGlideRequest.Builder.from(Glide.with(service), song)
.checkIgnoreMediaStore(service)
.generatePalette(service).build()
.into(new SimpleTarget<BitmapPaletteWrapper>(bigNotificationImageSize,
bigNotificationImageSize) {
@Override
public void onResourceReady(BitmapPaletteWrapper resource,
GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
MediaNotificationProcessor mediaNotificationProcessor = new MediaNotificationProcessor(
service, service, (i, i2) -> update(resource.getBitmap(), i, i2));
mediaNotificationProcessor.processNotification(resource.getBitmap());
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
update(null, Color.WHITE, Color.BLACK);
}
private void update(@Nullable Bitmap bitmap, int bgColor, int textColor) {
if (bitmap != null) {
notificationLayout.setImageViewBitmap(R.id.largeIcon, bitmap);
notificationLayoutBig.setImageViewBitmap(R.id.largeIcon, bitmap);
} else {
notificationLayout
.setImageViewResource(R.id.largeIcon, R.drawable.default_album_art);
notificationLayoutBig
.setImageViewResource(R.id.largeIcon, R.drawable.default_album_art);
}
if (!PreferenceUtil.getInstance(service).coloredNotification()) {
bgColor = Color.WHITE;
}
setBackgroundColor(bgColor);
setNotificationContent(ColorUtil.isColorLight(bgColor));
if (stopped) {
return; // notification has been stopped before loading was finished
}
updateNotifyModeAndPostNotification(builder.build());
}
private void setBackgroundColor(int color) {
notificationLayout.setInt(R.id.image, "setBackgroundColor", color);
notificationLayoutBig.setInt(R.id.image, "setBackgroundColor", color);
notificationLayout.setInt(R.id.foregroundImage, "setColorFilter", color);
notificationLayoutBig.setInt(R.id.foregroundImage, "setColorFilter", color);
}
private void setNotificationContent(boolean dark) {
int primary = MaterialValueHelper.getPrimaryTextColor(service, dark);
int secondary = MaterialValueHelper.getSecondaryTextColor(service, dark);
Bitmap close = createBitmap(
RetroUtil
.getTintedVectorDrawable(service, R.drawable.ic_close_white_24dp, primary),
NOTIFICATION_CONTROLS_SIZE_MULTIPLIER);
Bitmap prev = createBitmap(
RetroUtil
.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp,
primary), NOTIFICATION_CONTROLS_SIZE_MULTIPLIER);
Bitmap next = createBitmap(
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp,
primary), NOTIFICATION_CONTROLS_SIZE_MULTIPLIER);
Bitmap playPause = createBitmap(RetroUtil.getTintedVectorDrawable(service,
isPlaying ? R.drawable.ic_pause_white_24dp
: R.drawable.ic_play_arrow_white_24dp, primary),
NOTIFICATION_CONTROLS_SIZE_MULTIPLIER);
notificationLayout.setTextColor(R.id.title, primary);
notificationLayout.setTextColor(R.id.subtitle, secondary);
notificationLayout.setTextColor(R.id.appName, secondary);
notificationLayout.setImageViewBitmap(R.id.action_prev, prev);
notificationLayout.setImageViewBitmap(R.id.action_next, next);
notificationLayout.setImageViewBitmap(R.id.action_play_pause, playPause);
notificationLayoutBig.setTextColor(R.id.title, primary);
notificationLayoutBig.setTextColor(R.id.subtitle, secondary);
notificationLayoutBig.setTextColor(R.id.appName, secondary);
notificationLayoutBig.setImageViewBitmap(R.id.action_quit, close);
notificationLayoutBig.setImageViewBitmap(R.id.action_prev, prev);
notificationLayoutBig.setImageViewBitmap(R.id.action_next, next);
notificationLayoutBig.setImageViewBitmap(R.id.action_play_pause, playPause);
notificationLayout.setImageViewBitmap(R.id.smallIcon,
createBitmap(RetroUtil
.getTintedVectorDrawable(service, R.drawable.ic_notification, secondary),
0.6f));
notificationLayoutBig.setImageViewBitmap(R.id.smallIcon,
createBitmap(RetroUtil
.getTintedVectorDrawable(service, R.drawable.ic_notification, secondary),
0.6f));
notificationLayout.setInt(R.id.arrow, "setColorFilter", secondary);
notificationLayoutBig.setInt(R.id.arrow, "setColorFilter", secondary);
}
});
}
});
if (stopped) {
return; // notification has been stopped before loading was finished
} }
updateNotifyModeAndPostNotification(builder.build());
}
private PendingIntent buildPendingIntent(Context context, final String action, private PendingIntent buildPendingIntent(Context context, final String action,
final ComponentName serviceName) { final ComponentName serviceName) {
Intent intent = new Intent(action); Intent intent = new Intent(action);
intent.setComponent(serviceName); intent.setComponent(serviceName);
return PendingIntent.getService(context, 0, intent, 0); return PendingIntent.getService(context, 0, intent, 0);
} }
private void linkButtons(final RemoteViews notificationLayout) { private void linkButtons(final RemoteViews notificationLayout) {
PendingIntent pendingIntent; PendingIntent pendingIntent;
final ComponentName serviceName = new ComponentName(service, MusicService.class); final ComponentName serviceName = new ComponentName(service, MusicService.class);
// Previous track // Previous track
pendingIntent = buildPendingIntent(service, ACTION_REWIND, serviceName); pendingIntent = buildPendingIntent(service, ACTION_REWIND, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_prev, pendingIntent); notificationLayout.setOnClickPendingIntent(R.id.action_prev, pendingIntent);
// Play and pause // Play and pause
pendingIntent = buildPendingIntent(service, ACTION_TOGGLE_PAUSE, serviceName); pendingIntent = buildPendingIntent(service, ACTION_TOGGLE_PAUSE, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_play_pause, pendingIntent); notificationLayout.setOnClickPendingIntent(R.id.action_play_pause, pendingIntent);
// Next track // Next track
pendingIntent = buildPendingIntent(service, ACTION_SKIP, serviceName); pendingIntent = buildPendingIntent(service, ACTION_SKIP, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_next, pendingIntent); notificationLayout.setOnClickPendingIntent(R.id.action_next, pendingIntent);
// Close // Close
pendingIntent = buildPendingIntent(service, ACTION_QUIT, serviceName); pendingIntent = buildPendingIntent(service, ACTION_QUIT, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_quit, pendingIntent); notificationLayout.setOnClickPendingIntent(R.id.action_quit, pendingIntent);
} }
} }