forked from organicmaps/organicmaps
[android][temporary] Added Location logging
This commit is contained in:
parent
373fce941a
commit
9e7986538d
1 changed files with 47 additions and 11 deletions
|
@ -1,5 +1,10 @@
|
|||
package com.mapswithme.maps.location;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
@ -14,12 +19,13 @@ import android.location.Location;
|
|||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
public class LocationService implements LocationListener, SensorEventListener, WifiLocation.Listener
|
||||
{
|
||||
private static final String TAG = "LocationService";
|
||||
|
||||
|
||||
/// These constants should correspond to values defined in platform/location.hpp
|
||||
public static final int STOPPED = 0;
|
||||
public static final int STARTED = 1;
|
||||
|
@ -33,9 +39,9 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
public void onCompassUpdated(long time, double magneticNorth, double trueNorth, float accuracy);
|
||||
public void onLocationStatusChanged(int status);
|
||||
};
|
||||
|
||||
|
||||
private HashSet<Listener> m_observers = new HashSet<Listener>(2);
|
||||
|
||||
|
||||
// Used to filter locations from different providers
|
||||
private Location m_lastLocation = null;
|
||||
|
||||
|
@ -58,29 +64,40 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
m_sensorManager = (SensorManager) c.getSystemService(Context.SENSOR_SERVICE);
|
||||
if (m_sensorManager != null)
|
||||
m_compassSensor = m_sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
|
||||
File extDir = Environment.getExternalStorageDirectory();
|
||||
m_log = new File(extDir, "location_log.txt");
|
||||
try
|
||||
{
|
||||
if (m_log.exists())
|
||||
m_log.createNewFile();
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void notifyStatusChanged(int newStatus)
|
||||
{
|
||||
Iterator<Listener> it = m_observers.iterator();
|
||||
while (it.hasNext())
|
||||
it.next().onLocationStatusChanged(newStatus);
|
||||
}
|
||||
|
||||
|
||||
private void notifyLocationUpdated(long time, double lat, double lon, float accuracy)
|
||||
{
|
||||
Iterator<Listener> it = m_observers.iterator();
|
||||
while (it.hasNext())
|
||||
it.next().onLocationUpdated(time, lat, lon, accuracy);
|
||||
}
|
||||
|
||||
|
||||
private void notifyCompassUpdated(long time, double magneticNorth, double trueNorth, float accuracy)
|
||||
{
|
||||
Iterator<Listener> it = m_observers.iterator();
|
||||
while (it.hasNext())
|
||||
it.next().onCompassUpdated(time, magneticNorth, trueNorth, accuracy);
|
||||
}
|
||||
|
||||
|
||||
public void startUpdate(Listener observer, Context c)
|
||||
{
|
||||
m_observers.add(observer);
|
||||
|
@ -161,7 +178,7 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
// A new location is thrown away if it's too old
|
||||
if (java.lang.System.currentTimeMillis() - newLocation.getTime() > TWO_MINUTES)
|
||||
return false;
|
||||
|
||||
|
||||
// A new location is better than no location
|
||||
if (currentBestLocation == null)
|
||||
return true;
|
||||
|
@ -184,11 +201,11 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
// Check if the old and new location are from the same provider
|
||||
final boolean isFromSameProvider = isSameProvider(newLocation.getProvider(),
|
||||
currentBestLocation.getProvider());
|
||||
|
||||
|
||||
// Situation when last known location is equal to the new one
|
||||
if (timeDelta == 0 && isFromSameProvider)
|
||||
return true; // Because new location is at least not too old (< 2mins from now)
|
||||
|
||||
|
||||
// Check whether the new location fix is more or less accurate
|
||||
final int accuracyDelta = (int) (newLocation.getAccuracy()
|
||||
- currentBestLocation.getAccuracy());
|
||||
|
@ -218,10 +235,13 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
// *************** Notification Handlers ******************
|
||||
|
||||
private final static float HUNDRED_METRES = 100.0f;
|
||||
|
||||
|
||||
private File m_log;
|
||||
|
||||
//@Override
|
||||
public void onLocationChanged(Location l)
|
||||
{
|
||||
boolean passed = false;
|
||||
if (isBetterLocation(l, m_lastLocation))
|
||||
{
|
||||
if (m_reportFirstUpdate)
|
||||
|
@ -239,6 +259,22 @@ public class LocationService implements LocationListener, SensorEventListener, W
|
|||
}
|
||||
notifyLocationUpdated(l.getTime(), l.getLatitude(), l.getLongitude(), l.getAccuracy());
|
||||
m_lastLocation = l;
|
||||
passed = true;
|
||||
}
|
||||
try
|
||||
{
|
||||
BufferedWriter buf = new BufferedWriter(new FileWriter(m_log, true));
|
||||
final long currTime = System.currentTimeMillis();
|
||||
final double diffSeconds = (currTime - l.getTime())/1000.;
|
||||
Date d = new Date(currTime);
|
||||
buf.append((passed ? "+ " : "- ") + d.toLocaleString() + " " + diffSeconds + "sec " + l.getProvider() + " acc:"
|
||||
+ l.getAccuracy() + " lat:" + l.getLatitude() + " lon:" + l.getLongitude());
|
||||
buf.newLine();
|
||||
buf.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue