fixed possible deadlock

This commit is contained in:
Mariotaku Lee 2017-04-11 12:25:48 +08:00
parent cbdf5989b6
commit e0baf8c875
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
1 changed files with 15 additions and 12 deletions

View File

@ -25,7 +25,8 @@ import org.mariotaku.twidere.model.util.AccountUtils.ACCOUNT_USER_DATA_KEYS
import org.mariotaku.twidere.util.JsonSerializer
import org.mariotaku.twidere.util.ParseUtils
import java.io.IOException
import java.util.concurrent.FutureTask
import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
@ -162,15 +163,16 @@ private fun parseCredentials(authToken: String, @Credentials.Type authType: Stri
}
internal object AccountDataQueue {
val handler = Handler(Looper.getMainLooper())
private val executor = Executors.newSingleThreadExecutor()
fun getUserData(manager: AccountManager, account: Account, key: String): String? {
val future = FutureTask<String?> { manager.getUserData(account, key) }
if (Thread.currentThread() === Looper.getMainLooper().thread) {
future.run()
} else handler.post {
future.run()
val callable = Callable {
manager.getUserData(account, key)
}
if (Thread.currentThread() === Looper.getMainLooper().thread) {
return callable.call()
}
val future = executor.submit(callable)
try {
return future.get(1, TimeUnit.SECONDS)
} catch (e: TimeoutException) {
@ -179,12 +181,13 @@ internal object AccountDataQueue {
}
fun peekAuthToken(manager: AccountManager, account: Account, authTokenType: String): String? {
val future = FutureTask<String?> { manager.peekAuthToken(account, authTokenType) }
if (Thread.currentThread() === Looper.getMainLooper().thread) {
future.run()
} else handler.post {
future.run()
val callable = Callable {
manager.peekAuthToken(account, authTokenType)
}
if (Thread.currentThread() === Looper.getMainLooper().thread) {
return callable.call()
}
val future = executor.submit(callable)
try {
return future.get(1, TimeUnit.SECONDS)
} catch (e: TimeoutException) {