feat: 优化代码

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

@ -25,27 +25,20 @@ 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)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
FileChooserDialog.show( FileChooserDialog.show(
activity.supportFragmentManager, activity.supportFragmentManager,
requestCode, requestCode,
mode = FileChooserDialog.DIRECTORY mode = FileChooserDialog.DIRECTORY
) )
} }
.request()
}
} }
} }
}.show() }.show()
@ -64,27 +57,20 @@ 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)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
FileChooserDialog.show( FileChooserDialog.show(
fragment.childFragmentManager, fragment.childFragmentManager,
requestCode, requestCode,
mode = FileChooserDialog.DIRECTORY mode = FileChooserDialog.DIRECTORY
) )
} }
.request()
}
} }
} }
}.show() }.show()
@ -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,11 +102,7 @@ object FilePicker {
activity.toast(e.localizedMessage ?: "ERROR") activity.toast(e.localizedMessage ?: "ERROR")
} }
} }
2 -> { 2 -> checkPermissions(activity) {
PermissionsCompat.Builder(activity)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
FileChooserDialog.show( FileChooserDialog.show(
activity.supportFragmentManager, activity.supportFragmentManager,
requestCode, requestCode,
@ -130,8 +110,6 @@ object FilePicker {
allowExtensions = allowExtensions allowExtensions = allowExtensions
) )
} }
.request()
}
} }
} }
}.show() }.show()
@ -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,11 +142,7 @@ object FilePicker {
fragment.toast(e.localizedMessage ?: "ERROR") fragment.toast(e.localizedMessage ?: "ERROR")
} }
} }
2 -> { 2 -> checkPermissions(fragment) {
PermissionsCompat.Builder(fragment)
.addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage)
.onGranted {
FileChooserDialog.show( FileChooserDialog.show(
fragment.childFragmentManager, fragment.childFragmentManager,
requestCode, requestCode,
@ -178,11 +150,41 @@ object FilePicker {
allowExtensions = allowExtensions 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