Advertisement
Guest User

Untitled

a guest
May 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. class MySMSBroadcastReceiver : BroadcastReceiver() {
  2.  
  3. private var otpReceiver: OTPReceiveListener? = null
  4.  
  5. /**
  6. * @param receiver OTPReceiveListener
  7. */
  8. fun initOTPListener(receiver: OTPReceiveListener) {
  9. this.otpReceiver = receiver
  10. }
  11.  
  12. override fun onReceive(context: Context, intent: Intent) {
  13. if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
  14. val extras : Bundle = intent.extras
  15. val status : Status? = extras.get(SmsRetriever.EXTRA_STATUS) as? Status
  16.  
  17. when (status?.statusCode) {
  18. CommonStatusCodes.SUCCESS -> {
  19. // Get SMS message contents
  20. val otp : String? = extras.get(SmsRetriever.EXTRA_SMS_MESSAGE) as? String
  21.  
  22. // Extract one-time code from the message and complete verification
  23. // by sending the code back to your server for SMS authenticity.
  24. // But here we are just passing it to MainActivity
  25. otpReceiver?.let { otpReceiveListener ->
  26. otp?.let {otp ->
  27. otpReceiveListener.onOTPReceived(otp.replace("<#> Your ExampleApp code is: ", "").split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0])
  28. }
  29. }
  30. }
  31.  
  32. CommonStatusCodes.TIMEOUT ->
  33. // Waiting for SMS timed out (5 minutes)
  34. // Handle the error ...
  35. otpReceiver?.onOTPTimeOut()
  36. }
  37. }
  38. }
  39.  
  40. /**
  41. * Listener for Received OTP
  42. */
  43. interface OTPReceiveListener {
  44. /**
  45. * @param otp String
  46. */
  47. fun onOTPReceived(otp: String)
  48.  
  49. /**
  50. * Time Out
  51. */
  52. fun onOTPTimeOut()
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement