Refactor
parent
bb4d204228
commit
53f8d248b7
|
@ -8,12 +8,6 @@ import androidx.media3.common.util.UnstableApi
|
||||||
import gallery.memories.MainActivity
|
import gallery.memories.MainActivity
|
||||||
import gallery.memories.R
|
import gallery.memories.R
|
||||||
import io.github.g00fy2.versioncompare.Version
|
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
|
import java.net.SocketTimeoutException
|
||||||
|
|
||||||
@UnstableApi
|
@UnstableApi
|
||||||
|
@ -65,45 +59,25 @@ class AccountService(private val mCtx: MainActivity, private val mHttp: HttpServ
|
||||||
mCtx.binding.webview.loadUrl("file:///android_asset/sync.html")
|
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
|
var pollCount = 0
|
||||||
|
while (pollCount < 10 * 60) {
|
||||||
while (true) {
|
|
||||||
pollCount += 3
|
pollCount += 3
|
||||||
if (pollCount >= 10 * 60) return
|
|
||||||
|
|
||||||
// Sleep for 3s
|
// Sleep for 3s
|
||||||
Thread.sleep(3000)
|
Thread.sleep(3000)
|
||||||
|
|
||||||
// Poll login flow URL
|
|
||||||
val request = Request.Builder()
|
|
||||||
.url(pollUrl)
|
|
||||||
.post(rbody)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
val response: Response
|
|
||||||
try {
|
try {
|
||||||
response = client.newCall(request).execute()
|
val response = mHttp.getPollLogin(pollUrl, pollToken)
|
||||||
} catch (e: SocketTimeoutException) {
|
val body = mHttp.bodyJson(response) ?: throw Exception("Failed to parse login flow response")
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.v(TAG, "pollLogin: Got status code ${response.code}")
|
Log.v(TAG, "pollLogin: Got status code ${response.code}")
|
||||||
|
|
||||||
// Check status code
|
// Check status code
|
||||||
if (response.code != 200) {
|
if (response.code != 200) {
|
||||||
response.body?.close()
|
throw Exception("Failed to poll login flow")
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read response body
|
val loginName = body.getString("loginName")
|
||||||
val body = response.body!!.string()
|
val appPassword = body.getString("appPassword")
|
||||||
response.body?.close()
|
|
||||||
val json = JSONObject(body)
|
|
||||||
val loginName = json.getString("loginName")
|
|
||||||
val appPassword = json.getString("appPassword")
|
|
||||||
|
|
||||||
mCtx.runOnUiThread {
|
mCtx.runOnUiThread {
|
||||||
// Save login info (also updates header)
|
// Save login info (also updates header)
|
||||||
|
@ -113,7 +87,10 @@ class AccountService(private val mCtx: MainActivity, private val mHttp: HttpServ
|
||||||
mCtx.binding.webview.evaluateJavascript("window.loggedIn()", {})
|
mCtx.binding.webview.evaluateJavascript("window.loggedIn()", {})
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return
|
||||||
|
} catch (e: Exception) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ class HttpService {
|
||||||
val TAG = HttpService::class.java.simpleName
|
val TAG = HttpService::class.java.simpleName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val client = OkHttpClient()
|
||||||
private var authHeader: String? = null
|
private var authHeader: String? = null
|
||||||
private var memoriesUrl: String? = null
|
private var memoriesUrl: String? = null
|
||||||
|
|
||||||
|
@ -103,10 +104,19 @@ class HttpService {
|
||||||
.build())
|
.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 */
|
/** Run a request and get a JSON object */
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
private fun runRequest(request: Request): Response {
|
private fun runRequest(request: Request): Response {
|
||||||
return OkHttpClient().newCall(request).execute()
|
return client.newCall(request).execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Build a GET request */
|
/** Build a GET request */
|
||||||
|
|
Loading…
Reference in New Issue