1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-08 12:02:33 +01:00

Require main thread for cookiemanager calls

This commit is contained in:
Allan Wang 2021-01-24 17:19:08 -08:00
parent e0f289862b
commit c2f7e2e0c6
No known key found for this signature in database
GPG Key ID: 69D90B885D405BDB

View File

@ -29,14 +29,14 @@ import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.cookies
import com.pitchedapps.frost.utils.launchLogin
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withContext
import org.koin.dsl.module
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
/**
* Created by Allan Wang on 2017-05-30.
@ -64,7 +64,10 @@ class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) {
val messengerCookie: String?
get() = CookieManager.getInstance().getCookie(HTTPS_MESSENGER_COM)
private suspend fun CookieManager.suspendSetWebCookie(domain: String, cookie: String?): Boolean {
private suspend fun CookieManager.suspendSetWebCookie(
domain: String,
cookie: String?
): Boolean {
cookie ?: return true
return withContext(NonCancellable) {
// Save all cookies regardless of result, then check if all succeeded
@ -100,11 +103,13 @@ class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) {
suspend fun reset() {
prefs.userId = -1L
withContext(Dispatchers.Main + NonCancellable) {
with(CookieManager.getInstance()) {
removeAllCookies()
flush()
}
}
}
suspend fun switchUser(id: Long) {
val cookie = cookieDao.selectById(id) ?: return L.e { "No cookie for id" }
@ -117,7 +122,7 @@ class FbCookie(private val prefs: Prefs, private val cookieDao: CookieDao) {
return
}
val currentId = prefs.userId
withContext(Dispatchers.IO + NonCancellable) {
withContext(Dispatchers.Main + NonCancellable) {
L.d { "Switching User" }
// Save current messenger cookie state.
cookieDao.updateMessengerCookie(currentId, messengerCookie)