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

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>