diff --git a/MyTracks/src/com/google/android/apps/mytracks/DeleteAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/DeleteAsyncTask.java index 3a8349aa4..99ba963c7 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/DeleteAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/DeleteAsyncTask.java @@ -76,6 +76,16 @@ public class DeleteAsyncTask extends AsyncTask { MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context); if (trackIds.length == 1 && trackIds[0] == -1L) { + try { + while (SyncUtils.isSyncActive(context)) { + if (isCancelled()) { + return false; + } + Thread.sleep(1000); + } + } catch (InterruptedException e) { + return false; + } PreferencesUtils.setBoolean( context, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT); SyncUtils.disableSync(context); @@ -84,9 +94,10 @@ public class DeleteAsyncTask extends AsyncTask { return true; } else { for (long id : trackIds) { - if (!isCancelled()) { - myTracksProviderUtils.deleteTrack(id); + if (isCancelled()) { + return false; } + myTracksProviderUtils.deleteTrack(id); } return true; } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncAdapter.java b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncAdapter.java index a22a19425..eace3dd56 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncAdapter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncAdapter.java @@ -353,8 +353,12 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter { long[] trackIds = kmlImporter.importFile(inputStream); if (trackIds.length == 1) { Track track = myTracksProviderUtils.getTrack(trackIds[0]); - SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, driveFile); - Log.d(TAG, "Add from Google Drive " + track.getName()); + if (track == null) { + Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId()); + } else { + SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, driveFile); + Log.d(TAG, "Add from Google Drive " + track.getName()); + } } else { // Clean up if imported more than one track for (int i = 0; i < trackIds.length; i++) { @@ -495,8 +499,12 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter { long[] trackIds = kmlImporter.importFile(inputStream); if (trackIds.length == 1) { Track newTrack = myTracksProviderUtils.getTrack(trackIds[0]); - SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, newTrack, driveFile); - return true; + if (newTrack == null) { + Log.e(TAG, "Unable to merge, imported track is null"); + } else { + SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, newTrack, driveFile); + return true; + } } else { Log.e(TAG, "Unable to merge, imported size is not 1"); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java index 4790e868b..54471774c 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java @@ -127,6 +127,21 @@ public class SyncUtils { } } + /** + * Returns true if sync is active. + * + * @param context the context + */ + public static boolean isSyncActive(Context context) { + Account[] accounts = AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE); + for (Account account : accounts) { + if (ContentResolver.isSyncActive(account, SYNC_AUTHORITY)) { + return true; + } + } + return false; + } + /** * Enables sync. * diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java b/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java index e4de3e1cf..b6fae3b33 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java @@ -93,6 +93,11 @@ public class TrackRecordingService extends Service { public static final double PAUSE_LATITUDE = 100.0; public static final double RESUME_LATITUDE = 200.0; + /** + * Anything faster than that (in meters per second) will be considered moving. + */ + public static final double MAX_NO_MOVEMENT_SPEED = 0.224; + private static final String TAG = TrackRecordingService.class.getSimpleName(); private static final long ONE_SECOND = 1000; // in milliseconds private static final long ONE_MINUTE = 60 * ONE_SECOND; // in milliseconds @@ -127,6 +132,7 @@ public class TrackRecordingService extends Service { private SensorManager sensorManager; private Location lastLocation; private boolean currentSegmentHasLocation; + private boolean isIdle; // true if idle private ServiceBinder binder = new ServiceBinder(this); @@ -704,6 +710,7 @@ public class TrackRecordingService extends Service { sensorManager = SensorManagerFactory.getSystemSensorManager(this); lastLocation = null; currentSegmentHasLocation = false; + isIdle = false; startGps(); sendTrackBroadcast(trackStarted ? R.string.track_started_broadcast_action @@ -932,10 +939,9 @@ public class TrackRecordingService extends Service { } double distanceToLastTrackLocation = location.distanceTo(lastValidTrackPoint); - if (distanceToLastTrackLocation < recordingDistanceInterval && sensorDataSet == null) { - Log.d(TAG, "Not recording location due to recording distance interval."); - } else if (distanceToLastTrackLocation > maxRecordingDistance) { + if (distanceToLastTrackLocation > maxRecordingDistance) { insertLocation(track, lastLocation, lastValidTrackPoint); + Location pause = new Location(LocationManager.GPS_PROVIDER); pause.setLongitude(0); pause.setLatitude(PAUSE_LATITUDE); @@ -943,13 +949,22 @@ public class TrackRecordingService extends Service { insertLocation(track, pause, null); insertLocation(track, location, null); - } else { - /* - * (distanceToLastTrackLocation >= minRecordingDistance || - * hasSensorData) && distanceToLastTrackLocation <= maxRecordingDistance - */ + isIdle = false; + } else if (sensorDataSet != null + || distanceToLastTrackLocation >= recordingDistanceInterval) { insertLocation(track, lastLocation, lastValidTrackPoint); insertLocation(track, location, null); + isIdle = false; + } else if (!isIdle && location.hasSpeed() && location.getSpeed() < MAX_NO_MOVEMENT_SPEED) { + insertLocation(track, lastLocation, lastValidTrackPoint); + insertLocation(track, location, null); + isIdle = true; + } else if (isIdle && location.hasSpeed() && location.getSpeed() >= MAX_NO_MOVEMENT_SPEED) { + insertLocation(track, lastLocation, lastValidTrackPoint); + insertLocation(track, location, null); + isIdle = false; + } else { + Log.d(TAG, "Not recording location, idle"); } lastLocation = location; } catch (Error e) { diff --git a/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java b/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java index e34fec1a8..63a859276 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java +++ b/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java @@ -17,8 +17,9 @@ package com.google.android.apps.mytracks.stats; import static com.google.android.apps.mytracks.Constants.TAG; +import static com.google.android.apps.mytracks.services.TrackRecordingService.MAX_NO_MOVEMENT_SPEED; +import static com.google.android.apps.mytracks.services.TrackRecordingService.PAUSE_LATITUDE; -import com.google.android.apps.mytracks.services.TrackRecordingService; import com.google.android.apps.mytracks.util.LocationUtils; import com.google.common.annotations.VisibleForTesting; @@ -66,11 +67,6 @@ public class TripStatisticsUpdater { @VisibleForTesting static final int SPEED_SMOOTHING_FACTOR = 25; - /** - * Anything faster than that (in meters per second) will be considered moving. - */ - private static final double MAX_NO_MOVEMENT_SPEED = 0.224; - /** * Ignore any acceleration faster than this. Will ignore any speeds that imply * acceleration greater than 2g's 2g = 19.6 m/s^2 = 0.0002 m/ms^2 = 0.02 @@ -142,7 +138,7 @@ public class TripStatisticsUpdater { if (!LocationUtils.isValidLocation(location)) { // Either pause or resume marker - if (location.getLatitude() == TrackRecordingService.PAUSE_LATITUDE) { + if (location.getLatitude() == PAUSE_LATITUDE) { if (lastLocation != null && lastMovingLocation != null && lastLocation != lastMovingLocation) { currentSegment.addTotalDistance(lastMovingLocation.distanceTo(lastLocation)); @@ -173,11 +169,11 @@ public class TripStatisticsUpdater { } double movingDistance = lastMovingLocation.distanceTo(location); - if (movingDistance < minRecordingDistance) { - if (!location.hasSpeed() || location.getSpeed() < MAX_NO_MOVEMENT_SPEED) { - lastLocation = location; - return; - } + if (movingDistance < minRecordingDistance + && (!location.hasSpeed() || location.getSpeed() < MAX_NO_MOVEMENT_SPEED)) { + speedBuffer.reset(); + lastLocation = location; + return; } long movingTime = location.getTime() - lastLocation.getTime(); if (movingTime < 0) { @@ -203,7 +199,7 @@ public class TripStatisticsUpdater { updateSpeed( location.getTime(), location.getSpeed(), lastLocation.getTime(), lastLocation.getSpeed()); } - + lastLocation = location; lastMovingLocation = location; } @@ -231,13 +227,15 @@ public class TripStatisticsUpdater { */ @VisibleForTesting void updateSpeed(long time, double speed, long lastLocationTime, double lastLocationSpeed) { - if (!isValidSpeed(time, speed, lastLocationTime, lastLocationSpeed)) { + if (speed < MAX_NO_MOVEMENT_SPEED) { + speedBuffer.reset(); + } else if (isValidSpeed(time, speed, lastLocationTime, lastLocationSpeed)) { + speedBuffer.setNext(speed); + if (speed > currentSegment.getMaxSpeed()) { + currentSegment.setMaxSpeed(speed); + } + } else { Log.d(TAG, "Invalid speed. speed: " + speed + " lastLocationSpeed: " + lastLocationSpeed); - return; - } - speedBuffer.setNext(speed); - if (speed > currentSegment.getMaxSpeed()) { - currentSegment.setMaxSpeed(speed); } }