service can resume a recording (see #33)

This commit is contained in:
y20k 2018-03-21 15:18:40 +01:00
parent 764673edc8
commit 39a4cbbe41
6 changed files with 44 additions and 31 deletions

View file

@ -90,7 +90,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
private CardView mFloatingActionButtonSubSaveLabel;
private CardView mFloatingActionButtonSubClearLabel;
private CardView mFloatingActionButtonSubResumeLabel;
private BroadcastReceiver mTrackingStoppedReceiver;
private BroadcastReceiver mTrackingChangedReceiver;
private int mFloatingActionButtonState;
private int mSelectedTab;
@ -140,10 +140,10 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
protected void onStart() {
super.onStart();
// register broadcast receiver for stopped tracking
mTrackingStoppedReceiver = createTrackingStoppedReceiver();
IntentFilter trackingStoppedIntentFilter = new IntentFilter(ACTION_TRACKING_STOPPED);
LocalBroadcastManager.getInstance(this).registerReceiver(mTrackingStoppedReceiver, trackingStoppedIntentFilter);
// register broadcast receiver for changed tracking state
mTrackingChangedReceiver = createTrackingChangedReceiver();
IntentFilter trackingStoppedIntentFilter = new IntentFilter(ACTION_TRACKING_STATE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(mTrackingChangedReceiver, trackingStoppedIntentFilter);
}
@ -179,7 +179,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
mSelectedTab = FRAGMENT_ID_MAP;
// disable broadcast receiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(mTrackingStoppedReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mTrackingChangedReceiver);
}
@ -258,12 +258,6 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
// dismiss notification
startTrackerService(ACTION_DISMISS, null);
// Intent intent = new Intent(this, TrackerService.class);
// intent.setAction(ACTION_DISMISS);
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// startForegroundService(intent);
// }
// hide Floating Action Button sub menu
showFloatingActionButtonMenu(false);
@ -336,17 +330,15 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
/* Handles tap on the button "resume" */
private void handleResumeButtonClick(View view) {
// change state
mTrackerServiceRunning = true;
mFloatingActionButtonState = FAB_STATE_RECORDING;
setFloatingActionButtonState();
showFloatingActionButtonMenu(false);
// show snackbar
Snackbar.make(view, R.string.snackbar_message_tracking_resumed, Snackbar.LENGTH_SHORT).setAction("Action", null).show();
// resume tracking
startTrackerService(ACTION_RESUME, null);
// hide sub menu
showFloatingActionButtonMenu(false);
}
@ -695,18 +687,23 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
/* Creates receiver for stopped tracking */
private BroadcastReceiver createTrackingStoppedReceiver() {
private BroadcastReceiver createTrackingChangedReceiver() {
return new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// change state
mTrackerServiceRunning = false;
mTrackerServiceRunning = intent.getBooleanExtra(EXTRA_TRACKING_STATE, false);
if (mTrackerServiceRunning) {
mFloatingActionButtonState = FAB_STATE_RECORDING;
} else {
mFloatingActionButtonState = FAB_STATE_SAVE;
}
setFloatingActionButtonState();
// pass tracking state to MainActivityMapFragment // todo check -> may produce NullPointerException
MainActivityMapFragment mainActivityMapFragment = (MainActivityMapFragment) mSectionsPagerAdapter.getFragment(FRAGMENT_ID_MAP);
mainActivityMapFragment.setTrackingState(false);
mainActivityMapFragment.setTrackingState(mTrackerServiceRunning);
}
};
}

View file

@ -679,7 +679,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
}
// delete temp file
mStorageHelper.deleteTempFile();
// mStorageHelper.deleteTempFile(); // todo check if necessary
}
}
/**

View file

@ -124,7 +124,7 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
startTracking(intent, true);
}
// ACTION START
// ACTION RESUME
else if (intent.getAction().equals(ACTION_RESUME) && mLocationSystemSetting) {
startTracking(intent, false);
}
@ -207,7 +207,7 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
private void startTracking(@Nullable Intent intent, boolean createNewTrack) {
LogHelper.v(LOG_TAG, "Service received command: START");
// create a new track -- if necessary
// create a new track - if requested
if (createNewTrack) {
mTrack = new Track();
} else {
@ -215,8 +215,13 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
if (storageHelper.tempFileExists()) {
// load temp track file
mTrack = storageHelper.loadTrack(FILE_TEMP_TRACK);
// try to mark last waypoint as stopover
int lastWayPoint = mTrack.getWayPoints().size() - 1;
if (lastWayPoint >= 0) {
mTrack.getWayPoints().get(lastWayPoint).setIsStopOver(true);
}
} else {
// fallback, if temfile did not exist
// fallback, if tempfile did not exist
LogHelper.e(LOG_TAG, "Unable to find previously saved track temp file.");
mTrack = new Track();
}
@ -423,6 +428,9 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
}
LocationHelper.registerLocationListeners(mLocationManager, mGPSListener, mNetworkListener);
saveTrackerServiceState(mTrackerServiceRunning, FAB_STATE_RECORDING);
// notify MainActivity
broadcastTrackingStateChange();
}
@ -433,11 +441,18 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
mTrackerServiceRunning = false;
saveTrackerServiceState(mTrackerServiceRunning, FAB_STATE_SAVE);
// notify MainActivityMapFragment
// notify MainActivity
broadcastTrackingStateChange();
}
/* Sends a broadcast with tracking changed */
private void broadcastTrackingStateChange() {
Intent i = new Intent();
i.setAction(ACTION_TRACKING_STOPPED);
i.setAction(ACTION_TRACKING_STATE_CHANGED);
i.putExtra(EXTRA_TRACK, mTrack);
i.putExtra(EXTRA_LAST_LOCATION, mCurrentBestLocation);
i.putExtra(EXTRA_TRACKING_STATE, mTrackerServiceRunning);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}

View file

@ -27,7 +27,7 @@ import org.y20k.trackbook.BuildConfig;
*/
public final class LogHelper {
private final static boolean mTesting = false;
private final static boolean mTesting = true;
public static void d(final String tag, String message) {
// include logging only in debug versions

View file

@ -73,7 +73,8 @@ public class StorageHelper implements TrackbookKeys {
}
// create temp file object // todo check -> may produce NullPointerException
mTempFile = new File(mFolder.toString() + "/" + FILE_NAME_TEMP + FILE_TYPE_TRACKBOOK_EXTENSION);
String tempFilePathName = mFolder.toString() + "/" + FILE_NAME_TEMP + FILE_TYPE_TRACKBOOK_EXTENSION;
mTempFile = new File(tempFilePathName);
// delete old track - exclude temp file
deleteOldTracks(false);

View file

@ -33,7 +33,7 @@ public interface TrackbookKeys {
String ACTION_SHOW_MAP = "SHOW_MAP";
String ACTION_TRACK_UPDATED = "TRACK_UPDATED";
String ACTION_TRACK_REQUEST = "TRACK_REQUEST";
String ACTION_TRACKING_STOPPED = "TRACKING_STOPPED";
String ACTION_TRACKING_STATE_CHANGED = "TRACKING_STATE_CHANGED";
String ACTION_TRACK_SAVE = "TRACK_SAVE";
/* EXTRAS */