package fr.tvbarthel.apps.sayitfromthesky;


import android.app.Fragment;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;

public class SayItFragment extends Fragment implements SayItMapFragment.ISayItMapFragment {

    private static float DEFAULT_VALUE_ZOOM = 15f;
    private static String BUNDLE_KEY_LOCATION = "SayItFragment.Bundle.Key.Location";
    private static String BUNDLE_KEY_ZOOM = "SayItFragment.Bundle.Key.Zoom";

    private SayItMapFragment mMapFragment;
    private GoogleMap mGoogleMap;
    private Location mLastKnownLocation;
    private float mLastKnownZoom;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_say_it, container, false);
        mMapFragment = new SayItMapFragment(this);
        getFragmentManager().beginTransaction().add(R.id.fragment_say_it_map_container, mMapFragment, "fragmentTagMap").commit();

        mLastKnownZoom = DEFAULT_VALUE_ZOOM;

        if (savedInstanceState != null) {
            mLastKnownLocation = savedInstanceState.getParcelable(BUNDLE_KEY_LOCATION);
            mLastKnownZoom = savedInstanceState.getFloat(BUNDLE_KEY_ZOOM, DEFAULT_VALUE_ZOOM);
        }

        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(BUNDLE_KEY_LOCATION, mLastKnownLocation);
        if (mGoogleMap != null) {
            outState.putFloat(BUNDLE_KEY_ZOOM, mGoogleMap.getCameraPosition().zoom);
        }
    }

    @Override
    public void onMapReady() {
        if (mGoogleMap == null) {
            mGoogleMap = mMapFragment.getMap();
            if (mGoogleMap != null) {
                //the map is now active and can be manipulated
                mGoogleMap.setMyLocationEnabled(true);
                final UiSettings uiSettings = mGoogleMap.getUiSettings();
                uiSettings.setCompassEnabled(false);
                uiSettings.setZoomControlsEnabled(false);

                //try to restore last known location
                if (mLastKnownLocation != null) {
                    initMapLocation();
                }

                mGoogleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
                    @Override
                    public void onMyLocationChange(Location location) {
                        //first location
                        if (mLastKnownLocation == null) {
                            mLastKnownLocation = location;
                            initMapLocation();
                        }
                    }
                });
            }
        }
    }

    private void initMapLocation() {
        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), mLastKnownZoom));
    }

}

@@ -127,16 +127,41 @@
 Bundle;%0A
+import android.util.Log;%0A
 import a
@@ -443,16 +443,62 @@
 .LatLng;
+%0Aimport com.google.maps.android.SphericalUtil;
 %0A%0Apublic
@@ -635,16 +635,71 @@
  = 15f;%0A
+    private static float DELTA_DISTANCE_IN_METER = 5f;%0A
     priv
@@ -967,24 +967,69 @@
 wnLocation;%0A
+    private Location mLastCandidateLocation;%0A
     private 
@@ -3096,342 +3096,1481 @@
     
-initMapLocation();%0A                        %7D%0A                    %7D%0A                %7D);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void initMapLocation() %7B%0A        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(%0A                new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), mLastKnownZoom
+mLastCandidateLocation = location;%0A                            initMapLocation();%0A                        %7D else if (isLocationIfOutdated(location)) %7B%0A                            setNewLocation(location);%0A                        %7D%0A%0A                        mLastCandidateLocation = location;%0A                    %7D%0A                %7D);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void initMapLocation() %7B%0A        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(%0A                new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()), mLastKnownZoom));%0A    %7D%0A%0A    private boolean isLocationIfOutdated(Location candidateLocation) %7B%0A        final LatLng candidateLatLng = new LatLng(candidateLocation.getLatitude(), candidateLocation.getLongitude());%0A        final LatLng currentLatLng = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude());%0A        final double distance = SphericalUtil.computeDistanceBetween(candidateLatLng, currentLatLng);%0A        Log.d(%22argonne%22, %22distance : %22 + String.valueOf(distance));%0A        return distance %3E DELTA_DISTANCE_IN_METER;%0A    %7D%0A%0A    private void setNewLocation(Location newLocation) %7B%0A        //TODO look at the elapsed time ?%0A        Log.d(%22argonne%22, %22new location set !%22);%0A        mLastKnownLocation = newLocation;%0A        mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(%0A                new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude())
 ));%0A

Add a tracking skeleton.