This commit is contained in:
h4h13 2018-11-05 19:23:07 +05:30
parent 3d7ba2afc6
commit 08e00b89c5
341 changed files with 7612 additions and 6811 deletions

View file

@ -115,7 +115,7 @@ public class MultiPlayer implements Playback, MediaPlayer.OnErrorListener, Media
if (path == null) {
return;
}
if (PreferenceUtil.getInstance(context).gaplessPlayback()) {
if (PreferenceUtil.getInstance().gaplessPlayback()) {
mNextMediaPlayer = new MediaPlayer();
mNextMediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK);
mNextMediaPlayer.setAudioSessionId(getAudioSessionId());

View file

@ -27,8 +27,6 @@ import android.os.PowerManager;
import android.os.Process;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
@ -37,7 +35,6 @@ import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import code.name.monkey.retromusic.service.notification.PlayingNotificationOreo;
import com.bumptech.glide.BitmapRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
@ -48,6 +45,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.appwidgets.AppWidgetBig;
import code.name.monkey.retromusic.appwidgets.AppWidgetCard;
@ -66,6 +65,7 @@ import code.name.monkey.retromusic.providers.MusicPlaybackQueueStore;
import code.name.monkey.retromusic.providers.SongPlayCountStore;
import code.name.monkey.retromusic.service.notification.PlayingNotification;
import code.name.monkey.retromusic.service.notification.PlayingNotificationImpl24;
import code.name.monkey.retromusic.service.notification.PlayingNotificationOreo;
import code.name.monkey.retromusic.service.playback.Playback;
import code.name.monkey.retromusic.util.MusicUtil;
import code.name.monkey.retromusic.util.PreferenceUtil;
@ -295,7 +295,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
getContentResolver().registerContentObserver(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, mediaStoreObserver);
PreferenceUtil.getInstance(this).registerOnSharedPreferenceChangedListener(this);
PreferenceUtil.getInstance().registerOnSharedPreferenceChangedListener(this);
restoreState();
@ -446,7 +446,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
quit();
releaseResources();
getContentResolver().unregisterContentObserver(mediaStoreObserver);
PreferenceUtil.getInstance(this).unregisterOnSharedPreferenceChangedListener(this);
PreferenceUtil.getInstance().unregisterOnSharedPreferenceChangedListener(this);
wakeLock.release();
sendBroadcast(new Intent("code.name.monkey.retromusic.RETRO_MUSIC_MUSIC_SERVICE_DESTROYED"));
@ -627,7 +627,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
}
public void initNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !PreferenceUtil.getInstance(this).classicNotification()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !PreferenceUtil.getInstance().classicNotification()) {
playingNotification = new PlayingNotificationImpl24();
} else {
playingNotification = new PlayingNotificationOreo();
@ -672,12 +672,12 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
metaData.putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getPlayingQueue().size());
}
if (PreferenceUtil.getInstance(this).albumArtOnLockscreen()) {
if (PreferenceUtil.getInstance().albumArtOnLockscreen()) {
final Point screenSize = RetroUtil.getScreenSize(MusicService.this);
final BitmapRequestBuilder<?, Bitmap> request = SongGlideRequest.Builder.from(Glide.with(MusicService.this), song)
.checkIgnoreMediaStore(MusicService.this)
.asBitmap().build();
if (PreferenceUtil.getInstance(this).blurredAlbumArt()) {
if (PreferenceUtil.getInstance().blurredAlbumArt()) {
request.transform(new BlurTransformation.Builder(MusicService.this).build());
}
runOnUiThread(new Runnable() {
@ -1090,16 +1090,12 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
final Song song = getCurrentSong();
intent.putExtra("id", song.id);
intent.putExtra("artist", song.artistName);
intent.putExtra("album", song.albumName);
intent.putExtra("track", song.title);
intent.putExtra("duration", song.duration);
intent.putExtra("position", (long) getSongProgressMillis());
intent.putExtra("playing", isPlaying());
intent.putExtra("scrobbling_source", RETRO_MUSIC_PACKAGE_NAME);
sendStickyBroadcast(intent);
@ -1196,7 +1192,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
}
private void registerHeadsetEvents() {
if (!headsetReceiverRegistered && PreferenceUtil.getInstance(this).getHeadsetPlugged()) {
if (!headsetReceiverRegistered && PreferenceUtil.getInstance().getHeadsetPlugged()) {
registerReceiver(headsetReceiver, headsetReceiverIntentFilter);
headsetReceiverRegistered = true;
}
@ -1252,7 +1248,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
switch (msg.what) {
case DUCK:
if (PreferenceUtil.getInstance(service).audioDucking()) {
if (PreferenceUtil.getInstance().audioDucking()) {
currentDuckVolume -= .05f;
if (currentDuckVolume > .2f) {
sendEmptyMessageDelayed(DUCK, 10);
@ -1266,7 +1262,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
break;
case UNDUCK:
if (PreferenceUtil.getInstance(service).audioDucking()) {
if (PreferenceUtil.getInstance().audioDucking()) {
currentDuckVolume += .03f;
if (currentDuckVolume < 1f) {
sendEmptyMessageDelayed(UNDUCK, 10);
@ -1444,4 +1440,4 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
sendPublicIntent(PLAY_STATE_CHANGED); // for musixmatch synced lyrics
}
}
}
}

View file

@ -1,81 +1,82 @@
package code.name.monkey.retromusic.service.notification;
import static android.content.Context.NOTIFICATION_SERVICE;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import androidx.annotation.RequiresApi;
import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.service.MusicService;
import static android.content.Context.NOTIFICATION_SERVICE;
public abstract class PlayingNotification {
protected static final float NOTIFICATION_CONTROLS_SIZE_MULTIPLIER = 1.0f;
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 NOTIFY_MODE_FOREGROUND = 1;
private static final int NOTIFY_MODE_BACKGROUND = 0;
protected MusicService service;
boolean stopped;
private int notifyMode = NOTIFY_MODE_BACKGROUND;
private NotificationManager notificationManager;
private static final int NOTIFICATION_ID = 1;
private static final int NOTIFY_MODE_FOREGROUND = 1;
private static final int NOTIFY_MODE_BACKGROUND = 0;
protected MusicService service;
boolean stopped;
private int notifyMode = NOTIFY_MODE_BACKGROUND;
private NotificationManager notificationManager;
public synchronized void init(MusicService service) {
this.service = service;
notificationManager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
}
public abstract void update();
public synchronized void stop() {
stopped = true;
service.stopForeground(true);
notificationManager.cancel(NOTIFICATION_ID);
}
void updateNotifyModeAndPostNotification(Notification notification) {
int newNotifyMode;
if (service.isPlaying()) {
newNotifyMode = NOTIFY_MODE_FOREGROUND;
} else {
newNotifyMode = NOTIFY_MODE_BACKGROUND;
public synchronized void init(MusicService service) {
this.service = service;
notificationManager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
}
if (notifyMode != newNotifyMode && newNotifyMode == NOTIFY_MODE_BACKGROUND) {
service.stopForeground(false);
public abstract void update();
public synchronized void stop() {
stopped = true;
service.stopForeground(true);
notificationManager.cancel(NOTIFICATION_ID);
}
if (newNotifyMode == NOTIFY_MODE_FOREGROUND) {
service.startForeground(NOTIFICATION_ID, notification);
} else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) {
notificationManager.notify(NOTIFICATION_ID, notification);
void updateNotifyModeAndPostNotification(Notification notification) {
int newNotifyMode;
if (service.isPlaying()) {
newNotifyMode = NOTIFY_MODE_FOREGROUND;
} else {
newNotifyMode = NOTIFY_MODE_BACKGROUND;
}
if (notifyMode != newNotifyMode && newNotifyMode == NOTIFY_MODE_BACKGROUND) {
service.stopForeground(false);
}
if (newNotifyMode == NOTIFY_MODE_FOREGROUND) {
service.startForeground(NOTIFICATION_ID, notification);
} else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) {
notificationManager.notify(NOTIFICATION_ID, notification);
}
notifyMode = newNotifyMode;
}
notifyMode = newNotifyMode;
}
@RequiresApi(26)
private void createNotificationChannel() {
NotificationChannel notificationChannel = notificationManager
.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
if (notificationChannel == null) {
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
service.getString(R.string.playing_notification_name),
NotificationManager.IMPORTANCE_LOW);
notificationChannel
.setDescription(service.getString(R.string.playing_notification_description));
notificationChannel.enableLights(false);
notificationChannel.enableVibration(false);
notificationChannel.setShowBadge(false);
@RequiresApi(26)
private void createNotificationChannel() {
NotificationChannel notificationChannel = notificationManager
.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
if (notificationChannel == null) {
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
service.getString(R.string.playing_notification_name),
NotificationManager.IMPORTANCE_LOW);
notificationChannel
.setDescription(service.getString(R.string.playing_notification_description));
notificationChannel.enableLights(false);
notificationChannel.enableVibration(false);
notificationChannel.setShowBadge(false);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
}

View file

@ -9,8 +9,6 @@ import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;
@ -20,6 +18,8 @@ import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import code.name.monkey.appthemehelper.util.ColorUtil;
import code.name.monkey.appthemehelper.util.MaterialValueHelper;
import code.name.monkey.retromusic.R;
@ -28,6 +28,7 @@ import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper;
import code.name.monkey.retromusic.model.Song;
import code.name.monkey.retromusic.service.MusicService;
import code.name.monkey.retromusic.ui.activities.MainActivity;
import code.name.monkey.retromusic.ui.activities.NowPayingActivity;
import code.name.monkey.retromusic.util.PreferenceUtil;
import code.name.monkey.retromusic.util.RetroColorUtil;
import code.name.monkey.retromusic.util.RetroUtil;
@ -76,7 +77,7 @@ public class PlayingNotificationImpl extends PlayingNotification {
linkButtons(notificationLayout, notificationLayoutBig);
Intent action = new Intent(service, MainActivity.class);
Intent action = new Intent(service, NowPayingActivity.class);
action.putExtra("expand", true);
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
@ -114,7 +115,7 @@ public class PlayingNotificationImpl extends PlayingNotification {
public void onResourceReady(BitmapPaletteWrapper resource,
GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
update(resource.getBitmap(),
PreferenceUtil.getInstance(service).isDominantColor() ?
PreferenceUtil.getInstance().isDominantColor() ?
RetroColorUtil.getDominantColor(resource.getBitmap(), Color.TRANSPARENT) :
RetroColorUtil.getColor(resource.getPalette(), Color.TRANSPARENT));
}
@ -135,7 +136,7 @@ public class PlayingNotificationImpl extends PlayingNotification {
.setImageViewResource(R.id.image, R.drawable.default_album_art);
}
if (!PreferenceUtil.getInstance(service).coloredNotification()) {
if (!PreferenceUtil.getInstance().coloredNotification()) {
bgColor = Color.WHITE;
}
setBackgroundColor(bgColor);

View file

@ -1,10 +1,5 @@
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.content.ComponentName;
import android.content.Intent;
@ -13,131 +8,137 @@ import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.media.app.NotificationCompat.MediaStyle;
import android.text.Html;
import code.name.monkey.retromusic.Constants;
import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.RetroApplication;
import code.name.monkey.retromusic.glide.SongGlideRequest;
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper;
import code.name.monkey.retromusic.model.Song;
import code.name.monkey.retromusic.service.MusicService;
import code.name.monkey.retromusic.ui.activities.MainActivity;
import code.name.monkey.retromusic.util.PreferenceUtil;
import code.name.monkey.retromusic.util.RetroColorUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import androidx.core.app.NotificationCompat;
import androidx.media.app.NotificationCompat.MediaStyle;
import code.name.monkey.retromusic.Constants;
import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.glide.SongGlideRequest;
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper;
import code.name.monkey.retromusic.model.Song;
import code.name.monkey.retromusic.service.MusicService;
import code.name.monkey.retromusic.ui.activities.NowPayingActivity;
import code.name.monkey.retromusic.util.PreferenceUtil;
import code.name.monkey.retromusic.util.RetroColorUtil;
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;
public class PlayingNotificationImpl24 extends PlayingNotification {
@Override
public synchronized void update() {
stopped = false;
@Override
public synchronized void update() {
stopped = false;
final Song song = service.getCurrentSong();
final boolean isPlaying = service.isPlaying();
final Song song = service.getCurrentSong();
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 :
R.drawable.ic_play_arrow_white_24dp;
Intent action = new Intent(service, MainActivity.class);
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, NowPayingActivity.class);
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);
final ComponentName serviceName = new ComponentName(service, MusicService.class);
Intent intent = new Intent(Constants.ACTION_QUIT);
intent.setComponent(serviceName);
final PendingIntent deleteIntent = PendingIntent.getService(service, 0, intent, 0);
final ComponentName serviceName = new ComponentName(service, MusicService.class);
Intent intent = new Intent(Constants.ACTION_QUIT);
intent.setComponent(serviceName);
final PendingIntent deleteIntent = PendingIntent.getService(service, 0, intent, 0);
final int bigNotificationImageSize = service.getResources()
.getDimensionPixelSize(R.dimen.notification_big_image_size);
service.runOnUiThread(() -> 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) {
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()
.getDimensionPixelSize(R.dimen.notification_big_image_size);
service.runOnUiThread(() -> 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) {
update(resource.getBitmap(),
PreferenceUtil.getInstance().isDominantColor() ?
RetroColorUtil.getDominantColor(resource.getBitmap(), Color.TRANSPARENT) :
RetroColorUtil.getColor(resource.getPalette(), Color.TRANSPARENT));
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
update(null, Color.TRANSPARENT);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
update(null, Color.TRANSPARENT);
}
void update(Bitmap bitmap, int color) {
if (bitmap == null) {
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) {
if (bitmap == null) {
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));
NotificationCompat.Action closeAction = new NotificationCompat.Action(
R.drawable.ic_close_white_24dp,
service.getString(R.string.close_notification),
retrievePlaybackAction(ACTION_QUIT));
NotificationCompat.Action closeAction = new NotificationCompat.Action(
R.drawable.ic_close_white_24dp,
service.getString(R.string.close_notification),
retrievePlaybackAction(ACTION_QUIT));
NotificationCompat.Action previousAction = new NotificationCompat.Action(
R.drawable.ic_skip_previous_white_24dp,
service.getString(R.string.action_previous),
retrievePlaybackAction(ACTION_REWIND));
NotificationCompat.Action previousAction = new NotificationCompat.Action(
R.drawable.ic_skip_previous_white_24dp,
service.getString(R.string.action_previous),
retrievePlaybackAction(ACTION_REWIND));
NotificationCompat.Action nextAction = new NotificationCompat.Action(
R.drawable.ic_skip_next_white_24dp,
service.getString(R.string.action_next),
retrievePlaybackAction(ACTION_SKIP));
NotificationCompat.Action nextAction = new NotificationCompat.Action(
R.drawable.ic_skip_next_white_24dp,
service.getString(R.string.action_next),
retrievePlaybackAction(ACTION_SKIP));
NotificationCompat.Builder builder = new NotificationCompat.Builder(service,
NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.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,
NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setStyle(new MediaStyle()
.setMediaSession(service.getMediaSession().getSessionToken())
.setShowActionsInCompactView(0, 1, 2, 3, 4))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O && PreferenceUtil
.getInstance(service).coloredNotification()) {
builder.setColor(color);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setStyle(new MediaStyle()
.setMediaSession(service.getMediaSession().getSessionToken())
.setShowActionsInCompactView(0, 1, 2, 3, 4))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O &&
PreferenceUtil.getInstance().coloredNotification()) {
builder.setColor(color);
}
}
if (stopped) {
return; // notification has been stopped before loading was finished
}
updateNotifyModeAndPostNotification(builder.build());
}
}));
}
if (stopped) {
return; // notification has been stopped before loading was finished
}
updateNotifyModeAndPostNotification(builder.build());
}
}));
}
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);
}
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,11 +1,5 @@
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.content.ComponentName;
import android.content.Context;
@ -14,9 +8,15 @@ import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
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 androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import android.widget.RemoteViews;
import code.name.monkey.appthemehelper.util.ColorUtil;
import code.name.monkey.appthemehelper.util.MaterialValueHelper;
import code.name.monkey.retromusic.R;
@ -24,223 +24,225 @@ import code.name.monkey.retromusic.glide.SongGlideRequest;
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper;
import code.name.monkey.retromusic.model.Song;
import code.name.monkey.retromusic.service.MusicService;
import code.name.monkey.retromusic.ui.activities.MainActivity;
import code.name.monkey.retromusic.ui.activities.NowPayingActivity;
import code.name.monkey.retromusic.util.PreferenceUtil;
import code.name.monkey.retromusic.util.RetroUtil;
import code.name.monkey.retromusic.util.color.MediaNotificationProcessor;
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 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;
/**
* @author Hemanth S (h4h13).
*/
public class PlayingNotificationOreo extends PlayingNotification {
private Target<BitmapPaletteWrapper> target;
private Target<BitmapPaletteWrapper> target;
private RemoteViews getCombinedRemoteViews(boolean collapsed, Song song) {
RemoteViews remoteViews = new RemoteViews(service.getPackageName(),
collapsed ? R.layout.layout_notification_collapsed : R.layout.layout_notification_expanded);
private RemoteViews getCombinedRemoteViews(boolean collapsed, Song song) {
RemoteViews remoteViews = new RemoteViews(service.getPackageName(),
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.title, song.title);
remoteViews.setTextViewText(R.id.subtitle, song.artistName);
remoteViews.setTextViewText(R.id.appName,
service.getString(R.string.app_name) + "" + song.albumName);
remoteViews.setTextViewText(R.id.title, song.title);
remoteViews.setTextViewText(R.id.subtitle, song.artistName);
TypedArray typedArray = service
.obtainStyledAttributes(new int[]{android.R.attr.selectableItemBackground});
int selectableItemBackground = typedArray.getResourceId(0, 0);
typedArray.recycle();
TypedArray typedArray = service
.obtainStyledAttributes(new int[]{android.R.attr.selectableItemBackground});
int selectableItemBackground = typedArray.getResourceId(0, 0);
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));
return remoteViews;
}
@Override
public void update() {
stopped = false;
final Song song = service.getCurrentSong();
final boolean isPlaying = service.isPlaying();
final RemoteViews notificationLayout = getCombinedRemoteViews(true, song);
final RemoteViews notificationLayoutBig = getCombinedRemoteViews(false, song);
Intent action = new Intent(service, MainActivity.class);
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);
final PendingIntent deleteIntent = buildPendingIntent(service, ACTION_QUIT, null);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(service,
NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(clickIntent)
.setDeleteIntent(deleteIntent)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutBig)
.setOngoing(isPlaying);
final int bigNotificationImageSize = service.getResources()
.getDimensionPixelSize(R.dimen.notification_big_image_size);
service.runOnUiThread(new Runnable() {
@Override
public void run() {
if (target != null) {
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, (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
//setNotificationContent(remoteViews, ColorUtil.isColorLight(backgroundColor));
return remoteViews;
}
@Override
public void update() {
stopped = false;
final Song song = service.getCurrentSong();
final boolean isPlaying = service.isPlaying();
final RemoteViews notificationLayout = getCombinedRemoteViews(true, song);
final RemoteViews notificationLayoutBig = getCombinedRemoteViews(false, song);
Intent action = new Intent(service, NowPayingActivity.class);
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);
final PendingIntent deleteIntent = buildPendingIntent(service, ACTION_QUIT, null);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(service,
NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(clickIntent)
.setDeleteIntent(deleteIntent)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutBig)
.setOngoing(isPlaying);
final int bigNotificationImageSize = service.getResources()
.getDimensionPixelSize(R.dimen.notification_big_image_size);
service.runOnUiThread(new Runnable() {
@Override
public void run() {
if (target != null) {
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, (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().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());
}
updateNotifyModeAndPostNotification(builder.build());
}
private 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);
}
private 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);
}
private void linkButtons(final RemoteViews notificationLayout) {
PendingIntent pendingIntent;
private void linkButtons(final RemoteViews notificationLayout) {
PendingIntent pendingIntent;
final ComponentName serviceName = new ComponentName(service, MusicService.class);
final ComponentName serviceName = new ComponentName(service, MusicService.class);
// Previous track
pendingIntent = buildPendingIntent(service, ACTION_REWIND, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_prev, pendingIntent);
// Previous track
pendingIntent = buildPendingIntent(service, ACTION_REWIND, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_prev, pendingIntent);
// Play and pause
pendingIntent = buildPendingIntent(service, ACTION_TOGGLE_PAUSE, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_play_pause, pendingIntent);
// Play and pause
pendingIntent = buildPendingIntent(service, ACTION_TOGGLE_PAUSE, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_play_pause, pendingIntent);
// Next track
pendingIntent = buildPendingIntent(service, ACTION_SKIP, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_next, pendingIntent);
// Next track
pendingIntent = buildPendingIntent(service, ACTION_SKIP, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_next, pendingIntent);
// Close
pendingIntent = buildPendingIntent(service, ACTION_QUIT, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_quit, pendingIntent);
}
// Close
pendingIntent = buildPendingIntent(service, ACTION_QUIT, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.action_quit, pendingIntent);
}
}