<commit_before>package ca.dreamteam.logrunner;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;

import java.io.ByteArrayOutputStream;

import ca.concordia.sensortag.SensorTagListener;
import ca.concordia.sensortag.SensorTagLoggerListener;
import ca.concordia.sensortag.SensorTagManager;
import ca.concordia.sensortag.SensorTagManager.ErrorType;
import ca.concordia.sensortag.SensorTagManager.StatusType;
import ca.dreamteam.logrunner.Util.Utilities;
import ti.android.ble.sensortag.DeviceSelectActivity;
import ti.android.ble.sensortag.Sensor;

public class StartRunActivity extends Activity {

    final String TAG = "ca.dreamteam.logrunner.StartRunActivity";

    static protected final int UPDATE_TEMP_BARO_PERIOD_MS  = 30000;
    static protected final int UPDATE_HUMIDITY_PERIOD_MS = 900000;

    private TextView mTemperatureView;
    private TextView mBarometerView;
    private TextView mHumidityView;
    private TextView mDistanceView;
    private static double mAvgTemperature, mAvgHumidity, mAvgPressure;
    double latitude;
    double longitude;
    private String provider;
    private LocationManager locationManager;

    // Bluetooth communication with the SensorTag
    private BluetoothDevice mBtDevice;
    private SensorTagManager mStManager;
    private SensorTagListener mStListener;
    private GoogleMap map;
    private byte mByteArray[];

