Open Dkhusainov opened 6 years ago
@Dkhusainov can you show the code that you're using to request permissions and some of the class where you expect to get the callback?
My "PermissionService"
implements PermissionCallbacks
, then I just delegate onRequestPermissionsResult
callback manually from Activity
@Dkhusainov please show the code where you request permissions and how you "delegate" the callback.
act variable is an Activity from which I delegate the callback
@Dkhusainov I don't think this is a good idea, Service
classes should not hold references to Activities
. Also permissions should only be requested when the user is inside of your app, which means you should have an Activity
or Fragment
context at that time.
So I don't think EasyPermissions
should support this request.
Service
. Just a class that I suffixed with ServiceI simply ask you to maybe overload EasyPermissions and add another param(explicit PermissionCallbacks
), instead of casting things left and right the usual Google/Android way.
Any updates on this issue?
Any updates on this issue?
I ended up just using this hack. Hope this helps anyone
private val main = Handler(Looper.getMainLooper())
inline fun <reified T> Any.castOrNull(): T? = if (this is T) this else null
fun Any.getFieldValue(name: String): Any? {
val cls = this::class.java
val obj = this
return cls.getDeclaredField(name).run {
isAccessible = true
get(obj)
}
}
fun hackRationaleCallback(act: Activity, cb: EasyPermissions.RationaleCallbacks) {
main.post {
try {
act
.castOrNull<AppCompatActivity>()
?.supportFragmentManager
?.findFragmentByTag(RationaleDialogFragmentCompat.TAG)
?.castOrNull<RationaleDialogFragmentCompat>()
?.dialog
?.getFieldValue("mAlert")
?.getFieldValue("mButtonNegativeMessage")
?.getFieldValue("obj")
?.let { obj ->
obj::class.java.getDeclaredField("mRationaleCallbacks").apply {
isAccessible = true
set(obj, cb)
}
}
act
.fragmentManager
?.findFragmentByTag(RationaleDialogFragment.TAG)
?.castOrNull<RationaleDialogFragment>()
?.dialog
?.getFieldValue("mAlert")
?.getFieldValue("mButtonNegativeMessage")
?.getFieldValue("obj")
?.let { obj ->
obj::class.java.getDeclaredField("mRationaleCallbacks").apply {
isAccessible = true
set(obj, cb)
}
}
} catch (e: Throwable) {
Timber.e(e, "Failed to hack EasyPermission rationale callback")
}
}
}
For make permission request from service or worker - use Notification.
Basic Information
Device type: ____ OS version: 25 EasyPermissions version: 1.0.1
Describe the problem
What happened?
I don't use Fragment or Activity as my PermissionCallback. If the user denies rationale, I don't get the callback in onRequestPermissionsResult
What did you expect to happen? Get the callback