[Android] [PATCH 2/2] libvlc: media: replace getMrl by getUri

Thomas Guillem thomas at gllm.fr
Fri Jun 5 13:57:31 CEST 2015


- Convert Android Uri encoding to the VLC one when setting an Uri.
- Convert VLC mrl encoding to the Android one when returning an mrl.
---
 libvlc/src/org/videolan/libvlc/Media.java          | 68 +++++++++++++++++++---
 .../src/org/videolan/libvlc/util/MediaBrowser.java |  2 +-
 vlc-android/src/org/videolan/vlc/MediaWrapper.java |  2 +-
 .../vlc/gui/browser/StorageBrowserAdapter.java     |  2 +-
 4 files changed, 63 insertions(+), 11 deletions(-)

diff --git a/libvlc/src/org/videolan/libvlc/Media.java b/libvlc/src/org/videolan/libvlc/Media.java
index fd5f6c8..9413405 100644
--- a/libvlc/src/org/videolan/libvlc/Media.java
+++ b/libvlc/src/org/videolan/libvlc/Media.java
@@ -216,7 +216,7 @@ public class Media extends VLCObject {
     private static final int PARSE_STATUS_PARSING = 0x01;
     private static final int PARSE_STATUS_PARSED = 0x02;
 
-    private String mMrl = null;
+    private Uri mUri = null;
     private MediaList mSubItems = null;
     private int mParseStatus = PARSE_STATUS_INIT;
     private String mNativeMetas[] = null;
@@ -233,7 +233,7 @@ public class Media extends VLCObject {
      */
     public Media(LibVLC libVLC, String path) {
         nativeNewFromPath(libVLC, path);
-        mMrl = nativeGetMrl();
+        mUri = UriFromMrl(nativeGetMrl());
         mType = nativeGetType();
     }
 
@@ -244,8 +244,8 @@ public class Media extends VLCObject {
      * @param uri
      */
     public Media(LibVLC libVLC, Uri uri) {
-        nativeNewFromLocation(libVLC, uri.toString());
-        mMrl = nativeGetMrl();
+        nativeNewFromLocation(libVLC, locationFromUri(uri));
+        mUri = uri;
         mType = nativeGetType();
     }
 
@@ -257,7 +257,7 @@ public class Media extends VLCObject {
      */
     public Media(LibVLC libVLC, FileDescriptor fd) {
         nativeNewFromFD(libVLC, fd);
-        mMrl = nativeGetMrl();
+        mUri = UriFromMrl(nativeGetMrl());
         mType = nativeGetType();
     }
 
@@ -272,11 +272,63 @@ public class Media extends VLCObject {
         if (!ml.isLocked())
             throw new IllegalStateException("MediaList should be locked");
         nativeNewFromMediaList(ml, index);
-        mMrl = nativeGetMrl();
+        mUri = UriFromMrl(nativeGetMrl());
         mNativeMetas = nativeGetMetas();
         mType = nativeGetType();
     }
 
+    private static final String URI_AUTHORIZED_CHARS = "!'()*";
+
+    /**
+     * VLC authorize only "-._~" in Mrl format, android Uri authorize "_-!.~'()*".
+     * Therefore, decode the characters authorized by Android Uri when creating an Uri from VLC.
+     * @param mrl
+     * @return
+     */
+    private static Uri UriFromMrl(String mrl) {
+        final char array[] = mrl.toCharArray();
+        final StringBuilder sb = new StringBuilder(array.length);
+
+        for (int i = 0; i < array.length; ++i) {
+            final char c = array[i];
+            if (c == '%') {
+                if (array.length - i < 3)
+                    throw new IllegalArgumentException("bad mrl format");
+
+                final int hex = Integer.parseInt(new String(array, i + 1, 2), 16);
+                if (URI_AUTHORIZED_CHARS.indexOf(hex) != -1) {
+                    sb.append((char)hex);
+                    i += 2;
+                    continue;
+                }
+            }
+            sb.append(c);
+        }
+
+        return Uri.parse(sb.toString());
+    }
+
+    /**
+     * VLC authorize only "-._~" in Mrl format, android Uri authorize "_-!.~'()*".
+     * Therefore, encode the characters authorized by Android Uri when creating a mrl from an Uri.
+     * @param uri
+     * @return
+     */
+    private static String locationFromUri(Uri uri) {
+        final char array[] = uri.toString().toCharArray();
+        final StringBuilder sb = new StringBuilder(array.length * 2);
+
+        for (int i = 0; i < array.length; ++i) {
+            final char c = array[i];
+            if (URI_AUTHORIZED_CHARS.indexOf(c) != -1)
+                sb.append("%").append(Integer.toHexString(c));
+            else
+                sb.append(c);
+        }
+
+        return sb.toString();
+    }
+
     @Override
     protected synchronized Event onEventNative(int eventType, long arg1, long arg2) {
         switch (eventType) {
@@ -306,8 +358,8 @@ public class Media extends VLCObject {
     /**
      * Get the MRL associated with the Media.
      */
-    public synchronized String getMrl() {
-        return mMrl;
+    public synchronized Uri getUri() {
+        return mUri;
     }
 
     /**
diff --git a/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
index cda93af..a3f4eb3 100644
--- a/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
+++ b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
@@ -241,7 +241,7 @@ public class MediaBrowser {
                     /* one item can be found by severals discoverers */
                     boolean found = false;
                     for (Media media : mDiscovererMediaArray) {
-                        if (media.getMrl().equals(mlEvent.media.getMrl())) {
+                        if (media.getUri().equals(mlEvent.media.getUri())) {
                             found = true;
                             break;
                         }
diff --git a/vlc-android/src/org/videolan/vlc/MediaWrapper.java b/vlc-android/src/org/videolan/vlc/MediaWrapper.java
index dbe300a..9d89c90 100644
--- a/vlc-android/src/org/videolan/vlc/MediaWrapper.java
+++ b/vlc-android/src/org/videolan/vlc/MediaWrapper.java
@@ -108,7 +108,7 @@ public class MediaWrapper implements Parcelable {
         if (media == null)
             throw new NullPointerException("media was null");
 
-        mUri = getUri(media.getMrl());
+        mUri = media.getUri();
         init(media);
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
index 59f99bf..de7fdec 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.java
@@ -107,7 +107,7 @@ public class StorageBrowserAdapter extends BaseBrowserAdapter {
     }
 
     public void addItem(Media media, boolean notify, boolean top){
-        Storage storage = new Storage(media.getMrl());
+        Storage storage = new Storage(media.getUri().toString());
         addItem(storage, notify, top);
     }
 
-- 
2.1.4



More information about the Android mailing list