[Android] src: Add a context menu to the directory view

Edward Wang git at videolan.org
Wed Jul 11 05:04:41 CEST 2012


android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Tue Jul 10 23:00:21 2012 -0400| [7210159f40a4599bd9d429be3931799035073c69] | committer: Edward Wang

src: Add a context menu to the directory view

> http://git.videolan.org/gitweb.cgi/android.git/?a=commit;h=7210159f40a4599bd9d429be3931799035073c69
---

 vlc-android/res/menu/directory_view.xml            |   18 ++++
 vlc-android/res/values/strings.xml                 |    2 +
 .../src/org/videolan/vlc/gui/DirectoryAdapter.java |   12 ++-
 .../videolan/vlc/gui/DirectoryViewFragment.java    |   99 ++++++++++++++++----
 4 files changed, 113 insertions(+), 18 deletions(-)

diff --git a/vlc-android/res/menu/directory_view.xml b/vlc-android/res/menu/directory_view.xml
new file mode 100644
index 0000000..339aab3
--- /dev/null
+++ b/vlc-android/res/menu/directory_view.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android" >
+    <item
+        android:id="@+id/directory_view_play"
+        android:title="@string/play" />
+    <item
+        android:id="@+id/directory_view_append"
+        android:title="@string/append" />
+    <item
+	    android:id="@+id/directory_view_delete"
+	    android:title="@string/delete" />
+    <item
+        android:id="@+id/directory_view_play_audio"
+        android:title="@string/play_as_audio" />
+    <item
+        android:id="@+id/directory_view_play_video"
+        android:title="@string/play_as_video" />
+</menu>
\ No newline at end of file
diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index a9ea303..8315cf9 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -16,6 +16,8 @@
     <string name="show_player">Show player</string>
     <string name="hide_mini_player">Hide mini-player</string>
     <string name="play">Play</string>
+    <string name="play_as_audio">Play as audio</string>
+    <string name="play_as_video">Play as video</string>
     <string name="append">Append</string>
     <string name="play_all">Play all</string>
     <string name="append_all">Append all</string>
diff --git a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
index f96aff4..eb10681 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
@@ -282,6 +282,10 @@ public class DirectoryAdapter extends BaseAdapter {
         else
             holder.icon.setImageResource(R.drawable.ic_folder);
 
+        if(selectedNode.isFile())
+            v.setLongClickable(true);
+        else
+            v.setLongClickable(false);
         return v;
     }
 
@@ -305,6 +309,11 @@ public class DirectoryAdapter extends BaseAdapter {
         return true;
     }
 
