[Android] [PATCH 3/4] Handle custom directories creation and deletion

Geoffrey Métais geoffrey.metais at gmail.com
Thu Apr 30 14:27:21 CEST 2015


---
 vlc-android/res/drawable-hdpi/ic_add_white.png     | Bin 0 -> 180 bytes
 vlc-android/res/drawable-mdpi/ic_add_white.png     | Bin 0 -> 135 bytes
 vlc-android/res/drawable-xhdpi/ic_add_white.png    | Bin 0 -> 243 bytes
 vlc-android/res/drawable-xxhdpi/ic_add_white.png   | Bin 0 -> 428 bytes
 vlc-android/res/drawable-xxxhdpi/ic_add_white.png  | Bin 0 -> 609 bytes
 vlc-android/res/menu/directory_custom_dir.xml      |   6 +
 vlc-android/res/menu/media_library.xml             |   6 +
 .../src/org/videolan/vlc/gui/MainActivity.java     |  10 +-
 .../vlc/gui/browser/BaseBrowserAdapter.java        | 182 ++++++++++++---------
 .../vlc/gui/browser/BaseBrowserFragment.java       |   4 +-
 .../vlc/gui/browser/FileBrowserFragment.java       |  80 ++++++++-
 .../org/videolan/vlc/util/CustomDirectories.java   |  10 ++
 12 files changed, 216 insertions(+), 82 deletions(-)
 create mode 100644 vlc-android/res/drawable-hdpi/ic_add_white.png
 create mode 100644 vlc-android/res/drawable-mdpi/ic_add_white.png
 create mode 100644 vlc-android/res/drawable-xhdpi/ic_add_white.png
 create mode 100644 vlc-android/res/drawable-xxhdpi/ic_add_white.png
 create mode 100644 vlc-android/res/drawable-xxxhdpi/ic_add_white.png
 create mode 100644 vlc-android/res/menu/directory_custom_dir.xml

diff --git a/vlc-android/res/drawable-hdpi/ic_add_white.png b/vlc-android/res/drawable-hdpi/ic_add_white.png
new file mode 100644
index 0000000000000000000000000000000000000000..3a15191ccd086f909161c6d1b0c979536569044b
GIT binary patch
literal 180
zcmeAS at N?(olHy`uVBq!ia0vp^9w5xY1|&n at ZgvM!)t)YnAr-gYUNz)pVBlbO{4-mR
zE8>R2^(VY{E_nsbYG&XNP;h8qU}RDOlPnq6O&|=II17YP%e2b~B5~p at GZR#h5sBde
gG6XCR6_mUB%0H6-lIo at JKxZ&`y85}Sb4q9e0Gq5Su>b%7

literal 0
HcmV?d00001

