permissions-dispatcher / PermissionsDispatcher

A declarative API to handle Android runtime permissions.
https://github.com/permissions-dispatcher/PermissionsDispatcher
Apache License 2.0
11.22k stars 1.44k forks source link

Handling support fragment's getActivity() #483

Closed shiraji closed 6 years ago

shiraji commented 6 years ago

Currently, we ignore the case when getActivity() return null

fun ContactsFragment.fooWithPermissionCheck() {
  if (PermissionUtils.hasSelfPermissions(activity, *PERMISSION_FOO) || Settings.canDrawOverlays(activity)) {
    foo()
  } else {
    val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + activity!!.getPackageName())) // NPE if activity is null
    activity.startActivityForResult(intent, REQUEST_FOO)
  }
}

We should take care when getActivity() return null. The following code is just one idea I came up.

fun ContactsFragment.fooWithPermissionCheck() {
  val activity = activity ?: return // add this 
  if (PermissionUtils.hasSelfPermissions(activity, *PERMISSION_FOO) || Settings.canDrawOverlays(activity)) {
    foo()
  } else {
    val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + activity.getPackageName()))
    activity.startActivityForResult(intent, REQUEST_FOO)
  }
}

See #482

mannodermaus commented 6 years ago

Are we only going to add the check for Kotlin-based code generation, since it would break compilation? Or rather, do we also want to add this null check to our ProcessorUnits in Java?

shiraji commented 6 years ago

I would like to add null check in Java since it should happens in Java, too.

hotchemi commented 6 years ago

🙇

hotchemi commented 6 years ago

@tomoya0x00 Just in case, are you working on this issue?

tomoya0x00 commented 6 years ago

@hotchemi Yes!

But I'm sorry. Currently, I prioritize other tasks, and work on this issue will be resumed in latter half of next week.

hotchemi commented 6 years ago

https://twitter.com/renaud_mathieu/status/1019222211004129282

tomoya0x00 commented 6 years ago

@hotchemi Thank you for your review!! ( #511 )

If you like, I'd create PR on your idea during the weekend.

hotchemi commented 6 years ago

@tomoya0x00 Go ahead thank you!

hotchemi commented 6 years ago

Will be released in next milestone!