[Android] Skip media loading if uri is invalid

Geoffrey Métais git at videolan.org
Thu May 3 14:17:13 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu May  3 12:18:57 2018 +0200| [85eca84652645c33886b234fbf22b08638665836] | committer: Geoffrey Métais

Skip media loading if uri is invalid

> https://code.videolan.org/videolan/vlc-android/commit/85eca84652645c33886b234fbf22b08638665836
---

 .../src/org/videolan/vlc/media/PlaylistManager.kt    | 19 ++++++++++++++-----
 vlc-android/src/org/videolan/vlc/util/FileUtils.java | 20 ++++++++------------
 2 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 5550acd32..5f0020ac0 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -254,11 +254,15 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
         launch(UI, CoroutineStart.UNDISPATCHED) {
             if (mw.hasFlag(MediaWrapper.MEDIA_FORCE_AUDIO) && player.getAudioTracksCount() == 0) {
                 determinePrevAndNextIndices(true)
-                if (currentIndex != nextIndex) next()
-                else stop(false)
+                skipMedia()
             } else if (mw.type != MediaWrapper.TYPE_VIDEO || isVideoPlaying || player.hasRenderer
                     || mw.hasFlag(MediaWrapper.MEDIA_FORCE_AUDIO)) {
-                val media = Media(VLCInstance.get(), FileUtils.getUri(mw.uri))
+                val uri = FileUtils.getUri(mw.uri)
+                if (uri == null) {
+                    skipMedia()
+                    return at launch
+                }
+                val media = Media(VLCInstance.get(), uri)
                 setStartTime(media, mw)
                 VLCOptions.setMediaOptions(media, ctx, flags or mw.flags)
                 /* keeping only video during benchmark */
@@ -299,6 +303,11 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
         }
     }
 
+    private fun skipMedia() {
+        if (currentIndex != nextIndex) next()
+        else stop(false)
+    }
+
     fun onServiceDestroyed() {
         player.release()
     }
@@ -670,8 +679,8 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
             }
             MediaPlayer.Event.EncounteredError -> {
                 service.showToast(service.getString(
-                            R.string.invalid_location,
-                            getCurrentMedia()?.getLocation() ?: ""), Toast.LENGTH_SHORT)
+                        R.string.invalid_location,
+                        getCurrentMedia()?.getLocation() ?: ""), Toast.LENGTH_SHORT)
                 next()
             }
         }
diff --git a/vlc-android/src/org/videolan/vlc/util/FileUtils.java b/vlc-android/src/org/videolan/vlc/util/FileUtils.java
index 127825862..4beb093a9 100644
--- a/vlc-android/src/org/videolan/vlc/util/FileUtils.java
+++ b/vlc-android/src/org/videolan/vlc/util/FileUtils.java
@@ -41,6 +41,7 @@ import android.util.Log;
 
 import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.medialibrary.media.MediaWrapper;
+import org.videolan.vlc.BuildConfig;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.media.MediaUtils;
 
@@ -323,9 +324,7 @@ public class FileUtils {
             final ByteBuffer bb = ByteBuffer.allocateDirect((int)chunkSizeForFile);
             int read;
             long position = Math.max(size - HASH_CHUNK_SIZE, 0);
-            while ((read = fileChannel.read(bb, position)) > 0) {
-                position += read;
-            }
+            while ((read = fileChannel.read(bb, position)) > 0) position += read;
             bb.flip();
             tail = computeHashForChunk(bb);
             return String.format("%016x", size + head + tail);
@@ -341,8 +340,7 @@ public class FileUtils {
     private static long computeHashForChunk(ByteBuffer buffer) {
         final LongBuffer longBuffer = buffer.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer();
         long hash = 0;
-        while (longBuffer.hasRemaining())
-            hash += longBuffer.get();
+        while (longBuffer.hasRemaining()) hash += longBuffer.get();
         return hash;
     }
 
@@ -361,20 +359,18 @@ public class FileUtils {
                             new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
                     if (cursor != null && cursor.moveToFirst()) {
                         final String filename = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)).replace("/", "");
-                        Log.i(TAG, "Getting file " + filename + " from content:// URI");
+                        if (BuildConfig.DEBUG) Log.i(TAG, "Getting file " + filename + " from content:// URI");
                         is = ctx.getContentResolver().openInputStream(data);
                         if (is == null) return data;
                         os = new FileOutputStream(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY + "/Download/" + filename);
                         final byte[] buffer = new byte[1024];
                         int bytesRead;
-                        while ((bytesRead = is.read(buffer)) >= 0) {
-                            os.write(buffer, 0, bytesRead);
-                        }
+                        while ((bytesRead = is.read(buffer)) >= 0) os.write(buffer, 0, bytesRead);
                         uri = AndroidUtil.PathToUri(AndroidDevices.EXTERNAL_PUBLIC_DIRECTORY + "/Download/" + filename);
                     }
                 } catch (Exception e) {
                     Log.e(TAG, "Couldn't download file from mail URI");
-                    return data;
+                    return null;
                 } finally {
                     Util.close(is);
                     Util.close(os);
@@ -404,10 +400,10 @@ public class FileUtils {
 //                    }
                 } catch (FileNotFoundException|IllegalArgumentException e) {
                     Log.e(TAG, "Couldn't understand the intent");
-                    return data;
+                    return null;
                 } catch (SecurityException e) {
                     Log.e(TAG, "Permission is no longer valid");
-                    return data;
+                    return null;
                 }
             }
         }



More information about the Android mailing list