#14 Show GPS Status icon in Recorder

This commit is contained in:
jannis 2019-08-21 20:23:20 +02:00
parent 50e8976424
commit 9e893c5fad
4 changed files with 76 additions and 22 deletions

View File

@ -57,7 +57,7 @@ import de.tadris.fitness.map.tilesource.TileSources;
import de.tadris.fitness.util.ThemeManager; import de.tadris.fitness.util.ThemeManager;
import de.tadris.fitness.util.unit.UnitUtils; import de.tadris.fitness.util.unit.UnitUtils;
public class RecordWorkoutActivity extends FitoTrackActivity implements LocationListener.LocationChangeListener { public class RecordWorkoutActivity extends FitoTrackActivity implements LocationListener.LocationChangeListener, WorkoutRecorder.GpsStateChangedListener {
public static String ACTIVITY= Workout.WORKOUT_TYPE_RUNNING; public static String ACTIVITY= Workout.WORKOUT_TYPE_RUNNING;
@ -88,7 +88,7 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
checkPermissions(); checkPermissions();
recorder= new WorkoutRecorder(this, ACTIVITY); recorder= new WorkoutRecorder(this, ACTIVITY, this);
recorder.start(); recorder.start();
infoViews[0]= new InfoViewHolder(findViewById(R.id.recordInfo1Title), findViewById(R.id.recordInfo1Value)); infoViews[0]= new InfoViewHolder(findViewById(R.id.recordInfo1Title), findViewById(R.id.recordInfo1Value));
@ -96,6 +96,7 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
infoViews[2]= new InfoViewHolder(findViewById(R.id.recordInfo3Title), findViewById(R.id.recordInfo3Value)); infoViews[2]= new InfoViewHolder(findViewById(R.id.recordInfo3Title), findViewById(R.id.recordInfo3Value));
infoViews[3]= new InfoViewHolder(findViewById(R.id.recordInfo4Title), findViewById(R.id.recordInfo4Value)); infoViews[3]= new InfoViewHolder(findViewById(R.id.recordInfo4Title), findViewById(R.id.recordInfo4Value));
timeView= findViewById(R.id.recordTime); timeView= findViewById(R.id.recordTime);
gpsStatusView= findViewById(R.id.recordGpsStatus);
updateDescription(); updateDescription();
@ -303,6 +304,11 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
} }
} }
@Override
public void onGPSStateChanged(WorkoutRecorder.GpsState oldState, WorkoutRecorder.GpsState state) {
gpsStatusView.setTextColor(state.color);
}
public static class InfoViewHolder{ public static class InfoViewHolder{
TextView titleView, valueView; TextView titleView, valueView;

View File

@ -20,6 +20,7 @@
package de.tadris.fitness.location; package de.tadris.fitness.location;
import android.content.Context; import android.content.Context;
import android.graphics.Color;
import android.location.Location; import android.location.Location;
import android.util.Log; import android.util.Log;
@ -61,9 +62,16 @@ public class WorkoutRecorder implements LocationListener.LocationChangeListener
private double distance= 0; private double distance= 0;
private boolean hasBegan = false; private boolean hasBegan = false;
public WorkoutRecorder(Context context, String workoutType) { private static final double SIGNAL_BAD_THRESHOLD= 20; // In meters
private static final int SIGNAL_LOST_THRESHOLD= 10000; // In milliseconds
private Location lastFix;
private GpsStateChangedListener gpsStateChangedListener;
private GpsState gpsState= GpsState.SIGNAL_LOST;
public WorkoutRecorder(Context context, String workoutType, GpsStateChangedListener gpsStateChangedListener) {
this.context= context; this.context= context;
this.state= RecordingState.IDLE; this.state= RecordingState.IDLE;
this.gpsStateChangedListener= gpsStateChangedListener;
this.workout= new Workout(); this.workout= new Workout();
@ -92,34 +100,47 @@ public class WorkoutRecorder implements LocationListener.LocationChangeListener
} }
private void startWatchdog(){ private void startWatchdog(){
new Thread(new Runnable() { new Thread(() -> {
@Override try {
public void run() { while (isActive()){
try { checkSignalState();
while (isActive()){ synchronized (samples){
synchronized (samples){ if(samples.size() > 2){
if(samples.size() > 2){ WorkoutSample lastSample= samples.get(samples.size()-1);
WorkoutSample lastSample= samples.get(samples.size()-1); if(System.currentTimeMillis() - lastSampleTime > PAUSE_TIME){
if(System.currentTimeMillis() - lastSampleTime > PAUSE_TIME){ if(state == RecordingState.RUNNING){
if(state == RecordingState.RUNNING){ pause();
pause(); }
} }else{
}else{ if(state == RecordingState.PAUSED){
if(state == RecordingState.PAUSED){ resume();
resume();
}
} }
} }
} }
Thread.sleep(5000);
} }
} catch (InterruptedException e) { Thread.sleep(5000);
e.printStackTrace();
} }
} catch (InterruptedException e) {
e.printStackTrace();
} }
}).start(); }).start();
} }
private void checkSignalState(){
GpsState state;
if(System.currentTimeMillis() - lastFix.getTime() > SIGNAL_LOST_THRESHOLD){
state= GpsState.SIGNAL_LOST;
}else if(lastFix.getAccuracy() > SIGNAL_BAD_THRESHOLD){
state= GpsState.SIGNAL_BAD;
}else{
state= GpsState.SIGNAL_OKAY;
}
if(state != gpsState){
gpsStateChangedListener.onGPSStateChanged(gpsState, state);
gpsState= state;
}
}
private void resume(){ private void resume(){
Log.i("Recorder", "Resume"); Log.i("Recorder", "Resume");
state= RecordingState.RUNNING; state= RecordingState.RUNNING;
@ -169,6 +190,7 @@ public class WorkoutRecorder implements LocationListener.LocationChangeListener
@Override @Override
public void onLocationChange(Location location) { public void onLocationChange(Location location) {
lastFix= location;
if(isActive()){ if(isActive()){
double distance= 0; double distance= 0;
if(getSampleCount() > 0){ if(getSampleCount() > 0){
@ -262,4 +284,20 @@ public class WorkoutRecorder implements LocationListener.LocationChangeListener
IDLE, RUNNING, PAUSED, STOPPED IDLE, RUNNING, PAUSED, STOPPED
} }
public enum GpsState{
SIGNAL_LOST(Color.RED),
SIGNAL_OKAY(Color.GREEN),
SIGNAL_BAD(Color.YELLOW);
public int color;
GpsState(int color) {
this.color = color;
}
}
public interface GpsStateChangedListener{
void onGPSStateChanged(GpsState oldState, GpsState state);
}
} }

View File

@ -47,6 +47,15 @@
android:layout_gravity="center" android:layout_gravity="center"
android:src="@drawable/location_marker" /> android:src="@drawable/location_marker" />
<TextView
android:id="@+id/recordGpsStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="5dp"
android:text="@string/gps"
android:textColor="@android:color/transparent" />
</FrameLayout> </FrameLayout>

View File

@ -98,4 +98,5 @@
<string name="exportDataSummary">This takes a backup of all your preferences and workout data</string> <string name="exportDataSummary">This takes a backup of all your preferences and workout data</string>
<string name="importBackup">Import Data Backup</string> <string name="importBackup">Import Data Backup</string>
<string name="importBackupSummary">Restore a taken backup</string> <string name="importBackupSummary">Restore a taken backup</string>
<string name="gps">GPS</string>
</resources> </resources>