[Android] Skip media loading if uri is invalid

Geoffrey Métais git at videolan.org
Wed May 2 11:53:12 CEST 2018


vlc-android | branch: 3.0.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed May  2 11:52:07 2018 +0200| [39ff0ceb814e28caeccb0f07a95f787555fc6315] | committer: Geoffrey Métais

Skip media loading if uri is invalid

> https://code.videolan.org/videolan/vlc-android/commit/39ff0ceb814e28caeccb0f07a95f787555fc6315
---

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

diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 7e4c047da..2d238ea86 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -244,11 +244,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)
                 VLCOptions.setMediaOptions(media, ctx, flags or mw.flags)
                 /* keeping only video during benchmark */
                 if (isBenchmark) {
@@ -291,6 +295,11 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
         }
     }
 
+    private fun skipMedia() {
+        if (currentIndex != nextIndex) next()
+        else stop(false)
+    }
+
     fun onServiceDestroyed() {
         player.release()
     }
diff --git a/vlc-android/src/org/videolan/vlc/util/FileUtils.java b/vlc-android/src/org/videolan/vlc/util/FileUtils.java
index d26873697..8a91acaca 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;
 
@@ -325,9 +326,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);
@@ -343,8 +342,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;
     }
 
@@ -363,20 +361,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);
@@ -411,10 +407,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