pull/2718/head
kunfei 2 years ago
parent 8509276008
commit 50849f6a07
  1. 7
      app/src/main/java/io/legado/app/lib/permission/OnErrorCallback.kt
  2. 4
      app/src/main/java/io/legado/app/lib/permission/OnPermissionsResultCallback.kt
  3. 2
      app/src/main/java/io/legado/app/lib/permission/OnRequestPermissionsResultCallback.kt
  4. 9
      app/src/main/java/io/legado/app/lib/permission/PermissionActivity.kt
  5. 9
      app/src/main/java/io/legado/app/lib/permission/PermissionsCompat.kt
  6. 10
      app/src/main/java/io/legado/app/lib/permission/Request.kt
  7. 6
      app/src/main/java/io/legado/app/lib/permission/RequestManager.kt
  8. 6
      app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt

@ -0,0 +1,7 @@
package io.legado.app.lib.permission
interface OnErrorCallback {
fun onError(e: Exception)
}

@ -4,6 +4,8 @@ interface OnPermissionsResultCallback {
fun onPermissionsGranted()
fun onPermissionsDenied(deniedPermissions: Array<String>)
fun onPermissionsDenied(deniedPermissions: Array<String>?)
fun onError(e: Exception)
}

@ -5,4 +5,6 @@ interface OnRequestPermissionsResultCallback {
fun onRequestPermissionsResult(permissions: Array<String>, grantResults: IntArray)
fun onSettingActivityResult()
fun onError(e: Exception)
}

@ -23,12 +23,11 @@ class PermissionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val requestCode = intent.getIntExtra(KEY_INPUT_PERMISSIONS_CODE, 1000)
val permissions = intent.getStringArrayExtra(KEY_INPUT_PERMISSIONS)
when (intent.getIntExtra(KEY_INPUT_REQUEST_TYPE, Request.TYPE_REQUEST_PERMISSION)) {
//权限请求
Request.TYPE_REQUEST_PERMISSION -> {
val requestCode = intent.getIntExtra(KEY_INPUT_PERMISSIONS_CODE, 1000)
val permissions = intent.getStringArrayExtra(KEY_INPUT_PERMISSIONS)
if (permissions != null) {
ActivityCompat.requestPermissions(this, permissions, requestCode)
} else {
@ -42,6 +41,7 @@ class PermissionActivity : AppCompatActivity() {
settingActivityResult.launch(settingIntent)
} catch (e: Exception) {
toastOnUi(R.string.tip_cannot_jump_setting_page)
RequestPlugins.sRequestCallback?.onError(e)
finish()
}
//所有文件所有文件的管理权限
@ -54,7 +54,8 @@ class PermissionActivity : AppCompatActivity() {
throw NoStackTraceException("no MANAGE_ALL_FILES_ACCESS_PERMISSION")
}
} catch (e: Exception) {
toastOnUi(R.string.tip_cannot_jump_setting_page)
toastOnUi(e.localizedMessage)
RequestPlugins.sRequestCallback?.onError(e)
finish()
}
}

@ -47,6 +47,15 @@ class PermissionsCompat private constructor() {
return this
}
fun onError(callback: (e: Exception) -> Unit): Builder {
request.setOnErrorCallBack(object : OnErrorCallback{
override fun onError(e: Exception) {
callback(e)
}
})
return this
}
fun rationale(rationale: CharSequence): Builder {
request.setRationale(rationale)
return this

@ -19,6 +19,7 @@ internal class Request : OnRequestPermissionsResultCallback {
private var permissions: ArrayList<String>? = null
private var grantedCallback: OnPermissionsGrantedCallback? = null
private var deniedCallback: OnPermissionsDeniedCallback? = null
private var errorCallback: OnErrorCallback? = null
private var rationaleResId: Int = 0
private var rationale: CharSequence? = null
@ -53,6 +54,10 @@ internal class Request : OnRequestPermissionsResultCallback {
deniedCallback = callback
}
fun setOnErrorCallBack(callback: OnErrorCallback) {
errorCallback = callback
}
fun setRationale(@StringRes resId: Int) {
rationaleResId = resId
rationale = null
@ -201,6 +206,11 @@ internal class Request : OnRequestPermissionsResultCallback {
}
}
override fun onError(e: Exception) {
errorCallback?.onError(e)
RequestPlugins.sResultCallback?.onError(e)
}
companion object {
const val TYPE_REQUEST_PERMISSION = 1
const val TYPE_REQUEST_SETTING = 2

@ -62,7 +62,11 @@ internal object RequestManager : OnPermissionsResultCallback {
startNextRequest()
}
override fun onPermissionsDenied(deniedPermissions: Array<String>) {
override fun onPermissionsDenied(deniedPermissions: Array<String>?) {
startNextRequest()
}
override fun onError(e: Exception) {
startNextRequest()
}

@ -192,6 +192,12 @@ class HandleFileActivity :
.onGranted {
success?.invoke()
}
.onDenied {
finish()
}
.onError {
finish()
}
.request()
}

Loading…
Cancel
Save