From 62236b4fd738602e0437a8f226b665465f202ba5 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 1 Mar 2020 15:51:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/filechooser/FilePicker.kt | 122 +++++++++--------- 1 file changed, 62 insertions(+), 60 deletions(-) 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 affb430ad..392286479 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 @@ -25,26 +25,19 @@ object FilePicker { 0 -> default?.invoke() 1 -> { try { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + val intent = getSelectDirIntent() activity.startActivityForResult(intent, requestCode) } catch (e: java.lang.Exception) { e.printStackTrace() activity.toast(e.localizedMessage ?: "ERROR") } } - 2 -> { - PermissionsCompat.Builder(activity) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - FileChooserDialog.show( - activity.supportFragmentManager, - requestCode, - mode = FileChooserDialog.DIRECTORY - ) - } - .request() + 2 -> checkPermissions(activity) { + FileChooserDialog.show( + activity.supportFragmentManager, + requestCode, + mode = FileChooserDialog.DIRECTORY + ) } } } @@ -64,26 +57,19 @@ object FilePicker { 0 -> default?.invoke() 1 -> { try { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + val intent = getSelectDirIntent() fragment.startActivityForResult(intent, requestCode) } catch (e: java.lang.Exception) { e.printStackTrace() fragment.toast(e.localizedMessage ?: "ERROR") } } - 2 -> { - PermissionsCompat.Builder(fragment) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - FileChooserDialog.show( - fragment.childFragmentManager, - requestCode, - mode = FileChooserDialog.DIRECTORY - ) - } - .request() + 2 -> checkPermissions(fragment) { + FileChooserDialog.show( + fragment.childFragmentManager, + requestCode, + mode = FileChooserDialog.DIRECTORY + ) } } } @@ -108,9 +94,7 @@ object FilePicker { 0 -> default?.invoke() 1 -> { try { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + val intent = getSelectFileIntent() intent.type = type//设置类型 activity.startActivityForResult(intent, requestCode) } catch (e: java.lang.Exception) { @@ -118,19 +102,13 @@ object FilePicker { activity.toast(e.localizedMessage ?: "ERROR") } } - 2 -> { - PermissionsCompat.Builder(activity) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - FileChooserDialog.show( - activity.supportFragmentManager, - requestCode, - mode = FileChooserDialog.FILE, - allowExtensions = allowExtensions - ) - } - .request() + 2 -> checkPermissions(activity) { + FileChooserDialog.show( + activity.supportFragmentManager, + requestCode, + mode = FileChooserDialog.FILE, + allowExtensions = allowExtensions + ) } } } @@ -156,9 +134,7 @@ object FilePicker { 0 -> default?.invoke() 1 -> { try { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + val intent = getSelectFileIntent() intent.type = type//设置类型 fragment.startActivityForResult(intent, requestCode) } catch (e: java.lang.Exception) { @@ -166,23 +142,49 @@ object FilePicker { fragment.toast(e.localizedMessage ?: "ERROR") } } - 2 -> { - PermissionsCompat.Builder(fragment) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - FileChooserDialog.show( - fragment.childFragmentManager, - requestCode, - mode = FileChooserDialog.FILE, - allowExtensions = allowExtensions - ) - } - .request() + 2 -> checkPermissions(fragment) { + FileChooserDialog.show( + fragment.childFragmentManager, + requestCode, + mode = FileChooserDialog.FILE, + allowExtensions = allowExtensions + ) } } } }.show() } + private fun getSelectFileIntent(): Intent { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.addCategory(Intent.CATEGORY_OPENABLE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + return intent + } + + private fun getSelectDirIntent(): Intent { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + return intent + } + + private fun checkPermissions(fragment: Fragment, success: (() -> Unit)? = null) { + PermissionsCompat.Builder(fragment) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + success?.invoke() + } + .request() + } + + private fun checkPermissions(activity: AppCompatActivity, success: (() -> Unit)? = null) { + PermissionsCompat.Builder(activity) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + success?.invoke() + } + .request() + } } \ No newline at end of file