[Android] AudioService: do not try to play a non-existing local file

Ludovic Fauvet git at videolan.org
Wed May 22 15:43:30 CEST 2013


vlc-ports/android | branch: master | Ludovic Fauvet <etix at videolan.org> | Wed May 22 15:21:11 2013 +0200| [14b18bc27fe8d8ffdb8fd03d5562eb9d1e1fbe68] | committer: Ludovic Fauvet

AudioService: do not try to play a non-existing local file

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

 vlc-android/res/values/strings.xml                 |    1 +
 vlc-android/src/org/videolan/vlc/AudioService.java |   45 ++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index 9958cc7..fc879ba 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -143,6 +143,7 @@
 
     <string name="encountered_error_title">Playback error</string>
     <string name="encountered_error_message">VLC encountered an error with this media.\nPlease try refreshing the media library.</string>
+    <string name="invalid_location">The location %1$s cannot be played</string>
 
     <!-- About -->
     <string name="app_name_full">VLC for Android™</string>
diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index 556520e..19b9e8c 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -22,6 +22,7 @@ package org.videolan.vlc;
 
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -31,6 +32,7 @@ import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Stack;
 
 import org.videolan.libvlc.EventHandler;
@@ -60,6 +62,7 @@ import android.media.MediaMetadataRetriever;
 import android.media.RemoteControlClient;
 import android.media.RemoteControlClient.MetadataEditor;
 import android.os.Build;
+import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Message;
@@ -70,6 +73,7 @@ import android.support.v4.app.NotificationCompat;
 import android.telephony.TelephonyManager;
 import android.util.Log;
 import android.widget.RemoteViews;
+import android.widget.Toast;
 
 public class AudioService extends Service {
 
@@ -820,6 +824,10 @@ public class AudioService extends Service {
                     String path = mediaPathList.get(i);
                     Media media = db.getMedia(AudioService.this, path);
                     if(media == null) {
+                        if (!validatePath(path)) {
+                            showToast(getResources().getString(R.string.invalid_location, path), Toast.LENGTH_SHORT);
+                            continue;
+                        }
                         Log.v(TAG, "Creating on-the-fly Media object for " + path);
                         media = new Media(path, false);
                     }
@@ -896,6 +904,10 @@ public class AudioService extends Service {
                 String path = mediaPathList.get(i);
                 Media media = db.getMedia(AudioService.this, path);
                 if(media == null) {
+                    if (!validatePath(path)) {
+                        showToast(getResources().getString(R.string.invalid_location, path), Toast.LENGTH_SHORT);
+                        continue;
+                    }
                     Log.v(TAG, "Creating on-the-fly Media object for " + path);
                     media = new Media(path, false);
                 }
@@ -1104,4 +1116,37 @@ public class AudioService extends Service {
             e.printStackTrace();
         }
     }
+
+    private boolean validatePath(String path)
+    {
+        /* Check if the MRL contains a scheme */
+        if (!path.matches("\\w+://.+"))
+            path = "file://".concat(path);
+        if (path.toLowerCase(Locale.ENGLISH).startsWith("file://")) {
+            /* Ensure the file exists */
+            File f = new File(path);
+            if (!f.isFile())
+                return false;
+        }
+        return true;
+    }
+
+    private void showToast(String text, int duration) {
+        Message msg = new Message();
+        Bundle bundle = new Bundle();
+        bundle.putString("text", text);
+        bundle.putInt("duration", duration);
+        msg.setData(bundle);
+        toastHandler.sendMessage(msg);
+    }
+
+    private static Handler toastHandler = new Handler() {
+        @Override
+        public void handleMessage(Message msg) {
+            final Bundle bundle = msg.getData();
+            final String text = bundle.getString("text");
+            final int duration = bundle.getInt("duration");
+            Toast.makeText(VLCApplication.getAppContext(), text, duration).show();
+        }
+    };
 }



More information about the Android mailing list