@@ -12,52 +12,60 @@ import org.microg.gms.constellation.core.proto.GaiaAccountSignalType
1212import org.microg.gms.constellation.core.proto.GaiaSignalEntry
1313import org.microg.gms.constellation.core.proto.GaiaSignals
1414import org.microg.gms.constellation.core.proto.GaiaToken
15- import java.security.MessageDigest
16- import kotlin.math.abs
17- import kotlin.math.absoluteValue
1815
1916private const val TAG = " GaiaInfoBuilder"
2017
2118
2219@RequiresApi(Build .VERSION_CODES .O )
23- operator fun GaiaSignals.Companion.invoke (context : Context ): GaiaSignals ? {
24- val entries = mutableListOf<GaiaSignalEntry >()
25- try {
26- val accounts = AccountManager .get(context).getAccountsByType(" com.google" )
27- val md = MessageDigest .getInstance(" SHA-256" )
20+ suspend operator fun GaiaSignals.Companion.invoke (context : Context ): GaiaSignals ? =
21+ withContext(Dispatchers .IO ) {
22+ val entries = mutableListOf<GaiaSignalEntry >()
23+ val accountManager = AccountManager .get(context)
2824
29- for (account in accounts) {
30- // Note: Simple implementation, maybe do actual obfuscated Gaia ID retrieval later?
31- val hash = md.digest(account.name.toByteArray(Charsets .UTF_8 ))
32- val number =
33- hash.take(8 ).fold(0L ) { acc, byte -> (acc shl 8 ) or (byte.toLong() and 0xFF ) }
34- val obfuscatedId = try {
35- number.absoluteValue.toString()
36- } catch (_: Exception ) {
37- abs(account.name.hashCode().toLong()).toString()
38- }
25+ try {
26+ val accounts = accountManager.getAccountsByType(" com.google" )
3927
40- entries.add(
41- GaiaSignalEntry (
42- gaia_id = obfuscatedId,
43- signal_type = GaiaAccountSignalType .GAIA_ACCOUNT_SIGNAL_AUTHENTICATED ,
44- timestamp = Instant .now()
28+ for (account in accounts) {
29+ var id = accountManager.getUserData(account, " GoogleUserId" )
30+ if (id == " " ) {
31+ try {
32+ val future = accountManager.getAuthToken(
33+ account,
34+ " ^^_account_id_^^" ,
35+ null ,
36+ false ,
37+ null ,
38+ null
39+ )
40+ id = future.result?.getString(AccountManager .KEY_AUTHTOKEN )
41+ accountManager.setUserData(account, " GoogleUserId" , id)
42+ } catch (e: Exception ) {
43+ Log .w(TAG , " Could not retrieve Gaia ID for account ${account.name} " , e)
44+ continue
45+ }
46+ }
47+ entries.add(
48+ GaiaSignalEntry (
49+ gaia_id = id,
50+ signal_type = GaiaAccountSignalType .GAIA_ACCOUNT_SIGNAL_AUTHENTICATED ,
51+ timestamp = Instant .now()
52+ )
4553 )
46- )
54+ }
55+ } catch (e: Exception ) {
56+ Log .w(TAG , " Could not build Gaia signals" , e)
4757 }
48- } catch (e: Exception ) {
49- Log .w(TAG , " Could not build Gaia signals" , e)
58+ if (entries.isNotEmpty()) GaiaSignals (gaia_signals = entries) else null
5059 }
51- return if (entries.isNotEmpty()) GaiaSignals (gaia_signals = entries) else null
52- }
5360
5461@Suppress(" DEPRECATION" )
5562suspend fun GaiaToken.Companion.getList (context : Context ): List <GaiaToken > =
5663 withContext(Dispatchers .IO ) {
57- val accounts = AccountManager .get(context).getAccountsByType(" com.google" )
64+ val accountManager = AccountManager .get(context)
65+ val accounts = accountManager.getAccountsByType(" com.google" )
5866 accounts.mapNotNull { account ->
5967 try {
60- val future = AccountManager .get(context) .getAuthToken(
68+ val future = accountManager .getAuthToken(
6169 account,
6270 " oauth2:https://www.googleapis.com/auth/numberer" ,
6371 null ,
@@ -70,7 +78,7 @@ suspend fun GaiaToken.Companion.getList(context: Context): List<GaiaToken> =
7078 if (! token.isNullOrBlank()) GaiaToken (token = token) else null
7179
7280 } catch (e: Exception ) {
73- Log .w(TAG , " Could not retrieve Gaia token for an account" , e)
81+ Log .w(TAG , " Could not retrieve Gaia token for account ${ account.name} " , e)
7482 null
7583 }
7684 }
0 commit comments