diff --git a/app/src/main/java/org/y20k/trackbook/MainActivity.java b/app/src/main/java/org/y20k/trackbook/MainActivity.java index 23b17a0..d84449a 100644 --- a/app/src/main/java/org/y20k/trackbook/MainActivity.java +++ b/app/src/main/java/org/y20k/trackbook/MainActivity.java @@ -387,8 +387,8 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys { setContentView(R.layout.activity_main_onboarding); // show the okay button and attach listener - Button okButton = (Button) findViewById(R.id.button_okay); - okButton.setOnClickListener(new View.OnClickListener() { + Button okayButton = (Button) findViewById(R.id.button_okay); + okayButton.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.M) @Override public void onClick(View view) { @@ -562,6 +562,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys { if (intent.hasExtra(EXTRA_TRACKING_STATE)) { mTrackerServiceRunning = intent.getBooleanExtra(EXTRA_TRACKING_STATE, false); if (mTrackerServiceRunning) { + // set FAB state mFloatingActionButtonState = FAB_STATE_RECORDING; } } diff --git a/app/src/main/java/org/y20k/trackbook/MainActivityMapFragment.java b/app/src/main/java/org/y20k/trackbook/MainActivityMapFragment.java index da03167..e6f1ee3 100644 --- a/app/src/main/java/org/y20k/trackbook/MainActivityMapFragment.java +++ b/app/src/main/java/org/y20k/trackbook/MainActivityMapFragment.java @@ -21,6 +21,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.database.ContentObserver; import android.location.Location; import android.location.LocationListener; @@ -29,6 +30,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.SystemClock; +import android.preference.PreferenceManager; import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; import android.support.v4.content.LocalBroadcastManager; @@ -105,9 +107,10 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys { // restore first start state and tracking state mFirstStart = true; mTrackerServiceRunning = false; + loadTrackerServiceState(mActivity); if (savedInstanceState != null) { 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 @@ -196,11 +199,13 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys { // load track from temp file if it exists LoadTempTrackAsyncHelper loadTempTrackAsyncHelper = new LoadTempTrackAsyncHelper(); loadTempTrackAsyncHelper.execute(); + LogHelper.v(LOG_TAG, "MapFragment: getting track from temp file."); } else if (savedInstanceState != null) { // load track from saved instance mTrack = savedInstanceState.getParcelable(INSTANCE_TRACK_MAIN_MAP); if (mTrack != null) { 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; // 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 if (mTrackerServiceRunning) { @@ -348,6 +356,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys { @Override 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_TRACKING_STATE, mTrackerServiceRunning); outState.putParcelable(INSTANCE_CURRENT_LOCATION, mCurrentBestLocation); @@ -367,6 +376,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys { Intent intent = mActivity.getIntent(); if (intent != null && intent.hasExtra(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 @@ -418,33 +428,61 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys { } - /* Handles new incoming intents */ - private void handleIncomingIntent() { - LogHelper.v(LOG_TAG, "Map Fragment received intent."); - Intent intent = mActivity.getIntent(); - String intentAction = intent.getAction(); - switch (intentAction) { - case ACTION_SHOW_MAP: - if (intent.hasExtra(EXTRA_TRACK)) { - mTrack = intent.getParcelableExtra(EXTRA_TRACK); - drawTrackOverlay(mTrack); - } +// public void updateTrackingState(boolean trackerServiceRunning, Track track) { +// mTrackerServiceRunning = trackerServiceRunning; +// mTrack = track; +// if (mTrackerServiceRunning && mTrack != null) { +// drawTrackOverlay(mTrack); +// // remove the blue "my location" dot +// mMapView.getOverlays().remove(mMyLocationOverlay); +// } +// } - if (intent.hasExtra(EXTRA_TRACKING_STATE)) { - mTrackerServiceRunning = intent.getBooleanExtra(EXTRA_TRACKING_STATE, false); - 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; - } - } +// /* Handles new incoming intents */ +// private void handleIncomingIntent() { +// LogHelper.v(LOG_TAG, "Map Fragment received intent."); +// Intent intent = mActivity.getIntent(); +// String intentAction = intent.getAction(); +// switch (intentAction) { +// case ACTION_SHOW_MAP: +//// 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 */ @@ -520,8 +558,12 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys { /* Draws track onto overlay */ private void drawTrackOverlay(Track track) { mMapView.getOverlays().remove(mTrackOverlay); - mTrackOverlay = MapHelper.createTrackOverlay(mActivity, track, mTrackerServiceRunning); - mMapView.getOverlays().add(mTrackOverlay); + mTrackOverlay = null; + if (track != null) { + LogHelper.v(LOG_TAG, "MapFragment: drawing new track overlay."); + mTrackOverlay = MapHelper.createTrackOverlay(mActivity, track, mTrackerServiceRunning); + mMapView.getOverlays().add(mTrackOverlay); + } } @@ -564,6 +606,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys { mController.setCenter(convertToGeoPoint(mCurrentBestLocation)); // clear intent 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); // } + /* 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 diff --git a/app/src/main/java/org/y20k/trackbook/TrackerService.java b/app/src/main/java/org/y20k/trackbook/TrackerService.java index cbd17a3..c2b52e4 100644 --- a/app/src/main/java/org/y20k/trackbook/TrackerService.java +++ b/app/src/main/java/org/y20k/trackbook/TrackerService.java @@ -71,6 +71,7 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven private boolean mTrackerServiceRunning; private boolean mLocationSystemSetting; + @Override 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) { 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() { LogHelper.v(LOG_TAG, "Service received command: STOP"); diff --git a/app/src/main/java/org/y20k/trackbook/helpers/StorageHelper.java b/app/src/main/java/org/y20k/trackbook/helpers/StorageHelper.java index 9891ea0..72e9f54 100644 --- a/app/src/main/java/org/y20k/trackbook/helpers/StorageHelper.java +++ b/app/src/main/java/org/y20k/trackbook/helpers/StorageHelper.java @@ -103,17 +103,19 @@ public class StorageHelper implements TrackbookKeys { } if (mFolder != null && mFolder.exists() && mFolder.isDirectory() && mFolder.canWrite() && recordingStart != null && track != null) { - // construct filename from track recording date + // create file object String fileName; if (mFileType == FILETYPE_TEMP) { + // case: temp file fileName = FILENAME_TEMP + mFileExtension; } else { + // case: regular file DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US); fileName = dateFormat.format(recordingStart) + mFileExtension; } File file = new File(mFolder.toString() + "/" + fileName); - // convert to JSON + // convert track to JSON Gson gson = new Gson(); 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 (mFileType != FILETYPE_TEMP) { - deleteOldTracks(); + // include temp file if it exists + deleteOldTracks(true); } return true; @@ -213,7 +216,7 @@ public class StorageHelper implements TrackbookKeys { /* Gets the last track from directory */ - private void deleteOldTracks() { + private void deleteOldTracks(boolean includeTempFile) { // get "tracks" folder mFolder = mActivity.getExternalFilesDir(mDirectoryName); @@ -236,6 +239,12 @@ public class StorageHelper implements TrackbookKeys { index++; } } + + // delete temp file if it exists + if (includeTempFile && mTempFile.exists()) { + mTempFile.delete(); + } + } @@ -247,9 +256,9 @@ public class StorageHelper implements TrackbookKeys { @Override public int compare(File file1, File file2) { - // discard files not ending with ".trackbook" - boolean file1IsTrack = file1.getName().endsWith(mFileExtension); - boolean file2IsTrack = file2.getName().endsWith(mFileExtension); + // discard temp file and files not ending with ".trackbook" + boolean file1IsTrack = file1.getName().endsWith(mFileExtension) && !file1.equals(mTempFile); + boolean file2IsTrack = file2.getName().endsWith(mFileExtension) && !file2.equals(mTempFile); // note: "greater" means higher index in array if (!file1IsTrack && file2IsTrack) {