parent
36e91bfa6c
commit
702a2ecd05
10 changed files with 271 additions and 96 deletions
|
@ -9,7 +9,7 @@ android {
|
|||
minSdkVersion 22
|
||||
targetSdkVersion 25
|
||||
versionCode 7
|
||||
versionName "1.0.0 (Astronomy Domine )"
|
||||
versionName "1.0.0 (Astronomy Domine)"
|
||||
vectorDrawables.useSupportLibrary = true
|
||||
}
|
||||
buildTypes {
|
||||
|
@ -26,6 +26,6 @@ dependencies {
|
|||
compile 'com.android.support:appcompat-v7:25.1.0'
|
||||
compile 'com.android.support:design:25.1.0'
|
||||
compile 'com.android.support:cardview-v7:25.1.0'
|
||||
compile 'org.osmdroid:osmdroid-android:5.6.1'
|
||||
compile 'org.osmdroid:osmdroid-android:5.6.2'
|
||||
compile 'com.google.code.gson:gson:2.8.0'
|
||||
}
|
||||
|
|
|
@ -70,7 +70,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
/* Main class variables */
|
||||
private NonSwipeableViewPager mViewPager;
|
||||
private boolean mTrackerServiceRunning;
|
||||
private boolean mCurrentTrackVisible;
|
||||
// private boolean mCurrentTrackVisible;
|
||||
private boolean mPermissionsGranted;
|
||||
private boolean mFloatingActionButtonSubMenuVisible;
|
||||
private List<String> mMissingPermissions;
|
||||
|
@ -89,7 +89,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
super.onCreate(savedInstanceState);
|
||||
|
||||
// load saved state of app
|
||||
loadTrackState(this);
|
||||
loadFloatingActionButtonState(this);
|
||||
|
||||
// check permissions on Android 6 and higher
|
||||
mPermissionsGranted = false;
|
||||
|
@ -114,31 +114,33 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
protected void onStart() {
|
||||
super.onStart();
|
||||
|
||||
// add listeners to button and submenu
|
||||
if (mFloatingActionButton != null) {
|
||||
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
handleFloatingActionButtonClick(view);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (mFloatingActionButtonSubMenu1 != null) {
|
||||
mFloatingActionButtonSubMenu1.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
handleButtonSaveAndClearClick();
|
||||
}
|
||||
});
|
||||
}
|
||||
if (mFloatingActionButtonSubMenu2 != null) {
|
||||
mFloatingActionButtonSubMenu2.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
handleButtonClearClick();
|
||||
}
|
||||
});
|
||||
}
|
||||
// // add listeners to button and submenu
|
||||
// if (mFloatingActionButton != null) {
|
||||
// mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
|
||||
// @Override
|
||||
// public void onClick(View view) {
|
||||
// handleFloatingActionButtonClick(view);
|
||||
// }
|
||||
// });
|
||||
// } else {
|
||||
// LogHelper.e(LOG_TAG, "mFloatingActionButton is null!");
|
||||
// }
|
||||
// if (mFloatingActionButtonSubMenu1 != null) {
|
||||
// mFloatingActionButtonSubMenu1.setOnClickListener(new View.OnClickListener() {
|
||||
// @Override
|
||||
// public void onClick(View view) {
|
||||
// handleButtonSaveAndClearClick();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// if (mFloatingActionButtonSubMenu2 != null) {
|
||||
// mFloatingActionButtonSubMenu2.setOnClickListener(new View.OnClickListener() {
|
||||
// @Override
|
||||
// public void onClick(View view) {
|
||||
// handleButtonClearClick();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
// register broadcast receiver for stopped tracking
|
||||
mTrackingStoppedReceiver = createTrackingStoppedReceiver();
|
||||
|
@ -185,8 +187,8 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
super.onResume();
|
||||
LogHelper.v(LOG_TAG, "onResume called.");
|
||||
|
||||
// load state of track visibility
|
||||
loadTrackState(this);
|
||||
// load state of Floating Action Button
|
||||
loadFloatingActionButtonState(this);
|
||||
|
||||
// handle incoming intent (from notification)
|
||||
handleIncomingIntent();
|
||||
|
@ -202,6 +204,9 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
|
||||
// save state of Floating Action Button
|
||||
saveFloatingActionButtonState(this);
|
||||
}
|
||||
|
||||
|
||||
|
@ -253,7 +258,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
LogHelper.v(LOG_TAG, "onSaveInstanceState called.");
|
||||
outState.putBoolean(INSTANCE_TRACKING_STATE, mTrackerServiceRunning);
|
||||
outState.putInt(INSTANCE_SELECTED_TAB, mSelectedTab);
|
||||
outState.putInt(INSTANCE_FAB_STATE, mFloatingActionButtonState);
|
||||
// outState.putInt(INSTANCE_FAB_STATE, mFloatingActionButtonState);
|
||||
outState.putBoolean(INSTANCE_FAB_SUB_MENU_VISIBLE, mFloatingActionButtonSubMenuVisible);
|
||||
super.onSaveInstanceState(outState);
|
||||
}
|
||||
|
@ -266,17 +271,26 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
LogHelper.v(LOG_TAG, "onRestoreInstanceState called.");
|
||||
mTrackerServiceRunning = savedInstanceState.getBoolean(INSTANCE_TRACKING_STATE, false);
|
||||
mSelectedTab = savedInstanceState.getInt(INSTANCE_SELECTED_TAB, 0);
|
||||
mFloatingActionButtonState = savedInstanceState.getInt(INSTANCE_FAB_STATE, FAB_STATE_DEFAULT);
|
||||
// mFloatingActionButtonState = savedInstanceState.getInt(INSTANCE_FAB_STATE, FAB_STATE_DEFAULT);
|
||||
mFloatingActionButtonSubMenuVisible = savedInstanceState.getBoolean(INSTANCE_FAB_SUB_MENU_VISIBLE, false);
|
||||
}
|
||||
|
||||
|
||||
/* Loads state of track visibility from preferences */
|
||||
private void loadTrackState(Context context) {
|
||||
/* Loads state of Floating Action Button from preferences */
|
||||
private void loadFloatingActionButtonState(Context context) {
|
||||
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
mCurrentTrackVisible = settings.getBoolean(INSTANCE_TRACK_VISIBLE, false);
|
||||
mFloatingActionButtonState = settings.getInt(PREFS_FAB_STATE, FAB_STATE_DEFAULT);
|
||||
// mCurrentTrackVisible = settings.getBoolean(PREFS_TRACK_VISIBLE, false); // TODO remove mCurrentTrackVisible completely
|
||||
// mCurrentTrackVisible is handled / saved by fragment
|
||||
LogHelper.v(LOG_TAG, "Loading state. Track visibility: " + mCurrentTrackVisible);
|
||||
// LogHelper.v(LOG_TAG, "Loading state. Track visibility: " + mCurrentTrackVisible);
|
||||
}
|
||||
|
||||
/* Saves state of Floating Action Button */
|
||||
private void saveFloatingActionButtonState(Context context) {
|
||||
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putInt(PREFS_FAB_STATE, mFloatingActionButtonState);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
|
||||
|
@ -341,6 +355,33 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
showFloatingActionButtonMenu(false);
|
||||
}
|
||||
|
||||
// add listeners to button and submenu
|
||||
if (mFloatingActionButton != null) {
|
||||
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
handleFloatingActionButtonClick(view);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (mFloatingActionButtonSubMenu1 != null) {
|
||||
mFloatingActionButtonSubMenu1.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
handleButtonSaveAndClearClick();
|
||||
}
|
||||
});
|
||||
}
|
||||
if (mFloatingActionButtonSubMenu2 != null) {
|
||||
mFloatingActionButtonSubMenu2.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
handleButtonClearClick();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
// point to the on main onboarding layout
|
||||
setContentView(R.layout.activity_main_onboarding);
|
||||
|
@ -374,7 +415,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
|
||||
// change state
|
||||
mTrackerServiceRunning = true;
|
||||
mCurrentTrackVisible = true;
|
||||
// mCurrentTrackVisible = true;
|
||||
mFloatingActionButtonState = FAB_STATE_RECORDING;
|
||||
setFloatingActionButtonState();
|
||||
|
||||
|
@ -433,7 +474,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
|
||||
// clear map and save track
|
||||
mMainActivityMapFragment.clearMap(true);
|
||||
mCurrentTrackVisible = false;
|
||||
// mCurrentTrackVisible = false;
|
||||
|
||||
// display and update track tab
|
||||
mSelectedTab = FRAGMENT_ID_TRACK;
|
||||
|
@ -460,7 +501,7 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
|
|||
|
||||
// clear map, do not save track
|
||||
mMainActivityMapFragment.clearMap(false);
|
||||
mCurrentTrackVisible = false;
|
||||
// mCurrentTrackVisible = false;
|
||||
|
||||
// dismiss notification
|
||||
NotificationHelper.stop();
|
||||
|
|
|
@ -21,7 +21,6 @@ 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;
|
||||
|
@ -30,7 +29,6 @@ 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;
|
||||
|
@ -96,7 +94,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
|
|||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
LogHelper.v(LOG_TAG, "!!! MainActivityMapFragment onCreate called.");
|
||||
LogHelper.v(LOG_TAG, "MainActivityMapFragment onCreate called.");
|
||||
|
||||
// get activity
|
||||
mActivity = getActivity();
|
||||
|
@ -193,12 +191,18 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
|
|||
}
|
||||
|
||||
// restore track
|
||||
if (savedInstanceState != null) {
|
||||
StorageHelper storageHelper = new StorageHelper(mActivity, FILETYPE_TEMP);
|
||||
if (storageHelper.tempFileExists()) {
|
||||
// load track from temp file if it exists
|
||||
LoadTempTrackAsyncHelper loadTempTrackAsyncHelper = new LoadTempTrackAsyncHelper();
|
||||
loadTempTrackAsyncHelper.execute();
|
||||
} else if (savedInstanceState != null) {
|
||||
// load track from saved instance
|
||||
mTrack = savedInstanceState.getParcelable(INSTANCE_TRACK_MAIN_MAP);
|
||||
}
|
||||
if (mTrack != null) {
|
||||
drawTrackOverlay(mTrack);
|
||||
}
|
||||
}
|
||||
|
||||
// mark user's location on map
|
||||
if (mCurrentBestLocation != null && !mTrackerServiceRunning) {
|
||||
|
@ -256,8 +260,8 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
|
|||
// disable content observer for changes in System Settings
|
||||
mActivity.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
|
||||
|
||||
// save state of track visibility
|
||||
saveTrackVisibilityState(mActivity);
|
||||
// // save state of track visibility
|
||||
// saveTrackVisibilityState(mActivity);
|
||||
}
|
||||
|
||||
|
||||
|
@ -407,8 +411,8 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
|
|||
mTrack = null;
|
||||
}
|
||||
|
||||
// save track state
|
||||
saveTrackVisibilityState(mActivity);
|
||||
// // save track state
|
||||
// saveTrackVisibilityState(mActivity);
|
||||
}
|
||||
|
||||
|
||||
|
@ -477,7 +481,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
|
|||
return new LocationListener() {
|
||||
public void onLocationChanged(Location location) {
|
||||
// check if the new location is better
|
||||
if (LocationHelper.isBetterLocation(location, mCurrentBestLocation)) {
|
||||
if (mCurrentBestLocation == null || LocationHelper.isBetterLocation(location, mCurrentBestLocation)) {
|
||||
// save location
|
||||
mCurrentBestLocation = location;
|
||||
// mark user's new location on map and remove last marker
|
||||
|
@ -574,14 +578,14 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
|
|||
}
|
||||
|
||||
|
||||
/* Saves state of track visibility to SharedPreferences */
|
||||
private void saveTrackVisibilityState(Context context) {
|
||||
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putBoolean(INSTANCE_TRACK_VISIBLE, (mTrackOverlay != null));
|
||||
editor.apply();
|
||||
LogHelper.v(LOG_TAG, "Saving state: track visibility = " + (mTrackOverlay != null));
|
||||
}
|
||||
// /* Saves state of track visibility to SharedPreferences */
|
||||
// private void saveTrackVisibilityState(Context context) {
|
||||
// SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
// SharedPreferences.Editor editor = settings.edit();
|
||||
// editor.putBoolean(PREFS_TRACK_VISIBLE, (mTrackOverlay != null));
|
||||
// editor.apply();
|
||||
// LogHelper.v(LOG_TAG, "Saving state: track visibility = " + (mTrackOverlay != null));
|
||||
// }
|
||||
|
||||
|
||||
// /* Saves state of map */
|
||||
|
@ -647,7 +651,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
|
|||
protected Void doInBackground(Void... voids) {
|
||||
LogHelper.v(LOG_TAG, "Saving track object in background.");
|
||||
// save track object
|
||||
StorageHelper storageHelper = new StorageHelper(mActivity);
|
||||
StorageHelper storageHelper = new StorageHelper(mActivity, FILETYPE_TRACK);
|
||||
storageHelper.saveTrack(mTrack);
|
||||
return null;
|
||||
}
|
||||
|
@ -668,4 +672,35 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inner class: Loads track from external storage using AsyncTask
|
||||
*/
|
||||
private class LoadTempTrackAsyncHelper extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
StorageHelper storageHelper;
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
LogHelper.v(LOG_TAG, "Loading temporary track object in background.");
|
||||
// load track object
|
||||
storageHelper = new StorageHelper(mActivity, FILETYPE_TEMP);
|
||||
mTrack = storageHelper.loadTrack();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
super.onPostExecute(aVoid);
|
||||
LogHelper.v(LOG_TAG, "Loading finished.");
|
||||
|
||||
// draw track on map
|
||||
if (mTrack != null) {
|
||||
drawTrackOverlay(mTrack);
|
||||
}
|
||||
|
||||
// delete temp file
|
||||
storageHelper.deleteTempFile();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -303,9 +303,9 @@ public class MainActivityTrackFragment extends Fragment implements TrackbookKeys
|
|||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
LogHelper.v(LOG_TAG, "Loading track object in background.");
|
||||
// save track object
|
||||
StorageHelper storageHelper = new StorageHelper(mActivity);
|
||||
mTrack = storageHelper.loadTrack(storageHelper.getMostCurrentTrack());
|
||||
// load track object
|
||||
StorageHelper storageHelper = new StorageHelper(mActivity, FILETYPE_TRACK);
|
||||
mTrack = storageHelper.loadTrack();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ package org.y20k.trackbook;
|
|||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.ContentObserver;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
|
@ -27,10 +28,12 @@ import android.hardware.SensorManager;
|
|||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.CountDownTimer;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.widget.Toast;
|
||||
|
@ -40,9 +43,9 @@ import org.y20k.trackbook.core.WayPoint;
|
|||
import org.y20k.trackbook.helpers.LocationHelper;
|
||||
import org.y20k.trackbook.helpers.LogHelper;
|
||||
import org.y20k.trackbook.helpers.NotificationHelper;
|
||||
import org.y20k.trackbook.helpers.StorageHelper;
|
||||
import org.y20k.trackbook.helpers.TrackbookKeys;
|
||||
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
|
@ -105,6 +108,12 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
|
|||
// ACTION STOP
|
||||
else if (intent.getAction().equals(ACTION_STOP) || !mLocationSystemSetting) {
|
||||
stopTracking();
|
||||
|
||||
// save changed state of Floating Action Button
|
||||
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putInt(PREFS_FAB_STATE, FAB_STATE_SAVE);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
// START_STICKY is used for services that are explicitly started and stopped as needed
|
||||
|
@ -218,11 +227,15 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
|
|||
LogHelper.v(LOG_TAG, "Service received command: STOP");
|
||||
|
||||
// store current date and time
|
||||
mTrack.setRecordingEnd(GregorianCalendar.getInstance().getTime());
|
||||
mTrack.setRecordingEnd();
|
||||
|
||||
// stop timer
|
||||
mTimer.cancel();
|
||||
|
||||
// save a temp file in case the activity has been killed
|
||||
SaveTempTrackAsyncHelper saveTempTrackAsyncHelper = new SaveTempTrackAsyncHelper();
|
||||
saveTempTrackAsyncHelper.execute();
|
||||
|
||||
// change notification
|
||||
NotificationHelper.update(mTrack, false);
|
||||
|
||||
|
@ -369,7 +382,31 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
|
|||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* End of inner class
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Inner class: Saves track to external storage using AsyncTask
|
||||
*/
|
||||
private class SaveTempTrackAsyncHelper extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
LogHelper.v(LOG_TAG, "Saving temporary track object in background.");
|
||||
// save track object
|
||||
StorageHelper storageHelper = new StorageHelper(TrackerService.this, FILETYPE_TEMP);
|
||||
storageHelper.saveTrack(mTrack);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
super.onPostExecute(aVoid);
|
||||
LogHelper.v(LOG_TAG, "Saving finished.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -53,8 +53,8 @@ public class Track implements TrackbookKeys, Parcelable {
|
|||
/* Constructor */
|
||||
public Track() {
|
||||
mWayPoints = new ArrayList<WayPoint>();
|
||||
mTrackLength = 0;
|
||||
mStepCount = 0;
|
||||
mTrackLength = 0f;
|
||||
mStepCount = 0f;
|
||||
mRecordingStart = GregorianCalendar.getInstance().getTime();
|
||||
mRecordingStop = mRecordingStart;
|
||||
}
|
||||
|
@ -113,9 +113,9 @@ public class Track implements TrackbookKeys, Parcelable {
|
|||
}
|
||||
|
||||
|
||||
/* Setter for end time and date of recording */
|
||||
public void setRecordingEnd (Date recordingEnd) {
|
||||
mRecordingStop = recordingEnd;
|
||||
/* Sets end time and date of recording */
|
||||
public void setRecordingEnd () {
|
||||
mRecordingStop = GregorianCalendar.getInstance().getTime();;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -92,8 +92,7 @@ public class NotificationHelper implements TrackbookKeys {
|
|||
private static NotificationCompat.Builder getNotificationBuilder(Track track, boolean tracking) {
|
||||
|
||||
String contentText = mService.getString(R.string.notification_content_distance) + ": " + track.getTrackDistance() + " | " +
|
||||
mService.getString(R.string.notification_content_duration) + " : " + track.getTrackDuration();
|
||||
|
||||
mService.getString(R.string.notification_content_duration) + ": " + track.getTrackDuration();
|
||||
|
||||
// ACTION: NOTIFICATION TAP
|
||||
Intent tapActionIntent = new Intent(mService, MainActivity.class);
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
package org.y20k.trackbook.helpers;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.os.EnvironmentCompat;
|
||||
|
@ -50,17 +50,19 @@ public class StorageHelper implements TrackbookKeys {
|
|||
private static final String LOG_TAG = StorageHelper.class.getSimpleName();
|
||||
|
||||
/* Main class variables */
|
||||
private final int mMaxTrackFiles = 25;
|
||||
private final int mFileType;
|
||||
private final String mDirectoryName = "tracks";
|
||||
private final String mFileExtension = ".trackbook";
|
||||
private final Activity mActivity;
|
||||
private final Context mActivity;
|
||||
private File mFolder;
|
||||
private File mTempFile;
|
||||
|
||||
|
||||
/* Constructor */
|
||||
public StorageHelper(Activity activity) {
|
||||
public StorageHelper(Context activity, int fileType) {
|
||||
// store activity
|
||||
mActivity = activity;
|
||||
mFileType = fileType;
|
||||
|
||||
// get "tracks" folder
|
||||
mFolder = mActivity.getExternalFilesDir(mDirectoryName);
|
||||
|
@ -71,6 +73,21 @@ public class StorageHelper implements TrackbookKeys {
|
|||
LogHelper.v(LOG_TAG, "Creating new folder: " + mFolder.toString());
|
||||
mFolder.mkdir();
|
||||
}
|
||||
|
||||
// create temp file object
|
||||
mTempFile = new File(mFolder.toString() + "/" + FILENAME_TEMP + mFileExtension);
|
||||
}
|
||||
|
||||
|
||||
/* Checks if a temp file exits */
|
||||
public boolean tempFileExists() {
|
||||
return mTempFile.exists();
|
||||
}
|
||||
|
||||
|
||||
/* Deletes temp file - if it exits */
|
||||
public boolean deleteTempFile() {
|
||||
return mTempFile.exists() && mTempFile.delete();
|
||||
}
|
||||
|
||||
|
||||
|
@ -87,8 +104,13 @@ public class StorageHelper implements TrackbookKeys {
|
|||
|
||||
if (mFolder != null && mFolder.exists() && mFolder.isDirectory() && mFolder.canWrite() && recordingStart != null && track != null) {
|
||||
// construct filename from track recording date
|
||||
String fileName;
|
||||
if (mFileType == FILETYPE_TEMP) {
|
||||
fileName = FILENAME_TEMP + mFileExtension;
|
||||
} else {
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
|
||||
String fileName = dateFormat.format(recordingStart) + mFileExtension;
|
||||
fileName = dateFormat.format(recordingStart) + mFileExtension;
|
||||
}
|
||||
File file = new File(mFolder.toString() + "/" + fileName);
|
||||
|
||||
// convert to JSON
|
||||
|
@ -104,8 +126,10 @@ public class StorageHelper implements TrackbookKeys {
|
|||
return false;
|
||||
}
|
||||
|
||||
// if write was successful delete old track files
|
||||
// if write was successful delete old track files - only if not a temp file
|
||||
if (mFileType != FILETYPE_TEMP) {
|
||||
deleteOldTracks();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
|
@ -118,7 +142,15 @@ public class StorageHelper implements TrackbookKeys {
|
|||
|
||||
|
||||
/* Loads given file into memory */
|
||||
public Track loadTrack (File file) {
|
||||
public Track loadTrack () {
|
||||
|
||||
// get file reference
|
||||
File file;
|
||||
if (mFileType == FILETYPE_TEMP) {
|
||||
file = getTempFile();
|
||||
} else {
|
||||
file = getMostCurrentTrack();
|
||||
}
|
||||
|
||||
// check if given file was null
|
||||
if (file == null) {
|
||||
|
@ -127,7 +159,7 @@ public class StorageHelper implements TrackbookKeys {
|
|||
}
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||
LogHelper.v(LOG_TAG, "Loading track to external storage: " + file.toString());
|
||||
LogHelper.v(LOG_TAG, "Loading track from external storage: " + file.toString());
|
||||
|
||||
String line;
|
||||
StringBuilder sb = new StringBuilder("");
|
||||
|
@ -151,7 +183,7 @@ public class StorageHelper implements TrackbookKeys {
|
|||
|
||||
|
||||
/* Gets most current track from directory */
|
||||
public File getMostCurrentTrack() {
|
||||
private File getMostCurrentTrack() {
|
||||
|
||||
// get "tracks" folder
|
||||
mFolder = mActivity.getExternalFilesDir(mDirectoryName);
|
||||
|
@ -160,16 +192,26 @@ public class StorageHelper implements TrackbookKeys {
|
|||
// get files and sort them
|
||||
File[] files = mFolder.listFiles();
|
||||
files = sortFiles(files);
|
||||
if (files.length > 0 && files[0].getName().endsWith(mFileExtension)){
|
||||
if (files.length > 0 && files[0].getName().endsWith(mFileExtension) && !files[0].equals(mTempFile)){
|
||||
// return latest track
|
||||
return files[0];
|
||||
}
|
||||
}
|
||||
LogHelper.e(LOG_TAG, "Unable to get files from given folder.");
|
||||
LogHelper.e(LOG_TAG, "Unable to get files from given folder. Folder is probably empty.");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/* Gets temp file - if it exists */
|
||||
private File getTempFile() {
|
||||
if (mTempFile.exists()) {
|
||||
return mTempFile;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Gets the last track from directory */
|
||||
private void deleteOldTracks() {
|
||||
|
||||
|
@ -187,9 +229,9 @@ public class StorageHelper implements TrackbookKeys {
|
|||
int numberOfFiles = files.length;
|
||||
|
||||
// keep the latest ten (mMaxTrackFiles) track files
|
||||
int index = mMaxTrackFiles;
|
||||
int index = MAXIMUM_TRACK_FILES;
|
||||
// iterate through array
|
||||
while (index < numberOfFiles && files[index].getName().endsWith(mFileExtension)) {
|
||||
while (index < numberOfFiles && files[index].getName().endsWith(mFileExtension) && !files[index].equals(mTempFile)) {
|
||||
files[index].delete();
|
||||
index++;
|
||||
}
|
||||
|
|
|
@ -52,17 +52,18 @@ public interface TrackbookKeys {
|
|||
|
||||
/* PREFS */
|
||||
String PREFS_NAME = "org.y20k.trackbook.prefs";
|
||||
String PREFS_TILE_SOURCE = "tileSource";
|
||||
String PREFS_LATITUDE = "latitude";
|
||||
String PREFS_LONGITUDE = "longitude";
|
||||
String PREFS_ZOOM_LEVEL = "zoomLevel";
|
||||
String PREFS_SHOW_LOCATION = "showLocation";
|
||||
String PREFS_SHOW_COMPASS = "showCompass";
|
||||
String PREFS_TILE_SOURCE = "tileSourcePrefs";
|
||||
String PREFS_LATITUDE = "latitudePrefs";
|
||||
String PREFS_LONGITUDE = "longitudePrefs";
|
||||
String PREFS_ZOOM_LEVEL = "zoomLevelPrefs";
|
||||
String PREFS_SHOW_LOCATION = "showLocationPrefs";
|
||||
String PREFS_SHOW_COMPASS = "showCompassPrefs";
|
||||
String PREFS_FAB_STATE = "fabStatePrefs";
|
||||
String PREFS_TRACK_VISIBLE = "trackVisiblePrefs";
|
||||
|
||||
/* INSTANCE STATE */
|
||||
String INSTANCE_FIRST_START = "firstStart";
|
||||
String INSTANCE_TRACKING_STATE = "trackingState";
|
||||
String INSTANCE_TRACK_VISIBLE = "trackVisible";
|
||||
String INSTANCE_SELECTED_TAB = "selectedTab";
|
||||
String INSTANCE_FAB_STATE = "fabState";
|
||||
String INSTANCE_FAB_SUB_MENU_VISIBLE = "fabSubMenuVisible";
|
||||
|
@ -88,6 +89,7 @@ public interface TrackbookKeys {
|
|||
long FIFTEEN_SECONDS_IN_MILLISECONDS = 15000; // timer interval for tracking
|
||||
long FIVE_MINUTES_IN_NANOSECONDS = 5L * 60000000000L; // determines a stop over
|
||||
long TWO_MINUTES_IN_NANOSECONDS = 2L * 60000000000L; // defines an old location
|
||||
int MAXIMUM_TRACK_FILES = 25;
|
||||
|
||||
/* MISC */
|
||||
int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
|
||||
|
@ -98,6 +100,10 @@ public interface TrackbookKeys {
|
|||
int FAB_STATE_DEFAULT = 0;
|
||||
int FAB_STATE_RECORDING = 1;
|
||||
int FAB_STATE_SAVE = 2;
|
||||
int FILETYPE_TEMP = 0;
|
||||
int FILETYPE_TRACK = 1;
|
||||
|
||||
String FILENAME_TEMP = "temp";
|
||||
|
||||
double DEFAULT_LATITUDE = 49.41667; // latitude Nordkapp, Norway
|
||||
double DEFAULT_LONGITUDE = 8.67201; // longitude Nordkapp, Norway
|
||||
|
|
|
@ -28,18 +28,25 @@ import android.view.MotionEvent;
|
|||
import android.view.animation.DecelerateInterpolator;
|
||||
import android.widget.Scroller;
|
||||
|
||||
import org.y20k.trackbook.helpers.LogHelper;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
|
||||
public class NonSwipeableViewPager extends ViewPager {
|
||||
|
||||
/* Define log tag */
|
||||
private static final String LOG_TAG = NonSwipeableViewPager.class.getSimpleName();
|
||||
|
||||
|
||||
/* Constructor */
|
||||
public NonSwipeableViewPager(Context context) {
|
||||
super(context);
|
||||
setMyScroller();
|
||||
}
|
||||
|
||||
|
||||
/* Constructor */
|
||||
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setMyScroller();
|
||||
|
@ -59,9 +66,9 @@ public class NonSwipeableViewPager extends ViewPager {
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
//down one is added for smooth scrolling
|
||||
|
||||
/* Attaches a custom scroller to a ViewPager */
|
||||
private void setMyScroller() {
|
||||
try {
|
||||
Class<?> viewpager = ViewPager.class;
|
||||
|
@ -69,11 +76,15 @@ public class NonSwipeableViewPager extends ViewPager {
|
|||
scroller.setAccessible(true);
|
||||
scroller.set(this, new MyScroller(getContext()));
|
||||
} catch (Exception e) {
|
||||
LogHelper.e(LOG_TAG, "Problem accessing or modifying the mScroller field.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inner class: MyScroller is a custom Scroller
|
||||
*/
|
||||
public class MyScroller extends Scroller {
|
||||
public MyScroller(Context context) {
|
||||
super(context, new DecelerateInterpolator());
|
||||
|
@ -84,4 +95,8 @@ public class NonSwipeableViewPager extends ViewPager {
|
|||
super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* End of inner class
|
||||
*/
|
||||
|
||||
}
|
Loading…
Reference in a new issue