diff --git a/vlc-android/res/drawable-mdpi/ic_add_white.png b/vlc-android/res/drawable-mdpi/ic_add_white.png
new file mode 100644
index 0000000000000000000000000000000000000000..6d8e55d3dea22abcb334daf3345416d063717487
GIT binary patch
literal 135
zcmeAS at N?(olHy`uVBq!ia0vp^1|ZDA1|-9oezpTCKTj9OkcwMxZ*1gbFyLW!e7k?k
zrS>U`M+%zcA4rM&-S*J~DguG$-xwhb1|POPatsIZ8NxZB0td>az^n&x+^n~@9uu{n
Q>Ivd|y85}Sb4q9e0FUD#jQ{`u

literal 0
HcmV?d00001

diff --git a/vlc-android/res/drawable-xhdpi/ic_add_white.png b/vlc-android/res/drawable-xhdpi/ic_add_white.png
new file mode 100644
index 0000000000000000000000000000000000000000..078af52b5aa7c50b4925d5868d12eeebba8e7a6a
GIT binary patch
literal 243
zcmeAS at N?(olHy`uVBq!ia0vp^2_VeD1|%QND7OGoyF6VSLn>~)y}pr?!GVLt at pt~O
z9{XQDf^Ik1@|3haGxe1j7!(*7SQr`@7z7v?m>3)w7&sUh7=eQR`<a0T5CIPSak$Qp
qtWM4lnTLlBwFziF0|Tl<X02kZe0$wEJRx5jq~Fuk&t;ucLK6UsD<RYX

literal 0
HcmV?d00001

diff --git a/vlc-android/res/drawable-xxhdpi/ic_add_white.png b/vlc-android/res/drawable-xxhdpi/ic_add_white.png
new file mode 100644
index 0000000000000000000000000000000000000000..944441d8a8e265f77f11a29d784b7d9ee47856c8
GIT binary patch
literal 428
zcmeAS at N?(olHy`uVBq!ia0vp^6F``Q4M;wBd$farfw9=r#WAGf*4vv0c^M2im<_+h
zTg_c4I=@NzF4GBnEl<z&mQ4&o4on^lo(!A{j4BN(4J;FY0$?Fd1+WlMU=mOWp=Mf%
zwK+3~LIVR6Sp<uK0|O&L!k8iQJ%bU#4upkJL7QOPU`B`wJo*@jG81a;lQ<;%Iq~`f
w9xf~sfWZI?1Yj_LLIoI97~uj|bID%T!6wCHc9#8aU at S0ry85}Sb4q9e0PLPgkN^Mx

literal 0
HcmV?d00001

diff --git a/vlc-android/res/drawable-xxxhdpi/ic_add_white.png b/vlc-android/res/drawable-xxxhdpi/ic_add_white.png
new file mode 100644
index 0000000000000000000000000000000000000000..37544db8f9eb9f79c308b8ee28237e5107242f7b
GIT binary patch
literal 609
zcmeAS at N?(olHy`uVBq!ia0vp^2SAvE4M+yv$zf+;VAAk(aSW-r_4f8bP6h)G7RTTD
zzjTyXSf3|IPdH;db;**|mQ4&3s0btOzrG{Dz`z2unt_3lp at D(Hfq_ARfdMGSK|q)+
zO^iUV5&|C{Gv0hdND-?1hdOXr9$@&Oc&uH5M1RNt9Ysz65aSn^Gldw4fs>Ru7&wW5
andQ98_Qiaw{TjgyvcS{T&t;ucLK6Uf{YhK^

literal 0
HcmV?d00001

diff --git a/vlc-android/res/menu/directory_custom_dir.xml b/vlc-android/res/menu/directory_custom_dir.xml
new file mode 100644
index 0000000..7363c8e
--- /dev/null
+++ b/vlc-android/res/menu/directory_custom_dir.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+    <item
+        android:id="@+id/directory_remove_custom_path"
+        android:title="@string/remove_custom_path" />
+</menu>
\ No newline at end of file
diff --git a/vlc-android/res/menu/media_library.xml b/vlc-android/res/menu/media_library.xml
index 65a329f..4dbb4ad 100644
--- a/vlc-android/res/menu/media_library.xml
+++ b/vlc-android/res/menu/media_library.xml
@@ -10,6 +10,12 @@
         android:visible="false"
         vlc:showAsAction="ifRoom" />
     <item
+        android:id="@+id/ml_menu_add_dir"
+        android:icon="@drawable/ic_add_white"
+        android:title="@string/add_custom_path"
+        android:visible="false"
+        vlc:showAsAction="always" />
+    <item
         android:id="@+id/ml_menu_clean"
         android:icon="@drawable/ic_trash_normal_w"
         android:title="@string/clear_history"
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 5f40b37..b6d2308 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -74,6 +74,7 @@ import org.videolan.vlc.gui.SidebarAdapter.SidebarEntry;
 import org.videolan.vlc.gui.audio.AudioBrowserFragment;
 import org.videolan.vlc.gui.audio.AudioPlayer;
 import org.videolan.vlc.gui.browser.BaseBrowserFragment;
+import org.videolan.vlc.gui.browser.FileBrowserFragment;
 import org.videolan.vlc.gui.browser.MediaBrowserFragment;
 import org.videolan.vlc.gui.browser.NetworkBrowserFragment;
 import org.videolan.vlc.gui.video.VideoGridFragment;
@@ -516,7 +517,10 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen
         boolean showLast = current instanceof AudioBrowserFragment || (current instanceof VideoGridFragment && mSettings.getString(PreferencesActivity.VIDEO_LAST, null) != null);
         menu.findItem(R.id.ml_menu_last_playlist).setVisible(showLast);
 
-
+        if (current instanceof FileBrowserFragment && ((FileBrowserFragment) current).isRootDirectory())
+            menu.findItem(R.id.ml_menu_add_dir).setVisible(true);
+        else
+            menu.findItem(R.id.ml_menu_add_dir).setVisible(false);
 
         return super.onPrepareOptionsMenu(menu);
     }
@@ -589,6 +593,10 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen
                 ((NetworkBrowserFragment)current).toggleFavorite();
                 item.setIcon(R.drawable.ic_menu_bookmark_w);
                 break;
+            case R.id.ml_menu_add_dir:
+                if (current != null && current instanceof FileBrowserFragment)
+                    ((FileBrowserFragment) current).showAddDirectoryDialog();
+                break;
         }
         mRootContainer.closeDrawer(mListView);
         return super.onOptionsItemSelected(item);
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
index 8c28031..b473e4c 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -25,6 +25,7 @@ package org.videolan.vlc.gui.browser;
 import android.os.Environment;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -38,11 +39,13 @@ import org.videolan.vlc.MediaDatabase;
 import org.videolan.vlc.MediaWrapper;
 import org.videolan.vlc.R;
 import org.videolan.vlc.gui.audio.MediaComparators;
+import org.videolan.vlc.util.CustomDirectories;
 import org.videolan.vlc.util.Strings;
 import org.videolan.vlc.util.Util;
 
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
@@ -60,6 +63,7 @@ public class BaseBrowserAdapter extends  RecyclerView.Adapter<RecyclerView.ViewH
     BaseBrowserFragment fragment;
     MediaDatabase mDbManager;
     LinkedList<String> mMediaDirsLocation;
+    List<String> mCustomDirsLocation;
 
     public BaseBrowserAdapter(BaseBrowserFragment fragment){
         this.fragment = fragment;
@@ -85,83 +89,99 @@ public class BaseBrowserAdapter extends  RecyclerView.Adapter<RecyclerView.ViewH
     public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
         int viewType = getItemViewType(position);
         if (viewType == TYPE_MEDIA) {
-            final MediaViewHolder vh = (MediaViewHolder) holder;
-            final MediaWrapper media = (MediaWrapper) getItem(position);
-            boolean hasContextMenu = (media.getType() == MediaWrapper.TYPE_AUDIO ||
-                    media.getType() == MediaWrapper.TYPE_VIDEO ||
-                    (media.getType() == MediaWrapper.TYPE_DIR && Util.canWrite(media.getLocation())));
-            vh.checkBox.setVisibility(View.GONE);
-            vh.title.setText(media.getTitle());
-            if (!TextUtils.isEmpty(media.getDescription())) {
-                vh.text.setVisibility(View.VISIBLE);
-                vh.text.setText(media.getDescription());
-            } else
-                vh.text.setVisibility(View.INVISIBLE);
-            vh.icon.setImageResource(getIconResId(media));
-            vh.more.setVisibility(hasContextMenu ? View.VISIBLE : View.GONE);
-            vh.itemView.setOnClickListener(new View.OnClickListener() {
-                @Override
-                public void onClick(View v) {
-                    MediaWrapper mw = (MediaWrapper) getItem(holder.getAdapterPosition());
-                    if (mw.getType() == MediaWrapper.TYPE_DIR)
-                        fragment.browse(mw, holder.getPosition());
-                    else {
-                        int position = 0;
-                        LinkedList<String> mediaLocations = new LinkedList<String>();
-                        MediaWrapper mediaItem;
-                        for (Object item : mMediaList)
-                            if (item instanceof MediaWrapper){
-                                mediaItem = (MediaWrapper) item;
-                                if (mediaItem.getType() == MediaWrapper.TYPE_VIDEO || mediaItem.getType() == MediaWrapper.TYPE_AUDIO) {
-                                    mediaLocations.add(mediaItem.getLocation());
-                                    if (mediaItem.equals(mw))
-                                        position = mediaLocations.size()-1;
-                                }
+            onBindMediaViewHolder(holder, position);
+        } else if (viewType == TYPE_STORAGE) {
+            onBindStorageViewHolder(holder, position);
+        } else {
+            SeparatorViewHolder vh = (SeparatorViewHolder) holder;
+            vh.title.setText(getItem(position).toString());
+        }
+    }
+
+    private void onBindMediaViewHolder(final RecyclerView.ViewHolder holder, int position) {
+        final MediaViewHolder vh = (MediaViewHolder) holder;
+        final MediaWrapper media = (MediaWrapper) getItem(position);
+        boolean hasContextMenu = (media.getType() == MediaWrapper.TYPE_AUDIO ||
+                media.getType() == MediaWrapper.TYPE_VIDEO ||
+                (media.getType() == MediaWrapper.TYPE_DIR && Util.canWrite(media.getLocation())));
+        vh.checkBox.setVisibility(View.GONE);
+        vh.title.setText(media.getTitle());
+        if (!TextUtils.isEmpty(media.getDescription())) {
+            vh.text.setVisibility(View.VISIBLE);
+            vh.text.setText(media.getDescription());
+        } else
+            vh.text.setVisibility(View.INVISIBLE);
+        vh.icon.setImageResource(getIconResId(media));
+        vh.more.setVisibility(hasContextMenu ? View.VISIBLE : View.GONE);
+        vh.itemView.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                MediaWrapper mw = (MediaWrapper) getItem(holder.getAdapterPosition());
+                if (mw.getType() == MediaWrapper.TYPE_DIR)
+                    fragment.browse(mw, holder.getAdapterPosition());
+                else {
+                    int position = 0;
+                    LinkedList<String> mediaLocations = new LinkedList<String>();
+                    MediaWrapper mediaItem;
+                    for (Object item : mMediaList)
+                        if (item instanceof MediaWrapper) {
+                            mediaItem = (MediaWrapper) item;
+                            if (mediaItem.getType() == MediaWrapper.TYPE_VIDEO || mediaItem.getType() == MediaWrapper.TYPE_AUDIO) {
+                                mediaLocations.add(mediaItem.getLocation());
+                                if (mediaItem.equals(mw))
+                                    position = mediaLocations.size() - 1;
                             }
-                        Util.openList(v.getContext(), mediaLocations, position);
-                    }
+                        }
+                    Util.openList(v.getContext(), mediaLocations, position);
                 }
-            });
-            if (hasContextMenu) {
-                vh.more.setOnClickListener(new View.OnClickListener() {
-                    @Override
-                    public void onClick(View v) {
-                        fragment.onPopupMenu(vh.more, holder.getAdapterPosition());
-                    }
-                });
-                vh.itemView.setOnLongClickListener(new View.OnLongClickListener() {
-                    @Override
-                    public boolean onLongClick(View v) {
-                        fragment.mRecyclerView.openContextMenu(holder.getAdapterPosition());
-                        return true;
-                    }
-                });
             }
-        } else if (viewType == TYPE_STORAGE) {
-            final MediaViewHolder vh = (MediaViewHolder) holder;
-            final Storage storage = (Storage) getItem(position);
-            vh.title.setText(storage.getName());
-            vh.icon.setVisibility(View.GONE);
-            vh.checkBox.setVisibility(View.VISIBLE);
-            vh.more.setVisibility(View.GONE);
-            String description = storage.getDescription();
-            if (!TextUtils.isEmpty(description)) {
-                vh.text.setVisibility(View.VISIBLE);
-                vh.text.setText(description);
-            } else
-                vh.text.setVisibility(View.INVISIBLE);
-
-            vh.itemView.setOnClickListener(new View.OnClickListener() {
+        });
+        if (hasContextMenu) {
+            vh.more.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
-                    MediaWrapper mw = new MediaWrapper(((Storage)getItem(vh.getAdapterPosition())).getPath());
-                    mw.setType(MediaWrapper.TYPE_DIR);
-                    fragment.browse(mw, holder.getAdapterPosition());
+                    fragment.onPopupMenu(vh.more, holder.getAdapterPosition());
+                }
+            });
+            vh.itemView.setOnLongClickListener(new View.OnLongClickListener() {
+                @Override
+                public boolean onLongClick(View v) {
+                    fragment.mRecyclerView.openContextMenu(holder.getAdapterPosition());
+                    return true;
                 }
             });
-            vh.checkBox.setChecked(TextUtils.equals(storage.getPath(), Environment.getExternalStorageDirectory().getPath()) ||
-                    mMediaDirsLocation == null || mMediaDirsLocation.isEmpty() ||
-                    mMediaDirsLocation.contains(storage.getPath()));
+        }
+    }
+
+    private void onBindStorageViewHolder(final RecyclerView.ViewHolder holder, int position) {
+        final MediaViewHolder vh = (MediaViewHolder) holder;
+        final Storage storage = (Storage) getItem(position);
+        boolean isPublicStorage = TextUtils.equals(storage.getPath(), Environment.getExternalStorageDirectory().getPath());
+        boolean hasContextMenu = !isPublicStorage && mCustomDirsLocation.contains(storage.getPath());
+        vh.title.setText(storage.getName());
+        vh.icon.setVisibility(View.GONE);
+        vh.checkBox.setVisibility(View.VISIBLE);
+        vh.more.setVisibility(hasContextMenu ? View.VISIBLE : View.GONE);
+        String description = storage.getDescription();
+        if (!TextUtils.isEmpty(description)) {
+            vh.text.setVisibility(View.VISIBLE);
+            vh.text.setText(description);
+        } else
+            vh.text.setVisibility(View.INVISIBLE);
+
+        vh.itemView.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                MediaWrapper mw = new MediaWrapper(((Storage)getItem(vh.getAdapterPosition())).getPath());
+                mw.setType(MediaWrapper.TYPE_DIR);
+                fragment.browse(mw, holder.getAdapterPosition());
+            }
+        });
+        vh.checkBox.setChecked(isPublicStorage ||
+                mMediaDirsLocation == null || mMediaDirsLocation.isEmpty() ||
+                mMediaDirsLocation.contains(storage.getPath()));
+        if (!isPublicStorage) {
+            vh.checkBox.setEnabled(true);
             vh.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                 @Override
                 public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
@@ -182,10 +202,23 @@ public class BaseBrowserAdapter extends  RecyclerView.Adapter<RecyclerView.ViewH
                     }
                 }
             });
-        } else {
-            SeparatorViewHolder vh = (SeparatorViewHolder) holder;
-            vh.title.setText(getItem(position).toString());
-        }
+            if (hasContextMenu) {
+                vh.more.setOnClickListener(new View.OnClickListener() {
+                    @Override
+                    public void onClick(View v) {
+                        fragment.onPopupMenu(vh.more, holder.getAdapterPosition());
+                    }
+                });
+                vh.itemView.setOnLongClickListener(new View.OnLongClickListener() {
+                    @Override
+                    public boolean onLongClick(View v) {
+                        fragment.mRecyclerView.openContextMenu(holder.getAdapterPosition());
+                        return true;
+                    }
+                });
+            }
+        } else
+            vh.checkBox.setEnabled(false);
     }
 
     @Override
