Commit 18d24ab6 authored by Javier Baliosian's avatar Javier Baliosian
Browse files

primera adaptacion del código de LocationTacker de android

parent b2700dad
......@@ -26,6 +26,10 @@ package com.elbitsalvaje.wifitracker;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
......@@ -44,6 +48,7 @@ package com.elbitsalvaje.wifitracker;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
......@@ -88,6 +93,14 @@ public class TrackerService extends Service {
private PreferenceListener mPrefListener;
private SensorManager sensorManager;
private Sensor sensorAccelerometer;
private SensorReceiver positionReceiver;
private SensorEvent lastSensorEvent;
private Location lastLocation;
private float lastDistance;
public TrackerService() {
}
......@@ -109,6 +122,7 @@ public class TrackerService extends Service {
mNetworkLocation = null;
initLocationListeners();
Toast.makeText(this, "Tracking service started", Toast.LENGTH_SHORT);
}
......@@ -119,28 +133,34 @@ public class TrackerService extends Service {
mTrackedProviders = getTrackedProviders();
List<String> locationProviders = lm.getAllProviders();
mListeners = new ArrayList<LocationTrackingListener>(
locationProviders.size());
mListeners = new ArrayList<LocationTrackingListener>(locationProviders.size());
long minUpdateTime = getLocationUpdateTime();
float minDistance = getLocationMinDistance();
for (String providerName : locationProviders) {
if (mTrackedProviders.contains(providerName)) {
Log.d(LOG_TAG, "Adding location listener for provider " +
providerName);
Log.d(LOG_TAG, "Adding location listener for provider " + providerName);
if (doDebugLogging()) {
mTrackerData.writeEntry("init", String.format(
"start listening to %s : %d ms; %f meters",
providerName, minUpdateTime, minDistance));
}
LocationTrackingListener listener =
new LocationTrackingListener();
lm.requestLocationUpdates(providerName, minUpdateTime,
minDistance, listener);
LocationTrackingListener listener = new LocationTrackingListener();
lm.requestLocationUpdates(providerName, minUpdateTime, minDistance, listener);
mListeners.add(listener);
}
}
// addition for sensing position
sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
positionReceiver = new SensorReceiver();
sensorManager.registerListener(positionReceiver, sensorAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);
//------------
mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
if (doDebugLogging()) {
......@@ -159,8 +179,7 @@ public class TrackerService extends Service {
}
if (trackSignalStrength()) {
mTelephonyManager.listen(mPhoneStateListener,
PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
// register for preference changes, so we can restart listeners on
......@@ -248,6 +267,11 @@ public class TrackerService extends Service {
}
mListeners = null;
// stop sensor listener
if (sensorManager != null) {
sensorManager.unregisterListener(positionReceiver);
}
// stop cell state listener
if (mTelephonyManager != null) {
mTelephonyManager.listen(mPhoneStateListener, 0);
......@@ -303,7 +327,14 @@ public class TrackerService extends Service {
return;
}
float distance = getDistanceFromNetwork(location);
mTrackerData.writeEntry(location, distance);
if (lastSensorEvent == null)
mTrackerData.writeEntry(location, distance);
else
mTrackerData.writeEntry(location, distance, lastSensorEvent);
lastLocation = location;
lastDistance = distance;
}
/**
......@@ -387,8 +418,7 @@ public class TrackerService extends Service {
String action = intent.getAction();
if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
WifiManager wifiManager =
(WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> wifiScanResults = wifiManager.getScanResults();
String updateMsg = "num scan results=" +
(wifiScanResults == null ? "0" : wifiScanResults.size());
......@@ -432,6 +462,24 @@ public class TrackerService extends Service {
}
}
private class SensorReceiver implements SensorEventListener {
public void onSensorChanged(SensorEvent event) {
if (lastLocation != null)
mTrackerData.writeEntry(lastLocation, lastDistance, event);
lastSensorEvent = event;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
private class PreferenceListener implements OnSharedPreferenceChangeListener {
public void onSharedPreferenceChanged(
......
......@@ -70,6 +70,12 @@ class CSVFormatter implements IFormatter {
rowOutput.append(entry.getLocation().getBearing());
}
rowOutput.append(DELIMITER);
rowOutput.append(entry.getValueAzimuth());
rowOutput.append(DELIMITER);
rowOutput.append(entry.getValuePitch());
rowOutput.append(DELIMITER);
rowOutput.append(entry.getValueRoll());
rowOutput.append(DELIMITER);
rowOutput.append(entry.getDistFromNetLocation());
rowOutput.append(DELIMITER);
rowOutput.append(DateUtils.getKMLTimestamp(entry.getLocation().getTime()));
......
......@@ -18,6 +18,7 @@ package com.elbitsalvaje.wifitracker.data;
import android.content.Context;
import android.database.Cursor;
import android.hardware.SensorEvent;
import android.location.Location;
/**
......@@ -84,6 +85,14 @@ public class TrackerDataHelper {
writeEntry(TrackerEntry.createEntry(loc, distFromNetLoc));
}
/**
* insert given location into tracker data
*/
public void writeEntry(Location loc, float distFromNetLoc, SensorEvent event) {
writeEntry(TrackerEntry.createEntry(loc, distFromNetLoc, event));
}
/**
* insert given log message into tracker data
*/
......
......@@ -19,6 +19,7 @@ package com.elbitsalvaje.wifitracker.data;
import android.content.ContentValues;
import android.database.Cursor;
import android.hardware.SensorEvent;
import android.location.Location;
......@@ -61,10 +62,11 @@ class TrackerEntry {
static final String[] ATTRIBUTES = {
ID_COL, TIMESTAMP, TAG, ENTRY_TYPE, ACCURACY, LATITUDE, LONGITUDE,
ALTITUDE, SPEED, BEARING, DIST_NET_LOCATION, LOC_TIME, DEBUG_INFO};
ALTITUDE, SPEED, BEARING, AZIMUTH, PITCH, ROLL, DIST_NET_LOCATION, LOC_TIME, DEBUG_INFO};
static final String[] ATTRIBUTES_DATA_TYPE = {
INT_DATA + " PRIMARY KEY", STRING_DATA, STRING_DATA, STRING_DATA,
REAL_DATA, REAL_DATA, REAL_DATA, REAL_DATA, REAL_DATA, REAL_DATA,
REAL_DATA, REAL_DATA, REAL_DATA, REAL_DATA, REAL_DATA, REAL_DATA,REAL_DATA, REAL_DATA, REAL_DATA,
REAL_DATA, INT_DATA, STRING_DATA};
// location extra keys used to retrieve debug info
......@@ -83,6 +85,11 @@ class TrackerEntry {
private String mTag;
private EntryType mType;
//orientation information
private float valueAzimuth = 0;
private float valuePitch = 0;
private float valueRoll = 0;
private TrackerEntry(String tag, EntryType type) {
mType = type;
mTag = tag;
......@@ -94,6 +101,17 @@ class TrackerEntry {
mLocation = new Location(loc);
}
private TrackerEntry(Location loc, SensorEvent event ) {
this(loc.getProvider(), EntryType.LOCATION_TYPE);
mLocation = new Location(loc);
valueAzimuth = event.values[0];
valuePitch = event.values[1];
valueRoll = event.values[2];
}
/**
* Creates a TrackerEntry from a Location
*/
......@@ -106,6 +124,19 @@ class TrackerEntry {
return entry;
}
/**
* Creates a TrackerEntry from a Location
*/
static TrackerEntry createEntry(Location loc, float distFromNetLocation, SensorEvent event) {
TrackerEntry entry = new TrackerEntry(loc,event);
String timestampVal = DateUtils.getCurrentKMLTimestamp();
entry.setTimestamp(timestampVal);
entry.setDistFromNetLocation(distFromNetLocation);
return entry;
}
/**
* Creates a TrackerEntry from a log msg
*/
......@@ -117,6 +148,79 @@ class TrackerEntry {
return entry;
}
static TrackerEntry createEntry(Cursor cursor) {
String timestamp = cursor.getString(cursor.getColumnIndex(TIMESTAMP));
String tag = cursor.getString(cursor.getColumnIndex(TAG));
String sType = cursor.getString(cursor.getColumnIndex(ENTRY_TYPE));
TrackerEntry entry = new TrackerEntry(tag, EntryType.valueOf(sType));
entry.setTimestamp(timestamp);
if (entry.getType() == EntryType.LOCATION_TYPE) {
Location location = new Location(tag);
location.setLatitude(cursor.getFloat(cursor.getColumnIndexOrThrow(LATITUDE)));
location.setLongitude(cursor.getFloat(cursor.getColumnIndexOrThrow(LONGITUDE)));
Float accuracy = getNullableFloat(cursor, ACCURACY);
if (accuracy != null) {
location.setAccuracy(accuracy);
}
Float altitude = getNullableFloat(cursor, ALTITUDE);
if (altitude != null) {
location.setAltitude(altitude);
}
Float bearing = getNullableFloat(cursor, BEARING);
if (bearing != null) {
location.setBearing(bearing);
}
Float speed = getNullableFloat(cursor, SPEED);
if (speed != null) {
location.setSpeed(speed);
}
location.setTime(cursor.getLong(cursor.getColumnIndex(LOC_TIME)));
entry.setLocation(location);
// added to store position
Float azimuth = getNullableFloat(cursor, AZIMUTH);
if (azimuth != null) {
entry.setValueAzimuth(azimuth);
}
Float pitch = getNullableFloat(cursor, PITCH);
if (pitch != null) {
entry.setValuePitch(pitch);
}
Float roll = getNullableFloat(cursor, ROLL);
if (roll != null) {
entry.setValueRoll(roll);
}
}
entry.setLogMsg(cursor.getString(cursor.getColumnIndex(DEBUG_INFO)));
return entry;
}
public float getValueAzimuth() {
return valueAzimuth;
}
public void setValueAzimuth(float valueAzimuth) {
this.valueAzimuth = valueAzimuth;
}
public float getValuePitch() {
return valuePitch;
}
public void setValuePitch(float valuePitch) {
this.valuePitch = valuePitch;
}
public float getValueRoll() {
return valueRoll;
}
public void setValueRoll(float valueRoll) {
this.valueRoll = valueRoll;
}
private void setTimestamp(String timestamp) {
mTimestamp = timestamp;
}
......@@ -191,6 +295,15 @@ class TrackerEntry {
if (mLocation.hasBearing()) {
cValues.put(BEARING, mLocation.getBearing());
}
if (valueAzimuth != 0) {
cValues.put(AZIMUTH, valueAzimuth);
}
if (valuePitch != 0) {
cValues.put(PITCH, valuePitch);
}
if (valueRoll != 0) {
cValues.put(ROLL, valueRoll);
}
cValues.put(DIST_NET_LOCATION, mDistFromNetLocation);
cValues.put(LOC_TIME, mLocation.getTime());
StringBuilder debugBuilder = new StringBuilder("");
......@@ -210,40 +323,6 @@ class TrackerEntry {
return cValues;
}
static TrackerEntry createEntry(Cursor cursor) {
String timestamp = cursor.getString(cursor.getColumnIndex(TIMESTAMP));
String tag = cursor.getString(cursor.getColumnIndex(TAG));
String sType = cursor.getString(cursor.getColumnIndex(ENTRY_TYPE));
TrackerEntry entry = new TrackerEntry(tag, EntryType.valueOf(sType));
entry.setTimestamp(timestamp);
if (entry.getType() == EntryType.LOCATION_TYPE) {
Location location = new Location(tag);
location.setLatitude(cursor.getFloat(cursor.getColumnIndexOrThrow(LATITUDE)));
location.setLongitude(cursor.getFloat(cursor.getColumnIndexOrThrow(LONGITUDE)));
Float accuracy = getNullableFloat(cursor, ACCURACY);
if (accuracy != null) {
location.setAccuracy(accuracy);
}
Float altitude = getNullableFloat(cursor, ALTITUDE);
if (altitude != null) {
location.setAltitude(altitude);
}
Float bearing = getNullableFloat(cursor, BEARING);
if (bearing != null) {
location.setBearing(bearing);
}
Float speed = getNullableFloat(cursor, SPEED);
if (speed != null) {
location.setSpeed(speed);
}
location.setTime(cursor.getLong(cursor.getColumnIndex(LOC_TIME)));
entry.setLocation(location);
}
entry.setLogMsg(cursor.getString(cursor.getColumnIndex(DEBUG_INFO)));
return entry;
}
private static Float getNullableFloat(Cursor cursor, String colName) {
Float retValue = null;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment