From 2dc6766f43340580fe59f885a5af0e6513a50acf Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 25 Mar 2021 15:47:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/filepicker/FilePicker.kt | 118 +++++++----------- .../app/ui/filepicker/FilePickerDialog.kt | 18 ++- avd.bat | 1 + avd11.bat | 1 - 4 files changed, 61 insertions(+), 77 deletions(-) create mode 100644 avd.bat delete mode 100644 avd11.bat diff --git a/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt b/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt index 68151ce0f..e05687953 100644 --- a/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt +++ b/app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt @@ -2,11 +2,9 @@ package io.legado.app.ui.filepicker import android.content.Intent import android.os.Build -import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import io.legado.app.R -import io.legado.app.constant.Permissions import io.legado.app.lib.dialogs.alert @Suppress("unused") @@ -34,25 +32,21 @@ object FilePicker { val intent = createSelectDirIntent() activity.startActivityForResult(intent, requestCode) }.onFailure { - checkPermissions(activity) { - FilePickerDialog.show( - activity.supportFragmentManager, - requestCode, - mode = FilePickerDialog.DIRECTORY - ) - } + FilePickerDialog.show( + activity.supportFragmentManager, + requestCode, + mode = FilePickerDialog.DIRECTORY + ) } } else -> { val selectText = selectList[index] if (selectText == activity.getString(R.string.app_folder_picker)) { - checkPermissions(activity) { - FilePickerDialog.show( - activity.supportFragmentManager, - requestCode, - mode = FilePickerDialog.DIRECTORY - ) - } + FilePickerDialog.show( + activity.supportFragmentManager, + requestCode, + mode = FilePickerDialog.DIRECTORY + ) } else { otherFun?.invoke(selectText) } @@ -84,25 +78,21 @@ object FilePicker { val intent = createSelectDirIntent() fragment.startActivityForResult(intent, requestCode) }.onFailure { - checkPermissions(fragment) { - FilePickerDialog.show( - fragment.childFragmentManager, - requestCode, - mode = FilePickerDialog.DIRECTORY - ) - } + FilePickerDialog.show( + fragment.childFragmentManager, + requestCode, + mode = FilePickerDialog.DIRECTORY + ) } } else -> { val selectText = selectList[index] if (selectText == fragment.getString(R.string.app_folder_picker)) { - checkPermissions(fragment) { - FilePickerDialog.show( - fragment.childFragmentManager, - requestCode, - mode = FilePickerDialog.DIRECTORY - ) - } + FilePickerDialog.show( + fragment.childFragmentManager, + requestCode, + mode = FilePickerDialog.DIRECTORY + ) } else { otherFun?.invoke(selectText) } @@ -139,27 +129,23 @@ object FilePicker { ) activity.startActivityForResult(intent, requestCode) }.onFailure { - checkPermissions(activity) { - FilePickerDialog.show( - activity.supportFragmentManager, - requestCode, - mode = FilePickerDialog.FILE, - allowExtensions = allowExtensions - ) - } + FilePickerDialog.show( + activity.supportFragmentManager, + requestCode, + mode = FilePickerDialog.FILE, + allowExtensions = allowExtensions + ) } } else -> { val selectText = selectList[index] if (selectText == activity.getString(R.string.app_file_picker)) { - checkPermissions(activity) { - FilePickerDialog.show( - activity.supportFragmentManager, - requestCode, - mode = FilePickerDialog.FILE, - allowExtensions = allowExtensions - ) - } + FilePickerDialog.show( + activity.supportFragmentManager, + requestCode, + mode = FilePickerDialog.FILE, + allowExtensions = allowExtensions + ) } else { otherFun?.invoke(selectText) } @@ -196,27 +182,23 @@ object FilePicker { ) fragment.startActivityForResult(intent, requestCode) }.onFailure { - checkPermissions(fragment) { - FilePickerDialog.show( - fragment.childFragmentManager, - requestCode, - mode = FilePickerDialog.FILE, - allowExtensions = allowExtensions - ) - } + FilePickerDialog.show( + fragment.childFragmentManager, + requestCode, + mode = FilePickerDialog.FILE, + allowExtensions = allowExtensions + ) } } else -> { val selectText = selectList[index] if (selectText == fragment.getString(R.string.app_file_picker)) { - checkPermissions(fragment) { - FilePickerDialog.show( - fragment.childFragmentManager, - requestCode, - mode = FilePickerDialog.FILE, - allowExtensions = allowExtensions - ) - } + FilePickerDialog.show( + fragment.childFragmentManager, + requestCode, + mode = FilePickerDialog.FILE, + allowExtensions = allowExtensions + ) } else { otherFun?.invoke(selectText) } @@ -246,18 +228,6 @@ object FilePicker { return intent } - private fun checkPermissions(fragment: Fragment, success: (() -> Unit)? = null) { - fragment.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { - success?.invoke() - }.launch(Permissions.Group.STORAGE) - } - - private fun checkPermissions(activity: AppCompatActivity, success: (() -> Unit)? = null) { - activity.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { - success?.invoke() - }.launch(Permissions.Group.STORAGE) - } - private fun typesOfExtensions(allowExtensions: Array): Array { val types = hashSetOf() if (allowExtensions.isNullOrEmpty()) { diff --git a/app/src/main/java/io/legado/app/ui/filepicker/FilePickerDialog.kt b/app/src/main/java/io/legado/app/ui/filepicker/FilePickerDialog.kt index 4eaa8aaea..5e0762775 100644 --- a/app/src/main/java/io/legado/app/ui/filepicker/FilePickerDialog.kt +++ b/app/src/main/java/io/legado/app/ui/filepicker/FilePickerDialog.kt @@ -8,12 +8,14 @@ import android.view.LayoutInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.widget.Toolbar import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.R +import io.legado.app.constant.Permissions import io.legado.app.databinding.DialogFileChooserBinding import io.legado.app.lib.theme.primaryColor import io.legado.app.ui.filepicker.adapter.FileAdapter @@ -69,7 +71,19 @@ class FilePickerDialog : DialogFragment(), override var isShowHomeDir: Boolean = false override var isShowUpDir: Boolean = true override var isShowHideDir: Boolean = false - + private val queryPermission = + registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { + var hasPermission = true + it.forEach { (t, u) -> + if (!u) { + hasPermission = false + toastOnUi(t) + } + } + if (hasPermission) { + refreshCurrentDirPath(initPath) + } + } private var requestCode: Int = 0 var title: String? = null private var initPath = FileUtils.getSdCardPath() @@ -118,7 +132,7 @@ class FilePickerDialog : DialogFragment(), } initMenu() initContentView() - refreshCurrentDirPath(initPath) + queryPermission.launch(Permissions.Group.STORAGE) } private fun initMenu() { diff --git a/avd.bat b/avd.bat new file mode 100644 index 000000000..9c6c97af0 --- /dev/null +++ b/avd.bat @@ -0,0 +1 @@ +emulator -avd %1 -dns-server 8.8.8.8 -no-snapshot-load \ No newline at end of file diff --git a/avd11.bat b/avd11.bat deleted file mode 100644 index fef788ee9..000000000 --- a/avd11.bat +++ /dev/null @@ -1 +0,0 @@ -emulator -avd android11 -dns-server 8.8.8.8 -no-snapshot-load \ No newline at end of file