|
| 1 | +import android.app.Service; |
| 2 | +import android.content.Context; |
| 3 | +import android.content.Intent; |
| 4 | +import android.os.Binder; |
| 5 | +import android.os.IBinder; |
| 6 | +import android.os.Process; |
| 7 | +import android.util.Log; |
| 8 | +import java.io.File; |
| 9 | +import org.kivy.android.PythonUtil; |
| 10 | + |
| 11 | + |
| 12 | +public class PythonBoundService extends Service implements Runnable { |
| 13 | + // Binder given to clients |
| 14 | + private final IBinder binder = new PythonBoundServiceBinder(); |
| 15 | + |
| 16 | + // Thread for Python code |
| 17 | + private Thread pythonThread = null; |
| 18 | + |
| 19 | + // Application root directory |
| 20 | + private String appRoot; |
| 21 | + |
| 22 | + // Python environment variables |
| 23 | + private String androidPrivate; |
| 24 | + private String androidArgument; |
| 25 | + private String pythonName; |
| 26 | + private String pythonHome; |
| 27 | + private String pythonPath; |
| 28 | + private String workerEntrypoint; |
| 29 | + |
| 30 | + /** |
| 31 | + * Class used for the client Binder. Because we know this service always |
| 32 | + * runs in the same process as its clients, we don't need to deal with IPC. |
| 33 | + */ |
| 34 | + public class PythonBoundServiceBinder extends Binder { |
| 35 | + PythonBoundService getService() { |
| 36 | + // Return this instance of LocalService so clients can call public methods |
| 37 | + return PythonBoundService.this; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public void setPythonName(String value) { |
| 42 | + pythonName = value; |
| 43 | + } |
| 44 | + |
| 45 | + public void setWorkerEntrypoint(String value) { |
| 46 | + workerEntrypoint = value; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public IBinder onBind(Intent intent) { |
| 51 | + return binder; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onCreate() { |
| 56 | + super.onCreate(); |
| 57 | + |
| 58 | + Context context = getApplicationContext(); |
| 59 | + appRoot = PythonUtil.getAppRoot(context); |
| 60 | + |
| 61 | + androidPrivate = appRoot; |
| 62 | + androidArgument = appRoot; |
| 63 | + pythonHome = appRoot; |
| 64 | + pythonPath = appRoot + ":" + appRoot + "/lib"; |
| 65 | + |
| 66 | + File appRootFile = new File(appRoot); |
| 67 | + PythonUtil.unpackData(context, "private", appRootFile, false); |
| 68 | + |
| 69 | + pythonThread = new Thread(this); |
| 70 | + pythonThread.start(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onDestroy() { |
| 75 | + super.onDestroy(); |
| 76 | + pythonThread = null; |
| 77 | + Process.killProcess(Process.myPid()); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void run() { |
| 82 | + File appRootFile = new File(appRoot); |
| 83 | + |
| 84 | + PythonUtil.loadLibraries( |
| 85 | + appRootFile, |
| 86 | + new File(getApplicationContext().getApplicationInfo().nativeLibraryDir) |
| 87 | + ); |
| 88 | + |
| 89 | + nativeStart( |
| 90 | + androidPrivate, androidArgument, |
| 91 | + workerEntrypoint, pythonName, |
| 92 | + pythonHome, pythonPath |
| 93 | + ); |
| 94 | + Log.d("python bound service", "Python thread terminating"); |
| 95 | + } |
| 96 | + |
| 97 | + // Native part |
| 98 | + public static native void nativeStart( |
| 99 | + String androidPrivate, String androidArgument, |
| 100 | + String workerEntrypoint, String pythonName, |
| 101 | + String pythonHome, String pythonPath |
| 102 | + ); |
| 103 | +} |
0 commit comments