diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 69f8133f7..49405ecbf 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -21,8 +21,6 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.BookSource import io.legado.app.help.ItemTouchCallback -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.dialogs.* import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor @@ -42,7 +40,6 @@ import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult import org.jetbrains.anko.toast import java.io.File -import java.io.FileNotFoundException class BookSourceActivity : VMBaseActivity(R.layout.activity_book_source), PopupMenu.OnMenuItemClickListener, @@ -86,7 +83,8 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity R.id.menu_import_source_qr -> startActivityForResult(qrRequestCode) R.id.menu_group_manage -> GroupManageDialog().show(supportFragmentManager, "groupManage") - R.id.menu_import_source_local -> selectFileSys() + R.id.menu_import_source_local -> FilePicker + .selectFile(this, importSource, "text/*", arrayOf("txt", "json")) R.id.menu_import_source_onLine -> showImportDialog() } if (item.groupId == R.id.source_group) { @@ -239,34 +237,6 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity }.show().applyTint() } - private fun selectFileSys() { - try { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - intent.type = "text/*"//设置类型 - startActivityForResult(intent, importSource) - } catch (e: Exception) { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - selectFile() - } - .request() - } - } - - private fun selectFile() { - FileChooserDialog.show( - supportFragmentManager, importSource, - allowExtensions = arrayOf("txt", "json") - ) - } - override fun upCountView() { select_action_bar.upCountView(adapter.getSelection().size, adapter.getActualItemCount()) } @@ -338,17 +308,6 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity title_bar.snackbar(msg) } } - } catch (e: FileNotFoundException) { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - selectFileSys() - } - .request() } catch (e: Exception) { toast(e.localizedMessage ?: "ERROR") } diff --git a/app/src/main/java/io/legado/app/ui/filechooser/FileChooserDialog.kt b/app/src/main/java/io/legado/app/ui/filechooser/FileChooserDialog.kt index af8cf50f4..f1e6a7bc9 100644 --- a/app/src/main/java/io/legado/app/ui/filechooser/FileChooserDialog.kt +++ b/app/src/main/java/io/legado/app/ui/filechooser/FileChooserDialog.kt @@ -42,7 +42,7 @@ class FileChooserDialog : DialogFragment(), isShowHomeDir: Boolean = false, isShowUpDir: Boolean = true, isShowHideDir: Boolean = false, - allowExtensions: Array? = null, + allowExtensions: Array? = null, menus: Array? = null ) { FileChooserDialog().apply { @@ -61,7 +61,7 @@ class FileChooserDialog : DialogFragment(), } } - override var allowExtensions: Array? = null + override var allowExtensions: Array? = null override val isOnlyListDir: Boolean get() = mode == DIRECTORY override var isShowHomeDir: Boolean = false diff --git a/app/src/main/java/io/legado/app/ui/filechooser/FilePicker.kt b/app/src/main/java/io/legado/app/ui/filechooser/FilePicker.kt index aa3db53c3..0b81368a1 100644 --- a/app/src/main/java/io/legado/app/ui/filechooser/FilePicker.kt +++ b/app/src/main/java/io/legado/app/ui/filechooser/FilePicker.kt @@ -94,6 +94,7 @@ object FilePicker { activity: BaseActivity, requestCode: Int, type: String, + allowExtensions: Array?, default: (() -> Unit)? = null ) { activity.alert(titleResource = R.string.select_file) { @@ -125,7 +126,8 @@ object FilePicker { FileChooserDialog.show( activity.supportFragmentManager, requestCode, - mode = FileChooserDialog.FILE + mode = FileChooserDialog.FILE, + allowExtensions = allowExtensions ) } .request() @@ -139,6 +141,7 @@ object FilePicker { fragment: Fragment, requestCode: Int, type: String, + allowExtensions: Array, default: (() -> Unit)? = null ) { fragment.requireContext() @@ -171,7 +174,8 @@ object FilePicker { FileChooserDialog.show( fragment.childFragmentManager, requestCode, - mode = FileChooserDialog.FILE + mode = FileChooserDialog.FILE, + allowExtensions = allowExtensions ) } .request() diff --git a/app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt b/app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt index 0fbaafe33..42e34dfb1 100644 --- a/app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/filechooser/adapter/FileAdapter.kt @@ -122,7 +122,7 @@ class FileAdapter(context: Context, val callBack: CallBack) : interface CallBack { fun onFileClick(position: Int) //允许的扩展名 - var allowExtensions: Array? + var allowExtensions: Array? /** * 是否仅仅读取目录 */ diff --git a/app/src/main/java/io/legado/app/ui/filechooser/utils/FileUtils.kt b/app/src/main/java/io/legado/app/ui/filechooser/utils/FileUtils.kt index 309dee0a2..e7e2a4805 100644 --- a/app/src/main/java/io/legado/app/ui/filechooser/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/ui/filechooser/utils/FileUtils.kt @@ -60,7 +60,7 @@ object FileUtils { @JvmOverloads fun listDirs( startDirPath: String, - excludeDirs: Array? = null, @SortType sortType: Int = BY_NAME_ASC + excludeDirs: Array? = null, @SortType sortType: Int = BY_NAME_ASC ): Array { var excludeDirs1 = excludeDirs val dirList = ArrayList() @@ -76,7 +76,7 @@ object FileUtils { }) ?: return arrayOfNulls(0) if (excludeDirs1 == null) { - excludeDirs1 = arrayOfNulls(0) + excludeDirs1 = arrayOf() } for (dir in dirs) { val file = dir.absoluteFile @@ -115,7 +115,7 @@ object FileUtils { @JvmOverloads fun listDirsAndFiles( startDirPath: String, - allowExtensions: Array? = null + allowExtensions: Array? = null ): Array? { val dirs: Array? val files: Array? = if (allowExtensions == null) { @@ -189,12 +189,13 @@ object FileUtils { /** * 列出指定目录下的所有文件 */ - fun listFiles(startDirPath: String, allowExtensions: Array): Array? { + fun listFiles(startDirPath: String, allowExtensions: Array?): Array? { val file = File(startDirPath) return file.listFiles { _, name -> //返回当前目录所有以某些扩展名结尾的文件 val extension = getExtension(name) - allowExtensions.contentDeepToString().contains(extension) + allowExtensions?.contentDeepToString()?.contains(extension) == true + || allowExtensions == null } } @@ -202,7 +203,10 @@ object FileUtils { * 列出指定目录下的所有文件 */ fun listFiles(startDirPath: String, allowExtension: String?): Array? { - return listFiles(startDirPath, arrayOf(allowExtension)) + return if (allowExtension == null) + listFiles(startDirPath, allowExtension = null) + else + listFiles(startDirPath, arrayOf(allowExtension)) } /** diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index 5044e2667..940173bd9 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -20,12 +20,11 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.ReplaceRule import io.legado.app.help.ItemTouchCallback -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.dialogs.* import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.filechooser.FileChooserDialog +import io.legado.app.ui.filechooser.FilePicker import io.legado.app.ui.replacerule.edit.ReplaceEditDialog import io.legado.app.ui.widget.SelectActionBar import io.legado.app.ui.widget.recycler.VerticalDivider @@ -36,7 +35,6 @@ import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.view_search.* import org.jetbrains.anko.toast import java.io.File -import java.io.FileNotFoundException class ReplaceRuleActivity : VMBaseActivity(R.layout.activity_replace_rule), @@ -177,7 +175,8 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi R.id.menu_del_selection -> viewModel.delSelection(adapter.getSelection()) R.id.menu_import_source_onLine -> showImportDialog() - R.id.menu_import_source_local -> selectFileSys() + R.id.menu_import_source_local -> FilePicker + .selectFile(this, importSource, "text/*", arrayOf("txt", "json")) } return super.onCompatOptionsItemSelected(item) } @@ -233,34 +232,6 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi }.show().applyTint() } - private fun selectFileSys() { - try { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - intent.type = "text/*"//设置类型 - startActivityForResult(intent, importSource) - } catch (e: Exception) { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - selectFile() - } - .request() - } - } - - private fun selectFile() { - FileChooserDialog.show( - supportFragmentManager, importSource, - allowExtensions = arrayOf("txt", "json") - ) - } - override fun onFilePicked(requestCode: Int, currentPath: String) { if (requestCode == importSource) { Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() @@ -292,17 +263,6 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi title_bar.snackbar(msg) } } - } catch (e: FileNotFoundException) { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - selectFileSys() - } - .request() } catch (e: Exception) { toast(e.localizedMessage ?: "ERROR") } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 22277476e..b26cfe8f7 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -20,12 +20,11 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.RssSource import io.legado.app.help.ItemTouchCallback -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat import io.legado.app.lib.dialogs.* import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.filechooser.FileChooserDialog +import io.legado.app.ui.filechooser.FilePicker import io.legado.app.ui.qrcode.QrCodeActivity import io.legado.app.ui.rss.source.edit.RssSourceEditActivity import io.legado.app.ui.widget.SelectActionBar @@ -38,7 +37,6 @@ import kotlinx.android.synthetic.main.view_search.* import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult import org.jetbrains.anko.toast -import java.io.FileNotFoundException class RssSourceActivity : VMBaseActivity(R.layout.activity_rss_source), @@ -79,7 +77,8 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_add -> startActivity() - R.id.menu_import_source_local -> selectFileSys() + R.id.menu_import_source_local -> FilePicker + .selectFile(this, importSource, "text/*", arrayOf("txt", "json")) R.id.menu_import_source_onLine -> showImportDialog() R.id.menu_import_source_qr -> startActivityForResult(qrRequestCode) R.id.menu_group_manage -> GroupManageDialog() @@ -248,33 +247,6 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r }.show().applyTint() } - private fun selectFileSys() { - try { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "text/*"//设置类型 - startActivityForResult(intent, importSource) - } catch (e: Exception) { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - selectFile() - } - .request() - } - } - - private fun selectFile() { - FileChooserDialog.show( - supportFragmentManager, importSource, - allowExtensions = arrayOf("txt", "json") - ) - } - override fun onFilePicked(requestCode: Int, currentPath: String) { if (requestCode == importSource) { Snackbar.make(title_bar, R.string.importing, Snackbar.LENGTH_INDEFINITE).show() @@ -297,17 +269,6 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r title_bar.snackbar(msg) } } - } catch (e: FileNotFoundException) { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - selectFileSys() - } - .request() } catch (e: Exception) { toast(e.localizedMessage ?: "ERROR") } 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 87d87e8d6..bbeb7f04e 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 @@ -23,13 +23,13 @@ import io.legado.app.help.permission.Permissions 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.ui.filechooser.FilePicker import io.legado.app.utils.* import kotlinx.android.synthetic.main.dialog_font_select.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import org.jetbrains.anko.toast import java.io.File class FontSelectDialog : BaseDialogFragment(), @@ -107,41 +107,11 @@ class FontSelectDialog : BaseDialogFragment(), private fun openFolder() { launch(Main) { - 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 { - 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() + FilePicker.selectFolder(this@FontSelectDialog, fontFolderRequestCode) { + val path = "${FileUtils.getSdCardPath()}${File.separator}Fonts" + putPrefString(PreferKey.fontFolder, path) + getFontFilesByPermission(path) + } } }