wip: added logging to media manager to track down bug in bottom sheet dialogs

This commit is contained in:
eddyizm 2025-12-29 16:37:20 -08:00
parent 8c5390bfef
commit f39891dd2c
No known key found for this signature in database
GPG key ID: CF5F671829E8158A
3 changed files with 91 additions and 37 deletions

View file

@ -184,41 +184,57 @@ public class MediaManager {
@OptIn(markerClass = UnstableApi.class)
public static void startQueue(ListenableFuture<MediaBrowser> mediaBrowserListenableFuture, List<Child> media, int startIndex) {
if (mediaBrowserListenableFuture != null) {
Log.d(TAG, "startQueue called with " + (media != null ? media.size() : 0) + " songs");
mediaBrowserListenableFuture.addListener(() -> {
try {
if (mediaBrowserListenableFuture.isDone()) {
Log.d(TAG, "MediaBrowser future is done");
final MediaBrowser browser = mediaBrowserListenableFuture.get();
Log.d(TAG, "Got MediaBrowser, connected: " + browser.isConnected());
final List<MediaItem> items = MappingUtil.mapMediaItems(media);
Log.d(TAG, "Mapped " + items.size() + " media items");
new Handler(Looper.getMainLooper()).post(() -> {
Log.d(TAG, "Setting " + items.size() + " media items at index " + startIndex);
justStarted.set(true);
browser.setMediaItems(items, startIndex, 0);
browser.prepare();
Log.d(TAG, "setMediaItems and prepare called");
Player.Listener timelineListener = new Player.Listener() {
@Override
public void onTimelineChanged(Timeline timeline, int reason) {
Log.d(TAG, "onTimelineChanged: itemCount=" + browser.getMediaItemCount() + ", reason=" + reason);
int itemCount = browser.getMediaItemCount();
if (itemCount > 0 && startIndex >= 0 && startIndex < itemCount) {
Log.d(TAG, "Seeking to " + startIndex + " and playing");
browser.seekTo(startIndex, 0);
browser.play();
browser.removeListener(this);
Log.d(TAG, "Playback started");
} else {
Log.d(TAG, "Cannot start playback: itemCount=" + itemCount + ", startIndex=" + startIndex);
}
}
};
Log.d(TAG, "Adding timeline listener");
browser.addListener(timelineListener);
});
backgroundExecutor.execute(() -> {
Log.d(TAG, "Background: enqueuing to database");
enqueueDatabase(media, true, 0);
});
}
} catch (ExecutionException | InterruptedException e) {
Log.e(TAG, "Error executing startQueue logic: " + e.getMessage(), e);
Log.e(TAG, "Error in startQueue: " + e.getMessage(), e);
}
}, MoreExecutors.directExecutor());
}
}
public static void startQueue(ListenableFuture<MediaBrowser> mediaBrowserListenableFuture, Child media) {

View file

@ -139,10 +139,24 @@ public class AlbumBottomSheetDialog extends BottomSheetDialogFragment implements
}
}
view.postDelayed(() -> {
try {
if (mediaBrowserListenableFuture.isDone()) {
MediaBrowser browser = mediaBrowserListenableFuture.get();
if (browser != null && browser.isPlaying()) {
dismissBottomSheet();
return;
}
}
} catch (Exception e) {
// Ignore
}
view.postDelayed(() -> dismissBottomSheet(), 200);
}, 300);
}
});
});
TextView playRandom = view.findViewById(R.id.play_random_text_view);
playRandom.setOnClickListener(v -> {
AlbumRepository albumRepository = new AlbumRepository();

View file

@ -2,11 +2,13 @@ package com.cappielloantonio.tempo.ui.fragment.bottomsheetdialog;
import android.content.ComponentName;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.annotation.Nullable;
@ -89,21 +91,43 @@ public class ArtistBottomSheetDialog extends BottomSheetDialogFragment implement
TextView playRadio = view.findViewById(R.id.play_radio_text_view);
playRadio.setOnClickListener(v -> {
Log.d(TAG, "Artist instant mix clicked");
ArtistRepository artistRepository = new ArtistRepository();
Observer<List<Child>> observer = new Observer<List<Child>>() {
artistRepository.getInstantMix(artist, 20)
.observe(getViewLifecycleOwner(), new androidx.lifecycle.Observer<List<Child>>() {
@Override
public void onChanged(List<Child> songs) {
if (songs != null && !songs.isEmpty() && isAdded()) {
if (songs != null && !songs.isEmpty()) {
Log.d(TAG, "Starting queue with " + songs.size() + " songs");
MusicUtil.ratingFilter(songs);
MediaManager.startQueue(mediaBrowserListenableFuture, songs, 0);
((MainActivity) requireActivity()).setBottomSheetInPeek(true);
artistRepository.getInstantMix(artist, 20).removeObserver(this);
artistRepository.getInstantMix(artist, 20)
.removeObserver(this);
view.postDelayed(() -> {
try {
if (mediaBrowserListenableFuture.isDone()) {
MediaBrowser browser = mediaBrowserListenableFuture.get();
if (browser != null && browser.isPlaying()) {
dismissBottomSheet();
return;
}
}
} catch (Exception e) {
// Ignore
}
view.postDelayed(() -> dismissBottomSheet(), 200);
}, 300);
} else {
// No songs at all - all attempts failed
Toast.makeText(requireContext(),
"Could not load songs. Please check your connection.",
Toast.LENGTH_SHORT).show();
dismissBottomSheet();
}
}
};
artistRepository.getInstantMix(artist, 20).observe(getViewLifecycleOwner(), observer);
});
});
TextView playRandom = view.findViewById(R.id.play_random_text_view);