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

View File

@@ -4,12 +4,15 @@ import android.app.*
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.os.Binder
import android.os.Build import android.os.Build
import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import io.bartek.MainActivity import io.bartek.MainActivity
import io.bartek.R import io.bartek.R
import io.bartek.web.TTSServer import io.bartek.web.TTSServer
class ForegroundService : Service() { class ForegroundService : Service() {
private var port: Int = 8080 private var port: Int = 8080
private var wakeLock: PowerManager.WakeLock? = null private var wakeLock: PowerManager.WakeLock? = null
@@ -90,6 +93,7 @@ class ForegroundService : Service() {
} }
} }
ttsServer = TTSServer(port, this) ttsServer = TTSServer(port, this)
state = ServiceState.RUNNING
} }
private fun stopService() { private fun stopService() {
@@ -103,9 +107,14 @@ class ForegroundService : Service() {
stopForeground(true) stopForeground(true)
stopSelf() stopSelf()
} }
state = ServiceState.STOPPED
} }
companion object { 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" private const val NOTIFICATION_CHANNEL_ID = "TTSService.NOTIFICATION_CHANNEL"
const val PORT = "TTSService.PORT" const val PORT = "TTSService.PORT"
const val START = "START" 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
import fi.iki.elonen.NanoHTTPD.Response.Status.* import fi.iki.elonen.NanoHTTPD.Response.Status.*
import io.bartek.R import io.bartek.R
import io.bartek.service.ServiceState
import io.bartek.tts.TTS import io.bartek.tts.TTS
import org.json.JSONObject import org.json.JSONObject
import java.util.* import java.util.*
@@ -72,7 +73,7 @@ class TTSServer(port: Int, private val context: Context) : NanoHTTPD(port),
LocalBroadcastManager LocalBroadcastManager
.getInstance(context) .getInstance(context)
.sendBroadcast(Intent("io.bartek.web.server.CHANGE_STATE").also { .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 LocalBroadcastManager
.getInstance(context) .getInstance(context)
.sendBroadcast(Intent("io.bartek.web.server.CHANGE_STATE").also { .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:layout_height="wrap_content"
android:orientation="horizontal"> 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 <Button
android:id="@+id/run" android:id="@+id/control_server_button"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="2" android:layout_weight="2"
android:onClick="startServer" android:onClick="controlServer"
android:text="@string/main_activity_run" /> 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> </LinearLayout>
<Button <Button

View File

@@ -5,9 +5,6 @@
<string name="service_notification_title">Server is running</string> <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="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_server_port">Server port</string>
<string name="main_activity_run">Run</string> <string name="main_activity_run">Run</string>
<string name="main_activity_stop">Stop</string> <string name="main_activity_stop">Stop</string>