diff --git a/app/src/main/java/com/cappielloantonio/play/database/dao/QueueDao.java b/app/src/main/java/com/cappielloantonio/play/database/dao/QueueDao.java index f334c428..d229ae60 100644 --- a/app/src/main/java/com/cappielloantonio/play/database/dao/QueueDao.java +++ b/app/src/main/java/com/cappielloantonio/play/database/dao/QueueDao.java @@ -36,4 +36,7 @@ public interface QueueDao { @Query("UPDATE queue SET last_play=:timestamp WHERE id=:id") void setLastPlay(String id, long timestamp); + + @Query("SELECT track_order FROM queue ORDER BY last_play DESC LIMIT 1") + int getLastPlay(); } \ No newline at end of file diff --git a/app/src/main/java/com/cappielloantonio/play/repository/QueueRepository.java b/app/src/main/java/com/cappielloantonio/play/repository/QueueRepository.java index d4ce12e2..298de773 100644 --- a/app/src/main/java/com/cappielloantonio/play/repository/QueueRepository.java +++ b/app/src/main/java/com/cappielloantonio/play/repository/QueueRepository.java @@ -128,12 +128,29 @@ public class QueueRepository { return count; } - public void setTimestamp(Song song) { - SetTimestampThreadSafe delete = new SetTimestampThreadSafe(queueDao, song.getId()); - Thread thread = new Thread(delete); + public void setTimestamp(String id) { + SetTimestampThreadSafe timestamp = new SetTimestampThreadSafe(queueDao, id); + Thread thread = new Thread(timestamp); thread.start(); } + public int getLastPlayedSongIndex() { + int index = 0; + + GetLastPlayedSongThreadSafe getLastPlayedSongThreadSafe = new GetLastPlayedSongThreadSafe(queueDao); + Thread thread = new Thread(getLastPlayedSongThreadSafe); + thread.start(); + + try { + thread.join(); + index = getLastPlayedSongThreadSafe.getIndex(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + return index; + } + private static class GetSongsThreadSafe implements Runnable { private final QueueDao queueDao; private List songs; @@ -227,4 +244,22 @@ public class QueueRepository { queueDao.setLastPlay(songId, Instant.now().toEpochMilli()); } } + + private static class GetLastPlayedSongThreadSafe implements Runnable { + private final QueueDao queueDao; + private int index; + + public GetLastPlayedSongThreadSafe(QueueDao queueDao) { + this.queueDao = queueDao; + } + + @Override + public void run() { + index = queueDao.getLastPlay(); + } + + public int getIndex() { + return index; + } + } } diff --git a/app/src/main/java/com/cappielloantonio/play/service/MediaManager.java b/app/src/main/java/com/cappielloantonio/play/service/MediaManager.java index 904caeb2..a080f135 100644 --- a/app/src/main/java/com/cappielloantonio/play/service/MediaManager.java +++ b/app/src/main/java/com/cappielloantonio/play/service/MediaManager.java @@ -3,6 +3,7 @@ package com.cappielloantonio.play.service; import android.content.Context; import android.util.Log; +import androidx.media3.common.MediaItem; import androidx.media3.session.MediaBrowser; import com.cappielloantonio.play.App; @@ -44,6 +45,7 @@ public class MediaManager { if (mediaBrowserListenableFuture.isDone()) { mediaBrowserListenableFuture.get().clearMediaItems(); mediaBrowserListenableFuture.get().setMediaItems(MappingUtil.mapMediaItems(context, songs)); + mediaBrowserListenableFuture.get().seekTo(getQueueRepository().getLastPlayedSongIndex(), 0); mediaBrowserListenableFuture.get().prepare(); } } catch (ExecutionException | InterruptedException e) { @@ -238,6 +240,10 @@ public class MediaManager { } } + public static void timestamp(MediaItem mediaItem) { + if (mediaItem != null) getQueueRepository().setTimestamp(mediaItem.mediaId); + } + private static QueueRepository getQueueRepository() { return new QueueRepository(App.getInstance()); } diff --git a/app/src/main/java/com/cappielloantonio/play/service/MediaService.java b/app/src/main/java/com/cappielloantonio/play/service/MediaService.java index bb8c3495..4cde3b33 100644 --- a/app/src/main/java/com/cappielloantonio/play/service/MediaService.java +++ b/app/src/main/java/com/cappielloantonio/play/service/MediaService.java @@ -8,6 +8,7 @@ import androidx.annotation.Nullable; import androidx.media3.common.AudioAttributes; import androidx.media3.common.C; import androidx.media3.common.MediaItem; +import androidx.media3.common.Player; import androidx.media3.exoplayer.ExoPlayer; import androidx.media3.session.MediaLibraryService; import androidx.media3.session.MediaSession; @@ -26,6 +27,7 @@ public class MediaService extends MediaLibraryService { public void onCreate() { super.onCreate(); initializePlayer(); + initializePlayerListener(); } @Override @@ -73,4 +75,14 @@ public class MediaService extends MediaLibraryService { .build(); } } + + @SuppressLint("UnsafeOptInUsageError") + private void initializePlayerListener() { + player.addListener(new Player.Listener() { + @Override + public void onMediaItemTransition(@Nullable MediaItem mediaItem, int reason) { + MediaManager.timestamp(mediaItem); + } + }); + } }