[Android] Show a list of available storages in the directory view

Ludovic Fauvet git at videolan.org
Wed Nov 21 19:06:48 CET 2012


vlc-ports/android | branch: master | Ludovic Fauvet <etix at videolan.org> | Wed Nov 21 18:59:22 2012 +0100| [057ff4a9355d96f07634b6f9fa4c46a3637a95ad] | committer: Ludovic Fauvet

Show a list of available storages in the directory view

Internally, it adds support for multiple root directories in the
fragment. The code is bit dirty, improvements are welcome.

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

 .../src/org/videolan/vlc/gui/DirectoryAdapter.java |   85 ++++++++++++++------
 .../videolan/vlc/gui/DirectoryViewFragment.java    |    8 +-
 2 files changed, 62 insertions(+), 31 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
index 84731b1..5f1cb6d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
@@ -122,6 +122,20 @@ public class DirectoryAdapter extends BaseAdapter {
     }
 
     public void populateNode(DirectoryAdapter.Node n, String path) {
+        if (path == null) {
+            // We're on the storage list
+            String storages[] = Util.getStorageDirectories();
+            for (String storage : storages) {
+                File f = new File(storage);
+                DirectoryAdapter.Node child = new DirectoryAdapter.Node(f.getName());
+                child.isFile = false;
+                this.populateNode(child, storage);
+                n.children.add(child);
+            }
+            return;
+        }
+
+
         File file = new File(path);
         if(!file.exists() || !file.isDirectory())
             return;
@@ -168,36 +182,28 @@ public class DirectoryAdapter extends BaseAdapter {
             }
             Collections.sort(n.children);
         }
-        /* Don't let the user escape into the wild by jumping above the root dir */
-        if(mCurrentDir.contains(mRootDir) && !mCurrentDir.equals(mRootDir)) {
-            DirectoryAdapter.Node up = new DirectoryAdapter.Node("..");
-            n.children.add(0, up);
-        }
+
+        DirectoryAdapter.Node up = new DirectoryAdapter.Node("..");
+        n.children.add(0, up);
     }
 
     private LayoutInflater mInflater;
     private DirectoryAdapter.Node mRootNode;
     private DirectoryAdapter.Node mCurrentNode;
-    private String mRootDir;
     private String mCurrentDir;
+    private String mCurrentRoot;
 
     public DirectoryAdapter() {
-        DirectoryAdapter_Core(
-                android.os.Environment.getExternalStorageDirectory().getPath()
-        );
-    }
-
-    public DirectoryAdapter(Context context, String rootDir) {
-        DirectoryAdapter_Core(rootDir);
+        DirectoryAdapter_Core(null);
     }
 
     private void DirectoryAdapter_Core(String rootDir) {
-        rootDir = Util.stripTrailingSlash(rootDir);
+        if (rootDir != null)
+            rootDir = Util.stripTrailingSlash(rootDir);
         Log.v(TAG, "rootMRL is " + rootDir);
         mInflater = LayoutInflater.from(VLCApplication.getAppContext());
         mRootNode = new DirectoryAdapter.Node(rootDir);
         mCurrentDir = rootDir;
-        mRootDir = rootDir;
         this.populateNode(mRootNode, rootDir);
         mCurrentNode = mRootNode;
     }
@@ -290,14 +296,33 @@ public class DirectoryAdapter extends BaseAdapter {
     }
 
     public boolean browse(String directoryName) {
-        try {
-            this.mCurrentDir = new URI(
-                    Util.PathToURI(this.mCurrentDir + "/" + directoryName))
-                    .normalize().getPath();
-            this.mCurrentDir = Util.stripTrailingSlash(this.mCurrentDir);
-        } catch (URISyntaxException e) {
-            e.printStackTrace();
-            return false;
+        if (this.mCurrentDir == null) {
+            // We're on the storage list
+            String storages[] = Util.getStorageDirectories();
+            for (String storage : storages) {
+                storage = Util.stripTrailingSlash(storage);
+                if (storage.endsWith(directoryName)) {
+                    this.mCurrentRoot = storage;
+                    this.mCurrentDir = storage;
+                    this.mCurrentDir = Util.stripTrailingSlash(this.mCurrentDir);
+                    break;
+                }
+            }
+        } else {
+            try {
+                this.mCurrentDir = new URI(
+                        Util.PathToURI(this.mCurrentDir + "/" + directoryName))
+                        .normalize().getPath();
+                this.mCurrentDir = Util.stripTrailingSlash(this.mCurrentDir);
+                if (this.mCurrentDir.equals(getParentDir(this.mCurrentRoot))) {
+                    // Returning on the storage list
+                    this.mCurrentDir = null;
+                    this.mCurrentRoot = null;
+                }
+            } catch (URISyntaxException e) {
+                e.printStackTrace();
+                return false;
+            }
         }
 
         Log.d(TAG, "Browsing to " + this.mCurrentDir);
@@ -323,8 +348,8 @@ public class DirectoryAdapter extends BaseAdapter {
         );
     }
 
-    public String getmRootDir() {
-        return mRootDir;
+    public boolean isRoot() {
+        return mCurrentDir == null;
     }
 
     public String getmCurrentDir() {
@@ -349,4 +374,14 @@ public class DirectoryAdapter extends BaseAdapter {
 
         this.notifyDataSetChanged();
     }
+
+    private String getParentDir(String path) {
+        try {
+            path = new URI(Util.PathToURI(path + "/.."))
+                    .normalize().getPath();
+        } catch (URISyntaxException e) {
+            e.printStackTrace();
+        }
+        return Util.stripTrailingSlash(path);
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java b/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
index 8df5d54..da75b8f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
@@ -145,12 +145,8 @@ public class DirectoryViewFragment extends SherlockListFragment implements ISort
     }
 
     public boolean isRootDirectory () {
-        if (mDirectoryAdapter.getmCurrentDir() != null &&
-            mDirectoryAdapter.getmCurrentDir().equals(mDirectoryAdapter.getmRootDir())) {
-            return true;
-        }
-        return false;
-    };
+        return mDirectoryAdapter.isRoot();
+    }
 
     public void showParentDirectory() {
             mDirectoryAdapter.browse("..");



More information about the Android mailing list