+    public boolean isChildFile(int position) {
+        DirectoryAdapter.Node selectedNode = mCurrentNode.children.get(position);
+        return selectedNode.isFile();
+    }
+
     public String getMediaLocation(int position) {
         return LibVLC.getExistingInstance().nativeToURI(
                 this.mCurrentDir + "/" + mCurrentNode.children.get(position).name
@@ -313,7 +322,8 @@ public class DirectoryAdapter extends BaseAdapter {
 
     public ArrayList<String> getAllMediaLocations() {
         ArrayList<String> a = new ArrayList<String>();
-        for(int i = 0; i < mCurrentNode.children.size(); i++)
+        // i = 1 to exclude ".." folder
+        for(int i = 1; i < mCurrentNode.children.size(); i++)
             a.add(getMediaLocation(i));
         return a;
     }
diff --git a/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java b/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
index 78720f2..64a015e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DirectoryViewFragment.java
@@ -21,6 +21,7 @@
 package org.videolan.vlc.gui;
 
 import java.io.IOException;
+import java.util.ArrayList;
 
 import org.videolan.vlc.AudioServiceController;
 import org.videolan.vlc.LibVLC;
@@ -30,11 +31,19 @@ import org.videolan.vlc.gui.audio.AudioPlayerActivity;
 import org.videolan.vlc.gui.video.VideoPlayerActivity;
 import org.videolan.vlc.interfaces.ISortable;
 
+import android.app.AlertDialog;
 import android.content.Intent;
 import android.os.Bundle;
+import android.view.ContextMenu;
+import android.view.ContextMenu.ContextMenuInfo;
 import android.view.LayoutInflater;
+import android.view.MenuInflater;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AdapterView;
+import android.widget.AdapterView.AdapterContextMenuInfo;
+import android.widget.AdapterView.OnItemLongClickListener;
 import android.widget.ListView;
 
 import com.actionbarsherlock.app.SherlockListFragment;
@@ -56,32 +65,88 @@ public class DirectoryViewFragment extends SherlockListFragment implements ISort
     {
         View v = inflater.inflate(R.layout.directory_view, container, false);
         setListAdapter(mDirectoryAdapter);
+        final ListView listView = (ListView)v.findViewById(android.R.id.list);
+        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
 
+            @Override
+            public boolean onItemLongClick(AdapterView<?> adapterView, View v, int position, long id) {
+                if(mDirectoryAdapter.isChildFile(position)) {
+                    return false;
+                } else {
+                    return true; /* Terminate the automatic context menu */
+                }
+            }
+        });
+
+        registerForContextMenu(listView);
         return v;
     }
 
     @Override
+    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
+        MenuInflater menuInflater = getActivity().getMenuInflater();
+        menuInflater.inflate(R.menu.directory_view, menu);
+    }
+
+    @Override
+    public boolean onContextItemSelected(MenuItem item) {
+        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
+        int id = item.getItemId();
+        String mediaLocation = mDirectoryAdapter.getMediaLocation(info.position);
+        if(id == R.id.directory_view_play) {
+            openMediaFile(info.position);
+            return true;
+        } else if(id == R.id.directory_view_append) {
+            AudioServiceController audioController = AudioServiceController.getInstance();
+            ArrayList<String> tmp = new ArrayList<String>();
+            tmp.add(mediaLocation);
+            audioController.append(tmp);
+            return true;
+        } else if(id == R.id.directory_view_delete) {
+            AlertDialog alertDialog = CommonDialogs.deleteMedia(getActivity(), mediaLocation, null);
+            alertDialog.show();
+        } else if(id == R.id.directory_view_play_audio) {
+            AudioServiceController audioController = AudioServiceController.getInstance();
+            ArrayList<String> arrayList = new ArrayList<String>();
+            arrayList.add(mediaLocation);
+            audioController.load(arrayList, 0);
+            Intent intent = new Intent(getActivity(), AudioPlayerActivity.class);
+            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+            startActivity(intent);
+        } else if(id == R.id.directory_view_play_video) {
+            Intent intent = new Intent(getActivity(), VideoPlayerActivity.class);
+            intent.putExtra("itemLocation", mediaLocation);
+            startActivity(intent);
+        }
+        return super.onContextItemSelected(item);
+    }
+
+    @Override
     public void onListItemClick(ListView l, View v, int p, long id) {
         Boolean success = mDirectoryAdapter.browse(p);
         if(!success) { /* Clicked on a media file */
-            AudioServiceController audioController = AudioServiceController.getInstance();
-            String mediaFile = mDirectoryAdapter.getMediaLocation(p);
-
-            try {
-                if(!LibVLC.getExistingInstance().hasVideoTrack(mediaFile)) {
-                    audioController.load(mDirectoryAdapter.getAllMediaLocations(), p);
-                    Intent intent = new Intent(getActivity(), AudioPlayerActivity.class);
-                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-                    startActivity(intent);
-                } else {
-                    audioController.stop();
-                    Intent intent = new Intent(getActivity(), VideoPlayerActivity.class);
-                    intent.putExtra("itemLocation", mediaFile);
-                    startActivity(intent);
-                }
-            } catch (IOException e) {
-                /* disk error maybe? */
+            openMediaFile(p);
+        }
+    }
+
+    private void openMediaFile(int p) {
+        AudioServiceController audioController = AudioServiceController.getInstance();
+        String mediaFile = mDirectoryAdapter.getMediaLocation(p);
+
+        try {
+            if(!LibVLC.getExistingInstance().hasVideoTrack(mediaFile)) {
+                audioController.load(mDirectoryAdapter.getAllMediaLocations(), p-1); /* p-1 to exclude ".," */
+                Intent intent = new Intent(getActivity(), AudioPlayerActivity.class);
+                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+                startActivity(intent);
+            } else {
+                audioController.stop();
+                Intent intent = new Intent(getActivity(), VideoPlayerActivity.class);
+                intent.putExtra("itemLocation", mediaFile);
+                startActivity(intent);
             }
+        } catch (IOException e) {
+            /* disk error maybe? */
         }
     }
 



More information about the Android mailing list