diff --git a/app/src/main/java/de/mm20/launcher2/fragment/PreferencesSearchFragment.kt b/app/src/main/java/de/mm20/launcher2/fragment/PreferencesSearchFragment.kt index 9681c3a4..5422ebf9 100644 --- a/app/src/main/java/de/mm20/launcher2/fragment/PreferencesSearchFragment.kt +++ b/app/src/main/java/de/mm20/launcher2/fragment/PreferencesSearchFragment.kt @@ -153,7 +153,9 @@ class PreferencesSearchFragment : PreferenceFragmentCompat() { } private fun googleLogin() { - GoogleApiHelper.getInstance(requireContext()).login(requireActivity()) + lifecycleScope.launch { + GoogleApiHelper.getInstance(requireContext()).login(requireActivity()) + } } private fun setSettingsScreen(fragment: Fragment) { diff --git a/app/src/main/java/de/mm20/launcher2/fragment/PreferencesServicesFragment.kt b/app/src/main/java/de/mm20/launcher2/fragment/PreferencesServicesFragment.kt index 2fb04beb..d339c4e0 100644 --- a/app/src/main/java/de/mm20/launcher2/fragment/PreferencesServicesFragment.kt +++ b/app/src/main/java/de/mm20/launcher2/fragment/PreferencesServicesFragment.kt @@ -47,7 +47,9 @@ class PreferencesServicesFragment : PreferenceFragmentCompat() { setTitle(R.string.preference_google_signin) setSummary(R.string.preference_google_signin_summary) setOnPreferenceClickListener { - googleApiHelper.login(requireActivity()) + lifecycleScope.launch { + googleApiHelper.login(requireActivity()) + } true } } diff --git a/g-services/src/main/java/de/mm20/launcher2/gservices/GoogleApiHelper.kt b/g-services/src/main/java/de/mm20/launcher2/gservices/GoogleApiHelper.kt index cec25f6f..e5cac1e1 100644 --- a/g-services/src/main/java/de/mm20/launcher2/gservices/GoogleApiHelper.kt +++ b/g-services/src/main/java/de/mm20/launcher2/gservices/GoogleApiHelper.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.content.Context import android.content.Intent import android.net.Uri +import android.util.Log import androidx.browser.customtabs.* import androidx.core.content.edit 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 de.mm20.launcher2.crashreporter.CrashReporter import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withContext import java.io.IOException @@ -119,29 +121,41 @@ class GoogleApiHelper private constructor(private val context: Context) { .build() } - fun login(activity: Activity) { + private var callback: (() -> Unit)? = null + + suspend fun login(activity: Activity) { val authFlow = getAuthFlow() ?: return - val url = authFlow - .newAuthorizationUrl() - .setRedirectUri(getRedirectUri()) - .toString() - val themeColor = 0xFF4285f4.toInt() + suspendCancellableCoroutine { + val url = authFlow + .newAuthorizationUrl() + .setRedirectUri(getRedirectUri()) + .toString() + val themeColor = 0xFF4285f4.toInt() - val customTabsIntent = CustomTabsIntent - .Builder() - .setDefaultColorSchemeParams( - CustomTabColorSchemeParams.Builder() - .setToolbarColor(themeColor) - .setNavigationBarColor(themeColor) - .build() - ) - .build() + val customTabsIntent = CustomTabsIntent + .Builder() + .setDefaultColorSchemeParams( + CustomTabColorSchemeParams.Builder() + .setToolbarColor(themeColor) + .setNavigationBarColor(themeColor) + .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.launchUrl(activity, Uri.parse(url)) + customTabsIntent.intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY + customTabsIntent.launchUrl(activity, Uri.parse(url)) + + } } suspend fun finishAuthFlow(activity: Activity, code: String) { @@ -168,6 +182,7 @@ class GoogleApiHelper private constructor(private val context: Context) { callingActivity = null intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP) activity.startActivity(intent) + callback?.invoke() } private suspend fun loadAccountName(): String? {