feat: 优化代码

pull/123/head
kunfei 5 years ago
parent cd859406d5
commit 62236b4fd7
  1. 122
      app/src/main/java/io/legado/app/ui/filechooser/FilePicker.kt

@ -25,26 +25,19 @@ object FilePicker {
0 -> default?.invoke() 0 -> default?.invoke()
1 -> { 1 -> {
try { try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) val intent = getSelectDirIntent()
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
activity.startActivityForResult(intent, requestCode) activity.startActivityForResult(intent, requestCode)
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
e.printStackTrace() e.printStackTrace()
activity.toast(e.localizedMessage ?: "ERROR") activity.toast(e.localizedMessage ?: "ERROR")
} }
} }
2 -> { 2 -> checkPermissions(activity) {
PermissionsCompat.Builder(activity) FileChooserDialog.show(
.addPermissions(*Permissions.Group.STORAGE) activity.supportFragmentManager,
.rationale(R.string.tip_perm_request_storage) requestCode,
.onGranted { mode = FileChooserDialog.DIRECTORY
FileChooserDialog.show( )
activity.supportFragmentManager,
requestCode,
mode = FileChooserDialog.DIRECTORY
)
}
.request()
} }
} }
} }
@ -64,26 +57,19 @@ object FilePicker {
0 -> default?.invoke() 0 -> default?.invoke()
1 -> { 1 -> {
try { try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) val intent = getSelectDirIntent()
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
fragment.startActivityForResult(intent, requestCode) fragment.startActivityForResult(intent, requestCode)
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
e.printStackTrace() e.printStackTrace()
fragment.toast(e.localizedMessage ?: "ERROR") fragment.toast(e.localizedMessage ?: "ERROR")
} }
} }
2 -> { 2 -> checkPermissions(fragment) {
PermissionsCompat.Builder(fragment) FileChooserDialog.show(
.addPermissions(*Permissions.Group.STORAGE) fragment.childFragmentManager,
.rationale(R.string.tip_perm_request_storage) requestCode,
.onGranted { mode = FileChooserDialog.DIRECTORY
FileChooserDialog.show( )
fragment.childFragmentManager,
requestCode,
mode = FileChooserDialog.DIRECTORY
)
}
.request()
} }
} }
} }
@ -108,9 +94,7 @@ object FilePicker {
0 -> default?.invoke() 0 -> default?.invoke()
1 -> { 1 -> {
try { try {
val intent = Intent(Intent.ACTION_GET_CONTENT) val intent = getSelectFileIntent()
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.type = type//设置类型 intent.type = type//设置类型
activity.startActivityForResult(intent, requestCode) activity.startActivityForResult(intent, requestCode)
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
@ -118,19 +102,13 @@ object FilePicker {
activity.toast(e.localizedMessage ?: "ERROR") activity.toast(e.localizedMessage ?: "ERROR")
} }
} }
2 -> { 2 -> checkPermissions(activity) {
PermissionsCompat.Builder(activity) FileChooserDialog.show(
.addPermissions(*Permissions.Group.STORAGE) activity.supportFragmentManager,
.rationale(R.string.tip_perm_request_storage) requestCode,
.onGranted { mode = FileChooserDialog.FILE,
FileChooserDialog.show( allowExtensions = allowExtensions
activity.supportFragmentManager, )
requestCode,
mode = FileChooserDialog.FILE,
allowExtensions = allowExtensions
)
}
.request()
} }
} }
} }
@ -156,9 +134,7 @@ object FilePicker {
0 -> default?.invoke() 0 -> default?.invoke()
1 -> { 1 -> {
try { try {
val intent = Intent(Intent.ACTION_GET_CONTENT) val intent = getSelectFileIntent()
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.type = type//设置类型 intent.type = type//设置类型
fragment.startActivityForResult(intent, requestCode) fragment.startActivityForResult(intent, requestCode)
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
@ -166,23 +142,49 @@ object FilePicker {
fragment.toast(e.localizedMessage ?: "ERROR") fragment.toast(e.localizedMessage ?: "ERROR")
} }
} }
2 -> { 2 -> checkPermissions(fragment) {
PermissionsCompat.Builder(fragment) FileChooserDialog.show(
.addPermissions(*Permissions.Group.STORAGE) fragment.childFragmentManager,
.rationale(R.string.tip_perm_request_storage) requestCode,
.onGranted { mode = FileChooserDialog.FILE,
FileChooserDialog.show( allowExtensions = allowExtensions
fragment.childFragmentManager, )
requestCode,
mode = FileChooserDialog.FILE,
allowExtensions = allowExtensions
)
}
.request()
} }
} }
} }
}.show() }.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()
}
} }
Loading…
Cancel
Save