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

Trying to get rid of the black area #430

Closed
wants to merge 9 commits into from

Conversation

hugleo
Copy link
Contributor

@hugleo hugleo commented Aug 6, 2023

"Following r-1's suggestion on koreader/koreader#10591 (comment), I got rid of the variables surfaceHeight and surfaceWidth. I don't know if it will break something, but at least it is rendering normally on my devices."


This change is Reviewable

"Following r-1's suggestion on koreader/koreader#10591 (comment), I got rid of the variables surfaceHeight and surfaceWidth. I don't know if it will break something, but at least it is rendering normally on my devices."
@NiLuJe
Copy link
Member

NiLuJe commented Aug 6, 2023

Err, yes, you've just broken notches everywhere ;o).

@hugleo
Copy link
Contributor Author

hugleo commented Aug 6, 2023

Somehow, it seems to be working 100% here. I'm not a detail-oriented guy.

@pazos
Copy link
Member

pazos commented Aug 6, 2023

It works on your device because it doesn't have a hole. It screws all devices with holes. Both Android 13 and below.

We can't accept a PR like that

@Frenzie
Copy link
Member

Frenzie commented Aug 6, 2023

If I understand correctly that means the device is reporting bogus values? Since it doesn't have a cutout and all that?

I'm just testing with this...

Added:
window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES

It seems to have gotten rid of the notch problem on my device, which has a hole but it crashed on my old device, of course.
@pazos
Copy link
Member

pazos commented Aug 6, 2023

Hi, the latest commit doesn't fix anything. New variables are null so you're returning the same as before.

Please, if you want to fix the issue, ask first. We can give you hints to achieve what you want.

@hugleo
Copy link
Contributor Author

hugleo commented Aug 6, 2023

Just some crazy tests for now.

@hugleo hugleo closed this Aug 6, 2023
@pazos
Copy link
Member

pazos commented Aug 6, 2023

No problem :)

Always good to see people tinkering in this repo.

In case you would like to do another attempt we could:

  1. revert the changes in a9d36a0 (working for android 4 9 up to android 12)
  2. reapply the changes just for android 13
  3. Based on this comment: Tolino Epos 2 orientation change - faulty rendering koreader#10591 (comment) you tell me what we need to do on android 14 :). (either behave like in 9-12, the thing in 13 or that obscure flag window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES if this is the new thing to do:))

@hugleo
Copy link
Contributor Author

hugleo commented Aug 7, 2023

Debugging

@hugleo
Copy link
Contributor Author

hugleo commented Aug 7, 2023

I've discovered through debugging that the surfaceChanged function is executing some milliseconds after Koreader captures the event C.APP_CMD_CONFIG_CHANGED. This happens more often on older devices. I believe that eventually, this issue could also happens on Android 13, leading to a black area as Koreader draws with the inverted orientation. I reverted the changes and managed to simulate the notch problem on my device with Android 14. I'll attempt to debug this further.

@hugleo
Copy link
Contributor Author

hugleo commented Aug 8, 2023

More debugging reveals that the problem is even worse for older devices, such as my Android 6 device:

08-07 22:33:52.797 1824 1837 I KOReader: setting zoom mode to manual
08-07 22:33:53.974 1824 1824 V Surface : surface changed {
08-07 22:33:53.974 1824 1824 V Surface : width: 1872
08-07 22:33:53.974 1824 1824 V Surface : height: 1404
08-07 22:33:53.974 1824 1824 V Surface : format: OPAQUE
08-07 22:33:53.974 1824 1824 V Surface : }
08-07 22:33:54.199 1824 1837 V MainActivity: requesting epd update, type: EPD_FULL

08-07 22:35:01.789 548 1848 I ActivityManager: Config changes=480 {1.3 dualscreenflag=DISABLE ?mcc?mnc en_US ldltr sw702dp w702dp h891dp 320dpi lrg port finger -keyb/v/h -nav/h s.25}
08-07 22:35:02.637 1824 1837 V MainActivity: requesting epd update, type: EPD_FULL
08-07 22:37:43.316 1824 1837 V MainActivity: requesting epd update, type: EPD_FULL
08-07 22:38:48.198 548 1486 I ActivityManager: Config changes=480 {1.3 dualscreenflag=DISABLE ?mcc?mnc en_US ldltr sw702dp w936dp h657dp 320dpi lrg land finger -keyb/v/h -nav/h s.26}
08-07 22:38:49.406 1824 1837 V MainActivity: requesting epd update, type: EPD_FULL
08-07 22:41:46.110 548 669 I ActivityManager: Config changes=480 {1.3 dualscreenflag=DISABLE ?mcc?mnc en_US ldltr sw702dp w702dp h891dp 320dpi lrg port finger -keyb/v/h -nav/h s.27}

