Skip to content

Commit

Permalink
Fixed issue for Android Marshmallow and below - Url path has to be ma…
Browse files Browse the repository at this point in the history
…nually encoded
  • Loading branch information
izderadicka committed Jun 21, 2018
1 parent 122ab24 commit 1bb039f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/eu/zderadicka/audioserve/net/ApiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -417,7 +418,7 @@ class ApiClient private constructor(val context: Context) {
}

inner abstract class MyRequest<T>(val uri: String, private val callback: (T?, ApiError?) -> Unit)
:Request<T>( Request.Method.GET, uri,
:Request<T>( Request.Method.GET, encodeUri(uri),
{
Log.e(LOG_TAG, "Network Error $it")
val err = ApiError.fromResponseError(it)
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/eu/zderadicka/audioserve/net/FileCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -514,7 +515,7 @@ class FileLoader(private val queue: BlockingDeque<CacheItem>,
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")

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/eu/zderadicka/audioserve/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,3 +114,14 @@ fun splitExtension(name:String): Pair<String,String?> {
}
}

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()
}
}

0 comments on commit 1bb039f

Please sign in to comment.