diff --git a/android/jni/com/mapswithme/maps/LocationHelper.cpp b/android/jni/com/mapswithme/maps/LocationHelper.cpp index b42594ee70..973eff5a71 100644 --- a/android/jni/com/mapswithme/maps/LocationHelper.cpp +++ b/android/jni/com/mapswithme/maps/LocationHelper.cpp @@ -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 diff --git a/android/jni/com/mapswithme/maps/TrackRecorder.cpp b/android/jni/com/mapswithme/maps/TrackRecorder.cpp index ccd720bf07..500857bee3 100644 --- a/android/jni/com/mapswithme/maps/TrackRecorder.cpp +++ b/android/jni/com/mapswithme/maps/TrackRecorder.cpp @@ -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(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; } } diff --git a/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java b/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java index 29414eeb6c..0466e30fd8 100644 --- a/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java +++ b/android/src/com/mapswithme/maps/location/AndroidNativeProvider.java @@ -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(); diff --git a/android/src/com/mapswithme/maps/location/BaseLocationProvider.java b/android/src/com/mapswithme/maps/location/BaseLocationProvider.java index 487275e8a7..6cd6a6b4d1 100644 --- a/android/src/com/mapswithme/maps/location/BaseLocationProvider.java +++ b/android/src/com/mapswithme/maps/location/BaseLocationProvider.java @@ -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(); diff --git a/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java b/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java index 921017b809..0772736aa0 100644 --- a/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java +++ b/android/src/com/mapswithme/maps/location/GoogleFusedLocationProvider.java @@ -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(); } diff --git a/android/src/com/mapswithme/maps/location/LocationHelper.java b/android/src/com/mapswithme/maps/location/LocationHelper.java index 7b07e26134..6cb5d1927f 100644 --- a/android/src/com/mapswithme/maps/location/LocationHelper.java +++ b/android/src/com/mapswithme/maps/location/LocationHelper.java @@ -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(), diff --git a/android/src/com/mapswithme/maps/location/TrackRecorder.java b/android/src/com/mapswithme/maps/location/TrackRecorder.java index 2dbcaf7378..4d62a724b8 100644 --- a/android/src/com/mapswithme/maps/location/TrackRecorder.java +++ b/android/src/com/mapswithme/maps/location/TrackRecorder.java @@ -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);