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)
.onGranted {
FileUtils.getPath(requireContext(), uri)?.let { path ->
FileUtils.getRealPath(requireContext(), uri)?.let { path ->
ReadBookConfig.getConfig().setBg(2, path)
ReadBookConfig.upBg()
postEvent(EventBus.UP_CONFIG, false)

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

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

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

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

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

Loading…
Cancel
Save