Skip to content

Commit

Permalink
added --activity-class-name parameters and make possible to use custo…
Browse files Browse the repository at this point in the history
…m PythonActivity java class
  • Loading branch information
vesellov committed Jun 28, 2020
1 parent 1b3b0e3 commit c61304a
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 18 deletions.
2 changes: 2 additions & 0 deletions pythonforandroid/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ def __init__(self):
self.local_recipes = None
self.copy_libs = False

self.activity_class_name = u'org.kivy.android.PythonActivity'

# this list should contain all Archs, it is pruned later
self.archs = (
ArchARM(self),
Expand Down
2 changes: 2 additions & 0 deletions pythonforandroid/recipes/android/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def prebuild_arch(self, arch):
'PY2': 0,
'JAVA_NAMESPACE': java_ns,
'JNI_NAMESPACE': jni_ns,
'ACTIVITY_CLASS_NAME': self.ctx.activity_class_name,
'ACTIVITY_CLASS_NAMESPACE': self.ctx.activity_class_name.replace('.', '/'),
}

# create config files for Cython, C and Python
Expand Down
4 changes: 2 additions & 2 deletions pythonforandroid/recipes/android/src/android/_android.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ api_version = autoclass('android.os.Build$VERSION').SDK_INT
version_codes = autoclass('android.os.Build$VERSION_CODES')


