[Android] VideoPlayerActivity: allow opening a specific subtitle file

Edward Wang git at videolan.org
Sun Jun 16 05:55:51 CEST 2013


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Fri Jun 14 18:58:34 2013 -0400| [4d28bf50aa1becab06d056983564c04dc11a35b7] | committer: Edward Wang

VideoPlayerActivity: allow opening a specific subtitle file

Close #7585

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

 vlc-android/res/layout/advanced_options.xml        |   16 ++++++
 vlc-android/res/values/strings.xml                 |    3 ++
 .../src/org/videolan/vlc/gui/CommonDialogs.java    |   53 ++++++++++++++++++++
 .../vlc/gui/video/VideoPlayerActivity.java         |   26 ++++++++++
 4 files changed, 98 insertions(+)

diff --git a/vlc-android/res/layout/advanced_options.xml b/vlc-android/res/layout/advanced_options.xml
index 8101fab..60a99c1 100644
--- a/vlc-android/res/layout/advanced_options.xml
+++ b/vlc-android/res/layout/advanced_options.xml
@@ -33,6 +33,22 @@
         <org.videolan.vlc.gui.expandable.JumpToTime
             android:layout_width="match_parent"
             android:layout_height="wrap_content" />
+
+        <View
+            android:layout_width="match_parent"
+            android:layout_height="1px"
+            android:background="#8FFF"
+            tools:ignore="PxUsage"
+            android:id="@+id/add_subtitle_divider" />
+
+        <TextView
+            android:id="@+id/add_subtitle"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/subtitle_label"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:layout_margin="10dp"
+            android:background="@drawable/background_item" />
     </LinearLayout>
 
 </ScrollView>
\ No newline at end of file
diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index c3c743c..7e91747 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -124,6 +124,9 @@
     <string name="sleep_title">Sleep timer</string>
     <string name="sleep_cancel">Cancel sleep timer</string>
     <string name="jump_to_time">Jump to Time</string>
+    <string name="subtitle_label">Add subtitle file</string>
+    <string name="subtitle_select">Select subtitle file</string>
+    <string name="no_file_picker_found">No file picker was found on the system.</string>
 
     <string name="open">Open</string>
     <string name="open_mrl">Open MRL</string>
diff --git a/vlc-android/src/org/videolan/vlc/gui/CommonDialogs.java b/vlc-android/src/org/videolan/vlc/gui/CommonDialogs.java
index dc3e213..49c6bf7 100644
--- a/vlc-android/src/org/videolan/vlc/gui/CommonDialogs.java
+++ b/vlc-android/src/org/videolan/vlc/gui/CommonDialogs.java
@@ -30,16 +30,24 @@ import org.videolan.vlc.VlcRunnable;
 import org.videolan.vlc.interfaces.OnExpandableListener;
 import org.videolan.vlc.widget.ExpandableLayout;
 
+import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.AlertDialog.Builder;
+import android.content.ActivityNotFoundException;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.net.Uri;
+import android.util.Log;
 import android.view.ContextThemeWrapper;
 import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.WindowManager.LayoutParams;
 import android.widget.LinearLayout;
+import android.widget.TextView;
+import android.widget.Toast;
 
 public class CommonDialogs {
     public final static String TAG = "VLC/CommonDialogs";
@@ -47,6 +55,8 @@ public class CommonDialogs {
     public static enum MenuType {
         Video, Audio
     };
+    public static final int INTENT_SPECIFIC = 10; // PICK_FILE intent
+    public static final int INTENT_GENERIC = 20; // generic CATEGORY_OPENABLE
 
     public static AlertDialog deleteMedia(final Context context,
                                           final String addressMedia,
@@ -102,6 +112,49 @@ public class CommonDialogs {
             }
         }
 
+        View add_subtitle_divider = view.findViewById(R.id.add_subtitle_divider);
+        TextView add_subtitle = (TextView)view.findViewById(R.id.add_subtitle);
+        if(t == MenuType.Video) {
+            add_subtitle.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    Intent intent = new Intent("org.openintents.action.PICK_FILE");
+
+                    File file = new File(android.os.Environment.getExternalStorageDirectory().getPath());
+                    intent.setData(Uri.fromFile(file));
+
+                    // Set fancy title and button (optional)
+                    intent.putExtra("org.openintents.extra.TITLE", context.getString(R.string.subtitle_select));
+                    intent.putExtra("org.openintents.extra.BUTTON_TEXT", context.getString(R.string.open));
+
+                    if (context
+                            .getPackageManager()
+                            .queryIntentActivities(intent,
+                                    PackageManager.MATCH_DEFAULT_ONLY).size() > 0) {
+                        ((Activity)context).startActivityForResult(intent, INTENT_SPECIFIC);
+                    } else {
+                        // OI intent not found, trying anything
+                        Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT);
+                        intent2.setType("*/*");
+                        intent2.addCategory(Intent.CATEGORY_OPENABLE);
+                        try {
+                            ((Activity)context).startActivityForResult(intent2, INTENT_GENERIC);
+                        } catch(ActivityNotFoundException e) {
+                            Log.i(TAG, "No file picker found on system");
+                            Toast.makeText(context,
+                                    R.string.no_file_picker_found,
+                                    Toast.LENGTH_SHORT).show();
+                        }
+                    }
+
+                    dialog.dismiss();
+                }
+            });
+        } else {
+            add_subtitle.setVisibility(View.GONE);
+            add_subtitle_divider.setVisibility(View.GONE);
+        }
+
         // show dialog
         dialog.show();
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 87736f6..30fc3d8 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -180,6 +180,7 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
     // Tracks & Subtitles
     private Map<Integer,String> mAudioTracksList;
     private Map<Integer,String> mSubtitleTracksList;
+    private String mSelectedSubtitleFile = null; // used to store a selected subtitle; see onActivityResult
 
     @Override
     @TargetApi(Build.VERSION_CODES.HONEYCOMB)
@@ -428,6 +429,31 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
             }}, 500);
 
         showOverlay();
+
+        // Add any selected subtitle file from the file picker
+        if(mSelectedSubtitleFile != null) {
+            Log.i(TAG, "Adding user-selected subtitle " + mSelectedSubtitleFile);
+            mLibVLC.addSubtitleTrack(mSelectedSubtitleFile);
+            mSelectedSubtitleFile = null;
+        }
+    }
+
+    @Override
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+        if(data == null) return;
+
+        if(data.getDataString() == null) {
+            Log.d(TAG, "Subtitle selection dialog was cancelled");
+        }
+        if(data.getData() == null) return;
+
+        String uri = data.getData().getPath();
+        if(requestCode == CommonDialogs.INTENT_SPECIFIC) {
+            Log.d(TAG, "Specific subtitle file: " + uri);
+        } else if(requestCode == CommonDialogs.INTENT_GENERIC) {
+            Log.d(TAG, "Generic subtitle file: " + uri);
+        }
+        mSelectedSubtitleFile = data.getData().getPath();
     }
 
     public static void start(Context context, String location) {



More information about the Android mailing list