[Android] directory view sets selection to previously visited folder (close #9727)

Vojtech Letal git at videolan.org
Wed Mar 5 22:52:41 CET 2014


vlc-ports/android | branch: master | Vojtech Letal <letal.vojtech at gmail.com> | Wed Mar  5 12:51:18 2014 +0100| [98f194eae1a3790ad48619f0e863cff5df5339ef] | committer: Adrien Maglo

directory view sets selection to previously visited folder (close #9727)

Signed-off-by: Adrien Maglo <magsoft at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=98f194eae1a3790ad48619f0e863cff5df5339ef
---

 .../src/org/videolan/vlc/gui/DirectoryAdapter.java |   42 ++++++++++++++++----
 .../videolan/vlc/gui/DirectoryViewFragment.java    |   13 ++++--
 2 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
index c1da3a1..3b5d6db 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
@@ -25,6 +25,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.ListIterator;
 import java.util.regex.Pattern;
 
 import org.videolan.libvlc.LibVLC;
@@ -108,6 +109,19 @@ public class DirectoryAdapter extends BaseAdapter {
             return false;
         }
 
+        public int getChildPosition(DirectoryAdapter.Node child){
+            if(child == null)
+                return -1;
+
+            ListIterator<DirectoryAdapter.Node> it = children.listIterator();
+            while(it.hasNext()){
+                DirectoryAdapter.Node node = it.next();
+                if(child.equals(node)) return it.previousIndex();
+            }
+
+            return -1;
+        }
+
         public DirectoryAdapter.Node ensureExists(String _n) {
             for(DirectoryAdapter.Node n : this.children) {
                 if(Util.nullEquals(n.name, _n)) return n;
@@ -331,13 +345,23 @@ public class DirectoryAdapter extends BaseAdapter {
         return v;
     }
 
-    public boolean browse(int position) {
+    /**
+     * @return position of the current directory in a newly formed listview
+     * or -1 if opening not successful
+     * */
+
+    public int browse(int position) {
         DirectoryAdapter.Node selectedNode = mCurrentNode.children.get(position);
-        if(selectedNode.isFile()) return false;
+        if(selectedNode.isFile()) return -1;
         return browse(selectedNode.name);
     }
 
-    public boolean browse(String directoryName) {
+    /**
+     * @return position of the current directory in a newly formed listview
+     * or -1 if opening not successful
+     * */
+
+    public int browse(String directoryName) {
         if (this.mCurrentDir == null) {
             // We're on the storage list
             String storages[] = Util.getMediaDirectories();
@@ -363,18 +387,20 @@ public class DirectoryAdapter extends BaseAdapter {
                 }
             } catch(URISyntaxException e) {
                 Log.e(TAG, "URISyntaxException in browse()", e);
-                return false;
+                return -1;
             } catch(NullPointerException e) {
                 Log.e(TAG, "NullPointerException in browse()", e);
-                return false;
+                return -1;
             }
         }
 
         Log.d(TAG, "Browsing to " + this.mCurrentDir);
 
-        if(directoryName.equals(".."))
+        int currentDirPosition = 0;
+        if(directoryName.equals("..")){
+            currentDirPosition = mCurrentNode.parent.getChildPosition(mCurrentNode);
             this.mCurrentNode = this.mCurrentNode.parent;
-        else {
+        } else {
             this.mCurrentNode = this.mCurrentNode.getChildNode(directoryName);
             if(mCurrentNode.subfolderCount() < 1) {
                 // Clear the ".." entry
@@ -384,7 +410,7 @@ public class DirectoryAdapter extends BaseAdapter {
         }
 
         this.notifyDataSetChanged();
-        return true;
+        return (currentDirPosition == -1) ? 0 : currentDirPosition;
     }
 
     public boolean isChildFile(int position) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java b/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
index d656f5a..0392ce7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
@@ -152,10 +152,12 @@ public class DirectoryViewFragment extends SherlockListFragment implements ISort
 
     @Override
     public void onListItemClick(ListView l, View v, int p, long id) {
-        Boolean success = mDirectoryAdapter.browse(p);
-        if(!success) { /* Clicked on a media file */
+        int success = mDirectoryAdapter.browse(p);
+
+        if(success < 0) /* Clicked on a media file */
             openMediaFile(p);
-        }
+        else
+            setSelection(success);
     }
 
     public boolean isRootDirectory () {
@@ -163,7 +165,10 @@ public class DirectoryViewFragment extends SherlockListFragment implements ISort
     }
 
     public void showParentDirectory() {
-        mDirectoryAdapter.browse("..");
+        int success = mDirectoryAdapter.browse("..");
+
+        if(success >= 0)
+            setSelection(success);
     };
 
     private void openMediaFile(int p) {



More information about the Android mailing list