Use waklock for tracking

This commit is contained in:
jannis 2019-08-18 16:12:15 +02:00
parent c7ddf0ed58
commit aa8a213af2
6 changed files with 41 additions and 38 deletions

View File

@ -25,6 +25,7 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -24,6 +24,7 @@ import android.content.pm.PackageManager;
import android.location.Location; import android.location.Location;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.PowerManager;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -64,6 +65,7 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
TextView timeView, gpsStatusView; TextView timeView, gpsStatusView;
boolean isResumed= false; boolean isResumed= false;
private Handler mHandler= new Handler(); private Handler mHandler= new Handler();
PowerManager.WakeLock wakeLock;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -80,15 +82,22 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
recorder= new WorkoutRecorder(this, ACTIVITY); recorder= new WorkoutRecorder(this, ACTIVITY);
recorder.start(); recorder.start();
infoViews[0]= new InfoViewHolder((TextView) findViewById(R.id.recordInfo1Title), (TextView) findViewById(R.id.recordInfo1Value)); infoViews[0]= new InfoViewHolder(findViewById(R.id.recordInfo1Title), findViewById(R.id.recordInfo1Value));
infoViews[1]= new InfoViewHolder((TextView) findViewById(R.id.recordInfo2Title), (TextView) findViewById(R.id.recordInfo2Value)); infoViews[1]= new InfoViewHolder(findViewById(R.id.recordInfo2Title), findViewById(R.id.recordInfo2Value));
infoViews[2]= new InfoViewHolder((TextView) findViewById(R.id.recordInfo3Title), (TextView) findViewById(R.id.recordInfo3Value)); infoViews[2]= new InfoViewHolder(findViewById(R.id.recordInfo3Title), findViewById(R.id.recordInfo3Value));
infoViews[3]= new InfoViewHolder((TextView) findViewById(R.id.recordInfo4Title), (TextView) 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);
updateDescription(); updateDescription();
startUpdater(); startUpdater();
acquireWakelock();
}
private void acquireWakelock(){
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "de.tadris.fitotrack:workout_recorder");
wakeLock.acquire(1000*60*120);
} }
private void setupMap(){ private void setupMap(){
@ -110,24 +119,16 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
} }
private void startUpdater(){ private void startUpdater(){
new Thread(new Runnable() { new Thread(() -> {
@Override try{
public void run() { while (recorder.isActive()){
try{ Thread.sleep(1000);
while (recorder.isActive()){ if(isResumed){
Thread.sleep(1000); mHandler.post(() -> updateDescription());
if(isResumed){
mHandler.post(new Runnable() {
@Override
public void run() {
updateDescription();
}
});
}
} }
}catch (InterruptedException e){
e.printStackTrace();
} }
}catch (InterruptedException e){
e.printStackTrace();
} }
}).start(); }).start();
} }
@ -136,7 +137,7 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
timeView.setText(UnitUtils.getHourMinuteSecondTime(recorder.getDuration())); timeView.setText(UnitUtils.getHourMinuteSecondTime(recorder.getDuration()));
infoViews[0].setText(getString(R.string.workoutDistance), UnitUtils.getDistance(recorder.getDistance())); infoViews[0].setText(getString(R.string.workoutDistance), UnitUtils.getDistance(recorder.getDistance()));
infoViews[1].setText(getString(R.string.workoutBurnedEnergy), recorder.getCalories() + " kcal"); infoViews[1].setText(getString(R.string.workoutBurnedEnergy), recorder.getCalories() + " kcal");
infoViews[2].setText(getString(R.string.workoutAvgSpeed), UnitUtils.getSpeed(recorder.getAvgSpeed())); infoViews[2].setText(getString(R.string.workoutAvgSpeed), UnitUtils.getSpeed(Math.min(100d, recorder.getAvgSpeed())));
infoViews[3].setText(getString(R.string.workoutPauseDuration), UnitUtils.getHourMinuteSecondTime(recorder.getPauseDuration())); infoViews[3].setText(getString(R.string.workoutPauseDuration), UnitUtils.getHourMinuteSecondTime(recorder.getPauseDuration()));
} }
@ -180,6 +181,9 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
mapView.destroyAll(); mapView.destroyAll();
AndroidGraphicFactory.clearResourceMemoryCache(); AndroidGraphicFactory.clearResourceMemoryCache();
super.onDestroy(); super.onDestroy();
if(wakeLock.isHeld()){
wakeLock.release();
}
} }
@Override @Override

