Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[for discussion] Use OkHttp 5 #4023

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import okio.sink
import org.json.JSONObject
import java.io.File
import java.io.FileOutputStream
import java.net.URL
import java.net.URLDecoder
import javax.inject.Inject
Expand Down Expand Up @@ -1319,10 +1319,11 @@ class MessagingManager @Inject constructor(
videoFile.parentFile?.mkdirs()
videoFile.createNewFile()
}
FileOutputStream(videoFile).use { output ->
response.body?.byteStream()?.copyTo(output)
response.body.source().use { source ->
videoFile.sink().use { sink ->
source.readAll(sink)
}
}
response.close()

mediaRetriever.setDataSource(videoFile.absolutePath)
val durationInMicroSeconds = ((mediaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLongOrNull() ?: VIDEO_GUESS_MILLISECONDS)) * 1000
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.homeassistant.companion.android.common.data

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.webkit.CookieManager
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.PropertyNamingStrategies
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import dagger.hilt.android.qualifiers.ApplicationContext
import io.homeassistant.companion.android.common.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand All @@ -15,7 +18,8 @@ import java.util.concurrent.TimeUnit
import javax.inject.Inject

class HomeAssistantApis @Inject constructor(
private val tlsHelper: TLSHelper
private val tlsHelper: TLSHelper,
@ApplicationContext private val appContext: Context
) {
companion object {
private const val LOCAL_HOST = "http://localhost/"
Expand All @@ -41,18 +45,20 @@ class HomeAssistantApis @Inject constructor(
.build()
)
}
// Only deal with cookies when on non wear device and for now I don't have a better
// way to determine if we are really on wear os....
// TODO: Please fix me.
var cookieManager: CookieManager? = null
try {
cookieManager = CookieManager.getInstance()
} catch (e: Exception) {
// Noop
}
if (cookieManager != null) {
builder.cookieJar(CookieJarCookieManagerShim())

val isWear = appContext.packageManager.hasSystemFeature(PackageManager.FEATURE_WATCH)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably split this out. Let me know if you want it separately.

if (!isWear) {
var cookieManager: CookieManager? = null
try {
cookieManager = CookieManager.getInstance()
} catch (e: Exception) {
// Noop
}
if (cookieManager != null) {
builder.cookieJar(CookieJarCookieManagerShim())
}
}

builder.callTimeout(CALL_TIMEOUT, TimeUnit.SECONDS)
builder.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ class NetworkSensorManager : SensorManager {
override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) throw IOException("Unexpected response code $response")
try {
val jsonObject = JSONObject(response.body!!.string())
val jsonObject = JSONObject(response.body.string())
ip = jsonObject.getString("ip")
} catch (e: JSONException) {
Log.e(TAG, "Unable to parse ip address from response", e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package io.homeassistant.companion.android.common.util

import okhttp3.internal.and
import okio.ByteString.Companion.toByteString
import java.util.Locale

private val HEX_ARRAY = "0123456789ABCDEF".toCharArray()

fun ByteArray.toHexString(): String { // From https://stackoverflow.com/a/9855338/4214819
val hexChars = CharArray(this.size * 2)
for (j in 0 until this.size) {
val v = get(j) and 0xFF
hexChars[j * 2] = HEX_ARRAY[v ushr 4]
hexChars[j * 2 + 1] = HEX_ARRAY[v and 0x0F]
}
return String(hexChars)
fun ByteArray.toHexString(): String {
return toByteString().hex().uppercase()
}

fun String.capitalize(locale: Locale) =
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ lifecycle = "2.6.2"
material = "1.10.0"
media3 = "1.1.1"
navigation-compose = "2.7.4"
okhttp = "4.12.0"
okhttp = "5.0.0-alpha.11"
paging = "3.2.1"
picasso = "2.8"
play-services-threadnetwork = "16.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class CameraTile : TileService() {
val maxHeight = requestParams.deviceConfiguration.screenHeightDp * requestParams.deviceConfiguration.screenDensity
withContext(Dispatchers.IO) {
val response = okHttpClient.newCall(Request.Builder().url(url).build()).execute()
byteArray = response.body?.byteStream()?.readBytes()
byteArray = response.body.byteStream().readBytes()
byteArray?.let {
var bitmap = BitmapFactory.decodeByteArray(it, 0, it.size)
if (bitmap.width > maxWidth || bitmap.height > maxHeight) {
Expand Down