From 918eb294f3e438d80d7cb8da8cf74f2cb58c2ad3 Mon Sep 17 00:00:00 2001 From: Matas Date: Tue, 20 Dec 2022 17:53:47 +0000 Subject: [PATCH] feat(android)!: throw exception on authorize when setup is incorrect BREAKING CHANGE: If Google cloud console credential setup is incorrect throw exception when authorize method is run. --- .../googlefit/GoogleFitManager.kt | 49 ++++++++++++++++--- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/com/fitnesstracker/googlefit/GoogleFitManager.kt b/android/src/main/java/com/fitnesstracker/googlefit/GoogleFitManager.kt index c865364..92f8ea0 100644 --- a/android/src/main/java/com/fitnesstracker/googlefit/GoogleFitManager.kt +++ b/android/src/main/java/com/fitnesstracker/googlefit/GoogleFitManager.kt @@ -6,7 +6,10 @@ import com.facebook.react.bridge.* import com.fitnesstracker.permission.Permission import com.fitnesstracker.permission.PermissionKind import com.google.android.gms.auth.api.signin.GoogleSignIn +import com.google.android.gms.auth.api.signin.GoogleSignInAccount +import com.google.android.gms.common.api.ApiException import com.google.android.gms.fitness.data.DataType +import com.google.android.gms.tasks.Task class GoogleFitManager(private val reactContext: ReactApplicationContext) : ActivityEventListener { @@ -35,15 +38,33 @@ class GoogleFitManager(private val reactContext: ReactApplicationContext) : Acti data: Intent? ) { if (requestCode == GOOGLE_FIT_PERMISSIONS_REQUEST_CODE) { - if (resultCode == Activity.RESULT_OK) { - authorized = true - - /** Subscribes to tracking steps even if google fit is not installed */ - if (shouldSubscribeToSteps) recordingApi.subscribe(DataType.TYPE_STEP_COUNT_DELTA) + try { + /* Checks if correctly setup Google cloud console credentials */ + val task: Task = + GoogleSignIn.getSignedInAccountFromIntent(data) + task.getResult(ApiException::class.java) + + if (resultCode == Activity.RESULT_OK) { + handleSuccessfulLogin() + return + } - authorisationPromise?.resolve(true) - } else { authorisationPromise?.resolve(false) + } catch (e: ApiException) { + val code = e.statusCode + val errorCodeMessage = "ErrorCode: $code; " + println(errorCodeMessage) + if (e.statusCode == 10 || e.statusCode == 12500) { + authorisationPromise?.reject( + E_DEVELOPER_ERROR, + errorCodeMessage + E_DEVELOPER_ERROR_MESSAGE + ) + } else { + authorisationPromise?.reject( + E_UNKNOWN_ERROR, + errorCodeMessage + E_UNKNOWN_ERROR_MESSAGE + ) + } } } } @@ -98,12 +119,26 @@ class GoogleFitManager(private val reactContext: ReactApplicationContext) : Acti return activityHistory } + private fun handleSuccessfulLogin() { + authorized = true + + /* Subscribes to tracking steps even if google fit is not installed */ + if (shouldSubscribeToSteps) recordingApi.subscribe(DataType.TYPE_STEP_COUNT_DELTA) + + authorisationPromise?.resolve(true) + } + private fun promiseException(promise: Promise?, e: Exception) { promise!!.reject(e) e.printStackTrace() } companion object { + private const val E_DEVELOPER_ERROR = "E_DEVELOPER_ERROR" + private const val E_DEVELOPER_ERROR_MESSAGE = + "Developer error, please check if you correctly setup SHA1 and Package Name in the Google API console" + private const val E_UNKNOWN_ERROR = "E_UNKNOWN_ERROR" + private const val E_UNKNOWN_ERROR_MESSAGE = "Undefined error occurred." private const val GOOGLE_FIT_PERMISSIONS_REQUEST_CODE = 111 } }