From d47d67be1ac8c11bb4b85d2c785d51402a5f8af4 Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 10 Jan 2022 16:34:53 +0100 Subject: [PATCH] Request: use timeout values from settings --- .../java/dev/lowrespalmtree/comet/PageViewFragment.kt | 10 +++++++++- .../java/dev/lowrespalmtree/comet/PageViewModel.kt | 4 ++-- app/src/main/java/dev/lowrespalmtree/comet/Request.kt | 10 ++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/dev/lowrespalmtree/comet/PageViewFragment.kt b/app/src/main/java/dev/lowrespalmtree/comet/PageViewFragment.kt index b8bd5d5..a016b63 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/PageViewFragment.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/PageViewFragment.kt @@ -20,6 +20,7 @@ import androidx.appcompat.app.AlertDialog import androidx.core.view.setMargins import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider +import androidx.preference.PreferenceManager import androidx.recyclerview.widget.LinearLayoutManager import dev.lowrespalmtree.comet.databinding.FragmentPageViewBinding import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -112,7 +113,14 @@ class PageViewFragment : Fragment(), ContentAdapter.ContentAdapterListener { } when (uri.scheme) { - "gemini" -> pageViewModel.sendGeminiRequest(uri) + "gemini" -> { + val prefs = PreferenceManager.getDefaultSharedPreferences(requireContext()) + val connectionTimeout = + prefs.getInt("connection_timeout", Request.DEFAULT_CONNECTION_TIMEOUT_SEC) + val readTimeout = + prefs.getInt("read_timeout", Request.DEFAULT_READ_TIMEOUT_SEC) + pageViewModel.sendGeminiRequest(uri, connectionTimeout, readTimeout) + } else -> openUnknownScheme(uri) } } diff --git a/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt b/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt index 7f2c2d6..441bdef 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt @@ -39,14 +39,14 @@ class PageViewModel : ViewModel() { * * The URI must be valid, absolute and with a gemini scheme. */ - fun sendGeminiRequest(uri: Uri, redirects: Int = 0) { + fun sendGeminiRequest(uri: Uri, connectionTimeout: Int, readTimeout: Int, redirects: Int = 0) { Log.d(TAG, "sendRequest: URI \"$uri\"") state.postValue(State.CONNECTING) requestJob?.apply { if (isActive) cancel() } requestJob = viewModelScope.launch(Dispatchers.IO) { val response = try { val request = Request(uri) - val socket = request.connect() + val socket = request.connect(connectionTimeout, readTimeout) val channel = request.proceed(socket, this) Response.from(channel, viewModelScope) } catch (e: Exception) { diff --git a/app/src/main/java/dev/lowrespalmtree/comet/Request.kt b/app/src/main/java/dev/lowrespalmtree/comet/Request.kt index 6bac3cc..8ca2371 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/Request.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/Request.kt @@ -17,13 +17,13 @@ import javax.net.ssl.X509TrustManager class Request(private val uri: Uri) { private val port get() = if (uri.port > 0) uri.port else 1965 - fun connect(): SSLSocket { + fun connect(connectionTimeout: Int, readTimeout: Int): SSLSocket { Log.d(TAG, "connect") val context = SSLContext.getInstance("TLSv1.2") context.init(null, arrayOf(TrustManager()), null) val socket = context.socketFactory.createSocket() as SSLSocket - socket.soTimeout = 10000 - socket.connect(InetSocketAddress(uri.host, port), 10000) + socket.soTimeout = readTimeout * 1000 + socket.connect(InetSocketAddress(uri.host, port), connectionTimeout * 1000) socket.startHandshake() return socket } @@ -69,6 +69,8 @@ class Request(private val uri: Uri) { } companion object { - const val TAG = "Request" + private const val TAG = "Request" + const val DEFAULT_CONNECTION_TIMEOUT_SEC = 10 + const val DEFAULT_READ_TIMEOUT_SEC = 10 } } \ No newline at end of file