Commit 7210159f authored by Edward Wang's avatar Edward Wang
Browse files

src: Add a context menu to the directory view

parent a48ddb28
<?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
......@@ -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>
......
......@@ -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;
}
......
......@@ -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? */
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment