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

Android WebView lacks several features due to inability to subclass #1020

Open
paulproteus opened this issue Aug 11, 2020 · 6 comments
Open
Labels
android The issue relates to Android mobile support. bug A crash or error in behavior.

Comments

@paulproteus
Copy link
Contributor

Describe the bug
Currently, Android WebView lacks an onload handler.

To Reproduce
Steps to reproduce the behavior:

  1. Create a Toga app that uses WebView
  2. Add an onload handler (as the webview demo does!)

Expected behavior

Expect the onload handler to be fired when the page finishes loading.

Rationale

Android's WebView implementation needs an instance the WebViewClient class, or an instance of a WebViewClient subclass, in order to send notifications of pages completing loading.

rubicon-java doesn't support subclassing Java classes.

If it did, the code might look like:

from ..libs import android_widgets

class TogaWebViewClient(android_widgets.WebViewClient):
    def __init__(self, on_load=None):
        super().__init__()
        self._on_load = on_load

    class onPageFinished(self, _webview, _url):
        if self._on_load:
            self._on_load()

This depends on beeware/rubicon-java#25 (or something like it).

@paulproteus paulproteus added the bug A crash or error in behavior. label Aug 11, 2020
@paulproteus paulproteus mentioned this issue Aug 11, 2020
4 tasks
@beeware beeware deleted a comment from sarthaksharma058439 Aug 19, 2020
@xloem
Copy link

xloem commented Dec 19, 2020

The blocking issue here is a big project and it's appropriate to make helper java classes to work around issues until the blocking issue is resolved.

@xloem
Copy link

xloem commented Jan 8, 2021

One possible solution to issues like this would be to add wrappers for all the android api's abstract base classes to the android template. The wrappers would take java interfaces in their constructors and pass off their abstract method calls to the interfaces. This solution could be implemented by somebody who didn't know the project well.

@freakboy3742
Copy link
Member

@xloem Yes, this is a possible approach; however, it's an approach that I'd rather avoid. Baking wrappers for specific classes into the Android template would make the template GUI toolkit specific - you would only be able to extend the classes that we've pre-declared. Consider the case of wrapping a third party Android library, or an Android base library that Toga doesn't require.

I would be more inclined to support a metadata-based approach which allows toga-android to provide/include a jar file that contains class implementations that can be wrapped, and metadata in the toga-android wheel/egg-info that briefcase can use to obtain/install that jar file as needed in a generic Android template.

Better still would be a mechanism for that lets the Android template, or a template post-processing step, roll out the base classes based on a metadata declaration in the toga-android package. Post-processing is a feature that has been requested in other contexts (e.g., to do asset processing), so this could be a natural fit.

@xloem
Copy link

xloem commented Jan 24, 2021

Thanks for your response. In Kivy their solution was to expose in a variable the android java source path, so files could be copied into it during build. Natural fits are always good though, you want as many needed birds with your small stone as you can get =)

@freakboy3742 freakboy3742 added up-for-grabs android The issue relates to Android mobile support. labels Mar 29, 2022
@mhsmith mhsmith changed the title Android WebView lacks onload handler due to rubicon-java inability to subclass Android WebView lacks onload handler due to inability to subclass Jun 12, 2023
@mhsmith
Copy link
Member

mhsmith commented Jun 12, 2023

BeeWare now uses Chaquopy on Android, which does support subclassing Java classes in Python code (beeware/rubicon-java#75 (reply in thread)). However, it still requires some configuration in the build.gradle file to list which modules the classes can be found in. I've created chaquo/chaquopy#881 to track the idea of moving this to the package metadata.

@mhsmith
Copy link
Member

mhsmith commented Nov 14, 2023

This also affects the WebChromeClient class, which is required to show JavaScript alert dialogs, or file upload dialogs (#2173). The latter dialog could probably be shared with the general file picker dialog (#1158).

@mhsmith mhsmith changed the title Android WebView lacks onload handler due to inability to subclass Android WebView lacks several features due to inability to subclass Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android The issue relates to Android mobile support. bug A crash or error in behavior.
Projects
None yet
Development

No branches or pull requests

4 participants