Refactor all event listeners to lambdas

This commit is contained in:
tzugen 2021-04-22 11:47:15 +02:00
parent 2adb9ffc7e
commit 205f477b43
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
4 changed files with 132 additions and 169 deletions

View File

@ -1,11 +0,0 @@
package org.moire.ultrasonic.service;
/**
* Abstract class for consumers with two parameters
* @param <T> The type of the first object to consume
* @param <U> The type of the second object to consume
*/
public abstract class BiConsumer<T, U>
{
public abstract void accept(T t, U u);
}

View File

@ -1,9 +1,11 @@
package org.moire.ultrasonic.service; package org.moire.ultrasonic.service;
/** /**
* Deprecated: Should be replaced with lambdas
* Abstract class for consumers with one parameter * Abstract class for consumers with one parameter
* @param <T> The type of the object to consume * @param <T> The type of the object to consume
*/ */
@Deprecated
public abstract class Consumer<T> public abstract class Consumer<T>
{ {
public abstract void accept(T t); public abstract void accept(T t);

View File

@ -160,20 +160,17 @@ public class MediaPlayerService extends Service
setupOnPlayerStateChangedHandler(); setupOnPlayerStateChangedHandler();
setupOnSongCompletedHandler(); setupOnSongCompletedHandler();
localMediaPlayer.onPrepared = new Runnable() { localMediaPlayer.onPrepared = () -> {
@Override downloadQueueSerializer.serializeDownloadQueue(
public void run() { downloader.downloadList,
downloadQueueSerializer.serializeDownloadQueue(downloader.downloadList, downloader.getCurrentPlayingIndex(),
downloader.getCurrentPlayingIndex(), getPlayerPosition()); getPlayerPosition()
} );
}; return null;
localMediaPlayer.onNextSongRequested = new Runnable() {
@Override
public void run() {
setNextPlaying();
}
}; };
localMediaPlayer.onNextSongRequested = this::setNextPlaying;
// Create Notification Channel // Create Notification Channel
createNotificationChannel(); createNotificationChannel();
@ -259,45 +256,35 @@ public class MediaPlayerService extends Service
} }
} }
public void setupOnCurrentPlayingChangedHandler() public void setupOnCurrentPlayingChangedHandler() {
{ localMediaPlayer.onCurrentPlayingChanged = (DownloadFile currentPlaying) -> {
localMediaPlayer.onCurrentPlayingChanged = new Consumer<DownloadFile>() {
@Override
public void accept(DownloadFile currentPlaying) {
if (currentPlaying != null)
{
Util.broadcastNewTrackInfo(MediaPlayerService.this, currentPlaying.getSong());
Util.broadcastA2dpMetaDataChange(MediaPlayerService.this, getPlayerPosition(), currentPlaying,
downloader.getDownloads().size(), downloader.getCurrentPlayingIndex() + 1);
}
else
{
Util.broadcastNewTrackInfo(MediaPlayerService.this, null);
Util.broadcastA2dpMetaDataChange(MediaPlayerService.this, getPlayerPosition(), null,
downloader.getDownloads().size(), downloader.getCurrentPlayingIndex() + 1);
}
// Update widget if (currentPlaying != null) {
PlayerState playerState = localMediaPlayer.playerState; Util.broadcastNewTrackInfo(MediaPlayerService.this, currentPlaying.getSong());
MusicDirectory.Entry song = currentPlaying == null? null : currentPlaying.getSong(); Util.broadcastA2dpMetaDataChange(MediaPlayerService.this, getPlayerPosition(), currentPlaying,
UltrasonicAppWidgetProvider4X1.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false); downloader.getDownloads().size(), downloader.getCurrentPlayingIndex() + 1);
UltrasonicAppWidgetProvider4X2.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, true); } else {
UltrasonicAppWidgetProvider4X3.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false); Util.broadcastNewTrackInfo(MediaPlayerService.this, null);
UltrasonicAppWidgetProvider4X4.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false); Util.broadcastA2dpMetaDataChange(MediaPlayerService.this, getPlayerPosition(), null,
downloader.getDownloads().size(), downloader.getCurrentPlayingIndex() + 1);
if (currentPlaying != null)
{
updateNotification(localMediaPlayer.playerState, currentPlaying);
nowPlayingEventDistributor.getValue().raiseShowNowPlayingEvent();
}
else
{
nowPlayingEventDistributor.getValue().raiseHideNowPlayingEvent();
stopForeground(true);
isInForeground = false;
stopIfIdle();
}
} }
// Update widget
PlayerState playerState = localMediaPlayer.playerState;
MusicDirectory.Entry song = currentPlaying == null ? null : currentPlaying.getSong();
UpdateWidget(playerState, song);
if (currentPlaying != null) {
updateNotification(localMediaPlayer.playerState, currentPlaying);
nowPlayingEventDistributor.getValue().raiseShowNowPlayingEvent();
} else {
nowPlayingEventDistributor.getValue().raiseHideNowPlayingEvent();
stopForeground(true);
isInForeground = false;
stopIfIdle();
}
return null;
}; };
} }
@ -472,118 +459,101 @@ public class MediaPlayerService extends Service
localMediaPlayer.setPlayerState(STARTED); localMediaPlayer.setPlayerState(STARTED);
} }
public void setupOnPlayerStateChangedHandler() private void UpdateWidget(PlayerState playerState, MusicDirectory.Entry song) {
{ UltrasonicAppWidgetProvider4X1.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false);
localMediaPlayer.onPlayerStateChanged = new BiConsumer<PlayerState, DownloadFile>() { UltrasonicAppWidgetProvider4X2.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, true);
@Override UltrasonicAppWidgetProvider4X3.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false);
public void accept(PlayerState playerState, DownloadFile currentPlaying) { UltrasonicAppWidgetProvider4X4.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false);
// Notify MediaSession }
updateMediaSession(currentPlaying, playerState);
if (playerState == PAUSED) public void setupOnPlayerStateChangedHandler() {
{ localMediaPlayer.onPlayerStateChanged = (PlayerState playerState, DownloadFile currentPlaying) -> {
downloadQueueSerializer.serializeDownloadQueue(downloader.downloadList, downloader.getCurrentPlayingIndex(), getPlayerPosition()); // Notify MediaSession
} updateMediaSession(currentPlaying, playerState);
boolean showWhenPaused = (playerState != PlayerState.STOPPED && Util.isNotificationAlwaysEnabled(MediaPlayerService.this)); if (playerState == PAUSED) {
boolean show = playerState == PlayerState.STARTED || showWhenPaused; downloadQueueSerializer.serializeDownloadQueue(downloader.downloadList, downloader.getCurrentPlayingIndex(), getPlayerPosition());
MusicDirectory.Entry song = currentPlaying == null? null : currentPlaying.getSong();
Util.broadcastPlaybackStatusChange(MediaPlayerService.this, playerState);
Util.broadcastA2dpPlayStatusChange(MediaPlayerService.this, playerState, song,
downloader.downloadList.size() + downloader.backgroundDownloadList.size(),
downloader.downloadList.indexOf(currentPlaying) + 1, getPlayerPosition());
// Update widget
UltrasonicAppWidgetProvider4X1.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false);
UltrasonicAppWidgetProvider4X2.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, true);
UltrasonicAppWidgetProvider4X3.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false);
UltrasonicAppWidgetProvider4X4.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false);
if (show)
{
// Only update notification if player state is one that will change the icon
if (playerState == PlayerState.STARTED || playerState == PlayerState.PAUSED)
{
updateNotification(playerState, currentPlaying);
nowPlayingEventDistributor.getValue().raiseShowNowPlayingEvent();
}
}
else
{
nowPlayingEventDistributor.getValue().raiseHideNowPlayingEvent();
stopForeground(true);
isInForeground = false;
stopIfIdle();
}
if (playerState == STARTED)
{
scrobbler.scrobble(MediaPlayerService.this, currentPlaying, false);
}
else if (playerState == COMPLETED)
{
scrobbler.scrobble(MediaPlayerService.this, currentPlaying, true);
}
} }
boolean showWhenPaused = (playerState != PlayerState.STOPPED && Util.isNotificationAlwaysEnabled(MediaPlayerService.this));
boolean show = playerState == PlayerState.STARTED || showWhenPaused;
MusicDirectory.Entry song = currentPlaying == null ? null : currentPlaying.getSong();
Util.broadcastPlaybackStatusChange(MediaPlayerService.this, playerState);
Util.broadcastA2dpPlayStatusChange(MediaPlayerService.this, playerState, song,
downloader.downloadList.size() + downloader.backgroundDownloadList.size(),
downloader.downloadList.indexOf(currentPlaying) + 1, getPlayerPosition());
// Update widget
UpdateWidget(playerState, song);
if (show) {
// Only update notification if player state is one that will change the icon
if (playerState == PlayerState.STARTED || playerState == PlayerState.PAUSED) {
updateNotification(playerState, currentPlaying);
nowPlayingEventDistributor.getValue().raiseShowNowPlayingEvent();
}
} else {
nowPlayingEventDistributor.getValue().raiseHideNowPlayingEvent();
stopForeground(true);
isInForeground = false;
stopIfIdle();
}
if (playerState == STARTED) {
scrobbler.scrobble(MediaPlayerService.this, currentPlaying, false);
} else if (playerState == COMPLETED) {
scrobbler.scrobble(MediaPlayerService.this, currentPlaying, true);
}
return null;
}; };
} }
private void setupOnSongCompletedHandler() private void setupOnSongCompletedHandler() {
{ localMediaPlayer.onSongCompleted = (DownloadFile currentPlaying) -> {
localMediaPlayer.onSongCompleted = new Consumer<DownloadFile>() { int index = downloader.getCurrentPlayingIndex();
@Override
public void accept(DownloadFile currentPlaying) {
int index = downloader.getCurrentPlayingIndex();
if (currentPlaying != null) if (currentPlaying != null) {
{ final MusicDirectory.Entry song = currentPlaying.getSong();
final MusicDirectory.Entry song = currentPlaying.getSong();
if (song.getBookmarkPosition() > 0 && Util.getShouldClearBookmark(MediaPlayerService.this)) if (song.getBookmarkPosition() > 0 && Util.getShouldClearBookmark(MediaPlayerService.this)) {
{ MusicService musicService = MusicServiceFactory.getMusicService(MediaPlayerService.this);
MusicService musicService = MusicServiceFactory.getMusicService(MediaPlayerService.this); try {
try musicService.deleteBookmark(song.getId(), MediaPlayerService.this);
{ } catch (Exception ignored) {
musicService.deleteBookmark(song.getId(), MediaPlayerService.this);
}
catch (Exception ignored)
{
}
}
}
if (index != -1)
{
switch (getRepeatMode())
{
case OFF:
if (index + 1 < 0 || index + 1 >= downloader.downloadList.size())
{
if (Util.getShouldClearPlaylist(MediaPlayerService.this))
{
clear(true);
jukeboxMediaPlayer.getValue().updatePlaylist();
}
resetPlayback();
break;
}
play(index + 1);
break;
case ALL:
play((index + 1) % downloader.downloadList.size());
break;
case SINGLE:
play(index);
break;
default:
break;
} }
} }
} }
if (index != -1) {
switch (getRepeatMode()) {
case OFF:
if (index + 1 < 0 || index + 1 >= downloader.downloadList.size()) {
if (Util.getShouldClearPlaylist(MediaPlayerService.this)) {
clear(true);
jukeboxMediaPlayer.getValue().updatePlaylist();
}
resetPlayback();
break;
}
play(index + 1);
break;
case ALL:
play((index + 1) % downloader.downloadList.size());
break;
case SINGLE:
play(index);
break;
default:
break;
}
}
return null;
}; };
} }

