Refactor all event listeners to lambdas
This commit is contained in:
parent
2adb9ffc7e
commit
205f477b43
|
@ -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);
|
||||
}
|
|
@ -1,9 +1,11 @@
|
|||
package org.moire.ultrasonic.service;
|
||||
|
||||
/**
|
||||
* Deprecated: Should be replaced with lambdas
|
||||
* Abstract class for consumers with one parameter
|
||||
* @param <T> The type of the object to consume
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class Consumer<T>
|
||||
{
|
||||
public abstract void accept(T t);
|
||||
|
|
|
@ -160,20 +160,17 @@ public class MediaPlayerService extends Service
|
|||
setupOnPlayerStateChangedHandler();
|
||||
setupOnSongCompletedHandler();
|
||||
|
||||
localMediaPlayer.onPrepared = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
downloadQueueSerializer.serializeDownloadQueue(downloader.downloadList,
|
||||
downloader.getCurrentPlayingIndex(), getPlayerPosition());
|
||||
}
|
||||
};
|
||||
localMediaPlayer.onNextSongRequested = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
setNextPlaying();
|
||||
}
|
||||
localMediaPlayer.onPrepared = () -> {
|
||||
downloadQueueSerializer.serializeDownloadQueue(
|
||||
downloader.downloadList,
|
||||
downloader.getCurrentPlayingIndex(),
|
||||
getPlayerPosition()
|
||||
);
|
||||
return null;
|
||||
};
|
||||
|
||||
localMediaPlayer.onNextSongRequested = this::setNextPlaying;
|
||||
|
||||
// Create Notification Channel
|
||||
createNotificationChannel();
|
||||
|
||||
|
@ -259,45 +256,35 @@ public class MediaPlayerService extends Service
|
|||
}
|
||||
}
|
||||
|
||||
public void setupOnCurrentPlayingChangedHandler()
|
||||
{
|
||||
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);
|
||||
}
|
||||
public void setupOnCurrentPlayingChangedHandler() {
|
||||
localMediaPlayer.onCurrentPlayingChanged = (DownloadFile currentPlaying) -> {
|
||||
|
||||
// Update widget
|
||||
PlayerState playerState = localMediaPlayer.playerState;
|
||||
MusicDirectory.Entry song = currentPlaying == null? null : currentPlaying.getSong();
|
||||
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 (currentPlaying != null)
|
||||
{
|
||||
updateNotification(localMediaPlayer.playerState, currentPlaying);
|
||||
nowPlayingEventDistributor.getValue().raiseShowNowPlayingEvent();
|
||||
}
|
||||
else
|
||||
{
|
||||
nowPlayingEventDistributor.getValue().raiseHideNowPlayingEvent();
|
||||
stopForeground(true);
|
||||
isInForeground = false;
|
||||
stopIfIdle();
|
||||
}
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
public void setupOnPlayerStateChangedHandler()
|
||||
{
|
||||
localMediaPlayer.onPlayerStateChanged = new BiConsumer<PlayerState, DownloadFile>() {
|
||||
@Override
|
||||
public void accept(PlayerState playerState, DownloadFile currentPlaying) {
|
||||
// Notify MediaSession
|
||||
updateMediaSession(currentPlaying, playerState);
|
||||
private void UpdateWidget(PlayerState playerState, MusicDirectory.Entry song) {
|
||||
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 (playerState == PAUSED)
|
||||
{
|
||||
downloadQueueSerializer.serializeDownloadQueue(downloader.downloadList, downloader.getCurrentPlayingIndex(), getPlayerPosition());
|
||||
}
|
||||
public void setupOnPlayerStateChangedHandler() {
|
||||
localMediaPlayer.onPlayerStateChanged = (PlayerState playerState, DownloadFile currentPlaying) -> {
|
||||
// Notify MediaSession
|
||||
updateMediaSession(currentPlaying, playerState);
|
||||
|
||||
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
|
||||
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);
|
||||
}
|
||||
if (playerState == PAUSED) {
|
||||
downloadQueueSerializer.serializeDownloadQueue(downloader.downloadList, downloader.getCurrentPlayingIndex(), getPlayerPosition());
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
localMediaPlayer.onSongCompleted = new Consumer<DownloadFile>() {
|
||||
@Override
|
||||
public void accept(DownloadFile currentPlaying) {
|
||||
int index = downloader.getCurrentPlayingIndex();
|
||||
private void setupOnSongCompletedHandler() {
|
||||
localMediaPlayer.onSongCompleted = (DownloadFile currentPlaying) -> {
|
||||
int index = downloader.getCurrentPlayingIndex();
|
||||
|
||||
if (currentPlaying != null)
|
||||
{
|
||||
final MusicDirectory.Entry song = currentPlaying.getSong();
|
||||
if (currentPlaying != null) {
|
||||
final MusicDirectory.Entry song = currentPlaying.getSong();
|
||||
|
||||
if (song.getBookmarkPosition() > 0 && Util.getShouldClearBookmark(MediaPlayerService.this))
|
||||
{
|
||||
MusicService musicService = MusicServiceFactory.getMusicService(MediaPlayerService.this);
|
||||
try
|
||||
{
|
||||
musicService.deleteBookmark(song.getId(), MediaPlayerService.this);
|
||||
}
|
||||
catch (Exception ignored)
|
||||
{
|
||||
if (song.getBookmarkPosition() > 0 && Util.getShouldClearBookmark(MediaPlayerService.this)) {
|
||||
MusicService musicService = MusicServiceFactory.getMusicService(MediaPlayerService.this);
|
||||
try {
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -45,16 +45,16 @@ class LocalMediaPlayer(
|
|||
) {
|
||||
|
||||
@JvmField
|
||||
var onCurrentPlayingChanged: Consumer<DownloadFile?>? = null
|
||||
var onCurrentPlayingChanged: ((DownloadFile?) -> Unit?)? = null
|
||||
|
||||
@JvmField
|
||||
var onSongCompleted: Consumer<DownloadFile?>? = null
|
||||
var onSongCompleted: ((DownloadFile?) -> Unit?)? = null
|
||||
|
||||
@JvmField
|
||||
var onPlayerStateChanged: BiConsumer<PlayerState, DownloadFile?>? = null
|
||||
var onPlayerStateChanged: ((PlayerState, DownloadFile?) -> Unit?)? = null
|
||||
|
||||
@JvmField
|
||||
var onPrepared: Runnable? = null
|
||||
var onPrepared: (() -> Any?)? = null
|
||||
|
||||
@JvmField
|
||||
var onNextSongRequested: Runnable? = null
|
||||
|
@ -164,8 +164,9 @@ class LocalMediaPlayer(
|
|||
|
||||
if (onPlayerStateChanged != null) {
|
||||
val mainHandler = Handler(context.mainLooper)
|
||||
|
||||
val myRunnable = Runnable {
|
||||
onPlayerStateChanged!!.accept(playerState, currentPlaying)
|
||||
onPlayerStateChanged!!(playerState, currentPlaying)
|
||||
}
|
||||
mainHandler.post(myRunnable)
|
||||
}
|
||||
|
@ -189,7 +190,7 @@ class LocalMediaPlayer(
|
|||
|
||||
if (onCurrentPlayingChanged != null) {
|
||||
val mainHandler = Handler(context.mainLooper)
|
||||
val myRunnable = Runnable { onCurrentPlayingChanged!!.accept(currentPlaying) }
|
||||
val myRunnable = Runnable { onCurrentPlayingChanged!!(currentPlaying) }
|
||||
mainHandler.post(myRunnable)
|
||||
}
|
||||
}
|
||||
|
@ -424,7 +425,9 @@ class LocalMediaPlayer(
|
|||
}
|
||||
}
|
||||
|
||||
postRunnable(onPrepared)
|
||||
postRunnable {
|
||||
onPrepared
|
||||
}
|
||||
}
|
||||
attachHandlersToPlayer(mediaPlayer, downloadFile, partial)
|
||||
mediaPlayer.prepareAsync()
|
||||
|
@ -458,7 +461,6 @@ class LocalMediaPlayer(
|
|||
|
||||
setAudioAttributes(nextMediaPlayer!!)
|
||||
|
||||
|
||||
// This has nothing to do with the MediaSession, it is used to associate
|
||||
// the equalizer or visualizer with the player
|
||||
try {
|
||||
|
@ -536,7 +538,7 @@ class LocalMediaPlayer(
|
|||
} else {
|
||||
if (onSongCompleted != null) {
|
||||
val mainHandler = Handler(context.mainLooper)
|
||||
val myRunnable = Runnable { onSongCompleted!!.accept(currentPlaying) }
|
||||
val myRunnable = Runnable { onSongCompleted!!(currentPlaying) }
|
||||
mainHandler.post(myRunnable)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue