Skip to content

Commit

Permalink
feat(android)!: throw exception on authorize when setup is incorrect
Browse files Browse the repository at this point in the history
BREAKING CHANGE: If Google cloud console credential setup is incorrect
throw exception when authorize method is run.
  • Loading branch information
alarm109 committed Dec 20, 2022
1 parent 97a05d6 commit 918eb29
Showing 1 changed file with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<GoogleSignInAccount> =
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
)
}
}
}
}
Expand Down Expand Up @@ -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
}
}

0 comments on commit 918eb29

Please sign in to comment.