Skip to content
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
19 changes: 15 additions & 4 deletions pythonforandroid/bootstraps/sdl2/build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,25 @@ def make_package(args):
service = True

service_names = []
for entrypoint in args.services:
name, entrypoint = entrypoint.split(":", 1)
for sid, spec in enumerate(args.services):
spec = spec.split(':')
name = spec[0]
entrypoint = spec[1]
options = spec[2:]

foreground = False
if 'foreground' in options:
foreground = True

service_names.append(name)
render(
'Service.tmpl.java',
'src/{}/Service{}.java'.format(args.package.replace(".", "/"), name.capitalize()),
name=name,
entrypoint=entrypoint,
args=args
args=args,
foreground=foreground,
service_id=sid + 1,
)

render(
Expand Down Expand Up @@ -427,7 +437,8 @@ def parse_args(args=None):
ap.add_argument('--with-billing', dest='billing_pubkey',
help='If set, the billing service will be added (not implemented)')
ap.add_argument('--service', dest='services', action='append',
help='Declare a new service entrypoint: NAME:PATH_TO_PY')
help='Declare a new service entrypoint: '
'NAME:PATH_TO_PY[:foreground]')
ap.add_argument('--add-source', dest='extra_source_dirs', action='append',
help='Include additional source dirs in Java build')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,25 @@ public int onStartCommand(Intent intent, int flags, int startId) {
pythonThread = new Thread(this);
pythonThread.start();

if (this.canDisplayNotification()) {
doStartForeground(extras);

return START_NOT_STICKY;
}

protected void doStartForeground(Bundle extras) {
if (canDisplayNotification()) {
String serviceTitle = extras.getString("serviceTitle");
String serviceDescription = extras.getString("serviceDescription");

Context context = getApplicationContext();
Notification notification = new Notification(context.getApplicationInfo().icon,
serviceTitle,
System.currentTimeMillis());
serviceTitle, System.currentTimeMillis());
Intent contextIntent = new Intent(context, PythonActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent);
startForeground(1, notification);
}

return START_NOT_STICKY;
}

@Override
Expand All @@ -91,7 +94,7 @@ public void onDestroy() {

@Override
public void run(){
PythonUtil.loadLibraries(getFilesDir());
PythonUtil.loadLibraries(getFilesDir());
this.mService = this;
nativeStart(
androidPrivate, androidArgument,
Expand Down
14 changes: 12 additions & 2 deletions pythonforandroid/bootstraps/sdl2/build/templates/Service.tmpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

import android.content.Intent;
import android.content.Context;
import android.app.Notification;
import android.app.PendingIntent;
import android.os.Bundle;
import org.kivy.android.PythonService;
import org.kivy.android.PythonActivity;


public class Service{{ name|capitalize }} extends PythonService {
@Override
public boolean canDisplayNotification() {
return false;
protected void doStartForeground(Bundle extras) {
Context context = getApplicationContext();
Notification notification = new Notification(context.getApplicationInfo().icon,
"{{ args.name }}", System.currentTimeMillis());
Intent contextIntent = new Intent(context, PythonActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, "{{ args.name }}", "{{ name| capitalize }}", pIntent);
startForeground({{ service_id }}, notification);
}

static public void start(Context ctx, String pythonServiceArgument) {
Expand Down