Last active
November 2, 2021 21:46
-
-
Save aykuttasil/824ab91b683910b3c141c642cd00706d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginActivity : BaseActivity() { | |
private val viewModel: LoginViewModel by viewModels() | |
override fun getViewModel(): BaseAndroidViewModel { | |
return viewModel | |
} | |
private var phoneNumber: String? = null | |
// Activity Result yakalamak için register ediyoruz | |
private var smsConsentLauncher = | |
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { | |
if (it.resultCode == Activity.RESULT_OK && it.data != null) { | |
// Mesajın tüm içeriğine erişiyoruz. İhtiyacımız olan kısmı parsing yöntemleri elde edebiliriz. | |
val message = it?.data?.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE) | |
// SMS metnini viewmodel'ımız içerisinde parse ederek ihtiyacımız olan koda ulaşıyor | |
// ve bu kodu tekrar sunucumuza göndererek doğrulama işlemini tamamlıyoruz. | |
viewModel.validateToken(message) | |
} else { | |
viewModel.showErrorDialog(msg = "Doğrulamanın gerçekleşmesi için SMS okuma izni vermelisiniz!") | |
} | |
} | |
public override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityLoginLayoutBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
// Gelen SMS'i yakalamak için SMS_RETRIEVED_ACTION ile receiver'ı register ediyoruz | |
val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION) | |
registerReceiver( | |
smsVerificationReceiver, | |
intentFilter, | |
SmsRetriever.SEND_PERMISSION, | |
null | |
) | |
// Telefon Numarası | |
binding.btnSend.setOnClickListener { | |
// SMS Consent API'nin SMS dinleme süresi 5 dakika ile sınırlı oldugu icin sunucuya istek yapılmadan hemen önce dinlemeye başlıyoruz | |
startSmsUserConsent() | |
// Sunucumuza telefon bilgisini gönderdik. Ve sunucudan bu telefon numarasına dogrulama kodu göndermesini beklyoruz. | |
viewModel.sendCodeToPhoneNumber(phoneNumber) | |
} | |
} | |
private fun startSmsUserConsent() { | |
// Start listening for SMS User Consent broadcasts from senderPhoneNumber | |
// The Task<Void> will be successful if SmsRetriever was able to start | |
// SMS User Consent, and will error if there was an error starting. | |
SmsRetriever.getClient(this).startSmsUserConsent(null) | |
.addOnSuccessListener { | |
Timber.d("SMS listening..") | |
}.addOnFailureListener { | |
Timber.e(it) | |
} | |
} | |
private val smsVerificationReceiver = object : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) { | |
val extras = intent.extras | |
val smsRetrieverStatus = extras?.get(SmsRetriever.EXTRA_STATUS) as Status | |
when (smsRetrieverStatus.statusCode) { | |
CommonStatusCodes.SUCCESS -> { | |
// Get consent intent | |
val consentIntent = | |
extras.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT) | |
try { | |
// Start activity to show consent dialog to user, activity must be started in | |
// 5 minutes, otherwise you'll receive another TIMEOUT intent | |
smsConsentLauncher.launch(consentIntent) // Activity'i başlatıyoruz | |
} catch (e: ActivityNotFoundException) { | |
Timber.e(e) | |
} | |
} | |
CommonStatusCodes.TIMEOUT -> { | |
// Time out occurred, handle the error. | |
Timber.e("SMS retriever Status timeout") | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment