// Path: src/org/sigimera/app/android/exception/AuthenticationErrorException.java
// public class AuthenticationErrorException extends Exception {
// 	private static final long serialVersionUID = 2838015046177566733L;
// }
// 
// Path: src/org/sigimera/app/android/model/Constants.java
// public abstract class Constants {
// 	
// 	/**
// 	 * LOGs
// 	 */
// 	public static final String LOG_TAG_SIGIMERA_APP = "SigimeraApp";
// 	public static final String LOG_TAG_LOCATION_CONTROLLER = "LocationController";
// 	public static final String LOG_TAG_CONFIG = "Config";
// 	
// 	/**
// 	 * Location radius in kilometres
// 	 */
// 	public static final int LOCATION_RADIUS = 200;
// 	
// 	public static final double MAX_DISTANCE_NEAR_CRISIS = 5000;
// 	public static final double MIN_DISTANCE_NEAR_CRISIS = 50;
// 	
// 	
// 	/**
// 	 * Keys
// 	 */
// 	public static final String CRISES_LIST = "crises_list";	
// 	public static final String CRISIS = "crisis";
// 	public static final String CRISIS_ID = "crisis_id";
// 	public static final String LATITUDE = "latitude";
// 	public static final String LONGITUDE = "longitude";
// 	public static final String CRISIS_TYPE = "crisis_type";
// 	public static final String TOP = "top";
// 	public static final String BOTTOM = "bottom";	
// 	public static final String ICON = "icon";
// 	public static final String ARROW = "rightArrow";
// 	public static final String WINDOW_TYPE = "window_type"; 
// 	
// 	/**
// 	 * Crisis types.
// 	 */
// 	public static final String EARTHQUAKE = "earthquake";
// 	public static final String FLOOD = "flood";
// 	public static final String CYCLONE = "cyclone";
// 	public static final String VOLCANO = "volcano";
// 	
// 	/**
// 	 * Style handling.
// 	 */
// 	public static final String LATEST_CRISIS = "LATEST_CRISIS";
// 	public static final String NEAR_CRISIS = "NEAR_CRISIS";
// 	
// 	/**
// 	 * Window types.
// 	 */
// 	public static final String WINDOW_TYPE_SHARED_CRISIS = "shared_crisis";
// 	
// 	/**
// 	 * Notification Identifier.
// 	 */
// 	public static final int PING_ID = 12627;
// 	
// 	/**
// 	 * Preferences.
// 	 */
// 	public static final String PREFS_NAME = "session_handler_preferences";
// 	
// 	
// 	/**
// 	 * Static codes.
// 	 */
// 	
// 	/**
// 	 * 201 - created.
// 	 */
// 	public static final int STATUS_CODE_CREATED = 201;
// 		
// 	
// 	/**
// 	 * Time to sleep - 1 second.
// 	 */
// 	public static final int TIME_TO_SLEEP_ONE_SEC = 1000;
// 	
// 	/**
// 	 * Time to sleep - 2 seconds.
// 	 */
// 	public static final int TIME_TO_SLEEP_TWO_SEC = 2000;
// }


import org.sigimera.app.android.exception.AuthenticationErrorException;
import org.sigimera.app.android.model.Constants;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
/**
 * Sigimera Crises Information Platform Android Client
 * Copyright (C) 2012 by Sigimera
 * All Rights Reserved
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.sigimera.app.android.controller;



/**
 * Handle the location found via network or GPS.
 * @author Corneliu-Valentin Stanciu
 * @e-mail  corneliu.stanciu@sigimera.org
 */
public class LocationController {	
	
	private static LocationController instance = null;

	private LocationManager locationManager = null;
	
	/**
	 * Singleton pattern
	 */
	public static LocationController getInstance() {
		if ( instance == null ) instance = new LocationController();
		return instance;
	}	
	
