Refactor components
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
android:theme="@style/AppTheme">
|
||||
|
||||
<service
|
||||
android:name=".tts.WebService"
|
||||
android:name=".service.ForegroundService"
|
||||
android:enabled="true"
|
||||
android:exported="true" />
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import android.os.Build
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import io.bartek.tts.WebService
|
||||
import io.bartek.service.ForegroundService
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
@@ -16,12 +16,12 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
}
|
||||
|
||||
fun startServer(view: View) = actionOnService(WebService.START)
|
||||
fun startServer(view: View) = actionOnService(ForegroundService.START)
|
||||
|
||||
fun stopServer(view: View) = actionOnService(WebService.STOP)
|
||||
fun stopServer(view: View) = actionOnService(ForegroundService.STOP)
|
||||
|
||||
private fun actionOnService(action: String) {
|
||||
Intent(this, WebService::class.java).also {
|
||||
Intent(this, ForegroundService::class.java).also {
|
||||
it.action = action
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
startForegroundService(it)
|
||||
|
||||
3
app/src/main/java/io/bartek/exception/TTSException.kt
Normal file
3
app/src/main/java/io/bartek/exception/TTSException.kt
Normal file
@@ -0,0 +1,3 @@
|
||||
package io.bartek.exception
|
||||
|
||||
class TTSException : Exception("TTS process failed")
|
||||
@@ -1,20 +1,16 @@
|
||||
package io.bartek.tts
|
||||
package io.bartek.service
|
||||
|
||||
import android.app.*
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import android.os.PowerManager
|
||||
import android.telephony.ServiceState
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import io.bartek.MainActivity
|
||||
import io.bartek.R
|
||||
import io.bartek.web.TTSServer
|
||||
|
||||
class WebService : Service() {
|
||||
class ForegroundService : Service() {
|
||||
private var port: Int = 8080
|
||||
private var wakeLock: PowerManager.WakeLock? = null
|
||||
private var isServiceStarted = false
|
||||
@@ -3,6 +3,7 @@ package io.bartek.tts
|
||||
import android.content.Context
|
||||
import android.speech.tts.TextToSpeech
|
||||
import android.speech.tts.UtteranceProgressListener
|
||||
import io.bartek.exception.TTSException
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.FileInputStream
|
||||
import java.io.InputStream
|
||||
@@ -28,7 +29,7 @@ class TTS(context: Context, initListener: TextToSpeech.OnInitListener) {
|
||||
}
|
||||
|
||||
if (!lock.success) {
|
||||
throw RuntimeException("TTS failed")
|
||||
throw TTSException()
|
||||
}
|
||||
|
||||
val stream = BufferedInputStream(FileInputStream(file))
|
||||
@@ -42,8 +43,10 @@ class TTS(context: Context, initListener: TextToSpeech.OnInitListener) {
|
||||
|
||||
private data class Lock(var success: Boolean = false) : Object()
|
||||
|
||||
private class TTSProcessListener(private val uuid: String, private val lock: Lock) :
|
||||
UtteranceProgressListener() {
|
||||
private class TTSProcessListener(
|
||||
private val uuid: String,
|
||||
private val lock: Lock
|
||||
) : UtteranceProgressListener() {
|
||||
|
||||
override fun onDone(utteranceId: String?) {
|
||||
if (utteranceId == uuid) {
|
||||
|
||||
Reference in New Issue
Block a user