forked from organicmaps/organicmaps
[android] fix: Proper background collection of GPS track.
This commit is contained in:
parent
e17a9e0af5
commit
beb4376fb6
7 changed files with 65 additions and 33 deletions
|
@ -1,4 +1,5 @@
|
|||
#include "Framework.hpp"
|
||||
#include "map/gps_track.hpp"
|
||||
#include "platform/file_logging.hpp"
|
||||
|
||||
extern "C"
|
||||
|
@ -37,7 +38,10 @@ extern "C"
|
|||
info.m_speed = speed;
|
||||
|
||||
LOG_MEMORY_INFO();
|
||||
g_framework->OnLocationUpdated(info);
|
||||
if (g_framework)
|
||||
g_framework->OnLocationUpdated(info);
|
||||
else
|
||||
GetDefaultGpsTrack().AddPoint(info);
|
||||
}
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
|
|
|
@ -7,7 +7,8 @@ namespace
|
|||
|
||||
::Framework * frm()
|
||||
{
|
||||
return g_framework->NativeFramework();
|
||||
// TODO (trashkalmar): Temp solution until the GPS tracker is uncoupled from the framework.
|
||||
return (g_framework ? g_framework->NativeFramework() : nullptr);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
@ -17,13 +18,30 @@ extern "C"
|
|||
JNIEXPORT void JNICALL
|
||||
Java_com_mapswithme_maps_location_TrackRecorder_nativeSetEnabled(JNIEnv * env, jclass clazz, jboolean enable)
|
||||
{
|
||||
frm()->EnableGpsTracking(enable);
|
||||
// TODO (trashkalmar): Temp solution until the GPS tracker is uncoupled from the framework.
|
||||
|
||||
::Framework * const framework = frm();
|
||||
if (framework)
|
||||
{
|
||||
framework->EnableGpsTracking(enable);
|
||||
return;
|
||||
}
|
||||
|
||||
Settings::Set("GpsTrackingEnabled", static_cast<bool>(enable));
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_mapswithme_maps_location_TrackRecorder_nativeIsEnabled(JNIEnv * env, jclass clazz)
|
||||
{
|
||||
return frm()->IsGpsTrackingEnabled();
|
||||
// TODO (trashkalmar): Temp solution until the GPS tracker is uncoupled from the framework.
|
||||
|
||||
::Framework * const framework = frm();
|
||||
if (framework)
|
||||
return framework->IsGpsTrackingEnabled();
|
||||
|
||||
bool res = false;
|
||||
Settings::Get("GpsTrackingEnabled", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
|
@ -35,6 +53,14 @@ extern "C"
|
|||
JNIEXPORT jint JNICALL
|
||||
Java_com_mapswithme_maps_location_TrackRecorder_nativeGetDuration(JNIEnv * env, jclass clazz)
|
||||
{
|
||||
return frm()->GetGpsTrackingDuration().count();
|
||||
// TODO (trashkalmar): Temp solution until the GPS tracker is uncoupled from the framework.
|
||||
|
||||
::Framework * const framework = frm();
|
||||
if (framework)
|
||||
return framework->GetGpsTrackingDuration().count();
|
||||
|
||||
uint32_t res = 24;
|
||||
Settings::Get("GpsTrackingDuration", res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ public class AndroidNativeProvider extends BaseLocationProvider
|
|||
{
|
||||
mIsActive = true;
|
||||
for (final String provider : providers)
|
||||
mLocationManager.requestLocationUpdates(provider, LocationHelper.getUpdateInterval(), 0, this);
|
||||
mLocationManager.requestLocationUpdates(provider, UPDATE_INTERVAL_MS, 0, this);
|
||||
|
||||
LocationHelper.INSTANCE.registerSensorListeners();
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ abstract class BaseLocationProvider implements LocationListener
|
|||
private static final double DEFAULT_SPEED_MPS = 5;
|
||||
|
||||
protected static final Logger sLogger = SimpleLogger.get(BaseLocationProvider.class.getName());
|
||||
protected static final long UPDATE_INTERVAL_MS = 500;
|
||||
|
||||
protected abstract void startUpdates();
|
||||
|
||||
|
|
|
@ -36,8 +36,8 @@ public class GoogleFusedLocationProvider extends BaseLocationProvider
|
|||
{
|
||||
mLocationRequest = LocationRequest.create();
|
||||
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
|
||||
mLocationRequest.setInterval(LocationHelper.getUpdateInterval());
|
||||
mLocationRequest.setFastestInterval(LocationHelper.getUpdateInterval() / 2);
|
||||
mLocationRequest.setInterval(UPDATE_INTERVAL_MS);
|
||||
mLocationRequest.setFastestInterval(UPDATE_INTERVAL_MS / 2);
|
||||
|
||||
mGoogleApiClient.connect();
|
||||
}
|
||||
|
|
|
@ -41,8 +41,6 @@ public enum LocationHelper implements SensorEventListener
|
|||
|
||||
public static final String LOCATION_PREDICTOR_PROVIDER = "LocationPredictorProvider";
|
||||
private static final float DISTANCE_TO_RECREATE_MAGNETIC_FIELD_M = 1000;
|
||||
private static final long UPDATE_INTERVAL_FOREGROUND_MS = 500;
|
||||
private static final long UPDATE_INTERVAL_BACKGROUND_MS = 20000;
|
||||
private static final long STOP_DELAY_MS = 5000;
|
||||
|
||||
public interface LocationListener
|
||||
|
@ -314,12 +312,6 @@ public enum LocationHelper implements SensorEventListener
|
|||
mLocationProvider.startUpdates();
|
||||
}
|
||||
|
||||
public static long getUpdateInterval()
|
||||
{
|
||||
return (MwmApplication.backgroundTracker().isForeground() ? UPDATE_INTERVAL_FOREGROUND_MS
|
||||
: UPDATE_INTERVAL_BACKGROUND_MS);
|
||||
}
|
||||
|
||||
public static void onLocationUpdated(@NonNull Location location)
|
||||
{
|
||||
nativeLocationUpdated(location.getTime(),
|
||||
|
|
|
@ -13,15 +13,15 @@ public final class TrackRecorder
|
|||
{
|
||||
private static final AlarmManager sAlarmManager = (AlarmManager)MwmApplication.get().getSystemService(Context.ALARM_SERVICE);
|
||||
private static final Intent sAlarmIntent = new Intent("com.mapswithme.maps.TRACK_RECORDER_ALARM");
|
||||
private static final long WAKEUP_INTERVAL_MS = 20000;
|
||||
|
||||
private static final LocationHelper.LocationListener mLocationListener = new LocationHelper.LocationListener()
|
||||
private static final LocationHelper.LocationListener sLocationListener = new LocationHelper.LocationListener()
|
||||
{
|
||||
@Override
|
||||
public void onLocationUpdated(Location location)
|
||||
{
|
||||
LocationHelper.onLocationUpdated(location);
|
||||
TrackRecorderWakeService.stop();
|
||||
checkState();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -43,20 +43,12 @@ public final class TrackRecorder
|
|||
@Override
|
||||
public void onTransit(boolean foreground)
|
||||
{
|
||||
checkState();
|
||||
if (foreground)
|
||||
TrackRecorderWakeService.stop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void checkState()
|
||||
{
|
||||
if (MwmApplication.backgroundTracker().isForeground() || !isEnabled())
|
||||
{
|
||||
sAlarmManager.cancel(getAlarmIntent());
|
||||
TrackRecorderWakeService.stop();
|
||||
}
|
||||
else
|
||||
sAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + LocationHelper.getUpdateInterval(), getAlarmIntent());
|
||||
setEnabledInternal(nativeIsEnabled());
|
||||
}
|
||||
|
||||
private static PendingIntent getAlarmIntent()
|
||||
|
@ -64,6 +56,17 @@ public final class TrackRecorder
|
|||
return PendingIntent.getBroadcast(MwmApplication.get(), 0, sAlarmIntent, 0);
|
||||
}
|
||||
|
||||
private static void setEnabledInternal(boolean enabled)
|
||||
{
|
||||
if (enabled)
|
||||
sAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + WAKEUP_INTERVAL_MS, WAKEUP_INTERVAL_MS, getAlarmIntent());
|
||||
else
|
||||
{
|
||||
sAlarmManager.cancel(getAlarmIntent());
|
||||
TrackRecorderWakeService.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isEnabled()
|
||||
{
|
||||
return nativeIsEnabled();
|
||||
|
@ -72,7 +75,7 @@ public final class TrackRecorder
|
|||
public static void setEnabled(boolean enabled)
|
||||
{
|
||||
nativeSetEnabled(enabled);
|
||||
checkState();
|
||||
setEnabledInternal(enabled);
|
||||
}
|
||||
|
||||
public static int getDuration()
|
||||
|
@ -87,18 +90,24 @@ public final class TrackRecorder
|
|||
|
||||
static void onWakeAlarm()
|
||||
{
|
||||
if (nativeIsEnabled())
|
||||
if (!nativeIsEnabled())
|
||||
{
|
||||
setEnabledInternal(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!MwmApplication.backgroundTracker().isForeground())
|
||||
TrackRecorderWakeService.start();
|
||||
}
|
||||
|
||||
static void onServiceStarted()
|
||||
{
|
||||
LocationHelper.INSTANCE.addLocationListener(mLocationListener, false);
|
||||
LocationHelper.INSTANCE.addLocationListener(sLocationListener, false);
|
||||
}
|
||||
|
||||
static void onServiceStopped()
|
||||
{
|
||||
LocationHelper.INSTANCE.removeLocationListener(mLocationListener);
|
||||
LocationHelper.INSTANCE.removeLocationListener(sLocationListener);
|
||||
}
|
||||
|
||||
private static native void nativeSetEnabled(boolean enable);
|
||||
|
|
Loading…
Add table
Reference in a new issue