mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Optimized the history saving and scrobbling functions
This commit is contained in:
parent
1204716a65
commit
105d5ca9c5
4 changed files with 48 additions and 35 deletions
|
|
@ -40,9 +40,6 @@ public interface QueueDao {
|
||||||
@Query("UPDATE queue SET playing_changed=:timestamp WHERE id=:id")
|
@Query("UPDATE queue SET playing_changed=:timestamp WHERE id=:id")
|
||||||
void setPlayingChanged(String id, long timestamp);
|
void setPlayingChanged(String id, long timestamp);
|
||||||
|
|
||||||
@Query("SELECT track_order FROM queue ORDER BY last_play DESC LIMIT 1")
|
@Query("SELECT * FROM queue ORDER BY last_play DESC LIMIT 1")
|
||||||
int getLastPlay();
|
Queue getLastPlayed();
|
||||||
|
|
||||||
@Query("SELECT playing_changed FROM queue ORDER BY last_play DESC LIMIT 1")
|
|
||||||
long getLastPlayedTimestamp();
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
package com.cappielloantonio.play.repository;
|
package com.cappielloantonio.play.repository;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData;
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.media3.common.MediaItem;
|
||||||
|
|
||||||
import com.cappielloantonio.play.database.AppDatabase;
|
import com.cappielloantonio.play.database.AppDatabase;
|
||||||
import com.cappielloantonio.play.database.dao.QueueDao;
|
import com.cappielloantonio.play.database.dao.QueueDao;
|
||||||
import com.cappielloantonio.play.model.Queue;
|
|
||||||
import com.cappielloantonio.play.model.Media;
|
import com.cappielloantonio.play.model.Media;
|
||||||
|
import com.cappielloantonio.play.model.Queue;
|
||||||
import com.cappielloantonio.play.util.MappingUtil;
|
import com.cappielloantonio.play.util.MappingUtil;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
@ -149,7 +151,8 @@ public class QueueRepository {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
thread.join();
|
thread.join();
|
||||||
index = getLastPlayedMediaThreadSafe.getIndex();
|
Queue lastMediaPlayed = getLastPlayedMediaThreadSafe.getQueueItem();
|
||||||
|
index = lastMediaPlayed.getTrackOrder();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
@ -160,13 +163,14 @@ public class QueueRepository {
|
||||||
public long getLastPlayedMediaTimestamp() {
|
public long getLastPlayedMediaTimestamp() {
|
||||||
long timestamp = 0;
|
long timestamp = 0;
|
||||||
|
|
||||||
GetLastPlayedMediaTimestampThreadSafe getLastPlayedMediaTimestampThreadSafe = new GetLastPlayedMediaTimestampThreadSafe(queueDao);
|
GetLastPlayedMediaThreadSafe getLastPlayedMediaThreadSafe = new GetLastPlayedMediaThreadSafe(queueDao);
|
||||||
Thread thread = new Thread(getLastPlayedMediaTimestampThreadSafe);
|
Thread thread = new Thread(getLastPlayedMediaThreadSafe);
|
||||||
thread.start();
|
thread.start();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
thread.join();
|
thread.join();
|
||||||
timestamp = getLastPlayedMediaTimestampThreadSafe.getTimestamp();
|
Queue lastMediaPlayed = getLastPlayedMediaThreadSafe.getQueueItem();
|
||||||
|
timestamp = lastMediaPlayed.getPlayingChanged();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
@ -174,6 +178,32 @@ public class QueueRepository {
|
||||||
return timestamp;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isMediaPlayingPlausible(MediaItem mediaItem) {
|
||||||
|
boolean isPlausible = true;
|
||||||
|
|
||||||
|
GetLastPlayedMediaThreadSafe getLastPlayedMediaThreadSafe = new GetLastPlayedMediaThreadSafe(queueDao);
|
||||||
|
Thread thread = new Thread(getLastPlayedMediaThreadSafe);
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
thread.join();
|
||||||
|
Queue lastMediaPlayed = getLastPlayedMediaThreadSafe.getQueueItem();
|
||||||
|
|
||||||
|
if (mediaItem.mediaId.equals(lastMediaPlayed.getId())) {
|
||||||
|
|
||||||
|
if (System.currentTimeMillis() > lastMediaPlayed.getLastPlay() + lastMediaPlayed.getDuration() * 1000) {
|
||||||
|
isPlausible = true;
|
||||||
|
} else {
|
||||||
|
isPlausible = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return isPlausible;
|
||||||
|
}
|
||||||
|
|
||||||
private static class GetMediaThreadSafe implements Runnable {
|
private static class GetMediaThreadSafe implements Runnable {
|
||||||
private final QueueDao queueDao;
|
private final QueueDao queueDao;
|
||||||
private List<Media> media;
|
private List<Media> media;
|
||||||
|
|
@ -287,7 +317,7 @@ public class QueueRepository {
|
||||||
|
|
||||||
private static class GetLastPlayedMediaThreadSafe implements Runnable {
|
private static class GetLastPlayedMediaThreadSafe implements Runnable {
|
||||||
private final QueueDao queueDao;
|
private final QueueDao queueDao;
|
||||||
private int index;
|
private Queue lastMediaPlayed;
|
||||||
|
|
||||||
public GetLastPlayedMediaThreadSafe(QueueDao queueDao) {
|
public GetLastPlayedMediaThreadSafe(QueueDao queueDao) {
|
||||||
this.queueDao = queueDao;
|
this.queueDao = queueDao;
|
||||||
|
|
@ -295,29 +325,11 @@ public class QueueRepository {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
index = queueDao.getLastPlay();
|
lastMediaPlayed = queueDao.getLastPlayed();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getIndex() {
|
public Queue getQueueItem() {
|
||||||
return index;
|
return lastMediaPlayed;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class GetLastPlayedMediaTimestampThreadSafe implements Runnable {
|
|
||||||
private final QueueDao queueDao;
|
|
||||||
private long timestamp;
|
|
||||||
|
|
||||||
public GetLastPlayedMediaTimestampThreadSafe(QueueDao queueDao) {
|
|
||||||
this.queueDao = queueDao;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
timestamp = queueDao.getLastPlayedTimestamp();
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getTimestamp() {
|
|
||||||
return timestamp;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -300,12 +300,14 @@ public class MediaManager {
|
||||||
|
|
||||||
public static void scrobble(MediaItem mediaItem) {
|
public static void scrobble(MediaItem mediaItem) {
|
||||||
if (mediaItem != null)
|
if (mediaItem != null)
|
||||||
getSongRepository().scrobble(mediaItem.mediaMetadata.extras.getString("id"));
|
if (getQueueRepository().isMediaPlayingPlausible(mediaItem))
|
||||||
|
getSongRepository().scrobble(mediaItem.mediaMetadata.extras.getString("id"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void saveChronology(MediaItem mediaItem) {
|
public static void saveChronology(MediaItem mediaItem) {
|
||||||
if (mediaItem != null)
|
if (mediaItem != null)
|
||||||
getChronologyRepository().insert(MappingUtil.mapChronology(mediaItem));
|
if (getQueueRepository().isMediaPlayingPlausible(mediaItem))
|
||||||
|
getChronologyRepository().insert(MappingUtil.mapChronology(mediaItem));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static QueueRepository getQueueRepository() {
|
private static QueueRepository getQueueRepository() {
|
||||||
|
|
|
||||||
|
|
@ -233,11 +233,13 @@ class MediaService : MediaLibraryService(), SessionAvailabilityListener {
|
||||||
player.addListener(object : Player.Listener {
|
player.addListener(object : Player.Listener {
|
||||||
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
|
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
|
||||||
if (mediaItem == null) return
|
if (mediaItem == null) return
|
||||||
MediaManager.setLastPlayedTimestamp(mediaItem)
|
|
||||||
if (mediaItem.mediaMetadata.extras!!.getString("mediaType") == Media.MEDIA_TYPE_MUSIC) {
|
if (mediaItem.mediaMetadata.extras!!.getString("mediaType") == Media.MEDIA_TYPE_MUSIC) {
|
||||||
MediaManager.scrobble(mediaItem)
|
MediaManager.scrobble(mediaItem)
|
||||||
MediaManager.saveChronology(mediaItem)
|
MediaManager.saveChronology(mediaItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MediaManager.setLastPlayedTimestamp(mediaItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue