mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-05-31 11:06:29 +00:00
switch to the new API (does not work) (#317)
* switch to the new API (does not work) * kind of works without parsing JSON
This commit is contained in:
committed by
GitHub
parent
241d02584a
commit
12b4325435
+4
-4
@@ -6,12 +6,12 @@
|
||||
<type value="RUNNING_DEVICE_TARGET" />
|
||||
<deviceKey>
|
||||
<Key>
|
||||
<type value="VIRTUAL_DEVICE_PATH" />
|
||||
<value value="$USER_HOME$/.android/avd/Pixel_4a_API_32.avd" />
|
||||
<type value="SERIAL_NUMBER" />
|
||||
<value value="1C231FDF600FYG" />
|
||||
</Key>
|
||||
</deviceKey>
|
||||
</Target>
|
||||
</targetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2022-02-15T15:32:14.669079Z" />
|
||||
</runningDeviceTargetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2022-02-16T16:12:41.610721Z" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -22,42 +22,20 @@ Java_chat_simplex_app_SimplexAppKt_initHS(__unused JNIEnv *env, __unused jclass
|
||||
}
|
||||
|
||||
// from simplex-chat
|
||||
typedef void* chat_store;
|
||||
typedef void* controller;
|
||||
typedef void* chat_ctrl;
|
||||
|
||||
extern chat_store chat_init_store(const char * path);
|
||||
extern char *chat_get_user(chat_store store);
|
||||
extern char *chat_create_user(chat_store store, const char *data);
|
||||
extern controller chat_start(chat_store store);
|
||||
extern char *chat_send_cmd(controller ctl, const char *cmd);
|
||||
extern char *chat_recv_msg(controller ctl);
|
||||
extern chat_ctrl chat_init(const char * path);
|
||||
extern char *chat_send_cmd(chat_ctrl ctrl, const char *cmd);
|
||||
extern char *chat_recv_msg(chat_ctrl ctrl);
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_chat_simplex_app_SimplexAppKt_chatInit(JNIEnv *env, __unused jclass clazz, jstring datadir) {
|
||||
const char *_data = (*env)->GetStringUTFChars(env, datadir, JNI_FALSE);
|
||||
jlong res = (jlong)chat_init_store(_data);
|
||||
jlong res = (jlong)chat_init(_data);
|
||||
(*env)->ReleaseStringUTFChars(env, datadir, _data);
|
||||
return res;
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_chat_simplex_app_SimplexAppKt_chatGetUser(JNIEnv *env, __unused jclass clazz, jlong controller) {
|
||||
return (*env)->NewStringUTF(env, chat_get_user((void*)controller));
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_chat_simplex_app_SimplexAppKt_chatCreateUser(JNIEnv *env, __unused jclass clazz, jlong controller, jstring data) {
|
||||
const char *_data = (*env)->GetStringUTFChars(env, data, JNI_FALSE);
|
||||
jstring res = (*env)->NewStringUTF(env, chat_create_user((void*)controller, _data));
|
||||
(*env)->ReleaseStringUTFChars(env, data, _data);
|
||||
return res;
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_chat_simplex_app_SimplexAppKt_chatStart(JNIEnv *env, jclass clazz, jlong controller) {
|
||||
return (jlong)chat_start((void*)controller);
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_chat_simplex_app_SimplexAppKt_chatSendCmd(JNIEnv *env, __unused jclass clazz, jlong controller, jstring msg) {
|
||||
const char *_msg = (*env)->GetStringUTFChars(env, msg, JNI_FALSE);
|
||||
|
||||
@@ -3,8 +3,7 @@ package chat.simplex.app
|
||||
import android.app.Application
|
||||
import android.net.LocalServerSocket
|
||||
import android.util.Log
|
||||
import chat.simplex.app.model.ChatController
|
||||
import chat.simplex.app.model.ChatModel
|
||||
import chat.simplex.app.model.*
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.lang.ref.WeakReference
|
||||
@@ -18,29 +17,29 @@ external fun initHS()
|
||||
external fun pipeStdOutToSocket(socketName: String) : Int
|
||||
|
||||
// SimpleX API
|
||||
typealias Controller = Long
|
||||
typealias Store = Long
|
||||
external fun chatInit(filesDir: String): Store
|
||||
external fun chatGetUser(controller: Store) : String
|
||||
external fun chatCreateUser(controller: Store, data: String) : String
|
||||
external fun chatStart(controller: Store) : Controller
|
||||
external fun chatSendCmd(controller: Controller, msg: String) : String
|
||||
external fun chatRecvMsg(controller: Controller) : String
|
||||
typealias ChatCtrl = Long
|
||||
external fun chatInit(path: String): ChatCtrl
|
||||
external fun chatSendCmd(ctrl: ChatCtrl, msg: String) : String
|
||||
external fun chatRecvMsg(ctrl: ChatCtrl) : String
|
||||
|
||||
class SimplexApp: Application() {
|
||||
private lateinit var controller: ChatController
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
val store: Store = chatInit(applicationContext.filesDir.toString())
|
||||
// create user if needed
|
||||
if (chatGetUser(store) == "{}") {
|
||||
chatCreateUser(store, """
|
||||
{"displayName": "test", "fullName": "android test"}
|
||||
""".trimIndent())
|
||||
controller = ChatController(chatInit(applicationContext.filesDir.toString()))
|
||||
var user = controller.apiGetActiveUser()
|
||||
if (user == null) {
|
||||
// user = controller.apiCreateActiveUser(Profile("android", "Android test"))
|
||||
}
|
||||
Log.d("SIMPLEX (user)", user.toString())
|
||||
try {
|
||||
controller.apiStartChat()
|
||||
Log.d("SIMPLEX", "started chat")
|
||||
} catch(e: Error) {
|
||||
Log.d("SIMPLEX", "failed starting chat $e")
|
||||
// throw e
|
||||
}
|
||||
Log.d("SIMPLEX (user)", chatGetUser(store))
|
||||
controller = ChatController(chatStart(store))
|
||||
}
|
||||
|
||||
val chatModel by lazy {
|
||||
|
||||
@@ -14,7 +14,7 @@ import kotlin.concurrent.thread
|
||||
typealias Controller = Long
|
||||
|
||||
open class ChatController(val ctrl: Controller) {
|
||||
private lateinit var chatModel: ChatModel
|
||||
private var chatModel: ChatModel? = null
|
||||
|
||||
fun setModel(m: ChatModel) {
|
||||
chatModel = m
|
||||
@@ -26,16 +26,35 @@ open class ChatController(val ctrl: Controller) {
|
||||
while(true) {
|
||||
val json = chatRecvMsg(ctrl)
|
||||
Log.d("SIMPLEX chatRecvMsg: ", json)
|
||||
chatModel.terminalItems.add(TerminalItem.Resp(APIResponse.decodeStr(json)))
|
||||
chatModel?.terminalItems?.add(TerminalItem.Resp(APIResponse.decodeStr(json)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun sendCmd(cmd: String) {
|
||||
val json = chatSendCmd(ctrl, cmd)
|
||||
Log.d("SIMPLEX chatSendCmd: ", cmd)
|
||||
Log.d("SIMPLEX chatSendCmd response: ", json)
|
||||
chatModel.terminalItems.add(TerminalItem.Resp(APIResponse.decodeStr(json)))
|
||||
fun sendCmd(cmd: CC): CR {
|
||||
val c = cmd.cmdString
|
||||
val json = chatSendCmd(ctrl, c)
|
||||
Log.d("SIMPLEX", "sendCmd: $c")
|
||||
Log.d("SIMPLEX", "sendCmd response $json")
|
||||
chatModel?.terminalItems?.add(TerminalItem.Resp(APIResponse.decodeStr(json)))
|
||||
return APIResponse.decodeStr(json)
|
||||
}
|
||||
|
||||
fun apiGetActiveUser(): User? {
|
||||
val r = sendCmd(CC.ShowActiveUser())
|
||||
return if (r is CR.ActiveUser) r.user else null
|
||||
}
|
||||
|
||||
fun apiCreateActiveUser(p: Profile): User {
|
||||
val r = sendCmd(CC.CreateActiveUser(p))
|
||||
if (r is CR.ActiveUser) return r.user
|
||||
throw Error("failed creating user: ${r.toString()}")
|
||||
}
|
||||
|
||||
fun apiStartChat() {
|
||||
val r = sendCmd(CC.StartChat())
|
||||
if (r is CR.ChatStarted) return
|
||||
throw Error("failed starting chat: ${r.toString()}")
|
||||
}
|
||||
|
||||
class Mock: ChatController(0) {}
|
||||
@@ -112,6 +131,13 @@ sealed class CR {
|
||||
override val details get() = user.toString()
|
||||
}
|
||||
|
||||
@Serializable
|
||||
@SerialName("chatStarted")
|
||||
class ChatStarted: CR() {
|
||||
override val responseType get() = "chatStarted"
|
||||
override val details get() = CR.noDetails(this)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
@SerialName("contactSubscribed")
|
||||
class ContactSubscribed(val contact: Contact): CR() {
|
||||
@@ -121,7 +147,7 @@ sealed class CR {
|
||||
|
||||
@Serializable
|
||||
class Response(val type: String, val json: String): CR() {
|
||||
override val responseType get() = "* ${type}"
|
||||
override val responseType get() = "* $type"
|
||||
override val details get() = json
|
||||
}
|
||||
|
||||
@@ -131,10 +157,9 @@ sealed class CR {
|
||||
override val details get() = str
|
||||
}
|
||||
|
||||
// {"resp": {"activeUser": {"user": {<user>}}}}
|
||||
// {"resp": {"anythingElse": <json> }} -> Unknown(type = "anythingElse", json = "<the whole thing including resp>")
|
||||
|
||||
// {"type": "activeUser", "user": <user>}
|
||||
companion object {
|
||||
fun noDetails(r: CR): String ="${r.responseType}: no details"
|
||||
}
|
||||
}
|
||||
|
||||
abstract class TerminalItem {
|
||||
|
||||
@@ -6,6 +6,7 @@ import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import chat.simplex.app.model.CC
|
||||
import chat.simplex.app.model.ChatModel
|
||||
import chat.simplex.app.model.TerminalItem
|
||||
import chat.simplex.app.ui.theme.SimpleXTheme
|
||||
@@ -16,7 +17,9 @@ import chat.simplex.app.views.chat.SendMsgView
|
||||
fun TerminalView(chatModel: ChatModel) {
|
||||
Column {
|
||||
TerminalLog(chatModel.terminalItems)
|
||||
SendMsgView(chatModel.controller::sendCmd)
|
||||
SendMsgView(sendMessage = { cmd ->
|
||||
chatModel.controller.sendCmd(CC.Console(cmd))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user