mirror of
https://github.com/russok/FitoTrack.git
synced 2025-10-29 00:32:11 -07:00
Use waklock for tracking
This commit is contained in:
parent
c7ddf0ed58
commit
aa8a213af2
@ -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"
|
||||||
|
|||||||
@ -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,25 +119,17 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startUpdater(){
|
private void startUpdater(){
|
||||||
new Thread(new Runnable() {
|
new Thread(() -> {
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try{
|
try{
|
||||||
while (recorder.isActive()){
|
while (recorder.isActive()){
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
if(isResumed){
|
if(isResumed){
|
||||||
mHandler.post(new Runnable() {
|
mHandler.post(() -> updateDescription());
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
updateDescription();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch (InterruptedException e){
|
}catch (InterruptedException e){
|
||||||
e.printStackTrace();
|
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
|
||||||
|
|||||||
@ -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
|
|
||||||
public void run() {
|
|
||||||
map.getModel().mapViewPosition.setMapPosition(new MapPosition(bounds.getCenterPoint(),
|
map.getModel().mapViewPosition.setMapPosition(new MapPosition(bounds.getCenterPoint(),
|
||||||
(byte)(LatLongUtils.zoomForBounds(map.getDimension(), bounds, map.getModel().displayModel.getTileSize()))));
|
(byte)(LatLongUtils.zoomForBounds(map.getDimension(), bounds, map.getModel().displayModel.getTileSize()))));
|
||||||
map.animate().alpha(1f).setDuration(1000).start();
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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";
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user