Fix network call on main thread

pull/2473/head
Alex Baker 2 years ago
parent 7283491872
commit 5cb8419206

@ -116,28 +116,26 @@ class MicrosoftAuthenticationActivity : ComponentActivity() {
} }
} }
private suspend fun getEmail(accessToken: String?): String? { private suspend fun getEmail(accessToken: String?): String? = withContext(Dispatchers.IO) {
if (accessToken == null) { if (accessToken == null) {
return null return@withContext null
} }
val discovery = AuthorizationServiceDiscovery( val discovery = AuthorizationServiceDiscovery(
JSONObject( JSONObject(
intent.getStringExtra(EXTRA_SERVICE_DISCOVERY)!! intent.getStringExtra(EXTRA_SERVICE_DISCOVERY)!!
) )
) )
val userInfo = withContext(Dispatchers.IO) { val userInfo = httpClientFactory
httpClientFactory .newClient(foreground = false)
.newClient(foreground = false) .newCall(
.newCall( Request.Builder()
Request.Builder() .url(discovery.userinfoEndpoint!!.toString())
.url(discovery.userinfoEndpoint!!.toString()) .addHeader("Authorization", "Bearer $accessToken")
.addHeader("Authorization", "Bearer $accessToken") .build()
.build() )
) .execute()
.execute() val response = userInfo.body?.string() ?: return@withContext null
} JSONObject(response).getString("email")
val response = userInfo.body?.string() ?: return null
return JSONObject(response).getString("email")
} }
private fun error(message: String) { private fun error(message: String) {

Loading…
Cancel
Save