import android.content.Intent
import android.content.IntentSender
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.credentials.*
class MainActivity : AppCompatActivity() {
lateinit var open_btn: Button
lateinit var tv1: TextView
companion object {
var CREDENTIAL_PICKER_REQUEST = 1
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
open_btn = findViewById(R.id.btn_open)
tv1 = findViewById(R.id.tv1)
open_btn.setOnClickListener {
phoneSelection()
}
}
private fun phoneSelection() {
val hintRequest = HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build()
val options = CredentialsOptions.Builder()
.forceEnableSaveDialog()
.build()
val credentialsClient = Credentials.getClient(applicationContext, options)
val intent = credentialsClient.getHintPickerIntent(hintRequest)
try {
startIntentSenderForResult(
intent.intentSender,
CREDENTIAL_PICKER_REQUEST, null, 0, 0, 0, Bundle()
)
} catch (e: IntentSender.SendIntentException) {
e.printStackTrace()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == RESULT_OK) {
val credential: Credential? = data?.getParcelableExtra(Credential.EXTRA_KEY)
credential?.apply {
tv1.text = credential.id
}
} else if (requestCode == CREDENTIAL_PICKER_REQUEST && resultCode == CredentialsApi.ACTIVITY_RESULT_NO_HINTS_AVAILABLE) {
Toast.makeText(this, "No phone numbers found", Toast.LENGTH_LONG).show();
}
}
}