|
| 1 | +package com.flipcash.app.phone.components |
| 2 | + |
| 3 | +import android.app.Activity.RESULT_OK |
| 4 | +import android.content.BroadcastReceiver |
| 5 | +import android.content.Context |
| 6 | +import android.content.Intent |
| 7 | +import android.content.IntentFilter |
| 8 | +import android.os.Build |
| 9 | +import android.os.Bundle |
| 10 | +import androidx.activity.compose.rememberLauncherForActivityResult |
| 11 | +import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult |
| 12 | +import androidx.compose.foundation.text.input.TextFieldState |
| 13 | +import androidx.compose.runtime.Composable |
| 14 | +import androidx.compose.runtime.DisposableEffect |
| 15 | +import androidx.compose.runtime.getValue |
| 16 | +import androidx.compose.runtime.mutableStateOf |
| 17 | +import androidx.compose.runtime.remember |
| 18 | +import androidx.compose.runtime.rememberUpdatedState |
| 19 | +import androidx.compose.ui.platform.LocalContext |
| 20 | +import androidx.core.os.BundleCompat |
| 21 | +import com.getcode.utils.trace |
| 22 | +import com.google.android.gms.auth.api.phone.SmsRetriever |
| 23 | +import com.google.android.gms.common.api.CommonStatusCodes |
| 24 | +import com.google.android.gms.common.api.Status |
| 25 | + |
| 26 | +@Composable |
| 27 | +fun SmsOtpAutofill( |
| 28 | + otpState: TextFieldState, |
| 29 | + otpLength: Int, |
| 30 | + attempts: Int, |
| 31 | +) { |
| 32 | + val context = LocalContext.current |
| 33 | + val currentOtpState by rememberUpdatedState(otpState) |
| 34 | + val currentOtpLength by rememberUpdatedState(otpLength) |
| 35 | + val filled = remember { mutableStateOf(false) } |
| 36 | + |
| 37 | + val consentLauncher = rememberLauncherForActivityResult(StartActivityForResult()) { result -> |
| 38 | + if (result.resultCode == RESULT_OK) { |
| 39 | + val message = result.data?.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE) |
| 40 | + ?: return@rememberLauncherForActivityResult |
| 41 | + parseAndFill(message, currentOtpLength, currentOtpState, filled) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + DisposableEffect(attempts) { |
| 46 | + filled.value = false |
| 47 | + |
| 48 | + val client = SmsRetriever.getClient(context) |
| 49 | + client.startSmsRetriever() |
| 50 | + .addOnSuccessListener { trace("SmsRetriever started") } |
| 51 | + .addOnFailureListener { trace("SmsRetriever failed to start: ${it.message}") } |
| 52 | + client.startSmsUserConsent(null) |
| 53 | + .addOnSuccessListener { trace("SmsUserConsent started") } |
| 54 | + .addOnFailureListener { trace("SmsUserConsent failed to start: ${it.message}") } |
| 55 | + |
| 56 | + val filter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION) |
| 57 | + |
| 58 | + val receiver = object : BroadcastReceiver() { |
| 59 | + override fun onReceive(ctx: Context, intent: Intent) { |
| 60 | + if (intent.action != SmsRetriever.SMS_RETRIEVED_ACTION) return |
| 61 | + val extras: Bundle = intent.extras ?: return |
| 62 | + // GMS classes (Status) need the GMS class loader for deserialization |
| 63 | + extras.classLoader = SmsRetriever::class.java.classLoader |
| 64 | + |
| 65 | + val status = BundleCompat.getParcelable(extras, SmsRetriever.EXTRA_STATUS, Status::class.java) |
| 66 | + if (status?.statusCode != CommonStatusCodes.SUCCESS) { |
| 67 | + trace("SMS retrieval failed with status: ${status?.statusCode}") |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + // Retriever path: SMS message directly available |
| 72 | + val message = extras.getString(SmsRetriever.EXTRA_SMS_MESSAGE) |
| 73 | + if (message != null) { |
| 74 | + trace("SmsRetriever received message") |
| 75 | + parseAndFill(message, currentOtpLength, currentOtpState, filled) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + // User Consent path: need to launch consent dialog |
| 80 | + if (filled.value) return |
| 81 | + |
| 82 | + val consentIntent = BundleCompat.getParcelable( |
| 83 | + extras, |
| 84 | + SmsRetriever.EXTRA_CONSENT_INTENT, |
| 85 | + Intent::class.java |
| 86 | + ) |
| 87 | + if (consentIntent != null) { |
| 88 | + trace("Launching SMS consent dialog") |
| 89 | + try { |
| 90 | + consentLauncher.launch(consentIntent) |
| 91 | + } catch (e: Exception) { |
| 92 | + trace("Failed to launch consent dialog: ${e.message}") |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { |
| 99 | + context.registerReceiver( |
| 100 | + receiver, filter, SmsRetriever.SEND_PERMISSION, |
| 101 | + null, Context.RECEIVER_EXPORTED, |
| 102 | + ) |
| 103 | + } else { |
| 104 | + context.registerReceiver(receiver, filter, SmsRetriever.SEND_PERMISSION, null) |
| 105 | + } |
| 106 | + |
| 107 | + onDispose { |
| 108 | + context.unregisterReceiver(receiver) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +private fun parseAndFill( |
| 114 | + message: String, |
| 115 | + otpLength: Int, |
| 116 | + state: TextFieldState, |
| 117 | + filled: androidx.compose.runtime.MutableState<Boolean>, |
| 118 | +) { |
| 119 | + if (filled.value) return |
| 120 | + val otp = """\b(\d{$otpLength})\b""".toRegex().find(message)?.groupValues?.get(1) ?: return |
| 121 | + trace("Auto-filling OTP") |
| 122 | + state.edit { replace(0, length, otp) } |
| 123 | + filled.value = true |
| 124 | +} |
0 commit comments