diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 58bc77d9f..a1b9f1846 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,6 +23,7 @@ android:label="@string/app_name" android:networkSecurityConfig="@xml/network_security_config" android:supportsRtl="true" + android:requestLegacyExternalStorage="true" android:theme="@style/AppTheme.Light" tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute"> diff --git a/app/src/main/java/io/legado/app/help/permission/PermissionsCompat.kt b/app/src/main/java/io/legado/app/help/permission/PermissionsCompat.kt index a5ccdb266..4bbed5991 100644 --- a/app/src/main/java/io/legado/app/help/permission/PermissionsCompat.kt +++ b/app/src/main/java/io/legado/app/help/permission/PermissionsCompat.kt @@ -1,8 +1,11 @@ package io.legado.app.help.permission +import android.os.Build import androidx.annotation.StringRes import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment +import org.jetbrains.anko.startActivity +import java.util.ArrayList class PermissionsCompat private constructor() { @@ -12,6 +15,17 @@ class PermissionsCompat private constructor() { RequestManager.pushRequest(request) } + companion object { + // 检查权限, 如果已经拥有返回 true + fun check(activity: AppCompatActivity, vararg permissions: String): Boolean { + var request: Request = Request(activity) + var pers = ArrayList() + pers.addAll(listOf(*permissions)) + var data = request.getDeniedPermissions(pers.toTypedArray()) + return data == null; + } + } + class Builder { private val request: Request diff --git a/app/src/main/java/io/legado/app/help/permission/Request.kt b/app/src/main/java/io/legado/app/help/permission/Request.kt index 600cbe93c..f7ed0293a 100644 --- a/app/src/main/java/io/legado/app/help/permission/Request.kt +++ b/app/src/main/java/io/legado/app/help/permission/Request.kt @@ -102,7 +102,7 @@ internal class Request : OnRequestPermissionsResultCallback { deniedCallback = null } - private fun getDeniedPermissions(permissions: Array?): Array? { + fun getDeniedPermissions(permissions: Array?): Array? { if (permissions != null) { val deniedPermissionList = ArrayList() for (permission in permissions) { diff --git a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt index 2296423b4..ecd6f6e8f 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt @@ -8,6 +8,7 @@ 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 @@ -16,7 +17,11 @@ import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.EventBus import io.legado.app.help.AppConfig +import io.legado.app.help.ReadBookConfig +import io.legado.app.help.permission.Permissions +import io.legado.app.help.permission.PermissionsCompat import io.legado.app.ui.filechooser.FileChooserDialog import io.legado.app.ui.filechooser.FilePicker import io.legado.app.ui.widget.SelectActionBar @@ -80,6 +85,24 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity } } }) + + // 没有权限就显示一个授权提示和按钮 + 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) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + hint_per.visibility = View.GONE + initData() + upRootDoc() + } + .request() + } + } } private fun initEvent() { diff --git a/app/src/main/res/layout/activity_import_book.xml b/app/src/main/res/layout/activity_import_book.xml index e93a5b6c6..38d0b4f55 100644 --- a/app/src/main/res/layout/activity_import_book.xml +++ b/app/src/main/res/layout/activity_import_book.xml @@ -1,20 +1,23 @@ - + android:layout_height="wrap_content" + app:layout_constraintBottom_toBottomOf="parent" /> + + + + + + + + + + - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e42578401..63efa5aee 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -646,5 +646,6 @@ 当前阴影大小(elevation): %s 默认 主菜单 - + 点击授予权限 + 阅读需要访问存储卡权限,请点击下方的"授予权限"按钮,或前往“设置”—“应用权限”—打开所需权限。如果授予权限后仍然不正常,请点击右上角的“选择文件夹”,使用系统文件夹选择器。