<commit_before>package fr.free.nrw.commons.upload;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.ExifInterface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import java.io.IOException;

public class GPSExtractor {

    private String filePath;
    private double decLatitude, decLongitude;
    private double currentLatitude, currentLongitude;
    private Context context;
    private static final String TAG = GPSExtractor.class.getName();
    public boolean imageCoordsExists;
    private MyLocationListener myLocationListener;

    public GPSExtractor(String filePath, Context context){
        this.filePath = filePath;
        this.context = context;
    }

    private boolean gpsPreferenceEnabled() {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        boolean gpsPref = sharedPref.getBoolean("allowGps", false);
        Log.d(TAG, "Gps pref set to: " + gpsPref);
        return gpsPref;
    }

    //Extract GPS coords of image
    public String getCoords() {

        ExifInterface exif;
        String latitude = "";
        String longitude = "";
        String latitude_ref = "";
        String longitude_ref = "";
        String decimalCoords = "";

        try {
            exif = new ExifInterface(filePath);
        } catch (IOException e) {
            Log.w("Image", e);
            return null;
        }

        if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) == null) {
            imageCoordsExists = false;
            Log.d(TAG, "Picture has no GPS info");

            //Check what user's preference is for automatic location detection
            boolean gpsPrefEnabled = gpsPreferenceEnabled();

            if (gpsPrefEnabled) {
                //If pref enabled, set up LocationListener to get current location
                LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                String provider = locationManager.getBestProvider(criteria, true);

                myLocationListener = new MyLocationListener();
                locationManager.requestLocationUpdates(provider, 400, 1, myLocationListener);
                Location location = locationManager.getLastKnownLocation(provider);

                if (location != null) {
                    myLocationListener.onLocationChanged(location);
                } else {
                    //calling method is equipped to deal with null return value
                    return null;
                }
                Log.d(TAG, "Current location values: Lat = " + currentLatitude + " Long = " + currentLongitude);
                String currentCoords = String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude);
                return currentCoords;
            } else {
                //Otherwise treat as if no coords found
                return null;
            }
        } else {
            imageCoordsExists = true;
            Log.d(TAG, "Picture has GPS info");

            latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
            latitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
            longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
            longitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
            Log.d("Image", "Latitude: " + latitude + " " + latitude_ref);
            Log.d("Image", "Longitude: " + longitude + " " + longitude_ref);

            decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref);
            return decimalCoords;
        }
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            currentLatitude = location.getLatitude();
            currentLongitude = location.getLongitude();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d(TAG, provider + "'s status changed to " + status);<commit_msg>Minor formatting changes 2<commit_after>package fr.free.nrw.commons.upload;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.ExifInterface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import java.io.IOException;

public class GPSExtractor {

    private String filePath;
    private double decLatitude, decLongitude;
    private double currentLatitude, currentLongitude;
    private Context context;
    private static final String TAG = GPSExtractor.class.getName();
    public boolean imageCoordsExists;
    private MyLocationListener myLocationListener;

    public GPSExtractor(String filePath, Context context){
        this.filePath = filePath;
        this.context = context;
    }

    private boolean gpsPreferenceEnabled() {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        boolean gpsPref = sharedPref.getBoolean("allowGps", false);
        Log.d(TAG, "Gps pref set to: " + gpsPref);
        return gpsPref;
    }

    //Extract GPS coords of image
    public String getCoords() {

        ExifInterface exif;
        String latitude = "";
        String longitude = "";
        String latitude_ref = "";
        String longitude_ref = "";
        String decimalCoords = "";

        try {
            exif = new ExifInterface(filePath);
        } catch (IOException e) {
            Log.w("Image", e);
            return null;
        }

        if (exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) == null) {
            imageCoordsExists = false;
            Log.d(TAG, "Picture has no GPS info");

            //Check what user's preference is for automatic location detection
            boolean gpsPrefEnabled = gpsPreferenceEnabled();

            if (gpsPrefEnabled) {
                //If pref enabled, set up LocationListener to get current location
                LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                String provider = locationManager.getBestProvider(criteria, true);

                myLocationListener = new MyLocationListener();
                locationManager.requestLocationUpdates(provider, 400, 1, myLocationListener);
                Location location = locationManager.getLastKnownLocation(provider);

                if (location != null) {
                    myLocationListener.onLocationChanged(location);
                } else {
                    //calling method is equipped to deal with null return value
                    return null;
                }
                Log.d(TAG, "Current location values: Lat = " + currentLatitude + " Long = " + currentLongitude);
                String currentCoords = String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude);
                return currentCoords;
            } else {
                //Otherwise treat as if no coords found
                return null;
            }

        } else {
            imageCoordsExists = true;
            Log.d(TAG, "Picture has GPS info");

            latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
            latitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
            longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
            longitude_ref = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
            Log.d("Image", "Latitude: " + latitude + " " + latitude_ref);
            Log.d("Image", "Longitude: " + longitude + " " + longitude_ref);

            decimalCoords = getDecimalCoords(latitude, latitude_ref, longitude, longitude_ref);
            return decimalCoords;
        }
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            currentLatitude = location.getLatitude();
            currentLongitude = location.getLongitude();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d(TAG, provider + "'s status changed to " + status);
{'fraction_non_alphanumeric': 0.08239871824216068, 'fraction_numerical': 0.0010299839780270085, 'mean_word_length': 2.447337278106509, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2561149', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2160, 'n_words': 542}