2016-09-20 11:51:17 +00:00
|
|
|
/**
|
|
|
|
* MainActivityTrackFragment.java
|
|
|
|
* Implements the track fragment used in the track tab of the main activity
|
|
|
|
* This fragment displays a saved track
|
|
|
|
*
|
|
|
|
* This file is part of
|
|
|
|
* TRACKBOOK - Movement Recorder for Android
|
|
|
|
*
|
2017-01-19 12:36:49 +00:00
|
|
|
* Copyright (c) 2016-17 - Y20K.org
|
2016-09-20 11:51:17 +00:00
|
|
|
* Licensed under the MIT-License
|
|
|
|
* http://opensource.org/licenses/MIT
|
|
|
|
*
|
|
|
|
* Trackbook uses osmdroid - OpenStreetMap-Tools for Android
|
|
|
|
* https://github.com/osmdroid/osmdroid
|
|
|
|
*/
|
|
|
|
|
2016-09-16 15:45:10 +00:00
|
|
|
package org.y20k.trackbook;
|
|
|
|
|
2016-09-29 10:54:57 +00:00
|
|
|
import android.app.Activity;
|
2016-12-19 16:53:20 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2016-09-30 15:32:54 +00:00
|
|
|
import android.location.Location;
|
|
|
|
import android.os.AsyncTask;
|
2016-09-16 15:45:10 +00:00
|
|
|
import android.os.Bundle;
|
2016-12-05 14:10:24 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2016-09-16 15:45:10 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2016-09-29 10:54:57 +00:00
|
|
|
import android.support.design.widget.BottomSheetBehavior;
|
2016-09-16 15:45:10 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
2016-12-19 16:53:20 +00:00
|
|
|
import android.support.v4.content.LocalBroadcastManager;
|
2016-09-16 15:45:10 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2016-09-29 10:54:57 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2016-09-30 15:32:54 +00:00
|
|
|
import org.osmdroid.api.IMapController;
|
|
|
|
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
|
|
|
|
import org.osmdroid.util.GeoPoint;
|
|
|
|
import org.osmdroid.views.MapView;
|
|
|
|
import org.osmdroid.views.overlay.ItemizedIconOverlay;
|
|
|
|
import org.osmdroid.views.overlay.compass.CompassOverlay;
|
|
|
|
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider;
|
2016-09-29 10:54:57 +00:00
|
|
|
import org.y20k.trackbook.core.Track;
|
2016-09-30 15:32:54 +00:00
|
|
|
import org.y20k.trackbook.helpers.LogHelper;
|
|
|
|
import org.y20k.trackbook.helpers.MapHelper;
|
2016-09-29 10:54:57 +00:00
|
|
|
import org.y20k.trackbook.helpers.StorageHelper;
|
2016-09-30 15:32:54 +00:00
|
|
|
import org.y20k.trackbook.helpers.TrackbookKeys;
|
2016-09-16 15:45:10 +00:00
|
|
|
|
2016-10-05 11:53:31 +00:00
|
|
|
import java.text.DateFormat;
|
2017-01-23 11:32:27 +00:00
|
|
|
import java.util.Locale;
|
2016-10-05 11:53:31 +00:00
|
|
|
|
2016-09-20 11:51:17 +00:00
|
|
|
|
2016-09-16 15:45:10 +00:00
|
|
|
/**
|
2016-09-20 11:51:17 +00:00
|
|
|
* MainActivityTrackFragment class
|
2016-09-16 15:45:10 +00:00
|
|
|
*/
|
2016-09-30 15:32:54 +00:00
|
|
|
public class MainActivityTrackFragment extends Fragment implements TrackbookKeys {
|
2016-09-16 15:45:10 +00:00
|
|
|
|
2016-09-20 11:51:17 +00:00
|
|
|
/* Define log tag */
|
|
|
|
private static final String LOG_TAG = MainActivityTrackFragment.class.getSimpleName();
|
|
|
|
|
2016-09-16 15:45:10 +00:00
|
|
|
|
|
|
|
/* Main class variables */
|
2016-09-29 10:54:57 +00:00
|
|
|
private Activity mActivity;
|
2016-09-16 15:45:10 +00:00
|
|
|
private View mRootView;
|
2016-09-30 15:32:54 +00:00
|
|
|
private MapView mMapView;
|
|
|
|
private IMapController mController;
|
|
|
|
private ItemizedIconOverlay mTrackOverlay;
|
2016-09-29 10:54:57 +00:00
|
|
|
private TextView mDistanceView;
|
|
|
|
private TextView mStepsView;
|
|
|
|
private TextView mWaypointsView;
|
|
|
|
private TextView mDurationView;
|
|
|
|
private TextView mRecordingStartView;
|
|
|
|
private TextView mRecordingStopView;
|
|
|
|
private BottomSheetBehavior mStatisticsSheetBehavior;
|
|
|
|
private Track mTrack;
|
2016-12-19 16:53:20 +00:00
|
|
|
private BroadcastReceiver mTrackSavedReceiver;
|
2016-09-29 10:54:57 +00:00
|
|
|
|
2016-09-16 15:45:10 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
// action bar has options menu
|
|
|
|
setHasOptionsMenu(true);
|
2016-09-29 10:54:57 +00:00
|
|
|
|
|
|
|
// store activity
|
|
|
|
mActivity = getActivity();
|
2016-12-19 16:53:20 +00:00
|
|
|
|
2017-01-10 14:57:03 +00:00
|
|
|
// listen for finished save operation
|
2016-12-19 16:53:20 +00:00
|
|
|
mTrackSavedReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (intent.hasExtra(EXTRA_SAVE_FINISHED) && intent.getBooleanExtra(EXTRA_SAVE_FINISHED, false)) {
|
2017-01-10 14:57:03 +00:00
|
|
|
LogHelper.v(LOG_TAG, "Save operation detected. Start loading the new track.");
|
2016-12-19 16:53:20 +00:00
|
|
|
// load track and display map and statistics
|
|
|
|
LoadTrackAsyncHelper loadTrackAsyncHelper = new LoadTrackAsyncHelper();
|
|
|
|
loadTrackAsyncHelper.execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-01-10 14:57:03 +00:00
|
|
|
IntentFilter trackSavedReceiverIntentFilter = new IntentFilter(ACTION_TRACK_SAVE);
|
|
|
|
LocalBroadcastManager.getInstance(mActivity).registerReceiver(mTrackSavedReceiver, trackSavedReceiverIntentFilter);
|
2016-12-19 16:53:20 +00:00
|
|
|
|
2016-09-16 15:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
// inflate root view from xml
|
|
|
|
mRootView = inflater.inflate(R.layout.fragment_main_track, container, false);
|
|
|
|
|
2016-09-30 15:32:54 +00:00
|
|
|
// create basic map
|
|
|
|
mMapView = (MapView) mRootView.findViewById(R.id.track_map);
|
|
|
|
|
|
|
|
// get map controller
|
|
|
|
mController = mMapView.getController();
|
|
|
|
|
|
|
|
// basic map setup
|
|
|
|
mMapView.setTileSource(TileSourceFactory.MAPNIK);
|
|
|
|
mMapView.setTilesScaledToDpi(true);
|
|
|
|
|
|
|
|
// add multi-touch capability
|
|
|
|
mMapView.setMultiTouchControls(true);
|
|
|
|
|
|
|
|
// add compass to map
|
|
|
|
CompassOverlay compassOverlay = new CompassOverlay(mActivity, new InternalCompassOrientationProvider(mActivity), mMapView);
|
|
|
|
compassOverlay.enableCompass();
|
|
|
|
mMapView.getOverlays().add(compassOverlay);
|
|
|
|
|
|
|
|
// initiate map state
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
// restore saved instance of map
|
|
|
|
GeoPoint position = new GeoPoint(savedInstanceState.getDouble(INSTANCE_LATITUDE_TRACK_MAP, DEFAULT_LATITUDE), savedInstanceState.getDouble(INSTANCE_LONGITUDE_TRACK_MAP, DEFAULT_LONGITUDE));
|
|
|
|
mController.setCenter(position);
|
|
|
|
mController.setZoom(savedInstanceState.getInt(INSTANCE_ZOOM_LEVEL_MAIN_MAP, 16));
|
|
|
|
} else {
|
|
|
|
mController.setZoom(16);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-29 10:54:57 +00:00
|
|
|
// get views
|
2017-01-20 12:34:37 +00:00
|
|
|
View statisticsView = mRootView.findViewById(R.id.statistics_view);
|
2016-09-29 10:54:57 +00:00
|
|
|
mDistanceView = (TextView) mRootView.findViewById(R.id.statistics_data_distance);
|
|
|
|
mStepsView = (TextView) mRootView.findViewById(R.id.statistics_data_steps);
|
|
|
|
mWaypointsView = (TextView) mRootView.findViewById(R.id.statistics_data_waypoints);
|
|
|
|
mDurationView = (TextView) mRootView.findViewById(R.id.statistics_data_duration);
|
|
|
|
mRecordingStartView = (TextView) mRootView.findViewById(R.id.statistics_data_recording_start);
|
|
|
|
mRecordingStopView = (TextView) mRootView.findViewById(R.id.statistics_data_recording_stop);
|
2016-12-19 16:53:20 +00:00
|
|
|
View mStatisticsSheet = mRootView.findViewById(R.id.statistics_sheet);
|
2016-09-29 10:54:57 +00:00
|
|
|
|
2017-01-10 14:57:03 +00:00
|
|
|
if (savedInstanceState != null) {
|
|
|
|
// get track from saved instance and display map and statistics
|
|
|
|
mTrack = savedInstanceState.getParcelable(INSTANCE_TRACK_TRACK_MAP);
|
|
|
|
displayTrack();
|
|
|
|
} else if (mTrack == null) {
|
2016-10-05 11:53:31 +00:00
|
|
|
// load track and display map and statistics
|
|
|
|
LoadTrackAsyncHelper loadTrackAsyncHelper = new LoadTrackAsyncHelper();
|
|
|
|
loadTrackAsyncHelper.execute();
|
|
|
|
} else {
|
2016-10-25 14:38:27 +00:00
|
|
|
// just display map and statistics
|
2016-10-05 11:53:31 +00:00
|
|
|
displayTrack();
|
|
|
|
}
|
|
|
|
|
|
|
|
// show statistics sheet
|
2016-09-29 10:54:57 +00:00
|
|
|
mStatisticsSheetBehavior = BottomSheetBehavior.from(mStatisticsSheet);
|
2016-12-05 14:10:24 +00:00
|
|
|
mStatisticsSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
|
|
|
mStatisticsSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
|
|
|
|
@Override
|
|
|
|
public void onStateChanged(@NonNull View bottomSheet, int newState) {
|
|
|
|
// react to state change
|
|
|
|
switch (newState) {
|
|
|
|
case BottomSheetBehavior.STATE_EXPANDED:
|
2017-01-25 13:19:50 +00:00
|
|
|
// statistics sheet expanded
|
2016-12-05 14:10:24 +00:00
|
|
|
break;
|
|
|
|
case BottomSheetBehavior.STATE_COLLAPSED:
|
2017-01-25 13:19:50 +00:00
|
|
|
// statistics sheet collapsed
|
2016-12-05 14:10:24 +00:00
|
|
|
mStatisticsSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
|
|
|
break;
|
|
|
|
case BottomSheetBehavior.STATE_HIDDEN:
|
2017-01-25 13:19:50 +00:00
|
|
|
// statistics sheet hidden
|
2016-12-05 14:10:24 +00:00
|
|
|
mStatisticsSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
|
|
|
|
// react to dragging events
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// react to tap on sheet heading
|
2017-01-20 12:34:37 +00:00
|
|
|
statisticsView.setOnClickListener(new View.OnClickListener() {
|
2016-12-05 14:10:24 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
if (mStatisticsSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
|
|
|
|
mStatisticsSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
|
|
|
|
} else {
|
|
|
|
mStatisticsSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-29 10:54:57 +00:00
|
|
|
|
2016-09-16 15:45:10 +00:00
|
|
|
return mRootView;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2016-09-30 15:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView(){
|
|
|
|
super.onDestroyView();
|
|
|
|
|
|
|
|
// deactivate map
|
|
|
|
mMapView.onDetach();
|
|
|
|
}
|
2016-09-29 10:54:57 +00:00
|
|
|
|
|
|
|
|
2017-01-10 14:57:03 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2017-01-25 13:19:50 +00:00
|
|
|
LogHelper.v(LOG_TAG, "onDestroy called.");
|
2017-01-10 14:57:03 +00:00
|
|
|
|
|
|
|
// remove listener
|
|
|
|
LocalBroadcastManager.getInstance(mActivity).unregisterReceiver(mTrackSavedReceiver);
|
2017-01-25 13:19:50 +00:00
|
|
|
|
|
|
|
super.onDestroy();
|
2017-01-10 14:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-30 15:32:54 +00:00
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle outState) {
|
|
|
|
outState.putDouble(INSTANCE_LATITUDE_TRACK_MAP, mMapView.getMapCenter().getLatitude());
|
|
|
|
outState.putDouble(INSTANCE_LONGITUDE_TRACK_MAP, mMapView.getMapCenter().getLongitude());
|
|
|
|
outState.putInt(INSTANCE_ZOOM_LEVEL_TRACK_MAP, mMapView.getZoomLevel());
|
|
|
|
outState.putParcelable(INSTANCE_TRACK_TRACK_MAP, mTrack);
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Displays map and statistics for track */
|
|
|
|
private void displayTrack() {
|
|
|
|
GeoPoint position;
|
|
|
|
|
2016-09-29 10:54:57 +00:00
|
|
|
if (mTrack != null) {
|
2016-09-30 15:32:54 +00:00
|
|
|
// set end of track as position
|
|
|
|
Location lastLocation = mTrack.getWayPointLocation(mTrack.getSize() -1);
|
|
|
|
position = new GeoPoint(lastLocation.getLatitude(), lastLocation.getLongitude());
|
|
|
|
|
2017-01-23 11:32:27 +00:00
|
|
|
String recordingStart = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()).format(mTrack.getRecordingStart()) + " " +
|
|
|
|
DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault()).format(mTrack.getRecordingStart());
|
|
|
|
String recordingStop = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()).format(mTrack.getRecordingStop()) + " " +
|
|
|
|
DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault()).format(mTrack.getRecordingStop());
|
2016-10-05 11:53:31 +00:00
|
|
|
|
2016-09-30 15:32:54 +00:00
|
|
|
// populate views
|
2016-09-29 10:54:57 +00:00
|
|
|
mDistanceView.setText(mTrack.getTrackDistance());
|
2016-09-30 15:32:54 +00:00
|
|
|
mStepsView.setText(String.valueOf(Math.round(mTrack.getStepCount())));
|
2016-09-29 10:54:57 +00:00
|
|
|
mWaypointsView.setText(String.valueOf(mTrack.getWayPoints().size()));
|
|
|
|
mDurationView.setText(mTrack.getTrackDuration());
|
2016-10-05 11:53:31 +00:00
|
|
|
mRecordingStartView.setText(recordingStart);
|
|
|
|
mRecordingStopView.setText(recordingStop);
|
2016-09-30 15:32:54 +00:00
|
|
|
|
|
|
|
// draw track on map
|
|
|
|
drawTrackOverlay(mTrack);
|
|
|
|
} else {
|
|
|
|
position = new GeoPoint(DEFAULT_LATITUDE, DEFAULT_LONGITUDE);
|
2016-09-29 10:54:57 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 15:32:54 +00:00
|
|
|
// center map over position
|
|
|
|
mController.setCenter(position);
|
|
|
|
|
2016-09-16 15:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-30 15:32:54 +00:00
|
|
|
/* Draws track onto overlay */
|
|
|
|
private void drawTrackOverlay(Track track) {
|
|
|
|
mMapView.getOverlays().remove(mTrackOverlay);
|
|
|
|
mTrackOverlay = MapHelper.createTrackOverlay(mActivity, track, false);
|
|
|
|
mMapView.getOverlays().add(mTrackOverlay);
|
2016-09-16 15:45:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 14:57:03 +00:00
|
|
|
|
2016-09-30 15:32:54 +00:00
|
|
|
/**
|
|
|
|
* Inner class: Loads track from external storage using AsyncTask
|
|
|
|
*/
|
|
|
|
private class LoadTrackAsyncHelper extends AsyncTask<Void, Void, Void> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... voids) {
|
|
|
|
LogHelper.v(LOG_TAG, "Loading track object in background.");
|
2017-01-09 16:56:45 +00:00
|
|
|
// load track object
|
2017-01-19 12:36:49 +00:00
|
|
|
StorageHelper storageHelper = new StorageHelper(mActivity);
|
2017-01-25 13:19:50 +00:00
|
|
|
mTrack = storageHelper.loadTrack(FILE_TYPE_TRACK);
|
2016-09-30 15:32:54 +00:00
|
|
|
return null;
|
|
|
|
}
|
2016-09-16 15:45:10 +00:00
|
|
|
|
2016-09-30 15:32:54 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void aVoid) {
|
|
|
|
super.onPostExecute(aVoid);
|
2016-12-19 16:53:20 +00:00
|
|
|
LogHelper.v(LOG_TAG, "Loading finished. Displaying map and statistics of track.");
|
2016-09-30 15:32:54 +00:00
|
|
|
displayTrack();
|
|
|
|
}
|
2016-09-16 15:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|