Adds flexibility on timeouts between WIFI and mobile connections.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.vitorpamplona.amethyst.service
|
package com.vitorpamplona.amethyst.service
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
import java.net.Proxy
|
import java.net.Proxy
|
||||||
@@ -7,7 +8,11 @@ import java.time.Duration
|
|||||||
import kotlin.properties.Delegates
|
import kotlin.properties.Delegates
|
||||||
|
|
||||||
object HttpClient {
|
object HttpClient {
|
||||||
|
val DEFAULT_TIMEOUT_ON_WIFI = Duration.ofSeconds(10L)
|
||||||
|
val DEFAULT_TIMEOUT_ON_MOBILE = Duration.ofSeconds(30L)
|
||||||
|
|
||||||
var proxyChangeListeners = ArrayList<() -> Unit>()
|
var proxyChangeListeners = ArrayList<() -> Unit>()
|
||||||
|
var defaultTimeout = DEFAULT_TIMEOUT_ON_WIFI
|
||||||
|
|
||||||
// fires off every time value of the property changes
|
// fires off every time value of the property changes
|
||||||
private var internalProxy: Proxy? by Delegates.observable(null) { _, oldValue, newValue ->
|
private var internalProxy: Proxy? by Delegates.observable(null) { _, oldValue, newValue ->
|
||||||
@@ -22,6 +27,13 @@ object HttpClient {
|
|||||||
this.internalProxy = proxy
|
this.internalProxy = proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun changeTimeouts(timeout: Duration) {
|
||||||
|
Log.d("HttpClient", "Changing timeout to: $timeout")
|
||||||
|
if (this.defaultTimeout.seconds != timeout.seconds) {
|
||||||
|
this.defaultTimeout = timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun getHttpClient(timeout: Duration): OkHttpClient {
|
fun getHttpClient(timeout: Duration): OkHttpClient {
|
||||||
val seconds = if (internalProxy != null) timeout.seconds * 2 else timeout.seconds
|
val seconds = if (internalProxy != null) timeout.seconds * 2 else timeout.seconds
|
||||||
val duration = Duration.ofSeconds(seconds)
|
val duration = Duration.ofSeconds(seconds)
|
||||||
@@ -34,7 +46,7 @@ object HttpClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getHttpClient(): OkHttpClient {
|
fun getHttpClient(): OkHttpClient {
|
||||||
return getHttpClient(Duration.ofSeconds(10L))
|
return getHttpClient(defaultTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getProxy(): Proxy? {
|
fun getProxy(): Proxy? {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
|
|||||||
import com.google.accompanist.adaptive.calculateDisplayFeatures
|
import com.google.accompanist.adaptive.calculateDisplayFeatures
|
||||||
import com.vitorpamplona.amethyst.LocalPreferences
|
import com.vitorpamplona.amethyst.LocalPreferences
|
||||||
import com.vitorpamplona.amethyst.ServiceManager
|
import com.vitorpamplona.amethyst.ServiceManager
|
||||||
|
import com.vitorpamplona.amethyst.service.HttpClient
|
||||||
import com.vitorpamplona.amethyst.service.lang.LanguageTranslatorService
|
import com.vitorpamplona.amethyst.service.lang.LanguageTranslatorService
|
||||||
import com.vitorpamplona.amethyst.service.notifications.PushNotificationUtils
|
import com.vitorpamplona.amethyst.service.notifications.PushNotificationUtils
|
||||||
import com.vitorpamplona.amethyst.ui.components.DefaultMutedSetting
|
import com.vitorpamplona.amethyst.ui.components.DefaultMutedSetting
|
||||||
@@ -222,6 +223,14 @@ class MainActivity : AppCompatActivity() {
|
|||||||
changedNetwork = true
|
changedNetwork = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (changedNetwork) {
|
||||||
|
if (isOnMobileData) {
|
||||||
|
HttpClient.changeTimeouts(HttpClient.DEFAULT_TIMEOUT_ON_MOBILE)
|
||||||
|
} else {
|
||||||
|
HttpClient.changeTimeouts(HttpClient.DEFAULT_TIMEOUT_ON_WIFI)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return changedNetwork
|
return changedNetwork
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user