Show list of downloads on Android Auto

This commit is contained in:
ByteHamster 2021-10-28 22:18:25 +02:00
parent 345aad4148
commit 5d03405d14
2 changed files with 50 additions and 47 deletions

View File

@ -35,6 +35,7 @@ import android.view.SurfaceHolder;
import android.webkit.URLUtil;
import android.widget.Toast;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.core.app.NotificationCompat;
@ -366,30 +367,22 @@ public class PlaybackService extends MediaBrowserServiceCompat {
.subscribe(queueItems -> mediaSession.setQueue(queueItems), Throwable::printStackTrace);
}
private MediaBrowserCompat.MediaItem createBrowsableMediaItemForRoot() {
private MediaBrowserCompat.MediaItem createBrowsableMediaItem(
@StringRes int title, @DrawableRes int icon, int numEpisodes) {
Uri uri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(getResources().getResourcePackageName(R.drawable.ic_playlist_black))
.appendPath(getResources().getResourceTypeName(R.drawable.ic_playlist_black))
.appendPath(getResources().getResourceEntryName(R.drawable.ic_playlist_black))
.authority(getResources().getResourcePackageName(icon))
.appendPath(getResources().getResourceTypeName(icon))
.appendPath(getResources().getResourceEntryName(icon))
.build();
String subtitle = "";
try {
int count = taskManager.getQueue().size();
subtitle = getResources().getQuantityString(R.plurals.num_episodes, count, count);
} catch (InterruptedException e) {
e.printStackTrace();
}
MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
.setIconUri(uri)
.setMediaId(getResources().getString(R.string.queue_label))
.setTitle(getResources().getString(R.string.queue_label))
.setSubtitle(subtitle)
.setMediaId(getResources().getString(title))
.setTitle(getResources().getString(title))
.setSubtitle(getResources().getQuantityString(R.plurals.num_episodes, numEpisodes, numEpisodes))
.build();
return new MediaBrowserCompat.MediaItem(description,
MediaBrowserCompat.MediaItem.FLAG_BROWSABLE);
return new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_BROWSABLE);
}
private MediaBrowserCompat.MediaItem createBrowsableMediaItemForFeed(Feed feed) {
@ -421,46 +414,47 @@ public class PlaybackService extends MediaBrowserServiceCompat {
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> { }, Throwable::printStackTrace);
.subscribe(
() -> {
}, e -> {
e.printStackTrace();
result.sendResult(null);
});
}
private List<MediaBrowserCompat.MediaItem> loadChildrenSynchronous(@NonNull String parentId) {
private List<MediaBrowserCompat.MediaItem> loadChildrenSynchronous(@NonNull String parentId)
throws InterruptedException {
List<MediaBrowserCompat.MediaItem> mediaItems = new ArrayList<>();
if (parentId.equals(getResources().getString(R.string.app_name))) {
// Root List
try {
if (!(taskManager.getQueue().isEmpty())) {
mediaItems.add(createBrowsableMediaItemForRoot());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
mediaItems.add(createBrowsableMediaItem(R.string.queue_label, R.drawable.ic_playlist_black,
taskManager.getQueue().size()));
mediaItems.add(createBrowsableMediaItem(R.string.downloads_label, R.drawable.ic_download_black,
DBReader.getDownloadedItems().size()));
List<Feed> feeds = DBReader.getFeedList();
for (Feed feed : feeds) {
mediaItems.add(createBrowsableMediaItemForFeed(feed));
}
} else if (parentId.equals(getResources().getString(R.string.queue_label))) {
// Child List
try {
for (FeedItem feedItem : taskManager.getQueue()) {
FeedMedia media = feedItem.getMedia();
if (media != null) {
mediaItems.add(media.getMediaItem());
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return mediaItems;
}
List<FeedItem> feedItems;
if (parentId.equals(getResources().getString(R.string.queue_label))) {
feedItems = taskManager.getQueue();
} else if (parentId.equals(getResources().getString(R.string.downloads_label))) {
feedItems = DBReader.getDownloadedItems();
} else if (parentId.startsWith("FeedId:")) {
long feedId = Long.parseLong(parentId.split(":")[1]);
List<FeedItem> feedItems = DBReader.getFeedItemList(DBReader.getFeed(feedId));
int count = 0;
for (FeedItem feedItem : feedItems) {
if (feedItem.getMedia() != null && feedItem.getMedia().getMediaItem() != null) {
mediaItems.add(feedItem.getMedia().getMediaItem());
if (++count >= MAX_ANDROID_AUTO_EPISODES_PER_FEED) {
break;
}
feedItems = DBReader.getFeedItemList(DBReader.getFeed(feedId));
} else {
Log.e(TAG, "Parent ID not found: " + parentId);
return null;
}
int count = 0;
for (FeedItem feedItem : feedItems) {
if (feedItem.getMedia() != null && feedItem.getMedia().getMediaItem() != null) {
mediaItems.add(feedItem.getMedia().getMediaItem());
if (++count >= MAX_ANDROID_AUTO_EPISODES_PER_FEED) {
break;
}
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M18,15v3H6v-3H4v3c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2v-3H18zM17,11l-1.41,-1.41L13,12.17V4h-2v8.17L8.41,9.59L7,11l5,5L17,11z"/>
</vector>