	private LocationController() {
		// Acquire a reference to the system Location Manager
		Context context = ApplicationController.getInstance().getApplicationContext();
		if ( context != null )
			locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		else
			Log.e(Constants.LOG_TAG_LOCATION_CONTROLLER, "The context is null! We cannot update the location");
		
		// Define a listener that responds to location updates
		LocationListener locationListener = new LocationListener() {
		    public void onLocationChanged(Location location) {
		    	// Called when a new location is found by the network location provider.
		    	Log.i(Constants.LOG_TAG_LOCATION_CONTROLLER, "Found new location: " + location);
		    	
		    	if ( ApplicationController.getInstance().isEverythingUpdated() ) {
					try {
						final String auth_token = ApplicationController.getInstance().getSessionHandler().getAuthenticationToken();
						PersistanceController.getInstance().updateNearCrises(auth_token, 1, location);
// Path: src/org/sigimera/app/android/exception/AuthenticationErrorException.java
// public class AuthenticationErrorException extends Exception {
// 	private static final long serialVersionUID = 2838015046177566733L;
// }
// 
// Path: src/org/sigimera/app/android/model/Constants.java
// public abstract class Constants {
// 	
// 	/**
// 	 * LOGs
// 	 */
// 	public static final String LOG_TAG_SIGIMERA_APP = "SigimeraApp";
// 	public static final String LOG_TAG_LOCATION_CONTROLLER = "LocationController";
// 	public static final String LOG_TAG_CONFIG = "Config";
// 	
// 	/**
// 	 * Location radius in kilometres
// 	 */
// 	public static final int LOCATION_RADIUS = 200;
// 	
// 	public static final double MAX_DISTANCE_NEAR_CRISIS = 5000;
// 	public static final double MIN_DISTANCE_NEAR_CRISIS = 50;
// 	
// 	
// 	/**
// 	 * Keys
// 	 */
// 	public static final String CRISES_LIST = "crises_list";	
// 	public static final String CRISIS = "crisis";
// 	public static final String CRISIS_ID = "crisis_id";
// 	public static final String LATITUDE = "latitude";
// 	public static final String LONGITUDE = "longitude";
// 	public static final String CRISIS_TYPE = "crisis_type";
// 	public static final String TOP = "top";
// 	public static final String BOTTOM = "bottom";	
// 	public static final String ICON = "icon";
// 	public static final String ARROW = "rightArrow";
// 	public static final String WINDOW_TYPE = "window_type"; 
// 	
// 	/**
// 	 * Crisis types.
// 	 */
// 	public static final String EARTHQUAKE = "earthquake";
// 	public static final String FLOOD = "flood";
// 	public static final String CYCLONE = "cyclone";
// 	public static final String VOLCANO = "volcano";
// 	
// 	/**
// 	 * Style handling.
// 	 */
// 	public static final String LATEST_CRISIS = "LATEST_CRISIS";
// 	public static final String NEAR_CRISIS = "NEAR_CRISIS";
// 	
// 	/**
// 	 * Window types.
// 	 */
// 	public static final String WINDOW_TYPE_SHARED_CRISIS = "shared_crisis";
// 	
// 	/**
// 	 * Notification Identifier.
// 	 */
// 	public static final int PING_ID = 12627;
// 	
// 	/**
// 	 * Preferences.
// 	 */
// 	public static final String PREFS_NAME = "session_handler_preferences";
// 	
// 	
// 	/**
// 	 * Static codes.
// 	 */
// 	
// 	/**
// 	 * 201 - created.
// 	 */
// 	public static final int STATUS_CODE_CREATED = 201;
// 		
// 	
// 	/**
// 	 * Time to sleep - 1 second.
// 	 */
// 	public static final int TIME_TO_SLEEP_ONE_SEC = 1000;
// 	
// 	/**
// 	 * Time to sleep - 2 seconds.
// 	 */
// 	public static final int TIME_TO_SLEEP_TWO_SEC = 2000;
// }


// Path: src/org/sigimera/app/android/controller/LocationController.java
import org.sigimera.app.android.exception.AuthenticationErrorException;
import org.sigimera.app.android.model.Constants;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

/**
 * Sigimera Crises Information Platform Android Client
 * Copyright (C) 2012 by Sigimera
 * All Rights Reserved
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.sigimera.app.android.controller;



/**
 * Handle the location found via network or GPS.
 * @author Corneliu-Valentin Stanciu
 * @e-mail  corneliu.stanciu@sigimera.org
 */
public class LocationController {	
	
	private static LocationController instance = null;

	private LocationManager locationManager = null;
	
	/**
	 * Singleton pattern
	 */
	public static LocationController getInstance() {
		if ( instance == null ) instance = new LocationController();
		return instance;
	}	
	
	private LocationController() {
		// Acquire a reference to the system Location Manager
		Context context = ApplicationController.getInstance().getApplicationContext();
		if ( context != null )
			locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		else
			Log.e(Constants.LOG_TAG_LOCATION_CONTROLLER, "The context is null! We cannot update the location");
		
		// Define a listener that responds to location updates
		LocationListener locationListener = new LocationListener() {
		    public void onLocationChanged(Location location) {
		    	// Called when a new location is found by the network location provider.
		    	Log.i(Constants.LOG_TAG_LOCATION_CONTROLLER, "Found new location: " + location);
		    	
		    	if ( ApplicationController.getInstance().isEverythingUpdated() ) {
					try {
						final String auth_token = ApplicationController.getInstance().getSessionHandler().getAuthenticationToken();
						PersistanceController.getInstance().updateNearCrises(auth_token, 1, location);
					} catch (AuthenticationErrorException e) {