<commit_before>package de.fu_berlin.cdv.chasingpictures;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationServices;

import org.springframework.http.ResponseEntity;

import java.io.File;
import java.util.List;

import de.fu_berlin.cdv.chasingpictures.api.LocationRequest;
import de.fu_berlin.cdv.chasingpictures.api.Picture;
import de.fu_berlin.cdv.chasingpictures.api.Place;
import de.fu_berlin.cdv.chasingpictures.api.PlacesApiResult;
import de.fu_berlin.cdv.chasingpictures.util.Utilities;


public class PictureSelectionActivity extends Activity {
    private static final String TAG = "PictureSelection";
    private Location mLastLocation;
    private ImageView mImageView;
    private SwipeDetector mSwipeDetector;
    private List<Place> places;
    private int currentPlace = 0;
    private LocationHelper mLocationHelper;
    private ProgressBar mLocationProgressBar;
    private ProgressBar mImageProgressBar;

    private LocationListener placeFinderListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            mLastLocation = location;
            new LocationTask().execute(location);
        }
    };
    private LocationListener distanceCalculatorListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            mLastLocation = location;
            setPlaceInfoText(currentPlace);
        }
    };

    private class PictureViewLocationHelper extends LocationHelper {
        @Override
        public void onConnected(Bundle connectionHint) {
            Log.d(TAG, "Connected to Google API services.");

            Location lastLocation = getLastLocation();
            if (lastLocation != null) {
                mLastLocation = lastLocation;
                new LocationTask().execute(mLastLocation);
            }

            // TODO: Find sensible values for location updates, i.e. when do we want to search for new places
            startLocationUpdates(
                    makeLocationRequest(),
                    placeFinderListener
            );
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_selection);

        mLocationHelper = new PictureViewLocationHelper()
                .buildGoogleApiClient(getApplicationContext())
                .connect();

        mSwipeDetector = new SwipeDetector();
        mImageView = (ImageView) findViewById(R.id.picture_card_image);
        mImageView.setOnTouchListener(mSwipeDetector);
        mImageView.setOnClickListener(new ClickListener());

        mLocationProgressBar = (ProgressBar) findViewById(R.id.locationProgressBar);
        mImageProgressBar = (ProgressBar) findViewById(R.id.imageProgressBar);
    }

    private class MyPictureDownloader extends PictureDownloader {
        public MyPictureDownloader() {
            super(getCacheDir(), true);
        }

        @Override
        protected void handleProgressUpdate(@NonNull Progress progress) {
            if (progress.getState() == currentPlace) {
                updatePicture();
                showDelayedPlaceInfo(currentPlace);
            }
        }

        @Override
        protected void handleException(@Nullable Throwable exception) {
            super.handleException(exception);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            updatePicture();
            showDelayedPlaceInfo(currentPlace);
        }
    }

    private class LocationTask extends AsyncTask<Location, Object, List<Place>> {

        @Override
        protected List<Place> doInBackground(Location... params) {
            if (params.length == 0 || params[0] == null)
                return null;

            LocationRequest request = new LocationRequest(getApplicationContext(), params[0]);
            ResponseEntity<PlacesApiResult> result = request.sendRequest();
            PlacesApiResult body = result == null ? null : result.getBody();
            return body == null ? null : body.getPlaces();
        }

        @Override
        protected void onPostExecute(List<Place> resultPlaces) {
            if (resultPlaces == null || resultPlaces.isEmpty()) {
                Utilities.showError(getApplicationContext(), "No places found nearby");
                finish();
                return;
            }

            places = resultPlaces;
            checkAndFixIndex();

            // Since we have places now, de-register the listener
            mLocationHelper.stopLocationUpdates(placeFinderListener);

            // And register the distance calculator
            mLocationHelper.startLocationUpdates(LocationHelper.makeLocationRequest(), distanceCalculatorListener);

            // Hide the location progress bar
            mLocationProgressBar.setVisibility(View.GONE);
            mImageProgressBar.setVisibility(View.VISIBLE);
<commit_msg>Fix: If last location had no places nearby, the activity exited. No it waits for a new location<commit_after>package de.fu_berlin.cdv.chasingpictures;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.gms.location.LocationListener;

import org.springframework.http.ResponseEntity;

import java.io.File;
import java.util.List;

import de.fu_berlin.cdv.chasingpictures.api.LocationRequest;
import de.fu_berlin.cdv.chasingpictures.api.Picture;
import de.fu_berlin.cdv.chasingpictures.api.Place;
import de.fu_berlin.cdv.chasingpictures.api.PlacesApiResult;
import de.fu_berlin.cdv.chasingpictures.util.Utilities;


public class PictureSelectionActivity extends Activity {
    private static final String TAG = "PictureSelection";
    private Location mLastLocation;
    private ImageView mImageView;
    private SwipeDetector mSwipeDetector;
    private List<Place> places;
    private int currentPlace = 0;
    private LocationHelper mLocationHelper;
    private ProgressBar mLocationProgressBar;
    private ProgressBar mImageProgressBar;

    private LocationListener placeFinderListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            mLastLocation = location;
            new LocationTask().execute(location);
        }
    };
    private LocationListener distanceCalculatorListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            mLastLocation = location;
            setPlaceInfoText(currentPlace);
        }
    };

    private class PictureViewLocationHelper extends LocationHelper {
        @Override
        public void onConnected(Bundle connectionHint) {
            Log.d(TAG, "Connected to Google API services.");

            Location lastLocation = getLastLocation();
            if (lastLocation != null) {
                mLastLocation = lastLocation;
                new LocationTask(false).execute(mLastLocation);
            }

            // TODO: Find sensible values for location updates, i.e. when do we want to search for new places
            startLocationUpdates(
                    makeLocationRequest(),
                    placeFinderListener
            );
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_selection);

        mLocationHelper = new PictureViewLocationHelper()
                .buildGoogleApiClient(getApplicationContext())
                .connect();

        mSwipeDetector = new SwipeDetector();
        mImageView = (ImageView) findViewById(R.id.picture_card_image);
        mImageView.setOnTouchListener(mSwipeDetector);
        mImageView.setOnClickListener(new ClickListener());

        mLocationProgressBar = (ProgressBar) findViewById(R.id.locationProgressBar);
        mImageProgressBar = (ProgressBar) findViewById(R.id.imageProgressBar);
    }

    private class MyPictureDownloader extends PictureDownloader {
        public MyPictureDownloader() {
            super(getCacheDir(), true);
        }

        @Override
        protected void handleProgressUpdate(@NonNull Progress progress) {
            if (progress.getState() == currentPlace) {
                updatePicture();
                showDelayedPlaceInfo(currentPlace);
            }
        }

        @Override
        protected void handleException(@Nullable Throwable exception) {
            super.handleException(exception);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            updatePicture();
            showDelayedPlaceInfo(currentPlace);
        }
    }

    private class LocationTask extends AsyncTask<Location, Object, List<Place>> {

        private final boolean exitOnEmptyResult;

        public LocationTask(boolean exitOnEmptyResult) {
            this.exitOnEmptyResult = exitOnEmptyResult;
        }

        public LocationTask() {
            this(true);
        }

        @Override
        protected List<Place> doInBackground(Location... params) {
            if (params.length == 0 || params[0] == null)
                return null;

            LocationRequest request = new LocationRequest(getApplicationContext(), params[0]);
            ResponseEntity<PlacesApiResult> result = request.sendRequest();
            PlacesApiResult body = result == null ? null : result.getBody();
            return body == null ? null : body.getPlaces();
        }

        @Override
        protected void onPostExecute(List<Place> resultPlaces) {
            if (resultPlaces == null || resultPlaces.isEmpty()) {
                // TODO: Show better error and do not exit activity
                Utilities.showError(getApplicationContext(), "No places found nearby");
                if (exitOnEmptyResult) {
                    finish();
                }
                return;
            }

            places = resultPlaces;
            checkAndFixIndex();

            // Since we have places now, de-register the listener
            mLocationHelper.stopLocationUpdates(placeFinderListener);

            // And register the distance calculator
            mLocationHelper.startLocationUpdates(LocationHelper.makeLocationRequest(), distanceCalculatorListener);

            // Hide the location progress bar
            mLocationProgressBar.setVisibility(View.GONE);
            mImageProgressBar.setVisibility(View.VISIBLE);

{'fraction_non_alphanumeric': 0.07963604852686308, 'fraction_numerical': 0.0006932409012131716, 'mean_word_length': 2.6978532521627683, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4828762', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2759, 'n_words': 646}