Do you notice the recurring line "Config changes=480"? This occurs when I rotate my device to the opposite orientation. We should observe lines similar to:

08-07 22:33:53.974 1824 1824 V Surface : surface changed {...

Since the "surfaceChanged" function is not being called, we are not receiving the new dimensions.

@hugleo hugleo reopened this Aug 13, 2023
@hugleo hugleo marked this pull request as draft August 13, 2023 14:24
@hugleo
Copy link
Contributor Author

hugleo commented Aug 13, 2023

I've reverted commit a9d36a0 and have been testing. It appears that the window.decorView.rootWindowInsets.displayCutout method is not being called in full-screen mode. I have now implemented a new method to obtain the cutout information.

One thing I've noticed is that sometimes when we first install Koreader it requests all file permissions on the Android system screen. If we attempt to open Koreader after this, it get the weights before the onAttachedToWindow method is called, resulting in an incorrect cutout value. I still haven't tried to tackle this issue.

Here is a log demonstrating this behavior:

08-13 11:19:10.718 4421 4421 V Surface : Using Native Content implementation
08-13 11:19:10.735 4421 4483 D Surface : Height notch setted
08-13 11:19:15.742 4421 4421 D Surface : onAttachedToWindow()
08-13 11:19:15.755 4421 4421 V Surface : surface changed {
08-13 11:19:15.755 4421 4421 V Surface : width: 1080
08-13 11:19:15.755 4421 4421 V Surface : height: 1776
08-13 11:19:15.755 4421 4421 V Surface : format: RGB_565
08-13 11:19:15.755 4421 4421 V Surface : }

After closing Koreader and reopening it, the problem appears to be resolved for subsequent instances.

08-13 11:20:38.959 4499 4499 V Surface : Using Native Content implementation
08-13 11:20:39.019 4499 4499 D Surface : onAttachedToWindow()
08-13 11:20:39.151 4499 4499 V Surface : surface changed {
08-13 11:20:39.151 4499 4499 V Surface : width: 1080
08-13 11:20:39.151 4499 4499 V Surface : height: 1776
08-13 11:20:39.151 4499 4499 V Surface : format: RGB_565
08-13 11:20:39.151 4499 4499 V Surface : }
08-13 11:20:39.239 4499 4519 D Surface : Height notch setted

@NiLuJe
Copy link
Member

NiLuJe commented Aug 14, 2023

One thing I've noticed is that sometimes when we first install Koreader it requests all file permissions on the Android system screen. If we attempt to open Koreader after this, it get the weights before the onAttachedToWindow method is called, resulting in an incorrect cutout value. I still haven't tried to tackle this issue.

Yup, I definitely noticed that behavior since the PR ;).

@hugleo
Copy link
Contributor Author

hugleo commented Aug 14, 2023

I also took a shot at a different approach by making a few changes to the current code, seems to be working and it may be cleaner than this pull request. You can take a look at the changes in this branch: https://github.com/hugleo/android-luajit-launcher/tree/hugleo-fix-cutout

But now, there's a new issue that popped up yesterday (koreader/koreader#10808), and I'm thinking about whether he is already using the fixed version from commit a9d36a0 and he is using Android 13, which is supposed work at least with the controls, right?

Edit:
Learning some github stuffs :P ... and
Moved 'hugleo-fix-cutout' to this PR since I believe it's a better.
The old method is still available in the branch hugleo-fix-cutout-old-alternative.
It's been running on my devices for one month but it still needs testing from others.

@NiLuJe
Copy link
Member

NiLuJe commented Aug 14, 2023

Oh, I missed that #10808 was on A13. There may be some Pixel-specific weirdness involved, as I vaguely remember other reports of weirdness on Pixels...

@hugleo hugleo closed this Sep 21, 2023
@hugleo hugleo deleted the hugleo-patch-1 branch September 21, 2023 15:30
@hugleo hugleo restored the hugleo-patch-1 branch September 21, 2023 15:33
@hugleo hugleo reopened this Sep 21, 2023
I prefer this but the old is still in the branch hugleo-fix-cutout-old-alternative
@NiLuJe
Copy link
Member

NiLuJe commented Sep 21, 2023

For future reference, what are you testing on?

@hugleo
Copy link
Contributor Author

hugleo commented Sep 21, 2023

For future reference, what are you testing on?

