package com.location.demo;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity {
    /** Called when the activity is first created. */
	
	String lat, longg, accuracy;
	String msg;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        //get the location
        LocationManager manager= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        
        
        LocationListener listner=new LocationListener() {
			
			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProviderEnabled(String arg0) {
				// TODO Auto-generated method stub
				TextView textView= (TextView) findViewById(R.id.textView1);
				textView.setText("Location is enable...");
				
			}
			
			public void onProviderDisabled(String arg0) {
				// TODO Auto-generated method stub
				TextView textView= (TextView) findViewById(R.id.textView1);
				textView.setText("Location is Disabledd...");
			}
			
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				TextView textView= (TextView) findViewById(R.id.textView1);
				
				lat      = String.valueOf( location.getLatitude() );
				longg    = String.valueOf( location.getLongitude() );
				accuracy = String.valueOf(location.getAccuracy());
				
				msg= "Current Location\nLat:"+lat+"\nLong:"+longg+"\nAccuracy:"+accuracy;
				
				textView.setText(msg);
				
				
			}
		};
		
		manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,listner);
		
		
		//send data to server
		Button  postButton= (Button) findViewById(R.id.postData);
		
		postButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				
			String serverAddress="http://10.0.2.2/android/post.php";
			
			HttpClient httpClient=new DefaultHttpClient();
			HttpPost httpPost=new HttpPost(serverAddress);
			
			try{
				List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(3);
				
				nameValuePairs.add(new BasicNameValuePair("lat", lat));
				nameValuePairs.add(new BasicNameValuePair("longg", longg));
				nameValuePairs.add(new BasicNameValuePair("accuracy", accuracy));
				
				httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				
				httpClient.execute(httpPost);
			}
			catch(Exception ex){
				
			}
				
				
			}
		});
		
        
		//send sms
		Button b=(Button) findViewById(R.id.SendSMS);
		
		final PendingIntent pi=PendingIntent.getActivity(this, 0, new Intent(this,Main.class),0);
		
		b.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String phoneNo="+966123456" 
						"";
				
				SmsManager manager=SmsManager.getDefault();
				manager.sendTextMessage(phoneNo, null, msg, pi, null);
							
			}
		} );
    }
}
