Enable tracking service on main activity

This commit is contained in:
2020-05-08 21:42:06 +02:00
parent 4375eacf47
commit bb0a2a94ed
6 changed files with 46 additions and 58 deletions

View File

@@ -1,50 +1,41 @@
package io.bartek
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.*
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.provider.Settings.*
import android.view.View
import android.widget.Toast
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import io.bartek.service.ForegroundService
import io.bartek.service.ServiceState
class MainActivity : AppCompatActivity() {
private lateinit var controlServerButton: Button
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent?.let {
when(intent.getStringExtra("STATE")) {
"STARTED" -> notifyOnStart()
"STOPPED" -> notifyOnStop()
}
updateViewAccordingToServiceState(
ServiceState.valueOf(it.getStringExtra("STATE") ?: "STOPPED")
)
}
}
}
private fun notifyOnStart() {
Toast.makeText(
this,
resources.getString(R.string.server_toast_service_started),
Toast.LENGTH_SHORT
).show()
private fun updateViewAccordingToServiceState(newState: ServiceState) {
controlServerButton.isEnabled = true
when (newState) {
ServiceState.STOPPED -> controlServerButton.text = getString(R.string.main_activity_run)
ServiceState.RUNNING -> controlServerButton.text = getString(R.string.main_activity_stop)
}
private fun notifyOnStop() {
Toast.makeText(
this,
resources.getString(R.string.server_toast_service_stopped),
Toast.LENGTH_SHORT
).show()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
controlServerButton = findViewById(R.id.control_server_button)
}
override fun onResume() {
@@ -52,6 +43,7 @@ class MainActivity : AppCompatActivity() {
LocalBroadcastManager
.getInstance(this)
.registerReceiver(receiver, IntentFilter("io.bartek.web.server.CHANGE_STATE"))
updateViewAccordingToServiceState(ForegroundService.state)
}
override fun onPause() {
@@ -61,12 +53,16 @@ class MainActivity : AppCompatActivity() {
super.onPause()
}
fun startServer(view: View) = actionOnService(ForegroundService.START)
fun stopServer(view: View) = actionOnService(ForegroundService.STOP)
fun openTTSSettings(view: View) = startActivity(Intent("com.android.settings.TTS_SETTINGS"))
fun controlServer(view: View) {
controlServerButton.isEnabled = false
when (ForegroundService.state) {
ServiceState.STOPPED -> actionOnService(ForegroundService.START)
ServiceState.RUNNING -> actionOnService(ForegroundService.STOP)
}
}
private fun actionOnService(action: String) {
Intent(this, ForegroundService::class.java).also {
it.action = action
@@ -78,6 +74,4 @@ class MainActivity : AppCompatActivity() {
startService(it)
}
}
}

View File

@@ -4,12 +4,15 @@ import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.os.PowerManager
import io.bartek.MainActivity
import io.bartek.R
import io.bartek.web.TTSServer
class ForegroundService : Service() {
private var port: Int = 8080
private var wakeLock: PowerManager.WakeLock? = null
@@ -90,6 +93,7 @@ class ForegroundService : Service() {
}
}
ttsServer = TTSServer(port, this)
state = ServiceState.RUNNING
}
private fun stopService() {
@@ -103,9 +107,14 @@ class ForegroundService : Service() {
stopForeground(true)
stopSelf()
}
state = ServiceState.STOPPED
}
companion object {
// Disclaimer: I don't know the better way
// to check whether the service is already running
// than to place it as a static field
var state = ServiceState.STOPPED
private const val NOTIFICATION_CHANNEL_ID = "TTSService.NOTIFICATION_CHANNEL"
const val PORT = "TTSService.PORT"
const val START = "START"

View File

@@ -0,0 +1,6 @@
package io.bartek.service
enum class ServiceState {
RUNNING,
STOPPED
}

View File

@@ -8,6 +8,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import fi.iki.elonen.NanoHTTPD
import fi.iki.elonen.NanoHTTPD.Response.Status.*
import io.bartek.R
import io.bartek.service.ServiceState
import io.bartek.tts.TTS
import org.json.JSONObject
import java.util.*
@@ -72,7 +73,7 @@ class TTSServer(port: Int, private val context: Context) : NanoHTTPD(port),
LocalBroadcastManager
.getInstance(context)
.sendBroadcast(Intent("io.bartek.web.server.CHANGE_STATE").also {
it.putExtra("STATE", "STARTED")
it.putExtra("STATE", ServiceState.RUNNING.name)
})
}
@@ -81,7 +82,7 @@ class TTSServer(port: Int, private val context: Context) : NanoHTTPD(port),
LocalBroadcastManager
.getInstance(context)
.sendBroadcast(Intent("io.bartek.web.server.CHANGE_STATE").also {
it.putExtra("STATE", "STOPPED")
it.putExtra("STATE", ServiceState.STOPPED.name)
})
}
}

View File

@@ -12,32 +12,13 @@
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="@string/main_activity_server_port"
android:inputType="number"
android:text="8080" />
<Button
android:id="@+id/run"
android:id="@+id/control_server_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:onClick="startServer"
android:onClick="controlServer"
android:text="@string/main_activity_run" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:onClick="stopServer"
android:text="@string/main_activity_stop" />
</LinearLayout>
<Button

View File

@@ -5,9 +5,6 @@
<string name="service_notification_title">Server is running</string>
<string name="service_notification_text">The HTTP server is listening on port %1$d</string>
<string name="server_toast_service_started">TTS-HTTP Server started</string>
<string name="server_toast_service_stopped">TTS-HTTP Server stopped</string>
<string name="main_activity_server_port">Server port</string>
<string name="main_activity_run">Run</string>
<string name="main_activity_stop">Stop</string>