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

[w.i.p.] isWifiEnabled and isFullscreen use JNI:callBooleanMethod #73

Merged
merged 5 commits into from
Oct 28, 2017

Conversation

mwoz123
Copy link
Contributor

@mwoz123 mwoz123 commented Oct 23, 2017

in response to koreader/koreader#3396 (review)

Commit from phone.
needs testing

@mwoz123
Copy link
Contributor Author

mwoz123 commented Oct 23, 2017

That code change didn't help:

10-23 21:07:19.608 4368 4386 E luajit-launcher: Failed to run script: [string "android.lua"]:1050: 'struct JNINativeInterface' has no member named 'callBooleanMethod'
10-23 21:07:19.625 4368 4386 V threaded_app: android_app_destroy!

"android.lua" line 1050:

return self.env[0].callBooleanMethod(self.env, object, methodID, ...)

Is that the reason why nobody used callBooleanMethod before? :(

Does any one know how what's wrong here?

@@ -1360,7 +1360,7 @@ local function run(android_app_state)
end
android.isFullscreen = function()
return JNI:context(android.app.activity.vm, function(JNI)
local fullscreen = JNI:callIntMethod(
local fullscreen = JNI:callBooleanMethod(
android.app.activity.clazz,
"isFullscreen",
"()Z"
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be something like B then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean "()B" instead "()Z" ?
It's completely unreadable for me where it came from and what it does mean :(

If it caught my change the result is the same:
10-23 21:31:42.706 4577 4593 E luajit-launcher: Failed to run script: [string "android.lua"]:1050: 'struct JNINativeInterface' has no member named 'callBooleanMethod'
10-23 21:31:42.712 4577 4593 V threaded_app: android_app_destroy!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I must have overlooked doc for that. If there's such?

Copy link
Member

Choose a reason for hiding this comment

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

Pardon, B is byte and Z is boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just "Z" or "()Z" or "Z()"?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, please link directly to the line. It's clearer. :-) You should change it to a capital C.

Copy link
Member

Choose a reason for hiding this comment

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

On line 1050.

Copy link
Member

Choose a reason for hiding this comment

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

Just "Z" or "()Z" or "Z()"?

No it's good.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for my earlier brevity; I was on my phone in a slight hurry but I wanted to give you something useful to work with in case you were still looking at it.

Anyway, it should be fine if you fix it to an uppercase C on the line you mentioned.

return self.env[0].callBooleanMethod(self.env, object, methodID, ...)

@Frenzie
Copy link
Member

Frenzie commented Oct 24, 2017

I apologize if I was confusing, but it's this line:

return self.env[0].callBooleanMethod(self.env, object, methodID, ...)

That has to be changed to
return self.env[0].CallBooleanMethod(self.env, object, methodID, ...)

The other line is the line I mentioned, not the one you mentioned. ;-)

@mwoz123
Copy link
Contributor Author

mwoz123 commented Oct 27, 2017

I did a quick test and something still seems not ok:

I/KOReader( 3016): is WifiEnabled =
I/KOReader( 3016): DEBUG input event => type: 3, code: 47(S), value: 0, time: 1509127951.788931

at least on 686 emulator (android 5.1)

It doesn't throw any expection, but return is missing.

There's a workaround to use int as
android.isCharging = function() and then in lua compare if result ==1
but it would be nice to also callBooleanMethod working

@Frenzie is it time to try workaround? Or do you have any ideas what else might be wrong?

@mwoz123
Copy link
Contributor Author

mwoz123 commented Oct 27, 2017

same result for isFullscreen:

I/KOReader( 3137): is fullscreen =
I/KOReader( 3137): DEBUG screen_fullscreen_menu_table.lua: Fullscreen swiching to: false

@Frenzie
Copy link
Member

Frenzie commented Oct 27, 2017

I suspect you'll need to cast this jboolean object into a C bool.

Presumably it'll give you these values

static const int JNI_FALSE = 0;
static const int JNI_TRUE = 1;

There's a to_string.

function JNI:to_string(javastring)
local utf = self.env[0].GetStringUTFChars(self.env, javastring, nil)
local luastr = ffi.string(utf, self.env[0].GetStringUTFLength(self.env, javastring))
self.env[0].ReleaseStringUTFChars(self.env, javastring, utf)
return luastr
end

We'd probably have to add to_bool.

So you could fix it up properly if you want to. (I don't particularly feel like looking at that for the moment.)

But I think it's fine if you just keep the typo correction on CallBooleanMethod for the moment and use the int workaround for now. It doesn't look too horrible. :-)

@mwoz123
Copy link
Contributor Author

mwoz123 commented Oct 27, 2017

Changed to int + follow up changes in MainActivity.java but unfortunately on my x86 emulator result was exactly the same.

This can mean there's an issue in Java implementation, but it was just done excalty as on some recipes on internet (or issue with emulator?). Maybe it's again Java code has to be wrapped with something?

Any way if it's java issue then we would be able to uncommit ab96312

@Frenzie
Copy link
Member

Frenzie commented Oct 27, 2017

Oh, I thought you meant you'd already tested the int method. For wifi I would only trust results from a real device, although I would probably expect it to always return false in the emulator.

I don't have a working Android knowledge or anything. I only know various things that go into Android (such as XML and Java). But from browsing around the docs real quick it could be that you should use different methods for Android <4.4 and Android 4.4+.

API 16

https://developer.android.com/training/system-ui/immersive.html
https://developer.android.com/reference/android/view/View.html#SYSTEM_UI_FLAG_FULLSCREEN

API 1

https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_FULLSCREEN

Basically like this (should've checked our own code before checking the docs) except get instead of set

private void setFullscreenLayout() {
int SDK_INT = android.os.Build.VERSION.SDK_INT;
if(SDK_INT >= 11 && SDK_INT < 16) {
getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_HIDDEN);
} else if (SDK_INT >= 16 && SDK_INT < 19) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LOW_PROFILE);
} else if (SDK_INT >= 19) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}

@mwoz123
Copy link
Contributor Author

mwoz123 commented Oct 28, 2017

I checked it using 5.1 emulator which is api 22 so should work.

I checked setFullscreen and setWifiEnabled, it was working fine. Must have overlooked the isWifiEnabled and isFulscreen. Sorry.

BTW as far as I remember the setFullscreenLayout was after my full screen commit.

It seems to be the same issue. In android.lua or MainActivity.java.
From Java perspective it seems ok unless it again needs some extra magic around that code but it's now constructed exactly like isCharging so should work.(unless isCharging is not working correctly, but is at least returns a value in logs).

At least I can see now no difference between isCharging is Fullscreen and isWiFienabled...

@Frenzie
Copy link
Member

Frenzie commented Oct 28, 2017

I checked it using 5.1 emulator which is api 22 so should work.

Okay, but we want to keep things working on API 9 and up unless there's a really good reason not to. Doesn't your own Android reader come with Android API <16? ;-)

