<commit_before>/**
 * lib.survey
 */
var log = require('utils/log');
var events = require('event');
var notifications = require('notifications');

// Internals
var timing = false,
    serviceTrack = { service: null, notification: null }, 
    onServiceTrackEnd;  // Only for Android

var surveyTimer = module.exports = {
    /**
     * @method startSurvey
     * @return {Object} Survey object
     */
    startSurvey: function (userId) {
        // Get settings
        var settings = Ti.App.Properties.getObject('app-survey-settings');
        var totalTime = settings ? settings.surveyDuration : Alloy.CFG.surveyDuration;
        var startTime = new Date().getTime();
        var endTime = startTime + ( totalTime * 60000 );
        // Create uuid
        var uuid = require('uuid').create();
        // Create survey object
        var surveyObject = { surveyId: uuid.toString(), startTime: startTime, endTime: endTime };
        // Save active survey both persistent as in local memory
        saveSurveyData(surveyObject, userId);
        // Start trackingService on the background if we quit the app
        surveyTimer.startTrackingService();
        // Set a local notification
        setLocalNotification(endTime);
        log.info('[lib/survey] StartSurvey - ', surveyObject);
        // Return  data
        return surveyObject;
    },
    /**
     * @method activeSurvey
     * Return app-survey object from app properties
     * @return {Object} Active survey data object
     */
    activeSurvey: function () {
        log.info('[lib/survey] Return activeSurvey', Ti.App.Properties.getObject('app-survey'));
        return Ti.App.Properties.getObject('app-survey');
    },
    /**
     * @method stopSurvey
     * Stop survey by removing property
     */
    stopSurvey: function () {
        log.info('[lib/survey] Stop current survey');

        // Remove the background service
        if (OS_IOS && Ti.App.iOS.BackgroundService) {
            Ti.App.iOS.BackgroundService.unregister();
        } else if (OS_ANDROID && serviceTrack) {
            serviceTrack.stop();
            serviceTrack = null;
        }

        // Stop polling
        surveyTimer.stopTrackLocation();
    },

    /**
     * [destroySurvey description]
     * @return {[type]} [description]
     */
    destroySurvey: function () {
        surveyTimer.stopSurvey();
        var surveyData = Ti.App.Properties.getObject('app-survey');
        // Remove all data reference to the survey
        Ti.App.Properties.removeProperty('app-survey');
        Ti.App.Properties.removeProperty('app-survey-user');
        // Remove any left over events
        require('event').destroySurveyEvent();<commit_msg>Fix the crash when canceling the survey.<commit_after>/**
 * lib.survey
 */
var log = require('utils/log');
var events = require('event');
var notifications = require('notifications');

// Internals
var timing = false,
    serviceTrack = { service: null, notification: null }, 
    onServiceTrackEnd;  // Only for Android

var surveyTimer = module.exports = {
    /**
     * @method startSurvey
     * @return {Object} Survey object
     */
    startSurvey: function (userId) {
        // Get settings
        var settings = Ti.App.Properties.getObject('app-survey-settings');
        var totalTime = settings ? settings.surveyDuration : Alloy.CFG.surveyDuration;
        var startTime = new Date().getTime();
        var endTime = startTime + ( totalTime * 60000 );
        // Create uuid
        var uuid = require('uuid').create();
        // Create survey object
        var surveyObject = { surveyId: uuid.toString(), startTime: startTime, endTime: endTime };
        // Save active survey both persistent as in local memory
        saveSurveyData(surveyObject, userId);
        // Start trackingService on the background if we quit the app
        surveyTimer.startTrackingService();
        // Set a local notification
        setLocalNotification(endTime);
        log.info('[lib/survey] StartSurvey - ', surveyObject);
        // Return  data
        return surveyObject;
    },
    /**
     * @method activeSurvey
     * Return app-survey object from app properties
     * @return {Object} Active survey data object
     */
    activeSurvey: function () {
        log.info('[lib/survey] Return activeSurvey', Ti.App.Properties.getObject('app-survey'));
        return Ti.App.Properties.getObject('app-survey');
    },
    /**
     * @method stopSurvey
     * Stop survey by removing property
     */
    stopSurvey: function () {
        log.info('[lib/survey] Stop current survey');

        // Remove the background service
        if (OS_IOS && Ti.App.iOS.BackgroundService) {
            Ti.App.iOS.BackgroundService.unregister();
        } else if (OS_ANDROID && serviceTrack.service) {
            serviceTrack.service.stop();
            serviceTrack.service = null;
        }

        // Stop polling
        surveyTimer.stopTrackLocation();
    },

    /**
     * [destroySurvey description]
     * @return {[type]} [description]
     */
    destroySurvey: function () {
        surveyTimer.stopSurvey();
        var surveyData = Ti.App.Properties.getObject('app-survey');
        // Remove all data reference to the survey
        Ti.App.Properties.removeProperty('app-survey');
        Ti.App.Properties.removeProperty('app-survey-user');
        // Remove any left over events
        require('event').destroySurveyEvent();
{'fraction_non_alphanumeric': 0.11950726236440522, 'fraction_numerical': 0.0018385732671446957, 'mean_word_length': 2.4408602150537635, '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': '5477118', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1459, 'n_words': 373}