Force 3-characters wide spacing and refactor & reformat code accordingly to the new rules
This commit is contained in:
10
.idea/codeStyles/Project.xml
generated
10
.idea/codeStyles/Project.xml
generated
@@ -3,6 +3,12 @@
|
||||
<JetCodeStyleSettings>
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
</JetCodeStyleSettings>
|
||||
<codeStyleSettings language="JAVA">
|
||||
<indentOptions>
|
||||
<option name="INDENT_SIZE" value="3" />
|
||||
<option name="TAB_SIZE" value="3" />
|
||||
</indentOptions>
|
||||
</codeStyleSettings>
|
||||
<codeStyleSettings language="XML">
|
||||
<indentOptions>
|
||||
<option name="CONTINUATION_INDENT_SIZE" value="4" />
|
||||
@@ -117,6 +123,10 @@
|
||||
</codeStyleSettings>
|
||||
<codeStyleSettings language="kotlin">
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
<indentOptions>
|
||||
<option name="INDENT_SIZE" value="3" />
|
||||
<option name="TAB_SIZE" value="3" />
|
||||
</indentOptions>
|
||||
</codeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
||||
@@ -1,13 +1,11 @@
|
||||
package io.bartek
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
|
||||
@@ -20,18 +20,14 @@ import io.bartek.service.ServiceState
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
private lateinit var controlServerButton: AppCompatImageButton
|
||||
private lateinit var serverControlButton: AppCompatImageButton
|
||||
private lateinit var promptText: TextView
|
||||
|
||||
private val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
intent?.let {
|
||||
updateViewAccordingToServiceState(
|
||||
ServiceState.valueOf(
|
||||
it.getStringExtra(ForegroundService.STATE) ?: ServiceState.STOPPED.name
|
||||
)
|
||||
)
|
||||
}
|
||||
(intent?.getStringExtra(ForegroundService.STATE) ?: ServiceState.STOPPED.name)
|
||||
.let { ServiceState.valueOf(it) }
|
||||
.let { updateViewAccordingToServiceState(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,14 +46,14 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun updateViewAccordingToServiceState(newState: ServiceState) {
|
||||
controlServerButton.isEnabled = true
|
||||
serverControlButton.isEnabled = true
|
||||
when (newState) {
|
||||
ServiceState.STOPPED -> {
|
||||
controlServerButton.setImageResource(R.drawable.ic_power_off)
|
||||
serverControlButton.setImageResource(R.drawable.ic_power_off)
|
||||
promptText.text = getString(R.string.main_activity_prompt_to_run)
|
||||
}
|
||||
ServiceState.RUNNING -> {
|
||||
controlServerButton.setImageResource(R.drawable.ic_power_on)
|
||||
serverControlButton.setImageResource(R.drawable.ic_power_on)
|
||||
promptText.text = getString(R.string.main_activity_prompt_to_stop)
|
||||
}
|
||||
}
|
||||
@@ -66,7 +62,7 @@ class MainActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
controlServerButton = findViewById(R.id.control_server_button)
|
||||
serverControlButton = findViewById(R.id.server_control_button)
|
||||
promptText = findViewById(R.id.prompt_text)
|
||||
}
|
||||
|
||||
@@ -86,7 +82,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
fun controlServer(view: View) {
|
||||
controlServerButton.isEnabled = false
|
||||
serverControlButton.isEnabled = false
|
||||
when (ForegroundService.state) {
|
||||
ServiceState.STOPPED -> actionOnService(ForegroundService.START)
|
||||
ServiceState.RUNNING -> actionOnService(ForegroundService.STOP)
|
||||
|
||||
@@ -3,7 +3,6 @@ package io.bartek.preference
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import androidx.preference.EditTextPreference
|
||||
import java.lang.Integer.parseInt
|
||||
|
||||
class IntEditTextPreference : EditTextPreference {
|
||||
constructor(
|
||||
@@ -23,10 +22,12 @@ class IntEditTextPreference : EditTextPreference {
|
||||
|
||||
constructor(context: Context?) : super(context)
|
||||
|
||||
override fun getPersistedString(defaultReturnValue: String?) =
|
||||
getPersistedInt(Integer.valueOf(defaultReturnValue ?: "-1")).toString()
|
||||
|
||||
override fun persistString(value: String?) = persistInt(Integer.valueOf(value ?: "-1"))
|
||||
|
||||
override fun getPersistedString(defaultReturnValue: String?) = (defaultReturnValue ?: "-1")
|
||||
.let { Integer.valueOf(it) }
|
||||
.let { getPersistedInt(it) }
|
||||
.toString()
|
||||
|
||||
override fun persistString(value: String?) = (value ?: "-1")
|
||||
.let { Integer.valueOf(it) }
|
||||
.let { persistInt(it) }
|
||||
}
|
||||
@@ -22,13 +22,9 @@ class PreferencesFragment : PreferenceFragmentCompat() {
|
||||
|
||||
private val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
intent?.let {
|
||||
updateViewAccordingToServiceState(
|
||||
ServiceState.valueOf(
|
||||
it.getStringExtra(ForegroundService.STATE) ?: ServiceState.STOPPED.name
|
||||
)
|
||||
)
|
||||
}
|
||||
(intent?.getStringExtra(ForegroundService.STATE) ?: ServiceState.STOPPED.name)
|
||||
.let { ServiceState.valueOf(it) }
|
||||
.let { updateViewAccordingToServiceState(it) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,10 +17,7 @@ class ForegroundNotificationFactory(private val context: Context) {
|
||||
|
||||
fun createForegroundNotification(port: Int): Notification {
|
||||
createNotificationChannel()
|
||||
|
||||
val pendingIntent = createPendingIntent()
|
||||
|
||||
return buildNotification(port, pendingIntent)
|
||||
return buildNotification(port, createPendingIntent())
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
@@ -30,7 +27,7 @@ class ForegroundNotificationFactory(private val context: Context) {
|
||||
.setContentText(context.resources.getString(R.string.service_notification_text, port))
|
||||
.setContentIntent(pendingIntent)
|
||||
.setSmallIcon(R.drawable.ic_foreground_service)
|
||||
.setTicker(context.getString(R.string.service_notification_text))
|
||||
.setTicker(context.getString(R.string.service_notification_text, port))
|
||||
.setPriority(Notification.PRIORITY_HIGH) // for under android 26 compatibility
|
||||
.build()
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.bartek.service
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
@@ -7,18 +8,19 @@ import android.content.SharedPreferences
|
||||
import android.os.PowerManager
|
||||
import androidx.preference.PreferenceManager
|
||||
import io.bartek.preference.PreferenceKey
|
||||
import io.bartek.web.TTSServer
|
||||
import io.bartek.web.WebServer
|
||||
|
||||
|
||||
class ForegroundService : Service() {
|
||||
private lateinit var preferences: SharedPreferences
|
||||
private var wakeLock: PowerManager.WakeLock? = null
|
||||
private var isServiceStarted = false
|
||||
private var ttsServer: TTSServer? = null
|
||||
private var webServer: WebServer? = null
|
||||
private val port: Int
|
||||
get() = preferences.getInt(PreferenceKey.PORT, 8080)
|
||||
private val notificationFactory = ForegroundNotificationFactory(this)
|
||||
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
preferences = PreferenceManager.getDefaultSharedPreferences(this)
|
||||
@@ -29,7 +31,7 @@ class ForegroundService : Service() {
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
intent?.let {
|
||||
when(it.action) {
|
||||
when (it.action) {
|
||||
START -> startService()
|
||||
STOP -> stopService()
|
||||
}
|
||||
@@ -39,11 +41,12 @@ class ForegroundService : Service() {
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
ttsServer = null
|
||||
webServer = null
|
||||
}
|
||||
|
||||
@SuppressLint("WakelockTimeout")
|
||||
private fun startService() {
|
||||
if(isServiceStarted) return
|
||||
if (isServiceStarted) return
|
||||
isServiceStarted = true
|
||||
wakeLock =
|
||||
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
|
||||
@@ -51,15 +54,15 @@ class ForegroundService : Service() {
|
||||
acquire()
|
||||
}
|
||||
}
|
||||
ttsServer = TTSServer(port, this)
|
||||
webServer = WebServer(port, this)
|
||||
state = ServiceState.RUNNING
|
||||
}
|
||||
|
||||
private fun stopService() {
|
||||
ttsServer?.stop()
|
||||
ttsServer = null
|
||||
webServer?.stop()
|
||||
webServer = null
|
||||
wakeLock?.let {
|
||||
if(it.isHeld) {
|
||||
if (it.isHeld) {
|
||||
it.release()
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import io.bartek.exception.TTSException
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.FileInputStream
|
||||
import java.io.InputStream
|
||||
import java.lang.RuntimeException
|
||||
import java.util.*
|
||||
|
||||
data class SpeechData(val stream: InputStream, val size: Long)
|
||||
@@ -51,12 +50,14 @@ class TTS(context: Context, initListener: TextToSpeech.OnInitListener) {
|
||||
lock.wait()
|
||||
}
|
||||
|
||||
if(!lock.success) {
|
||||
if (!lock.success) {
|
||||
throw TTSException()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
|
||||
// TODO: Investigate the Kotlin way to achieve the same
|
||||
private data class Lock(var success: Boolean = false) : Object()
|
||||
|
||||
private class TTSProcessListener(
|
||||
@@ -72,6 +73,7 @@ private class TTSProcessListener(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onError(utteranceId: String?) {
|
||||
if (utteranceId == uuid) {
|
||||
synchronized(lock) {
|
||||
@@ -82,5 +84,4 @@ private class TTSProcessListener(
|
||||
}
|
||||
|
||||
override fun onStart(utteranceId: String?) {}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import java.util.*
|
||||
private data class TTSRequestData(val text: String, val language: Locale)
|
||||
|
||||
|
||||
class TTSServer(port: Int, private val context: Context) : NanoHTTPD(port),
|
||||
class WebServer(port: Int, private val context: Context) : NanoHTTPD(port),
|
||||
TextToSpeech.OnInitListener {
|
||||
private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
private val tts = TTS(context, this)
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
android:textAlignment="center" />
|
||||
|
||||
<ImageButton
|
||||
android:id="@+id/control_server_button"
|
||||
android:id="@+id/server_control_button"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="#00000000"
|
||||
|
||||
Reference in New Issue
Block a user