package fi_81.cwp_morse_mangle;

import fi_81.cwp_morse_mangle.CWPControlService.CWPControlBinder;
import fi_81.cwp_morse_mangle.CWPControlService.CWPControlNotification;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends Activity {
	private static final String TAG = "MainActivity";

	/* Service state */
	private boolean serviceBound = false;
	private CWPControlService cwpService;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreate()");

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		/* Start CWP service */
		startService(new Intent(this, CWPControlService.class));
	}

	@Override
	public void onRestart() {
		Log.d(TAG, "onRestart()");

		super.onRestart();
	}

	@Override
	public void onStart() {
		Log.d(TAG, "onStart()");

		super.onStart();

		/* Bind to CWP service */
		bindService(new Intent(this, CWPControlService.class),
				cwpServiceConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	public void onResume() {
		Log.d(TAG, "onResume()");

		super.onResume();
	}

	@Override
	public void onPause() {
		Log.d(TAG, "onPause()");

		super.onPause();
	}

	@Override
	public void onStop() {
		Log.d(TAG, "onStop()");

		super.onStop();

		/* Unbind from the service */
		if (serviceBound) {
			unbindService(cwpServiceConnection);
			serviceBound = false;
		}
	}

	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy()");

		super.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Log.d(TAG, "onSaveInstanceState()");

		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		Log.d(TAG, "onRestoreInstanceState()");

		super.onRestoreInstanceState(savedInstanceState);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Log.d(TAG, "onCreateOptionsMenu()");

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.d(TAG, "onOptionsItemSelected()");

		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_settings:
			Intent i = new Intent(this, MainSettingsActivity.class);
			startActivity(i);
			return true;
		}

		return false;
	}

	/** Callbacks for CWP events */
	private final CWPControlNotification cwpNotifications = new CWPControlNotification() {
		private static final String TAG = "cwpNotifications";

		@Override
		public void stateChange(int state) {
			Log.d(TAG, "stateChange(" + state + ")");
		}

		@Override
		public void morseUpdated(String morse) {
			Log.d(TAG, "receivedMorseMessage(" + morse + ")");
		}
	};

	/** Callbacks for service binding, passed to bindService() */
	private final ServiceConnection cwpServiceConnection = new ServiceConnection() {
		private static final String TAG = "cwpServiceConnection";

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.d(TAG, "onServiceConnected()");

			CWPControlBinder binder = (CWPControlBinder) service;

			cwpService = binder.getService();
			serviceBound = true;

			cwpService.registerNotifications(cwpNotifications, new Handler());
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.d(TAG, "onServiceDisconnected()");

			serviceBound = false;
		}
	};
}
@@ -530,16 +530,150 @@
 enuItem;
+%0Aimport android.view.MotionEvent;%0Aimport android.view.View;%0Aimport android.view.View.OnTouchListener;%0Aimport android.widget.ImageView;
 %0A%0Apublic
@@ -863,16 +863,78 @@
 rvice;%0A%0A
+%09/* Views that need control */%0A%09private ImageView lampImage;%0A%0A
 %09/** Cal
@@ -1149,85 +1149,626 @@
 %09/* 
-Start CWP service */%0A%09%09startService(new Intent(this, CWPControlService.class)
+Handle touching of lamp */%0A%09%09lampImage = (ImageView) findViewById(R.id.lamp);%0A%09%09lampImage.setOnTouchListener(new OnTouchListener() %7B%0A%09%09%09public boolean onTouch(View v, MotionEvent event) %7B%0A%09%09%09%09Log.d(TAG, %22onTouch(%22 + event.getActionMasked() + %22)%22);%0A%0A%09%09%09%09switch (event.getActionMasked()) %7B%0A%09%09%09%09case MotionEvent.ACTION_DOWN:%0A%09%09%09%09%09/* touching */%0A%09%09%09%09%09lampImage.setImageResource(R.drawable.green_circle);%0A%09%09%09%09%09return true;%0A%09%09%09%09case MotionEvent.ACTION_UP:%0A%09%09%09%09case MotionEvent.ACTION_CANCEL:%0A%09%09%09%09%09/* end of touch */%0A%09%09%09%09%09lampImage.setImageResource(R.drawable.gray_circle);%0A%09%09%09%09%09return true;%0A%09%09%09%09%7D%0A%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%7D
 );%0A%09

Add touching of lamp-image, change appearence when pressed