View File

@ -45,16 +45,16 @@ class LocalMediaPlayer(
) { ) {
@JvmField @JvmField
var onCurrentPlayingChanged: Consumer<DownloadFile?>? = null var onCurrentPlayingChanged: ((DownloadFile?) -> Unit?)? = null
@JvmField @JvmField
var onSongCompleted: Consumer<DownloadFile?>? = null var onSongCompleted: ((DownloadFile?) -> Unit?)? = null
@JvmField @JvmField
var onPlayerStateChanged: BiConsumer<PlayerState, DownloadFile?>? = null var onPlayerStateChanged: ((PlayerState, DownloadFile?) -> Unit?)? = null
@JvmField @JvmField
var onPrepared: Runnable? = null var onPrepared: (() -> Any?)? = null
@JvmField @JvmField
var onNextSongRequested: Runnable? = null var onNextSongRequested: Runnable? = null
@ -164,8 +164,9 @@ class LocalMediaPlayer(
if (onPlayerStateChanged != null) { if (onPlayerStateChanged != null) {
val mainHandler = Handler(context.mainLooper) val mainHandler = Handler(context.mainLooper)
val myRunnable = Runnable { val myRunnable = Runnable {
onPlayerStateChanged!!.accept(playerState, currentPlaying) onPlayerStateChanged!!(playerState, currentPlaying)
} }
mainHandler.post(myRunnable) mainHandler.post(myRunnable)
} }
@ -189,7 +190,7 @@ class LocalMediaPlayer(
if (onCurrentPlayingChanged != null) { if (onCurrentPlayingChanged != null) {
val mainHandler = Handler(context.mainLooper) val mainHandler = Handler(context.mainLooper)
val myRunnable = Runnable { onCurrentPlayingChanged!!.accept(currentPlaying) } val myRunnable = Runnable { onCurrentPlayingChanged!!(currentPlaying) }
mainHandler.post(myRunnable) mainHandler.post(myRunnable)
} }
} }
@ -424,7 +425,9 @@ class LocalMediaPlayer(
} }
} }
postRunnable(onPrepared) postRunnable {
onPrepared
}
} }
attachHandlersToPlayer(mediaPlayer, downloadFile, partial) attachHandlersToPlayer(mediaPlayer, downloadFile, partial)
mediaPlayer.prepareAsync() mediaPlayer.prepareAsync()
@ -458,7 +461,6 @@ class LocalMediaPlayer(
setAudioAttributes(nextMediaPlayer!!) setAudioAttributes(nextMediaPlayer!!)
// This has nothing to do with the MediaSession, it is used to associate // This has nothing to do with the MediaSession, it is used to associate
// the equalizer or visualizer with the player // the equalizer or visualizer with the player
try { try {
@ -536,7 +538,7 @@ class LocalMediaPlayer(
} else { } else {
if (onSongCompleted != null) { if (onSongCompleted != null) {
val mainHandler = Handler(context.mainLooper) val mainHandler = Handler(context.mainLooper)
val myRunnable = Runnable { onSongCompleted!!.accept(currentPlaying) } val myRunnable = Runnable { onSongCompleted!!(currentPlaying) }
mainHandler.post(myRunnable) mainHandler.post(myRunnable)
} }
} }