change check TTS and STT
This commit is contained in:
parent
d2df43bf02
commit
dccc75d1ea
@ -1,6 +1,7 @@
|
||||
package adrienmalin.pingpoints
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
@ -13,6 +14,7 @@ import android.support.v4.app.ActivityCompat
|
||||
import android.support.v4.content.ContextCompat
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.widget.*
|
||||
|
||||
@ -29,6 +31,7 @@ class StarterNameActivity : AppCompatActivity() {
|
||||
var previousMatch: SharedPreferences? = null
|
||||
var previousPlayers: Set<String> = emptySet()
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_starter_name)
|
||||
@ -40,12 +43,6 @@ class StarterNameActivity : AppCompatActivity() {
|
||||
enableTtsSwitch = findViewById(R.id.enableTtsSwitch)
|
||||
enableSttSwitch = findViewById(R.id.enableSttSwitch)
|
||||
|
||||
// Check if function is available on switch checked or swapped
|
||||
enableTtsSwitch?.setOnCheckedChangeListener { view, isChecked -> checkTTS() }
|
||||
enableTtsSwitch?.setOnTouchListener { view, event -> checkTTS(); false}
|
||||
enableSttSwitch?.setOnCheckedChangeListener { view, isChecked -> checkSTT() }
|
||||
enableSttSwitch?.setOnTouchListener { view, event -> checkSTT(); false}
|
||||
|
||||
// Restore previous data
|
||||
previousMatch = getPreferences(Context.MODE_PRIVATE)
|
||||
previousMatch?.apply {
|
||||
@ -70,20 +67,43 @@ class StarterNameActivity : AppCompatActivity() {
|
||||
enableTtsSwitch?.isChecked = getBoolean("enableTTS", false)
|
||||
enableSttSwitch?.isChecked = getBoolean("enableSTT", false)
|
||||
}
|
||||
}
|
||||
|
||||
fun swapNames(view: View) {
|
||||
player1NameInput?.text = player2NameInput?.text.also {
|
||||
player2NameInput?.text = player1NameInput?.text
|
||||
}
|
||||
}
|
||||
|
||||
fun checkTTS(){
|
||||
if (enableTtsSwitch?.isChecked == true) {
|
||||
Intent().apply {
|
||||
action = TextToSpeech.Engine.ACTION_CHECK_TTS_DATA
|
||||
startActivityForResult(this, CHECK_TTS)
|
||||
// Check if function is available on switch checked or swapped
|
||||
enableTtsSwitch?.setOnTouchListener { view, event ->
|
||||
if ((event.action == MotionEvent.ACTION_DOWN) and (enableTtsSwitch?.isChecked == false)) {
|
||||
Intent().apply {
|
||||
action = TextToSpeech.Engine.ACTION_CHECK_TTS_DATA
|
||||
startActivityForResult(this, CHECK_TTS)
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
enableSttSwitch?.setOnTouchListener { view, event ->
|
||||
if ((event.action == MotionEvent.ACTION_DOWN) and (enableSttSwitch?.isChecked == false)) {
|
||||
if (SpeechRecognizer.isRecognitionAvailable(this@StarterNameActivity)) {
|
||||
// Ask for record audio permission
|
||||
if (ContextCompat.checkSelfPermission(
|
||||
this@StarterNameActivity,
|
||||
Manifest.permission.RECORD_AUDIO
|
||||
) != PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
ActivityCompat.requestPermissions(
|
||||
this@StarterNameActivity,
|
||||
arrayOf(Manifest.permission.RECORD_AUDIO),
|
||||
ASK_PERMISSIONS_RECORD_AUDIO
|
||||
)
|
||||
}
|
||||
} else {
|
||||
enableSttSwitch?.isChecked = false
|
||||
Snackbar.make(
|
||||
findViewById(R.id.coordinatorLayout),
|
||||
R.string.STT_unavailable,
|
||||
Snackbar.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,92 +111,44 @@ class StarterNameActivity : AppCompatActivity() {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
when (requestCode) {
|
||||
CHECK_TTS -> {
|
||||
if (resultCode != TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
|
||||
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
|
||||
enableTtsSwitch?.isChecked = true
|
||||
} else {
|
||||
enableTtsSwitch?.isChecked = false
|
||||
Snackbar.make(
|
||||
findViewById(R.id.coordinatorLayout),
|
||||
R.string.TTS_unavailable,
|
||||
Snackbar.LENGTH_SHORT
|
||||
).show()
|
||||
enableTtsSwitch?.isChecked = false
|
||||
Intent().apply {
|
||||
action = TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA
|
||||
startActivity(this)
|
||||
}
|
||||
}
|
||||
} else -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun checkSTT(){
|
||||
enableSttSwitch?.apply {
|
||||
if (isChecked) {
|
||||
if (!SpeechRecognizer.isRecognitionAvailable(this@StarterNameActivity)) {
|
||||
Snackbar.make(
|
||||
findViewById(R.id.coordinatorLayout),
|
||||
R.string.STT_unavailable,
|
||||
Snackbar.LENGTH_SHORT
|
||||
).show()
|
||||
isChecked = false
|
||||
} else {
|
||||
// Ask for record audio permission
|
||||
if (ContextCompat.checkSelfPermission(
|
||||
this@StarterNameActivity,
|
||||
Manifest.permission.RECORD_AUDIO
|
||||
) != PackageManager.PERMISSION_GRANTED) {
|
||||
// Permission is not granted
|
||||
// Should we show an explanation?
|
||||
if (ActivityCompat.shouldShowRequestPermissionRationale(
|
||||
this@StarterNameActivity,
|
||||
Manifest.permission.RECORD_AUDIO
|
||||
)
|
||||
) {
|
||||
AlertDialog.Builder(this@StarterNameActivity).apply {
|
||||
setTitle(R.string.STT)
|
||||
setMessage(R.string.explain_record_audio_request)
|
||||
setPositiveButton(R.string.OK) { dialog, id ->
|
||||
ActivityCompat.requestPermissions(
|
||||
this@StarterNameActivity,
|
||||
arrayOf(Manifest.permission.RECORD_AUDIO),
|
||||
ASK_PERMISSIONS_RECORD_AUDIO
|
||||
)
|
||||
}
|
||||
setNegativeButton(R.string.cancel) { dialog, id ->
|
||||
isChecked = false
|
||||
}
|
||||
create()
|
||||
show()
|
||||
}
|
||||
} else {
|
||||
// No explanation needed, we can request the permission.
|
||||
ActivityCompat.requestPermissions(
|
||||
this@StarterNameActivity,
|
||||
arrayOf(Manifest.permission.RECORD_AUDIO),
|
||||
ASK_PERMISSIONS_RECORD_AUDIO
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
||||
when (requestCode) {
|
||||
ASK_PERMISSIONS_RECORD_AUDIO -> {
|
||||
// If request is cancelled, the result arrays are empty.
|
||||
if ((grantResults.isNotEmpty() && grantResults[0] != PackageManager.PERMISSION_GRANTED)) {
|
||||
// permission denied
|
||||
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
|
||||
enableSttSwitch?.isChecked = true
|
||||
} else {
|
||||
enableSttSwitch?.isChecked = false
|
||||
Snackbar.make(
|
||||
findViewById(R.id.coordinatorLayout),
|
||||
R.string.audio_record_permission_denied,
|
||||
Snackbar.LENGTH_LONG
|
||||
).show()
|
||||
enableSttSwitch?.isChecked = false
|
||||
}
|
||||
return
|
||||
} else -> {
|
||||
}
|
||||
} else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
fun swapNames(view: View) {
|
||||
player1NameInput?.text = player2NameInput?.text.also {
|
||||
player2NameInput?.text = player1NameInput?.text
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,10 +12,6 @@
|
||||
<string name="iconCredits">Icônes par <a href="http://www.freepik.com" title="Freepik">Freepik</a> chez <a href="https://www.flaticon.com/" title="Flaticon">flaticon.com</a> Licence <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></string>
|
||||
<string name="TTS_unavailable">Désolé, votre appareil ne permet pas la synthèse vocale.</string>
|
||||
<string name="STT_unavailable">Désolé, votre appareil ne permet pas la reconnaissance vocale.</string>
|
||||
<string name="explain_record_audio_request">"Cette appli utilise l'API de Google pour la reconnaissance vocale : votre voix sera enregistrée et envoyée aux serveurs de Google pour analyse.
|
||||
Cela requiert une connexion internet et la permission d'enregistrer des fichiers audio.
|
||||
Vous pouvez à tout moment changer la permission dans les paramètres Android."
|
||||
</string>
|
||||
<string name="OK">OK</string>
|
||||
<string name="cancel">Annuler</string>
|
||||
<string name="audio_record_permission_denied">La reconnaissance vocale a besoin de la permission d\'enregistrer des
|
||||
|
@ -15,11 +15,6 @@
|
||||
</string>
|
||||
<string name="TTS_unavailable">Sorry, your device doesn\'t support text to speech.</string>
|
||||
<string name="STT_unavailable">Sorry, your device doesn\'t support voice recognition.</string>
|
||||
<string name="explain_record_audio_request">"This app uses Google API for voice recognition: your voice will be
|
||||
recorded and analyzed by Google's servers.
|
||||
It requires an internet connection and permission for audio recording.
|
||||
You can always change permission in Android settings."
|
||||
</string>
|
||||
<string name="OK">OK</string>
|
||||
<string name="cancel">Cancel</string>
|
||||
<string name="audio_record_permission_denied">Audio recording permission required to use voice recognition.</string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user