Just some trouble with github from first time user ;-)
First renamed hugleo-patch-1 branch and the PR autoclose and chain all of that. Sorry for the boring notifications.

@NiLuJe
Copy link
Member

NiLuJe commented Sep 21, 2023

Oh, not that ;p. That was a genuine question, what are you testing the changes on? ;o).

@hugleo
Copy link
Contributor Author

hugleo commented Sep 21, 2023

Oh, not that ;p. That was a genuine question, what are you testing the changes on? ;o).

I tested it some time ago in the Android 13 emulator, both with and without a cutout. I rotated it multiple times and everything looks fine now I don't see the notches anymore.
Since then, I've been using it on my mobile phone with a cutout (Android 14) as well as on e-readers Likebook Mars (Android 6) and the Onyx Ultra (Android 11).
However it needs to be tested by others in case I missed something.

Maybe this check is better or is it a waste?
@pazos
Copy link
Member

pazos commented Sep 22, 2023

Hey, I'm unsuscribing to this ticket because it causes a bit of noise on my inbox.

Feel free to play and do crazy tests all you want. If some day this is ready for a review please ping me.

Copy link
Member

@pazos pazos left a comment

Choose a reason for hiding this comment

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

requires review

@hugleo
Copy link
Contributor Author

hugleo commented Sep 22, 2023

Hi @pazos

Is my last change.
When you have the time to review and test, please share your thoughts. You can go ahead.
I'll leave this as is until you're able to do it.

@hugleo hugleo marked this pull request as ready for review September 22, 2023 17:50
@@ -236,7 +236,7 @@ fun Activity.requestSpecialPermission(intent: Intent, rationale: String,
.setCancelable(false)
.setPositiveButton(ok) { _, _ ->
startActivity(intent)
finish()
finishAffinity()
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand what are you trying to do here.

Copy link
Member

Choose a reason for hiding this comment

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

The finish activity is there on purpose, to prevent ANR on bad behaving devices. It happens once in the lifetime of an installation, as the instructions already tell you so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When we first install koreader it's ask for permissions like the following images bellow. If I try to open Koreader after that the bottom menu is missing on devices with cutout because somehow gets the wrong dimensions. Not always but eventually happens.
Simply close KOReader and reopen it; this issue won't occur in subsequent runs.
It's not such a big problem, I think, but it seems to get rid of it using a more forceful finish with finishAffinity() that's is not as hard as System.exit(0).

Screenshot_2023-09-22-15-40-26-853_org koreader launcher debug

Screenshot_2023-09-22-15-40-35-928_com android settings

Copy link
Member

Choose a reason for hiding this comment

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

In that case, lets fix first the bigger problems and lets discard this for the moment.

Once we get something that works on all android versions we can address these little nitpicks ;)

Copy link
Member

Choose a reason for hiding this comment

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

(I've mentioned it elsewhere before, but I can reproduce similar weirdness on first boot-after-install on my device, FWIW).

surfaceHeight = height

// We need to trigger a new configuration event for KoReader to fix rotation when surfaceChanged might be invoked later
if (onConfigurationChangedCalled)
Copy link
Member

Choose a reason for hiding this comment

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

please don't call callbacks within other callbacks. That's strictly wrong. If you need to operate on whatever do it in https://github.com/koreader/koreader/blob/master/frontend/device/android/device.lua#L136

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, that's was my main concern about this changes. If it's wrong we try the other way ;-)

Tested and it really works on Lua, thanks.
https://github.com/hugleo/android-luajit-launcher/tree/hugleo-fix-cutout
https://github.com/hugleo/koreader/tree/cutout-handle-surface-in-lua
rotate on C.APP_CMD_CONFIG_CHANGED or C.APP_CMD_WINDOW_RESIZED (surface changed).
It seems to be working, and fix the order when android call surfacechanged later than configurationchanged.
Still can be improved but...
Following your suggestion in #430 (comment) seems better since it's more well tested. You tell me

Copy link
Member

Choose a reason for hiding this comment

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

mmm, afaik WINDOW_RESIZED always happens after CONFIG_CHANGED.

The issue with WINDOW_RESIZED is that we didn't receive that callback until we backported the glue code (in 623a834)

It looks that you can get rid of the CONFIG_CHANGED entirely (after all we just handle orientation and size changes and that will trigger the next callback)

In the same spirit I bet you can get rid of the if statements that check if it was indeed a change on the width and height and just start with this.device.screen:resize()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mmm, afaik WINDOW_RESIZED always happens after CONFIG_CHANGED.

The issue with WINDOW_RESIZED is that we didn't receive that callback until we backported the glue code (in 623a834)

It looks that you can get rid of the CONFIG_CHANGED entirely (after all we just handle orientation and size changes and that will trigger the next callback)

In the same spirit I bet you can get rid of the if statements that check if it was indeed a change on the width and height and just start with this.device.screen:resize()

Let the code speak for itself. I created a match between them.
https://github.com/hugleo/koreader/tree/cutout-handle-surface-in-lua

adb logcat -s KOReader | grep raced


Onyx device:

First time start Koreader
09-23 19:04:37.929 32440 32466 V KOReader: APP_CMD_WINDOW_RESIZED raced

First time we open a document:
none of them raced

Let's rotate

Rotate 1:
09-23 19:02:52.449 32257 32283 V KOReader: C.APP_CMD_CONFIG_CHANGED raced and won the match. Lets rotate here!
09-23 19:02:52.450 32257 32283 V KOReader: APP_CMD_WINDOW_RESIZED raced

Rotate 2:
09-23 19:03:08.435 32257 32283 V KOReader: C.APP_CMD_CONFIG_CHANGED raced and won the match. Lets rotate here!
09-23 19:03:08.436 32257 32283 V KOReader: APP_CMD_WINDOW_RESIZED raced

Rotate n
...


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Likebook Mars device:

First time start Koreader:
none of them raced

Fist time we open a document:
09-23 19:22:35.535 1528 1542 V KOReader: APP_CMD_WINDOW_RESIZED raced
09-23 19:22:35.537 1528 1542 V KOReader: APP_CMD_WINDOW_RESIZED raced

Let's rotate

Rotate 1:
09-23 19:26:34.554 1528 1542 V KOReader: C.APP_CMD_CONFIG_CHANGED raced and won the match. Lets rotate here!
09-23 19:26:34.564 1528 1542 V KOReader: C.APP_CMD_CONFIG_CHANGED raced

Rotate 2:
09-23 19:27:27.659 1528 1542 V KOReader: C.APP_CMD_CONFIG_CHANGED raced and won the match. Lets rotate here!
09-23 19:27:27.662 1528 1542 V KOReader: C.APP_CMD_CONFIG_CHANGED raced

Rotate n
...

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Android 14 device
(should be similar for Android 13 emulator ;-))

