Skip to content

Instantly share code, notes, and snippets.

View Younes-Charfaoui's full-sized avatar
🎨
Focusing

Younes Charfaoui Younes-Charfaoui

🎨
Focusing
View GitHub Profile
val textToSpeech = TextToSpeech(this, this)
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()
}
}
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 {
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.
}
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()
}
override fun onVerificationFailed(exception: FirebaseException?) {
Toast.makeText(
this, "Something went wrong, try again later please.",
Toast.LENGTH_LONG).show()
finish()
}
override fun onCodeSent(
verificationId: String?,
token: PhoneAuthProvider.ForceResendingToken?
) {
storedVerificationId = verificationId!!
resendToken = token!!
}
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()
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(
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)