diff --git a/google/build.gradle b/google/build.gradle index 1ceba9247..7d2cddd4f 100644 --- a/google/build.gradle +++ b/google/build.gradle @@ -42,6 +42,8 @@ android { dependencies { api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" api "com.google.android.gms:play-services-auth:19.2.0" + api "androidx.activity:activity-ktx:1.3.1" + api "androidx.fragment:fragment-ktx:1.3.1" implementation project(":parse") } diff --git a/google/src/main/java/com/parse/google/ParseGoogleUtils.kt b/google/src/main/java/com/parse/google/ParseGoogleUtils.kt index 6ed42f765..d9bad72e0 100644 --- a/google/src/main/java/com/parse/google/ParseGoogleUtils.kt +++ b/google/src/main/java/com/parse/google/ParseGoogleUtils.kt @@ -3,6 +3,7 @@ package com.parse.google import android.app.Activity import android.content.Context import android.content.Intent +import androidx.activity.result.ActivityResultLauncher import com.google.android.gms.auth.api.signin.GoogleSignIn import com.google.android.gms.auth.api.signin.GoogleSignInAccount import com.google.android.gms.auth.api.signin.GoogleSignInClient @@ -22,12 +23,12 @@ import com.parse.boltsinternal.TaskCompletionSource object ParseGoogleUtils { private const val AUTH_TYPE = "google" - private var clientId: String? = null + private lateinit var clientId: String private val lock = Any() private var isInitialized = false - private var googleSignInClient: GoogleSignInClient? = null + private lateinit var googleSignInClient: GoogleSignInClient /** * Just hope this doesn't clash I guess... @@ -61,33 +62,35 @@ object ParseGoogleUtils { * * @param activity The activity which passes along the result via [onActivityResult] * @param callback The [LogInCallback] which is invoked on log in success or error + * @param launcher The ActivityResultLauncher from AndroidX for Example: + * + * val launcher: ActivityResultLauncher = registerForActivityResult( + * ActivityResultContracts.StartActivityForResult()) { result -> + * ParseGoogleUtils.onActivityResult(result.resultCode, result.data!!) + * } */ @JvmStatic - fun logIn(activity: Activity, callback: LogInCallback) { + fun logIn(activity: Activity, launcher: ActivityResultLauncher, callback: LogInCallback) { checkInitialization() this.currentCallback = callback val googleSignInClient = buildGoogleSignInClient(activity) this.googleSignInClient = googleSignInClient - activity.startActivityForResult( - googleSignInClient.signInIntent, - REQUEST_CODE_GOOGLE_SIGN_IN - ) + launcher.launch(googleSignInClient.signInIntent) } /** * The method that should be called from the Activity's or Fragment's onActivityResult method. * - * @param requestCode The request code that's received by the Activity or Fragment. * @param resultCode The result code that's received by the Activity or Fragment. * @param data The result data that's received by the Activity or Fragment. * @return true if the result could be handled. */ @JvmStatic - fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean { - if (requestCode != REQUEST_CODE_GOOGLE_SIGN_IN) { + fun onActivityResult(resultCode: Int, data: Intent?): Boolean { + if (resultCode != Activity.RESULT_OK) { return false } - if (requestCode == REQUEST_CODE_GOOGLE_SIGN_IN) { + if (resultCode == Activity.RESULT_OK) { if (data != null) { handleSignInResult(data) } else { @@ -151,7 +154,7 @@ object ParseGoogleUtils { } private fun onSignedIn(account: GoogleSignInAccount) { - googleSignInClient?.signOut()?.addOnCompleteListener {} + googleSignInClient.signOut().addOnCompleteListener {} val authData: Map = getAuthData(account) ParseUser.logInWithInBackground(AUTH_TYPE, authData) .continueWith { task -> @@ -224,14 +227,14 @@ object ParseGoogleUtils { } val tcs: TaskCompletionSource = TaskCompletionSource() task.continueWith( - Continuation { task -> - if (task.isCancelled && !reportCancellation) { + Continuation { task2 -> + if (task2.isCancelled && !reportCancellation) { tcs.setCancelled() return@Continuation null } Task.UI_THREAD_EXECUTOR.execute { try { - var error = task.error + var error = task2.error if (error != null && error !is ParseException) { error = ParseException(error) } @@ -239,19 +242,19 @@ object ParseGoogleUtils { callback.done(error as? ParseException) } else if (callback is LogInCallback) { callback.done( - task.result as? ParseUser, error as? ParseException + task2.result as? ParseUser, error as? ParseException ) } } finally { when { - task.isCancelled -> { + task2.isCancelled -> { tcs.setCancelled() } - task.isFaulted -> { - tcs.setError(task.error) + task2.isFaulted -> { + tcs.setError(task2.error) } else -> { - tcs.setResult(task.result) + tcs.setResult(task2.result) } } }