[Android] TV: restore current browser state

Geoffrey Métais git at videolan.org
Tue Oct 31 11:52:15 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Oct 31 09:55:28 2017 +0100| [3912ae05d37fb119d228b79f6ffe09e0ed912b11] | committer: Geoffrey Métais

TV: restore current browser state

> https://code.videolan.org/videolan/vlc-android/commit/3912ae05d37fb119d228b79f6ffe09e0ed912b11
---

 .../vlc/gui/tv/browser/MediaSortedFragment.java    |  5 ++++
 .../vlc/gui/tv/browser/SongsBrowserFragment.java   |  5 ++--
 .../vlc/gui/tv/browser/SortedBrowserFragment.java  | 28 +++++++++++++++++-----
 .../vlc/gui/tv/browser/VideoBrowserFragment.java   |  5 ++++
 4 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/MediaSortedFragment.java b/vlc-android/src/org/videolan/vlc/gui/tv/browser/MediaSortedFragment.java
index 49bb52dc8..a9d64dff9 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/MediaSortedFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/MediaSortedFragment.java
@@ -59,6 +59,11 @@ public abstract class MediaSortedFragment extends SortedBrowserFragment implemen
     abstract protected void browseRoot();
 
     @Override
+    protected String getKey() {
+        return mUri != null ? CURRENT_BROWSER_MAP+mUri.getPath() : CURRENT_BROWSER_MAP;
+    }
+
+    @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         if (savedInstanceState != null) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/SongsBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/tv/browser/SongsBrowserFragment.java
index 64465c1bd..2e1063ae1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/SongsBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/SongsBrowserFragment.java
@@ -26,7 +26,6 @@ package org.videolan.vlc.gui.tv.browser;
 
 import android.annotation.TargetApi;
 import android.os.Build;
-import android.os.Bundle;
 import android.support.v17.leanback.widget.Presenter;
 import android.support.v17.leanback.widget.Row;
 import android.support.v17.leanback.widget.RowPresenter;
@@ -46,8 +45,8 @@ public class SongsBrowserFragment extends SortedBrowserFragment {
     private MediaWrapper[] mSongs;
 
     @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
+    protected String getKey() {
+        return CURRENT_BROWSER_MAP+"songs";
     }
 
     @Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/SortedBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/tv/browser/SortedBrowserFragment.java
index e98a02961..4ddbe7431 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/SortedBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/SortedBrowserFragment.java
@@ -72,6 +72,7 @@ public abstract class SortedBrowserFragment extends BrowseSupportFragment implem
     public static final String KEY_URI = "uri";
     public static final String SELECTED_ITEM = "selected";
     public static final String CURRENT_BROWSER_LIST = "CURRENT_BROWSER_LIST";
+    public static final String CURRENT_BROWSER_MAP = "CURRENT_BROWSER_MAP";
 
     public static final int UPDATE_DISPLAY = 1;
     public static final int UPDATE_ITEM = 2;
@@ -80,13 +81,14 @@ public abstract class SortedBrowserFragment extends BrowseSupportFragment implem
 
     protected ArrayObjectAdapter mAdapter = new ArrayObjectAdapter(new ListRowPresenter());
     protected MediaWrapper mItemSelected;
-    protected Map<String, ListItem> mMediaItemMap = new ArrayMap<>();
+    protected Map<String, ListItem> mMediaItemMap = new ArrayMap<>(), mTempMap;
     protected final SimpleArrayMap<String, Integer> mMediaIndex = new SimpleArrayMap<>();
     ArrayList<MediaWrapper> mVideosList = new ArrayList<>();
     protected final BrowserHandler mHandler = new BrowserHandler(this);
     private BackgroundManager mBackgroundManager;
 
     abstract protected void browse();
+    abstract protected String getKey();
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -112,6 +114,15 @@ public abstract class SortedBrowserFragment extends BrowseSupportFragment implem
         setOnItemViewSelectedListener(this);
         if (savedInstanceState == null)
             browse();
+        else {
+            synchronized (mMediaItemMap) {
+                mMediaItemMap = (Map<String, ListItem>) VLCApplication.getData(getKey());
+            }
+            if (mMediaItemMap != null)
+                sort();
+            else
+                getActivity().finish();
+        }
     }
 
     @Override
@@ -140,12 +151,13 @@ public abstract class SortedBrowserFragment extends BrowseSupportFragment implem
         super.onSaveInstanceState(outState);
         if (mItemSelected != null)
             outState.putParcelable(SELECTED_ITEM, mItemSelected);
+        VLCApplication.storeData(getKey(), mMediaItemMap);
     }
 
     public void showDetails() {
         if (mItemSelected == null)
             return;
-        Intent intent = new Intent(getActivity(),
+        final Intent intent = new Intent(getActivity(),
                 DetailsActivity.class);
         // pass the item information
         intent.putExtra("media", mItemSelected);
@@ -190,11 +202,11 @@ public abstract class SortedBrowserFragment extends BrowseSupportFragment implem
         VLCApplication.runBackground(new Runnable() {
             @Override
             public void run() {
-                mMediaItemMap = new TreeMap<>(mMediaItemMap); //sort sections
-                for (ListItem item : mMediaItemMap.values()) {
-                    Collections.sort(item.mediaList, MediaComparators.byFileType);
-                    mVideosList.addAll(item.mediaList);
+                synchronized (mMediaItemMap) {
+                    mTempMap = new TreeMap<>(mMediaItemMap); //sort sections
                 }
+                for (ListItem item : mMediaItemMap.values())
+                    Collections.sort(item.mediaList, MediaComparators.byFileType);
                 mHandler.sendEmptyMessage(UPDATE_DISPLAY);
                 VLCApplication.storeData(CURRENT_BROWSER_LIST, mVideosList);
             }
@@ -210,6 +222,10 @@ public abstract class SortedBrowserFragment extends BrowseSupportFragment implem
         setAdapter(mAdapter);
         ArrayObjectAdapter adapter;
         HeaderItem header;
+        synchronized (mMediaItemMap) {
+            if (mTempMap != null)
+                mMediaItemMap = mTempMap;
+        }
         for (ListItem item : mMediaItemMap.values()) {
             adapter = new ArrayObjectAdapter(new CardPresenter(activity));
             header = new HeaderItem(0, item.Letter);
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.java
index 87592190a..f33f8941f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/VideoBrowserFragment.java
@@ -45,6 +45,11 @@ public class VideoBrowserFragment extends SortedBrowserFragment implements Media
     private Medialibrary mMediaLibrary;
 
     @Override
+    protected String getKey() {
+        return CURRENT_BROWSER_MAP+"videos";
+    }
+
+    @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         mMediaLibrary = VLCApplication.getMLInstance();



More information about the Android mailing list