Skip to content
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 retrieving command line flags in Android. #84102

Merged
merged 1 commit into from
Oct 28, 2023

Conversation

MTareqAzim
Copy link
Contributor

Fixes #80574

This fix stemmed from immersive mode not working in Godot 4.2. It was caused by the command line flags not properly being retrieved.

In this case, the strings retrieved were not properly added to the ArrayList. I do not know Kotlin, so it took time for me to find this fix, but that also means there might be a better fix than this one.

Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally, it works as expected on a Samsung Galaxy Z Fold4 (Android 13).

Testing project: test_android_immersive_mode.zip

Before1 After (this PR)
Screenshot_20231028_102954_Test Android Immersive Mode Screenshot_20231028_103510_Test Android Immersive Mode

APKs for testing:

Footnotes

  1. Showing notification bar is disabled on screenshots in my system settings, but notice how the image is less tall due to some space being taken by the notification bar.

@akien-mga
Copy link
Member

akien-mga commented Oct 28, 2023

The fix seems correct, but to make it cleaner, I would suggest removing the argc argument from var cmdline = ArrayList<String>(argc).

This seems to be a misinterpretation of the API, this argument is called initialCapacity but it doesn't actually resize the ArrayList. It's not clear what it does, but it might be that it doesn't do anything actually: https://stackoverflow.com/a/45661667

I confirm that this program prints an empty array:

fun main() {
    var argc = 4
    val cmdline = ArrayList<String>(argc)
    println(cmdline)
}

I checked and that's the only occurrence of using the optional initialCapacity parameter in our Kotlin codebase.

Note: Please make modifications by amending the commit, and force pushing the changes, so that it stays as a single commit (see PR workflow).

@MTareqAzim MTareqAzim force-pushed the fix-android-immersive-mode branch from 9f49cdb to bc44584 Compare October 28, 2023 10:36
@m4gr3d
Copy link
Contributor

m4gr3d commented Oct 28, 2023

@MTareqAzim Thanks for the fix!
The original type of cmdLine was Array<String> for which the addition as previously done worked.
The switch to ArrayList<> changed the semantics, introducing the bug.

@m4gr3d
Copy link
Contributor

m4gr3d commented Oct 28, 2023

The fix seems correct, but to make it cleaner, I would suggest removing the argc argument from var cmdline = ArrayList<String>(argc).

This seems to be a misinterpretation of the API, this argument is called initialCapacity but it doesn't actually resize the ArrayList. It's not clear what it does, but it might be that it doesn't do anything actually: https://stackoverflow.com/a/45661667

I confirm that this program prints an empty array:

fun main() {
    var argc = 4
    val cmdline = ArrayList<String>(argc)
    println(cmdline)
}

I checked and that's the only occurrence of using the optional initialCapacity parameter in our Kotlin codebase.

Note: Please make modifications by amending the commit, and force pushing the changes, so that it stays as a single commit (see PR workflow).

The ArrayList data structure has an internal array it uses to store data that is dynamically resized. The initialCapacity argument is used to pre-allocate the size of that array, which is useful if you know how big it can grow to, avoiding re-allocation, which involves resizing and copying the original array.

As such, let's leave the original allocation as it was. For most operation it won't make a difference since the default initial capacity is 10, and the argument count is likely to be smaller or equal to that, but in occasional situation, it may be useful.

@MTareqAzim MTareqAzim force-pushed the fix-android-immersive-mode branch from bc44584 to ee31f24 Compare October 28, 2023 11:43
Copy link
Contributor

@m4gr3d m4gr3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@akien-mga akien-mga merged commit fd49c42 into godotengine:master Oct 28, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks! And congrats for your first merged Godot contribution 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Immersive mode no longer works on android, both the notification bar and the navigation bar are still visible.
4 participants