From b972679f6a14375065652284a16adeef1a98b598 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 2 Jan 2020 16:16:41 +0800 Subject: [PATCH] up --- .../legado/app/base/BasePreferenceFragment.kt | 27 ----- .../java/io/legado/app/constant/PreferKey.kt | 1 + .../ui/book/read/config/ReadStyleDialog.kt | 17 +-- .../app/ui/config/WebDavConfigFragment.kt | 22 +++- .../app/ui/widget/font/FontSelectDialog.kt | 108 +++++++++++------- .../java/io/legado/app/utils/DocumentUtils.kt | 4 + .../main/res/layout/dialog_font_select.xml | 16 ++- 7 files changed, 102 insertions(+), 93 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/base/BasePreferenceFragment.kt diff --git a/app/src/main/java/io/legado/app/base/BasePreferenceFragment.kt b/app/src/main/java/io/legado/app/base/BasePreferenceFragment.kt deleted file mode 100644 index b2c07d63f..000000000 --- a/app/src/main/java/io/legado/app/base/BasePreferenceFragment.kt +++ /dev/null @@ -1,27 +0,0 @@ -package io.legado.app.base - -import android.os.Bundle -import androidx.preference.PreferenceFragmentCompat -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import kotlin.coroutines.CoroutineContext - - -abstract class BasePreferenceFragment : PreferenceFragmentCompat(), CoroutineScope { - - lateinit var job: Job - - override val coroutineContext: CoroutineContext - get() = job + Dispatchers.Main - - override fun onCreate(savedInstanceState: Bundle?) { - job = Job() - super.onCreate(savedInstanceState) - } - - override fun onDestroy() { - super.onDestroy() - job.cancel() - } -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index c777fdb9a..bc74ed1ab 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -16,4 +16,5 @@ object PreferKey { const val lastGroup = "lastGroup" const val pageAnim = "pageAnim" const val readBookFont = "readBookFont" + const val fontFolder = "fontFolder" } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt index 098edad43..e19231b4a 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt @@ -20,7 +20,10 @@ import io.legado.app.lib.theme.primaryColor import io.legado.app.ui.book.read.Help import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.widget.font.FontSelectDialog -import io.legado.app.utils.* +import io.legado.app.utils.getPrefInt +import io.legado.app.utils.postEvent +import io.legado.app.utils.progressAdd +import io.legado.app.utils.putPrefInt import kotlinx.android.synthetic.main.activity_book_read.* import kotlinx.android.synthetic.main.dialog_read_book_style.* import org.jetbrains.anko.sdk27.listeners.onCheckedChange @@ -86,17 +89,7 @@ class ReadStyleDialog : DialogFragment() { postEvent(Bus.UP_CONFIG, false) } tv_text_font.onClick { - FontSelectDialog(requireContext()).apply { - curPath = requireContext().getPrefString(PreferKey.readBookFont) - defaultFont = { - requireContext().putPrefString(PreferKey.readBookFont, "") - postEvent(Bus.UP_CONFIG, true) - } - selectFile = { - requireContext().putPrefString(PreferKey.readBookFont, it) - postEvent(Bus.UP_CONFIG, true) - } - }.show() + FontSelectDialog().show(childFragmentManager, "fontSelectDialog") } tv_text_indent.onClick { selector( diff --git a/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt index 68fc6780a..28f9c825d 100644 --- a/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt @@ -11,8 +11,8 @@ import androidx.documentfile.provider.DocumentFile import androidx.preference.EditTextPreference import androidx.preference.ListPreference import androidx.preference.Preference +import androidx.preference.PreferenceFragmentCompat import io.legado.app.R -import io.legado.app.base.BasePreferenceFragment import io.legado.app.help.IntentHelp import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.PermissionsCompat @@ -28,17 +28,22 @@ import io.legado.app.utils.DocumentUtils import io.legado.app.utils.LogUtils import io.legado.app.utils.applyTint import io.legado.app.utils.getPrefString -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import org.jetbrains.anko.toast +import kotlin.coroutines.CoroutineContext -class WebDavConfigFragment : BasePreferenceFragment(), Preference.OnPreferenceChangeListener { - +class WebDavConfigFragment : PreferenceFragmentCompat(), + Preference.OnPreferenceChangeListener, + CoroutineScope { + lateinit var job: Job private val oldDataRequestCode = 23156 + override val coroutineContext: CoroutineContext + get() = job + Dispatchers.Main + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + job = Job() fun bindPreferenceSummaryToValue(preference: Preference?) { preference?.apply { onPreferenceChangeListener = this@WebDavConfigFragment @@ -76,6 +81,11 @@ class WebDavConfigFragment : BasePreferenceFragment(), Preference.OnPreferenceCh ATH.applyEdgeEffectColor(listView) } + override fun onDestroy() { + super.onDestroy() + job.cancel() + } + override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean { when { preference?.key == "web_dav_password" -> if (newValue == null) { diff --git a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt index b276072b4..f95923f1e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt @@ -1,68 +1,83 @@ package io.legado.app.ui.widget.font import android.annotation.SuppressLint -import android.content.Context -import android.os.Environment +import android.app.Activity.RESULT_OK +import android.content.Intent +import android.net.Uri +import android.os.Bundle +import android.util.DisplayMetrics import android.view.LayoutInflater import android.view.View -import androidx.appcompat.app.AlertDialog +import android.view.ViewGroup +import androidx.documentfile.provider.DocumentFile +import androidx.fragment.app.DialogFragment import androidx.recyclerview.widget.LinearLayoutManager +import io.legado.app.App import io.legado.app.R -import io.legado.app.lib.dialogs.AlertBuilder -import io.legado.app.lib.dialogs.alert -import io.legado.app.utils.applyTint -import io.legado.app.utils.invisible -import io.legado.app.utils.visible -import kotlinx.android.synthetic.main.dialog_font_select.view.* +import io.legado.app.constant.PreferKey +import io.legado.app.help.FileHelp +import io.legado.app.utils.DocumentUtils +import io.legado.app.utils.getPrefString +import kotlinx.android.synthetic.main.dialog_font_select.* import java.io.File -class FontSelectDialog(context: Context) : FontAdapter.CallBack { - - private val defaultFolder = - Environment.getExternalStorageDirectory().absolutePath + File.separator + "Fonts" +class FontSelectDialog : DialogFragment(), FontAdapter.CallBack { + private val fontFolderRequestCode = 35485 private lateinit var adapter: FontAdapter - private var builder: AlertBuilder - private var dialog: AlertDialog? = null - @SuppressLint("InflateParams") - private var view: View = LayoutInflater.from(context).inflate(R.layout.dialog_font_select, null) var curPath: String? = null - var fontFolder: String? = null + val fontFolder = App.INSTANCE.filesDir.absolutePath + File.separator + "Fonts" + File.separator var defaultFont: (() -> Unit)? = null var selectFile: ((path: String) -> Unit)? = null - init { - builder = context.alert(title = context.getString(R.string.select_font)) { - customView = view - positiveButton(R.string.default_font) { defaultFont?.invoke() } - negativeButton(R.string.cancel) - } - initData() + override fun onStart() { + super.onStart() + val dm = DisplayMetrics() + activity?.windowManager?.defaultDisplay?.getMetrics(dm) + dialog?.window?.setLayout((dm.widthPixels * 0.9).toInt(), (dm.heightPixels * 0.9).toInt()) } - fun show() { - dialog = builder.show().applyTint() + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + return inflater.inflate(R.layout.dialog_font_select, container) } - private fun initData() = with(view) { - adapter = FontAdapter(context, this@FontSelectDialog) + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + tool_bar.setTitle(R.string.select_font) + adapter = FontAdapter(requireContext(), this) recycler_view.layoutManager = LinearLayoutManager(context) recycler_view.adapter = adapter - val files = getFontFiles() - if (files.isNullOrEmpty()) { - tv_no_data.visible() - } else { - tv_no_data.invisible() - adapter.setItems(files.toList()) + + val fontPath = getPrefString(PreferKey.fontFolder) + if (fontPath.isNullOrEmpty()) { + 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) { + + } + } + } + + @SuppressLint("DefaultLocale") + private fun getFontFiles(uri: Uri) { + DocumentFile.fromTreeUri(requireContext(), uri)?.listFiles()?.forEach { file -> + if (file.name?.toLowerCase()?.matches(".*\\.[ot]tf".toRegex()) == true) { + DocumentUtils.readBytes(App.INSTANCE, file.uri)?.let { + FileHelp.getFile(fontFolder + file.name).writeBytes(it) + } + } } } @SuppressLint("DefaultLocale") private fun getFontFiles(): Array? { - val path = if (fontFolder.isNullOrEmpty()) { - defaultFolder - } else fontFolder return try { - val file = File(path) + val file = File(fontFolder) file.listFiles { pathName -> pathName.name.toLowerCase().matches(".*\\.[ot]tf".toRegex()) } @@ -83,4 +98,19 @@ class FontSelectDialog(context: Context) : FontAdapter.CallBack { override fun curFilePath(): String { return curPath ?: "" } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + when (requestCode) { + fontFolderRequestCode -> if (resultCode == RESULT_OK) { + data?.data?.let { uri -> + getFontFiles(uri) + } + } + } + } + + interface CallBack { + fun selectFile(uri: Uri) + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt index e10170892..dcb128a8e 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt @@ -5,10 +5,12 @@ import android.net.Uri object DocumentUtils { + @JvmStatic fun writeText(context: Context, data: String, fileUri: Uri): Boolean { return writeBytes(context, data.toByteArray(), fileUri) } + @JvmStatic fun writeBytes(context: Context, data: ByteArray, fileUri: Uri): Boolean { try { context.contentResolver.openOutputStream(fileUri)?.let { @@ -22,6 +24,7 @@ object DocumentUtils { return false } + @JvmStatic fun readText(context: Context, uri: Uri): String? { readBytes(context, uri)?.let { return String(it) @@ -29,6 +32,7 @@ object DocumentUtils { return null } + @JvmStatic fun readBytes(context: Context, uri: Uri): ByteArray? { try { context.contentResolver.openInputStream(uri)?.let { diff --git a/app/src/main/res/layout/dialog_font_select.xml b/app/src/main/res/layout/dialog_font_select.xml index 08e33c0a9..60f080003 100644 --- a/app/src/main/res/layout/dialog_font_select.xml +++ b/app/src/main/res/layout/dialog_font_select.xml @@ -1,20 +1,18 @@ - - - + android:layout_height="match_parent" /> - \ No newline at end of file + \ No newline at end of file