From 53f8d248b7498252f617eb2da2ecefa29e33c726 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Mon, 2 Oct 2023 09:36:23 -0700 Subject: [PATCH] Refactor --- .../memories/service/AccountService.kt | 69 +++++++------------ .../gallery/memories/service/HttpService.kt | 12 +++- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/gallery/memories/service/AccountService.kt b/app/src/main/java/gallery/memories/service/AccountService.kt index 1dea19c8..b2e3d4e0 100644 --- a/app/src/main/java/gallery/memories/service/AccountService.kt +++ b/app/src/main/java/gallery/memories/service/AccountService.kt @@ -8,12 +8,6 @@ import androidx.media3.common.util.UnstableApi import gallery.memories.MainActivity import gallery.memories.R import io.github.g00fy2.versioncompare.Version -import okhttp3.MediaType.Companion.toMediaTypeOrNull -import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.RequestBody.Companion.toRequestBody -import okhttp3.Response -import org.json.JSONObject import java.net.SocketTimeoutException @UnstableApi @@ -65,55 +59,38 @@ class AccountService(private val mCtx: MainActivity, private val mHttp: HttpServ mCtx.binding.webview.loadUrl("file:///android_asset/sync.html") } - val client = OkHttpClient() - val rbody = - "token=$pollToken".toRequestBody("application/x-www-form-urlencoded".toMediaTypeOrNull()) var pollCount = 0 - - while (true) { + while (pollCount < 10 * 60) { pollCount += 3 - if (pollCount >= 10 * 60) return // Sleep for 3s Thread.sleep(3000) - // Poll login flow URL - val request = Request.Builder() - .url(pollUrl) - .post(rbody) - .build() - - val response: Response try { - response = client.newCall(request).execute() - } catch (e: SocketTimeoutException) { + val response = mHttp.getPollLogin(pollUrl, pollToken) + val body = mHttp.bodyJson(response) ?: throw Exception("Failed to parse login flow response") + Log.v(TAG, "pollLogin: Got status code ${response.code}") + + // Check status code + if (response.code != 200) { + throw Exception("Failed to poll login flow") + } + + val loginName = body.getString("loginName") + val appPassword = body.getString("appPassword") + + mCtx.runOnUiThread { + // Save login info (also updates header) + storeCredentials(baseUrl, loginName, appPassword) + + // Go to next screen + mCtx.binding.webview.evaluateJavascript("window.loggedIn()", {}) + } + + return + } catch (e: Exception) { continue } - - Log.v(TAG, "pollLogin: Got status code ${response.code}") - - // Check status code - if (response.code != 200) { - response.body?.close() - continue - } - - // Read response body - val body = response.body!!.string() - response.body?.close() - val json = JSONObject(body) - val loginName = json.getString("loginName") - val appPassword = json.getString("appPassword") - - mCtx.runOnUiThread { - // Save login info (also updates header) - storeCredentials(baseUrl, loginName, appPassword) - - // Go to next screen - mCtx.binding.webview.evaluateJavascript("window.loggedIn()", {}) - } - - return; } } diff --git a/app/src/main/java/gallery/memories/service/HttpService.kt b/app/src/main/java/gallery/memories/service/HttpService.kt index 27ca39ad..738759ee 100644 --- a/app/src/main/java/gallery/memories/service/HttpService.kt +++ b/app/src/main/java/gallery/memories/service/HttpService.kt @@ -16,6 +16,7 @@ class HttpService { val TAG = HttpService::class.java.simpleName } + private val client = OkHttpClient() private var authHeader: String? = null private var memoriesUrl: String? = null @@ -103,10 +104,19 @@ class HttpService { .build()) } + /** Make login polling request */ + @Throws(Exception::class) + fun getPollLogin(pollUrl: String, pollToken: String): Response { + return runRequest(Request.Builder() + .url(pollUrl) + .post("token=$pollToken".toRequestBody("application/x-www-form-urlencoded".toMediaTypeOrNull())) + .build()) + } + /** Run a request and get a JSON object */ @Throws(Exception::class) private fun runRequest(request: Request): Response { - return OkHttpClient().newCall(request).execute() + return client.newCall(request).execute() } /** Build a GET request */