<commit_before>package epcc.ed.ac.uk.gcrf_rear;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ToggleButton;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import epcc.ed.ac.uk.gcrf_rear.data.DatabaseThread;

public class MainActivity extends AppCompatActivity implements LocationListener {

    private DatabaseThread mDatabase;
    private LocationManager mLocationManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDatabase = ((REARApplication)getApplication()).getDatabase();
        SharedPreferences settings = getSharedPreferences(SettingsActivity.PREFS_NAME, 0);
        int rate = settings.getInt(SettingsActivity.FREQUENCY, 100);
        final int samplingPeriod;
        if (rate > 0) {
            samplingPeriod = 1000000 / rate;
        }
        else {
            samplingPeriod = 10000; // rate of 100 Hertz i.e. sampling period 10,000 microseconds
        }

        ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Log.d("main", "sampling period: " + samplingPeriod + "microseconds");
                    mDatabase.setFileStoreOn(true);
                    SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
                    Sensor senAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
                    if (senAccelerometer != null) {
                        sensorManager.registerListener((SensorEventListener) getApplication(), senAccelerometer, samplingPeriod);
                        Log.d("main", "registered listener for accelerometer");
                    }
                    Sensor senGyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
                    if (senGyroscope != null) {
                        sensorManager.registerListener((SensorEventListener) getApplication(), senGyroscope, samplingPeriod);
                        Log.d("main", "registered listener for gyroscope");
                    }
                    Sensor senMagneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
                    if (senMagneticField != null) {
                        sensorManager.registerListener((SensorEventListener) getApplication(), senMagneticField, samplingPeriod);
                        Log.d("main", "registered listener for magnetic field");
                    }
//                    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//                    try {
//                        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0l, 0f, MainActivity.this);
//                    }
//                    catch (SecurityException e) {
//                        // check permissions
//                    }

                } else {
                    SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
                    sensorManager.unregisterListener((SensorEventListener) getApplication());
                    mDatabase.setFileStoreOn(false);
                    mDatabase.close();
                }
            }
        });

//        ToggleButton displayToggle = (ToggleButton) findViewById(R.id.displayToggleButton);
//        displayToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//                    Sensor senAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//                    if (senAccelerometer != null) {
//                        sensorManager.registerListener((SensorEventListener) getApplication(), senAccelerometer, mSamplingPeriod);
//                        Log.d("main", "registered listener for accelerometer");
//                    }
//                }
//                else {
//                    SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//                    sensorManager.unregisterListener((SensorEventListener) getApplication());
//                }
//                ((REARApplication) getApplication()).getDatabase().setDisplayOn(isChecked);
//            }
//        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void registerDevice(String password) {<commit_msg>check boxes for sensor types fixed bug in closing data store when no records written<commit_after>package epcc.ed.ac.uk.gcrf_rear;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import epcc.ed.ac.uk.gcrf_rear.data.DatabaseThread;

public class MainActivity extends AppCompatActivity implements LocationListener {

    private DatabaseThread mDatabase;
    private LocationManager mLocationManager;

    private static final int DEFAULT_SAMPLING_RATE = 100; // default is 100 Hertz


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDatabase = ((REARApplication)getApplication()).getDatabase();
        SharedPreferences settings = getSharedPreferences(SettingsActivity.PREFS_NAME, 0);
        int rate = settings.getInt(SettingsActivity.FREQUENCY, DEFAULT_SAMPLING_RATE);
        if (rate <= 0) {
            rate = DEFAULT_SAMPLING_RATE;
        }
        final int samplingPeriod = 1000000 / rate;
        TextView freqText = (TextView)findViewById(R.id.main_frequency_text);
        freqText.setText("Frequency: " + rate + " Hertz");
        final SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        final Sensor senAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        final Sensor senGyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        final Sensor senMagneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        if (senAccelerometer == null) {
            findViewById(R.id.track_accel_checkBox).setVisibility(View.GONE);
        }
        if (senGyroscope == null) {
            findViewById(R.id.track_gyro_checkBox).setVisibility(View.GONE);
        }
        if (senMagneticField == null) {
            findViewById(R.id.track_magnet_checkBox).setVisibility(View.GONE);
        }
        final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Log.d("main", "sampling period: " + samplingPeriod + " microseconds");
                    mDatabase.setFileStoreOn(true);
                    if (senAccelerometer != null && ((CheckBox) findViewById(R.id.track_accel_checkBox)).isChecked()) {
                        sensorManager.registerListener((SensorEventListener) getApplication(), senAccelerometer, samplingPeriod);
                        Log.d("main", "registered listener for accelerometer");
                    }
                    if (senGyroscope != null && ((CheckBox) findViewById(R.id.track_accel_checkBox)).isChecked()) {
                        sensorManager.registerListener((SensorEventListener) getApplication(), senGyroscope, samplingPeriod);
                        Log.d("main", "registered listener for gyroscope");
                    }
                    if (senMagneticField != null && ((CheckBox) findViewById(R.id.track_accel_checkBox)).isChecked()) {
                        sensorManager.registerListener((SensorEventListener) getApplication(), senMagneticField, samplingPeriod);
                        Log.d("main", "registered listener for magnetic field");
                    }
//                    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//                    try {
//                        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0l, 0f, MainActivity.this);
//                    }
//                    catch (SecurityException e) {
//                        // check permissions
//                    }

                } else {
                    sensorManager.unregisterListener((SensorEventListener) getApplication());
                    mDatabase.setFileStoreOn(false);
                    mDatabase.close();
                }
            }
        });
        CheckBox accelCheckBox = (CheckBox) findViewById(R.id.track_accel_checkBox);
        accelCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Sensor sensor = senAccelerometer;
                if (sensor != null && toggle.isChecked()) {
                    if (((CheckBox) v).isChecked()) {
                        sensorManager.registerListener((SensorEventListener) getApplication(), sensor, samplingPeriod);
                        Log.d("main", "registered listener for accelerometer");
                    } else {
                        sensorManager.unregisterListener((SensorEventListener) getApplication(), sensor);
                    }
                }
            }
        });
        CheckBox gyroCheckBox = (CheckBox) findViewById(R.id.track_gyro_checkBox);
        gyroCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Sensor sensor = senGyroscope;
                if (sensor != null && toggle.isChecked()) {
                    if (((CheckBox) v).isChecked()) {
                        sensorManager.registerListener((SensorEventListener) getApplication(), sensor, samplingPeriod);
                        Log.d("main", "registered listener for accelerometer");
                    } else {
                        sensorManager.unregisterListener((SensorEventListener) getApplication(), sensor);
                    }
                }
            }
        });
        final CheckBox magnetCheckBox = (CheckBox) findViewById(R.id.track_gyro_checkBox);
        magnetCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Sensor sensor = senMagneticField;
                if (sensor != null && toggle.isChecked()) {
                    if (((CheckBox) v).isChecked()) {
                        sensorManager.registerListener((SensorEventListener) getApplication(), sensor, samplingPeriod);
                        Log.d("main", "registered listener for accelerometer");
                    } else {
                        sensorManager.unregisterListener((SensorEventListener) getApplication(), sensor);
                    }
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void registerDevice(String password) {
{'fraction_non_alphanumeric': 0.09142436510857563, 'fraction_numerical': 0.003827751196172249, 'mean_word_length': 2.4127103742778195, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '2979238', 'n_tokens_mistral': 3387, 'n_tokens_neox': 3222, 'n_words': 649}