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: use Chaquopy syntax directly #1980

Closed
mhsmith opened this issue Jun 12, 2023 · 10 comments · Fixed by #2129
Closed

Android: use Chaquopy syntax directly #1980

mhsmith opened this issue Jun 12, 2023 · 10 comments · Fixed by #2129
Labels
android The issue relates to Android mobile support. good first issue Is this your first time contributing? This could be a good place to start!

Comments

@mhsmith
Copy link
Member

mhsmith commented Jun 12, 2023

The Android native classes are imported into toga_android.libs, and then from there into the files that use them. This doubles the amount of editing you have to do to import a new class.

Now that we've been using Chaquopy for 9 months, we might as well switch to using its import syntax directly.

At the same time, we can also:

This will not break compatibliity with any Briefcase templates released since the Chaquopy migration: they will provide the Rubicon compatibility layer, but Toga simply won't use it anymore.

However, projects which are using pre-Chaquopy Briefcase templates will break the next time they reinstall their requirements, because the briefcase-template pyproject.toml had no upper limit to its Toga version number at that time. They can work around this by adding an upper limit manually, or by upgrading to a newer version of Briefcase.

The current briefcase-template version does have a limit (~=0.3.1), so that will allow us to mark any future incompatible changes to Toga, and this will be the last time we break existing projects in this way.

Related: #1979.

@mhsmith mhsmith added the android The issue relates to Android mobile support. label Jun 12, 2023
@freakboy3742 freakboy3742 added the good first issue Is this your first time contributing? This could be a good place to start! label Jun 12, 2023
@freakboy3742
Copy link
Member

FWIW - I'm not overly concerned about compatibility with pre-Chaquopy Briefcase. We're still pre-1.0 on both Toga and Android; I don't think it's unreasonable to expect users to keep on top of changes in both projects when we're still in rapid development.

@mzdk100
Copy link

mzdk100 commented Sep 14, 2023

I want to know the relationship between Chaquopy and BeeWare. As far as I know, using free Chaquopy directly has a 5-minute running limit, and BeeWare also relies on it. Why is there no such limit?

@mhsmith
Copy link
Member Author

mhsmith commented Sep 14, 2023

Chaquopy has been free and open-source for over a year now, so there's no longer any time limit.

@mzdk100
Copy link

mzdk100 commented Sep 14, 2023

I see.
Then I have a small question. The value obtained using sys.platform on Android is 'Linux'. How can I obtain the specific platform value ('Android')?

@freakboy3742
Copy link
Member

sys.platform is what the CPython interpreter returns on Android. This has been the subject of some discussion with the CPython core team; it's possible that this might be modified to return linux-android at some point in the near future, but at least for now, it will always return linux.

If you want to know what platform you're on, Toga provides toga.platform.current_platform, which will return android.

@dgmouris
Copy link
Contributor

Hey I was wondering on the renaming of the imports do you want to essentially move the JavaClass(...) (shown below)

MainActivity = JavaClass("org/beeware/android/MainActivity")

into the files like so?

from .libs.activity import IPythonApp, MainActivity

Or would you like them defined in toga_android/libs/__init__.py?

@mhsmith
Copy link
Member Author

mhsmith commented Sep 19, 2023

The idea is to get rid of the libs package entirely.

For JavaClass, Chaquopy allows standard import syntax, so we can change this:

from .libs.activity import MainActivity

... to this:

from org.beeware.android import MainActivity

JavaInterface can also be replaced with a normal import, but the class statement will have to change slightly. For example, in widgets/button.py we would change this:

from ..libs.android.view import OnClickListener

class TogaOnClickListener(OnClickListener):

... to this:

from android.view import View
from java import dynamic_proxy

class TogaOnClickListener(dynamic_proxy(View.OnClickListener)):

@dgmouris
Copy link
Contributor

Hey @mhsmith,

Thank you for the help. I'm just going through these imports and I just wanted to make sure I'm going in the right direction.

For example:

from .libs.android import R__id
from .libs.android.view import ViewTreeObserver__OnGlobalLayoutListener
class LayoutListener(ViewTreeObserver__OnGlobalLayoutListener):
def __init__(self, window):

Would become the following:

from android import R
from android.view import ViewTreeObserver

from .container import Container


class LayoutListener(dynamic_proxy(ViewTreeObserver.OnGlobalLayoutListener)):

(I have it in a branch here https://github.com/dgmouris/toga/blob/5ec3edd2533de10b1665bfc9190f4c8a481d5c37/android/src/toga_android/window.py#L5-L11)

Thanks for your time.

@mhsmith
Copy link
Member Author

mhsmith commented Sep 21, 2023

Yes, everything on that branch looks fine so far. Please create a draft PR so we'll be notified of your progress.

You can test your changes using the testbed app as shown here. For example, after updating box.py, you can run:

cd testbed
briefcase run android --test -- tests/widgets/test_box.py

There are tests in the main branch for every class except Canvas, Window and App. I'm working on Canvas today, and I'll be working on Window and App next week. Although, every widget tests Window and App indirectly, so you can still use the testbed to verify them.

@dgmouris
Copy link
Contributor

Thanks @mhsmith I'll be testing all of the changes today, and noting the pieces that have no tests around it.

Much appreciated for the help on this.

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. good first issue Is this your first time contributing? This could be a good place to start!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants