/*
 * Matti Ahinko
 * matti.m.ahinko@student.jyu.fi
 * 2012
 * 
 */
package ahinko.matti.geotrack;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

/*
 * Splash for getting GPS ready
 */
public class SplashFragment extends Fragment {

	// Locations
	private LocationsManager locations = LocationsManager.getInstance();

	// Progressbar
	private ProgressBar progressbar;

	// Textview of status
	private TextView textViewStatus;

	// Location manager
	private LocationManager locationManager;

	/*
	 * View on create (non-Javadoc)
	 * 
	 * 
	 * @see
	 * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
	 * android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.fragment_splash, container, false);
	}

	/*
	 * View created (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onViewCreated(android.view.View,
	 * android.os.Bundle)
	 */
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		this.progressbar = (ProgressBar) getView().findViewById(
				R.id.progressBarGPS);
		this.textViewStatus = (TextView) getView().findViewById(
				R.id.textViewStatus);
	}

	/*
	 * On resume activate GPS (non-Javadoc)
	 * 
	 * @see android.support.v4.app.FragmentActivity#onResume()
	 */
	@Override
	public void onResume() {
		super.onResume();
		String service = Context.LOCATION_SERVICE;
		locationManager = (LocationManager) this.getActivity()
				.getSystemService(service);
		getLocation();
		progressbar.setVisibility(View.VISIBLE);
	}

	/*
	 * On pause (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onPause()
	 */
	@Override
	public void onPause() {
		super.onPause();
		locationManager.removeUpdates(locationListener);
	}

	/*
	 * On destroy (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onDestroy()
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		if (locationManager != null && locationListener != null)
			locationManager.removeUpdates(locationListener);
	}

	/**
	 * Get location
	 */
	public void getLocation() {
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setSpeedRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_HIGH);
		String provider = locationManager.getBestProvider(criteria, false);
		locationManager.requestLocationUpdates(provider, 10000, 10,
				locationListener);
	}

	// Location listener
	private final LocationListener locationListener = new LocationListener() {
		/*
		 * If status changes (non-Javadoc)
		 * 
		 * @see
		 * android.location.LocationListener#onStatusChanged(java.lang.String,
		 * int, android.os.Bundle)
		 */
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			switch (status) {
			case LocationProvider.OUT_OF_SERVICE:
				textViewStatus.setText("GPS Signal lost");
				progressbar.setVisibility(View.VISIBLE);
				break;
			case LocationProvider.TEMPORARILY_UNAVAILABLE:
				textViewStatus.setText("GPS Signal temporarily unavailable");
				progressbar.setVisibility(View.VISIBLE);
				break;
			case LocationProvider.AVAILABLE:
				textViewStatus.setText("GPS Signal available");
				progressbar.setVisibility(View.INVISIBLE);
				break;
			default:
				break;
			}

		}

		/*
		 * If provider is enabled (non-Javadoc)
		 * 
		 * @see
		 * android.location.LocationListener#onProviderEnabled(java.lang.String)
		 */
		@Override
		public void onProviderEnabled(String provider) {
			textViewStatus.setText("GPS Signal Provider enabled");
			progressbar.setVisibility(View.INVISIBLE);
		}

		/*
		 * If provider is disabled (non-Javadoc)
		 * 
		 * @see
		 * android.location.LocationListener#onProviderDisabled(java.lang.String
		 * )
		 */
		@Override
		public void onProviderDisabled(String provider) {
			textViewStatus.setText("GPS Signal Provider disabled");
			progressbar.setVisibility(View.INVISIBLE);
		}

		/*
		 * Called when the listened gets a location update. Broadcasts the
		 * update. (non-Javadoc)
		 * 
		 * @see
		 * android.location.LocationListener#onLocationChanged(android.location
		 * .Location)
		 */
		@Override
		public void onLocationChanged(final Location location) {
			locations.add(location);
			progressbar.setVisibility(View.INVISIBLE);
		}
	};

}
