Add communication between WebServer and MainActivity

This commit is contained in:
2020-05-07 22:54:45 +02:00
parent 4458bf66c2
commit 4375eacf47
3 changed files with 58 additions and 11 deletions

View File

@@ -32,6 +32,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'org.nanohttpd:nanohttpd:2.2.0'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

View File

@@ -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() {
}
}
}

View File

@@ -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")
})
}
}