<commit_before>package com.neurodoro.muse;

import android.support.annotation.Nullable;
import android.util.Log;

import com.choosemuse.libmuse.Eeg;
import com.choosemuse.libmuse.Muse;
import com.choosemuse.libmuse.MuseArtifactPacket;
import com.choosemuse.libmuse.MuseDataListener;
import com.choosemuse.libmuse.MuseDataPacket;
import com.choosemuse.libmuse.MuseDataPacketType;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.neurodoro.MainApplication;
import com.neurodoro.signal.CircularBuffer;
import com.neurodoro.signal.FFT;
import com.neurodoro.signal.Filter;
import com.neurodoro.signal.DynamicSeries;
import com.neurodoro.signal.NoiseDetector;
import com.neurodoro.signal.PSDBuffer2D;
import com.neurodoro.muse.MuseDataSource;

/**
 * Streams data from Muses and performs preprocessing functions
 */

public class MuseRecorder extends ReactContextBaseJavaModule {

    // ----------------------------------------------------------
    // Variables

    public DynamicSeries dataSeries;
    String TAG = "EEGGraph";
    public int BUFFER_LENGTH;
    Thread dataThread;
    MuseDataSource data;
    public DataListener dataListener;
    public CircularBuffer eegBuffer;
    public double[] newData = new double[4];
    public String userName = "";
    public int fileNum = 0;

    // Filter variables
    public int filterFreq;
    public Filter bandPassFilter;
    public double[][] bandPassFiltState;
    public double[] filtResult = new double[4];

    // Bridged props
    public String recorderDataType = "DENOISED_PSD";

    // Reference to global Muse
    MainApplication appState;

    // ---------------------------------------------------------
    // Constructor
    public MuseRecorder(ReactApplicationContext reactContext) {
        super(reactContext);
        appState = ((MainApplication)reactContext.getApplicationContext());
    }

    // ---------------------------------------------------------
    // React Native Module methods
    // Required by ReactContextBaseJavaModule

    @Override
    public String getName() {
        return "MuseRecorder";
    }

    // Called to emit events to event listeners in JS
    private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);
    }

    // ---------------------------------------------------------
    // Bridged methods

    @ReactMethod
    public void startRecording(String dataType) {
        Log.w("Listener", "Start Listening Called");
        this.recorderDataType = dataType;
        initListener();
        data.fileWriter.initFile();
        startDataThread();
    }

    @ReactMethod
    public void stopRecording() {
        Log.w("Listener", "Stop Listening Called");
        stopThreads();
        fileNum++;
    }

    @ReactMethod
    public void sendTaskInfo(int difficulty, int performance) {
        data.fileWriter.updateTaskInfo(difficulty, performance);
    }

    @ReactMethod
    public void setUserName(String name) {
        this.userName = name;
    }

    // ---------------------------------------------------------
    // Internal Methods
<commit_msg>Added null check in sendTaskInfo to prevent crash when taking test without recording<commit_after>package com.neurodoro.muse;

import android.support.annotation.Nullable;
import android.util.Log;

import com.choosemuse.libmuse.Eeg;
import com.choosemuse.libmuse.Muse;
import com.choosemuse.libmuse.MuseArtifactPacket;
import com.choosemuse.libmuse.MuseDataListener;
import com.choosemuse.libmuse.MuseDataPacket;
import com.choosemuse.libmuse.MuseDataPacketType;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.neurodoro.MainApplication;
import com.neurodoro.signal.CircularBuffer;
import com.neurodoro.signal.FFT;
import com.neurodoro.signal.Filter;
import com.neurodoro.signal.DynamicSeries;
import com.neurodoro.signal.NoiseDetector;
import com.neurodoro.signal.PSDBuffer2D;
import com.neurodoro.muse.MuseDataSource;

/**
 * Streams data from Muses and performs preprocessing functions
 */

public class MuseRecorder extends ReactContextBaseJavaModule {

    // ----------------------------------------------------------
    // Variables

    public DynamicSeries dataSeries;
    String TAG = "EEGGraph";
    public int BUFFER_LENGTH;
    Thread dataThread;
    MuseDataSource data;
    public DataListener dataListener;
    public CircularBuffer eegBuffer;
    public double[] newData = new double[4];
    public String userName = "";
    public int fileNum = 0;

    // Filter variables
    public int filterFreq;
    public Filter bandPassFilter;
    public double[][] bandPassFiltState;
    public double[] filtResult = new double[4];

    // Bridged props
    public String recorderDataType = "DENOISED_PSD";

    // Reference to global Muse
    MainApplication appState;

    // ---------------------------------------------------------
    // Constructor
    public MuseRecorder(ReactApplicationContext reactContext) {
        super(reactContext);
        appState = ((MainApplication)reactContext.getApplicationContext());
    }

    // ---------------------------------------------------------
    // React Native Module methods
    // Required by ReactContextBaseJavaModule

    @Override
    public String getName() {
        return "MuseRecorder";
    }

    // Called to emit events to event listeners in JS
    private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);
    }

    // ---------------------------------------------------------
    // Bridged methods

    @ReactMethod
    public void startRecording(String dataType) {
        Log.w("Listener", "Start Listening Called");
        this.recorderDataType = dataType;
        initListener();
        data.fileWriter.initFile();
        startDataThread();
    }

    @ReactMethod
    public void stopRecording() {
        Log.w("Listener", "Stop Listening Called");
        stopThreads();
        fileNum++;
    }

    @ReactMethod
    public void sendTaskInfo(int difficulty, int performance) {
        if (data.fileWriter != null) {
            data.fileWriter.updateTaskInfo(difficulty, performance);
        }
    }

    @ReactMethod
    public void setUserName(String name) {
        this.userName = name;
    }

    // ---------------------------------------------------------
    // Internal Methods

{'fraction_non_alphanumeric': 0.15989423879766212, 'fraction_numerical': 0.001113275814082939, 'mean_word_length': 2.4486564299424183, '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': '7319750', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1891, 'n_words': 425}