Reset connected Google account if datastore could not be read
This commit is contained in:
parent
8dd0852a57
commit
b1851401f2
@ -20,6 +20,7 @@ import de.mm20.launcher2.crashreporter.CrashReporter
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
class GoogleApiHelper private constructor(private val context: Context) {
|
class GoogleApiHelper private constructor(private val context: Context) {
|
||||||
@ -100,25 +101,38 @@ class GoogleApiHelper private constructor(private val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun getAuthFlow(): GoogleAuthorizationCodeFlow? {
|
private fun getAuthFlow(throwErrors: Boolean = false): GoogleAuthorizationCodeFlow? {
|
||||||
val configResId = getConfigResId()
|
val configResId = getConfigResId()
|
||||||
if (configResId == 0) return null
|
if (configResId == 0) return null
|
||||||
val jsonFactory = GsonFactory.getDefaultInstance()
|
val jsonFactory = GsonFactory.getDefaultInstance()
|
||||||
return GoogleAuthorizationCodeFlow.Builder(
|
try {
|
||||||
NetHttpTransport(),
|
return GoogleAuthorizationCodeFlow.Builder(
|
||||||
jsonFactory,
|
NetHttpTransport(),
|
||||||
GoogleClientSecrets.load(
|
|
||||||
jsonFactory,
|
jsonFactory,
|
||||||
context.resources.openRawResource(configResId).reader()
|
GoogleClientSecrets.load(
|
||||||
),
|
jsonFactory,
|
||||||
SCOPES
|
context.resources.openRawResource(configResId).reader()
|
||||||
)
|
),
|
||||||
.setCredentialDataStore(
|
SCOPES
|
||||||
FileDataStoreFactory(context.filesDir).getDataStore(
|
|
||||||
"google_signin"
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.build()
|
.setCredentialDataStore(
|
||||||
|
FileDataStoreFactory(context.filesDir).getDataStore(
|
||||||
|
"google_signin"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.build()
|
||||||
|
} catch (e: IOException) {
|
||||||
|
if (throwErrors) throw e
|
||||||
|
else {
|
||||||
|
File(context.filesDir, "google_signin").delete()
|
||||||
|
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit {
|
||||||
|
putString(PREF_ACCOUNT_NAME, null)
|
||||||
|
}
|
||||||
|
Log.e("MM20", "Google account has been reset because data store couldn't be readg")
|
||||||
|
CrashReporter.logException(e)
|
||||||
|
return getAuthFlow(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var callback: (() -> Unit)? = null
|
private var callback: (() -> Unit)? = null
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user