feat: 优化代码

pull/105/head
kunfei 5 years ago
parent e4d572f0cf
commit 260cd64d77
  1. 109
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt

@ -19,20 +19,24 @@ import io.legado.app.base.BaseDialogFragment
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.Permissions
import io.legado.app.help.permission.PermissionsCompat import io.legado.app.help.permission.PermissionsCompat
import io.legado.app.lib.dialogs.alert
import io.legado.app.ui.filechooser.FileChooserDialog
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.synthetic.main.dialog_font_select.* import kotlinx.android.synthetic.main.dialog_font_select.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast
import java.io.File import java.io.File
class FontSelectDialog : BaseDialogFragment(), class FontSelectDialog : BaseDialogFragment(),
Toolbar.OnMenuItemClickListener, Toolbar.OnMenuItemClickListener,
FontAdapter.CallBack { FontAdapter.CallBack {
private val fontFolderRequestCode = 35485 private val fontFolderRequestCode = 35485
private val fontFolder = private val fontFolder by lazy {
App.INSTANCE.filesDir.absolutePath + File.separator + "Fonts" + File.separator FileUtils.createFolderIfNotExist(App.INSTANCE.filesDir, "Fonts")
}
private val fontCacheFolder by lazy { private val fontCacheFolder by lazy {
FileUtils.createFolderIfNotExist(App.INSTANCE.cacheDir, "Fonts") FileUtils.createFolderIfNotExist(App.INSTANCE.cacheDir, "Fonts")
} }
@ -66,9 +70,9 @@ class FontSelectDialog : BaseDialogFragment(),
if (fontPath.isNullOrEmpty()) { if (fontPath.isNullOrEmpty()) {
openFolder() openFolder()
} else { } else {
val uri = Uri.parse(fontPath) val doc = DocumentFile.fromTreeUri(requireContext(), Uri.parse(fontPath))
if (DocumentFile.fromTreeUri(requireContext(), uri)?.canRead() == true) { if (doc?.canRead() == true) {
getFontFiles(uri) getFontFiles(doc)
} else { } else {
openFolder() openFolder()
} }
@ -94,23 +98,46 @@ class FontSelectDialog : BaseDialogFragment(),
} }
private fun openFolder() { private fun openFolder() {
try { alert {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) titleResource = R.string.select_folder
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) items(resources.getStringArray(R.array.select_folder).toList()) { _, index ->
startActivityForResult(intent, fontFolderRequestCode) when (index) {
} catch (e: java.lang.Exception) { 0 -> {
PermissionsCompat.Builder(this) val path = "${FileUtils.getSdCardPath()}${File.separator}Fonts"
.addPermissions(*Permissions.Group.STORAGE) putPrefString(PreferKey.fontFolder, path)
.rationale(R.string.tip_perm_request_storage) getFontFilesByPermission(path)
.onGranted { getFontFilesOld() } }
.request() 1 -> {
} try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(intent, fontFolderRequestCode)
} catch (e: java.lang.Exception) {
e.printStackTrace()
requireContext().toast(e.localizedMessage ?: "ERROR")
}
}
2 -> {
PermissionsCompat.Builder(this@FontSelectDialog)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
FileChooserDialog.show(
childFragmentManager,
fontFolderRequestCode,
mode = FileChooserDialog.DIRECTORY
)
}
.request()
}
}
}
}.show()
} }
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
private fun getFontFiles(uri: Uri) { private fun getFontFiles(doc: DocumentFile) {
launch(IO) { launch(IO) {
val doc = DocumentFile.fromTreeUri(App.INSTANCE, uri) ?: return@launch
val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.uri) val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.uri)
fontCacheFolder.listFiles()?.forEach { fontFile -> fontCacheFolder.listFiles()?.forEach { fontFile ->
var contain = false var contain = false
@ -149,23 +176,28 @@ class FontSelectDialog : BaseDialogFragment(),
} }
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
private fun getFontFilesOld() { private fun getFontFilesByPermission(path: String) {
try { PermissionsCompat.Builder(this@FontSelectDialog)
val file = .addPermissions(*Permissions.Group.STORAGE)
File(FileUtils.getSdCardPath() + File.separator + "Fonts") .rationale(R.string.tip_perm_request_storage)
file.listFiles { pathName -> .onGranted {
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) try {
}?.let { val file = File(path)
adapter?.setItems(it.toList()) file.listFiles { pathName ->
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())
}?.let {
adapter?.setItems(it.toList())
}
} catch (e: Exception) {
toast(e.localizedMessage ?: "")
}
} }
} catch (e: Exception) { .request()
toast(e.localizedMessage ?: "")
}
} }
override fun onClick(file: File) { override fun onClick(file: File) {
launch(IO) { launch(IO) {
file.copyTo(FileUtils.createFileIfNotExist(fontFolder + file.name), true) file.copyTo(FileUtils.createFileIfNotExist(fontFolder, file.name), true)
.absolutePath.let { path -> .absolutePath.let { path ->
val cb = (parentFragment as? CallBack) ?: (activity as? CallBack) val cb = (parentFragment as? CallBack) ?: (activity as? CallBack)
cb?.let { cb?.let {
@ -192,11 +224,18 @@ class FontSelectDialog : BaseDialogFragment(),
fontFolderRequestCode -> if (resultCode == RESULT_OK) { fontFolderRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri -> data?.data?.let { uri ->
putPrefString(PreferKey.fontFolder, uri.toString()) putPrefString(PreferKey.fontFolder, uri.toString())
context?.contentResolver?.takePersistableUriPermission( val doc = DocumentFile.fromTreeUri(requireContext(), uri)
uri, if (doc != null) {
Intent.FLAG_GRANT_READ_URI_PERMISSION context?.contentResolver?.takePersistableUriPermission(
) uri,
getFontFiles(uri) Intent.FLAG_GRANT_READ_URI_PERMISSION
)
getFontFiles(doc)
} else {
RealPathUtil.getPath(requireContext(), uri)?.let {
getFontFilesByPermission(it)
}
}
} }
} }
} }

Loading…
Cancel
Save