<commit_before>/**************************************************************************************************
 * Copyright (C) 2010 Sense Observation Systems, Rotterdam, the Netherlands. All rights reserved. *
 *************************************************************************************************/
package nl.sense_os.service.location;

import nl.sense_os.service.R;
import nl.sense_os.service.constants.SenseDataTypes;
import nl.sense_os.service.constants.SensePrefs;
import nl.sense_os.service.constants.SensePrefs.Main;
import nl.sense_os.service.constants.SensorData.DataPoint;
import nl.sense_os.service.constants.SensorData.SensorNames;
import nl.sense_os.service.ctrl.Controller;
import nl.sense_os.service.provider.SNTP;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

/**
 * Represents the location sensor. Implements the basic Android LocationListener to receive location
 * updates from the OS.<br/>
 * <br/>
 * Generates data for the following sensors:
 * <ul>
 * <li>position</li>
 * <li>traveled distance 1h</li>
 * </ul>
 * 
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class LocationSensor {
	
	private static LocationSensor instance = null;
	
    protected LocationSensor(Context context) {
		this.context = context;
		locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        controller = Controller.getController(context);
		gpsListener = new MyLocationListener();
		nwListener = new MyLocationListener();
		pasListener = new MyLocationListener();
		distanceEstimator = new TraveledDistanceEstimator();
	}
    
    public static LocationSensor getInstance(Context context) {
	    if(instance == null) {
	       instance = new LocationSensor(context);
	    }
	    return instance;
    }
	
	private class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location fix) {

			if (null != lastGpsFix) {
				if (SNTP.getInstance().getTime() - lastGpsFix.getTime() < MIN_SAMPLE_DELAY) {
					// Log.v(TAG, "New location fix is too fast after last one");
					return;
				}
			}
			
			//controller = Controller.getController(context);
			JSONObject json = new JSONObject();
			try {
				json.put("latitude", fix.getLatitude());
				json.put("longitude", fix.getLongitude());

				// always include all JSON fields, or we get problems with varying data_structure
				json.put("accuracy", fix.hasAccuracy() ? fix.getAccuracy() : -1.0d);
				json.put("altitude", fix.hasAltitude() ? fix.getAltitude() : -1.0);
				json.put("speed", fix.hasSpeed() ? fix.getSpeed() : -1.0d);
				json.put("bearing", fix.hasBearing() ? fix.getBearing() : -1.0d);
				json.put("provider", null != fix.getProvider() ? fix.getProvider() : "unknown");

				if (fix.getProvider().equals(LocationManager.GPS_PROVIDER)) {
					lastGpsFix = fix;
				} 
				else if (fix.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
					lastNwFix = fix;
				}
				else {		
					//do nothing
				} 

			} catch (JSONException e) {
				Log.e(TAG, "JSONException in onLocationChanged", e);
				return;
			}

			// pass message to the MsgHandler
			Intent i = new Intent(context.getString(R.string.action_sense_new_data));
			i.putExtra(DataPoint.SENSOR_NAME, SensorNames.LOCATION);
			i.putExtra(DataPoint.VALUE, json.toString());
			i.putExtra(DataPoint.DATA_TYPE, SenseDataTypes.JSON);
			i.putExtra(DataPoint.TIMESTAMP, fix.getTime());
			context.startService(i);

			distanceEstimator.addPoint(fix);
		}

		@Override
		public void onProviderDisabled(String provider) {
			controller.checkSensorSettings(isGpsAllowed, isListeningNw, isListeningGps, time, lastGpsFix, listenGpsStart, lastNwFix, listenNwStart, listenGpsStop, listenNwStop);
		}

		@Override
		public void onProviderEnabled(String provider) {
			controller.checkSensorSettings(isGpsAllowed, isListeningNw, isListeningGps, time, lastGpsFix, listenGpsStart, lastNwFix, listenNwStart, listenGpsStop, listenNwStop);
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// do nothing
		}
	}

	private static final String TAG = "Sense LocationSensor";<commit_msg>hotfix: use SNTP time for position sensor<commit_after>/**************************************************************************************************
 * Copyright (C) 2010 Sense Observation Systems, Rotterdam, the Netherlands. All rights reserved. *
 *************************************************************************************************/
