From c7d6cd358cb9c7c14a92dc3b35eabf8695d35999 Mon Sep 17 00:00:00 2001 From: Tony Tam <149837+tonytamsf@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:33:16 -0800 Subject: [PATCH] Honor sort in episode list view in Android Auto (#6756) --- .../fragment/AllEpisodesFragment.java | 27 +++++++------------ .../preferences/PreferenceUpgrader.java | 17 ++++++++++++ .../service/playback/PlaybackService.java | 6 ++--- .../antennapod/core/storage/DBReader.java | 9 +++++-- .../storage/preferences/UserPreferences.java | 21 +++++++++++++++ 5 files changed, 58 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java index 7061a69f3..65693965b 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java @@ -1,7 +1,5 @@ package de.danoeh.antennapod.fragment; -import android.content.Context; -import android.content.SharedPreferences; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MenuItem; @@ -14,6 +12,8 @@ import de.danoeh.antennapod.dialog.AllEpisodesFilterDialog; import de.danoeh.antennapod.model.feed.FeedItem; import de.danoeh.antennapod.model.feed.FeedItemFilter; import de.danoeh.antennapod.model.feed.SortOrder; +import de.danoeh.antennapod.storage.preferences.UserPreferences; + import org.apache.commons.lang3.StringUtils; import org.greenrobot.eventbus.Subscribe; @@ -26,10 +26,7 @@ import java.util.List; */ public class AllEpisodesFragment extends EpisodesListFragment { public static final String TAG = "EpisodesFragment"; - private static final String PREF_NAME = "PrefAllEpisodesFragment"; - private static final String PREF_FILTER = "filter"; - public static final String PREF_SORT = "prefEpisodesSort"; - private SharedPreferences prefs; + public static final String PREF_NAME = "PrefAllEpisodesFragment"; @NonNull @Override @@ -40,7 +37,6 @@ public class AllEpisodesFragment extends EpisodesListFragment { toolbar.setTitle(R.string.episodes_label); updateToolbar(); updateFilterUi(); - prefs = getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); txtvInformation.setOnClickListener( v -> AllEpisodesFilterDialog.newInstance(getFilter()).show(getChildFragmentManager(), null)); return root; @@ -61,13 +57,15 @@ public class AllEpisodesFragment extends EpisodesListFragment { @NonNull @Override protected List loadData() { - return DBReader.getEpisodes(0, page * EPISODES_PER_PAGE, getFilter(), getSortOrder()); + return DBReader.getEpisodes(0, page * EPISODES_PER_PAGE, getFilter(), + UserPreferences.getAllEpisodesSortOrder()); } @NonNull @Override protected List loadMoreData(int page) { - return DBReader.getEpisodes((page - 1) * EPISODES_PER_PAGE, EPISODES_PER_PAGE, getFilter(), getSortOrder()); + return DBReader.getEpisodes((page - 1) * EPISODES_PER_PAGE, EPISODES_PER_PAGE, getFilter(), + UserPreferences.getAllEpisodesSortOrder()); } @Override @@ -77,8 +75,7 @@ public class AllEpisodesFragment extends EpisodesListFragment { @Override protected FeedItemFilter getFilter() { - SharedPreferences prefs = getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); - return new FeedItemFilter(prefs.getString(PREF_FILTER, "")); + return new FeedItemFilter(UserPreferences.getPrefFilterAllEpisodes()); } @Override @@ -119,13 +116,13 @@ public class AllEpisodesFragment extends EpisodesListFragment { } private void saveSortOrderAndRefresh(SortOrder type) { - prefs.edit().putString(PREF_SORT, "" + type.code).apply(); + UserPreferences.setAllEpisodesSortOrder(type); loadItems(); } @Subscribe public void onFilterChanged(AllEpisodesFilterDialog.AllEpisodesFilterChangedEvent event) { - prefs.edit().putString(PREF_FILTER, StringUtils.join(event.filterValues, ",")).apply(); + UserPreferences.setPrefFilterAllEpisodes(StringUtils.join(event.filterValues, ",")); updateFilterUi(); page = 1; loadItems(); @@ -143,8 +140,4 @@ public class AllEpisodesFragment extends EpisodesListFragment { toolbar.getMenu().findItem(R.id.action_favorites).setIcon( getFilter().showIsFavorite ? R.drawable.ic_star : R.drawable.ic_star_border); } - - private SortOrder getSortOrder() { - return SortOrder.fromCodeString(prefs.getString(PREF_SORT, "" + SortOrder.DATE_NEW_OLD.code)); - } } diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceUpgrader.java b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceUpgrader.java index cc298b38d..3a3063599 100644 --- a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceUpgrader.java +++ b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceUpgrader.java @@ -6,12 +6,15 @@ import android.view.KeyEvent; import androidx.core.app.NotificationManagerCompat; import androidx.preference.PreferenceManager; +import org.apache.commons.lang3.StringUtils; + import java.util.concurrent.TimeUnit; import de.danoeh.antennapod.BuildConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.preferences.SleepTimerPreferences; import de.danoeh.antennapod.error.CrashReportWriter; +import de.danoeh.antennapod.fragment.AllEpisodesFragment; import de.danoeh.antennapod.storage.preferences.UserPreferences; import de.danoeh.antennapod.storage.preferences.UserPreferences.EnqueueLocation; import de.danoeh.antennapod.fragment.QueueFragment; @@ -149,5 +152,19 @@ public class PreferenceUpgrader { if (oldVersion < 3020000) { NotificationManagerCompat.from(context).deleteNotificationChannel("auto_download"); } + + if (oldVersion < 3030000) { + SharedPreferences allEpisodesPreferences = + context.getSharedPreferences(AllEpisodesFragment.PREF_NAME, Context.MODE_PRIVATE); + String oldEpisodeSort = allEpisodesPreferences.getString(UserPreferences.PREF_SORT_ALL_EPISODES, ""); + if (!StringUtils.isAllEmpty(oldEpisodeSort)) { + prefs.edit().putString(UserPreferences.PREF_SORT_ALL_EPISODES, oldEpisodeSort).apply(); + } + + String oldEpisodeFilter = allEpisodesPreferences.getString("filter", ""); + if (!StringUtils.isAllEmpty(oldEpisodeFilter)) { + prefs.edit().putString(UserPreferences.PREF_FILTER_ALL_EPISODES, oldEpisodeFilter).apply(); + } + } } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 5c8dead81..9242ea2c1 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -89,7 +89,6 @@ import de.danoeh.antennapod.model.feed.FeedItem; import de.danoeh.antennapod.model.feed.FeedItemFilter; import de.danoeh.antennapod.model.feed.FeedMedia; import de.danoeh.antennapod.model.feed.FeedPreferences; -import de.danoeh.antennapod.model.feed.SortOrder; import de.danoeh.antennapod.model.playback.MediaType; import de.danoeh.antennapod.model.playback.Playable; import de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer; @@ -429,10 +428,11 @@ public class PlaybackService extends MediaBrowserServiceCompat { new FeedItemFilter(FeedItemFilter.DOWNLOADED), UserPreferences.getDownloadsSortedOrder()); } else if (parentId.equals(getResources().getString(R.string.episodes_label))) { feedItems = DBReader.getEpisodes(0, MAX_ANDROID_AUTO_EPISODES_PER_FEED, - new FeedItemFilter(FeedItemFilter.UNPLAYED), SortOrder.DATE_NEW_OLD); + new FeedItemFilter(FeedItemFilter.UNPLAYED), UserPreferences.getAllEpisodesSortOrder()); } else if (parentId.startsWith("FeedId:")) { long feedId = Long.parseLong(parentId.split(":")[1]); - feedItems = DBReader.getFeedItemList(DBReader.getFeed(feedId)); + Feed feed = DBReader.getFeed(feedId); + feedItems = DBReader.getFeedItemList(feed, FeedItemFilter.unfiltered(), feed.getSortOrder()); } else if (parentId.equals(getString(R.string.recently_played_episodes))) { Playable playable = PlaybackPreferences.createInstanceFromPreferences(this); if (playable instanceof FeedMedia) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java index d83557b0c..1b1027c49 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java @@ -15,9 +15,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import de.danoeh.antennapod.core.util.FeedItemPermutors; import de.danoeh.antennapod.core.util.LongList; import de.danoeh.antennapod.core.util.comparator.DownloadResultComparator; -import de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator; import de.danoeh.antennapod.core.util.comparator.PlaybackCompletionDateComparator; import de.danoeh.antennapod.model.feed.Chapter; import de.danoeh.antennapod.model.feed.Feed; @@ -170,13 +170,18 @@ public final class DBReader { } public static List getFeedItemList(final Feed feed, final FeedItemFilter filter) { + return getFeedItemList(feed, filter, SortOrder.DATE_NEW_OLD); + } + + public static List getFeedItemList(final Feed feed, final FeedItemFilter filter, SortOrder sortOrder) { Log.d(TAG, "getFeedItemList() called with: " + "feed = [" + feed + "]"); PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); try (Cursor cursor = adapter.getItemsOfFeedCursor(feed, filter)) { List items = extractItemlistFromCursor(adapter, cursor); - Collections.sort(items, new FeedItemPubdateComparator()); + FeedItemPermutors.getPermutor(sortOrder).reorder(items); + feed.setItems(items); for (FeedItem item : items) { item.setFeed(feed); } diff --git a/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java b/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java index 80a58525d..7b15584ba 100644 --- a/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java +++ b/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java @@ -67,6 +67,10 @@ public class UserPreferences { private static final String PREF_DOWNLOADS_SORTED_ORDER = "prefDownloadSortedOrder"; private static final String PREF_INBOX_SORTED_ORDER = "prefInboxSortedOrder"; + // Episode + public static final String PREF_SORT_ALL_EPISODES = "prefEpisodesSort"; + public static final String PREF_FILTER_ALL_EPISODES = "prefEpisodesFilter"; + // Playback public static final String PREF_PAUSE_ON_HEADSET_DISCONNECT = "prefPauseOnHeadsetDisconnect"; public static final String PREF_UNPAUSE_ON_HEADSET_RECONNECT = "prefUnpauseOnHeadsetReconnect"; @@ -867,4 +871,21 @@ public class UserPreferences { public static boolean shouldShowSubscriptionTitle() { return prefs.getBoolean(PREF_SUBSCRIPTION_TITLE, false); } + + public static void setAllEpisodesSortOrder(SortOrder s) { + prefs.edit().putString(PREF_SORT_ALL_EPISODES, "" + s.code).apply(); + } + + public static SortOrder getAllEpisodesSortOrder() { + return SortOrder.fromCodeString(prefs.getString(PREF_SORT_ALL_EPISODES, + "" + SortOrder.DATE_NEW_OLD.code)); + } + + public static String getPrefFilterAllEpisodes() { + return prefs.getString(PREF_FILTER_ALL_EPISODES, ""); + } + + public static void setPrefFilterAllEpisodes(String filter) { + prefs.edit().putString(PREF_FILTER_ALL_EPISODES, filter).apply(); + } }