package com.oneiroi;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements CommunicationManager.Listener {
	
	private Handler handler = new Handler();
	private CommunicationManager commMananger;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		setCommandForButton(R.id.btnScan, CommunicationManager.COMMAND_SCAN);
		setCommandForButton(R.id.btnForward, CommunicationManager.COMMAND_FORWARD);
		setCommandForButton(R.id.btnBackward, CommunicationManager.COMMAND_BACKWARD);
		setCommandForButton(R.id.btnRight, CommunicationManager.COMMAND_RIGHT);
		setCommandForButton(R.id.btnLeft, CommunicationManager.COMMAND_LEFT);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private void setCommandForButton(final int buttonId, final String command) {
		((Button)(findViewById(buttonId))).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				CommunicationManager commMananger = MainActivity.this.commMananger;
				if (commMananger != null) {
					commMananger.sendCommand(command);
				}
				else {
					Log.e("LLEGO", "Communication manager not initialized");
				}
			}
		});
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if (this.commMananger != null) {
			this.commMananger.stop();
			this.commMananger = null;
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (this.commMananger == null) {
			this.commMananger = new CommunicationManager(this, this);
		}
		this.commMananger.start();
	}
	
	@Override
	public void onSentSuccess(String command) {
		updateText(R.id.txtSent, command);
	}

	@Override
	public void onDataReceived(String response) {
		updateText(R.id.txtReceived, response);
	}

	@Override
	public void onStatusChanged(String message) {
		updateText(R.id.txtStatus, message);
	}

	private void updateText(final int textViewId, final String text) {
		this.handler.post(new Runnable() {
			@Override
			public void run() {
				((TextView)(findViewById(textViewId))).setText(text);
			}
		});
	}

}

@@ -410,16 +410,100 @@
 ananger;
+%0A%09private String lastCommand = %22%22;%0A%09private Handler uiThreadHandler = new Handler();
 %0A%0A%09@Over
@@ -1580,16 +1580,62 @@
 mmand);%0A
+%09%09%09%09%09MainActivity.this.lastCommand = command;%0A
 %09%09%09%09%7D%0A%09%09
@@ -2232,24 +2232,24 @@
 response) %7B%0A
-
 %09%09updateText
@@ -2274,24 +2274,826 @@
  response);%0A
+%09%09%0A%09%09if (this.lastCommand.equals(CommunicationManager.COMMAND_SCAN)) %7B%0A%09%09%09try %7B%0A%09%09%09%09final String%5B%5D input = response.replace(%22%5C%5CD+%22, %22 %22).trim().split(%22 %22);%0A%09%09%09%09if (input.length == 3) %7B%0A%09%09%09%09%09final int right = Integer.parseInt(input%5B0%5D);%0A%09%09%09%09%09final int front = Integer.parseInt(input%5B1%5D);%0A%09%09%09%09%09final int left = Integer.parseInt(input%5B2%5D);%0A%09%09%09%09%09%0A%09%09%09%09%09this.uiThreadHandler.post(new Runnable() %7B%0A%09%09%09%09%09%09@Override%0A%09%09%09%09%09%09public void run() %7B%0A%09%09%09%09%09%09%09findViewById(R.id.btnRight).setEnabled(right %3E 10);%0A%09%09%09%09%09%09%09findViewById(R.id.btnForward).setEnabled(front %3E 10);%0A%09%09%09%09%09%09%09findViewById(R.id.btnLeft).setEnabled(left %3E 10);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D);%0A%09%09%09%09%7D%0A%09%09%09%09else %7B%0A%09%09%09%09%09onStatusChanged(%22Invalid number of arguments (%22 + input.length + %22)%22);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09catch (Exception e) %7B%0A%09%09%09%09onStatusChanged(e.getMessage());%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Overri

Disable buttons based on scan results