package dk.cmol.arduinorgb_controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;

public class ArduinoSocket extends Thread{
	
	private ArduinoRGBActivity parent = null;
	private Socket sock = null;
	private OutputStream stream = null;
	private String ip = null;
	private int port;
	public Handler mHandler;
	
	public ArduinoSocket(ArduinoRGBActivity parent) {
		this.parent = parent;
	}
	
	public void run() {
		connect();
		
		Looper.prepare();

        mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    try {
						stream.write((byte[]) msg.obj);
						stream.write((byte) 10);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                }
        };
        Looper.loop();
	}
	
	public void writeMessage(final byte[] message) {
		Message msg = Message.obtain();
		msg.obj = message;
		mHandler.sendMessage(msg);
	}
	
	private void connect() {
		getPreferences();
		try {
			sock = new Socket(ip, port);
			stream = sock.getOutputStream();
		} catch (IOException e) {
			// TODO: Write error to parent view
			e.printStackTrace();
			Log.e("ArduinoSocket", e.getMessage());
		}
	}
	
	private void getPreferences() {
		// Read settings from settings manager
		Log.i("ArduinoSocket", "Setting IP and PORT");
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(parent);
		ip = sharedPref.getString("server_addr", "");
		port = Integer.parseInt(sharedPref.getString("server_port", ""));
	}
	
	public void close() {
		try {
			sock.close();
		} catch (IOException e) {
			// TODO Write error to parent view
			e.printStackTrace();
			Log.e("ArduinoSocket", e.getMessage());
		}
	}

}

@@ -306,16 +306,45 @@
 til.Log;
+%0Aimport android.widget.Toast;
 %0A%0Apublic
@@ -670,21 +670,8 @@
 ) %7B%0A
-%09%09connect();%0A
 %09%09%0A%09
@@ -688,16 +688,29 @@
 epare();
+%0A%09%09connect();
 %0A%0A      
@@ -821,21 +821,36 @@
   try %7B%0A
-%09%09%09%09%09
+                    
 %09stream.
@@ -914,98 +914,203 @@
 %09%09%09%09
-%7D catch (IOException e) %7B%0A%09%09%09%09%09%09// TODO Auto-generated catch block%0A%09%09%09%09%09%09e.printStackTrace
+%09Log.i(%22ArduinoSocket%22, %22Data written...%22);%0A%09%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09%09Toast toast = Toast.makeText(parent.getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT);%0A%09%09%09%09%09%09toast.show
 ();%0A
@@ -1469,108 +1469,152 @@
 %0A%09%09%09
-// TODO: Write error to parent view%0A%09%09%09e.printStackTrace();%0A%09%09%09Log.e(%22ArduinoSocket%22, e.getMessage()
+e.printStackTrace();%0A%09%09%09Toast toast = Toast.makeText(parent.getApplicationContext(), %22Failed to connect :-(%22, Toast.LENGTH_LONG);%0A%09%09%09toast.show(
 );%0A%09
@@ -1832,16 +1832,25 @@
 arent);%0A
+%09%09try %7B%0A%09
 %09%09ip = s
@@ -1889,16 +1889,17 @@
 %22, %22%22);%0A
+%09
 %09%09port =
@@ -1958,16 +1958,232 @@
 , %22%22));%0A
+%09%09%7D catch (Exception e) %7B%0A%09%09%09Log.w(%22ArduinoSocket%22, %22Failed setting IP and PORT%22);%0A%09%09%09Toast toast = Toast.makeText(parent.getApplicationContext(), %22Please input settings%22, Toast.LENGTH_LONG);%0A%09%09%09toast.show();%0A%09%09%7D%0A%09%09%0A
 %09%7D%0A%09%0A%09pu
@@ -2292,24 +2292,26 @@
 ent view%0A%09%09%09
+//
 e.printStack
@@ -2318,24 +2318,26 @@
 Trace();%0A%09%09%09
+//
 Log.e(%22Ardui

Add toasts and error handeling