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

Prevent exit fullscreen mode from closing application #823

Merged
merged 5 commits into from
Apr 14, 2020
Merged

Prevent exit fullscreen mode from closing application #823

merged 5 commits into from
Apr 14, 2020

Conversation

Chuckytuh
Copy link
Contributor

Platforms affected

Android

Motivation and Context

This PR proposes a simple, quick, solution to overcome a problem with full screen mode that, when on and back button is pressed, the application is closed instead of exiting full screen.

For more details see #822 and also https://bugs.chromium.org/p/chromium/issues/detail?id=999212

Fixes #822

Description

The custom view provided by WebKit on SystemWebChromeClients's onShowCustomView method is wrapped in a custom FrameLayout in order to be able to grab key events and redirect them into SystemWebView's dispatchKeyEvent.

Given that the custom view is the one with the focus, that's the one handling input, for this reason, we can hijack those key events and redirect them so that they end up closing the custom view because, as stated on the linked chromium bug (#999212), the onHideCustomView method from the WebChromeClient subclass is not being executed for recent versions of Chrome, hence, not exiting full screen mode and instead, closing the MainActivity/ Application.

Testing

Manual tests on a Pixel (Android 7.1.2) and Pixel 2 XL (Android 10) with Chrome updated to the most recent version.

Checklist

  • I've run the tests to see all new and existing tests pass
  • I added automated test coverage as appropriate for this change
  • Commit is prefixed with (platform) if this change only applies to one platform (e.g. (android))
  • If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct keyword to close issues using keywords)
  • I've updated the documentation if necessary

Wraps the custom view in a FrameLayout in order
to capture key events and redirect them to SystemWebView's
dispatchKeyEvent.
@breautek
Copy link
Contributor

breautek commented Oct 9, 2019

full screen mode

When you say this, do you mean when the application has the FullScreen preference enabled? Or when you have a video player open and you hit the full screen button? Or both?

I'll try to test this tonight.

@itsvick
Copy link

itsvick commented Oct 15, 2019

@breautek Any update on this?
I have the same issue when pressing the fullscreen button from the video.

Copy link
Contributor

@breautek breautek left a comment

Choose a reason for hiding this comment

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

Tested 8.1.0 on chrome webview 77 and confirmed the issue appears, and confirmed that this PR fixes the issue with no obvious side effects.

I've also tested this on chrome webview 69 via emulator and it didn't exhibit the original issue.

@Chuckytuh
Copy link
Contributor Author

When you say this, do you mean when the application has the FullScreen preference enabled? Or when you have a video player open and you hit the full screen button? Or both?

Sorry for the late reply, I meant full screen mode on html video player.

@patchon
Copy link

patchon commented Oct 26, 2019

Hi,

I recently stumbled on this on an application I'm developing.
It uses a javascript video-player (plyr.js), and the problem is as described (back button from fullscreen exits the app).

I've applied the commit fdc4da5 and now backbutton works as expected.

Almost anyway :)

