package com.example.gps_service01;

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 MainActivity extends Activity 
{
	
	Button btn_start, btn_stop;
	TextView infos;
	BroadcastReceiver receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		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
					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(), "msg msg", 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(), "msg msg", Toast.LENGTH_LONG).show();
				  
	
				}

		});


		
	}
}
