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 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 onRequestPermissionsResult(permissions: Array<String>, grantResults: IntArray)
fun onSettingActivityResult() fun onSettingActivityResult()
fun onError(e: Exception)
} }

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

@ -47,6 +47,15 @@ class PermissionsCompat private constructor() {
return this 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 { fun rationale(rationale: CharSequence): Builder {
request.setRationale(rationale) request.setRationale(rationale)
return this return this

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

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

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

Loading…
Cancel
Save