mirror of
https://github.com/fumiama/simple-dict-android.git
synced 2026-06-08 12:00:35 +08:00
v2.0
1. 增加设置/删除鉴权 2. 设置时自动去除多余空格并转换为半角
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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) { _, _ -> }
|
||||
|
||||
@@ -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<String, String?>()
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user