package nl.sense_os.service.location;

import nl.sense_os.service.R;
import nl.sense_os.service.constants.SenseDataTypes;
import nl.sense_os.service.constants.SensePrefs;
import nl.sense_os.service.constants.SensePrefs.Main;
import nl.sense_os.service.constants.SensorData.DataPoint;
import nl.sense_os.service.constants.SensorData.SensorNames;
import nl.sense_os.service.ctrl.Controller;
import nl.sense_os.service.provider.SNTP;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

/**
 * Represents the location sensor. Implements the basic Android LocationListener to receive location
 * updates from the OS.<br/>
 * <br/>
 * Generates data for the following sensors:
 * <ul>
 * <li>position</li>
 * <li>traveled distance 1h</li>
 * </ul>
 * 
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class LocationSensor {
	
	private static LocationSensor instance = null;
	
    protected LocationSensor(Context context) {
		this.context = context;
		locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        controller = Controller.getController(context);
		gpsListener = new MyLocationListener();
		nwListener = new MyLocationListener();
		pasListener = new MyLocationListener();
		distanceEstimator = new TraveledDistanceEstimator();
	}
    
    public static LocationSensor getInstance(Context context) {
	    if(instance == null) {
	       instance = new LocationSensor(context);
	    }
	    return instance;
    }
	
	private class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location fix) {

			if (null != lastGpsFix) {
				if (SNTP.getInstance().getTime() - lastGpsFix.getTime() < MIN_SAMPLE_DELAY) {
					// Log.v(TAG, "New location fix is too fast after last one");
					return;
				}
			}
			
			//controller = Controller.getController(context);
			JSONObject json = new JSONObject();
			try {
				json.put("latitude", fix.getLatitude());
				json.put("longitude", fix.getLongitude());

				// always include all JSON fields, or we get problems with varying data_structure
				json.put("accuracy", fix.hasAccuracy() ? fix.getAccuracy() : -1.0d);
				json.put("altitude", fix.hasAltitude() ? fix.getAltitude() : -1.0);
				json.put("speed", fix.hasSpeed() ? fix.getSpeed() : -1.0d);
				json.put("bearing", fix.hasBearing() ? fix.getBearing() : -1.0d);
				json.put("provider", null != fix.getProvider() ? fix.getProvider() : "unknown");

				if (fix.getProvider().equals(LocationManager.GPS_PROVIDER)) {
					lastGpsFix = fix;
				} 
				else if (fix.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
					lastNwFix = fix;
				}
				else {		
					//do nothing
				} 

			} catch (JSONException e) {
				Log.e(TAG, "JSONException in onLocationChanged", e);
				return;
			}

            // use SNTP time
            long timestamp = SNTP.getInstance().getTime();

			// pass message to the MsgHandler
			Intent i = new Intent(context.getString(R.string.action_sense_new_data));
			i.putExtra(DataPoint.SENSOR_NAME, SensorNames.LOCATION);
			i.putExtra(DataPoint.VALUE, json.toString());
			i.putExtra(DataPoint.DATA_TYPE, SenseDataTypes.JSON);
            i.putExtra(DataPoint.TIMESTAMP, timestamp);
			context.startService(i);

			distanceEstimator.addPoint(fix);
		}

		@Override
		public void onProviderDisabled(String provider) {
			controller.checkSensorSettings(isGpsAllowed, isListeningNw, isListeningGps, time, lastGpsFix, listenGpsStart, lastNwFix, listenNwStart, listenGpsStop, listenNwStop);
		}

		@Override
		public void onProviderEnabled(String provider) {
			controller.checkSensorSettings(isGpsAllowed, isListeningNw, isListeningGps, time, lastGpsFix, listenGpsStart, lastNwFix, listenNwStart, listenGpsStop, listenNwStop);
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// do nothing
		}
	}

	private static final String TAG = "Sense LocationSensor";
{'fraction_non_alphanumeric': 0.15297389816003423, 'fraction_numerical': 0.0027813436029097134, 'mean_word_length': 4.545077105575326, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, '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': '3627345', 'n_tokens_mistral': 3090, 'n_tokens_neox': 2777, 'n_words': 607}