From 1bb039f1ffe0e48b01c98dd0b3dfb487b89c7093 Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 21 Jun 2018 10:18:20 +0200 Subject: [PATCH] Fixed issue for Android Marshmallow and below - Url path has to be manually encoded --- app/build.gradle | 4 ++-- .../zderadicka/audioserve/UriEncodingTest.kt | 21 +++++++++++++++++++ .../eu/zderadicka/audioserve/net/ApiClient.kt | 3 ++- .../eu/zderadicka/audioserve/net/FileCache.kt | 3 ++- .../eu/zderadicka/audioserve/utils/Utils.kt | 13 ++++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 app/src/androidTest/java/eu/zderadicka/audioserve/UriEncodingTest.kt diff --git a/app/build.gradle b/app/build.gradle index 3567ec3..cc338c8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -18,8 +18,8 @@ android { applicationId "eu.zderadicka.audioserve" minSdkVersion 21 targetSdkVersion 27 - versionCode 17 - versionName "0.6.10" + versionCode 18 + versionName "0.6.11" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/app/src/androidTest/java/eu/zderadicka/audioserve/UriEncodingTest.kt b/app/src/androidTest/java/eu/zderadicka/audioserve/UriEncodingTest.kt new file mode 100644 index 0000000..cac5bed --- /dev/null +++ b/app/src/androidTest/java/eu/zderadicka/audioserve/UriEncodingTest.kt @@ -0,0 +1,21 @@ +package eu.zderadicka.audioserve + +import android.os.Build +import eu.zderadicka.audioserve.utils.encodeUri +import org.junit.Test +import org.junit.Assert.* + +class UriEncodingTest { + @Test + fun testEncoding() { + val uriEncoded = "http://localhost:3000/folder/Doyle%2C%20Arthur%20Conan" + val uriWithSpace = "http://localhost:3000/folder/Doyle, Arthur Conan" + val res = encodeUri(uriWithSpace) + if (Build.VERSION.SDK_INT<= 23) { + + assertEquals(uriEncoded, res) + } else { + assertEquals(uriWithSpace, res) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt b/app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt index 13a8554..5f174af 100644 --- a/app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt +++ b/app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt @@ -24,6 +24,7 @@ import eu.zderadicka.audioserve.data.AudioFolder import eu.zderadicka.audioserve.data.parseCollectionsFromJson import eu.zderadicka.audioserve.data.parseFolderfromJson import eu.zderadicka.audioserve.data.parseTranscodingsFromJson +import eu.zderadicka.audioserve.utils.encodeUri import eu.zderadicka.audioserve.utils.fromMarkdown import java.io.File import java.io.UnsupportedEncodingException @@ -417,7 +418,7 @@ class ApiClient private constructor(val context: Context) { } inner abstract class MyRequest(val uri: String, private val callback: (T?, ApiError?) -> Unit) - :Request( Request.Method.GET, uri, + :Request( Request.Method.GET, encodeUri(uri), { Log.e(LOG_TAG, "Network Error $it") val err = ApiError.fromResponseError(it) diff --git a/app/src/main/java/eu/zderadicka/audioserve/net/FileCache.kt b/app/src/main/java/eu/zderadicka/audioserve/net/FileCache.kt index 3f5e715..3790c22 100644 --- a/app/src/main/java/eu/zderadicka/audioserve/net/FileCache.kt +++ b/app/src/main/java/eu/zderadicka/audioserve/net/FileCache.kt @@ -11,6 +11,7 @@ import android.os.FileObserver import android.preference.PreferenceManager import android.util.Log import eu.zderadicka.audioserve.utils.defPrefs +import eu.zderadicka.audioserve.utils.encodeUri import eu.zderadicka.audioserve.utils.isNetworkConnected import java.io.File import java.io.IOException @@ -514,7 +515,7 @@ class FileLoader(private val queue: BlockingDeque, if (item.transcode != null) { urlStr += "?${TRANSCODE_QUERY}=${item.transcode}" } - url = URL(urlStr) + url = URL(encodeUri(urlStr)) val conn = url.openConnection() as HttpURLConnection Log.d(LOG_TAG, "Started download of $url") diff --git a/app/src/main/java/eu/zderadicka/audioserve/utils/Utils.kt b/app/src/main/java/eu/zderadicka/audioserve/utils/Utils.kt index 6d2058f..ec0fe99 100644 --- a/app/src/main/java/eu/zderadicka/audioserve/utils/Utils.kt +++ b/app/src/main/java/eu/zderadicka/audioserve/utils/Utils.kt @@ -5,6 +5,8 @@ import android.support.v4.media.session.PlaybackStateCompat import android.util.Log import android.net.NetworkInfo import android.net.ConnectivityManager +import android.net.Uri +import android.os.Build import java.io.File import java.io.FileInputStream import java.io.FileOutputStream @@ -112,3 +114,14 @@ fun splitExtension(name:String): Pair { } } +fun encodeUri(uri:String):String { + //encoding of uri path is only needed for API level <= 23 + if (Build.VERSION.SDK_INT > 23) { + return uri + } else { + val u = Uri.parse(uri) + val p = u.encodedPath + return u.buildUpon().path(p).build().toString() + } +} +