package fi.cie.chiru.servicefusionar;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {
	private static final String TAG = "fi.cie.chiru.servicefusionar";
	private ServiceFusionSetup serviceFusionSetup;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
        serviceFusionSetup = new ServiceFusionSetup();
        serviceFusionSetup.run(this);
        
		File f = extractScript("scripts", "feedparser.py");
        //f = extractScript("scripts", "hello.py");
		
		f = extractScript("scripts", "sfkit.py");
		f = extractScript("scripts", "sftest_ylenews.py");
        
        Intent intent = buildStartInTerminalIntent(f); // /mnt/sdcard/sl4a/scripts/hello_world.py")); //scripts/sftest_ylenews.py")); //
        startActivity(intent);

	}
   
    private File extractScript(String folder, String filename)
    {
    	File f = new File("/mnt/sdcard/" + TAG + "/" + filename); // getCacheDir()+"/script.py"); new FileOutputStream(f); //
    	try
    	{
    		if (f.exists())
    		{
    			f.delete();
    			Log.d(TAG, "tiedosto poistettu");
    		}
    		/*else
    		{
    			f.createNewFile();
    		}*/

    		InputStream is = getAssets().open(folder + "/" + filename);
			int size = is.available();
			byte[] buffer = new byte[size];
			is.read(buffer);
			is.close();
			
			FileOutputStream fos = new FileOutputStream(f.getAbsolutePath());

			//FileOutputStream fos = openFileOutput("/mnt/sdcard/Temp/Scripts/script.py", MODE_WORLD_READABLE); //
			fos.write(buffer);
			fos.close();

    	}
    	catch (IOException e)
    	{
    		Log.d(TAG, e.getMessage());
    	}
    	
    	return f;
    }
    
    public static Intent buildStartInTerminalIntent(File script) {
	final ComponentName componentName = Constants.SL4A_SERVICE_LAUNCHER_COMPONENT_NAME;
    Intent intent = new Intent();
    intent.setComponent(componentName);
    intent.setAction(Constants.ACTION_LAUNCH_BACKGROUND_SCRIPT); //
    intent.putExtra(Constants.EXTRA_SCRIPT_PATH, script.getAbsolutePath());
    
    return intent;
    } // buildStartInTerminalIntent
    
   
    
    @Override
    protected void onDestroy() 
    {
        super.onStop();
        serviceFusionSetup.stopServer();
    }
    
    
}

@@ -2591,101 +2591,795 @@
    %0A
-   %0A    %0A    @Override%0A    protected void onDestroy() %0A    %7B%0A        super.onStop();
+%09@Override%0A%09protected void onRestart() %0A%09%7B%0A%09%09if (serviceFusionSetup != null)%0A%09%09%09serviceFusionSetup.onRestart(this);%0A%09%09super.onRestart();%0A%09%7D%0A%0A%09@Override%0A%09protected void onResume() %0A%09%7B%0A%09%09if (serviceFusionSetup != null)%0A%09%09%09serviceFusionSetup.onResume(this);%0A%09%09super.onResume();%0A%09%7D%0A%0A%09@Override%0A%09protected void onStart() %0A%09%7B%0A%09%09if (serviceFusionSetup != null)%0A%09%09%09serviceFusionSetup.onStart(this);%0A%09%09super.onStart();%0A%09%7D%0A%0A%09@Override%0A%09protected void onStop() %0A%09%7B%0A%09%09if (serviceFusionSetup != null)%0A%09%09%09serviceFusionSetup.onStop(this);%0A%09%09super.onStop();%0A%09%7D%0A%0A%09@Override%0A%09protected void onPause() %0A%09%7B%0A%09%09if (serviceFusionSetup != null)%0A%09%09%09serviceFusionSetup.onPause(this);%0A%09%09super.onPause();%0A%09%7D%0A    %0A    @Override%0A    protected void onDestroy() %0A    %7B%0A    %09if (serviceFusionSetup != null)
 %0A    
+%09%7B%0A%09
     
+%09
 serv
@@ -3403,24 +3403,97 @@
 opServer();%0A
+%09%09%09serviceFusionSetup.onDestroy(this);%0A    %09%7D%0A        super.onDestroy();%0A
     %7D%0A    %0A 

Add cleaning functionality to MainActivity.