What happens is,

  • If I go fullscreen, then
  • Press backbutton, then
  • Press backbutton again
  • App exits (which it shouldn't since I have an handler registered that should popup a question if I really want to exit before exiting).

It works correctly though, if you do it like this

  • If I go fullscreen, then
  • Press backbutton, then
  • Touches somewhere in the app (on the background for example), then
  • Press backbutton,
  • Registered handler popups a question whether I want to exit or not (ie. it works as expected).

I have the following logs from logcat,
Works correctly

2019-10-26 16:53:32.633 4042-4042/? D/LightBarController: onNavigationVisibilityChanged mNavigationLight = false
2019-10-26 16:53:32.643 4042-4042/? D/StatusBar: Status bar WINDOW_STATE_SHOWING
2019-10-26 16:53:32.644 4042-4042/? D/OverviewProxyService: SystemUi flags: 2
2019-10-26 16:53:32.646 13826-22583/? D/TouchInteractionService: onSystemUiStateChanged# stateFlags: 2
2019-10-26 16:53:32.646 4042-4042/? D/OverviewProxyService: SystemUi flags: 2
2019-10-26 16:53:32.646 13826-22583/? D/TouchInteractionService: onSystemUiStateChanged# stateFlags: 2
2019-10-26 16:53:32.646 4042-4042/? D/NavigationBar: Navigation bar WINDOW_STATE_SHOWING
2019-10-26 16:53:32.647 4042-4042/? D/OverviewProxyService: SystemUi flags: 0
2019-10-26 16:53:32.647 13826-22583/? D/TouchInteractionService: onSystemUiStateChanged# stateFlags: 0
2019-10-26 16:53:33.121 4042-4042/? I/KeyButtonView: Back button event: ACTION_DOWN
    
    --------- beginning of system
2019-10-26 16:53:33.124 1277-5925/? D/VibratorService: vibrate from 'android', usageHint = 13
2019-10-26 16:53:33.172 4042-4042/? I/KeyButtonView: Back button event: ACTION_UP
2019-10-26 16:53:33.172 13826-22583/? D/TouchInteractionService: onBackAction#
2019-10-26 16:53:33.250 19090-19090/io.my-app D/SystemWebChromeClient: https://my-app/assets/js/navigation.js: Line 177 : Nothing to go back to, popping up exit dialog..
2019-10-26 16:46:55.697 18294-18314/io.my-app D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@b70400a[MainActivity]
2019-10-26 16:46:55.709 18294-18294/io.my-app W/Choreographer: Already have a pending vsync event.  There should only be one at a time.

Does not work correctly

2019-10-26 16:51:46.239 4042-4042/? D/LightBarController: onNavigationVisibilityChanged mNavigationLight = false
2019-10-26 16:51:46.260 4042-4042/? D/StatusBar: Status bar WINDOW_STATE_SHOWING
2019-10-26 16:51:46.262 4042-4042/? D/OverviewProxyService: SystemUi flags: 2
2019-10-26 16:51:46.262 13826-22583/? D/TouchInteractionService: onSystemUiStateChanged# stateFlags: 2
2019-10-26 16:51:46.263 4042-4042/? D/OverviewProxyService: SystemUi flags: 2
2019-10-26 16:51:46.263 13826-22583/? D/TouchInteractionService: onSystemUiStateChanged# stateFlags: 2
2019-10-26 16:51:46.263 4042-4042/? D/NavigationBar: Navigation bar WINDOW_STATE_SHOWING
2019-10-26 16:51:46.263 4042-4042/? D/OverviewProxyService: SystemUi flags: 0
2019-10-26 16:51:46.264 13826-22583/? D/TouchInteractionService: onSystemUiStateChanged# stateFlags: 0
2019-10-26 16:51:46.594 4042-4042/? I/KeyButtonView: Back button event: ACTION_DOWN
    
    --------- beginning of system
2019-10-26 16:51:46.597 1277-6554/? D/VibratorService: vibrate from 'android', usageHint = 13
2019-10-26 16:51:46.653 4042-4042/? I/KeyButtonView: Back button event: ACTION_UP
2019-10-26 16:51:46.654 13826-22583/? D/TouchInteractionService: onBackAction#
2019-10-26 16:51:46.661 1277-5925/? D/OpQuickReply: setQuickReplyResumed focusedApp AppWindowToken{9230f7c token=Token{f1f966f ActivityRecord{b3ce54e u0 com.teslacoilsw.launcher/.NovaLauncher t8082}}} pkgName com.teslacoilsw.launcher
2019-10-26 16:46:20.052 1277-4389/? D/OpSlaNetlinkHelper: frontPackageChanged: com.teslacoilsw.launcher uid:10136 pid:32020 | io.my-app uid:10298 pid:18294
2019-10-26 16:46:20.062 18294-18294/io.my-app D/CordovaActivity: Paused the activity.
2019-10-26 16:46:20.176 18294-18294/io.my-app D/CordovaActivity: Stopped the activity.
2019-10-26 16:46:20.177 18294-18294/io.my-app I/Surface: opservice is null false
2019-10-26 16:46:20.178 18294-18294/io.my-app D/CordovaActivity: CordovaActivity.onDestroy()
2019-10-26 16:46:20.178 18294-18294/io.my-app D/CordovaWebViewImpl: >>> loadUrl(about:blank)
2019-10-26 16:46:20.179 18294-18294/io.my-app W/cr_AwContents: WebView.destroy() called while WebView is still attached to window.
2019-10-26 16:46:20.196 18294-18294/io.my-app W/cr_AwContents: Application attempted to call on a destroyed WebView
    java.lang.Throwable
        at org.chromium.android_webview.AwContents.c(PG:3)
        at org.chromium.android_webview.AwContents.T(PG:1)
        at Io.loadingStateChanged(PG:1)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:336)
        at android.os.Looper.loop(Looper.java:174)
        at android.app.ActivityThread.main(ActivityThread.java:7682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

It seems like, when pressing the back button twice in a row (ie. not touching anything in the app between), it fires the fires the "stop activity" and then the "destroy activity", which is not the case when I press something in the app between the "back button presses".

Any ideas about this ?
Are you able to reproduce it ?

Copy link

@l-hwen l-hwen left a comment

Choose a reason for hiding this comment

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

Sorry, I followed the instructions to modify my file, but this still doesn't work for me.
Cordova-android@8.1.0
@breautek

Copy link
Member

@erisu erisu left a comment

Choose a reason for hiding this comment

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

Everything looked OK but there was some formatting that could be corrected. It seems the PR added some extra whitespace/newlines that were not needed or in the wrong place.

Could you correct these?

framework/src/org/apache/cordova/CordovaWebViewImpl.java Outdated Show resolved Hide resolved
framework/src/org/apache/cordova/CordovaWebViewImpl.java Outdated Show resolved Hide resolved
framework/src/org/apache/cordova/CordovaWebViewImpl.java Outdated Show resolved Hide resolved
@erisu erisu added this to the 9.0.0 milestone Jan 6, 2020
breautek and others added 3 commits April 13, 2020 21:13
Co-Authored-By: エリス <erisu@users.noreply.github.com>
Co-Authored-By: エリス <erisu@users.noreply.github.com>
Co-Authored-By: エリス <erisu@users.noreply.github.com>
@breautek breautek requested a review from erisu April 14, 2020 00:22
@timbru31
Copy link
Member

@breautek - Unfortunately, the newlines around line 520 (@Override) are still off.

@Chuckytuh
Copy link
Contributor Author

Chuckytuh commented Apr 14, 2020

Formatting should now be right

Copy link
Member

@timbru31 timbru31 left a comment

Choose a reason for hiding this comment

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

Thanks!

@timbru31 timbru31 merged commit 71f63d7 into apache:master Apr 14, 2020
@Chuckytuh Chuckytuh deleted the fix/fullscreenbackbutton branch April 14, 2020 18:57
@PhamNguyenNL
Copy link

PhamNguyenNL commented Apr 20, 2020

Could you please create a new release/tag for this? This is currently only fixed on the master branch.

@timbru31
Copy link
Member

Not an extra release, but cordova-android@9 will be released soonish.

@simion
Copy link

simion commented Apr 23, 2020

I need this fix too :) please release

@Hanzofm
Copy link

Hanzofm commented Apr 23, 2021

Hi @patchon,

For me still not working with cordova-android 9.0.0 where is merged this PR. Same problem of you with plyr.js.

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.

Back button event on full screen video closes application
10 participants