prevents a bug where the previous track became visible again on the main map even though it was "cleared"

This commit is contained in:
y20k 2017-01-11 21:52:45 +01:00
parent 892d41a5b3
commit 6ddcf3ae67
4 changed files with 103 additions and 38 deletions

View file

@ -387,8 +387,8 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
setContentView(R.layout.activity_main_onboarding); setContentView(R.layout.activity_main_onboarding);
// show the okay button and attach listener // show the okay button and attach listener
Button okButton = (Button) findViewById(R.id.button_okay); Button okayButton = (Button) findViewById(R.id.button_okay);
okButton.setOnClickListener(new View.OnClickListener() { okayButton.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.M) @TargetApi(Build.VERSION_CODES.M)
@Override @Override
public void onClick(View view) { public void onClick(View view) {
@ -562,6 +562,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
if (intent.hasExtra(EXTRA_TRACKING_STATE)) { if (intent.hasExtra(EXTRA_TRACKING_STATE)) {
mTrackerServiceRunning = intent.getBooleanExtra(EXTRA_TRACKING_STATE, false); mTrackerServiceRunning = intent.getBooleanExtra(EXTRA_TRACKING_STATE, false);
if (mTrackerServiceRunning) { if (mTrackerServiceRunning) {
// set FAB state
mFloatingActionButtonState = FAB_STATE_RECORDING; mFloatingActionButtonState = FAB_STATE_RECORDING;
} }
} }

View file

@ -21,6 +21,7 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
@ -29,6 +30,7 @@ import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
@ -105,9 +107,10 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
// restore first start state and tracking state // restore first start state and tracking state
mFirstStart = true; mFirstStart = true;
mTrackerServiceRunning = false; mTrackerServiceRunning = false;
loadTrackerServiceState(mActivity);
if (savedInstanceState != null) { if (savedInstanceState != null) {
mFirstStart = savedInstanceState.getBoolean(INSTANCE_FIRST_START, true); mFirstStart = savedInstanceState.getBoolean(INSTANCE_FIRST_START, true);
mTrackerServiceRunning = savedInstanceState.getBoolean(INSTANCE_TRACKING_STATE, false); // mTrackerServiceRunning = savedInstanceState.getBoolean(INSTANCE_TRACKING_STATE, false);
} }
// acquire reference to Location Manager // acquire reference to Location Manager
@ -196,11 +199,13 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
// load track from temp file if it exists // load track from temp file if it exists
LoadTempTrackAsyncHelper loadTempTrackAsyncHelper = new LoadTempTrackAsyncHelper(); LoadTempTrackAsyncHelper loadTempTrackAsyncHelper = new LoadTempTrackAsyncHelper();
loadTempTrackAsyncHelper.execute(); loadTempTrackAsyncHelper.execute();
LogHelper.v(LOG_TAG, "MapFragment: getting track from temp file.");
} else if (savedInstanceState != null) { } else if (savedInstanceState != null) {
// load track from saved instance // load track from saved instance
mTrack = savedInstanceState.getParcelable(INSTANCE_TRACK_MAIN_MAP); mTrack = savedInstanceState.getParcelable(INSTANCE_TRACK_MAIN_MAP);
if (mTrack != null) { if (mTrack != null) {
drawTrackOverlay(mTrack); drawTrackOverlay(mTrack);
LogHelper.v(LOG_TAG, "MapFragment: got track from saved instance.");
} }
} }
@ -222,7 +227,10 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
mFragmentVisible = true; mFragmentVisible = true;
// handle incoming intent (from notification) // handle incoming intent (from notification)
handleIncomingIntent(); // handleIncomingIntent(); // todo remove
// load state of tracker service - see if anything changed
loadTrackerServiceState(mActivity);
// center map on current position - if TrackerService is running // center map on current position - if TrackerService is running
if (mTrackerServiceRunning) { if (mTrackerServiceRunning) {
@ -348,6 +356,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
LogHelper.v(LOG_TAG, "MapFragment: onSaveInstanceState called. mTrack object is Null: " + (mTrack == null));
outState.putBoolean(INSTANCE_FIRST_START, mFirstStart); outState.putBoolean(INSTANCE_FIRST_START, mFirstStart);
outState.putBoolean(INSTANCE_TRACKING_STATE, mTrackerServiceRunning); outState.putBoolean(INSTANCE_TRACKING_STATE, mTrackerServiceRunning);
outState.putParcelable(INSTANCE_CURRENT_LOCATION, mCurrentBestLocation); outState.putParcelable(INSTANCE_CURRENT_LOCATION, mCurrentBestLocation);
@ -367,6 +376,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
Intent intent = mActivity.getIntent(); Intent intent = mActivity.getIntent();
if (intent != null && intent.hasExtra(EXTRA_TRACK)) { if (intent != null && intent.hasExtra(EXTRA_TRACK)) {
mTrack = intent.getParcelableExtra(EXTRA_TRACK); mTrack = intent.getParcelableExtra(EXTRA_TRACK);
LogHelper.v(LOG_TAG, "MapFragment: getting track from intent.");
} }
// turn on/off tracking for MainActivity Fragment - prevent double tracking // turn on/off tracking for MainActivity Fragment - prevent double tracking
@ -418,33 +428,61 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
} }
/* Handles new incoming intents */ // public void updateTrackingState(boolean trackerServiceRunning, Track track) {
private void handleIncomingIntent() { // mTrackerServiceRunning = trackerServiceRunning;
LogHelper.v(LOG_TAG, "Map Fragment received intent."); // mTrack = track;
Intent intent = mActivity.getIntent(); // if (mTrackerServiceRunning && mTrack != null) {
String intentAction = intent.getAction(); // drawTrackOverlay(mTrack);
switch (intentAction) { // // remove the blue "my location" dot
case ACTION_SHOW_MAP: // mMapView.getOverlays().remove(mMyLocationOverlay);
if (intent.hasExtra(EXTRA_TRACK)) { // }
mTrack = intent.getParcelableExtra(EXTRA_TRACK); // }
drawTrackOverlay(mTrack);
}
if (intent.hasExtra(EXTRA_TRACKING_STATE)) {
mTrackerServiceRunning = intent.getBooleanExtra(EXTRA_TRACKING_STATE, false);
mMapView.getOverlays().remove(mMyLocationOverlay);
}
// prevent multiple reactions to intent // /* Handles new incoming intents */
intent.setAction(ACTION_DEFAULT); // private void handleIncomingIntent() {
// LogHelper.v(LOG_TAG, "Map Fragment received intent.");
break; // Intent intent = mActivity.getIntent();
// String intentAction = intent.getAction();
default: // switch (intentAction) {
LogHelper.v(LOG_TAG, "Doing nothing. Type of ACTION: " + intentAction); // case ACTION_SHOW_MAP:
break; //// if (intent.hasExtra(EXTRA_TRACK)) {
} //// LogHelper.v(LOG_TAG, "MapFragment: Intent received drawing map.");
} //// mTrack = intent.getParcelableExtra(EXTRA_TRACK);
//// drawTrackOverlay(mTrack);
//// }
////
//// if (intent.hasExtra(EXTRA_TRACKING_STATE)) {
//// mTrackerServiceRunning = intent.getBooleanExtra(EXTRA_TRACKING_STATE, false);
//// mMapView.getOverlays().remove(mMyLocationOverlay);
//// }
//
//
// if (intent.hasExtra(EXTRA_TRACKING_STATE)) {
// // store tracker service state
// mTrackerServiceRunning = intent.getBooleanExtra(EXTRA_TRACKING_STATE, false);
//
// // get most current track (from notification) - if recording is active
// if (mTrackerServiceRunning && intent.hasExtra(EXTRA_TRACK)) {
// LogHelper.v(LOG_TAG, "MapFragment: Intent received drawing map.");
// mTrack = intent.getParcelableExtra(EXTRA_TRACK);
// drawTrackOverlay(mTrack);
// // remove the blue "my location" dot
// mMapView.getOverlays().remove(mMyLocationOverlay);
// }
// }
//
//
// // prevent multiple reactions to intent
// intent.setAction(ACTION_DEFAULT);
//
// break;
//
// default:
// LogHelper.v(LOG_TAG, "Doing nothing. Type of ACTION: " + intentAction);
// break;
// }
// }
/* Start preliminary tracking for map */ /* Start preliminary tracking for map */
@ -520,9 +558,13 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
/* Draws track onto overlay */ /* Draws track onto overlay */
private void drawTrackOverlay(Track track) { private void drawTrackOverlay(Track track) {
mMapView.getOverlays().remove(mTrackOverlay); mMapView.getOverlays().remove(mTrackOverlay);
mTrackOverlay = null;
if (track != null) {
LogHelper.v(LOG_TAG, "MapFragment: drawing new track overlay.");
mTrackOverlay = MapHelper.createTrackOverlay(mActivity, track, mTrackerServiceRunning); mTrackOverlay = MapHelper.createTrackOverlay(mActivity, track, mTrackerServiceRunning);
mMapView.getOverlays().add(mTrackOverlay); mMapView.getOverlays().add(mTrackOverlay);
} }
}
/* Toggles snackbar indicating that location setting is off */ /* Toggles snackbar indicating that location setting is off */
@ -564,6 +606,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
mController.setCenter(convertToGeoPoint(mCurrentBestLocation)); mController.setCenter(convertToGeoPoint(mCurrentBestLocation));
// clear intent // clear intent
intent.setAction(ACTION_DEFAULT); intent.setAction(ACTION_DEFAULT);
LogHelper.v(LOG_TAG, "MapFragment: Track update received - drawing map.");
} }
} }
}; };
@ -605,6 +648,15 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
// int zoom = settings.getInt(PREFS_ZOOM_LEVEL, 16); // int zoom = settings.getInt(PREFS_ZOOM_LEVEL, 16);
// } // }
/* Loads state tracker service from preferences */
private void loadTrackerServiceState(Context context) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
int fabState = settings.getInt(PREFS_FAB_STATE, FAB_STATE_DEFAULT);
if (fabState == FAB_STATE_RECORDING) {
mTrackerServiceRunning = true;
}
}
/** /**
* Inner class: SettingsContentObserver is a custom ContentObserver for changes in Android Settings * Inner class: SettingsContentObserver is a custom ContentObserver for changes in Android Settings

View file

@ -71,6 +71,7 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
private boolean mTrackerServiceRunning; private boolean mTrackerServiceRunning;
private boolean mLocationSystemSetting; private boolean mLocationSystemSetting;
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
@ -164,6 +165,7 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
} }
/* Start tracking location */
private void startTracking(Intent intent) { private void startTracking(Intent intent) {
LogHelper.v(LOG_TAG, "Service received command: START"); LogHelper.v(LOG_TAG, "Service received command: START");
@ -224,6 +226,7 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
} }
/* Stop tracking location */
private void stopTracking() { private void stopTracking() {
LogHelper.v(LOG_TAG, "Service received command: STOP"); LogHelper.v(LOG_TAG, "Service received command: STOP");

View file

@ -103,17 +103,19 @@ public class StorageHelper implements TrackbookKeys {
} }
if (mFolder != null && mFolder.exists() && mFolder.isDirectory() && mFolder.canWrite() && recordingStart != null && track != null) { if (mFolder != null && mFolder.exists() && mFolder.isDirectory() && mFolder.canWrite() && recordingStart != null && track != null) {
// construct filename from track recording date // create file object
String fileName; String fileName;
if (mFileType == FILETYPE_TEMP) { if (mFileType == FILETYPE_TEMP) {
// case: temp file
fileName = FILENAME_TEMP + mFileExtension; fileName = FILENAME_TEMP + mFileExtension;
} else { } else {
// case: regular file
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
fileName = dateFormat.format(recordingStart) + mFileExtension; fileName = dateFormat.format(recordingStart) + mFileExtension;
} }
File file = new File(mFolder.toString() + "/" + fileName); File file = new File(mFolder.toString() + "/" + fileName);
// convert to JSON // convert track to JSON
Gson gson = new Gson(); Gson gson = new Gson();
String json = gson.toJson(track); String json = gson.toJson(track);
@ -128,7 +130,8 @@ public class StorageHelper implements TrackbookKeys {
// if write was successful delete old track files - only if not a temp file // if write was successful delete old track files - only if not a temp file
if (mFileType != FILETYPE_TEMP) { if (mFileType != FILETYPE_TEMP) {
deleteOldTracks(); // include temp file if it exists
deleteOldTracks(true);
} }
return true; return true;
@ -213,7 +216,7 @@ public class StorageHelper implements TrackbookKeys {
/* Gets the last track from directory */ /* Gets the last track from directory */
private void deleteOldTracks() { private void deleteOldTracks(boolean includeTempFile) {
// get "tracks" folder // get "tracks" folder
mFolder = mActivity.getExternalFilesDir(mDirectoryName); mFolder = mActivity.getExternalFilesDir(mDirectoryName);
@ -236,6 +239,12 @@ public class StorageHelper implements TrackbookKeys {
index++; index++;
} }
} }
// delete temp file if it exists
if (includeTempFile && mTempFile.exists()) {
mTempFile.delete();
}
} }
@ -247,9 +256,9 @@ public class StorageHelper implements TrackbookKeys {
@Override @Override
public int compare(File file1, File file2) { public int compare(File file1, File file2) {
// discard files not ending with ".trackbook" // discard temp file and files not ending with ".trackbook"
boolean file1IsTrack = file1.getName().endsWith(mFileExtension); boolean file1IsTrack = file1.getName().endsWith(mFileExtension) && !file1.equals(mTempFile);
boolean file2IsTrack = file2.getName().endsWith(mFileExtension); boolean file2IsTrack = file2.getName().endsWith(mFileExtension) && !file2.equals(mTempFile);
// note: "greater" means higher index in array // note: "greater" means higher index in array
if (!file1IsTrack && file2IsTrack) { if (!file1IsTrack && file2IsTrack) {