BTW as far as I remember the setFullscreenLayout was after my full screen commit.

It was structured differently but it was there from the first time the Java layer was added in cb6deb8 (with a slight mistake fixed in 655bf22).

public boolean isFullscreen() {
return (getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0;
public int isFullscreen() {
return ((getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0) ? 1: 0;
Copy link
Member

Choose a reason for hiding this comment

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

Should this be &&?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

& is a Java low level (bit) comparison as far as I remember from couple years back, Java trainings. It's almost not used in plain Java. Never had a chance to see it in real code.
But did seen it in android.

Also if this was the issue it should return at least false.

I'll double check if it's not the case..

Copy link
Member

Choose a reason for hiding this comment

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

I see, it's bitwise AND to extract the flag. Makes sense, although it seems more like a C kind of thing than something you'd expect to see in Java.

@mwoz123
Copy link
Contributor Author

mwoz123 commented Oct 28, 2017

This was a problem with x86 emulator.

Works on ARM. Please merge. I'll push new PR for API 11 Fullscreen

@Frenzie
Copy link
Member

Frenzie commented Oct 28, 2017

It's fine if it doesn't work but does it also not crash? :-P

@mwoz123
Copy link
Contributor Author

mwoz123 commented Oct 28, 2017

emulator x86 = doesn't crash, doesn't work :(

arm = doesn't crash, works;)

@Frenzie
Copy link
Member

Frenzie commented Oct 28, 2017

I meant API-wise, sorry for the confusion.

Edit: well, I suppose I meant both arch and API. I'll look into setting up some Android unit tests to prevent regressions (we currently only test if it builds) but I'm not entirely sure how yet.

Part of the problem is that it'll take 20 minutes or more, so they can't quite be made part of the regular CI. Maybe something involving a Gitlab mirror…

@Frenzie Frenzie merged commit 5541846 into koreader:master Oct 28, 2017
mwoz123 added a commit to mwoz123/android-luajit-launcher that referenced this pull request Nov 4, 2017
@mwoz123 mwoz123 deleted the mwoz123-use-call-boolean-method branch November 4, 2017 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants