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.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 {
    private static String TAG = PythonService.class.getSimpleName();

    public static PythonService mService = null;
    /**
     * Intent that started the service
     */
    private Intent startIntent = null;

    private Thread pythonThread = null;

    // Python environment variables
    private String androidPrivate;
    private String androidArgument;
    private String pythonName;
    private String pythonHome;
    private String pythonPath;
    private String serviceEntrypoint;
    private String pythonServiceArgument;

    private boolean autoRestartService = false;

    public void setAutoRestartService(boolean restart) {
        autoRestartService = restart;
    }

    public boolean canDisplayNotification() {
        return true;
    }

    public int startType() {
        return START_NOT_STICKY;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate() {
        Log.v(TAG, "Device: " + android.os.Build.DEVICE);
        Log.v(TAG, "Model: " + android.os.Build.MODEL);
        AssetExtract.extractAsset(getApplicationContext(), "private.mp3", getFilesDir());
        super.onCreate();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (pythonThread != null) {
            Log.v(TAG, "Service exists, do not start again");
            return START_NOT_STICKY;
        }
        startIntent = intent;

        Bundle extras = intent.getExtras();
        androidPrivate = extras.getString("androidPrivate");
        androidArgument = extras.getString("androidArgument");
        serviceEntrypoint = extras.getString("serviceEntrypoint");
        pythonName = extras.getString("pythonName");
        pythonHome = extras.getString("pythonHome");
        pythonPath = extras.getString("pythonPath");
        pythonServiceArgument = extras.getString("pythonServiceArgument");

        Log.v(TAG, "Starting Python thread");
        pythonThread = new Thread(this);
        pythonThread.start();

        if (canDisplayNotification()) {
            doStartForeground(extras);
        }

        return startType();
    }

    protected void doStartForeground(Bundle extras) {
        String serviceTitle = extras.getString("serviceTitle");
        String serviceDescription = extras.getString("serviceDescription");

        Context context = getApplicationContext();

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(context.getApplicationInfo().icon)
                        .setContentTitle(serviceTitle)
                        .setContentText(serviceDescription);

        int NOTIFICATION_ID = 1;

        Intent targetIntent = new Intent(this, Activity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        startForeground(NOTIFICATION_ID, builder.build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        pythonThread = null;
        if (autoRestartService && startIntent != null) {
            Log.v(TAG, "Service restart requested");
            startService(startIntent);
        }
        Process.killProcess(Process.myPid());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void run() {
        PythonUtil.loadLibraries(getFilesDir());
        mService = this;
        nativeStart(androidPrivate, androidArgument, serviceEntrypoint,
                pythonName, pythonHome, pythonPath, pythonServiceArgument);
        stopSelf();
    }

    /**
     * @param androidPrivate        Directory for private files
     * @param androidArgument       Android path
     * @param serviceEntrypoint     Python file to execute first
     * @param pythonName            Python name
     * @param pythonHome            Python home
     * @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,
                                          String pythonServiceArgument);
}

@@ -166,32 +166,75 @@
 content.Intent;%0A
+import android.content.pm.ApplicationInfo;%0A
 import android.o
@@ -512,57 +512,8 @@
 );%0A%0A
-    public static PythonService mService = null;%0A
     
@@ -931,21 +931,23 @@
 %0A%0A    pr
-ivate
+otected
  boolean
@@ -979,55 +979,30 @@
 se;%0A
-%0A
     p
-ublic void setAutoRestartService(
+rotected 
 boolean 
 rest
@@ -1001,136 +1001,31 @@
 ean 
-re
 start
-) %7B%0A        autoRestartService = restart;%0A    %7D%0A%0A    public boolean canDisplayNotification() %7B%0A        return true;%0A    %7D
+Foreground = true;
 %0A%0A  
@@ -2432,32 +2432,23 @@
 if (
-canDisplayNotification()
+startForeground
 ) %7B%0A
@@ -2582,24 +2582,146 @@
 e extras) %7B%0A
+        Context appContext = getApplicationContext();%0A        ApplicationInfo appInfo = appContext.getApplicationInfo();%0A%0A
         Stri
@@ -2769,16 +2769,21 @@
 ceTitle%22
+, TAG
 );%0A     
@@ -2850,28 +2850,31 @@
 ription%22
+, %22%22
 );%0A
-%0A
         
 Context 
@@ -2869,48 +2869,71 @@
     
-Context context = getApplicationContext(
+int serviceIconId = extras.getInt(%22serviceIconId%22, appInfo.icon
 );%0A%0A
@@ -3072,41 +3072,21 @@
 con(
-context.getApplicationInfo().i
+serviceI
 con
+Id
 )%0A  
@@ -4014,33 +4014,8 @@
 ));%0A
-        mService = this;%0A
     

change service icon option