start MatchActivity

This commit is contained in:
adrienmalin
2018-12-03 17:43:27 +01:00
parent 83d50e6294
commit ef230d5520
15 changed files with 714 additions and 250 deletions

View File

@ -0,0 +1,24 @@
package adrienmalin.pingpoints
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.app.AppCompatDelegate
import android.view.View
import android.arch.lifecycle.ViewModelProviders
class MatchActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
setContentView(R.layout.activity_match)
setSupportActionBar(findViewById(R.id.toolbar))
val matchModel = ViewModelProviders.of(this).get(MatchModel::class.java)
}
fun updateScore(view: View) {
}
}

View File

@ -3,8 +3,15 @@ package adrienmalin.pingpoints
import android.arch.lifecycle.ViewModel
class MatchModel : ViewModel() {
var players: List<Player> = listOf(Player(), Player())
var server: Int = 0
var ttsEnabled: Boolean = false
var sttEnabled: Boolean = false
lateinit var players: List<Player>
lateinit var server: Int
lateinit var ttsEnabled: Boolean
lateinit var sttEnabled: Boolean
fun startMatch(player1Name: String, player2Name:String, starterId: Int, enableTTS: Boolean, enableSTT: Boolean) {
players = listOf(Player(player1Name), Player(player2Name))
server = starterId
ttsEnabled = enableTTS
sttEnabled = enableSTT
}
}

View File

@ -1,19 +1,26 @@
package adrienmalin.pingpoints
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.support.v7.app.AppCompatActivity
import android.content.pm.PackageManager
import android.os.Bundle
import android.speech.SpeechRecognizer
import android.speech.tts.TextToSpeech
import android.support.design.widget.Snackbar
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.text.method.LinkMovementMethod
import android.view.View
import android.widget.*
import android.speech.tts.TextToSpeech
import android.content.Intent
import android.speech.SpeechRecognizer
val CHECK_TTS = 1
const val CHECK_TTS = 1
const val ASK_PERMISSIONS_RECORD_AUDIO = 2
class StarterNameActivity : AppCompatActivity() {
@ -29,23 +36,21 @@ class StarterNameActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_starter_name)
setSupportActionBar(findViewById(R.id.toolbar))
// Set HTML text for icons credits
findViewById<TextView>(R.id.iconsCredit).run {
setHtmlText(getString(R.string.iconCredits))
movementMethod = LinkMovementMethod.getInstance()
}
// Find views
player1NameInput = findViewById(R.id.player1Name)
player2NameInput = findViewById(R.id.player2Name)
starterRadioGroup = findViewById(R.id.starterRadioGroup)
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}
@ -94,15 +99,18 @@ class StarterNameActivity : AppCompatActivity() {
when (requestCode) {
CHECK_TTS -> {
if (resultCode != TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
Toast.makeText(applicationContext, R.string.TTS_unavailable, Toast.LENGTH_LONG).show()
Snackbar.make(
findViewById(R.id.coordinatorLayout),
R.string.TTS_unavailable,
Snackbar.LENGTH_SHORT
).show()
enableTtsSwitch?.isChecked = false
Intent().run {
action = TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA
startActivity(this)
}
}
}
else -> {
} else -> {
}
}
}
@ -111,28 +119,93 @@ class StarterNameActivity : AppCompatActivity() {
enableSttSwitch?.let {
if (it.isChecked) {
if (!SpeechRecognizer.isRecognitionAvailable(this)) {
Toast.makeText(applicationContext, R.string.STT_unavailable, Toast.LENGTH_LONG).show()
it.isChecked = false
Snackbar.make(
findViewById(R.id.coordinatorLayout),
R.string.STT_unavailable,
Snackbar.LENGTH_SHORT
).show()
enableSttSwitch?.isChecked = false
} else {
// Ask for record audio permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECORD_AUDIO)) {
AlertDialog.Builder(this)
.setTitle(R.string.STT)
.setMessage(R.string.explain_record_audio_request)
.setPositiveButton(R.string.OK) { dialog, id ->
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), ASK_PERMISSIONS_RECORD_AUDIO)
}
.setNegativeButton(R.string.cancel) { dialog, id ->
enableSttSwitch?.isChecked = false
}
.create()
.show()
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO), ASK_PERMISSIONS_RECORD_AUDIO)
}
}
}
}
}
}
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
Snackbar.make(
findViewById(R.id.coordinatorLayout),
R.string.audio_record_permission_denied,
Snackbar.LENGTH_LONG
).show()
enableSttSwitch?.isChecked = false
}
return
} else -> {
}
}
}
fun startMatch(view: View) {
val player1Name = player1NameInput?.text.toString()
val player2Name = player2NameInput?.text.toString()
val radioStarterId = starterRadioGroup?.checkedRadioButtonId
val enableTTS = enableTtsSwitch?.isChecked
val enableSTT = enableSttSwitch?.isChecked
// Save
previousMatch?.edit()?.run{
putString("previousPlayer1", player1Name)
putString("previousPlayer2", player2Name)
starterRadioGroup?.let{ putInt("previousStarterId", it.checkedRadioButtonId) }
previousPlayers?.let { putStringSet("previousPlayers", it.plus(player1Name).plus(player2Name)) }
enableTtsSwitch?.let { putBoolean("enableTTS", it.isChecked) }
enableSttSwitch?.let { putBoolean("enableSTT", it.isChecked) }
previousMatch?.edit()?.apply{
player1Name.let { putString("previousPlayer1", it) }
player2Name.let { putString("previousPlayer2", it) }
radioStarterId?.let{ putInt("previousStarterId", it) }
putStringSet("previousPlayers", previousPlayers.plus(player1Name).plus(player2Name))
enableTTS?.let { putBoolean("enableTTS", it) }
enableSTT?.let { putBoolean("enableSTT", it) }
commit()
}
startActivity(
Intent(this, MatchActivity::class.java).apply {
putExtra("player1Name", player1Name)
putExtra("player2Name", player2Name)
putExtra(
"starterId",
when(radioStarterId) {
R.id.radioPlayer1Starts -> 0
R.id.radioPlayer2Starts -> 1
else -> 0
}
)
putExtra("enableTTS", enableTTS)
putExtra("enableSTT", enableSTT)
}
)
finish()
}
}