@@ -3,11 +3,10 @@ package com.getcode.util
33import android.accounts.Account
44import android.accounts.AccountManager
55import android.accounts.AuthenticatorException
6- import android.app.Activity
76import android.content.Context
8- import android.os.Bundle
97import android.os.Handler
108import android.os.HandlerThread
9+ import androidx.core.os.bundleOf
1110import com.getcode.BuildConfig
1211import com.getcode.utils.TraceType
1312import com.getcode.utils.trace
@@ -23,14 +22,20 @@ import kotlin.coroutines.resume
2322
2423
2524object AccountUtils {
26- private const val acctType = BuildConfig .APPLICATION_ID
27-
28- fun addAccount (context : Context , name : String , password : String , token : String ) {
29- val am: AccountManager = AccountManager .get(context)
30- val a = Account (name, acctType)
31-
32- am.addAccountExplicitly(a, password, Bundle ())
33- am.setAuthToken(a, acctType, token)
25+ private const val ACCOUNT_TYPE = BuildConfig .APPLICATION_ID
26+
27+ fun addAccount (
28+ context : Context ,
29+ name : String ,
30+ password : String ,
31+ token : String
32+ ) {
33+ val accountManager: AccountManager = AccountManager .get(context)
34+ val account = Account (name, ACCOUNT_TYPE )
35+
36+ val data = bundleOf(AccountManager .KEY_AUTH_TOKEN_LABEL to " entropy" )
37+ accountManager.addAccountExplicitly(account, password, data)
38+ accountManager.setAuthToken(account, ACCOUNT_TYPE , token)
3439 }
3540
3641 suspend fun removeAccounts (context : Context ): @NonNull Single <Boolean > {
@@ -63,54 +68,48 @@ object AccountUtils {
6368
6469 private suspend fun getAccountNoActivity (
6570 context : Context
66- ) : Pair <String ?, Account ?>? = suspendCancellableCoroutine { cont ->
71+ ): Pair <String ?, Account ?>? = suspendCancellableCoroutine { cont ->
6772 trace(" getAuthToken" , type = TraceType .Silent )
6873 val am: AccountManager = AccountManager .get(context)
69- val accountthing = am.accounts.getOrNull(0 )
70- if (accountthing == null ) {
74+ val account = am.accounts.getOrNull(0 )
75+ if (account == null ) {
7176 trace(" no associated account found" , type = TraceType .Error )
7277 cont.resume(null to null )
7378 return @suspendCancellableCoroutine
7479 }
7580 val start = Clock .System .now()
7681 am.getAuthToken(
77- accountthing, acctType , null , false ,
82+ account, ACCOUNT_TYPE , null , false ,
7883 { future ->
7984 try {
8085 val bundle = future?.result
8186 val authToken = bundle?.getString(AccountManager .KEY_AUTHTOKEN )
82- val accountName = bundle?.getString(AccountManager .KEY_ACCOUNT_NAME )
83- val account: Account ? = getAccount(context, accountName)
8487
8588 val end = Clock .System .now()
8689 trace(" auth token fetch took ${end.toEpochMilliseconds() - start.toEpochMilliseconds()} ms" )
8790
88- cont.resume(authToken.orEmpty() to account)
89-
90- if (null == account && authToken != null ) {
91- addAccount(context, accountName.orEmpty(), " " , authToken)
92- }
91+ cont.resume(authToken.orEmpty() to account)
9392 } catch (e: AuthenticatorException ) {
93+ e.printStackTrace()
9494 trace(message = " failed to read account" , error = e, type = TraceType .Error )
9595 cont.resume(null to null )
9696 }
9797 }, handler
9898 )
9999 }
100100
101-
102101 suspend fun getToken (context : Context ): String? {
103- return getAccountNoActivity(context)?.first
104- }
105-
106- private fun getAccount (context : Context ? , accountName : String? ): Account ? {
107102 val accountManager = AccountManager .get(context)
108- val accounts = accountManager.getAccountsByType(acctType)
109- for (account in accounts) {
110- if (account.name.equals(accountName, ignoreCase = true )) {
111- return account
103+ val account = accountManager.accounts.firstOrNull()
104+ if (account != null ) {
105+ val token = runCatching { accountManager.peekAuthToken(account, ACCOUNT_TYPE ) }
106+ .getOrNull()?.takeIf { it.isNotEmpty() }
107+
108+ if (token != null ) {
109+ return token
112110 }
113111 }
114- return null
112+
113+ return getAccountNoActivity(context)?.first
115114 }
116115}
0 commit comments