|
| 1 | +package org.renpy.android; |
| 2 | + |
| 3 | +import android.app.Service; |
| 4 | +import android.os.IBinder; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.content.Intent; |
| 7 | +import android.content.Context; |
| 8 | +import android.util.Log; |
| 9 | +import android.app.Notification; |
| 10 | +import android.app.PendingIntent; |
| 11 | +import android.os.Process; |
| 12 | +import android.R; |
| 13 | + |
| 14 | +public class PythonService extends Service implements Runnable { |
| 15 | + |
| 16 | + // Thread for Python code |
| 17 | + private Thread pythonThread = null; |
| 18 | + |
| 19 | + // Python environment variables |
| 20 | + private String androidPrivate; |
| 21 | + private String androidArgument; |
| 22 | + private String pythonHome; |
| 23 | + private String pythonPath; |
| 24 | + // Argument to pass to Python code, |
| 25 | + private String pythonServiceArgument; |
| 26 | + |
| 27 | + @Override |
| 28 | + public IBinder onBind(Intent arg0) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void onCreate() { |
| 34 | + super.onCreate(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public int onStartCommand(Intent intent, int flags, int startId) { |
| 39 | + Bundle extras = intent.getExtras(); |
| 40 | + androidPrivate = extras.getString("androidPrivate"); |
| 41 | + androidArgument = extras.getString("androidArgument") + "/service"; |
| 42 | + pythonHome = extras.getString("pythonHome"); |
| 43 | + pythonPath = extras.getString("pythonPath"); |
| 44 | + pythonServiceArgument = extras.getString("pythonServiceArgument"); |
| 45 | + String serviceTitle = extras.getString("serviceTitle"); |
| 46 | + String serviceDescription = extras.getString("serviceDescription"); |
| 47 | + |
| 48 | + pythonThread = new Thread(this); |
| 49 | + pythonThread.start(); |
| 50 | + |
| 51 | + Notification notification = new Notification(R.drawable.sym_def_app_icon, |
| 52 | + serviceTitle, |
| 53 | + System.currentTimeMillis()); |
| 54 | + Context context = getApplicationContext(); |
| 55 | + Intent contextIntent = new Intent(context, PythonActivity.class); |
| 56 | + PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent, |
| 57 | + PendingIntent.FLAG_UPDATE_CURRENT); |
| 58 | + notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent); |
| 59 | + startForeground(1, notification); |
| 60 | + |
| 61 | + return START_NOT_STICKY; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void onDestroy() { |
| 66 | + super.onDestroy(); |
| 67 | + Process.killProcess(Process.myPid()); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void run(){ |
| 72 | + System.loadLibrary("sdl"); |
| 73 | + System.loadLibrary("sdl_image"); |
| 74 | + System.loadLibrary("sdl_ttf"); |
| 75 | + System.loadLibrary("sdl_mixer"); |
| 76 | + System.loadLibrary("python2.7"); |
| 77 | + System.loadLibrary("application"); |
| 78 | + System.loadLibrary("sdl_main"); |
| 79 | + |
| 80 | + System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_io.so"); |
| 81 | + System.load(getFilesDir() + "/lib/python2.7/lib-dynload/unicodedata.so"); |
| 82 | + |
| 83 | + try { |
| 84 | + System.loadLibrary("sqlite3"); |
| 85 | + System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_sqlite3.so"); |
| 86 | + } catch(UnsatisfiedLinkError e) { |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_imaging.so"); |
| 91 | + System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_imagingft.so"); |
| 92 | + System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_imagingmath.so"); |
| 93 | + } catch(UnsatisfiedLinkError e) { |
| 94 | + } |
| 95 | + |
| 96 | + nativeStart(androidPrivate, androidArgument, pythonHome, pythonPath, |
| 97 | + pythonServiceArgument); |
| 98 | + } |
| 99 | + |
| 100 | + // Native part |
| 101 | + public static native void nativeStart(String androidPrivate, String androidArgument, |
| 102 | + String pythonHome, String pythonPath, |
| 103 | + String pythonServiceArgument); |
| 104 | + |
| 105 | +} |
0 commit comments