This file contains hidden or 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
| val textToSpeech = TextToSpeech(this, this) |
This file contains hidden or 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
| private fun saySomething(something: String, queueMode: Int = TextToSpeech.QUEUE_ADD) { | |
| val speechStatus = textToSpeech.speak(something, queueMode, null, "ID") | |
| if (speechStatus == TextToSpeech.ERROR) { | |
| Toast.makeText(this, "Cant use the Text to speech.", Toast.LENGTH_LONG).show() | |
| } | |
| } |
This file contains hidden or 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
| override fun onInit(status: Int) { | |
| // check the results in status variable. | |
| if (status == TextToSpeech.SUCCESS) { | |
| // setting the language to the default phone language. | |
| val ttsLang = textToSpeech.setLanguage(Locale.getDefault()) | |
| // check if the language is supportable. | |
| if (ttsLang == TextToSpeech.LANG_MISSING_DATA || ttsLang == TextToSpeech.LANG_NOT_SUPPORTED) { | |
| Toast.makeText(this, "We can't support your language", Toast.LENGTH_LONG).show() | |
| } | |
| } else { |
This file contains hidden or 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
| textToSpeech.setOnUtteranceProgressListener(object : UtteranceProgressListener() { | |
| override fun onDone(utteranceId: String?) { | |
| //do whatever you want when TTS finish speaking. | |
| } | |
| override fun onError(utteranceId: String?) { | |
| //do whatever you want if TTS makes an error. | |
| } |
This file contains hidden or 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
| override fun onVerificationCompleted(credential: PhoneAuthCredential?) { | |
| if (credential != null && storedVerificationId != null) | |
| signInWithPhoneAuthCredential(credential) | |
| else { | |
| Toast.makeText( | |
| this, | |
| "Something went wrong, please try later.", | |
| Toast.LENGTH_LONG).show() | |
| finish() | |
| } |
This file contains hidden or 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
| override fun onVerificationFailed(exception: FirebaseException?) { | |
| Toast.makeText( | |
| this, "Something went wrong, try again later please.", | |
| Toast.LENGTH_LONG).show() | |
| finish() | |
| } |
This file contains hidden or 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
| override fun onCodeSent( | |
| verificationId: String?, | |
| token: PhoneAuthProvider.ForceResendingToken? | |
| ) { | |
| storedVerificationId = verificationId!! | |
| resendToken = token!! | |
| } |
This file contains hidden or 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
| private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) { | |
| auth.signInWithCredential(credential) | |
| .addOnCompleteListener(this) { | |
| if (it.isSuccessful) { | |
| // Successful mean that the phone is verified, so do whatever you want here. | |
| } else { | |
| Toast.makeText( | |
| this, | |
| "Something went wrong, try again later please.", | |
| Toast.LENGTH_LONG).show() |
This file contains hidden or 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
| import com.google.firebase.FirebaseException | |
| import com.google.firebase.auth.PhoneAuthCredential | |
| import com.google.firebase.auth.PhoneAuthProvider | |
| class PhoneCallbacks(private val listener : PhoneCallbacksListener) : PhoneAuthProvider.OnVerificationStateChangedCallbacks() { | |
| interface PhoneCallbacksListener { | |
| fun onVerificationCompleted(credential: PhoneAuthCredential?) | |
| fun onVerificationFailed(exception: FirebaseException?) | |
| fun onCodeSent( |
This file contains hidden or 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 PhoneVerificationActivity : AppCompatActivity(), PhoneCallbacks.PhoneCallbacksListener { | |
| private lateinit var auth: FirebaseAuth | |
| private var storedVerificationId: String? = null | |
| private lateinit var resendToken: PhoneAuthProvider.ForceResendingToken | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| .... | |
| auth = FirebaseAuth.getInstance() | |
| auth.setLanguageCode(Locale.getDefault().language) |