First time start Koreader:
none of them raced

Fist time we open a document:
09-23 19:39:19.107 8470 8499 V KOReader: APP_CMD_WINDOW_RESIZED raced
09-23 19:39:19.107 8470 8499 V KOReader: APP_CMD_WINDOW_RESIZED raced

Rotate 1
09-23 19:40:00.266 8470 8499 V KOReader: C.APP_CMD_CONFIG_CHANGED raced
09-23 19:40:00.301 8470 8499 V KOReader: APP_CMD_WINDOW_RESIZED raced and won the match. Lets rotate here!

Rotate 2
09-23 19:40:27.153 8470 8499 V KOReader: C.APP_CMD_CONFIG_CHANGED raced
09-23 19:40:27.226 8470 8499 V KOReader: APP_CMD_WINDOW_RESIZED raced and won the match. Lets rotate here!

Rotate 3
09-23 19:40:27.153 8470 8499 V KOReader: C.APP_CMD_CONFIG_CHANGED raced
09-23 19:40:27.226 8470 8499 V KOReader: APP_CMD_WINDOW_RESIZED raced and won the match. Lets rotate here!

Rotate 4
09-23 19:40:49.444 8470 8499 V KOReader: C.APP_CMD_CONFIG_CHANGED raced and won the match. Lets rotate here!
09-23 19:40:49.445 8470 8499 V KOReader: APP_CMD_WINDOW_RESIZED raced

Rotate 5
09-23 19:40:59.999 8470 8499 V KOReader: C.APP_CMD_CONFIG_CHANGED raced
09-23 19:41:00.037 8470 8499 V KOReader: APP_CMD_WINDOW_RESIZED raced and won the match. Lets rotate here!

Rotate 6
09-23 19:41:05.681 8470 8499 V KOReader: C.APP_CMD_CONFIG_CHANGED raced
09-23 19:41:05.721 8470 8499 V KOReader: APP_CMD_WINDOW_RESIZED raced and won the match. Lets rotate here!

Rotate 7
09-23 19:41:13.745 8470 8499 V KOReader: C.APP_CMD_CONFIG_CHANGED raced and won the match. Lets rotate here!
09-23 19:41:13.746 8470 8499 V KOReader: APP_CMD_WINDOW_RESIZED raced

Rotate n
Who knows?