python_act = autoclass(JAVA_NAMESPACE + u'.PythonActivity')
python_act = autoclass(ACTIVITY_CLASS_NAME)
Rect = autoclass(u'android.graphics.Rect')
mActivity = python_act.mActivity
if mActivity:
Expand Down Expand Up @@ -293,7 +293,7 @@ def start_service(title="Background Service",
arg = ""

# Start service:
mActivity = autoclass('org.kivy.android.PythonActivity').mActivity
mActivity = autoclass(ACTIVITY_CLASS_NAME).mActivity
if as_foreground:
mActivity.start_service(
title, description, arg
Expand Down
8 changes: 4 additions & 4 deletions pythonforandroid/recipes/android/src/android/activity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from jnius import PythonJavaClass, autoclass, java_method
from android.config import JAVA_NAMESPACE, JNI_NAMESPACE
from android.config import ACTIVITY_CLASS_NAME, ACTIVITY_CLASS_NAMESPACE

_activity = autoclass(JAVA_NAMESPACE + '.PythonActivity').mActivity
_activity = autoclass(ACTIVITY_CLASS_NAME).mActivity

_callbacks = {
'on_new_intent': [],
Expand All @@ -10,7 +10,7 @@


class NewIntentListener(PythonJavaClass):
__javainterfaces__ = [JNI_NAMESPACE + '/PythonActivity$NewIntentListener']
__javainterfaces__ = [ACTIVITY_CLASS_NAMESPACE + '$NewIntentListener']
__javacontext__ = 'app'

def __init__(self, callback, **kwargs):
Expand All @@ -23,7 +23,7 @@ def onNewIntent(self, intent):


class ActivityResultListener(PythonJavaClass):
__javainterfaces__ = [JNI_NAMESPACE + '/PythonActivity$ActivityResultListener']
__javainterfaces__ = [ACTIVITY_CLASS_NAMESPACE + '$ActivityResultListener']
__javacontext__ = 'app'

def __init__(self, callback):
Expand Down
4 changes: 2 additions & 2 deletions pythonforandroid/recipes/android/src/android/broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Broadcast receiver bridge

from jnius import autoclass, PythonJavaClass, java_method
from android.config import JAVA_NAMESPACE, JNI_NAMESPACE
from android.config import JAVA_NAMESPACE, JNI_NAMESPACE, ACTIVITY_CLASS_NAME


class BroadcastReceiver(object):
Expand Down Expand Up @@ -74,5 +74,5 @@ def context(self):
if 'PYTHON_SERVICE_ARGUMENT' in environ:
PythonService = autoclass(JAVA_NAMESPACE + '.PythonService')
return PythonService.mService
PythonActivity = autoclass(JAVA_NAMESPACE + '.PythonActivity')
PythonActivity = autoclass(ACTIVITY_CLASS_NAME)
return PythonActivity.mActivity
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

from jnius import autoclass

from android.config import ACTIVITY_CLASS_NAME


def hide_loading_screen():
python_activity = autoclass('org.kivy.android.PythonActivity')
python_activity = autoclass(ACTIVITY_CLASS_NAME)
python_activity.removeLoadingScreen()
11 changes: 7 additions & 4 deletions pythonforandroid/recipes/android/src/android/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ def autoclass(item):
raise RuntimeError("pyjnius not available")


from android.config import ACTIVITY_CLASS_NAME, ACTIVITY_CLASS_NAMESPACE


class Permission:
ACCEPT_HANDOVER = "android.permission.ACCEPT_HANDOVER"
ACCESS_COARSE_LOCATION = "android.permission.ACCESS_COARSE_LOCATION"
Expand Down Expand Up @@ -431,7 +434,7 @@ class _onRequestPermissionsCallback(PythonJavaClass):
"""Callback class for registering a Python callback from
onRequestPermissionsResult in PythonActivity.
"""
__javainterfaces__ = ['org.kivy.android.PythonActivity$PermissionsCallback']
__javainterfaces__ = [ACTIVITY_CLASS_NAMESPACE + '$PermissionsCallback']
__javacontext__ = 'app'

def __init__(self, func):
Expand Down Expand Up @@ -484,7 +487,7 @@ class _RequestPermissionsManager:
def register_callback(cls):
"""Register Java callback for requestPermissions."""
cls._java_callback = _onRequestPermissionsCallback(cls.python_callback)
python_activity = autoclass('org.kivy.android.PythonActivity')
python_activity = autoclass(ACTIVITY_CLASS_NAME)
python_activity.addPermissionsCallback(cls._java_callback)

@classmethod
Expand All @@ -508,7 +511,7 @@ def request_permissions(cls, permissions, callback=None):
with cls._lock:
if not cls._java_callback:
cls.register_callback()
python_activity = autoclass('org.kivy.android.PythonActivity')
python_activity = autoclass(ACTIVITY_CLASS_NAME)
if not callback:
python_activity.requestPermissions(permissions)
else:
Expand Down Expand Up @@ -583,7 +586,7 @@ def check_permission(permission):
Returns:
bool: True if the app holds the permission given, False otherwise.
"""
python_activity = autoclass('org.kivy.android.PythonActivity')
python_activity = autoclass(ACTIVITY_CLASS_NAME)
result = bool(python_activity.checkCurrentPermission(
permission + ""
))
Expand Down
4 changes: 2 additions & 2 deletions pythonforandroid/recipes/android/src/android/runnable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
'''

from jnius import PythonJavaClass, java_method, autoclass
from android.config import JAVA_NAMESPACE
from android.config import ACTIVITY_CLASS_NAME

# Reference to the activity
_PythonActivity = autoclass(JAVA_NAMESPACE + '.PythonActivity')
_PythonActivity = autoclass(ACTIVITY_CLASS_NAME)

# Cache of functions table. In older Android versions the number of JNI references
# is limited, so by caching them we avoid running out.
Expand Down
6 changes: 4 additions & 2 deletions pythonforandroid/recipes/android/src/android/storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from jnius import autoclass, cast
import os

from android.config import JAVA_NAMESPACE, ACTIVITY_CLASS_NAME


Environment = autoclass('android.os.Environment')
File = autoclass('java.io.File')
Expand Down Expand Up @@ -28,11 +30,11 @@ def _get_activity():
"""
Retrieves the activity from `PythonActivity` fallback to `PythonService`.
"""
PythonActivity = autoclass('org.kivy.android.PythonActivity')
PythonActivity = autoclass(ACTIVITY_CLASS_NAME)
activity = PythonActivity.mActivity
if activity is None:
# assume we're running from the background service
PythonService = autoclass('org.kivy.android.PythonService')
PythonService = autoclass(JAVA_NAMESPACE + '.' + 'PythonService')
activity = PythonService.mService
return activity

Expand Down
7 changes: 7 additions & 0 deletions pythonforandroid/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ def __init__(self):
dest='local_recipes', default='./p4a-recipes',
help='Directory to look for local recipes')

generic_parser.add_argument(
'--activity-class-name',
dest='activity_class_name', default='org.kivy.android.PythonActivity',
help='The full java class name of the main activity')

generic_parser.add_argument(
'--java-build-tool',
dest='java_build_tool', default='auto',
Expand Down Expand Up @@ -704,6 +709,8 @@ def add_parser(subparsers, *args, **kwargs):
self.ctx.local_recipes = args.local_recipes
self.ctx.copy_libs = args.copy_libs

self.ctx.activity_class_name = args.activity_class_name

# Each subparser corresponds to a method
command = args.subparser_name.replace('-', '_')
getattr(self, command)(args)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def test_create(self):
'--bootstrap=service_only',
'--requirements=python3',
'--dist-name=test_toolchain',
'--activity-class-name=abc.myapp.android.CustomPythonActivity',
]
with patch_sys_argv(argv), mock.patch(
'pythonforandroid.build.get_available_apis'
Expand All @@ -77,7 +78,8 @@ def test_create(self):
m_get_toolchain_versions.return_value = (['4.9'], True)
m_get_ndk_platform_dir.return_value = (
'/tmp/android-ndk/platforms/android-21/arch-arm', True)
ToolchainCL()
tchain = ToolchainCL()
assert tchain.ctx.activity_class_name == 'abc.myapp.android.CustomPythonActivity'
assert m_get_available_apis.call_args_list in [
[mock.call('/tmp/android-sdk')], # linux case
[mock.call('/private/tmp/android-sdk')] # macos case
Expand Down

0 comments on commit c61304a

Please sign in to comment.