<commit_before>package lt.vilnius.tvarkau.fragments;


import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import lt.vilnius.tvarkau.R;
import lt.vilnius.tvarkau.entity.Problem;
import lt.vilnius.tvarkau.events_listeners.MapInfoWindowShownEvent;
import lt.vilnius.tvarkau.views.adapters.MapsInfoWindowAdapter;

public abstract class BaseMapFragment extends SupportMapFragment
        implements GoogleMap.OnMarkerClickListener {

    protected static final LatLng VILNIUS_LAT_LNG = new LatLng(54.687157, 25.279652);

    protected GoogleMap googleMap;

    protected BitmapDescriptor doneMarker;
    protected BitmapDescriptor postponedMarker;
    protected BitmapDescriptor registeredMarker;
    protected BitmapDescriptor transferredMarker;
    protected BitmapDescriptor selectedMarker;

    protected HashMap<String, Problem> problemHashMap = new HashMap<>();

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

        setMarkerResources();
    }

    private void setMarkerResources() {
        doneMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_drop_done);
        postponedMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_drop_postponed);
        transferredMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_drop_transferred);
        registeredMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_drop_registered);
        selectedMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_drop_selected);
    }

    protected void onMapReady(GoogleMap map) {
        googleMap = map;
        googleMap.setOnMarkerClickListener(this);

        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(VILNIUS_LAT_LNG, 10f));

        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager
            .PERMISSION_GRANTED && ActivityCompat
            .checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            googleMap.setMyLocationEnabled(true);
        }

        initMapData();
    }

    protected void setMarkerInfoWindowAdapter() {
        googleMap.setInfoWindowAdapter(new MapsInfoWindowAdapter(getActivity(), problemHashMap));
    }

    protected abstract void initMapData();

    protected void placeMarkerOnTheMap(Problem problem, boolean shouldShowInfoWindow) {
        String problemStringId = String.valueOf(problem.getId());

        // Hack: Google Map don't have setData method.
        // There is no easy way to get problem from marker.
        // Set problem id as marker title and keep hashmap of problems with ids
        problemHashMap.put(problemStringId, problem);

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(problem.getLatLng());
        markerOptions.title(problemStringId);
        markerOptions.icon(getMarkerIcon(problem));

        if (shouldShowInfoWindow) {
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(problem.getLatLng(), 12f));
            setMarkerInfoWindowAdapter();
            Marker marker = googleMap.addMarker(markerOptions);
            marker.setIcon(selectedMarker);
            marker.showInfoWindow();
            //  There is a known issue where in InfoWindow the image
            // doesn't load properly the first time. Need to reload  each time
            final Handler handler = new Handler();
            handler.postDelayed(marker::showInfoWindow, 200);
        } else
            googleMap.addMarker(markerOptions);
    }

    public BitmapDescriptor getMarkerIcon(Problem problem) {
        switch (problem.getStatus()) {
            case Problem.STATUS_DONE:
                return doneMarker;
            case Problem.STATUS_RESOLVED:
                return doneMarker;
            case Problem.STATUS_POSTPONED:
                return postponedMarker;
            case Problem.STATUS_TRANSFERRED:
                return transferredMarker;
            case Problem.STATUS_REGISTERED:
                return registeredMarker;
            default:
                return registeredMarker;
        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        getActivity().setTitle(getProblemByMarker(marker).getAddress());
        marker.setIcon(selectedMarker);
        EventBus.getDefault().post(new MapInfoWindowShownEvent(marker));
        marker.showInfoWindow();
        //  There is a known issue where in InfoWindow the image
        // doesn't load properly the first time. Need to reload  each time
        final Handler handler = new Handler();
        handler.postDelayed(marker::showInfoWindow, 200);
        return false;
    }

    public Problem getProblemByMarker(Marker marker) {
        return problemHashMap.get(marker.getTitle());
    }
}
<commit_msg>Zoom to last known user location when map is loaded<commit_after>package lt.vilnius.tvarkau.fragments;


