package com.example.Maps09;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

//https://www.youtube.com/watch?v=lvcGh2ZgHeA
// Filip Vujovic 

// https://www.youtube.com/watch?v=QNb_3QKSmMk

public class Control_Service extends Activity 
{
	
	Button btn_start, btn_stop;
	TextView infos;
	BroadcastReceiver receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.control_service_layout);
		
		btn_start = (Button) findViewById(R.id.button1);    
		btn_stop = (Button) findViewById(R.id.button2);    
		infos = (TextView) findViewById(R.id.textView2);   
		
		enable_buttons();
		
	}

	
	protected void onResume()
	{
		super.onResume();
		
		if(receiver == null)
		{
			receiver = new BroadcastReceiver() 
			{
	
				@Override
				public void onReceive(Context context, Intent intent)
				{
					// TODO Auto-generated method stub
					Toast.makeText(getApplicationContext(), "Refresh GPS", Toast.LENGTH_SHORT).show();
					
					infos.append("\n" + intent.getExtras().getByte("coordinates"));
				}
			};
		}
		
		//registering our receiver
				registerReceiver(receiver, new IntentFilter("location_update"));
	}
	
	protected void onDestroy()
	{
		super.onDestroy();
		if(receiver != null)
		{
			unregisterReceiver(receiver);
		}
	}
	
	private void enable_buttons()
	{
		btn_start.setOnClickListener(new OnClickListener() 
		{

				@Override
				public void onClick(View arg0) 
				{
					Intent i = new Intent(getApplicationContext(), GPS_Service.class);
					startService(i);
					Toast.makeText(getApplicationContext(), "Start GPS service", Toast.LENGTH_LONG).show();
				}

		});
		
		btn_stop.setOnClickListener(new OnClickListener() 
		{

				@Override
				public void onClick(View arg0) 
				{
					Intent i = new Intent(getApplicationContext(), GPS_Service.class);
					stopService(i);
					Toast.makeText(getApplicationContext(), "Stop service : down", Toast.LENGTH_LONG).show();
				}

		});


		
	}
}