@@ -295,6 +328,7 @@ public class BaseBrowserAdapter extends  RecyclerView.Adapter<RecyclerView.ViewH
         for (File dir : mediaDirs){
             mMediaDirsLocation.add(dir.getPath());
         }
+        mCustomDirsLocation = Arrays.asList(CustomDirectories.getCustomDirectories());
     }
 
     public void addAll(ArrayList<MediaWrapper> mediaList){
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
index dee0c1d..fa2d859 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
@@ -337,7 +337,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
         setContextMenu(getActivity().getMenuInflater(), menu, info.position);
     }
 
-    private void setContextMenu(MenuInflater inflater, Menu menu, int position) {
+    protected void setContextMenu(MenuInflater inflater, Menu menu, int position) {
         MediaWrapper mw = (MediaWrapper) mAdapter.getItem(position);
         boolean canWrite = Util.canWrite(mw.getLocation());
         if (mw.getType() == MediaWrapper.TYPE_AUDIO || mw.getType() == MediaWrapper.TYPE_VIDEO) {
@@ -381,7 +381,7 @@ public abstract class BaseBrowserFragment extends MediaBrowserFragment implement
         popupMenu.show();
     }
 
-    private boolean handleContextItemSelected(MenuItem item, int position) {
+    protected boolean handleContextItemSelected(MenuItem item, int position) {
         int id = item.getItemId();
         if (! (mAdapter.getItem(position) instanceof MediaWrapper))
             return super.onContextItemSelected(item);
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
index 34884f5..c11c0f7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/FileBrowserFragment.java
@@ -25,25 +25,32 @@ package org.videolan.vlc.gui.browser;
 
 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.os.Environment;
 import android.support.v4.app.Fragment;
-import android.support.v7.internal.widget.AdapterViewCompat;
-import android.view.ContextMenu;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.widget.AppCompatEditText;
+import android.text.InputType;
+import android.view.Menu;
 import android.view.MenuInflater;
-import android.view.View;
+import android.view.MenuItem;
+import android.widget.Toast;
 
-import org.videolan.vlc.MediaWrapper;
+import org.videolan.libvlc.LibVlcUtil;
+import org.videolan.vlc.MediaDatabase;
 import org.videolan.vlc.R;
 import org.videolan.vlc.util.AndroidDevices;
-import org.videolan.vlc.util.Util;
+import org.videolan.vlc.util.CustomDirectories;
 
 import java.io.File;
 
 public class FileBrowserFragment extends BaseBrowserFragment {
 
+    private AlertDialog mAlertDialog;
+
     public FileBrowserFragment() {
         super();
         ROOT = Environment.getExternalStorageDirectory().getPath();
@@ -67,6 +74,7 @@ public class FileBrowserFragment extends BaseBrowserFragment {
 
     @Override
     protected void browseRoot() {
+        mAdapter.updateMediaDirs();
         String storages[] = AndroidDevices.getMediaDirectories();
         BaseBrowserAdapter.Storage storage;
         for (String mediaDirLocation : storages) {
@@ -106,6 +114,68 @@ public class FileBrowserFragment extends BaseBrowserFragment {
     public void onStop() {
         super.onStop();
         getActivity().unregisterReceiver(storageReceiver);
+        if (mAlertDialog != null && mAlertDialog.isShowing())
+            mAlertDialog.dismiss();
+    }
+
+    public void showAddDirectoryDialog() {
+        final Context context = getActivity();
+        AlertDialog.Builder builder = new AlertDialog.Builder(context);
+        final AppCompatEditText input = new AppCompatEditText(context);
+        if (!LibVlcUtil.isHoneycombOrLater()) {
+            input.setTextColor(getResources().getColor(R.color.grey50));
+        }
+        input.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
+        builder.setTitle(R.string.add_custom_path);
+        builder.setMessage(R.string.add_custom_path_description);
+        builder.setView(input);
+        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialogInterface, int which) {
+                return;
+            }
+        });
+        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                String path = input.getText().toString().trim();
+                File f = new File(path);
+                if (!f.exists() || !f.isDirectory()) {
+                    Toast.makeText(context, getString(R.string.directorynotfound, path), Toast.LENGTH_SHORT).show();
+                    return;
+                }
+
+                CustomDirectories.addCustomDirectory(f.getAbsolutePath());
+                refresh();
+            }
+        });
+        mAlertDialog = builder.show();
+    }
+
+    protected void setContextMenu(MenuInflater inflater, Menu menu, int position) {
+        if (mRoot) {
+            BaseBrowserAdapter.Storage storage = (BaseBrowserAdapter.Storage) mAdapter.getItem(position);
+            boolean isCustom = CustomDirectories.contains(storage.getPath());
+            if (isCustom)
+                inflater.inflate(R.menu.directory_custom_dir, menu);
+        } else
+            super.setContextMenu(inflater, menu, position);
+    }
+
+    @Override
+    protected boolean handleContextItemSelected(MenuItem item, int position) {
+        if (mRoot) {
+            if (item.getItemId() == R.id.directory_remove_custom_path){
+                BaseBrowserAdapter.Storage storage = (BaseBrowserAdapter.Storage) mAdapter.getItem(position);
+                MediaDatabase.getInstance().recursiveRemoveDir(storage.getPath());
+                CustomDirectories.removeCustomDirectory(storage.getPath());
+                mAdapter.updateMediaDirs();
+                mAdapter.removeItem(position, true);
+                return true;
+            } else
+                return false;
+        } else
+            return super.handleContextItemSelected(item, position);
     }
 
     private final BroadcastReceiver storageReceiver = new BroadcastReceiver() {
diff --git a/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java b/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java
index 1a1ac1b..bef2c38 100644
--- a/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java
+++ b/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java
@@ -27,6 +27,7 @@ import org.videolan.vlc.VLCApplication;
 
 import android.content.SharedPreferences;
 import android.preference.PreferenceManager;
+import android.text.TextUtils;
 
 public class CustomDirectories {
 
@@ -81,4 +82,13 @@ public class CustomDirectories {
             return custom_paths.split(":");
     }
 
+    public static boolean contains(String directory){
+        directory = directory.trim();
+        String[] directories = getCustomDirectories();
+        for (int i = 0 ; i < directories.length ; ++i){
+            if (TextUtils.equals(directory, directories[i]))
+                return true;
+        }
+        return false;
+    }
 }
-- 
2.1.4



More information about the Android mailing list