diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..cecde39 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +SimpleDict \ No newline at end of file diff --git a/.idea/dictionaries/rumia.xml b/.idea/dictionaries/rumia.xml new file mode 100644 index 0000000..1a0979a --- /dev/null +++ b/.idea/dictionaries/rumia.xml @@ -0,0 +1,7 @@ + + + + spwd + + + \ No newline at end of file diff --git a/.idea/markdown-navigator-enh.xml b/.idea/markdown-navigator-enh.xml new file mode 100644 index 0000000..a8fcc84 --- /dev/null +++ b/.idea/markdown-navigator-enh.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator.xml b/.idea/markdown-navigator.xml new file mode 100644 index 0000000..a2fc086 --- /dev/null +++ b/.idea/markdown-navigator.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index e6e473d..48d2b9e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,8 +12,8 @@ android { applicationId "top.fumiama.simpledict" minSdkVersion 26 targetSdkVersion 30 - versionCode 13 - versionName '1.9.2' + versionCode 14 + versionName '2.0' resConfigs "zh", "zh-rCN" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/java/top/fumiama/simpledict/Client.kt b/app/src/main/java/top/fumiama/simpledict/Client.kt index 72655d5..9f8085c 100644 --- a/app/src/main/java/top/fumiama/simpledict/Client.kt +++ b/app/src/main/java/top/fumiama/simpledict/Client.kt @@ -62,7 +62,7 @@ class Client(private val ip: String, private val port: Int) { return false } - fun receiveRawMessage(totalSize: Int = -1, bufferSize: Int = 4096) : ByteArray { + fun receiveRawMessage(totalSize: Int = -1, bufferSize: Int = 1048576) : ByteArray { var re = byteArrayOf() try { if (isConnect) { @@ -72,8 +72,9 @@ class Client(private val ip: String, private val port: Int) { do { a = din?.read(inMessage)?:0 //a存储返回消息的长度 re += inMessage.copyOf(a) - Log.d("MyC", "reply length:$a: ${re.decodeToString()}") - } while (a == bufferSize || totalSize > re.size) + Log.d("MyC", "reply length:$a") + if(totalSize < 0 && a < bufferSize) break + } while (totalSize > re.size) } else Log.d("MyC", "no connect to receive message") } catch (e: IOException) { Log.d("MyC", "receive message failed") diff --git a/app/src/main/java/top/fumiama/simpledict/MainActivity.kt b/app/src/main/java/top/fumiama/simpledict/MainActivity.kt index 78be7a5..7e89dfa 100644 --- a/app/src/main/java/top/fumiama/simpledict/MainActivity.kt +++ b/app/src/main/java/top/fumiama/simpledict/MainActivity.kt @@ -37,6 +37,7 @@ class MainActivity : AppCompatActivity() { private var host = "127.0.0.1" private var port = 80 private var pwd = "demo" + private var spwd: String? = null private var dict: SimpleDict? = null private var hasLiked = false private var cm: ClipboardManager? = null @@ -52,8 +53,9 @@ class MainActivity : AppCompatActivity() { if(contains("host")) getString("host", host)?.apply { host = this } if(contains("port")) getInt("port", port).apply { port = this } if(contains("pwd")) getString("pwd", pwd)?.apply { pwd = this } + if(contains("spwd")) getString("spwd", spwd)?.apply { spwd = this } } - dict = SimpleDict(Client(host, port), pwd) + dict = SimpleDict(Client(host, port), pwd, spwd) ad = LikeViewHolder(ffr).RecyclerViewAdapter() cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager ffr.apply { @@ -110,7 +112,7 @@ class MainActivity : AppCompatActivity() { override fun onQueryTextSubmit(query: CharSequence): Boolean { if(query.isNotEmpty()) { - val key = query.toString() + val key = query.toString().trim().replace(Regex("[\\uFF00-\\uFF5E]")) { (it.value[0] - 0xFEE0).toString() } val data = dict?.get(key) showDictAlert(key, data, recyclerView.children.toList().let { val i = it.map { it.ta.text }.indexOf(key) @@ -135,17 +137,23 @@ class MainActivity : AppCompatActivity() { val h = info.substringBefore(':') val l = info.substringAfter(':') val p = l.substringBefore('_').toInt() - val w = l.substringAfter('_') + var w = l.substringAfter('_') if (h != "" && p > 0 && p < 65536 && w != "") { getSharedPreferences("remote", MODE_PRIVATE)?.edit { putString("host", h) putInt("port", p) + if(w.contains('^')) { + val s = w.substringAfterLast('^') + if (s != "") { + putString("spwd", s) + w = w.substringBeforeLast('^') + } + } putString("pwd", w) apply() Toast.makeText(this@MainActivity, "下次生效", Toast.LENGTH_SHORT).show() return@setPositiveButton - } - throw FileNotFoundException("getSharedPreferences named \"remote\" error.") + }?:throw FileNotFoundException("getSharedPreferences named \"remote\" error.") } else throw IllegalArgumentException() } catch (e: Exception) { e.printStackTrace() @@ -233,11 +241,14 @@ class MainActivity : AppCompatActivity() { .setTitle("$hintAdd$key") .setView(t) .setPositiveButton(android.R.string.ok) { _, _ -> - val newText = t.diet.text.toString() + val newText = t.diet.text.toString().trim().replace(Regex("[\\uFF00-\\uFF5E]")) { (it.value[0] - 0xFEE0).toString() } if (t.diet.text.isNotEmpty() && newText != data) Thread { - dict?.set(key, newText) - line?.tb?.text = newText - updateSize() + if(dict?.set(key, newText) == true) { + line?.tb?.text = newText + updateSize() + } else runOnUiThread { + Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show() + } }.start() else Toast.makeText(this, "未更改", Toast.LENGTH_SHORT).show() } @@ -246,8 +257,7 @@ class MainActivity : AppCompatActivity() { } .setNeutralButton("删除") { _, _ -> Thread{ - dict?.minusAssign(key) - line?.apply { + if(dict?.del(key) == true) line?.apply { val delKey = SpannableString(key) val delData = SpannableString(data) delKey.setSpan(StrikethroughSpan(), 0, key.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) @@ -257,6 +267,9 @@ class MainActivity : AppCompatActivity() { tb.text = delData updateSize() } + else runOnUiThread { + Toast.makeText(this, "失败", Toast.LENGTH_SHORT).show() + } }.start() } .setNegativeButton(android.R.string.cancel) { _, _ -> } diff --git a/app/src/main/java/top/fumiama/simpledict/SimpleDict.kt b/app/src/main/java/top/fumiama/simpledict/SimpleDict.kt index 90d1976..8734949 100644 --- a/app/src/main/java/top/fumiama/simpledict/SimpleDict.kt +++ b/app/src/main/java/top/fumiama/simpledict/SimpleDict.kt @@ -3,7 +3,7 @@ package top.fumiama.simpledict import android.util.Log import java.lang.Thread.sleep -class SimpleDict(private val client: Client, private val pwd: String) { //must run in thread +class SimpleDict(private val client: Client, private val pwd: String, private val spwd: String?) { //must run in thread private var dict = HashMap() val size get() = dict.size val keys get() = dict.keys @@ -92,39 +92,45 @@ class SimpleDict(private val client: Client, private val pwd: String) { //must doCommon?.let { it() } } - operator fun minusAssign(key: String) { - if(initDict()) { - sendMessageWithDelay("del") + fun del(key: String): Boolean { + if(spwd == null) return false + else if(initDict()) { + sendMessageWithDelay("del$spwd") client.receiveMessage() sendMessageWithDelay(key) - client.receiveMessage() - if(closeDict()) { - dict.remove(key) - val end = latestKeys.size-1 - if(end > 0) latestKeys = latestKeys.let { oldArr -> - var index = -1 - Array(end) { - if(oldArr[it] == key) index = it - return@Array if(index < 0 || (index > 0 && it < index)) oldArr[it] else oldArr[it+1] + if(client.receiveMessage() == "succ") { + if(closeDict()) { + dict.remove(key) + val end = latestKeys.size-1 + if(end > 0) latestKeys = latestKeys.let { oldArr -> + var index = -1 + Array(end) { + if(oldArr[it] == key) index = it + return@Array if(index < 0 || (index > 0 && it < index)) oldArr[it] else oldArr[it+1] + } } + return true } - } + } else closeDict() } + return false } operator fun get(key: String) = dict[key] - operator fun set(key: String, value: String): String? { - val p = dict[key] - if(initDict()) { - sendMessageWithDelay("set") + fun set(key: String, value: String): Boolean { + if(spwd == null) return false + else if(initDict()) { + sendMessageWithDelay("set$spwd") client.receiveMessage() sendMessageWithDelay(key) - client.receiveMessage() - sendMessageWithDelay(value) - client.receiveMessage() - if(closeDict()) dict[key] = value + if(client.receiveMessage() == "data") { + sendMessageWithDelay(value) + client.receiveMessage() + if(closeDict()) dict[key] = value + return true + } else closeDict() } - return p + return false } } \ No newline at end of file