1
0
mirror of https://github.com/fumiama/simple-dict-android.git synced 2026-06-05 00:30:24 +08:00
1. 增加设置/删除鉴权
2. 设置时自动去除多余空格并转换为半角
This commit is contained in:
fumiama
2021-05-04 00:08:31 +08:00
parent 516727ee7e
commit 8fad5d2ab7
8 changed files with 139 additions and 39 deletions

1
.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
SimpleDict

7
.idea/dictionaries/rumia.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="rumia">
<words>
<w>spwd</w>
</words>
</dictionary>
</component>

10
.idea/markdown-navigator-enh.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MarkdownEnhProjectSettings">
<AnnotatorSettings targetHasSpaces="true" linkCaseMismatch="true" wikiCaseMismatch="true" wikiLinkHasDashes="true" notUnderWikiHome="true" targetNotWikiPageExt="true" notUnderSourceWikiHome="true" targetNameHasAnchor="true" targetPathHasAnchor="true" wikiLinkHasSlash="true" wikiLinkHasSubdir="true" wikiLinkHasOnlyAnchor="true" linkTargetsWikiHasExt="true" linkTargetsWikiHasBadExt="true" notUnderSameRepo="true" targetNotUnderVcs="false" linkNeedsExt="true" linkHasBadExt="true" linkTargetNeedsExt="true" linkTargetHasBadExt="true" wikiLinkNotInWiki="true" imageTargetNotInRaw="true" repoRelativeAcrossVcsRoots="true" multipleWikiTargetsMatch="true" unresolvedLinkReference="true" linkIsIgnored="true" anchorIsIgnored="true" anchorIsUnresolved="true" anchorLineReferenceIsUnresolved="true" anchorLineReferenceFormat="true" anchorHasDuplicates="true" abbreviationDuplicates="true" abbreviationNotUsed="true" attributeIdDuplicateDefinition="true" attributeIdNotUsed="true" footnoteDuplicateDefinition="true" footnoteUnresolved="true" footnoteDuplicates="true" footnoteNotUsed="true" macroDuplicateDefinition="true" macroUnresolved="true" macroDuplicates="true" macroNotUsed="true" referenceDuplicateDefinition="true" referenceUnresolved="true" referenceDuplicates="true" referenceNotUsed="true" referenceUnresolvedNumericId="true" enumRefDuplicateDefinition="true" enumRefUnresolved="true" enumRefDuplicates="true" enumRefNotUsed="true" enumRefLinkUnresolved="true" enumRefLinkDuplicates="true" simTocUpdateNeeded="true" simTocTitleSpaceNeeded="true" />
<HtmlExportSettings updateOnSave="false" parentDir="" targetDir="" cssDir="css" scriptDir="js" plainHtml="false" imageDir="" copyLinkedImages="false" imagePathType="0" targetPathType="2" targetExt="" useTargetExt="false" noCssNoScripts="false" useElementStyleAttribute="false" linkToExportedHtml="true" exportOnSettingsChange="true" regenerateOnProjectOpen="false" linkFormatType="HTTP_ABSOLUTE" />
<LinkMapSettings>
<textMaps />
</LinkMapSettings>
</component>
</project>

62
.idea/markdown-navigator.xml generated Normal file
View File

@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MarkdownProjectSettings">
<PreviewSettings splitEditorLayout="SPLIT" splitEditorPreview="PREVIEW" useGrayscaleRendering="false" zoomFactor="1.0" maxImageWidth="0" synchronizePreviewPosition="true" highlightPreviewType="LINE" highlightFadeOut="5" highlightOnTyping="true" synchronizeSourcePosition="true" verticallyAlignSourceAndPreviewSyncPosition="true" showSearchHighlightsInPreview="true" showSelectionInPreview="true" lastLayoutSetsDefault="false">
<PanelProvider>
<provider providerId="com.vladsch.md.nav.editor.swing.html.panel" providerName="Default - Swing" />
</PanelProvider>
</PreviewSettings>
<ParserSettings gitHubSyntaxChange="false" correctedInvalidSettings="false" emojiShortcuts="1" emojiImages="0">
<PegdownExtensions>
<option name="ATXHEADERSPACE" value="true" />
<option name="FENCED_CODE_BLOCKS" value="true" />
<option name="INTELLIJ_DUMMY_IDENTIFIER" value="true" />
<option name="RELAXEDHRULES" value="true" />
<option name="STRIKETHROUGH" value="true" />
<option name="TABLES" value="true" />
<option name="TASKLISTITEMS" value="true" />
</PegdownExtensions>
<ParserOptions>
<option name="COMMONMARK_LISTS" value="true" />
<option name="EMOJI_SHORTCUTS" value="true" />
<option name="GFM_TABLE_RENDERING" value="true" />
<option name="PRODUCTION_SPEC_PARSER" value="true" />
<option name="SIM_TOC_BLANK_LINE_SPACER" value="true" />
</ParserOptions>
</ParserSettings>
<HtmlSettings headerTopEnabled="false" headerBottomEnabled="false" bodyTopEnabled="false" bodyBottomEnabled="false" addPageHeader="false" addAnchorLinks="false" anchorLinksWrapText="false" imageUriSerials="false" addDocTypeHtml="true" noParaTags="false" defaultUrlTitle="false" migratedPlantUml="true" migratedAnchorLinks="true" plantUmlConversion="0">
<GeneratorProvider>
<provider providerId="com.vladsch.md.nav.editor.text.html.generator" providerName="Unmodified HTML Generator" />
</GeneratorProvider>
<headerTop />
<headerBottom />
<bodyTop />
<bodyBottom />
<fencedCodeConversions>
<option name="c4plantuml" value="NONE" />
<option name="ditaa" value="NONE" />
<option name="erd" value="NONE" />
<option name="graphviz" value="NONE" />
<option name="latex" value="KATEX" />
<option name="math" value="KATEX" />
<option name="mermaid" value="NONE" />
<option name="nomnoml" value="NONE" />
<option name="plantuml" value="NONE" />
<option name="puml" value="NONE" />
<option name="svgbob" value="NONE" />
<option name="umlet" value="NONE" />
<option name="vega" value="NONE" />
<option name="vegalite" value="NONE" />
<option name="wavedrom" value="NONE" />
</fencedCodeConversions>
</HtmlSettings>
<CssSettings previewScheme="UI_SCHEME" cssUri="" isCssUriEnabled="false" isCssUriSerial="true" isCssTextEnabled="false" isDynamicPageWidth="true">
<StylesheetProvider>
<provider providerId="com.vladsch.md.nav.editor.text.html.css" providerName="No Stylesheet" />
</StylesheetProvider>
<ScriptProviders />
<cssText />
<cssUriHistory />
</CssSettings>
</component>
</project>

View File

@@ -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"

View File

@@ -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")

View File

@@ -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) { _, _ -> }

View File

@@ -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
}
}