<commit_before>package mobi.droid.fakeroad.ui.activity;

import android.app.*;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.directions.route.RoutingListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.maps.android.ui.IconGenerator;
import mobi.droid.fakeroad.R;
import mobi.droid.fakeroad.location.MapsHelper;
import mobi.droid.fakeroad.ui.fragments.PathFragment;
import mobi.droid.fakeroad.ui.fragments.SearchLocationFragment;

import java.util.LinkedList;

public class MainActivity extends BaseMapViewActivity{

    public static final String TAG_ROUTE = "route";
    public static final String TAG_PATH = "path";
    private IconGenerator mIconGenerator;
    ///
    private MarkerOptions mRouteStartMarkerOptions;
    private MarkerOptions mRouteEndMarkerOptions;
    private LinkedList<LatLng> mMarkers = new LinkedList<LatLng>();
    ///
    private ProgressDialog mProgressDialog;
    private ActionBar.TabListener mTabListener = new ActionBar.TabListener(){

        @Override
        public void onTabSelected(final ActionBar.Tab tab, final FragmentTransaction ft){
            invalidateOptionsMenu();
            mMap.clear();
            mMarkers.clear();
            mRouteStartMarkerOptions = null;
            mRouteEndMarkerOptions = null;

            if(tab.getTag().equals(TAG_ROUTE)){
                pushFragment(SearchLocationFragment.class, null, R.id.fragmentHeader);
            } else if(tab.getTag().equals(TAG_PATH)){
                pushFragment(PathFragment.class, null, R.id.fragmentHeader);
                if(mMap != null){
                    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener(){

                        @Override
                        public void onMapLongClick(final LatLng aLatLng){
                            onAddMarker(aLatLng);
                        }
                    });
                }
            }
        }

        @Override
        public void onTabUnselected(final ActionBar.Tab tab, final FragmentTransaction ft){
            if(TAG_PATH.equals(tab.getTag())){
                if(mMap != null){
                    mMap.setOnMapLongClickListener(null);
                }

            }
        }

        @Override
        public void onTabReselected(final ActionBar.Tab tab, final FragmentTransaction ft){
        }
    };

    @Override
    public void onCreate(final Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        ActionBar bar = getActionBar();
        //noinspection ConstantConditions
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.addTab(bar.newTab().setText("Routing (start+finish)").setTabListener(mTabListener).setTag(TAG_ROUTE));
        bar.addTab(bar.newTab().setText("Path").setTabListener(mTabListener).setTag(TAG_PATH));
    }

    @Override
    protected void configureMap(final Bundle savedInstanceState){
        super.configureMap(savedInstanceState);

        mIconGenerator = new IconGenerator(this);
        mIconGenerator.setBackground(new ColorDrawable(Color.LTGRAY));
        mIconGenerator.setContentPadding(2, 2, 2, 2);
    }

    @Override
    protected void onAddMarker(final LatLng aLatLng){
        MarkerOptions pointMarker = new MarkerOptions();
        pointMarker.draggable(false);
        pointMarker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
        pointMarker.position(aLatLng);
        pointMarker.visible(true);
        mMap.addMarker(pointMarker);

        if(!mMarkers.isEmpty()){
            LatLng last = mMarkers.getLast();

            PolylineOptions polylineOptions = new PolylineOptions();
            polylineOptions.add(last, aLatLng);
            polylineOptions.width(5);
            mMap.addPolyline(polylineOptions);
        }
        LatLng oldLast = mMarkers.peekLast();
        mMarkers.add(aLatLng);
        if(mMarkers.size() > 1){
            MarkerOptions distanceMarker = new MarkerOptions();
            int distance = (int) MapsHelper.distance(oldLast, aLatLng) / 2;
            distanceMarker.position(MapsHelper.calcLngLat(oldLast, distance, MapsHelper.bearing(oldLast, aLatLng)));
            distanceMarker.draggable(false);
            distanceMarker.visible(true);

            String text = distance < 1000 ?
                    String.valueOf(distance) + " m" :
                    String.valueOf(distance / 1000) + "." + String.valueOf(distance % 1000) + " km";
            Bitmap icon = mIconGenerator.makeIcon(text);
            distanceMarker.icon(BitmapDescriptorFactory.fromBitmap(icon));
            mMap.addMarker(distanceMarker);
        }
    }

    public void addMarkerStart(LatLng aLatLng){
        // Start marker
        if(mRouteStartMarkerOptions != null){
            mRouteStartMarkerOptions.visible(false);
        }
        mRouteStartMarkerOptions = new MarkerOptions();
        mRouteStartMarkerOptions.position(aLatLng);
        mRouteStartMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue));

        mMap.addMarker(mRouteStartMarkerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(aLatLng));
    }

    public void addMarkerEnd(LatLng aLatLng){
        // End marker
        if(mRouteEndMarkerOptions != null){
            mRouteEndMarkerOptions.visible(false);
        }
        mRouteEndMarkerOptions = new MarkerOptions();
        mRouteEndMarkerOptions.position(aLatLng);
        mRouteEndMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green));

        mMap.addMarker(mRouteEndMarkerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(aLatLng));
    }

    public void calculateRoute(final LatLng aFrom, final LatLng aTo){
        MapsHelper.calculateRoute(aFrom, aTo, new RoutingListener(){

            @Override
            public void onRoutingFailure(){
                hideProgress();
                Toast.makeText(MainActivity.this, "Failed to determine routing", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRoutingStart(){
                String message = "Routing calculation...";
                showProgress(message);
            }

            @Override
            public void onRoutingSuccess(final PolylineOptions mPolyOptions){
                hideProgress();
                if(mMap != null){
                    mMap.clear();

                    // Start marker
                    MarkerOptions options = new MarkerOptions();
                    options.position(aFrom);
                    options.icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue));
                    mMap.addMarker(options);

                    // End marker
                    options.position(aTo);
                    options.icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green));
                    mMap.addMarker(options);
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(aTo));

                    PolylineOptions polylineOptions = new PolylineOptions();
                    polylineOptions.color(Color.BLUE);
                    polylineOptions.width(10);
                    polylineOptions.addAll(mPolyOptions.getPoints());

                    mMap.addPolyline(polylineOptions);
                }
            }
        });
    }

    @Override
    public void onPause(){
        hideProgress();
        super.onPause();
    }

    private void showProgress(final String aMessage){
        mProgressDialog = ProgressDialog.show(this, null, aMessage, true);
    }
