-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix compile error when request SYSTEM_ALERT_WINDOW on SupportFragment #482
Fix compile error when request SYSTEM_ALERT_WINDOW on SupportFragment #482
Conversation
Current generated code where we got syntax error for 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()))
activity.startActivityForResult(intent, REQUEST_FOO)
}
}
fun ContactsFragment.onActivityResult(requestCode: Int) {
when (requestCode) {
REQUEST_FOO -> {
if (PermissionUtils.hasSelfPermissions(activity, *PERMISSION_FOO) || Settings.canDrawOverlays(activity)) {
foo()
}
}
}
} Generated Code fun ContactsFragment.fooWithPermissionCheck() {
if (PermissionUtils.hasSelfPermissions(this.activity, *PERMISSION_FOO) || Settings.canDrawOverlays(this.activity)) {
foo()
} else {
val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + this.activity!!.getPackageName()))
this.startActivityForResult(intent, REQUEST_FOO)
}
}
fun ContactsFragment.onActivityResult(requestCode: Int) {
when (requestCode) {
REQUEST_FOO -> {
if (PermissionUtils.hasSelfPermissions(this.activity, *PERMISSION_FOO) || Settings.canDrawOverlays(this.activity)) {
foo()
}
}
}
} |
@@ -14,7 +14,7 @@ class KotlinSupportFragmentProcessorUnit(messager: Messager) : KotlinBaseProcess | |||
|
|||
override fun getTargetType(): TypeMirror = typeMirrorOf("android.support.v4.app.Fragment") | |||
|
|||
override fun getActivityName(): String = "activity" | |||
override fun getActivityName(targetParam: String): String = "$targetParam.activity" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder can we ignore targetParam
here?
override fun getActivityName(targetParam: String): String = "activity"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The answer is No! We have PermissionRequest
class
val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + target.activity!!.getPackageName()))
We really need compiling test for kotlin now... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may need to discuss about !!
but at this moment, we can merge this in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much! is it possible to avoid using !!
? 👁
builder.addStatement("val intent = %T(%T.ACTION_MANAGE_OVERLAY_PERMISSION, %T.parse(\"package:\" + %N.getPackageName()))", INTENT, SETTINGS, URI, activity) | ||
builder.addStatement("%N.startActivityForResult(intent, %N)", activity, requestCodeField) | ||
override fun addRequestPermissionsStatement(builder: FunSpec.Builder, targetParam: String, activityVar: String, requestCodeField: String) { | ||
builder.addStatement("val intent = %T(%T.ACTION_MANAGE_OVERLAY_PERMISSION, %T.parse(\"package:\" + %N!!.getPackageName()))", INTENT, SETTINGS, URI, activityVar) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have 2 options to not to use !!
-
We can replace
!!
with?
. Likeparse("package: " + activity?.getPackageName())
it ends upparse("package: null")
. -
However, if we could add null check to Java version, we can do early return in kotlin so that we don't need to worry about this problem. (This generated code still have problem with
startActivityForResult
)
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)
}
}
What do you think ? @hotchemi @tomoya0x00
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we should do this in this PR. We can ship this PR and then later version we can add null check in both Java and Kotlin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed this comment.
I think we can add a null check later.
I think that early return
is better. So We already use it in PermissionRequest class, I feel a sense of unity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea I definitely prefer the latter one. But it can be another PR for sure~
There is no kotlin compiler testing that tests kapt generated test. @mannodermaus asks moshi team to open kotlin code gen test class |
OK let me merge! Hopefully someone would address |
This problem seems to have happened because
android.support.v4.app.Fragment.getActivity()
is specified as nullable.And I also fix #429.