|
|
|
@ -1,6 +1,5 @@ |
|
|
|
|
package io.legado.app.ui.widget.font |
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint |
|
|
|
|
import android.app.Activity.RESULT_OK |
|
|
|
|
import android.content.Intent |
|
|
|
|
import android.net.Uri |
|
|
|
@ -38,6 +37,7 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
|
Toolbar.OnMenuItemClickListener, |
|
|
|
|
FontAdapter.CallBack { |
|
|
|
|
private val fontFolderRequestCode = 35485 |
|
|
|
|
private val fontRegex = Regex(".*\\.[ot]tf") |
|
|
|
|
private val fontFolder by lazy { |
|
|
|
|
FileUtils.createFolderIfNotExist(App.INSTANCE.filesDir, "Fonts") |
|
|
|
|
} |
|
|
|
@ -75,12 +75,12 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
|
if (fontPath.isContentPath()) { |
|
|
|
|
val doc = DocumentFile.fromTreeUri(requireContext(), Uri.parse(fontPath)) |
|
|
|
|
if (doc?.canRead() == true) { |
|
|
|
|
getFontFiles(doc) |
|
|
|
|
loadFontFiles(doc) |
|
|
|
|
} else { |
|
|
|
|
openFolder() |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
getFontFilesByPermission(fontPath) |
|
|
|
|
loadFontFilesByPermission(fontPath) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -111,22 +111,21 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
|
FilePicker.selectFolder(this@FontSelectDialog, fontFolderRequestCode) { |
|
|
|
|
val path = "${FileUtils.getSdCardPath()}${File.separator}Fonts" |
|
|
|
|
putPrefString(PreferKey.fontFolder, path) |
|
|
|
|
getFontFilesByPermission(path) |
|
|
|
|
loadFontFilesByPermission(path) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@SuppressLint("DefaultLocale") |
|
|
|
|
private fun getFontFiles(doc: DocumentFile) { |
|
|
|
|
private fun loadFontFiles(doc: DocumentFile) { |
|
|
|
|
execute { |
|
|
|
|
val fontItems = arrayListOf<DocItem>() |
|
|
|
|
val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.uri) |
|
|
|
|
docItems.forEach { item -> |
|
|
|
|
if (item.name.toLowerCase().matches(".*\\.[ot]tf".toRegex())) { |
|
|
|
|
if (item.name.toLowerCase(Locale.getDefault()).matches(fontRegex)) { |
|
|
|
|
fontItems.add(item) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
fontItems |
|
|
|
|
fontItems.sortedBy { it.name } |
|
|
|
|
}.onSuccess { |
|
|
|
|
adapter?.setItems(it) |
|
|
|
|
}.onError { |
|
|
|
@ -134,17 +133,22 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@SuppressLint("DefaultLocale") |
|
|
|
|
private fun getFontFilesByPermission(path: String) { |
|
|
|
|
private fun loadFontFilesByPermission(path: String) { |
|
|
|
|
PermissionsCompat.Builder(this@FontSelectDialog) |
|
|
|
|
.addPermissions(*Permissions.Group.STORAGE) |
|
|
|
|
.rationale(R.string.tip_perm_request_storage) |
|
|
|
|
.onGranted { |
|
|
|
|
try { |
|
|
|
|
loadFontFiles(path) |
|
|
|
|
} |
|
|
|
|
.request() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun loadFontFiles(path: String) { |
|
|
|
|
execute { |
|
|
|
|
val fontItems = arrayListOf<DocItem>() |
|
|
|
|
val file = File(path) |
|
|
|
|
file.listFiles { pathName -> |
|
|
|
|
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) |
|
|
|
|
pathName.name.toLowerCase(Locale.getDefault()).matches(fontRegex) |
|
|
|
|
}?.forEach { |
|
|
|
|
fontItems.add( |
|
|
|
|
DocItem( |
|
|
|
@ -156,12 +160,12 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
adapter?.setItems(fontItems) |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
toast(e.localizedMessage ?: "") |
|
|
|
|
} |
|
|
|
|
fontItems.sortedBy { it.name } |
|
|
|
|
}.onSuccess { |
|
|
|
|
adapter?.setItems(it) |
|
|
|
|
}.onError { |
|
|
|
|
toast("getFontFiles:${it.localizedMessage}") |
|
|
|
|
} |
|
|
|
|
.request() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onClick(docItem: DocItem) { |
|
|
|
@ -198,7 +202,7 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
|
when (requestCode) { |
|
|
|
|
fontFolderRequestCode -> { |
|
|
|
|
putPrefString(PreferKey.fontFolder, currentPath) |
|
|
|
|
getFontFilesByPermission(currentPath) |
|
|
|
|
loadFontFilesByPermission(currentPath) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -215,10 +219,10 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
|
uri, |
|
|
|
|
Intent.FLAG_GRANT_READ_URI_PERMISSION |
|
|
|
|
) |
|
|
|
|
getFontFiles(doc) |
|
|
|
|
loadFontFiles(doc) |
|
|
|
|
} else { |
|
|
|
|
RealPathUtil.getPath(requireContext(), uri)?.let { |
|
|
|
|
getFontFilesByPermission(it) |
|
|
|
|
loadFontFilesByPermission(it) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|