mirror of
https://github.com/GoldenCheetah/GoldenCheetah.git
synced 2026-04-15 05:32:21 +00:00
Add better crash detection and clean up dialog message title mix up
This commit is contained in:
@@ -8,6 +8,9 @@
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.SEND_SMS"/>
|
||||
<uses-sdk android:minSdkVersion="19"
|
||||
android:targetSdkVersion="19"
|
||||
android:maxSdkVersion="22" />
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
|
||||
@@ -48,12 +48,12 @@ import android.telephony.SmsManager;
|
||||
*/
|
||||
public class RideService extends Service
|
||||
{
|
||||
public static GzipWriter buf; //writes to log file buffered
|
||||
public static long startTime; //start time of the ride
|
||||
public static Map<String, String> currentValues; //hash of current values
|
||||
public boolean rideStarted = false; //have we started logging the ride
|
||||
|
||||
public static Map<String, Base<?>> sensors = new HashMap<String, Base<?>>();
|
||||
public GzipWriter buf; //writes to log file buffered
|
||||
public long startTime; //start time of the ride
|
||||
public Map<String, String> currentValues; //hash of current values
|
||||
public boolean rideStarted = false; //have we started logging the ride
|
||||
|
||||
public Map<String, Base<?>> sensors = new HashMap<String, Base<?>>();
|
||||
//All other Android sensor class
|
||||
MultiDeviceSearch mSearch; //Ant+ device search class to init connections
|
||||
MultiDeviceSearch.SearchCallbacks mCallback; //Ant+ device class to setup sensors after they are found
|
||||
@@ -65,7 +65,6 @@ public class RideService extends Service
|
||||
SharedPreferences settings; //Object to load our setting from android's storage
|
||||
public Boolean snoop = false; //should we log others ant+ devices
|
||||
Set<String> pairedAnts; //list of ant devices to pair with
|
||||
private TimerTask smsHomeTimerTask; //timer task to send periodic messages to emergency contact
|
||||
public boolean phoneHome = false; //if we should send the messages or not
|
||||
private Timer timer; //timer class to control the periodic messages
|
||||
public boolean detectCrash = false; //should we try to detect crashes and message emergency contact
|
||||
@@ -218,14 +217,17 @@ public class RideService extends Service
|
||||
|
||||
if(phoneHome) {
|
||||
timer = new Timer();
|
||||
smsHomeTimerTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
phoneHome();
|
||||
}
|
||||
};
|
||||
|
||||
timer.scheduleAtFixedRate(smsHomeTimerTask, 600000, 600000); //every ten min let them know where you are at
|
||||
timer.scheduleAtFixedRate(
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
phoneHome();
|
||||
}
|
||||
},
|
||||
600000,
|
||||
600000
|
||||
); //every ten min let them know where you are at
|
||||
phoneStart();
|
||||
}
|
||||
|
||||
@@ -254,7 +256,7 @@ public class RideService extends Service
|
||||
public void phoneCrash(double mag) {
|
||||
String body = "CRASH DETECTED!\n";
|
||||
if(currentValues.containsKey("LAT") && currentValues.containsKey("LON")) {
|
||||
body = body + "https://www.google.com/maps/@" + currentValues.get("LAT") + "," + currentValues.get("LON") + ",10z";
|
||||
body = body + "https://www.google.com/maps/place/" + currentValues.get("LAT") + "," + currentValues.get("LON");
|
||||
} else {
|
||||
body = body + "Unknow location.";
|
||||
}
|
||||
@@ -262,14 +264,43 @@ public class RideService extends Service
|
||||
smsHome(body);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* let a love one know where you are at about every 10 min
|
||||
*/
|
||||
public void phoneStart() {
|
||||
String body = "I'm starting my ride";
|
||||
public void phoneCrashConfirm() {
|
||||
String body = "CRASH CONFIRMED!\n";
|
||||
if(currentValues.containsKey("LAT") && currentValues.containsKey("LON")) {
|
||||
body = body + ":\n https://www.google.com/maps/@" + currentValues.get("LAT") + "," + currentValues.get("LON") + ",10z";
|
||||
body = body + "https://www.google.com/maps/place/" + currentValues.get("LAT") + "," + currentValues.get("LON");
|
||||
} else {
|
||||
body = body + "Unknow location.";
|
||||
}
|
||||
smsHome(body);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* let them know we are starting
|
||||
*/
|
||||
public void phoneStart() {
|
||||
smsWithLocation("I'm starting my ride");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* let them know we are stopping
|
||||
*/
|
||||
public void phoneStop() {
|
||||
smsWithLocation("I'm done with my ride");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* send an sms with location
|
||||
*/
|
||||
public void smsWithLocation(String body) {
|
||||
if(currentValues.containsKey("LAT") && currentValues.containsKey("LON")) {
|
||||
body = body + "\n https://www.google.com/maps/place/" + currentValues.get("LAT") + "," + currentValues.get("LON");
|
||||
} else {
|
||||
body = body + ".";
|
||||
}
|
||||
@@ -282,8 +313,7 @@ public class RideService extends Service
|
||||
* let a love one know where you are at about every 10 min
|
||||
*/
|
||||
public void phoneHome() {
|
||||
String body = "I'm here:\n https://www.google.com/maps/@" + currentValues.get("LAT") + "," + currentValues.get("LON") + ",10z";
|
||||
smsHome(body);
|
||||
smsWithLocation("I'm riding:");
|
||||
}
|
||||
|
||||
|
||||
@@ -299,6 +329,8 @@ public class RideService extends Service
|
||||
//stop the ride and clean up resources
|
||||
protected void stopRide() {
|
||||
if(!rideStarted) return;
|
||||
|
||||
phoneStop();
|
||||
|
||||
for (Map.Entry<String, Base<?>> entry : sensors.entrySet()) {
|
||||
entry.getValue().onDestroy();
|
||||
|
||||
@@ -222,8 +222,8 @@ public class StartActivity extends FragmentActivity
|
||||
|
||||
// 2. Chain together various setter methods to set the dialog characteristics
|
||||
AlertDialog dialog = builder
|
||||
.setMessage(title)
|
||||
.setTitle(message)
|
||||
.setMessage(message)
|
||||
.setTitle(title)
|
||||
.setView(view)
|
||||
.setPositiveButton(positiveLabel, positiveClick)
|
||||
.create();
|
||||
|
||||
@@ -28,9 +28,9 @@ public class Base<T>
|
||||
|
||||
//setup references to buffer and current values and context
|
||||
public void init(RideService mContext) {
|
||||
buf = RideService.buf; //shared file buffer object
|
||||
currentValues = RideService.currentValues; //shared currentValues object
|
||||
context = mContext;
|
||||
buf = context.buf; //shared file buffer object
|
||||
currentValues = context.currentValues; //shared currentValues object
|
||||
}
|
||||
|
||||
|
||||
@@ -149,7 +149,7 @@ public class Base<T>
|
||||
|
||||
//get current time stamp
|
||||
public String getTs() {
|
||||
return reduceNumberToString((double) (System.currentTimeMillis() - RideService.startTime) / 1000.0);
|
||||
return reduceNumberToString((double) (System.currentTimeMillis() - context.startTime) / 1000.0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.ridelogger.listners;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import com.ridelogger.RideService;
|
||||
|
||||
@@ -61,30 +63,69 @@ public class Sensors extends Base<Object>
|
||||
}
|
||||
if(mAccel != null) {
|
||||
accelListner = new SensorEventListener() {
|
||||
@Override
|
||||
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
|
||||
|
||||
@Override
|
||||
public final void onSensorChanged(SensorEvent event) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("ms2x", reduceNumberToString(event.values[0]));
|
||||
map.put("ms2y", reduceNumberToString(event.values[1]));
|
||||
map.put("ms2z", reduceNumberToString(event.values[2]));
|
||||
alterCurrentData(map);
|
||||
private boolean crashed = false;
|
||||
private Timer timer = new Timer();
|
||||
private double[] St = new double[3];
|
||||
|
||||
@Override
|
||||
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
|
||||
|
||||
if(context.detectCrash) {
|
||||
float ax = event.values[0];
|
||||
float ay = event.values[1];
|
||||
float az = event.values[2];
|
||||
double amag = Math.sqrt(ax*ax+ay*ay+az*az);
|
||||
|
||||
if(amag > CRASHMAGNITUDE) {
|
||||
context.phoneCrash(amag);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public final void onSensorChanged(SensorEvent event) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("ms2x", reduceNumberToString(event.values[0]));
|
||||
map.put("ms2y", reduceNumberToString(event.values[1]));
|
||||
map.put("ms2z", reduceNumberToString(event.values[2]));
|
||||
alterCurrentData(map);
|
||||
|
||||
if(context.detectCrash) {
|
||||
if(St.length == 0) {
|
||||
St[0] = event.values[0];
|
||||
St[1] = event.values[1];
|
||||
St[2] = event.values[2];
|
||||
}
|
||||
|
||||
St[0] = 0.6 * event.values[0] + 0.4 * St[0];
|
||||
St[1] = 0.6 * event.values[1] + 0.4 * St[1];
|
||||
St[2] = 0.6 * event.values[2] + 0.4 * St[2];
|
||||
|
||||
double amag = Math.sqrt(St[0]*St[0] + St[1]*St[1] + St[2]*St[2]);
|
||||
|
||||
if(amag > CRASHMAGNITUDE && !crashed) {
|
||||
crashed = true;
|
||||
context.phoneCrash(amag);
|
||||
timer.schedule(
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
crashed = false;
|
||||
}
|
||||
},
|
||||
180000
|
||||
); //in three min reset
|
||||
|
||||
if(context.currentValues.containsKey("KPH")) {
|
||||
timer.schedule(
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
//if we are traveling less then 1km/h at 5 seconds after crash detection
|
||||
// confirm the crash
|
||||
if(1.0 > Double.parseDouble(context.currentValues.get("KPH"))) {
|
||||
context.phoneCrashConfirm();
|
||||
}
|
||||
}
|
||||
},
|
||||
5000
|
||||
); //in five sec reset
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
mSensorManager.registerListener(accelListner, mAccel, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
}
|
||||
if(mPress != null) {
|
||||
|
||||
Reference in New Issue
Block a user