feat: 优化代码

pull/105/head
kunfei 5 years ago
parent e4d572f0cf
commit 260cd64d77
  1. 67
      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() {
alert {
titleResource = R.string.select_folder
items(resources.getStringArray(R.array.select_folder).toList()) { _, index ->
when (index) {
0 -> {
val path = "${FileUtils.getSdCardPath()}${File.separator}Fonts"
putPrefString(PreferKey.fontFolder, path)
getFontFilesByPermission(path)
}
1 -> {
try { try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(intent, fontFolderRequestCode) startActivityForResult(intent, fontFolderRequestCode)
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
PermissionsCompat.Builder(this) e.printStackTrace()
requireContext().toast(e.localizedMessage ?: "ERROR")
}
}
2 -> {
PermissionsCompat.Builder(this@FontSelectDialog)
.addPermissions(*Permissions.Group.STORAGE) .addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage) .rationale(R.string.tip_perm_request_storage)
.onGranted { getFontFilesOld() } .onGranted {
FileChooserDialog.show(
childFragmentManager,
fontFolderRequestCode,
mode = FileChooserDialog.DIRECTORY
)
}
.request() .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,10 +176,13 @@ class FontSelectDialog : BaseDialogFragment(),
} }
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
private fun getFontFilesOld() { private fun getFontFilesByPermission(path: String) {
PermissionsCompat.Builder(this@FontSelectDialog)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
try { try {
val file = val file = File(path)
File(FileUtils.getSdCardPath() + File.separator + "Fonts")
file.listFiles { pathName -> file.listFiles { pathName ->
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())
}?.let { }?.let {
@ -162,10 +192,12 @@ class FontSelectDialog : BaseDialogFragment(),
toast(e.localizedMessage ?: "") toast(e.localizedMessage ?: "")
} }
} }
.request()
}
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())
val doc = DocumentFile.fromTreeUri(requireContext(), uri)
if (doc != null) {
context?.contentResolver?.takePersistableUriPermission( context?.contentResolver?.takePersistableUriPermission(
uri, uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION
) )
getFontFiles(uri) getFontFiles(doc)
} else {
RealPathUtil.getPath(requireContext(), uri)?.let {
getFontFilesByPermission(it)
}
}
} }
} }
} }

Loading…
Cancel
Save