Add communication between WebServer and MainActivity
This commit is contained in:
@@ -32,6 +32,7 @@ dependencies {
|
|||||||
implementation 'androidx.core:core-ktx:1.2.0'
|
implementation 'androidx.core:core-ktx:1.2.0'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
||||||
implementation 'org.nanohttpd:nanohttpd:2.2.0'
|
implementation 'org.nanohttpd:nanohttpd:2.2.0'
|
||||||
|
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||||
|
|||||||
@@ -1,21 +1,64 @@
|
|||||||
package io.bartek
|
package io.bartek
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.IntentFilter
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import android.provider.Settings.*
|
import android.provider.Settings.*
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import android.widget.Toast
|
||||||
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||||
import io.bartek.service.ForegroundService
|
import io.bartek.service.ForegroundService
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
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?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
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)
|
fun startServer(view: View) = actionOnService(ForegroundService.START)
|
||||||
@@ -36,4 +79,5 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package io.bartek.web
|
package io.bartek.web
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
import android.speech.tts.TextToSpeech
|
import android.speech.tts.TextToSpeech
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
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
|
||||||
@@ -67,19 +69,19 @@ class TTSServer(port: Int, private val context: Context) : NanoHTTPD(port),
|
|||||||
|
|
||||||
override fun start() {
|
override fun start() {
|
||||||
super.start()
|
super.start()
|
||||||
Toast.makeText(
|
LocalBroadcastManager
|
||||||
context,
|
.getInstance(context)
|
||||||
context.resources.getString(R.string.server_toast_service_started),
|
.sendBroadcast(Intent("io.bartek.web.server.CHANGE_STATE").also {
|
||||||
Toast.LENGTH_SHORT
|
it.putExtra("STATE", "STARTED")
|
||||||
).show()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun stop() {
|
override fun stop() {
|
||||||
super.stop()
|
super.stop()
|
||||||
Toast.makeText(
|
LocalBroadcastManager
|
||||||
context,
|
.getInstance(context)
|
||||||
context.resources.getString(R.string.server_toast_service_stopped),
|
.sendBroadcast(Intent("io.bartek.web.server.CHANGE_STATE").also {
|
||||||
Toast.LENGTH_SHORT
|
it.putExtra("STATE", "STOPPED")
|
||||||
).show()
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user