<commit_msg>routing points saved after success<commit_after>package mobi.droid.fakeroad.ui.activity;

import android.app.*;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.directions.route.RoutingListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.maps.android.ui.IconGenerator;
import mobi.droid.fakeroad.R;
import mobi.droid.fakeroad.location.MapsHelper;
import mobi.droid.fakeroad.ui.fragments.PathFragment;
import mobi.droid.fakeroad.ui.fragments.SearchLocationFragment;

import java.util.LinkedList;
import java.util.List;

public class MainActivity extends BaseMapViewActivity{

    public static final String TAG_ROUTE = "route";
    public static final String TAG_PATH = "path";
    private IconGenerator mIconGenerator;
    ///
    private MarkerOptions mRouteStartMarkerOptions;
    private MarkerOptions mRouteEndMarkerOptions;
    private LinkedList<LatLng> mMarkers = new LinkedList<LatLng>();
    ///
    private ProgressDialog mProgressDialog;
    private ActionBar.TabListener mTabListener = new ActionBar.TabListener(){

        @Override
        public void onTabSelected(final ActionBar.Tab tab, final FragmentTransaction ft){
            invalidateOptionsMenu();
            cleanup();

            if(tab.getTag().equals(TAG_ROUTE)){
                pushFragment(SearchLocationFragment.class, null, R.id.fragmentHeader);
            } else if(tab.getTag().equals(TAG_PATH)){
                pushFragment(PathFragment.class, null, R.id.fragmentHeader);
                if(mMap != null){
                    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener(){

                        @Override
                        public void onMapLongClick(final LatLng aLatLng){
                            onAddMarker(aLatLng);
                        }
                    });
                }
            }
        }

        private void cleanup(){
            mMap.clear();
            mMarkers.clear();
            mRouteStartMarkerOptions = null;
            mRouteEndMarkerOptions = null;
            mRoutingPoints = null;
        }

        @Override
        public void onTabUnselected(final ActionBar.Tab tab, final FragmentTransaction ft){
            if(TAG_PATH.equals(tab.getTag())){
                if(mMap != null){
                    mMap.setOnMapLongClickListener(null);
                }

            }
        }

