From dc9a7eb7a3914de6f54b0627b3e08402b82557f2 Mon Sep 17 00:00:00 2001 From: Paul Brussee Date: Tue, 19 Jul 2016 14:49:36 +0200 Subject: [PATCH 1/9] pep8 --- .../bootstraps/service_only/__init__.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pythonforandroid/bootstraps/service_only/__init__.py b/pythonforandroid/bootstraps/service_only/__init__.py index 4a00e78c73..ba9e7d85fc 100644 --- a/pythonforandroid/bootstraps/service_only/__init__.py +++ b/pythonforandroid/bootstraps/service_only/__init__.py @@ -1,10 +1,12 @@ -from pythonforandroid.toolchain import Bootstrap, shprint, current_directory, info, warning, ArchARM, info_main -from os.path import join, exists, curdir, abspath -from os import walk import glob +from os import walk +from os.path import join, exists, curdir, abspath import sh +from pythonforandroid.toolchain import Bootstrap, shprint, current_directory, info, warning, ArchARM, info_main + class ServiceOnlyBootstrap(Bootstrap): + name = 'service_only' recipe_depends = ['genericndkbuild', ('python2', 'python3crystax')] @@ -62,7 +64,7 @@ def run_distribute(self): # AND: Copylibs stuff should go here if exists(join('libs', arch.arch, 'libpymodules.so')): shprint(sh.mv, join('libs', arch.arch, 'libpymodules.so'), 'private/') - shprint(sh.cp, join('python-install', 'include' , 'python2.7', 'pyconfig.h'), join('private', 'include', 'python2.7/')) + shprint(sh.cp, join('python-install', 'include', 'python2.7', 'pyconfig.h'), join('private', 'include', 'python2.7/')) info('Removing some unwanted files') shprint(sh.rm, '-f', join('private', 'lib', 'libpython2.7.so')) @@ -118,4 +120,5 @@ def run_distribute(self): self.fry_eggs(site_packages_dir) super(ServiceOnlyBootstrap, self).run_distribute() -bootstrap = ServiceOnlyBootstrap() \ No newline at end of file + +bootstrap = ServiceOnlyBootstrap() From b76fe2219705320744366b8f1119dc39c56bb143 Mon Sep 17 00:00:00 2001 From: Paul Brussee Date: Tue, 19 Jul 2016 14:49:59 +0200 Subject: [PATCH 2/9] change service icon option --- .../src/org/kivy/android/PythonService.java | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java b/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java index a9bc8c9a90..f52ed74b00 100644 --- a/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java +++ b/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java @@ -5,6 +5,7 @@ import android.app.Service; import android.content.Context; import android.content.Intent; +import android.content.pm.ApplicationInfo; import android.os.Bundle; import android.os.IBinder; import android.os.Process; @@ -14,7 +15,6 @@ public class PythonService extends Service implements Runnable { private static String TAG = PythonService.class.getSimpleName(); - public static PythonService mService = null; /** * Intent that started the service */ @@ -31,15 +31,8 @@ public class PythonService extends Service implements Runnable { private String serviceEntrypoint; private String pythonServiceArgument; - private boolean autoRestartService = false; - - public void setAutoRestartService(boolean restart) { - autoRestartService = restart; - } - - public boolean canDisplayNotification() { - return true; - } + protected boolean autoRestartService = false; + protected boolean startForeground = true; public int startType() { return START_NOT_STICKY; @@ -88,7 +81,7 @@ public int onStartCommand(Intent intent, int flags, int startId) { pythonThread = new Thread(this); pythonThread.start(); - if (canDisplayNotification()) { + if (startForeground) { doStartForeground(extras); } @@ -96,14 +89,16 @@ public int onStartCommand(Intent intent, int flags, int startId) { } protected void doStartForeground(Bundle extras) { - String serviceTitle = extras.getString("serviceTitle"); - String serviceDescription = extras.getString("serviceDescription"); + Context appContext = getApplicationContext(); + ApplicationInfo appInfo = appContext.getApplicationInfo(); - Context context = getApplicationContext(); + String serviceTitle = extras.getString("serviceTitle", TAG); + String serviceDescription = extras.getString("serviceDescription", ""); + int serviceIconId = extras.getInt("serviceIconId", appInfo.icon); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) - .setSmallIcon(context.getApplicationInfo().icon) + .setSmallIcon(serviceIconId) .setContentTitle(serviceTitle) .setContentText(serviceDescription); @@ -136,7 +131,6 @@ public void onDestroy() { @Override public void run() { PythonUtil.loadLibraries(getFilesDir()); - mService = this; nativeStart(androidPrivate, androidArgument, serviceEntrypoint, pythonName, pythonHome, pythonPath, pythonServiceArgument); stopSelf(); From 5a8bfe9d963691b304335d12d4a7afd15d652b00 Mon Sep 17 00:00:00 2001 From: Paul Brussee Date: Fri, 22 Jul 2016 00:48:20 +0200 Subject: [PATCH 3/9] consistent arg naming --- .../bootstraps/service_only/build/jni/src/start.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pythonforandroid/bootstraps/service_only/build/jni/src/start.c b/pythonforandroid/bootstraps/service_only/build/jni/src/start.c index 37a9b1d90f..7251cdd132 100644 --- a/pythonforandroid/bootstraps/service_only/build/jni/src/start.c +++ b/pythonforandroid/bootstraps/service_only/build/jni/src/start.c @@ -279,7 +279,7 @@ int main(int argc, char *argv[]) { } JNIEXPORT void JNICALL Java_org_kivy_android_PythonService_nativeStart( - JNIEnv *env, jobject thiz, jstring j_android_private, + JNIEnv *env, jobject j_this, jstring j_android_private, jstring j_android_argument, jstring j_service_entrypoint, jstring j_python_name, jstring j_python_home, jstring j_python_path, jstring j_arg) { @@ -296,7 +296,8 @@ JNIEXPORT void JNICALL Java_org_kivy_android_PythonService_nativeStart( (*env)->GetStringUTFChars(env, j_python_home, &iscopy); const char *python_path = (*env)->GetStringUTFChars(env, j_python_path, &iscopy); - const char *arg = (*env)->GetStringUTFChars(env, j_arg, &iscopy); + const char *arg = + (*env)->GetStringUTFChars(env, j_arg, &iscopy); setenv("ANDROID_PRIVATE", android_private, 1); setenv("ANDROID_ARGUMENT", android_argument, 1); From 5a99afabb5100fea351262e8846880c4768b8958 Mon Sep 17 00:00:00 2001 From: Paul Brussee Date: Fri, 22 Jul 2016 01:02:50 +0200 Subject: [PATCH 4/9] install *.gradle build scripts with bootstraps --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 91bd0edd20..e91d5398e8 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,8 @@ def recursively_include(results, directory, patterns): '*.mk', '*.jam', ]) recursively_include(package_data, 'pythonforandroid/bootstraps', ['*.properties', '*.xml', '*.java', '*.tmpl', '*.txt', '*.png', - '*.mk', '*.c', '*.h', '*.py', '*.sh', '*.jpg', '*.aidl', ]) + '*.mk', '*.c', '*.h', '*.py', '*.sh', '*.jpg', '*.aidl', + '*.gradle', ]) recursively_include(package_data, 'pythonforandroid/bootstraps', ['sdl-config', ]) recursively_include(package_data, 'pythonforandroid/bootstraps/webview', From f0a35d6a753457c4b500abd64902cc5112836517 Mon Sep 17 00:00:00 2001 From: Paul Brussee Date: Fri, 29 Jul 2016 17:26:04 +0200 Subject: [PATCH 5/9] service binder interface --- .../src/org/kivy/android/PythonService.java | 29 +++++++++++-------- .../build/templates/Service.tmpl.java | 28 ++++++++++++++++-- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java b/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java index f52ed74b00..132379f6a7 100644 --- a/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java +++ b/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java @@ -31,13 +31,18 @@ public class PythonService extends Service implements Runnable { private String serviceEntrypoint; private String pythonServiceArgument; - protected boolean autoRestartService = false; - protected boolean startForeground = true; - - public int startType() { + public int getStartType() { return START_NOT_STICKY; } + public boolean getStartForeground() { + return false; + } + + public boolean getAutoRestart() { + return false; + } + /** * {@inheritDoc} */ @@ -81,11 +86,11 @@ public int onStartCommand(Intent intent, int flags, int startId) { pythonThread = new Thread(this); pythonThread.start(); - if (startForeground) { + if (getStartForeground()) { doStartForeground(extras); } - return startType(); + return getStartType(); } protected void doStartForeground(Bundle extras) { @@ -118,7 +123,7 @@ protected void doStartForeground(Bundle extras) { public void onDestroy() { super.onDestroy(); pythonThread = null; - if (autoRestartService && startIntent != null) { + if (getAutoRestart() && startIntent != null) { Log.v(TAG, "Service restart requested"); startService(startIntent); } @@ -131,8 +136,8 @@ public void onDestroy() { @Override public void run() { PythonUtil.loadLibraries(getFilesDir()); - nativeStart(androidPrivate, androidArgument, serviceEntrypoint, - pythonName, pythonHome, pythonPath, pythonServiceArgument); + nativeStart(androidPrivate, androidArgument, serviceEntrypoint, pythonName, pythonHome, + pythonPath, pythonServiceArgument); stopSelf(); } @@ -145,8 +150,8 @@ public void run() { * @param pythonPath Python path * @param pythonServiceArgument Argument to pass to Python code */ - public static native void nativeStart(String androidPrivate, - String androidArgument, String serviceEntrypoint, - String pythonName, String pythonHome, String pythonPath, + public static native void nativeStart(String androidPrivate, String androidArgument, + String serviceEntrypoint, String pythonName, + String pythonHome, String pythonPath, String pythonServiceArgument); } diff --git a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java index 3523ecc114..d5e5d0fdd6 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java @@ -4,14 +4,18 @@ import android.content.Context; import org.kivy.android.PythonService; - public class Service{{ name|capitalize }} extends PythonService { + /** + * Binder given to clients + */ + private final IBinder mBinder = new LocalBinder(); + {% if sticky %} /** * {@inheritDoc} */ @Override - public int startType() { + public int getStartType() { return START_STICKY; } {% endif %} @@ -21,7 +25,7 @@ public int startType() { * {@inheritDoc} */ @Override - public boolean canDisplayNotification() { + public boolean getStartForeground() { return false; } {% endif %} @@ -46,4 +50,22 @@ public static void stop(Context ctx) { ctx.stopService(intent); } + /** + * 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. + */ + public class Service{{ name|capitalize }}Binder extends Binder { + Service{{ name|capitalize }} getService() { + // Return this instance of Service{{ name|capitalize }} so clients can call public methods + return Service{{ name|capitalize }}.this; + } + } + + /** + * {@inheritDoc} + */ + @Override + public IBinder onBind(Intent intent) { + return mBinder; + } } From 5f40f0f6f0e359c745759ff5316c6d384e7250b1 Mon Sep 17 00:00:00 2001 From: Paul Brussee Date: Fri, 29 Jul 2016 17:28:02 +0200 Subject: [PATCH 6/9] flipped default value --- .../bootstraps/service_only/build/templates/Service.tmpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java index d5e5d0fdd6..a3e63aa84e 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java @@ -20,13 +20,13 @@ public int getStartType() { } {% endif %} - {% if not foreground %} + {% if foreground %} /** * {@inheritDoc} */ @Override public boolean getStartForeground() { - return false; + return true; } {% endif %} From e5b9c004e72c0900eb6020a7072dd2432f4e18f4 Mon Sep 17 00:00:00 2001 From: Paul Brussee Date: Fri, 29 Jul 2016 17:29:16 +0200 Subject: [PATCH 7/9] generate binder name --- .../bootstraps/service_only/build/templates/Service.tmpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java index a3e63aa84e..e445ab27e3 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java @@ -8,7 +8,7 @@ public class Service{{ name|capitalize }} extends PythonService { /** * Binder given to clients */ - private final IBinder mBinder = new LocalBinder(); + private final IBinder mBinder = new Service{{ name|capitalize }}Binder(); {% if sticky %} /** From a3af3134f04dfea3d832402ab52b13459a000893 Mon Sep 17 00:00:00 2001 From: Paul Brussee Date: Fri, 29 Jul 2016 17:35:02 +0200 Subject: [PATCH 8/9] force onBind from abstract class --- .../build/src/org/kivy/android/PythonService.java | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java b/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java index 132379f6a7..df4fe0d2c8 100644 --- a/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java +++ b/pythonforandroid/bootstraps/service_only/build/src/org/kivy/android/PythonService.java @@ -1,18 +1,16 @@ package org.kivy.android; -import android.app.Activity; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.os.Bundle; -import android.os.IBinder; import android.os.Process; import android.support.v4.app.NotificationCompat; import android.util.Log; -public class PythonService extends Service implements Runnable { +public abstract class PythonService extends Service implements Runnable { private static String TAG = PythonService.class.getSimpleName(); /** @@ -43,14 +41,6 @@ public boolean getAutoRestart() { return false; } - /** - * {@inheritDoc} - */ - @Override - public IBinder onBind(Intent intent) { - return null; - } - /** * {@inheritDoc} */ @@ -109,7 +99,7 @@ protected void doStartForeground(Bundle extras) { int NOTIFICATION_ID = 1; - Intent targetIntent = new Intent(this, Activity.class); + Intent targetIntent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); From 83394c72f3ec84d90c6a25a87d7287cff2754e15 Mon Sep 17 00:00:00 2001 From: Paul Brussee Date: Fri, 29 Jul 2016 17:35:35 +0200 Subject: [PATCH 9/9] onBind template --- .../build/templates/Service.tmpl.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java index e445ab27e3..31c4183449 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java +++ b/pythonforandroid/bootstraps/service_only/build/templates/Service.tmpl.java @@ -50,6 +50,14 @@ public static void stop(Context ctx) { ctx.stopService(intent); } + /** + * {@inheritDoc} + */ + @Override + 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. @@ -60,12 +68,4 @@ public class Service{{ name|capitalize }}Binder extends Binder { return Service{{ name|capitalize }}.this; } } - - /** - * {@inheritDoc} - */ - @Override - public IBinder onBind(Intent intent) { - return mBinder; - } }