Why am I failing to get the new phone-picker to work properly?

28 Views Asked by At

Background

In the past, it was possible to get a phone picker using this:

implementation "com.google.android.gms:play-services-auth:20.7.0"

Code:

private val requestFillPhoneNumber: ActivityResultLauncher<IntentSenderRequest> =
    registerForActivityResult(
        ActivityResultContracts.StartIntentSenderForResult()) { result: ActivityResult ->
        if (result.resultCode != Activity.RESULT_OK)
            return@registerForActivityResult
        val cred: Credential = result.data?.getParcelableExtraCompat(Credential.EXTRA_KEY)
            ?: return@registerForActivityResult
        val phoneNumber = cred.id
        Log.d("AppLog", "phone:$phoneNumber")
    }

private fun showPhonePickerDialog() {
    val hintRequest = HintRequest.Builder().setPhoneNumberIdentifierSupported(true).build()
    val intent = Credentials.getClient(this).getHintPickerIntent(hintRequest)
    try {
        requestFillPhoneNumber.launch(IntentSenderRequest.Builder(intent.intentSender).build())
    } catch (e: Exception) {
        Log.d("AppLog", "failed to show phone picker using old method:$e")
    }
}

The problem

Recently a new version came that removed these classes, so what I can use instead is this:

implementation 'com.google.android.gms:play-services-auth:21.0.0'

Code:

private val phoneNumberHintIntentResultLauncher: ActivityResultLauncher<IntentSenderRequest> =
    registerForActivityResult(
        ActivityResultContracts.StartIntentSenderForResult()
    ) { result ->
        try {
            val phoneNumber =
                Identity.getSignInClient(this).getPhoneNumberFromIntent(result.data)
            Log.d("AppLog", "phone:$phoneNumber")
        } catch (e: Exception) {
            Log.d("AppLog", "failed to get phone number from picker:$e")
        }
    }

private fun showNewPhonePickerDialog() {
    val request: GetPhoneNumberHintIntentRequest =
        GetPhoneNumberHintIntentRequest.builder().build()
    Identity.getSignInClient(this)
        .getPhoneNumberHintIntent(request)
        .addOnSuccessListener {
            phoneNumberHintIntentResultLauncher.launch(
                IntentSenderRequest.Builder(it.intentSender).build()
            )
        }
        .addOnFailureListener { e ->
            Log.d("AppLog", "failed to show phone picker using new method:$e")
        }
}

Thing is, this is very inconsistent and also wrong sometimes:

  1. On emulator with API 34, it seems to be working fine.
  2. On Pixel 6 device with Android 14 and Samsung A32 with Android 13, it fails with the exception com.google.android.gms.common.api.ApiException: 16: No phone number is found on this device. . The dialog isn't shown.
  3. On Honor Magic4 pro with API 33, it returns me phone numbers (multi-SIM), but with incorrect country prefix ("+1" instead of "+972"). It's not even valid, according to this website:

https://libphonenumber.appspot.com/

What I've tried

While there are alternatives to these (subscriptionManager.activeSubscriptionInfoList with subscriptionManager.getPhoneNumber, and the old telephonyManager.line1Number) , using Phone permissions, this is supposed to be the official way, to let the user choose.

In the past I tried to report about the issue, but it still wasn't fixed to this day:

https://issuetracker.google.com/issues/266967506

Questions

  1. Did I do this correctly?
  2. Where should I have reported about this issue, if it's actually the correct way to do it? 3. Why do some devices return me incorrect phone number country prefix.
0

There are 0 best solutions below