package edu.stanford.nick;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

// Activity just holds the MonkeyView and start/stop buttons.
// This now also handles pause/resume correctly.

public class MonkeyActivity extends Activity {

	private MonkeyView mMonkey;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		LinearLayout top = (LinearLayout) findViewById(R.id.topcontainer);

		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		// Make the monkey view, giving it new handler to use.
		mMonkey = new MonkeyView(this, new Handler(), metrics.density);
		mMonkey.setId(111111);  // View needs some id for onSaveInstanceState() etc. to work
		mMonkey.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));

		// Install it
		top.addView(mMonkey);


		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mMonkey.startMotoring(true, true);
			}
		});

		button = (Button) findViewById(R.id.button2);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mMonkey.stopMotoring(true);
			}
		});
	}


	@Override
	protected void onPause() {
		super.onPause();
		mMonkey.pause();
	}


	@Override
	protected void onResume() {
		super.onResume();
		mMonkey.resume();
	}
}
