fix: refactor start queue to put the db writing in the background (#287)

This commit is contained in:
eddyizm 2025-11-28 10:00:35 -08:00 committed by GitHub
commit 22f196c8c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,14 +1,13 @@
package com.cappielloantonio.tempo.service; package com.cappielloantonio.tempo.service;
import android.content.ComponentName; import android.content.ComponentName;
import android.util.Log;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.OptIn; import androidx.annotation.OptIn;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.media3.common.MediaItem; import androidx.media3.common.MediaItem;
@ -188,14 +187,12 @@ public class MediaManager {
try { try {
if (mediaBrowserListenableFuture.isDone()) { if (mediaBrowserListenableFuture.isDone()) {
final MediaBrowser browser = mediaBrowserListenableFuture.get(); final MediaBrowser browser = mediaBrowserListenableFuture.get();
backgroundExecutor.execute(() -> {
final List<MediaItem> items = MappingUtil.mapMediaItems(media); final List<MediaItem> items = MappingUtil.mapMediaItems(media);
enqueueDatabase(media, true, 0);
new Handler(Looper.getMainLooper()).post(() -> { new Handler(Looper.getMainLooper()).post(() -> {
justStarted.set(true); justStarted.set(true);
browser.setMediaItems(items, startIndex, 0); browser.setMediaItems(items, startIndex, 0);
browser.prepare(); browser.prepare();
Player.Listener timelineListener = new Player.Listener() { Player.Listener timelineListener = new Player.Listener() {
@Override @Override
public void onTimelineChanged(Timeline timeline, int reason) { public void onTimelineChanged(Timeline timeline, int reason) {
@ -209,6 +206,9 @@ public class MediaManager {
}; };
browser.addListener(timelineListener); browser.addListener(timelineListener);
}); });
backgroundExecutor.execute(() -> {
enqueueDatabase(media, true, 0);
}); });
} }
} catch (ExecutionException | InterruptedException e) { } catch (ExecutionException | InterruptedException e) {