The Android thing is a mess, isn't it? :) 😄

Copy link
Member

Choose a reason for hiding this comment

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

The Android thing is a mess, isn't it? :) 😄

Indeed :/

But I'm not sure what are you looking for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Android thing is a mess, isn't it? :) 😄

Indeed :/

But I'm not sure what are you looking for.

All of this devices will fail when rotate in our current code after a9d36a0. It might not be the first time when rotate for Android13/Android14 but eventually it will :)

So the conclusion is not true that WINDOW_RESIZED always happens after CONFIG_CHANGED ;-)

And as we can see in the race tests we can't get rid CONFIG_CHANGED either for some old devices since WINDOW_RESIZED is never called when rotates.

So rotate in the two events is the only thing I can think ATM.
For the other PR that I'll submit later it's a different story as we obtain the measurements on start in onAttachedWindow ;-)

Copy link
Member

Choose a reason for hiding this comment

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

All of this devices will fail when rotate in our current code after a9d36a0

Could you please test my suggestions all together?

Please reread all my comments again :), you should start with a working environment, as suggested.

Then confirm that everything is working again on all versions from 4.3 to 13.
Then check what happens with 14.

So the conclusion is not true that WINDOW_RESIZED always happens after CONFIG_CHANGED ;-)

Did it happen before?

Copy link
Contributor Author

@hugleo hugleo Sep 24, 2023

Choose a reason for hiding this comment

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

All of this devices will fail when rotate in our current code after a9d36a0

Could you please test my suggestions all together?

Please reread all my comments again :), you should start with a working environment, as suggested.
Sure,

I did. But I don't "reapply the changes just for android 13" just yet and make a different change instead that I will show you if you already tried that function that I used. Android 13 and Android 14 hit success in my teste at first glance ;)

It's true that will fix the notches.
My concern is only about rotation.
But I suspected that people saying in the issues that works for Android 13 only uses it in portrait mode and got lucky when they rotate, because in my tests is more probable that work that not.
Some evidences is that I can simulate the problem in Android 13 (with cutout support enabled in developer options ;))
My Android was 13 before became 14 and I remember facing the rotation problem as well but I don't remember the koreader version

So I already have the changes for Android 1-12, Android 14. Android 13 you see if you can accept the changes or we keep the current ;-)

If everyone is happy I'm totally fine, after all I don't have a Android 13 hardware anymore :)

I'll open the other PR and we work on that to make the changes to make everyone happy ;)

Then confirm that everything is working again on all versions from 4.3 to 13. Then check what happens with 14.

So the conclusion is not true that WINDOW_RESIZED always happens after CONFIG_CHANGED ;-)

Did it happen before?

What do you mean by before?

Copy link
Member

@pazos pazos left a comment

Choose a reason for hiding this comment

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

I really don't understand the intention of the change.

Doing #430 (comment) gets us the proper behaviour on android 4-12 and android 13. If that's not enough for android 14 please review the developers documentation about what changed.

If you don't find anything relevant then feel free to address the oddities guarded by that specific android version.

@hugleo
Copy link
Contributor Author

hugleo commented Sep 22, 2023

I really don't understand the intention of the change.

Doing #430 (comment) gets us the proper behaviour on android 4-12 and android 13. If that's not enough for android 14 please review the developers documentation about what changed.

If you don't find anything relevant then feel free to address the oddities guarded by that specific android version.

It was another method that I tried (The intention was just keep the code clean and avoid the checks on getScreenWidth/getScreenHeight). I also implemented reverting the changes in the branch:

https://github.com/hugleo/android-luajit-launcher/tree/hugleo-fix-cutout-old-alternative

I didn't know which one was better and choose one myself. Didn't know how to work to github workflow so didn't put that to review first. Sorry for the trouble.

Can I replace the changes here for review with that instead?

@pazos
Copy link
Member

pazos commented Sep 23, 2023

@hugleo:

first see my two comments above.

Can I replace the changes here for review with that instead?

Yes, or open a new PR.
Better if you can do it on two different commits.

One that reverts a9d36a0 and other that reapplies the changes there for android 13.

We most likely want to keep the changes here for a few weeks, since any stuff that reaches the main repo will cause a lot of noise on bug reports.

In a couple of days I'll have my dev machine ready to poke a bit with android, something that I didn't seriously for the last couple of years.

will delve into it some other way some other time.
forget to put the changes also in this branch
@pazos
Copy link
Member

pazos commented Dec 17, 2023

superseeded by #439

@pazos pazos closed this Dec 17, 2023
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.

4 participants