feat: 优化代码

pull/105/head
kunfei 5 years ago
parent 6d07fa110a
commit 440ad903d8
  1. 2
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  2. 2
      app/src/main/java/io/legado/app/ui/qrcode/QrCodeActivity.kt
  3. 30
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt
  4. 7
      app/src/main/java/io/legado/app/utils/DocumentUtils.kt
  5. 2
      app/src/main/java/io/legado/app/utils/FileUtils.kt
  6. 8
      app/src/main/java/io/legado/app/utils/UriExtensions.kt

@ -193,7 +193,7 @@ class BgTextConfigDialog : DialogFragment() {
) )
.rationale(R.string.bg_image_per) .rationale(R.string.bg_image_per)
.onGranted { .onGranted {
FileUtils.getPath(requireContext(), uri)?.let { path -> FileUtils.getRealPath(requireContext(), uri)?.let { path ->
ReadBookConfig.getConfig().setBg(2, path) ReadBookConfig.getConfig().setBg(2, path)
ReadBookConfig.upBg() ReadBookConfig.upBg()
postEvent(EventBus.UP_CONFIG, false) postEvent(EventBus.UP_CONFIG, false)

@ -98,7 +98,7 @@ class QrCodeActivity : BaseActivity(R.layout.activity_qrcode_capture), QRCodeVie
zxingview.startSpotAndShowRect() // 显示扫描框,并开始识别 zxingview.startSpotAndShowRect() // 显示扫描框,并开始识别
if (resultCode == Activity.RESULT_OK && requestCode == requestQrImage) { if (resultCode == Activity.RESULT_OK && requestCode == requestQrImage) {
val picturePath = FileUtils.getPath(this, it) val picturePath = FileUtils.getRealPath(this, it)
// 本来就用到 QRCodeView 时可直接调 QRCodeView 的方法,走通用的回调 // 本来就用到 QRCodeView 时可直接调 QRCodeView 的方法,走通用的回调
zxingview.decodeQRCode(picturePath) zxingview.decodeQRCode(picturePath)
} }

@ -38,8 +38,9 @@ class FontSelectDialog : DialogFragment(),
private val fontFolderRequestCode = 35485 private val fontFolderRequestCode = 35485
private val fontFolder = private val fontFolder =
App.INSTANCE.filesDir.absolutePath + File.separator + "Fonts" + File.separator App.INSTANCE.filesDir.absolutePath + File.separator + "Fonts" + File.separator
private val fontCacheFolder = private val fontCacheFolder by lazy {
App.INSTANCE.cacheDir.absolutePath + File.separator + "Fonts" + File.separator FileUtils.createFolderIfNotExist(App.INSTANCE.cacheDir, "Fonts")
}
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext
get() = job + Main get() = job + Main
private var adapter: FontAdapter? = null private var adapter: FontAdapter? = null
@ -122,18 +123,31 @@ class FontSelectDialog : DialogFragment(),
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
private fun getFontFiles(uri: Uri) { private fun getFontFiles(uri: Uri) {
launch(IO) { launch(IO) {
FileUtils.deleteFile(fontCacheFolder) val docItems = DocumentUtils.listFiles(App.INSTANCE, uri)
DocumentUtils.listFiles(App.INSTANCE, uri).forEach { item -> fontCacheFolder.listFiles()?.forEach { fontFile ->
var contain = false
for (item in docItems) {
if (fontFile.name == item.name) {
contain = true
break
}
}
if (!contain) {
fontFile.delete()
}
}
docItems.forEach { item ->
if (item.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())) { if (item.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())) {
val fontFile = FileUtils.getFile(fontCacheFolder, item.name)
if (!fontFile.exists()) {
DocumentUtils.readBytes(App.INSTANCE, item.uri)?.let { byteArray -> DocumentUtils.readBytes(App.INSTANCE, item.uri)?.let { byteArray ->
FileUtils.createFileIfNotExist(fontCacheFolder + item.name) fontFile.writeBytes(byteArray)
.writeBytes(byteArray) }
} }
} }
} }
try { try {
val file = File(fontCacheFolder) fontCacheFolder.listFiles { pathName ->
file.listFiles { pathName ->
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())
}?.let { }?.let {
withContext(Main) { withContext(Main) {

@ -85,12 +85,12 @@ object DocumentUtils {
fun listFiles(context: Context, uri: Uri): ArrayList<DocItem> { fun listFiles(context: Context, uri: Uri): ArrayList<DocItem> {
val docList = arrayListOf<DocItem>() val docList = arrayListOf<DocItem>()
var c: Cursor? = null
try {
val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree( val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(
uri, uri,
DocumentsContract.getDocumentId(uri) DocumentsContract.getDocumentId(uri)
) )
var c: Cursor? = null
try {
c = context.contentResolver.query( c = context.contentResolver.query(
childrenUri, arrayOf( childrenUri, arrayOf(
DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DOCUMENT_ID,
@ -118,7 +118,8 @@ object DocumentUtils {
docList.add(item) docList.add(item)
} while (c.moveToNext()) } while (c.moveToNext())
} }
} catch (e: java.lang.Exception) { } catch (e: Exception) {
e.printStackTrace()
} finally { } finally {
c?.close() c?.close()
} }

@ -105,7 +105,7 @@ object FileUtils {
return sdCardDirectory return sdCardDirectory
} }
fun getPath(context: Context, uri: Uri): String? { fun getRealPath(context: Context, uri: Uri): String? {
// DocumentProvider // DocumentProvider
if (DocumentsContract.isDocumentUri(context, uri)) { if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider // ExternalStorageProvider

@ -10,7 +10,7 @@ fun Uri.readBytes(context: Context): ByteArray? {
if (DocumentFile.isDocumentUri(context, this)) { if (DocumentFile.isDocumentUri(context, this)) {
return DocumentUtils.readBytes(context, this) return DocumentUtils.readBytes(context, this)
} else { } else {
val path = FileUtils.getPath(context, this) val path = FileUtils.getRealPath(context, this)
if (path?.isNotEmpty() == true) { if (path?.isNotEmpty() == true) {
return File(path).readBytes() return File(path).readBytes()
} }
@ -23,7 +23,7 @@ fun Uri.readText(context: Context): String? {
if (DocumentFile.isDocumentUri(context, this)) { if (DocumentFile.isDocumentUri(context, this)) {
return DocumentUtils.readText(context, this) return DocumentUtils.readText(context, this)
} else { } else {
val path = FileUtils.getPath(context, this) val path = FileUtils.getRealPath(context, this)
if (path?.isNotEmpty() == true) { if (path?.isNotEmpty() == true) {
return File(path).readText() return File(path).readText()
} }
@ -36,7 +36,7 @@ fun Uri.writeBytes(context: Context, byteArray: ByteArray): Boolean {
if (DocumentFile.isDocumentUri(context, this)) { if (DocumentFile.isDocumentUri(context, this)) {
return DocumentUtils.writeBytes(context, byteArray, this) return DocumentUtils.writeBytes(context, byteArray, this)
} else { } else {
val path = FileUtils.getPath(context, this) val path = FileUtils.getRealPath(context, this)
if (path?.isNotEmpty() == true) { if (path?.isNotEmpty() == true) {
File(path).writeBytes(byteArray) File(path).writeBytes(byteArray)
return true return true
@ -50,7 +50,7 @@ fun Uri.writeText(context: Context, text: String): Boolean {
if (DocumentFile.isDocumentUri(context, this)) { if (DocumentFile.isDocumentUri(context, this)) {
return DocumentUtils.writeText(context, text, this) return DocumentUtils.writeText(context, text, this)
} else { } else {
val path = FileUtils.getPath(context, this) val path = FileUtils.getRealPath(context, this)
if (path?.isNotEmpty() == true) { if (path?.isNotEmpty() == true) {
File(path).writeText(text) File(path).writeText(text)
return true return true

Loading…
Cancel
Save