From 7ad96b20750300ba5de4bd2448faa2b4bdc0e9e0 Mon Sep 17 00:00:00 2001 From: Ryan Pessa Date: Mon, 15 Feb 2016 14:51:06 -0600 Subject: [PATCH] add sticky services, auto-restart services, fix foreground option --- .../bootstraps/sdl2/build/build.py | 6 +-- .../src/org/kivy/android/PythonService.java | 48 ++++++++++++------- .../sdl2/build/templates/Service.tmpl.java | 14 ++++++ 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/pythonforandroid/bootstraps/sdl2/build/build.py b/pythonforandroid/bootstraps/sdl2/build/build.py index 89ff955ce1..aa818ed61b 100755 --- a/pythonforandroid/bootstraps/sdl2/build/build.py +++ b/pythonforandroid/bootstraps/sdl2/build/build.py @@ -311,9 +311,8 @@ def make_package(args): entrypoint = spec[1] options = spec[2:] - foreground = False - if 'foreground' in options: - foreground = True + foreground = 'foreground' in options + sticky = 'sticky' in options service_names.append(name) render( @@ -323,6 +322,7 @@ def make_package(args): entrypoint=entrypoint, args=args, foreground=foreground, + sticky=sticky, service_id=sid + 1, ) diff --git a/pythonforandroid/bootstraps/sdl2/build/src/org/kivy/android/PythonService.java b/pythonforandroid/bootstraps/sdl2/build/src/org/kivy/android/PythonService.java index 8e29a9eee8..f8dde3e0d2 100644 --- a/pythonforandroid/bootstraps/sdl2/build/src/org/kivy/android/PythonService.java +++ b/pythonforandroid/bootstraps/sdl2/build/src/org/kivy/android/PythonService.java @@ -29,12 +29,23 @@ public class PythonService extends Service implements Runnable { private String serviceEntrypoint; // Argument to pass to Python code, private String pythonServiceArgument; - public static Service mService = null; + public static PythonService mService = null; + private Intent startIntent = null; + + private boolean autoRestartService = false; + + public void setAutoRestartService(boolean restart) { + autoRestartService = restart; + } public boolean canDisplayNotification() { return true; } + public int startType() { + return START_NOT_STICKY; + } + @Override public IBinder onBind(Intent arg0) { return null; @@ -52,6 +63,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { return START_NOT_STICKY; } + startIntent = intent; Bundle extras = intent.getExtras(); androidPrivate = extras.getString("androidPrivate"); androidArgument = extras.getString("androidArgument"); @@ -64,31 +76,35 @@ public int onStartCommand(Intent intent, int flags, int startId) { pythonThread = new Thread(this); pythonThread.start(); - doStartForeground(extras); + if (canDisplayNotification()) { + doStartForeground(extras); + } - return START_NOT_STICKY; + return startType(); } 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()); - Intent contextIntent = new Intent(context, PythonActivity.class); - PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent); - startForeground(1, notification); - } + String serviceTitle = extras.getString("serviceTitle"); + String serviceDescription = extras.getString("serviceDescription"); + + Context context = getApplicationContext(); + Notification notification = new Notification(context.getApplicationInfo().icon, + serviceTitle, System.currentTimeMillis()); + Intent contextIntent = new Intent(context, PythonActivity.class); + PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent); + startForeground(1, notification); } @Override public void onDestroy() { super.onDestroy(); pythonThread = null; + if (autoRestartService && startIntent != null) { + Log.v("python service", "service restart requested"); + startService(startIntent); + } Process.killProcess(Process.myPid()); } diff --git a/pythonforandroid/bootstraps/sdl2/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/sdl2/build/templates/Service.tmpl.java index 12e770b572..bf87996212 100644 --- a/pythonforandroid/bootstraps/sdl2/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/sdl2/build/templates/Service.tmpl.java @@ -10,6 +10,20 @@ public class Service{{ name|capitalize }} extends PythonService { + {% if sticky %} + @Override + public int startType() { + return START_STICKY; + } + {% endif %} + + {% if not foreground %} + @Override + public boolean canDisplayNotification() { + return false; + } + {% endif %} + @Override protected void doStartForeground(Bundle extras) { Context context = getApplicationContext();