package com.beatcraft.demos.gps;

import java.util.List;

import android.app.Activity;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;

public class BC9GPSActivity extends Activity {

	private String BC9GPS_LOG_LABEL = "BC9GPS-APP";
	private LocationManager loc;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loc = (LocationManager) getSystemService(LOCATION_SERVICE);
        List<String> providers = loc.getAllProviders();
        
        boolean provider_found = false;
        for (int i = 0; i < providers.size(); i++) {
        	String provider = providers.get(i);
        	if (loc.isProviderEnabled(provider)) {
        		loc.requestLocationUpdates("gps", 2000, 0, onLocationUpdate);
        		provider_found = true;
        	}
        }
        if (!provider_found) {
        	Log.e(BC9GPS_LOG_LABEL, "gps provider not found..");
        }
    }
    
    private LocationListener onLocationUpdate = new LocationListener() {

		public void onLocationChanged(Location location) {
			Log.i(BC9GPS_LOG_LABEL,
				   "Location Changed!!! langitude:"
				 + location.getLatitude()
				 + "longitude:"
				 + location.getLongitude()
		    );
		}

		public void onProviderDisabled(String provider) {
			Log.i(BC9GPS_LOG_LABEL, "provider disabled: " + provider);
		}

		public void onProviderEnabled(String provider) {
			Log.i(BC9GPS_LOG_LABEL, "provider enabled: " + provider);
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			Log.i(BC9GPS_LOG_LABEL, "provider status changed: " + provider + " status:" + status);
		}
    };
    
    public void onStop()
    {
    	Log.i(BC9GPS_LOG_LABEL, "onStop Called");
    	super.onStop();
    	loc.removeUpdates(onLocationUpdate);
    }
    
    public void onDestroy()
    {
    	Log.i(BC9GPS_LOG_LABEL, "onDestroy Called");
    	super.onDestroy();
    	loc.removeUpdates(onLocationUpdate);
    }
    
    public void onRestart()
    {
    	Log.i(BC9GPS_LOG_LABEL, "onRestart Called");
    	super.onRestart();
    	loc.requestLocationUpdates("gps", 2000, 0, onLocationUpdate);
    }

}