diff --git a/app/build.gradle b/app/build.gradle index 368ed23..472a02c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,8 +12,8 @@ android { applicationId "top.fumiama.base16384" minSdkVersion 23 targetSdkVersion 30 - versionCode 6 - versionName '1.2' + versionCode 7 + versionName '1.3' resConfigs "zh", "en" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" diff --git a/app/src/main/java/top/fumiama/base16384/MainActivity.kt b/app/src/main/java/top/fumiama/base16384/MainActivity.kt index c1afced..57abd56 100644 --- a/app/src/main/java/top/fumiama/base16384/MainActivity.kt +++ b/app/src/main/java/top/fumiama/base16384/MainActivity.kt @@ -13,23 +13,36 @@ import android.net.Uri import android.os.Build import android.os.Bundle import android.view.View +import android.widget.ArrayAdapter +import android.widget.ListAdapter import android.widget.Toast import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import com.google.android.material.textfield.TextInputEditText import kotlinx.android.synthetic.main.activity_main.* +import top.fumiama.base16384.tools.PropertiesTools import java.io.File import java.io.FileInputStream +import java.nio.charset.Charset class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager + val pCharsets = PropertiesTools(File(filesDir, "charsets.prop")) sv.viewTreeObserver.addOnGlobalLayoutListener { setTitleVisibility() } - fab.setOnClickListener { if(checkReadPermission()) pickFile() } - ben.setOnClickListener { clickButton(true, cm) } - bde.setOnClickListener { clickButton(false, cm) } + fab.setOnClickListener { pickFile() } + ben.setOnClickListener { clickButton(true, cm, pCharsets) } + bde.setOnClickListener { clickButton(false, cm, pCharsets) } + ben.setOnLongClickListener { + callCharsetSelectList(true, pCharsets) + false + } + bde.setOnLongClickListener { + callCharsetSelectList(false, pCharsets) + false + } tti.setOnLongClickListener { AlertDialog.Builder(this).setTitle(R.string.info).setMessage(R.string.info_content).setIcon(R.mipmap.ic_launcher).show() true @@ -46,19 +59,6 @@ class MainActivity : Activity() { } } - override fun onRequestPermissionsResult( - requestCode: Int, - permissions: Array, - grantResults: IntArray - ) { - when (requestCode) { - 1 -> { - if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) pickFile() - else Toast.makeText(this, R.string.permissionDenied, Toast.LENGTH_SHORT).show() - } - } - } - private fun doFromFile(uri: Uri){ val inputFile = generateCacheFile("input") val outputFile = generateCacheFile("output") @@ -112,20 +112,6 @@ class MainActivity : Activity() { startActivityForResult(intent, 2) } - private fun checkReadPermission(): Boolean { - return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N && ContextCompat.checkSelfPermission( - this, - Manifest.permission.READ_EXTERNAL_STORAGE - ) != PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions( - this, - arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), - 1 - ) - false - } else true - } - private fun pickFile() { val i = Intent(Intent.ACTION_GET_CONTENT) i.type = "*/*" @@ -148,17 +134,17 @@ class MainActivity : Activity() { } } - private fun clickButton(isEncode: Boolean, cm:ClipboardManager){ + private fun clickButton(isEncode: Boolean, cm:ClipboardManager, pc: PropertiesTools){ val tin = if(isEncode)ten else tde val tou = if(isEncode)tde else ten tin.text?.let { if(it.isNotEmpty()){ val inputFile = generateCacheFile("input") val outputFile = generateCacheFile("output") - inputFile.writeText(it.toString(), Charsets.UTF_16BE) + inputFile.writeText(it.toString(), getCharset(getCustomCharsetPosition(isEncode, pc))) if(isEncode) encode(inputFile.absolutePath, outputFile.absolutePath) else decode(inputFile.absolutePath, outputFile.absolutePath) - tou.setText(outputFile.readText(Charsets.UTF_16BE)) + tou.setText(outputFile.readText(getCharset(getCustomCharsetPosition(!isEncode, pc)))) copyText(tou, cm) } } @@ -171,6 +157,41 @@ class MainActivity : Activity() { tti.visibility = if(h > r.bottom) View.GONE else View.VISIBLE } + private fun callCharsetSelectList(isEncode: Boolean, pc: PropertiesTools){ + val charsetsArr = resources.getStringArray(R.array.charsets) + AlertDialog.Builder(this) + .setTitle(R.string.select_charset) + .setIcon(R.mipmap.ic_launcher) + .setSingleChoiceItems(ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, charsetsArr), getCustomCharsetPosition(isEncode, pc)){ d, p -> + setCustomCharsetPosition(p, isEncode, pc) + d.cancel() + }.show() + } + + private fun getCustomCharsetPosition(isEncode: Boolean, pc: PropertiesTools): Int{ + val cs = if(isEncode) pc["encode"] else pc["decode"] + return if(cs == "null"){ + if(isEncode) 8 else 3 + }else cs.toInt() + } + + private fun setCustomCharsetPosition(p: Int, isEncode: Boolean, pc: PropertiesTools){ + pc[if(isEncode) "encode" else "decode"] = p.toString() + } + + private fun getCharset(p: Int) = when (p) { + 0 -> Charsets.ISO_8859_1 + 1 -> Charsets.US_ASCII + 2 -> Charsets.UTF_16 + 3 -> Charsets.UTF_16BE + 4 -> Charsets.UTF_16LE + 5 -> Charsets.UTF_32 + 6 -> Charsets.UTF_32BE + 7 -> Charsets.UTF_32LE + 8 -> Charsets.UTF_8 + else -> Charset.defaultCharset() + } + /** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. diff --git a/app/src/main/java/top/fumiama/base16384/tools/PropertiesTools.kt b/app/src/main/java/top/fumiama/base16384/tools/PropertiesTools.kt new file mode 100644 index 0000000..606d80e --- /dev/null +++ b/app/src/main/java/top/fumiama/base16384/tools/PropertiesTools.kt @@ -0,0 +1,53 @@ +package top.fumiama.base16384.tools +//PropertiesTools.kt +//created by fumiama 20200724 +import android.util.Log +import java.io.File +import java.io.InputStream +import java.util.* + +class PropertiesTools(private val f: File):Properties() { + private val propfile:File + get() { + if(!f.exists()) { + if(f.parentFile?.exists() != true) f.parentFile?.mkdirs() + if(f.parentFile?.canWrite() != true) f.parentFile?.setWritable(true) + createNew(f) + }else if(f.isDirectory) { + if(f.parentFile?.canWrite() != true) f.parentFile?.setWritable(true) + f.delete() + createNew(f) + } + if(f.parentFile?.canWrite() != true) f.parentFile?.setWritable(true) + if(f.parentFile?.canRead() != true) f.parentFile?.setReadable(true) + return f + } + private fun createNew(f: File){ + f.createNewFile() + val o = f.outputStream() + this.storeToXML(o, "store") + Log.d("MyPT", "Generate new prop.") + o.close() + } + private fun loadFromXml(`in`: InputStream?): PropertiesTools { + this.loadFromXML(`in`) + return this + } + private fun setProp(key: String?, value: String?): PropertiesTools { + this.setProperty(key, value) + return this + } + operator fun get(key: String): String{ + val i = propfile.inputStream() + val re = this.loadFromXml(i).getProperty(key)?:"null" + Log.d("MyPT", "Get $key = $re") + i.close() + return re + } + operator fun set(key: String, value: String){ + val o = propfile.outputStream() + this.setProp(key, value).storeToXML(o, "store") + Log.d("MyPT", "Set $key = $value") + o.close() + } +} \ No newline at end of file diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index de86fd9..38799f1 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -16,4 +16,5 @@ 关于 作者:源文雨。感谢咲桜动漫社成员的支持。 已复制 + 选择字符集 \ No newline at end of file diff --git a/app/src/main/res/values/charsets.xml b/app/src/main/res/values/charsets.xml new file mode 100644 index 0000000..bdc7e6c --- /dev/null +++ b/app/src/main/res/values/charsets.xml @@ -0,0 +1,14 @@ + + + + ISO_8859_1 + US_ASCII + UTF_16 + UTF_16BE + UTF_16LE + UTF_32 + UTF_32BE + UTF_32LE + UTF_8 + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 751d590..d0526f9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,6 +13,7 @@ Decode Succeed Encode Succeed Info - Author: Fumiama, with precious supports of the members in Sakura Anime Club. + Author: Fumiama, with precious supports from the members in Sakura Anime Club. Copied Text + Select Charset \ No newline at end of file