Skip to content

Corrections for Fix bootstraps for webview and service_only (recently merged) #1586

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

Merged
merged 4 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pythonforandroid/bootstraps/common/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,11 @@ def make_package(args):
with open(args.intent_filters) as fd:
args.intent_filters = fd.read()

# if get_bootstrap_name() == "sdl2":
args.add_activity = args.add_activity or []
args.activity_launch_mode = args.activity_launch_mode or ''
if not args.add_activity:
args.add_activity = []

if not args.activity_launch_mode:
args.activity_launch_mode = ''

if args.extra_source_dirs:
esd = []
Expand Down Expand Up @@ -536,6 +538,9 @@ def parse_args(args=None):
ap.add_argument('--icon', dest='icon',
help=('A png file to use as the icon for '
'the application.'))
ap.add_argument('--service', dest='services', action='append',
help='Declare a new service entrypoint: '
'NAME:PATH_TO_PY[:foreground]')
if get_bootstrap_name() != "service_only":
ap.add_argument('--presplash', dest='presplash',
help=('A jpeg file to use as a screen while the '
Expand Down Expand Up @@ -564,10 +569,6 @@ def parse_args(args=None):
'https://developer.android.com/guide/'
'topics/manifest/'
'activity-element.html'))
else:
ap.add_argument('--service', dest='services', action='append',
help='Declare a new service entrypoint: '
'NAME:PATH_TO_PY[:foreground]')
ap.add_argument('--wakelock', dest='wakelock', action='store_true',
help=('Indicate if the application needs the device '
'to stay on'))
Expand Down Expand Up @@ -687,7 +688,7 @@ def _read_configuration():
if args.meta_data is None:
args.meta_data = []

if (not hasattr(args, 'services')) or args.services is None:
if args.services is None:
args.services = []
Copy link
Member

Choose a reason for hiding this comment

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

Now I'm thinking we may go even more concise with:

args.services = getattr(args, 'services', [])

What do you think?

Copy link

Choose a reason for hiding this comment

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

yes that looks exactly how the default value of getattr is supposed to be used like 👍

Copy link

Choose a reason for hiding this comment

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

oh wait, actually this was supposed to also apply if args.services is None, right? which it won't, so it's not the same thing

Copy link

Choose a reason for hiding this comment

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

This would work, but it looks uglier: args.services = getattr(args, 'services', None) or [] (or alternatively, args.services = getattr(args, 'services', []) or [] but that's super redundant)

Copy link
Member

Choose a reason for hiding this comment

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

True, the semantic would not be the same for the args.services is None use case. But is that even an use case with add_argument('--service', dest='services', action='append')? I'm really wondering

Copy link

Choose a reason for hiding this comment

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

hmm only if the default is wrong, I suppose. if we checked that the default has a proper value and never None then it would be fine


if args.try_system_python_compile:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package {{ args.package }};

import android.os.Binder;
import android.os.IBinder;
import android.content.Intent;
import android.content.Context;
import org.kivy.android.PythonService;
Expand Down Expand Up @@ -31,7 +33,7 @@ public boolean getStartForeground() {
{% endif %}

public static void start(Context ctx, String pythonServiceArgument) {
String argument = ctx.getFilesDir().getAbsolutePath();
String argument = ctx.getFilesDir().getAbsolutePath() + "/app";
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
intent.putExtra("androidPrivate", argument);
intent.putExtra("androidArgument", argument);
Expand All @@ -45,7 +47,7 @@ public static void start(Context ctx, String pythonServiceArgument) {
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
ctx.startService(intent);
}

public static void stop(Context ctx) {
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
ctx.stopService(intent);
Expand All @@ -58,7 +60,7 @@ public static void stop(Context ctx) {
public IBinder onBind(Intent intent) {
return mBinder;
}

/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
Expand Down
5 changes: 3 additions & 2 deletions testapps/setup_testapp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import find_packages

options = {'apk': {'debug': None,
'requirements': 'python2,genericndkbuild',
'requirements': 'python2,genericndkbuild,pyjnius',
'android-api': 27,
'ndk-api': 21,
'ndk-dir': '/home/asandy/android/crystax-ndk-10.3.2',
Expand All @@ -12,6 +12,7 @@
'bootstrap': 'service_only',
'permissions': ['INTERNET', 'VIBRATE'],
'arch': 'armeabi-v7a',
'service': 'time:p4atime.py',
}}

package_data = {'': ['*.py']}
Expand All @@ -21,7 +22,7 @@

setup(
name='testapp_service',
version='1.0',
version='1.1',
description='p4a service testapp',
author='Alexander Taylor',
author_email='[email protected]',
Expand Down
20 changes: 6 additions & 14 deletions testapps/testapp_service/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
print('main.py was successfully called')
print('Service Test App main.py was successfully called')

import sys
print('python version is: ', sys.version)
Expand All @@ -11,17 +11,9 @@
print(i, sqrt(i))

print('Just printing stuff apparently worked, trying a simple service')
import datetime, threading, time

next_call = time.time()


def service_timer():
global next_call
print('P4a datetime service: {}'.format(datetime.datetime.now()))
next_call = next_call + 1
threading.Timer(next_call - time.time(), service_timer).start()


print('Starting the service timer...')
service_timer()
from jnius import autoclass
service = autoclass('org.test.testapp_service.ServiceTime')
mActivity = autoclass('org.kivy.android.PythonActivity').mActivity
argument = 'test argument ok'
service.start(mActivity, argument)
19 changes: 19 additions & 0 deletions testapps/testapp_service/p4atime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import datetime
import threading
import time
from os import environ
argument = environ.get('PYTHON_SERVICE_ARGUMENT', '')
print('p4atime.py was successfully called with argument: "{}"'.format(argument))

next_call = time.time()


def service_timer():
global next_call
print('P4a datetime service: {}'.format(datetime.datetime.now()))
next_call = next_call + 1
threading.Timer(next_call - time.time(), service_timer).start()


print('Starting the service timer...')
service_timer()