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,19 +256,14 @@ public class MediaPlayerService extends Service
} }
} }
public void setupOnCurrentPlayingChangedHandler() public void setupOnCurrentPlayingChangedHandler() {
{ localMediaPlayer.onCurrentPlayingChanged = (DownloadFile currentPlaying) -> {
localMediaPlayer.onCurrentPlayingChanged = new Consumer<DownloadFile>() {
@Override if (currentPlaying != null) {
public void accept(DownloadFile currentPlaying) {
if (currentPlaying != null)
{
Util.broadcastNewTrackInfo(MediaPlayerService.this, currentPlaying.getSong()); Util.broadcastNewTrackInfo(MediaPlayerService.this, currentPlaying.getSong());
Util.broadcastA2dpMetaDataChange(MediaPlayerService.this, getPlayerPosition(), currentPlaying, Util.broadcastA2dpMetaDataChange(MediaPlayerService.this, getPlayerPosition(), currentPlaying,
downloader.getDownloads().size(), downloader.getCurrentPlayingIndex() + 1); downloader.getDownloads().size(), downloader.getCurrentPlayingIndex() + 1);
} } else {
else
{
Util.broadcastNewTrackInfo(MediaPlayerService.this, null); Util.broadcastNewTrackInfo(MediaPlayerService.this, null);
Util.broadcastA2dpMetaDataChange(MediaPlayerService.this, getPlayerPosition(), null, Util.broadcastA2dpMetaDataChange(MediaPlayerService.this, getPlayerPosition(), null,
downloader.getDownloads().size(), downloader.getCurrentPlayingIndex() + 1); downloader.getDownloads().size(), downloader.getCurrentPlayingIndex() + 1);
@ -280,24 +272,19 @@ public class MediaPlayerService extends Service
// Update widget // Update widget
PlayerState playerState = localMediaPlayer.playerState; PlayerState playerState = localMediaPlayer.playerState;
MusicDirectory.Entry song = currentPlaying == null ? null : currentPlaying.getSong(); MusicDirectory.Entry song = currentPlaying == null ? null : currentPlaying.getSong();
UltrasonicAppWidgetProvider4X1.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false); UpdateWidget(playerState, song);
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 (currentPlaying != null) if (currentPlaying != null) {
{
updateNotification(localMediaPlayer.playerState, currentPlaying); updateNotification(localMediaPlayer.playerState, currentPlaying);
nowPlayingEventDistributor.getValue().raiseShowNowPlayingEvent(); nowPlayingEventDistributor.getValue().raiseShowNowPlayingEvent();
} } else {
else
{
nowPlayingEventDistributor.getValue().raiseHideNowPlayingEvent(); nowPlayingEventDistributor.getValue().raiseHideNowPlayingEvent();
stopForeground(true); stopForeground(true);
isInForeground = false; isInForeground = false;
stopIfIdle(); stopIfIdle();
} }
}
return null;
}; };
} }
@ -472,16 +459,19 @@ 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);
}
public void setupOnPlayerStateChangedHandler() {
localMediaPlayer.onPlayerStateChanged = (PlayerState playerState, DownloadFile currentPlaying) -> {
// Notify MediaSession // Notify MediaSession
updateMediaSession(currentPlaying, playerState); updateMediaSession(currentPlaying, playerState);
if (playerState == PAUSED) if (playerState == PAUSED) {
{
downloadQueueSerializer.serializeDownloadQueue(downloader.downloadList, downloader.getCurrentPlayingIndex(), getPlayerPosition()); downloadQueueSerializer.serializeDownloadQueue(downloader.downloadList, downloader.getCurrentPlayingIndex(), getPlayerPosition());
} }
@ -495,74 +485,53 @@ public class MediaPlayerService extends Service
downloader.downloadList.indexOf(currentPlaying) + 1, getPlayerPosition()); downloader.downloadList.indexOf(currentPlaying) + 1, getPlayerPosition());
// Update widget // Update widget
UltrasonicAppWidgetProvider4X1.getInstance().notifyChange(MediaPlayerService.this, song, playerState == PlayerState.STARTED, false); UpdateWidget(playerState, song);
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) if (show) {
{
// Only update notification if player state is one that will change the icon // Only update notification if player state is one that will change the icon
if (playerState == PlayerState.STARTED || playerState == PlayerState.PAUSED) if (playerState == PlayerState.STARTED || playerState == PlayerState.PAUSED) {
{
updateNotification(playerState, currentPlaying); updateNotification(playerState, currentPlaying);
nowPlayingEventDistributor.getValue().raiseShowNowPlayingEvent(); nowPlayingEventDistributor.getValue().raiseShowNowPlayingEvent();
} }
} } else {
else
{
nowPlayingEventDistributor.getValue().raiseHideNowPlayingEvent(); nowPlayingEventDistributor.getValue().raiseHideNowPlayingEvent();
stopForeground(true); stopForeground(true);
isInForeground = false; isInForeground = false;
stopIfIdle(); stopIfIdle();
} }
if (playerState == STARTED) if (playerState == STARTED) {
{
scrobbler.scrobble(MediaPlayerService.this, currentPlaying, false); scrobbler.scrobble(MediaPlayerService.this, currentPlaying, false);
} } else if (playerState == COMPLETED) {
else if (playerState == COMPLETED)
{
scrobbler.scrobble(MediaPlayerService.this, currentPlaying, true); scrobbler.scrobble(MediaPlayerService.this, currentPlaying, true);
} }
}
return null;
}; };
} }
private void setupOnSongCompletedHandler() private void setupOnSongCompletedHandler() {
{ localMediaPlayer.onSongCompleted = (DownloadFile currentPlaying) -> {
localMediaPlayer.onSongCompleted = new Consumer<DownloadFile>() {
@Override
public void accept(DownloadFile currentPlaying) {
int index = downloader.getCurrentPlayingIndex(); 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); musicService.deleteBookmark(song.getId(), MediaPlayerService.this);
} } catch (Exception ignored) {
catch (Exception ignored)
{
} }
} }
} }
if (index != -1) if (index != -1) {
{ switch (getRepeatMode()) {
switch (getRepeatMode())
{
case OFF: case OFF:
if (index + 1 < 0 || index + 1 >= downloader.downloadList.size()) if (index + 1 < 0 || index + 1 >= downloader.downloadList.size()) {
{ if (Util.getShouldClearPlaylist(MediaPlayerService.this)) {
if (Util.getShouldClearPlaylist(MediaPlayerService.this))
{
clear(true); clear(true);
jukeboxMediaPlayer.getValue().updatePlaylist(); jukeboxMediaPlayer.getValue().updatePlaylist();
} }
@ -583,7 +552,8 @@ public class MediaPlayerService extends Service
break; 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)
} }
} }