Create debug() function
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.bartlomiejpluta.ttsserver.core.lua.lib
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import com.bartlomiejpluta.R
|
||||
import com.bartlomiejpluta.ttsserver.ui.main.MainActivity
|
||||
import org.luaj.vm2.LuaValue
|
||||
import org.luaj.vm2.lib.OneArgFunction
|
||||
import org.luaj.vm2.lib.TwoArgFunction
|
||||
|
||||
class DebugLibrary(private val context: Context) : TwoArgFunction() {
|
||||
override fun call(modname: LuaValue, env: LuaValue): LuaValue {
|
||||
env.set("debug", DebugFunction(context))
|
||||
|
||||
return LuaValue.NIL
|
||||
}
|
||||
|
||||
class DebugFunction(private val context: Context) : OneArgFunction() {
|
||||
override fun call(arg: LuaValue): LuaValue {
|
||||
LocalBroadcastManager
|
||||
.getInstance(context)
|
||||
.sendBroadcast(Intent(MainActivity.POPUP).apply {
|
||||
putExtra(MainActivity.TITLE, context.resources.getString(R.string.debug))
|
||||
putExtra(MainActivity.MESSAGE, arg.toString())
|
||||
})
|
||||
|
||||
return LuaValue.NIL
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.ttsserver.core.lua.loader
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import com.bartlomiejpluta.R
|
||||
import com.bartlomiejpluta.ttsserver.core.lua.sandbox.SandboxFactory
|
||||
import com.bartlomiejpluta.ttsserver.core.web.endpoint.DefaultEndpoint
|
||||
import com.bartlomiejpluta.ttsserver.core.web.endpoint.Endpoint
|
||||
@@ -44,8 +45,9 @@ class EndpointLoader(
|
||||
|
||||
private fun handleError(exception: LuaError) = LocalBroadcastManager
|
||||
.getInstance(context)
|
||||
.sendBroadcast(Intent(MainActivity.LUA_ERROR).also {
|
||||
it.putExtra(MainActivity.MESSAGE, exception.message)
|
||||
.sendBroadcast(Intent(MainActivity.POPUP).apply {
|
||||
putExtra(MainActivity.TITLE, context.resources.getString(R.string.error))
|
||||
putExtra(MainActivity.MESSAGE, exception.message)
|
||||
})
|
||||
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.luaj.vm2.lib.jse.JseOsLib
|
||||
class SandboxFactory(
|
||||
private val context: Context,
|
||||
private val configLoader: ConfigLoader,
|
||||
private val debugLibrary: DebugLibrary,
|
||||
private val threadLibrary: ThreadLibrary,
|
||||
private val serverLibrary: ServerLibrary,
|
||||
private val httpLibrary: HTTPLibrary,
|
||||
@@ -39,6 +40,7 @@ class SandboxFactory(
|
||||
}
|
||||
|
||||
private fun loadApplicationLibraries(sandbox: Globals) {
|
||||
sandbox.load(debugLibrary)
|
||||
sandbox.load(threadLibrary)
|
||||
sandbox.load(serverLibrary)
|
||||
sandbox.load(httpLibrary)
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.bartlomiejpluta.ttsserver.core.web.worker
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import com.bartlomiejpluta.R
|
||||
import com.bartlomiejpluta.ttsserver.core.web.dto.Request
|
||||
import com.bartlomiejpluta.ttsserver.service.foreground.ForegroundService
|
||||
import com.bartlomiejpluta.ttsserver.service.state.ServiceState
|
||||
@@ -36,8 +37,9 @@ class Worker(
|
||||
private fun handleLuaError(exception: LuaError) {
|
||||
LocalBroadcastManager
|
||||
.getInstance(context)
|
||||
.sendBroadcast(Intent(MainActivity.LUA_ERROR).also {
|
||||
it.putExtra(MainActivity.MESSAGE, exception.message)
|
||||
.sendBroadcast(Intent(MainActivity.POPUP).apply {
|
||||
putExtra(MainActivity.TITLE, context.resources.getString(R.string.error))
|
||||
putExtra(MainActivity.MESSAGE, exception.message)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ class LuaModule {
|
||||
fun sandboxFactory(
|
||||
context: Context,
|
||||
configLoader: ConfigLoader,
|
||||
debugLibrary: DebugLibrary,
|
||||
threadLibrary: ThreadLibrary,
|
||||
serverLibrary: ServerLibrary,
|
||||
httpLibrary: HTTPLibrary,
|
||||
@@ -38,6 +39,7 @@ class LuaModule {
|
||||
) = SandboxFactory(
|
||||
context,
|
||||
configLoader,
|
||||
debugLibrary,
|
||||
threadLibrary,
|
||||
serverLibrary,
|
||||
httpLibrary,
|
||||
@@ -47,7 +49,11 @@ class LuaModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun utilLibrary() = ThreadLibrary()
|
||||
fun debugLibrary(context: Context) = DebugLibrary(context)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun threadLibrary() = ThreadLibrary()
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
|
||||
@@ -41,7 +41,7 @@ class MainActivity : DaggerAppCompatActivity() {
|
||||
private val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) = when (intent?.action) {
|
||||
CHANGE_STATE -> dispatchChangeStateIntent(intent)
|
||||
LUA_ERROR -> dispatchLuaErrorIntent(intent)
|
||||
POPUP -> dispatchLuaErrorIntent(intent)
|
||||
else -> throw UnsupportedOperationException("This action is not supported")
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ class MainActivity : DaggerAppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun dispatchLuaErrorIntent(intent: Intent) = AlertDialog.Builder(this)
|
||||
.setTitle(R.string.error_title)
|
||||
.setTitle(intent.getStringExtra(TITLE))
|
||||
.setMessage(intent.getStringExtra(MESSAGE))
|
||||
.setPositiveButton(android.R.string.ok) { _, _ -> }
|
||||
.create()
|
||||
@@ -103,7 +103,7 @@ class MainActivity : DaggerAppCompatActivity() {
|
||||
super.onResume()
|
||||
val filter = IntentFilter().apply {
|
||||
addAction(CHANGE_STATE)
|
||||
addAction(LUA_ERROR)
|
||||
addAction(POPUP)
|
||||
}
|
||||
|
||||
LocalBroadcastManager
|
||||
@@ -140,9 +140,10 @@ class MainActivity : DaggerAppCompatActivity() {
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val CHANGE_STATE = "com.bartlomiejpluta.ttsserver.service.CHANGE_STATE"
|
||||
const val LUA_ERROR = "com.bartlomiejpluta.ttsserver.service.LUA_ERROR"
|
||||
const val MESSAGE = "message"
|
||||
const val CHANGE_STATE = "com.bartlomiejpluta.ttsserver.ui.main.CHANGE_STATE"
|
||||
const val POPUP = "com.bartlomiejpluta.ttsserver.ui.main.POPUP"
|
||||
const val TITLE = "TITLE"
|
||||
const val MESSAGE = "MESSAGE"
|
||||
const val STATE = "STATE"
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,8 @@
|
||||
<string name="menu_settings">Settings</string>
|
||||
<string name="menu_help">Help</string>
|
||||
|
||||
<string name="error_title">Error</string>
|
||||
<string name="error_invalid_endpoint_script">Skipping %1$s file because of error:\n%2$s</string>
|
||||
|
||||
<string name="error">Error</string>
|
||||
<string name="debug">Debug</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user