Catch MSAL exceptions

Close #320
This commit is contained in:
MM20 2023-04-16 18:46:45 +02:00
parent d443d67895
commit b0ee0c63d8
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389

View File

@ -3,6 +3,7 @@ package de.mm20.launcher2.msservices
import android.app.Activity
import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.core.content.edit
import com.azure.core.credential.AccessToken
import com.azure.core.credential.TokenCredential
@ -43,13 +44,18 @@ class MicrosoftGraphApiHelper(val context: Context) {
if (resId == 0) return null
if (clientApplication == null) {
clientApplication = withContext(Dispatchers.IO) {
PublicClientApplication.createSingleAccountPublicClientApplication(
context.applicationContext,
resId
)
try {
PublicClientApplication.createSingleAccountPublicClientApplication(
context.applicationContext,
resId
)
} catch (e: MsalClientException) {
CrashReporter.logException(e)
null
}
}
}
return clientApplication!!
return clientApplication
}
private suspend fun acquireAccessToken(): Boolean {
@ -72,7 +78,11 @@ class MicrosoftGraphApiHelper(val context: Context) {
}
suspend fun login(context: Activity) {
val clientApplication = getClientApplication() ?: return
val clientApplication = getClientApplication() ?: return run {
withContext(Dispatchers.Main) {
Toast.makeText(context, "Something went wrong, please check the logs", Toast.LENGTH_LONG).show()
}
}
suspendCoroutine<IAuthenticationResult?> {
clientApplication.signIn(context, "", SCOPES, object : AuthenticationCallback {
override fun onSuccess(authenticationResult: IAuthenticationResult?) {