Add communication between WebServer and MainActivity
This commit is contained in:
@@ -1,21 +1,64 @@
|
||||
package io.bartek
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
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 androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import io.bartek.service.ForegroundService
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
private val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
intent?.let {
|
||||
when(intent.getStringExtra("STATE")) {
|
||||
"STARTED" -> notifyOnStart()
|
||||
"STOPPED" -> notifyOnStop()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun notifyOnStart() {
|
||||
Toast.makeText(
|
||||
this,
|
||||
resources.getString(R.string.server_toast_service_started),
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
LocalBroadcastManager
|
||||
.getInstance(this)
|
||||
.registerReceiver(receiver, IntentFilter("io.bartek.web.server.CHANGE_STATE"))
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
LocalBroadcastManager
|
||||
.getInstance(this)
|
||||
.unregisterReceiver(receiver)
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
fun startServer(view: View) = actionOnService(ForegroundService.START)
|
||||
@@ -36,4 +79,5 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package io.bartek.web
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.speech.tts.TextToSpeech
|
||||
import android.widget.Toast
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import fi.iki.elonen.NanoHTTPD
|
||||
import fi.iki.elonen.NanoHTTPD.Response.Status.*
|
||||
import io.bartek.R
|
||||
@@ -46,7 +48,7 @@ class TTSServer(port: Int, private val context: Context) : NanoHTTPD(port),
|
||||
throw ResponseException(METHOD_NOT_ALLOWED, "")
|
||||
}
|
||||
|
||||
if (session.headers["content-type"] ?. let { it != "application/json" } != false) {
|
||||
if (session.headers["content-type"]?.let { it != "application/json" } != false) {
|
||||
throw ResponseException(BAD_REQUEST, "")
|
||||
}
|
||||
|
||||
@@ -67,19 +69,19 @@ class TTSServer(port: Int, private val context: Context) : NanoHTTPD(port),
|
||||
|
||||
override fun start() {
|
||||
super.start()
|
||||
Toast.makeText(
|
||||
context,
|
||||
context.resources.getString(R.string.server_toast_service_started),
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
LocalBroadcastManager
|
||||
.getInstance(context)
|
||||
.sendBroadcast(Intent("io.bartek.web.server.CHANGE_STATE").also {
|
||||
it.putExtra("STATE", "STARTED")
|
||||
})
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
super.stop()
|
||||
Toast.makeText(
|
||||
context,
|
||||
context.resources.getString(R.string.server_toast_service_stopped),
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
LocalBroadcastManager
|
||||
.getInstance(context)
|
||||
.sendBroadcast(Intent("io.bartek.web.server.CHANGE_STATE").also {
|
||||
it.putExtra("STATE", "STOPPED")
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user