package fi.cie.chiru.servicefusionar;

import android.os.Bundle;
import android.app.Activity;
import java.io.*;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.util.Log;

public class MainActivity extends Activity {
	private static final String TAG = "fi.cie.chiru.servicefusionar";
	private ServiceFusionSetup serviceFusionSetup;
//	private Intent scriptIntent;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		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");
//        
//		if (scriptIntent == null)
//		{
//	        scriptIntent = buildStartInTerminalIntent(f); // /mnt/sdcard/sl4a/scripts/hello_world.py")); //scripts/sftest_ylenews.py")); //
//	        startActivity(scriptIntent);
//		}

	}
   
    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 onRestart() 
	{
		if (serviceFusionSetup != null)
			serviceFusionSetup.onRestart(this);
		super.onRestart();
	}

	@Override
	protected void onResume() 
	{
		if (serviceFusionSetup != null)
			serviceFusionSetup.onResume(this);
		super.onResume();
	}

	@Override
	protected void onStart() 
	{
		if (serviceFusionSetup != null)
			serviceFusionSetup.onStart(this);
		super.onStart();
	}

	@Override
	protected void onStop() 
	{
		if (serviceFusionSetup != null)
			serviceFusionSetup.onStop(this);
		super.onStop();
	}

	@Override
	protected void onPause() 
	{
		if (serviceFusionSetup != null)
			serviceFusionSetup.onPause(this);
		super.onPause();
	}
    
    @Override
    protected void onDestroy() 
    {
    	if (serviceFusionSetup != null)
    	{
	    	serviceFusionSetup.stopServer();
			serviceFusionSetup.onDestroy(this);
    	}

        super.onDestroy();
    }
    
    
}

@@ -2577,32 +2577,53 @@
 onRestart() %0A%09%7B%0A
+%09%09super.onRestart();%0A
 %09%09if (serviceFus
@@ -2632,32 +2632,32 @@
 nSetup != null)%0A
-
 %09%09%09serviceFusion
@@ -2679,37 +2679,16 @@
 (this);%0A
-%09%09super.onRestart();%0A
 %09%7D%0A%0A%09@Ov
@@ -2717,32 +2717,52 @@
  onResume() %0A%09%7B%0A
+%09%09super.onResume();%0A
 %09%09if (serviceFus
@@ -2817,36 +2817,16 @@
 (this);%0A
-%09%09super.onResume();%0A
 %09%7D%0A%0A%09@Ov
@@ -2858,24 +2858,43 @@
 Start() %0A%09%7B%0A
+%09%09super.onStart();%0A
 %09%09if (servic
@@ -2952,35 +2952,16 @@
 (this);%0A
-%09%09super.onStart();%0A
 %09%7D%0A%0A%09@Ov
@@ -2992,24 +2992,42 @@
 nStop() %0A%09%7B%0A
+%09%09super.onStop();%0A
 %09%09if (servic
@@ -3084,34 +3084,16 @@
 (this);%0A
-%09%09super.onStop();%0A
 %09%7D%0A%0A%09@Ov
@@ -3129,16 +3129,35 @@
 e() %0A%09%7B%0A
+%09%09super.onPause();%0A
 %09%09if (se
@@ -3219,35 +3219,16 @@
 (this);%0A
-%09%09super.onPause();%0A
 %09%7D%0A    %0A
@@ -3275,24 +3275,48 @@
 oy() %0A    %7B%0A
+    %09super.onDestroy();%0A
     %09if (ser
@@ -3429,42 +3429,14 @@
 s);%0A
+
     %09%7D
-%0A%0A        super.onDestroy();
 %0A   

Fix life cycle management calling order in MainActivity.