优化Android 11上的文件选择

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

@ -24,7 +24,7 @@ object FilePicker {
otherFun: ((action: String) -> Unit)? = null
) {
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))
}
otherActions?.let {
@ -69,7 +69,7 @@ object FilePicker {
otherFun: ((action: String) -> Unit)? = null
) {
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))
}
otherActions?.let {
@ -115,7 +115,7 @@ object FilePicker {
otherFun: ((action: String) -> Unit)? = null
) {
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))
}
otherActions?.let {
@ -174,7 +174,7 @@ object FilePicker {
otherFun: ((action: String) -> Unit)? = null
) {
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))
}
otherActions?.let {

@ -73,40 +73,4 @@
android:layout_height="wrap_content"
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>
Loading…
Cancel
Save