Bastien Léonard avatar Bastien Léonard committed 09758a3

The back button now goes back to the previous directory

Comments (0)

Files changed (2)

src/net/alwaysdata/bastien_leonard/media_hider/BrowserActivity.java

     }
 
     @Override
+    public boolean onKeyDown(int keyCode, KeyEvent event)
+    {
+        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
+        {
+            // If going back to the previous directory worked, swallow the event
+            if (items.goToPreviousPath())
+            {
+                return true;
+            }
+        }
+
+        return super.onKeyDown(keyCode, event);
+    }
+
+    @Override
     public boolean onCreateOptionsMenu(Menu menu)
     {
         MenuInflater inflater = getMenuInflater();
                     Environment.getExternalStorageDirectory()
                     .getAbsolutePath()))
             {
-                items.setCurrentPath(items.getCurrentPath().getParentFile());
+                items.goToPreviousPath();
             }
         }
         else

src/net/alwaysdata/bastien_leonard/media_hider/FileListAdapter.java

 package net.alwaysdata.bastien_leonard.media_hider;
 
 import java.io.File;
+import java.util.Stack;
 
 import android.provider.MediaStore;
 import android.view.*;
     extends ArrayAdapter<String>
 {
     File currentPath;
+    Stack<File> previousPaths;
 
     public FileListAdapter(android.content.Context context, int resource,
                            int textViewResourceId, File currentPath)
     {
         super(context, resource, textViewResourceId);
         this.currentPath = currentPath;
+        previousPaths = new Stack<File>();
     }
 
     public File getCurrentPath()
 
     public void setCurrentPath(File currentPath)
     {
+        previousPaths.push(this.currentPath);
         this.currentPath = currentPath;
     }
 
+    /**
+     * Go back to the previous path that the user navigated to.
+     *
+     * @return false if there is no previous path, otherwise true is returned. 
+     */
+    public boolean goToPreviousPath()
+    {
+        if (previousPaths.empty())
+        {
+            return false;
+        }
+
+        currentPath = previousPaths.pop();
+        fillList();
+
+        return true;
+    }
+
     @Override
     public View getView(int position, View convertView,
                         ViewGroup parent)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.