Clean up, Refactor and Comment
This commit is contained in:
Chet Henry
2014-11-20 15:23:33 -07:00
parent baa4dfc9e9
commit a222df1a40
8 changed files with 366 additions and 312 deletions

View File

@@ -19,6 +19,7 @@ import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult;
import com.dsi.ant.plugins.antplus.pccbase.MultiDeviceSearch;
import com.dsi.ant.plugins.antplus.pccbase.MultiDeviceSearch.MultiDeviceSearchResult;
import com.ridelogger.R;
import com.ridelogger.listners.Base;
import com.ridelogger.listners.Gps;
import com.ridelogger.listners.HeartRate;
import com.ridelogger.listners.Power;
@@ -35,145 +36,84 @@ import android.os.Environment;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
/**
* RideService
* @author Chet Henry
* Performs ride logging from sensors as an android service
*/
public class RideService extends Service
{
public static BufferedWriter buf;
public static long start_time;
public static Map<String, String> current_values;
public boolean rideStarted = false;
public static BufferedWriter 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 HeartRate hr;
public static Power w;
public static Gps gps;
public static Sensors sensors;
public static Map<String, Base<?>> sensors; //All other Android sensor class
MultiDeviceSearch mSearch;
MultiDeviceSearch.SearchCallbacks mCallback;
MultiDeviceSearch.RssiCallback mRssiCallback;
MultiDeviceSearch mSearch; //Ant+ device search class to init connections
MultiDeviceSearch.SearchCallbacks mCallback; //Ant+ device class to setup sensors after they are found
MultiDeviceSearch.RssiCallback mRssiCallback; //Ant+ class to do something with the signal strength on device find
public int notifyID = 1;
NotificationManager mNotificationManager;
public int notifyID = 1; //Id of the notification in the top android bar that this class creates and alters
NotificationManager mNotificationManager; //Manager class to setup and alter our notification
public String file_name = "";
SharedPreferences settings;
public String fileName = ""; //File where the ride will go
SharedPreferences settings; //Object to load our setting from android's storage
public Boolean snoop = false; //should we log others ant+ devices
/**
*
* @return BufferedWriter
* starts the ride on service start
*/
public static BufferedWriter getBuf() {
return buf;
}
/**
*
* @return start_time
*/
public static long getStartTime() {
return start_time;
}
/**
*
* @return start_time
*/
public static Map<String, String> getCurrentValues() {
return current_values;
}
/**
*
* @return w
*/
public static Power getPower() {
return w;
}
/**
*
* @return w
*/
public static void setPower(Power pw) {
w = pw;
if(w == null) {
w = pw;
}
}
/**
*
* @return hr
*/
public static HeartRate getHeartRate() {
return hr;
}
public static void setHeartRate(HeartRate phr) {
if(hr == null) {
hr = phr;
}
}
public static void setGps(Gps pgps) {
if(gps == null) {
gps = pgps;
}
}
public static Gps getGps() {
return gps;
}
public static void setSensors(Sensors psens) {
if(sensors == null) {
sensors = psens;
}
}
public static Sensors getSensors() {
return sensors;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startRide();
return Service.START_NOT_STICKY;
}
/**
* sets android service settings
*/
@Override
public IBinder onBind(Intent arg0) {
return null;
}
/**
* sets android service settings
*/
@Override
public boolean onUnbind (Intent intent) {
return true;
}
/**
* stop the ride on service stop
*/
@Override
public void onDestroy() {
stopRide();
super.onDestroy();
}
/**
* start a ride if there is not one started yet
*/
protected void startRide() {
if(rideStarted) return;
start_time = System.currentTimeMillis();
file_name = "ride-" + start_time + ".json";
current_values = new HashMap<String, String>();
startTime = System.currentTimeMillis();
fileName = "ride-" + startTime + ".json";
currentValues = new HashMap<String, String>();
SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
String utc = f.format(new Date(start_time));
String utc = f.format(new Date(startTime));
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(start_time);
cal.setTimeInMillis(startTime);
String month = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US);
String week_day = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
@@ -183,7 +123,7 @@ public class RideService extends Service
final Set<String> pairedAnts = settings.getStringSet(StartActivity.PAIRED_ANTS, null);
current_values.put("SECS", "0.0");
currentValues.put("SECS", "0.0");
String rideHeadder = "{" +
"\"RIDE\":{" +
@@ -221,7 +161,7 @@ public class RideService extends Service
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS
) + "/Rides",
"ride-" + start_time + ".json"
"ride-" + startTime + ".json"
);
try {
@@ -230,19 +170,20 @@ public class RideService extends Service
buf = new BufferedWriter(new FileWriter(file, true));
buf.write(rideHeadder);
if(gps == null) {
gps = new Gps(this);
}
sensors = new HashMap<String, Base<?>>();
if(sensors == null) {
sensors = new Sensors(this);
}
sensors.put("GPS", new Gps(this));
sensors.put("AndroidSensors", new Sensors(this));
mCallback = new MultiDeviceSearch.SearchCallbacks(){
public void onDeviceFound(final MultiDeviceSearchResult deviceFound)
{
if (!deviceFound.isAlreadyConnected() && (pairedAnts == null || pairedAnts.contains(Integer.toString(deviceFound.getAntDeviceNumber())))) {
launchConnection(deviceFound);
if (!deviceFound.isAlreadyConnected()) {
if(pairedAnts == null || pairedAnts.contains(Integer.toString(deviceFound.getAntDeviceNumber()))) {
launchConnection(deviceFound, false);
} else if (snoop) {
launchConnection(deviceFound, true);
}
}
}
@@ -267,7 +208,7 @@ public class RideService extends Service
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Ride On")
.setContentText("Building ride: " + file_name + " Click to stop ride.");
.setContentText("Building ride: " + fileName + " Click to stop ride.");
mBuilder.setProgress(0, 0, true);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, StartActivity.class);
@@ -291,25 +232,17 @@ public class RideService extends Service
}
//stop the ride and clean up resources
protected void stopRide() {
if(!rideStarted) return;
if(w != null) {
w.onDestroy();
}
if(hr != null) {
hr.onDestroy();
for (Map.Entry<String, Base<?>> entry : sensors.entrySet())
{
entry.getValue().onDestroy();
}
if(gps != null) {
gps.onDestroy();
}
if(sensors != null) {
sensors.onDestroy();
}
//stop the Ant+ search
mSearch.close();
try {
@@ -322,16 +255,21 @@ public class RideService extends Service
}
public void launchConnection(MultiDeviceSearchResult result)
//remove snooped sensors if they are not longer in range
public void releaseSnoopedSensor(Base<?> sensor) {
sensors.remove(sensor);
}
//launch ant+ connection
public void launchConnection(MultiDeviceSearchResult result, Boolean snoop)
{
switch (result.getAntDeviceType())
{
case BIKE_CADENCE:
break;
case BIKE_POWER:
if(w == null) {
w = new Power(result, this);
}
sensors.put(String.valueOf(result.getAntDeviceNumber()), new Power(result, this, snoop));
break;
case BIKE_SPD:
break;
@@ -344,9 +282,7 @@ public class RideService extends Service
case WEIGHT_SCALE:
break;
case HEARTRATE:
if(hr == null) {
hr = new HeartRate(result, this);
}
sensors.put(String.valueOf(result.getAntDeviceNumber()), new HeartRate(result, this, snoop));
break;
case STRIDE_SDM:
break;

View File

@@ -54,7 +54,6 @@ public class StartActivity extends FragmentActivity
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

View File

@@ -0,0 +1,85 @@
package com.ridelogger.listners;
import java.util.Map;
import com.dsi.ant.plugins.antplus.pcc.defines.DeviceState;
import com.dsi.ant.plugins.antplus.pccbase.PccReleaseHandle;
import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc.IDeviceStateChangeReceiver;
import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc.IPluginAccessResultReceiver;
import com.dsi.ant.plugins.antplus.pccbase.MultiDeviceSearch.MultiDeviceSearchResult;
import com.ridelogger.RideService;
/**
* Ant
* @author Chet Henry
* Listen to and log Ant+ events base class
*/
public class Ant extends Base<Object>
{
public PccReleaseHandle<?> releaseHandle; //Handle class
public IPluginAccessResultReceiver<?> mResultReceiver; //Receiver class
public Boolean snooped = false; //should we snoop others connections?
public String prefix = ""; //prefix log messages with this
public Ant that = null;
//setup listeners and logging
public Ant(MultiDeviceSearchResult result, RideService mContext) {
super(mContext);
}
public Ant(MultiDeviceSearchResult result, RideService mContext, Boolean psnoop) {
super(mContext);
that = this;
if(psnoop) {
snooped = true;
prefix = "SNOOPED-";
}
}
IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new IDeviceStateChangeReceiver()
{
@Override
public void onDeviceStateChange(final DeviceState newDeviceState){
//if we lose a device zero out its values
if(newDeviceState.equals(DeviceState.DEAD)) {
zeroReadings();
if(snooped) {
releaseHandle.close(); // release ourselves if snooped
context.releaseSnoopedSensor(that);
}
}
}
};
@Override
public void onDestroy()
{
if(releaseHandle != null) {
releaseHandle.close();
}
}
@Override
public void alterCurrentData(String key, String value)
{
super.alterCurrentData(prefix + key, value);
}
@Override
public void alterCurrentData(Map<String, String> map)
{
for (Map.Entry<String, String> entry : map.entrySet())
{
map.remove(entry);
map.put(prefix + entry.getKey(), entry.getValue());
}
super.alterCurrentData(map);
}
public void zeroReadings() {}
}

View File

@@ -1,58 +1,45 @@
package com.ridelogger.listners;
import android.content.Context;
import com.dsi.ant.plugins.antplus.pcc.defines.DeviceState;
import com.dsi.ant.plugins.antplus.pccbase.PccReleaseHandle;
import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc.IDeviceStateChangeReceiver;
import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc.IPluginAccessResultReceiver;
import com.dsi.ant.plugins.antplus.pccbase.MultiDeviceSearch.MultiDeviceSearchResult;
import com.ridelogger.RideService;
import java.io.BufferedWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* Base class to connects to Heart Rate Plugin and display all the event data.
* Base
* @author Chet Henry
* Base sensor class that has methods to time stamp are write to buffer
*/
public class Base
public class Base<T>
{
public static BufferedWriter buf;
public static long start_time;
public static Map<String, String> current_values;
public PccReleaseHandle<?> releaseHandle;
public Context context;
public static long startTime;
public static Map<String, String> currentValues;
public Base(MultiDeviceSearchResult result, Context mContext) {
public RideService context;
public Base(RideService mContext) {
init(mContext);
}
public Base(Context mContext) {
init(mContext);
//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;
}
public void init(Context mContext) {
buf = RideService.getBuf();
start_time = RideService.getStartTime();
current_values = RideService.getCurrentValues();
context = mContext;
}
IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new IDeviceStateChangeReceiver() {
@Override
public void onDeviceStateChange(final DeviceState newDeviceState){}
};
public IPluginAccessResultReceiver<?> mResultReceiver;
public void writeData(String key, String value)
{
if(!current_values.containsKey(key) || current_values.get(key) != value) {
String ts = String.valueOf((double) (System.currentTimeMillis() - start_time) / 1000.0);
current_values.put("SECS", ts);
current_values.put(key, value);
if(!currentValues.containsKey(key) || currentValues.get(key) != value) {
String ts = getTs();
currentValues.put("SECS", ts);
currentValues.put(key, value);
try {
synchronized (buf) {
@@ -77,8 +64,8 @@ public class Base
public void writeData(Map<String, String> map)
{
String ts = String.valueOf((double) (System.currentTimeMillis() - start_time) / 1000.0);
current_values.put("SECS", ts);
String ts = getTs();
currentValues.put("SECS", ts);
try {
synchronized (buf) {
@@ -99,7 +86,7 @@ public class Base
buf.write("\":");
buf.write(value);
current_values.put(key, value);
currentValues.put(key, value);
}
buf.write("}");
@@ -110,9 +97,10 @@ public class Base
public void alterCurrentData(String key, String value)
{
synchronized (current_values) {
current_values.put("SECS", getTs());
current_values.put(key, value);
synchronized (currentValues) {
currentValues.put("SECS", getTs());
currentValues.put(key, value);
writeCurrentData();
}
}
@@ -120,13 +108,15 @@ public class Base
public void alterCurrentData(Map<String, String> map)
{
synchronized (current_values) {
current_values.put("SECS", getTs());
synchronized (currentValues) {
currentValues.put("SECS", getTs());
for (Map.Entry<String, String> entry : map.entrySet())
{
current_values.put(entry.getKey(), entry.getValue());
currentValues.put(entry.getKey(), entry.getValue());
}
writeCurrentData();
}
}
@@ -137,8 +127,17 @@ public class Base
synchronized (buf) {
buf.write(",{");
for (Map.Entry<String, String> entry : current_values.entrySet())
{
Iterator<Entry<String, String>> it = currentValues.entrySet().iterator();
if(it.hasNext()) {
buf.write("\"");
Entry<String, String> entry = it.next();
buf.write(entry.getKey());
buf.write("\":");
buf.write(entry.getValue());
}
while (it.hasNext()) {
Entry<String, String> entry = it.next();
buf.write(",\"");
buf.write(entry.getKey());
buf.write("\":");
@@ -152,7 +151,7 @@ public class Base
public String getTs() {
return reduceNumberToString((double) (System.currentTimeMillis() - start_time) / 1000.0);
return reduceNumberToString((double) (System.currentTimeMillis() - RideService.startTime) / 1000.0);
}
@@ -185,13 +184,8 @@ public class Base
}
}
public void onDestroy()
{
if(releaseHandle != null) {
releaseHandle.close();
}
}
//Clean up my listners here
public void onDestroy() {}
}

View File

@@ -3,21 +3,28 @@ package com.ridelogger.listners;
import java.util.HashMap;
import java.util.Map;
import com.ridelogger.RideService;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class Gps extends Base
/**
* Gps
* @author henry
* Listen and log gps events
*/
public class Gps extends Base<Gps>
{
public Gps(Context mContext)
public Gps(RideService mContext)
{
super(mContext);
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//listen to gps events and log them
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Map<String, String> map = new HashMap<String, String>();

View File

@@ -1,7 +1,5 @@
package com.ridelogger.listners;
import android.content.Context;
import com.dsi.ant.plugins.antplus.pcc.AntPlusHeartRatePcc;
import com.dsi.ant.plugins.antplus.pcc.AntPlusHeartRatePcc.DataState;
import com.dsi.ant.plugins.antplus.pcc.AntPlusHeartRatePcc.IHeartRateDataReceiver;
@@ -10,20 +8,29 @@ import com.dsi.ant.plugins.antplus.pcc.defines.EventFlag;
import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult;
import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc.IPluginAccessResultReceiver;
import com.dsi.ant.plugins.antplus.pccbase.MultiDeviceSearch.MultiDeviceSearchResult;
import com.ridelogger.RideService;
import java.math.BigDecimal;
import java.util.EnumSet;
/**
* Base class to connects to Heart Rate Plugin and display all the event data.
* HeartRate
* @author Chet Henry
* Listen to and log Ant+ HearRate events
*/
public class HeartRate extends Base
public class HeartRate extends Ant
{
public HeartRate(MultiDeviceSearchResult result, Context mContext) {
public HeartRate(MultiDeviceSearchResult result, RideService mContext) {
super(result, mContext);
releaseHandle = AntPlusHeartRatePcc.requestAccess(context, result.getAntDeviceNumber(), 0, mResultReceiver, mDeviceStateChangeReceiver);
}
public HeartRate(MultiDeviceSearchResult result, RideService mContext, Boolean psnoop) {
super(result, mContext, psnoop);
releaseHandle = AntPlusHeartRatePcc.requestAccess(context, result.getAntDeviceNumber(), 0, mResultReceiver, mDeviceStateChangeReceiver);
}
public IPluginAccessResultReceiver<AntPlusHeartRatePcc> mResultReceiver = new IPluginAccessResultReceiver<AntPlusHeartRatePcc>() {
//Handle the result, connecting to events on success or reporting failure to user.
@Override
@@ -41,4 +48,11 @@ public class HeartRate extends Base
}
}
};
@Override
public void zeroReadings()
{
alterCurrentData("HR", "0");
}
}

View File

@@ -1,7 +1,5 @@
package com.ridelogger.listners;
import android.content.Context;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.CalculatedWheelDistanceReceiver;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.CalculatedWheelSpeedReceiver;
@@ -19,35 +17,36 @@ import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.ITorqueEffectivenessR
import com.dsi.ant.plugins.antplus.pcc.defines.DeviceState;
import com.dsi.ant.plugins.antplus.pcc.defines.EventFlag;
import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult;
import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc.IDeviceStateChangeReceiver;
import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc.IPluginAccessResultReceiver;
import com.dsi.ant.plugins.antplus.pccbase.MultiDeviceSearch.MultiDeviceSearchResult;
import com.ridelogger.RideService;
import java.math.BigDecimal;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
/**
* Base class to connects to Heart Rate Plugin and display all the event data.
* Power
* @author Chet Henry
* Listen to and log Ant+ Power events
*/
public class Power extends Base
public class Power extends Ant
{
public BigDecimal wheelCircumferenceInMeters = new BigDecimal("2.07"); //size of wheel to calculate speed
public Power(MultiDeviceSearchResult result, Context mContext) {
//setup listeners and logging
public Power(MultiDeviceSearchResult result, RideService mContext) {
super(result, mContext);
releaseHandle = AntPlusBikePowerPcc.requestAccess(context, result.getAntDeviceNumber(), 0, mResultReceiver, mDeviceStateChangeReceiver);
}
BigDecimal wheelCircumferenceInMeters = new BigDecimal("2.07");
IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new IDeviceStateChangeReceiver()
{
@Override
public void onDeviceStateChange(final DeviceState newDeviceState){}
};
public Power(MultiDeviceSearchResult result, RideService mContext, Boolean psnoop) {
super(result, mContext, psnoop);
releaseHandle = AntPlusBikePowerPcc.requestAccess(context, result.getAntDeviceNumber(), 0, mResultReceiver, mDeviceStateChangeReceiver);
}
//Handle messages
protected IPluginAccessResultReceiver<AntPlusBikePowerPcc> mResultReceiver = new IPluginAccessResultReceiver<AntPlusBikePowerPcc>() {
//Handle the result, connecting to events on success or reporting failure to user.
@Override
@@ -75,7 +74,7 @@ public class Power extends Base
new ICalculatedCrankCadenceReceiver() {
@Override
public void onNewCalculatedCrankCadence(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedCrankCadence) {
alterCurrentData("RPM", reduceNumberToString(calculatedCrankCadence));
alterCurrentData("CAD", reduceNumberToString(calculatedCrankCadence));
}
}
);
@@ -85,7 +84,7 @@ public class Power extends Base
@Override
public void onNewCalculatedWheelSpeed(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedWheelSpeed)
{
alterCurrentData("KMH", reduceNumberToString(calculatedWheelSpeed));
alterCurrentData("KPH", reduceNumberToString(calculatedWheelSpeed));
}
}
);
@@ -105,7 +104,7 @@ public class Power extends Base
@Override
public void onNewInstantaneousCadence(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final int instantaneousCadence)
{
alterCurrentData("RPM", reduceNumberToString(instantaneousCadence));
alterCurrentData("CAD", reduceNumberToString(instantaneousCadence));
}
}
);
@@ -125,7 +124,7 @@ public class Power extends Base
@Override
public void onNewPedalPowerBalance(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final boolean rightPedalIndicator, final int pedalPowerPercentage)
{
alterCurrentData("LTE", reduceNumberToString(pedalPowerPercentage));
//alterCurrentData("LTE", reduceNumberToString(pedalPowerPercentage));
}
}
);
@@ -155,10 +154,10 @@ public class Power extends Base
@Override
public void onNewTorqueEffectiveness(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long powerOnlyUpdateEventCount, final BigDecimal leftTorqueEffectiveness, final BigDecimal rightTorqueEffectiveness)
{
Map<String, String> map = new HashMap<String, String>();
map.put("LTE", reduceNumberToString(leftTorqueEffectiveness));
map.put("RTE", reduceNumberToString(rightTorqueEffectiveness));
alterCurrentData(map);
//Map<String, String> map = new HashMap<String, String>();
//map.put("LTE", reduceNumberToString(leftTorqueEffectiveness));
//map.put("RTE", reduceNumberToString(rightTorqueEffectiveness));
//alterCurrentData(map);
}
}
@@ -168,16 +167,29 @@ public class Power extends Base
@Override
public void onNewPedalSmoothness(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long powerOnlyUpdateEventCount, final boolean separatePedalSmoothnessSupport, final BigDecimal leftOrCombinedPedalSmoothness, final BigDecimal rightPedalSmoothness)
{
Map<String, String> map = new HashMap<String, String>();
map.put("SNPLC", reduceNumberToString(leftOrCombinedPedalSmoothness));
map.put("SNPR", reduceNumberToString(rightPedalSmoothness));
alterCurrentData(map);
//Map<String, String> map = new HashMap<String, String>();
//map.put("SNPLC", reduceNumberToString(leftOrCombinedPedalSmoothness));
//map.put("SNPR", reduceNumberToString(rightPedalSmoothness));
//alterCurrentData(map);
}
}
);
}
}
};
@Override
public void zeroReadings()
{
Map<String, String> map = new HashMap<String, String>();
map.put("WATTS", "0");
map.put("NM", "0");
map.put("CAD", "0");
map.put("KPH", "0");
map.put("KM", "0");
alterCurrentData(map);
}
}

View File

@@ -3,13 +3,21 @@ package com.ridelogger.listners;
import java.util.HashMap;
import java.util.Map;
import com.ridelogger.RideService;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class Sensors extends Base
/**
* Sensors
* @author Chet Henry
* Listen to android sensor events and log them
*/
public class Sensors extends Base<Object>
{
private SensorManager mSensorManager;
@@ -25,7 +33,7 @@ public class Sensors extends Base
private SensorEventListener tempListner;
private SensorEventListener fieldListner;
public Sensors(Context mContext)
public Sensors(RideService mContext)
{
super(mContext);
@@ -37,105 +45,104 @@ public class Sensors extends Base
mTemp = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
mField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
if(mLight != null) {
luxListner = new SensorEventListener() {
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
alterCurrentData("lux", reduceNumberToString(event.values[0]));
}
};
if(mLight != null) {
luxListner = new SensorEventListener() {
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
mSensorManager.registerListener(luxListner, mLight, 3000000);
}
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);
}
};
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
alterCurrentData("lux", reduceNumberToString(event.values[0]));
}
};
mSensorManager.registerListener(luxListner, mLight, 3000000);
}
if(mAccel != null) {
accelListner = new SensorEventListener() {
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
mSensorManager.registerListener(accelListner, mAccel, SensorManager.SENSOR_DELAY_NORMAL);
}
if(mPress != null) {
pressListner = new SensorEventListener() {
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
alterCurrentData("press", reduceNumberToString(event.values[0]));
}
};
@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);
}
};
mSensorManager.registerListener(accelListner, mAccel, SensorManager.SENSOR_DELAY_NORMAL);
}
if(mPress != null) {
pressListner = new SensorEventListener() {
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
mSensorManager.registerListener(pressListner, mPress, 3000000);
}
if(mTemp != null) {
tempListner = new SensorEventListener() {
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
alterCurrentData("temp", reduceNumberToString(event.values[0]));
}
};
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
alterCurrentData("press", reduceNumberToString(event.values[0]));
}
};
mSensorManager.registerListener(pressListner, mPress, 3000000);
}
if(mTemp != null) {
tempListner = new SensorEventListener() {
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
mSensorManager.registerListener(tempListner, mTemp, 3000000);
}
if(mField != null) {
fieldListner = 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("uTx", reduceNumberToString(event.values[0]));
map.put("uTy", reduceNumberToString(event.values[1]));
map.put("uTz", reduceNumberToString(event.values[2]));
alterCurrentData(map);
}
};
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
alterCurrentData("temp", reduceNumberToString(event.values[0]));
}
};
mSensorManager.registerListener(tempListner, mTemp, 3000000);
}
if(mField != null) {
fieldListner = new SensorEventListener() {
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
mSensorManager.registerListener(fieldListner, mField, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public final void onSensorChanged(SensorEvent event) {
Map<String, String> map = new HashMap<String, String>();
map.put("uTx", reduceNumberToString(event.values[0]));
map.put("uTy", reduceNumberToString(event.values[1]));
map.put("uTz", reduceNumberToString(event.values[2]));
alterCurrentData(map);
}
};
mSensorManager.registerListener(fieldListner, mField, SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
public void onDestroy()
{
if(luxListner != null) {
mSensorManager.unregisterListener(luxListner);
}
if(accelListner != null) {
mSensorManager.unregisterListener(accelListner);
}
if(pressListner != null) {
mSensorManager.unregisterListener(pressListner);
}
if(tempListner != null) {
mSensorManager.unregisterListener(tempListner);
}
if(fieldListner != null) {
mSensorManager.unregisterListener(fieldListner);
}
if(accelListner != null) {
mSensorManager.unregisterListener(accelListner);
}
if(pressListner != null) {
mSensorManager.unregisterListener(pressListner);
}
if(tempListner != null) {
mSensorManager.unregisterListener(tempListner);
}
if(fieldListner != null) {
mSensorManager.unregisterListener(fieldListner);
}
}
}