package org.renpy.android;

import android.app.Service;
import android.os.IBinder;
import android.os.Bundle;
import android.content.Intent;
import android.content.Context;
import android.util.Log;
import android.app.Notification;
import android.app.PendingIntent;
import android.os.Process; 
import android.R;

public class PythonService extends Service  implements Runnable {

    // Thread for Python code
    private Thread pythonThread = null;

    // Python environment variables
    private String androidPrivate;
    private String androidArgument;
    private String pythonHome;
    private String pythonPath;
    // Argument to pass to Python code,
    private String pythonServiceArgument;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle extras = intent.getExtras();
        androidPrivate = extras.getString("androidPrivate");
        androidArgument = extras.getString("androidArgument") + "/service";
        pythonHome = extras.getString("pythonHome");
        pythonPath = extras.getString("pythonPath");
        pythonServiceArgument = extras.getString("pythonServiceArgument");
        String serviceTitle = extras.getString("serviceTitle");
        String serviceDescription = extras.getString("serviceDescription");

        pythonThread = new Thread(this);
        pythonThread.start();

        Notification notification = new Notification(R.drawable.sym_def_app_icon,
                                                     serviceTitle,
                                                     System.currentTimeMillis());
        Context context = getApplicationContext();
        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);

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Process.killProcess(Process.myPid());
    }

    @Override
    public void run(){
        System.loadLibrary("sdl");
        System.loadLibrary("sdl_image");
        System.loadLibrary("sdl_ttf");
        System.loadLibrary("sdl_mixer");
        System.loadLibrary("python2.7");
        System.loadLibrary("application");
        System.loadLibrary("sdl_main");

        System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_io.so");
        System.load(getFilesDir() + "/lib/python2.7/lib-dynload/unicodedata.so");

        try {
        System.loadLibrary("sqlite3");
        System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_sqlite3.so");
        } catch(UnsatisfiedLinkError e) {
        }

        try {
        System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_imaging.so");
        System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_imagingft.so");
        System.load(getFilesDir() + "/lib/python2.7/lib-dynload/_imagingmath.so");
        } catch(UnsatisfiedLinkError e) {
        }

        nativeStart(androidPrivate, androidArgument, pythonHome, pythonPath,
                    pythonServiceArgument);
    }

    // Native part
    public static native void nativeStart(String androidPrivate, String androidArgument,
                                          String pythonHome, String pythonPath,
                                          String pythonServiceArgument);

}

@@ -934,24 +934,182 @@
  startId) %7B%0A
+        if (pythonThread != null) %7B%0A            Log.v(%22python service%22, %22service exists, do not start again%22);%0A            return START_NOT_STICKY;%0A        %7D%0A
         Bund
@@ -2399,24 +2399,53 @@
 nDestroy();%0A
+        pythonThread = null;%0A
         Proc

handle repeated starting attempts of Android service