|
|
@ -26,11 +26,11 @@ import io.legado.app.ui.filechooser.FileChooserDialog |
|
|
|
import io.legado.app.ui.filechooser.FilePicker |
|
|
|
import io.legado.app.ui.filechooser.FilePicker |
|
|
|
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.Main |
|
|
|
import kotlinx.coroutines.Dispatchers.Main |
|
|
|
import kotlinx.coroutines.launch |
|
|
|
import kotlinx.coroutines.launch |
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
|
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
class FontSelectDialog : BaseDialogFragment(), |
|
|
|
class FontSelectDialog : BaseDialogFragment(), |
|
|
|
FileChooserDialog.CallBack, |
|
|
|
FileChooserDialog.CallBack, |
|
|
@ -40,9 +40,6 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
private val fontFolder by lazy { |
|
|
|
private val fontFolder by lazy { |
|
|
|
FileUtils.createFolderIfNotExist(App.INSTANCE.filesDir, "Fonts") |
|
|
|
FileUtils.createFolderIfNotExist(App.INSTANCE.filesDir, "Fonts") |
|
|
|
} |
|
|
|
} |
|
|
|
private val fontCacheFolder by lazy { |
|
|
|
|
|
|
|
FileUtils.createFolderIfNotExist(App.INSTANCE.cacheDir, "Fonts") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private var adapter: FontAdapter? = null |
|
|
|
private var adapter: FontAdapter? = null |
|
|
|
|
|
|
|
|
|
|
|
override fun onStart() { |
|
|
|
override fun onStart() { |
|
|
@ -90,7 +87,9 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
R.id.menu_default -> { |
|
|
|
R.id.menu_default -> { |
|
|
|
val requireContext = requireContext() |
|
|
|
val requireContext = requireContext() |
|
|
|
requireContext.alert(titleResource = R.string.system_typeface) { |
|
|
|
requireContext.alert(titleResource = R.string.system_typeface) { |
|
|
|
items(requireContext.resources.getStringArray(R.array.system_typefaces).toList()) { _, i -> |
|
|
|
items( |
|
|
|
|
|
|
|
requireContext.resources.getStringArray(R.array.system_typefaces).toList() |
|
|
|
|
|
|
|
) { _, i -> |
|
|
|
AppConfig.systemTypefaces = i |
|
|
|
AppConfig.systemTypefaces = i |
|
|
|
onDefaultFontChange() |
|
|
|
onDefaultFontChange() |
|
|
|
dismiss() |
|
|
|
dismiss() |
|
|
@ -117,40 +116,16 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
@SuppressLint("DefaultLocale") |
|
|
|
@SuppressLint("DefaultLocale") |
|
|
|
private fun getFontFiles(doc: DocumentFile) { |
|
|
|
private fun getFontFiles(doc: DocumentFile) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
|
|
|
|
val fontItems = arrayListOf<DocItem>() |
|
|
|
val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.uri) |
|
|
|
val docItems = DocumentUtils.listFiles(App.INSTANCE, doc.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 -> |
|
|
|
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) |
|
|
|
fontItems.add(item) |
|
|
|
if (!fontFile.exists()) { |
|
|
|
|
|
|
|
DocumentUtils.readBytes(App.INSTANCE, item.uri)?.let { byteArray -> |
|
|
|
|
|
|
|
fontFile.writeBytes(byteArray) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
fontCacheFolder.listFiles { pathName -> |
|
|
|
|
|
|
|
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) |
|
|
|
|
|
|
|
}?.let { |
|
|
|
|
|
|
|
withContext(Main) { |
|
|
|
|
|
|
|
adapter?.setItems(it.toList()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e: Exception) { |
|
|
|
fontItems |
|
|
|
toast(e.localizedMessage ?: "") |
|
|
|
}.onSuccess { |
|
|
|
} |
|
|
|
adapter?.setItems(it) |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
|
toast("getFontFiles:${it.localizedMessage}") |
|
|
|
toast("getFontFiles:${it.localizedMessage}") |
|
|
|
} |
|
|
|
} |
|
|
@ -163,12 +138,22 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
.rationale(R.string.tip_perm_request_storage) |
|
|
|
.rationale(R.string.tip_perm_request_storage) |
|
|
|
.onGranted { |
|
|
|
.onGranted { |
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
val fontItems = arrayListOf<DocItem>() |
|
|
|
val file = File(path) |
|
|
|
val file = File(path) |
|
|
|
file.listFiles { pathName -> |
|
|
|
file.listFiles { pathName -> |
|
|
|
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) |
|
|
|
pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) |
|
|
|
}?.let { |
|
|
|
}?.forEach { |
|
|
|
adapter?.setItems(it.toList()) |
|
|
|
fontItems.add( |
|
|
|
|
|
|
|
DocItem( |
|
|
|
|
|
|
|
it.name, |
|
|
|
|
|
|
|
it.extension, |
|
|
|
|
|
|
|
it.length(), |
|
|
|
|
|
|
|
Date(it.lastModified()), |
|
|
|
|
|
|
|
Uri.parse(it.absolutePath) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
adapter?.setItems(fontItems) |
|
|
|
} catch (e: Exception) { |
|
|
|
} catch (e: Exception) { |
|
|
|
toast(e.localizedMessage ?: "") |
|
|
|
toast(e.localizedMessage ?: "") |
|
|
|
} |
|
|
|
} |
|
|
@ -176,8 +161,22 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
.request() |
|
|
|
.request() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onClick(file: File) { |
|
|
|
override fun onClick(docItem: DocItem) { |
|
|
|
launch(IO) { |
|
|
|
execute { |
|
|
|
|
|
|
|
fontFolder.listFiles()?.forEach { |
|
|
|
|
|
|
|
it.delete() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (docItem.isContentPath) { |
|
|
|
|
|
|
|
val file = FileUtils.createFileIfNotExist(fontFolder, docItem.name) |
|
|
|
|
|
|
|
@Suppress("BlockingMethodInNonBlockingContext") |
|
|
|
|
|
|
|
requireActivity().contentResolver.openInputStream(docItem.uri)?.use { input -> |
|
|
|
|
|
|
|
file.outputStream().use { output -> |
|
|
|
|
|
|
|
input.copyTo(output) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
callBack?.selectFile(file.path) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
val file = File(docItem.uri.toString()) |
|
|
|
file.copyTo(FileUtils.createFileIfNotExist(fontFolder, file.name), true) |
|
|
|
file.copyTo(FileUtils.createFileIfNotExist(fontFolder, file.name), true) |
|
|
|
.absolutePath.let { path -> |
|
|
|
.absolutePath.let { path -> |
|
|
|
if (curFilePath != path) { |
|
|
|
if (curFilePath != path) { |
|
|
@ -186,6 +185,8 @@ class FontSelectDialog : BaseDialogFragment(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}.onSuccess { |
|
|
|
dialog?.dismiss() |
|
|
|
dialog?.dismiss() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|