import android.Manifest;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import lt.vilnius.tvarkau.R;
import lt.vilnius.tvarkau.entity.Problem;
import lt.vilnius.tvarkau.events_listeners.MapInfoWindowShownEvent;
import lt.vilnius.tvarkau.views.adapters.MapsInfoWindowAdapter;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public abstract class BaseMapFragment extends SupportMapFragment
        implements GoogleMap.OnMarkerClickListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    protected static final LatLng VILNIUS_LAT_LNG = new LatLng(54.687157, 25.279652);

    protected GoogleMap googleMap;

    protected BitmapDescriptor doneMarker;
    protected BitmapDescriptor postponedMarker;
    protected BitmapDescriptor registeredMarker;
    protected BitmapDescriptor transferredMarker;
    protected BitmapDescriptor selectedMarker;

    protected HashMap<String, Problem> problemHashMap = new HashMap<>();

    private GoogleApiClient googleApi;
    private Handler handler;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        handler = new Handler();

        setMarkerResources();

        if (googleApi == null) {
            googleApi = new GoogleApiClient.Builder(getContext())
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .enableAutoManage(getActivity(), this)
                    .build();
        }
    }

    private void setMarkerResources() {
        doneMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_drop_done);
        postponedMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_drop_postponed);
        transferredMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_drop_transferred);
        registeredMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_drop_registered);
        selectedMarker = BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_drop_selected);
    }

    protected void onMapReady(GoogleMap map) {
        googleMap = map;
        googleMap.setOnMarkerClickListener(this);

        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(VILNIUS_LAT_LNG, 10f));

        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PERMISSION_GRANTED) {
            googleMap.setMyLocationEnabled(true);
        }

        initMapData();
    }

    private void zoomToMyLocation(GoogleMap map, Location lastLocation) {
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()))
                .zoom(17f)
                .build();

        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }

    protected void setMarkerInfoWindowAdapter() {
        googleMap.setInfoWindowAdapter(new MapsInfoWindowAdapter(getActivity(), problemHashMap));
    }

    protected abstract void initMapData();

    protected void placeMarkerOnTheMap(Problem problem, boolean shouldShowInfoWindow) {
        String problemStringId = String.valueOf(problem.getId());

        // Hack: Google Map don't have setData method.
        // There is no easy way to get problem from marker.
        // Set problem id as marker title and keep hashmap of problems with ids
        problemHashMap.put(problemStringId, problem);

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(problem.getLatLng());
        markerOptions.title(problemStringId);
        markerOptions.icon(getMarkerIcon(problem));

        if (shouldShowInfoWindow) {
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(problem.getLatLng(), 12f));
            setMarkerInfoWindowAdapter();
            Marker marker = googleMap.addMarker(markerOptions);
            marker.setIcon(selectedMarker);
            marker.showInfoWindow();
            //  There is a known issue where in InfoWindow the image
            // doesn't load properly the first time. Need to reload  each time
            final Handler handler = new Handler();
            handler.postDelayed(marker::showInfoWindow, 200);
        } else
            googleMap.addMarker(markerOptions);
    }

    public BitmapDescriptor getMarkerIcon(Problem problem) {
        switch (problem.getStatus()) {
            case Problem.STATUS_DONE:
                return doneMarker;
            case Problem.STATUS_RESOLVED:
                return doneMarker;
            case Problem.STATUS_POSTPONED:
                return postponedMarker;
            case Problem.STATUS_TRANSFERRED:
                return transferredMarker;
            case Problem.STATUS_REGISTERED:
                return registeredMarker;
            default:
                return registeredMarker;
        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        getActivity().setTitle(getProblemByMarker(marker).getAddress());
        marker.setIcon(selectedMarker);
        EventBus.getDefault().post(new MapInfoWindowShownEvent(marker));
        marker.showInfoWindow();
        //  There is a known issue where in InfoWindow the image
        // doesn't load properly the first time. Need to reload  each time
        handler.postDelayed(marker::showInfoWindow, 200);
        return false;
    }

    public Problem getProblemByMarker(Marker marker) {
        return problemHashMap.get(marker.getTitle());
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PERMISSION_GRANTED) {
            return;
        }

        Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApi);
        if (lastLocation != null) {
            handler.post(() -> zoomToMyLocation(googleMap, lastLocation));
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        //don't care about that, can't do anything anyway
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        //don't care about that, can't do anything anyway
    }

    @Override
    public void onDestroyView() {
        googleMap.setOnMarkerClickListener(null);
        googleMap = null;
        super.onDestroyView();
    }
}

{'fraction_non_alphanumeric': 0.0811319334688041, 'fraction_numerical': 0.00421464589448333, 'mean_word_length': 3.2712954034072643, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '3436452', 'n_tokens_mistral': 3615, 'n_tokens_neox': 3404, 'n_words': 699}