Make Google login a suspend fun which waits for auth completion
This commit is contained in:
parent
893e644b0f
commit
f4c7f54df1
@ -153,7 +153,9 @@ class PreferencesSearchFragment : PreferenceFragmentCompat() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun googleLogin() {
|
private fun googleLogin() {
|
||||||
GoogleApiHelper.getInstance(requireContext()).login(requireActivity())
|
lifecycleScope.launch {
|
||||||
|
GoogleApiHelper.getInstance(requireContext()).login(requireActivity())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setSettingsScreen(fragment: Fragment) {
|
private fun setSettingsScreen(fragment: Fragment) {
|
||||||
|
|||||||
@ -47,7 +47,9 @@ class PreferencesServicesFragment : PreferenceFragmentCompat() {
|
|||||||
setTitle(R.string.preference_google_signin)
|
setTitle(R.string.preference_google_signin)
|
||||||
setSummary(R.string.preference_google_signin_summary)
|
setSummary(R.string.preference_google_signin_summary)
|
||||||
setOnPreferenceClickListener {
|
setOnPreferenceClickListener {
|
||||||
googleApiHelper.login(requireActivity())
|
lifecycleScope.launch {
|
||||||
|
googleApiHelper.login(requireActivity())
|
||||||
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import android.app.Activity
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.util.Log
|
||||||
import androidx.browser.customtabs.*
|
import androidx.browser.customtabs.*
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
import com.google.api.client.auth.oauth2.Credential
|
import com.google.api.client.auth.oauth2.Credential
|
||||||
@ -17,6 +18,7 @@ import com.google.api.services.drive.Drive
|
|||||||
import com.google.api.services.oauth2.Oauth2
|
import com.google.api.services.oauth2.Oauth2
|
||||||
import de.mm20.launcher2.crashreporter.CrashReporter
|
import de.mm20.launcher2.crashreporter.CrashReporter
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
@ -119,29 +121,41 @@ class GoogleApiHelper private constructor(private val context: Context) {
|
|||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun login(activity: Activity) {
|
private var callback: (() -> Unit)? = null
|
||||||
|
|
||||||
|
suspend fun login(activity: Activity) {
|
||||||
val authFlow = getAuthFlow() ?: return
|
val authFlow = getAuthFlow() ?: return
|
||||||
|
|
||||||
val url = authFlow
|
suspendCancellableCoroutine<Unit> {
|
||||||
.newAuthorizationUrl()
|
val url = authFlow
|
||||||
.setRedirectUri(getRedirectUri())
|
.newAuthorizationUrl()
|
||||||
.toString()
|
.setRedirectUri(getRedirectUri())
|
||||||
val themeColor = 0xFF4285f4.toInt()
|
.toString()
|
||||||
|
val themeColor = 0xFF4285f4.toInt()
|
||||||
|
|
||||||
val customTabsIntent = CustomTabsIntent
|
val customTabsIntent = CustomTabsIntent
|
||||||
.Builder()
|
.Builder()
|
||||||
.setDefaultColorSchemeParams(
|
.setDefaultColorSchemeParams(
|
||||||
CustomTabColorSchemeParams.Builder()
|
CustomTabColorSchemeParams.Builder()
|
||||||
.setToolbarColor(themeColor)
|
.setToolbarColor(themeColor)
|
||||||
.setNavigationBarColor(themeColor)
|
.setNavigationBarColor(themeColor)
|
||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
callingActivity = activity.javaClass
|
callingActivity = activity.javaClass
|
||||||
|
callback = {
|
||||||
|
it.resumeWith(Result.success(Unit))
|
||||||
|
}
|
||||||
|
it.invokeOnCancellation {
|
||||||
|
callback = null
|
||||||
|
Log.d("MM20", "Google Signin has been canceled")
|
||||||
|
}
|
||||||
|
|
||||||
customTabsIntent.intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY
|
customTabsIntent.intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY
|
||||||
customTabsIntent.launchUrl(activity, Uri.parse(url))
|
customTabsIntent.launchUrl(activity, Uri.parse(url))
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun finishAuthFlow(activity: Activity, code: String) {
|
suspend fun finishAuthFlow(activity: Activity, code: String) {
|
||||||
@ -168,6 +182,7 @@ class GoogleApiHelper private constructor(private val context: Context) {
|
|||||||
callingActivity = null
|
callingActivity = null
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
|
||||||
activity.startActivity(intent)
|
activity.startActivity(intent)
|
||||||
|
callback?.invoke()
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun loadAccountName(): String? {
|
private suspend fun loadAccountName(): String? {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user