[Android] BrowserActivity: Implement custom path support

Edward Wang git at videolan.org
Sat Dec 29 19:33:10 CET 2012


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Sat Dec 29 11:32:39 2012 -0500| [1e049837074a107ef5205fce64695755c1fc5145] | committer: Edward Wang

BrowserActivity: Implement custom path support

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

 vlc-android/res/values/strings.xml                 |    3 +
 .../src/org/videolan/vlc/gui/BrowserActivity.java  |   73 +++++++++++++++++++-
 .../src/org/videolan/vlc/gui/BrowserAdapter.java   |   14 +++-
 3 files changed, 86 insertions(+), 4 deletions(-)

diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index b063e83..b2bb6fd 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -150,6 +150,9 @@
     <!-- Preferences -->
     <string name="preferences">Preferences</string>
     <string name="main_prefs_category">Main</string>
+    <string name="add_custom_path">Add a custom path</string>
+    <string name="add_custom_path_description">Enter additional custom directory to scan:</string>
+    <string name="remove_custom_path">Remove custom path</string>
     <string name="privacy_prefs_category">Privacy</string>
     <string name="clear_history">Clear search history</string>
     <string name="advanced_prefs_category">Advanced</string>
diff --git a/vlc-android/src/org/videolan/vlc/gui/BrowserActivity.java b/vlc-android/src/org/videolan/vlc/gui/BrowserActivity.java
index 6c6e519..b7f5d2f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/BrowserActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/BrowserActivity.java
@@ -22,21 +22,33 @@ package org.videolan.vlc.gui;
 
 import java.io.File;
 import java.io.FileFilter;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Stack;
 
 import org.videolan.vlc.Media;
 import org.videolan.vlc.R;
 import org.videolan.vlc.Util;
 
+import android.view.MenuItem;
+
+import android.app.AlertDialog;
 import android.app.ListActivity;
 import android.content.BroadcastReceiver;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Bundle;
+import android.text.InputType;
+import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
+import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.view.KeyEvent;
 import android.view.View;
+import android.widget.EditText;
 import android.widget.ListView;
+import android.widget.Toast;
 
 public class BrowserActivity extends ListActivity {
     public final static String TAG = "VLC/BrowserActivity";
@@ -75,8 +87,17 @@ public class BrowserActivity extends ListActivity {
         filter.addDataScheme("file");
         registerReceiver(messageReceiver, filter);
 
-        mRoots = Util.getStorageDirectories();
+        refreshRoots();
         openStorageDevices(mRoots);
+
+        registerForContextMenu(getListView());
+    }
+
+    private void refreshRoots() {
+        ArrayList<String> list = new ArrayList<String>();
+        list.addAll(Arrays.asList(Util.getStorageDirectories()));
+        list.addAll(Arrays.asList(Util.getCustomDirectories()));
+        mRoots = list.toArray(new String[list.size()]);
     }
 
     @Override
@@ -87,6 +108,28 @@ public class BrowserActivity extends ListActivity {
         mScollStates.clear();
     }
 
+    @Override
+    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
+        int position = ((AdapterContextMenuInfo)menuInfo).position;
+        final File item = mAdapter.getItem(position);
+        if (mCurrentDir != null
+                || item.getPath().equals(BrowserAdapter.ADD_ITEM_PATH)
+                || Arrays.asList(Util.getStorageDirectories()).contains(
+                        item.getPath())) {
+            return;
+        }
+
+        MenuItem delete = menu.add(R.string.remove_custom_path);
+        delete.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+            @Override
+            public boolean onMenuItemClick(MenuItem arg0) {
+                Util.removeCustomDirectory(item.getPath());
+                refresh();
+                return true;
+            }
+        });
+    }
+
     private void openStorageDevices(String roots[]) {
         mCurrentDir = null;
         mAdapter.clear();
@@ -95,6 +138,7 @@ public class BrowserActivity extends ListActivity {
             if (f.exists())
                 mAdapter.add(f);
         }
+        mAdapter.add(new File(BrowserAdapter.ADD_ITEM_PATH));
         mAdapter.sort();
 
         // set scroll position to top
@@ -102,7 +146,8 @@ public class BrowserActivity extends ListActivity {
     }
 
     private void openDir(File file) {
-        if (!file.exists() || file.getPath() == null)
+        if(file == null || !file.exists() || file.getPath() == null
+                || file.getPath().equals(BrowserAdapter.ADD_ITEM_PATH))
             return;
 
         mAdapter.clear();
@@ -125,6 +170,28 @@ public class BrowserActivity extends ListActivity {
     @Override
     protected void onListItemClick(ListView l, View v, int position, long id) {
         File file = mAdapter.getItem(position);
+        if(file.getPath() == BrowserAdapter.ADD_ITEM_PATH) {
+            AlertDialog.Builder b = new AlertDialog.Builder(this);
+            final EditText input = new EditText(this);
+            input.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
+            b.setTitle(R.string.add_custom_path);
+            b.setMessage(R.string.add_custom_path_description);
+            b.setView(input);
+            b.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
+                @Override
+                public void onClick(DialogInterface x, int y) {return;}
+            });
+            b.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
+                @Override
+                public void onClick(DialogInterface dialog, int which) {
+                    Util.addCustomDirectory(input.getText().toString());
+                    refresh();
+                }
+            });
+            b.show();
+            return;
+        }
+
         File[] files = file.listFiles(new DirFilter());
         if (files != null && files.length > 0) {
             // store scroll state
@@ -170,7 +237,7 @@ public class BrowserActivity extends ListActivity {
 
     public void refresh() {
         if (mCurrentDir == null) {
-            mRoots = Util.getStorageDirectories();
+            refreshRoots();
             openStorageDevices(mRoots);
         } else {
             openDir(mCurrentDir);
diff --git a/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java
index 05bec38..3515196 100644
--- a/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java
@@ -45,6 +45,8 @@ public class BrowserAdapter extends ArrayAdapter<File>
                             implements Comparator<File> {
     public final static String TAG = "VLC/BrowserAdapter";
 
+    public final static String ADD_ITEM_PATH = "/add/a/path";
+
     public BrowserAdapter(Context context) {
         super(context, 0);
     }
@@ -76,9 +78,13 @@ public class BrowserAdapter extends ArrayAdapter<File>
         final File item = getItem(position);
         final DatabaseManager dbManager = DatabaseManager.getInstance(view.getContext());
 
-        if (item != null && item.getName() != null) {
+        if(item != null && item.getPath().equals(ADD_ITEM_PATH)) {
+            holder.text.setText(R.string.add_custom_path);
+            holder.check.setVisibility(View.GONE);
+        } else if(item != null && item.getName() != null) {
             Util.setItemBackground(holder.layout, position);
             holder.text.setText(getVisibleName(item));
+            holder.check.setVisibility(View.VISIBLE);
             holder.check.setOnCheckedChangeListener(null);
             holder.check.setTag(item);
             holder.check.setEnabled(true);
@@ -132,6 +138,12 @@ public class BrowserAdapter extends ArrayAdapter<File>
 
     @Override
     public int compare(File file1, File file2) {
+        // float the add item to the bottom
+        if(file1.getPath().equals(ADD_ITEM_PATH))
+            return 1;
+        else if(file2.getPath().equals(ADD_ITEM_PATH))
+            return -1;
+
         return String.CASE_INSENSITIVE_ORDER.compare(file1.getName(), file2.getName());
     }
 



More information about the Android mailing list