View File

@ -20,7 +20,6 @@
package de.tadris.fitness.activity; package de.tadris.fitness.activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
@ -114,7 +113,7 @@ public class ShowWorkoutActivity extends FitoTrackActivity {
addTitle(getString(R.string.workoutBurnedEnergy)); addTitle(getString(R.string.workoutBurnedEnergy));
addKeyValue(getString(R.string.workoutTotalEnergy), workout.calorie + " kcal", addKeyValue(getString(R.string.workoutTotalEnergy), workout.calorie + " kcal",
getString(R.string.workoutEnergyConsumption), UnitUtils.getPace((double)workout.calorie / workout.length / 1000)); getString(R.string.workoutEnergyConsumption), UnitUtils.getRelativeEnergyConsumption((double)workout.calorie / ((double)workout.length / 1000)));
} }
@ -224,13 +223,10 @@ public class ShowWorkoutActivity extends FitoTrackActivity {
map.addLayer(workoutLayer); map.addLayer(workoutLayer);
final BoundingBox bounds= new BoundingBox(workoutLayer.getLatLongs()).extendMeters(50); final BoundingBox bounds= new BoundingBox(workoutLayer.getLatLongs()).extendMeters(50);
new Handler().postDelayed(new Runnable() { new Handler().postDelayed(() -> {
@Override map.getModel().mapViewPosition.setMapPosition(new MapPosition(bounds.getCenterPoint(),
public void run() { (byte)(LatLongUtils.zoomForBounds(map.getDimension(), bounds, map.getModel().displayModel.getTileSize()))));
map.getModel().mapViewPosition.setMapPosition(new MapPosition(bounds.getCenterPoint(), map.animate().alpha(1f).setDuration(1000).start();
(byte)(LatLongUtils.zoomForBounds(map.getDimension(), bounds, map.getModel().displayModel.getTileSize()))));
map.animate().alpha(1f).setDuration(1000).start();
}
}, 1000); }, 1000);
map.getModel().mapViewPosition.setMapLimit(bounds); map.getModel().mapViewPosition.setMapLimit(bounds);
@ -282,12 +278,7 @@ public class ShowWorkoutActivity extends FitoTrackActivity {
new AlertDialog.Builder(this).setTitle(R.string.deleteWorkout) new AlertDialog.Builder(this).setTitle(R.string.deleteWorkout)
.setMessage(R.string.deleteWorkoutMessage) .setMessage(R.string.deleteWorkoutMessage)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.delete, (dialog, which) -> deleteWorkout())
@Override
public void onClick(DialogInterface dialog, int which) {
deleteWorkout();
}
})
.create().show(); .create().show();
} }

View File

@ -26,7 +26,6 @@ import androidx.room.PrimaryKey;
public class Workout{ public class Workout{
public static final String WORKOUT_TYPE_RUNNING= "running"; public static final String WORKOUT_TYPE_RUNNING= "running";
public static final String WORKOUT_TYPE_WALKING= "running";
public static final String WORKOUT_TYPE_HIKING= "hiking"; public static final String WORKOUT_TYPE_HIKING= "hiking";
public static final String WORKOUT_TYPE_CYCLING= "cycling"; public static final String WORKOUT_TYPE_CYCLING= "cycling";

View File

@ -210,9 +210,15 @@ public class WorkoutRecorder implements LocationListener.LocationChangeListener
return (int)distance; return (int)distance;
} }
private int maxCalories= 0;
public int getCalories(){ public int getCalories(){
workout.avgSpeed= getAvgSpeed(); workout.avgSpeed= getAvgSpeed();
return CalorieCalculator.calculateCalories(workout, Instance.getInstance(context).userPreferences.weight); workout.duration= getDuration();
int calories= CalorieCalculator.calculateCalories(workout, Instance.getInstance(context).userPreferences.weight);
if(calories > maxCalories){
maxCalories= calories;
}
return maxCalories;
} }
/** /**

View File

@ -24,6 +24,8 @@ import de.tadris.fitness.data.Workout;
public class CalorieCalculator { public class CalorieCalculator {
/** /**
*
* workoutType, duration and avgSpeed of workout have to be set
* *
* @param workout the workout * @param workout the workout
* @param weight the weight of the person in kilogram * @param weight the weight of the person in kilogram