        @Override
        public void onTabReselected(final ActionBar.Tab tab, final FragmentTransaction ft){
        }
    };
    private List<LatLng> mRoutingPoints;

    @Override
    public void onCreate(final Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        ActionBar bar = getActionBar();
        //noinspection ConstantConditions
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.addTab(bar.newTab().setText("Routing (start+finish)").setTabListener(mTabListener).setTag(TAG_ROUTE));
        bar.addTab(bar.newTab().setText("Path").setTabListener(mTabListener).setTag(TAG_PATH));
    }

    @Override
    protected void configureMap(final Bundle savedInstanceState){
        super.configureMap(savedInstanceState);

        mIconGenerator = new IconGenerator(this);
        mIconGenerator.setBackground(new ColorDrawable(Color.LTGRAY));
        mIconGenerator.setContentPadding(2, 2, 2, 2);
    }

    @Override
    protected void onAddMarker(final LatLng aLatLng){
        MarkerOptions pointMarker = new MarkerOptions();
        pointMarker.draggable(false);
        pointMarker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
        pointMarker.position(aLatLng);
        pointMarker.visible(true);
        mMap.addMarker(pointMarker);

        if(!mMarkers.isEmpty()){
            LatLng last = mMarkers.getLast();

            PolylineOptions polylineOptions = new PolylineOptions();
            polylineOptions.add(last, aLatLng);
            polylineOptions.width(5);
            mMap.addPolyline(polylineOptions);
        }
        LatLng oldLast = mMarkers.peekLast();
        mMarkers.add(aLatLng);
        if(mMarkers.size() > 1){
            MarkerOptions distanceMarker = new MarkerOptions();
            int distance = (int) MapsHelper.distance(oldLast, aLatLng) / 2;
            distanceMarker.position(MapsHelper.calcLngLat(oldLast, distance, MapsHelper.bearing(oldLast, aLatLng)));
            distanceMarker.draggable(false);
            distanceMarker.visible(true);

            String text = distance < 1000 ?
                    String.valueOf(distance) + " m" :
                    String.valueOf(distance / 1000) + "." + String.valueOf(distance % 1000) + " km";
            Bitmap icon = mIconGenerator.makeIcon(text);
            distanceMarker.icon(BitmapDescriptorFactory.fromBitmap(icon));
            mMap.addMarker(distanceMarker);
        }
    }

    public void addMarkerStart(LatLng aLatLng){
        // Start marker
        if(mRouteStartMarkerOptions != null){
            mRouteStartMarkerOptions.visible(false);
        }
        mRouteStartMarkerOptions = new MarkerOptions();
        mRouteStartMarkerOptions.position(aLatLng);
        mRouteStartMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue));

        mMap.addMarker(mRouteStartMarkerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(aLatLng));
    }

    public void addMarkerEnd(LatLng aLatLng){
        // End marker
        if(mRouteEndMarkerOptions != null){
            mRouteEndMarkerOptions.visible(false);
        }
        mRouteEndMarkerOptions = new MarkerOptions();
        mRouteEndMarkerOptions.position(aLatLng);
        mRouteEndMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green));

        mMap.addMarker(mRouteEndMarkerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLng(aLatLng));
    }

    public void calculateRoute(final LatLng aFrom, final LatLng aTo){
        MapsHelper.calculateRoute(aFrom, aTo, new RoutingListener(){

            @Override
            public void onRoutingFailure(){
                hideProgress();
                Toast.makeText(MainActivity.this, "Failed to determine routing", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRoutingStart(){
                String message = "Routing calculation...";
                showProgress(message);
            }

            @Override
            public void onRoutingSuccess(final PolylineOptions aPolyOptions){
                hideProgress();
                if(mMap != null){
                    mMap.clear();

                    // Start marker
                    MarkerOptions options = new MarkerOptions();
                    options.position(aFrom);
                    options.icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue));
                    mMap.addMarker(options);

                    // End marker
                    options.position(aTo);
                    options.icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green));
                    mMap.addMarker(options);
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(aTo));

                    PolylineOptions polylineOptions = new PolylineOptions();
                    polylineOptions.color(Color.BLUE);
                    polylineOptions.width(10);

                    mRoutingPoints = aPolyOptions.getPoints();
                    polylineOptions.addAll(mRoutingPoints);

                    mMap.addPolyline(polylineOptions);
                }
            }
        });
    }

    @Override
    public void onPause(){
        hideProgress();
        super.onPause();
    }

    private void showProgress(final String aMessage){
        mProgressDialog = ProgressDialog.show(this, null, aMessage, true);
    }

{'fraction_non_alphanumeric': 0.09346421168150225, 'fraction_numerical': 0.002560663333739788, 'mean_word_length': 2.4009952311839102, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6544456', 'n_tokens_mistral': 4482, 'n_tokens_neox': 4261, 'n_words': 723}