优化Android 11上的文件选择

pull/457/head
gedoor 4 years ago
parent bae6cb6657
commit 2ab03229f6
  1. 41
      app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt
  2. 8
      app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt
  3. 36
      app/src/main/res/layout/activity_import_book.xml

@ -4,11 +4,11 @@ import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.provider.DocumentsContract import android.provider.DocumentsContract
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
@ -119,36 +119,32 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
} }
private fun upRootDoc() { private fun upRootDoc() {
AppConfig.importBookPath?.let { AppConfig.importBookPath?.let { lastPath ->
if (it.isContentPath()) { when {
val rootUri = Uri.parse(it) lastPath.isContentPath() -> {
rootDoc = DocumentFile.fromTreeUri(this, rootUri) val rootUri = Uri.parse(lastPath)
subDocs.clear() rootDoc = DocumentFile.fromTreeUri(this, rootUri)
} else { subDocs.clear()
rootDoc = null upPath()
subDocs.clear() }
path = it Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> {
} FilePicker.selectFolder(this, requestCodeSelectFolder)
} ?: let { }
// 没有权限就显示一个授权提示和按钮 lastPath.isNotBlank() -> {
if (PermissionsCompat.check(this, *Permissions.Group.STORAGE)) {
hint_per.visibility = View.GONE
} else {
hint_per.visibility = View.VISIBLE
tv_request_per.onClick {
PermissionsCompat.Builder(this) PermissionsCompat.Builder(this)
.addPermissions(*Permissions.Group.STORAGE) .addPermissions(*Permissions.Group.STORAGE)
.rationale(R.string.tip_perm_request_storage) .rationale(R.string.tip_perm_request_storage)
.onGranted { .onGranted {
hint_per.visibility = View.GONE rootDoc = null
initData() subDocs.clear()
upRootDoc() path = lastPath
upPath()
} }
.request() .request()
} }
else -> FilePicker.selectFolder(this, requestCodeSelectFolder)
} }
} }
upPath()
} }
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
@ -183,6 +179,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
} }
} }
} ?: let { } ?: let {
if (path.isBlank()) return
tv_path.text = path.replace(sdPath, "SD") tv_path.text = path.replace(sdPath, "SD")
val docList = arrayListOf<DocItem>() val docList = arrayListOf<DocItem>()
File(path).listFiles()?.forEach { File(path).listFiles()?.forEach {

@ -24,7 +24,7 @@ object FilePicker {
otherFun: ((action: String) -> Unit)? = null otherFun: ((action: String) -> Unit)? = null
) { ) {
val selectList = arrayListOf(activity.getString(R.string.sys_folder_picker)) val selectList = arrayListOf(activity.getString(R.string.sys_folder_picker))
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
selectList.add(activity.getString(R.string.app_folder_picker)) selectList.add(activity.getString(R.string.app_folder_picker))
} }
otherActions?.let { otherActions?.let {
@ -69,7 +69,7 @@ object FilePicker {
otherFun: ((action: String) -> Unit)? = null otherFun: ((action: String) -> Unit)? = null
) { ) {
val selectList = arrayListOf(fragment.getString(R.string.sys_folder_picker)) val selectList = arrayListOf(fragment.getString(R.string.sys_folder_picker))
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
selectList.add(fragment.getString(R.string.app_folder_picker)) selectList.add(fragment.getString(R.string.app_folder_picker))
} }
otherActions?.let { otherActions?.let {
@ -115,7 +115,7 @@ object FilePicker {
otherFun: ((action: String) -> Unit)? = null otherFun: ((action: String) -> Unit)? = null
) { ) {
val selectList = arrayListOf(activity.getString(R.string.sys_file_picker)) val selectList = arrayListOf(activity.getString(R.string.sys_file_picker))
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
selectList.add(activity.getString(R.string.app_file_picker)) selectList.add(activity.getString(R.string.app_file_picker))
} }
otherActions?.let { otherActions?.let {
@ -174,7 +174,7 @@ object FilePicker {
otherFun: ((action: String) -> Unit)? = null otherFun: ((action: String) -> Unit)? = null
) { ) {
val selectList = arrayListOf(fragment.getString(R.string.sys_file_picker)) val selectList = arrayListOf(fragment.getString(R.string.sys_file_picker))
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
selectList.add(fragment.getString(R.string.app_file_picker)) selectList.add(fragment.getString(R.string.app_file_picker))
} }
otherActions?.let { otherActions?.let {

@ -73,40 +73,4 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent" /> app:layout_constraintBottom_toBottomOf="parent" />
<LinearLayout
android:id="@+id/hint_per"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical"
android:padding="36dp"
android:gravity="center"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="@string/tip_local_perm_request_storage"
android:layout_marginBottom="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
<io.legado.app.ui.widget.text.AccentBgTextView
android:id="@+id/tv_request_per"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:minHeight="42dp"
android:gravity="center"
android:text="@string/request_permission"
android:textSize="14sp"
app:radius="30dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save