    private LocationListener locationListener = new LocationListener() {

        int counter = 0;

        @Override
        public void onLocationChanged(Location location) {
            double previous_latitude = latitude;
            double previous_longitude = longitude;
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            android.util.Log.v(TAG, "lat = " + latitude + " & lng = " + longitude);
            counter++;
            if (counter > 3) {
                map.addPolyline(
                        new PolylineOptions().
                                add(new LatLng(previous_latitude, previous_longitude),
                                        new LatLng(latitude, longitude)).width(5)
                                .color(android.graphics.Color.RED).geodesic(true));
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

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

        Button runButton = (Button) findViewById(R.id.runButton);

        // Get the Bluetooth device selected by the user
        mBtDevice = (BluetoothDevice) getIntent().getParcelableExtra(DeviceSelectActivity.EXTRA_DEVICE);
        if (mBtDevice == null) {
            android.util.Log.e(TAG, "No BluetoothDevice extra [" + DeviceSelectActivity.EXTRA_DEVICE
                    + "] provided in Intent.");
            Toast.makeText(this, "No Bluetooth Device selected", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        mStManager = new SensorTagManager(getApplicationContext(), mBtDevice);
        mStListener = new ManagerListener();
        final Chronometer chronometer = (Chronometer) findViewById(R.id.mChronometer);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        runButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TextView textButton = (TextView) findViewById(R.id.textButton);
                final Button tempButton = (Button) findViewById(R.id.runButton);
                final Button saveButton = (Button) findViewById(R.id.save_button);
                final Button discardButton = (Button) findViewById(R.id.discard_button);
                tempButton.setClickable(false);

                // Based on the textButton value change between Run, Stop & Save actions
                if (((String)textButton.getText()).compareTo("START RUN") == 0) {
                    tempButton.setBackgroundColor(android.graphics.Color.RED); // Blue
                    textButton.setText("STOP");

                    mAvgTemperature = mAvgHumidity = mAvgPressure = 0.0;
                    // Get references to the GUI text box objects
                    mTemperatureView = (TextView) findViewById(R.id.value_temp);
                    mTemperatureView.setVisibility(View.VISIBLE);
                    mBarometerView = (TextView) findViewById(R.id.value_baro);
                    mBarometerView.setVisibility(View.VISIBLE);
                    mHumidityView = (TextView) findViewById(R.id.value_humi);
                    mHumidityView.setVisibility(View.VISIBLE);
                    mDistanceView = (TextView) findViewById(R.id.value_dist);
                    mDistanceView.setVisibility(View.VISIBLE);
                    mDistanceView.setText(
                            Utilities.convertDist(0.00, mDistanceView, StartRunActivity.this));

                    chronometer.setBase(SystemClock.elapsedRealtime());
                    chronometer.start();
                    mStManager.addListener(mStListener);
                    mStManager.initServices();
                    if (!mStManager.isServicesReady()) { // initServices failed or took too long
                        android.util.Log.e(TAG, "Discover failed - exiting");
                        finish();
                        return;
                    }
                    mStManager.enableUpdates();

                    boolean res = true;
                    if (mStManager.isPeriodSupported(Sensor.IR_TEMPERATURE)) {
                        res = res && mStManager.enableSensor(Sensor.IR_TEMPERATURE,
                                UPDATE_TEMP_BARO_PERIOD_MS);
                    } else {
                        res = res && mStManager.enableSensor(Sensor.IR_TEMPERATURE);
                    }
                    if (mStManager.isPeriodSupported(Sensor.BAROMETER)) {
                        res = res && mStManager.enableSensor(Sensor.BAROMETER,
                                UPDATE_TEMP_BARO_PERIOD_MS);
                    } else {
                        res = res && mStManager.enableSensor(Sensor.BAROMETER);
                    }
                    if (mStManager.isPeriodSupported(Sensor.HUMIDITY)) {
                        res = res && mStManager.enableSensor(Sensor.HUMIDITY,
                                UPDATE_HUMIDITY_PERIOD_MS);
                    } else {
                        res = res && mStManager.enableSensor(Sensor.HUMIDITY);
                    }
                    if (!res) {
                        android.util.Log.e(TAG, "Sensor configuration failed - exiting");
                        Toast.makeText(getApplicationContext(),
                                "Sensor configuration failed - exiting",
                                        Toast.LENGTH_LONG).show();
                        finish();
                    }
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                                                           3000,
                                                           2,
                                                           locationListener);
                } else if (((String)textButton.getText()).compareTo("STOP") == 0) {

                    AlertDialog.Builder alert = new AlertDialog.Builder(
                            StartRunActivity.this);

                    alert.setTitle("Stop");
                    alert.setMessage("Are you sure want to stop the current run?");
                    alert.setPositiveButton("STOP", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {<commit_msg>back to most precise params using requestLocationUpdates<commit_after>package ca.dreamteam.logrunner;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;

import java.io.ByteArrayOutputStream;

import ca.concordia.sensortag.SensorTagListener;
import ca.concordia.sensortag.SensorTagLoggerListener;
import ca.concordia.sensortag.SensorTagManager;
import ca.concordia.sensortag.SensorTagManager.ErrorType;
import ca.concordia.sensortag.SensorTagManager.StatusType;
import ca.dreamteam.logrunner.Util.Utilities;
import ti.android.ble.sensortag.DeviceSelectActivity;
import ti.android.ble.sensortag.Sensor;

public class StartRunActivity extends Activity {

    final String TAG = "ca.dreamteam.logrunner.StartRunActivity";

    static protected final int UPDATE_TEMP_BARO_PERIOD_MS  = 30000;
    static protected final int UPDATE_HUMIDITY_PERIOD_MS = 900000;

    private TextView mTemperatureView;
    private TextView mBarometerView;
    private TextView mHumidityView;
    private TextView mDistanceView;
    private static double mAvgTemperature, mAvgHumidity, mAvgPressure;
    double latitude;
    double longitude;
    private String provider;
    private LocationManager locationManager;

    // Bluetooth communication with the SensorTag
    private BluetoothDevice mBtDevice;
    private SensorTagManager mStManager;
    private SensorTagListener mStListener;
    private GoogleMap map;
    private byte mByteArray[];

    private LocationListener locationListener = new LocationListener() {

        int counter = 0;

        @Override
        public void onLocationChanged(Location location) {
            double previous_latitude = latitude;
            double previous_longitude = longitude;
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            android.util.Log.v(TAG, "lat = " + latitude + " & lng = " + longitude);
            counter++;
            if (counter > 3) {
                map.addPolyline(
                        new PolylineOptions().
                                add(new LatLng(previous_latitude, previous_longitude),
                                        new LatLng(latitude, longitude)).width(5)
                                .color(android.graphics.Color.RED).geodesic(true));
            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // Auto-generated method stub
        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

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

        Button runButton = (Button) findViewById(R.id.runButton);

        // Get the Bluetooth device selected by the user
        mBtDevice = (BluetoothDevice) getIntent().getParcelableExtra(DeviceSelectActivity.EXTRA_DEVICE);
        if (mBtDevice == null) {
            android.util.Log.e(TAG, "No BluetoothDevice extra [" + DeviceSelectActivity.EXTRA_DEVICE
                    + "] provided in Intent.");
            Toast.makeText(this, "No Bluetooth Device selected", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        mStManager = new SensorTagManager(getApplicationContext(), mBtDevice);
        mStListener = new ManagerListener();
        final Chronometer chronometer = (Chronometer) findViewById(R.id.mChronometer);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        runButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TextView textButton = (TextView) findViewById(R.id.textButton);
                final Button tempButton = (Button) findViewById(R.id.runButton);
                final Button saveButton = (Button) findViewById(R.id.save_button);
                final Button discardButton = (Button) findViewById(R.id.discard_button);
                tempButton.setClickable(false);

                // Based on the textButton value change between Run, Stop & Save actions
                if (((String)textButton.getText()).compareTo("START RUN") == 0) {
                    tempButton.setBackgroundColor(android.graphics.Color.RED); // Blue
                    textButton.setText("STOP");

                    mAvgTemperature = mAvgHumidity = mAvgPressure = 0.0;
                    // Get references to the GUI text box objects
                    mTemperatureView = (TextView) findViewById(R.id.value_temp);
                    mTemperatureView.setVisibility(View.VISIBLE);
                    mBarometerView = (TextView) findViewById(R.id.value_baro);
                    mBarometerView.setVisibility(View.VISIBLE);
                    mHumidityView = (TextView) findViewById(R.id.value_humi);
                    mHumidityView.setVisibility(View.VISIBLE);
                    mDistanceView = (TextView) findViewById(R.id.value_dist);
                    mDistanceView.setVisibility(View.VISIBLE);
                    mDistanceView.setText(
                            Utilities.convertDist(0.00, mDistanceView, StartRunActivity.this));

                    chronometer.setBase(SystemClock.elapsedRealtime());
                    chronometer.start();
                    mStManager.addListener(mStListener);
                    mStManager.initServices();
                    if (!mStManager.isServicesReady()) { // initServices failed or took too long
                        android.util.Log.e(TAG, "Discover failed - exiting");
                        finish();
                        return;
                    }
                    mStManager.enableUpdates();

                    boolean res = true;
                    if (mStManager.isPeriodSupported(Sensor.IR_TEMPERATURE)) {
                        res = res && mStManager.enableSensor(Sensor.IR_TEMPERATURE,
                                UPDATE_TEMP_BARO_PERIOD_MS);
                    } else {
                        res = res && mStManager.enableSensor(Sensor.IR_TEMPERATURE);
                    }
                    if (mStManager.isPeriodSupported(Sensor.BAROMETER)) {
                        res = res && mStManager.enableSensor(Sensor.BAROMETER,
                                UPDATE_TEMP_BARO_PERIOD_MS);
                    } else {
                        res = res && mStManager.enableSensor(Sensor.BAROMETER);
                    }
                    if (mStManager.isPeriodSupported(Sensor.HUMIDITY)) {
                        res = res && mStManager.enableSensor(Sensor.HUMIDITY,
                                UPDATE_HUMIDITY_PERIOD_MS);
                    } else {
                        res = res && mStManager.enableSensor(Sensor.HUMIDITY);
                    }
                    if (!res) {
                        android.util.Log.e(TAG, "Sensor configuration failed - exiting");
                        Toast.makeText(getApplicationContext(),
                                "Sensor configuration failed - exiting",
                                        Toast.LENGTH_LONG).show();
                        finish();
                    }
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                                                           0,
                                                           0,
                                                           locationListener);
                } else if (((String)textButton.getText()).compareTo("STOP") == 0) {

                    AlertDialog.Builder alert = new AlertDialog.Builder(
                            StartRunActivity.this);

                    alert.setTitle("Stop");
                    alert.setMessage("Are you sure want to stop the current run?");
                    alert.setPositiveButton("STOP", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
{'fraction_non_alphanumeric': 0.08224572457245724, 'fraction_numerical': 0.0027565256525652567, 'mean_word_length': 1.9320468414976084, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6441617', 'n_tokens_mistral': 4567, 'n_tokens_neox': 4249, 'n_words': 862}