Reduce memory footprint and improve service performance

This commit is contained in:
Chet Henry
2014-11-29 23:00:27 -07:00
parent f37d9e8516
commit 1433c6d1a4
10 changed files with 351 additions and 265 deletions

View File

@@ -1,8 +1,5 @@
package com.ridelogger;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.View;
@@ -13,16 +10,25 @@ import android.widget.TextView;
public class CurrentValuesAdapter extends BaseAdapter {
public StartActivity context;
public LinkedHashMap<String, TextView> tvs = new LinkedHashMap<String, TextView>();
public TextView[] tvs = new TextView[RideService.TOTALSENSORS];
public CurrentValuesAdapter(StartActivity c) {
context = c;
for (int i = 0; i < RideService.TOTALSENSORS; i++) {
tvs[i] = new TextView(context);
tvs[i].setTextAppearance(context, android.R.attr.textAppearanceLarge);
tvs[i].setTypeface(null, Typeface.BOLD);
tvs[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
tvs[i].setText(String.format("%.2f", (float) 0.0) + " " + RideService.KEYS[i].toString().toLowerCase());
}
}
@Override
public int getCount() {
return tvs.size();
return tvs.length;
}
@@ -40,33 +46,27 @@ public class CurrentValuesAdapter extends BaseAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) { // if it's not recycled, initialize some attributes
return (TextView) tvs.values().toArray()[position];
if (convertView == null) {
return (TextView) tvs[position];
} else {
return (TextView) convertView;
}
}
public void update(LinkedHashMap<String, String> currentValues) {
for (Entry<String, String> entry : currentValues.entrySet()) {
String key = entry.getKey();
String value = String.format("%.2f", Float.valueOf(entry.getValue())) + " " + key.toLowerCase();
if(tvs.containsKey(key)) {
tvs.get(key).setText(value);
public void update(float[] values) {
for (int i = 0; i < values.length; i++) {
if(tvs[i] != null) {
tvs[i].setText(String.format("%.2f", values[i]) + " " + RideService.KEYS[i].toString().toLowerCase());
} else {
TextView tv = new TextView(context);
tv.setTextAppearance(context, android.R.attr.textAppearanceLarge);
tv.setTypeface(null, Typeface.BOLD);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
tv.setText(value);
tvs.put(key, tv);
tvs[i] = new TextView(context);
tvs[i].setTextAppearance(context, android.R.attr.textAppearanceLarge);
tvs[i].setTypeface(null, Typeface.BOLD);
tvs[i].setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
tvs[i].setText(String.format("%.2f", values[i]) + " " + RideService.KEYS[i].toString().toLowerCase());
}
}
this.notifyDataSetChanged();
}
}

View File

@@ -5,7 +5,6 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@@ -53,27 +52,84 @@ import android.telephony.SmsManager;
* Performs ride logging from sensors as an android service
*/
public class RideService extends Service
{
public GzipWriter buf; //writes to log file buffered
public long startTime; //start time of the ride
public LinkedHashMap<String, String> currentValues; //hash of current values
public boolean rideStarted = false; //have we started logging the ride
{
public static final int notifyID = 1; //Id of the notification in the top android bar that this class creates and alters
public static final int TOTALSENSORS = 25;
public static final int SECS = 0;
public static final int KPH = 1;
public static final int ALTITUDE = 2;
public static final int bearing = 3;
public static final int gpsa = 4;
public static final int LAT = 5;
public static final int LON = 6;
public static final int HR = 7;
public static final int WATTS = 8;
public static final int NM = 9;
public static final int CAD = 10;
public static final int KM = 11;
public static final int LTE = 12;
public static final int RTE = 13;
public static final int SNPLC = 14;
public static final int SNPR = 15;
public static final int ms2x = 16;
public static final int ms2y = 17;
public static final int ms2z = 18;
public static final int temp = 19;
public static final int uTx = 20;
public static final int uTy = 21;
public static final int uTz = 22;
public static final int press = 23;
public static final int lux = 24;
public static CharSequence[] KEYS = {
"SECS",
"KPH",
"ALTITUDE",
"bearing",
"gpsa",
"LAT",
"LON",
"HR",
"WATTS",
"NM",
"CAD",
"KM",
"LTE",
"RTE",
"SNPLC",
"SNPR",
"ms2x",
"ms2y",
"ms2z",
"temp",
"uTx",
"uTy",
"uTz",
"press",
"lux"
};
public float[] currentValues = new float[RideService.TOTALSENSORS]; //float array of current values
public float[] snoopedValues = new float[RideService.TOTALSENSORS]; //float array of snooped values
public GzipWriter buf; //writes to log file buffered
public long startTime; //start time of the ride
public boolean rideStarted = false; //have we started logging the ride
public Boolean snoop = false; //should we log others ant+ devices
private Set<String> pairedAnts; //list of ant devices to pair with
private Timer timer; //timer class to control the periodic messages
private Timer timerUI; //timer class to control the periodic messages
private String emergencyNumbuer; //the number to send the messages to
public LinkedHashMap<String, Base<?>> sensors = new LinkedHashMap<String, Base<?>>();
public LinkedHashMap<String, Base<?>> sensors = new LinkedHashMap<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
MultiDeviceSearch.RssiCallback mRssiCallback; //Ant+ class to do something with the signal strength on device find
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; //Id of the notification in the top android bar that this class creates and alters
public String fileName = ""; //File where the ride will go
public 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 Timer timer; //timer class to control the periodic messages
private Timer timerUI; //timer class to control the periodic messages
public String emergencyNumbuer; //the number to send the messages to
final Messenger mMessenger = new Messenger(new IncomingHandler()); // Target we publish for clients to send messages to IncomingHandler.
Messenger replyTo;
@@ -94,7 +150,7 @@ public class RideService extends Service
public void run() {
Message msg = Message.obtain(null, 2, 0, 0);
Bundle bundle = new Bundle();
bundle.putSerializable("currentValues", (Serializable) currentValues);
bundle.putSerializable("currentValues", currentValues);
msg.setData(bundle);
try {
replyTo.send(msg);
@@ -119,7 +175,7 @@ public class RideService extends Service
/**
* sets android service settings
* returns the messenger to talk to the app with
*/
@Override
public IBinder onBind(Intent arg0) {
@@ -128,7 +184,7 @@ public class RideService extends Service
/**
* sets android service settings
* releases the timer that sends messages to the app
*/
@Override
public boolean onUnbind (Intent intent) {
@@ -156,8 +212,7 @@ public class RideService extends Service
if(rideStarted) return;
startTime = System.currentTimeMillis();
fileName = "ride-" + startTime + ".json.gzip";
currentValues = new LinkedHashMap<String, String>();
final String fileName = "ride-" + startTime + ".json.gzip";
SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
@@ -171,11 +226,11 @@ public class RideService extends Service
String weekDay = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
String year = Integer.toString(cal.get(Calendar.YEAR));
settings = PreferenceManager.getDefaultSharedPreferences(this);
emergencyNumbuer = settings.getString(getString(R.string.PREF_EMERGENCY_NUMBER), "");
pairedAnts = settings.getStringSet(getString(R.string.PREF_PAIRED_ANTS), null);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
emergencyNumbuer = settings.getString(getString(R.string.PREF_EMERGENCY_NUMBER), "");
pairedAnts = settings.getStringSet(getString(R.string.PREF_PAIRED_ANTS), null);
currentValues.put("SECS", "0.0");
currentValues[SECS] = (float) 0.0;
String rideHeadder = "{" +
"\"RIDE\":{" +
@@ -301,15 +356,10 @@ public class RideService extends Service
* let a love one know where you are at about every 10 min
*/
public void phoneCrash(double mag) {
String body = getString(R.string.crash_warning) + "\n";
if(currentValues.containsKey("LAT") && currentValues.containsKey("LON")) {
body = body + "https://www.google.com/maps/place/" + currentValues.get("LAT") + "," + currentValues.get("LON");
} else {
body = body + getString(R.string.crash_unknow_location);
}
body = body + "\n " + getString(R.string.crash_magnitude) + ": " + String.valueOf(mag);
smsHome(body);
smsHome(
getString(R.string.crash_warning) + "\n" + getLocationLink()
+ "\n " + getString(R.string.crash_magnitude) + ": " + String.valueOf(mag)
);
}
@@ -317,14 +367,7 @@ public class RideService extends Service
* confirm the crash if we are not moving
*/
public void phoneCrashConfirm() {
String body = getString(R.string.crash_confirm) + "!\n";
if(currentValues.containsKey("LAT") && currentValues.containsKey("LON")) {
body = body + "https://www.google.com/maps/place/" + currentValues.get("LAT") + "," + currentValues.get("LON");
} else {
body = body + getString(R.string.crash_unknow_location);
}
smsHome(body);
smsHome(getString(R.string.crash_confirm) + "!\n" + getLocationLink());
}
@@ -347,12 +390,8 @@ public class RideService extends Service
/**
* 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");
}
smsHome(body);
public void smsWithLocation(String body) {
smsHome(body + "\n " + getLocationLink());
}
@@ -369,7 +408,16 @@ public class RideService extends Service
*/
public void smsHome(String body) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(emergencyNumbuer, null, body, null, null);
if(emergencyNumbuer != null) {
smsManager.sendTextMessage(emergencyNumbuer, null, body, null, null);
}
}
public String getLocationLink() {
if(currentValues[LAT] != 0.0 || currentValues[LON] != 0.0) {
return "https://www.google.com/maps/place/" + currentValues[LAT] + "," + currentValues[LON];
}
return getString(R.string.crash_unknow_location);
}

View File

@@ -1,6 +1,5 @@
package com.ridelogger;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
@@ -14,8 +13,6 @@ import com.dsi.ant.plugins.antplus.pccbase.MultiDeviceSearch.MultiDeviceSearchRe
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.preference.MultiSelectListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

View File

@@ -1,7 +1,4 @@
package com.ridelogger;
import java.util.LinkedHashMap;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.BroadcastReceiver;
@@ -38,6 +35,8 @@ public class StartActivity extends FragmentActivity
final Messenger mMessenger = new Messenger(new IncomingHandler());
private CurrentValuesAdapter currentValuesAdapter;
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
@@ -63,8 +62,6 @@ public class StartActivity extends FragmentActivity
}
};
private CurrentValuesAdapter currentValuesAdapter;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
@@ -73,8 +70,6 @@ public class StartActivity extends FragmentActivity
inflater.inflate(R.menu.start_activity, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
@@ -107,6 +102,7 @@ public class StartActivity extends FragmentActivity
layout.setAdapter(currentValuesAdapter);
}
@Override
protected void onResume() {
super.onResume();
@@ -170,8 +166,7 @@ public class StartActivity extends FragmentActivity
* @param bundle
*/
public void updateValues(Bundle bundle) {
@SuppressWarnings("unchecked")
LinkedHashMap<String, String> currentValues = (LinkedHashMap<String, String>) bundle.getSerializable("currentValues");
float[] currentValues = (float[]) bundle.getSerializable("currentValues");
currentValuesAdapter.update(currentValues);
}

View File

@@ -1,13 +1,11 @@
package com.ridelogger.listners;
import java.util.LinkedHashMap;
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;
@@ -21,20 +19,16 @@ 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) {
public Ant(MultiDeviceSearchResult result, RideService mContext, Boolean pSnoop) {
super(mContext);
that = this;
if(psnoop) {
snooped = true;
prefix = "SNOOPED-";
}
snooped = pSnoop;
}
@@ -62,43 +56,45 @@ public class Ant extends Base<Object>
}
@Override
public void writeData(String key, String value)
public void writeData(int key, float value)
{
super.writeData(prefix + key, value);
if(snooped) {
super.writeSnoopedData(key, value);
} else {
super.writeData(key, value);
}
}
@Override
public void writeData(LinkedHashMap<String, String> map)
public void writeData(int[] keys, float[] values)
{
if(prefix != "") {
for (Map.Entry<String, String> entry : map.entrySet()) {
map.remove(entry);
map.put(prefix + entry.getKey(), entry.getValue());
}
if(snooped) {
super.writeSnoopedData(keys, values);
} else {
super.writeData(keys, values);
}
super.writeData(map);
}
@Override
public void alterCurrentData(String key, String value)
{
super.alterCurrentData(prefix + key, value);
public void alterCurrentData(int key, float value)
{
if(snooped) {
super.alterSnoopedData(key, value);
} else {
super.alterCurrentData(key, value);
}
}
@Override
public void alterCurrentData(LinkedHashMap<String, String> map)
public void alterCurrentData(int[] keys, float[] values)
{
if(prefix != "") {
for (Map.Entry<String, String> entry : map.entrySet()) {
map.remove(entry);
map.put(prefix + entry.getKey(), entry.getValue());
}
if(snooped) {
super.alterSnoopedData(keys, values);
} else {
super.alterCurrentData(keys, values);
}
super.alterCurrentData(map);
}

View File

@@ -4,11 +4,6 @@ import com.ridelogger.GzipWriter;
import com.ridelogger.RideService;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* Base
@@ -17,11 +12,10 @@ import java.util.Map.Entry;
*/
public class Base<T>
{
public static GzipWriter buf;
public static long startTime;
public static LinkedHashMap<String, String> currentValues;
public GzipWriter buf;
public long startTime;
public RideService context;
public RideService context;
public Base(RideService mContext) {
init(mContext);
@@ -31,30 +25,28 @@ public class Base<T>
public void init(RideService mContext) {
context = mContext;
buf = context.buf; //shared file buffer object
currentValues = context.currentValues; //shared currentValues object
}
public void writeData(String key, String value)
public void writeData(int key, float value)
{
if(!currentValues.containsKey(key) || currentValues.get(key) != value) {
String ts = getTs();
currentValues.put("SECS", ts);
currentValues.put(key, value);
if(context.currentValues[key] != value) {
context.currentValues[RideService.SECS] = getTs();
context.currentValues[key] = value;
try {
synchronized (buf) {
buf.write(",{");
buf.write("\"");
buf.write("SECS");
buf.write((String) RideService.KEYS[RideService.SECS]);
buf.write("\":");
buf.write(ts);
buf.write(String.format("%f", context.currentValues[RideService.SECS]));
buf.write(",\"");
buf.write(key);
buf.write((String) RideService.KEYS[key]);
buf.write("\":");
buf.write(value);
buf.write(String.format("%f", value));
buf.write("}");
}
@@ -63,30 +55,27 @@ public class Base<T>
}
public void writeData(LinkedHashMap<String, String> map)
public void writeData(int[] keys, float[] values)
{
String ts = getTs();
currentValues.put("SECS", ts);
context.currentValues[RideService.SECS] = getTs();
try {
synchronized (buf) {
buf.write(",{");
buf.write("\"");
buf.write("SECS");
buf.write((String) RideService.KEYS[RideService.SECS]);
buf.write("\":");
buf.write(ts);
buf.write(String.format("%f", context.currentValues[RideService.SECS]));
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
int i = 0;
for (int key : keys) {
context.currentValues[key] = values[i];
buf.write(",\"");
buf.write(key);
buf.write((String) RideService.KEYS[key]);
buf.write("\":");
buf.write(value);
currentValues.put(key, value);
buf.write(String.format("%f", context.currentValues[i]));
i++;
}
buf.write("}");
@@ -95,24 +84,56 @@ public class Base<T>
}
public void alterCurrentData(String key, String value)
public void writeSnoopedData(int key, float value)
{
synchronized (currentValues) {
currentValues.put("SECS", getTs());
currentValues.put(key, value);
writeCurrentData();
if(context.currentValues[key] != value) {
context.snoopedValues[RideService.SECS] = getTs();
context.snoopedValues[key] = value;
}
}
public void alterCurrentData(LinkedHashMap<String, String> map)
public void writeSnoopedData(int[] keys, float[] values)
{
synchronized (currentValues) {
currentValues.put("SECS", getTs());
context.snoopedValues[RideService.SECS] = getTs();
synchronized (context.snoopedValues) {
int i = 0;
for (int key : keys) {
context.snoopedValues[key] = values[i];
i++;
}
}
}
public void alterCurrentData(int key, float value)
{
synchronized (context.currentValues) {
context.currentValues[RideService.SECS] = getTs();
context.currentValues[key] = value;
writeCurrentData();
}
}
public void alterSnoopedData(int key, float value)
{
synchronized (context.snoopedValues) {
context.snoopedValues[RideService.SECS] = getTs();
context.snoopedValues[key] = value;
}
}
public void alterCurrentData(int[] keys, float[] values)
{
synchronized (context.currentValues) {
context.currentValues[RideService.SECS] = getTs();
for (Map.Entry<String, String> entry : map.entrySet()) {
currentValues.put(entry.getKey(), entry.getValue());
int i = 0;
for (int key : keys) {
context.currentValues[key] = values[i];
i++;
}
writeCurrentData();
@@ -120,27 +141,35 @@ public class Base<T>
}
public void alterSnoopedData(int[] keys, float[] values)
{
synchronized (context.snoopedValues) {
context.snoopedValues[RideService.SECS] = getTs();
int i = 0;
for (int key : keys) {
context.snoopedValues[key] = values[i];
i++;
}
}
}
public void writeCurrentData()
{
try {
synchronized (buf) {
buf.write(",{");
buf.write("\"");
buf.write((String) RideService.KEYS[0]);
buf.write("\":");
buf.write(String.format("%f", context.currentValues[0]));
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();
for (int i = 1; i < context.currentValues.length; i++) {
buf.write(",\"");
buf.write(entry.getKey());
buf.write((String) RideService.KEYS[i]);
buf.write("\":");
buf.write(entry.getValue());
buf.write(String.format("%f", context.currentValues[i]));
}
buf.write("}");
@@ -148,36 +177,10 @@ public class Base<T>
} catch (IOException e) {}
}
//get current time stamp
public String getTs() {
return reduceNumberToString((double) (System.currentTimeMillis() - context.startTime) / 1000.0);
}
//reduce number data types to consistently formatted strings
public static String reduceNumberToString(double d)
{
if(d == (long) d)
return String.format("%d",(long)d);
else
return String.format("%f", d);
}
//reduce number data types to consistently formatted strings
public static String reduceNumberToString(float d)
{
if(d == (long) d)
return String.format("%d",(long)d);
else
return String.format("%f", d);
}
//reduce number data types to consistently formatted strings
public static String reduceNumberToString(BigDecimal d)
{
return String.format("%s", d.toPlainString());
public float getTs() {
return (float) ((System.currentTimeMillis() - context.startTime) / 1000.0);
}

View File

@@ -1,11 +1,12 @@
package com.ridelogger.listners;
import java.util.LinkedHashMap;
import com.ridelogger.RideService;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
/**
@@ -24,14 +25,25 @@ public class Gps extends Base<Gps>
//listen to gps events and log them
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("ALTITUDE", reduceNumberToString(location.getAltitude()) );
map.put("KPH", reduceNumberToString(location.getSpeed()) );
map.put("bearing", reduceNumberToString(location.getBearing()) );
map.put("gpsa", reduceNumberToString(location.getAccuracy()) );
map.put("LAT", reduceNumberToString(location.getLatitude()) );
map.put("LON", reduceNumberToString(location.getLongitude()));
alterCurrentData(map);
int[] keys = {
RideService.ALTITUDE,
RideService.KPH,
RideService.bearing,
RideService.gpsa,
RideService.LAT,
RideService.LON
};
float[] values = {
(float) location.getAltitude(),
location.getSpeed(),
location.getBearing(),
location.getAccuracy(),
(float) location.getLatitude(),
(float) location.getLongitude()
};
alterCurrentData(keys, values);
}
@Override

View File

@@ -41,7 +41,7 @@ public class HeartRate extends Ant
new IHeartRateDataReceiver() {
@Override
public void onNewHeartRateData(final long estTimestamp, EnumSet<EventFlag> eventFlags, final int computedHeartRate, final long heartBeatCount, final BigDecimal heartBeatEventTime, final DataState dataState) {
alterCurrentData("HR", reduceNumberToString(computedHeartRate));
alterCurrentData(RideService.HR, computedHeartRate);
}
}
);
@@ -53,6 +53,6 @@ public class HeartRate extends Ant
@Override
public void zeroReadings()
{
alterCurrentData("HR", "0");
alterCurrentData(RideService.HR, (float) 0.0);
}
}

View File

@@ -14,10 +14,11 @@ 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;
import java.util.LinkedHashMap;
/**
* Power
@@ -50,7 +51,7 @@ public class Power extends Ant
result.subscribeCalculatedPowerEvent(new ICalculatedPowerReceiver() {
@Override
public void onNewCalculatedPower(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedPower) {
alterCurrentData("WATTS", reduceNumberToString(calculatedPower));
alterCurrentData(RideService.WATTS, calculatedPower.floatValue());
}
}
);
@@ -59,7 +60,7 @@ public class Power extends Ant
new ICalculatedTorqueReceiver() {
@Override
public void onNewCalculatedTorque(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedTorque) {
alterCurrentData("NM", reduceNumberToString(calculatedTorque));
alterCurrentData(RideService.NM, calculatedTorque.floatValue());
}
}
);
@@ -68,7 +69,7 @@ public class Power extends Ant
new ICalculatedCrankCadenceReceiver() {
@Override
public void onNewCalculatedCrankCadence(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedCrankCadence) {
alterCurrentData("CAD", reduceNumberToString(calculatedCrankCadence));
alterCurrentData(RideService.CAD, calculatedCrankCadence.floatValue());
}
}
);
@@ -78,7 +79,7 @@ public class Power extends Ant
@Override
public void onNewCalculatedWheelSpeed(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedWheelSpeed)
{
alterCurrentData("KPH", reduceNumberToString(calculatedWheelSpeed));
alterCurrentData(RideService.KPH, calculatedWheelSpeed.floatValue());
}
}
);
@@ -88,7 +89,7 @@ public class Power extends Ant
@Override
public void onNewCalculatedWheelDistance(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedWheelDistance)
{
alterCurrentData("KM", reduceNumberToString(calculatedWheelDistance));
alterCurrentData(RideService.KM, calculatedWheelDistance.floatValue());
}
}
);
@@ -98,7 +99,7 @@ public class Power extends Ant
@Override
public void onNewInstantaneousCadence(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final int instantaneousCadence)
{
alterCurrentData("CAD", reduceNumberToString(instantaneousCadence));
alterCurrentData(RideService.CAD, instantaneousCadence);
}
}
);
@@ -108,7 +109,7 @@ public class Power extends Ant
@Override
public void onNewRawPowerOnlyData(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long powerOnlyUpdateEventCount, final int instantaneousPower, final long accumulatedPower)
{
alterCurrentData("WATTS", reduceNumberToString(instantaneousPower));
alterCurrentData(RideService.WATTS, instantaneousPower);
}
}
);
@@ -118,7 +119,7 @@ public class Power extends Ant
@Override
public void onNewPedalPowerBalance(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final boolean rightPedalIndicator, final int pedalPowerPercentage)
{
alterCurrentData("LTE", reduceNumberToString(pedalPowerPercentage));
alterCurrentData(RideService.LTE, pedalPowerPercentage);
}
}
);
@@ -128,7 +129,7 @@ public class Power extends Ant
@Override
public void onNewRawWheelTorqueData(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long wheelTorqueUpdateEventCount, final long accumulatedWheelTicks, final BigDecimal accumulatedWheelPeriod, final BigDecimal accumulatedWheelTorque)
{
alterCurrentData("NM", reduceNumberToString(accumulatedWheelTorque));
alterCurrentData(RideService.NM, accumulatedWheelTorque);
}
}
);
@@ -138,7 +139,7 @@ public class Power extends Ant
@Override
public void onNewRawCrankTorqueData(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long crankTorqueUpdateEventCount, final long accumulatedCrankTicks, final BigDecimal accumulatedCrankPeriod, final BigDecimal accumulatedCrankTorque)
{
alterCurrentData("NM", reduceNumberToString(accumulatedCrankTorque));
alterCurrentData(RideService.NM, accumulatedCrankTorque);
}
}
);
@@ -148,10 +149,17 @@ public class Power extends Ant
@Override
public void onNewTorqueEffectiveness(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long powerOnlyUpdateEventCount, final BigDecimal leftTorqueEffectiveness, final BigDecimal rightTorqueEffectiveness)
{
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("LTE", reduceNumberToString(leftTorqueEffectiveness));
map.put("RTE", reduceNumberToString(rightTorqueEffectiveness));
alterCurrentData(map);
int[] keys = {
RideService.LTE,
RideService.RTE
};
float[] values = {
leftTorqueEffectiveness,
rightTorqueEffectiveness
}
alterCurrentData(keys, values);
}
}
@@ -161,9 +169,16 @@ public class Power extends Ant
@Override
public void onNewPedalSmoothness(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long powerOnlyUpdateEventCount, final boolean separatePedalSmoothnessSupport, final BigDecimal leftOrCombinedPedalSmoothness, final BigDecimal rightPedalSmoothness)
{
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("SNPLC", reduceNumberToString(leftOrCombinedPedalSmoothness));
map.put("SNPR", reduceNumberToString(rightPedalSmoothness));
int[] keys = {
RideService.SNPLC,
RideService.SNPR
};
float[] values = {
leftOrCombinedPedalSmoothness,
rightPedalSmoothness
}
alterCurrentData(map);
}
}
@@ -175,14 +190,24 @@ public class Power extends Ant
@Override
public void zeroReadings()
{
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("WATTS", "0");
map.put("NM", "0");
map.put("CAD", "0");
map.put("KPH", "0");
map.put("KM", "0");
alterCurrentData(map);
{
int[] keys = {
RideService.WATTS,
RideService.NM,
RideService.CAD,
RideService.KPH,
RideService.KM
};
float[] values = {
(float) 0.0,
(float) 0.0,
(float) 0.0,
(float) 0.0,
(float) 0.0
};
alterCurrentData(keys, values);
}
}

View File

@@ -1,6 +1,5 @@
package com.ridelogger.listners;
import java.util.LinkedHashMap;
import java.util.Timer;
import java.util.TimerTask;
@@ -8,10 +7,12 @@ import com.ridelogger.R;
import com.ridelogger.RideService;
import android.content.Context;
import android.content.SharedPreferences;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.preference.PreferenceManager;
/**
* Sensors
@@ -55,14 +56,16 @@ public class Sensors extends Base<Object>
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]));
alterCurrentData(RideService.lux, event.values[0]);
}
};
mSensorManager.registerListener(luxListner, mLight, 3000000);
}
if(mAccel != null) {
if(context.settings.getBoolean(context.getString(R.string.PREF_DETECT_CRASH), false)) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
if(settings.getBoolean(context.getString(R.string.PREF_DETECT_CRASH), false)) {
accelListner = new SensorEventListener() {
private boolean crashed = false;
private Timer timer = new Timer();
@@ -73,11 +76,13 @@ public class Sensors extends Base<Object>
@Override
public final void onSensorChanged(SensorEvent event) {
LinkedHashMap<String, String> map = new LinkedHashMap<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);
int[] keys = {
RideService.ms2x,
RideService.ms2y,
RideService.ms2z
};
alterCurrentData(keys, event.values);
if(St.length == 0) {
St[0] = event.values[0];
@@ -95,14 +100,14 @@ public class Sensors extends Base<Object>
crashed = true;
context.phoneCrash(amag);
if(context.currentValues.containsKey("KPH")) {
if(!Float.isNaN(context.currentValues[RideService.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"))) {
if(1.0 > context.currentValues[RideService.KPH]) {
context.phoneCrashConfirm();
} else {
crashed = false;
@@ -132,20 +137,21 @@ public class Sensors extends Base<Object>
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public final void onSensorChanged(SensorEvent event) {
LinkedHashMap<String, String> map = new LinkedHashMap<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);
public final void onSensorChanged(SensorEvent event) {
int[] keys = {
RideService.ms2x,
RideService.ms2y,
RideService.ms2z
};
alterCurrentData(keys, event.values);
}
};
}
mSensorManager.registerListener(accelListner, mAccel, SensorManager.SENSOR_DELAY_NORMAL);
}
if(mPress != null) {
pressListner = new SensorEventListener() {
@Override
@@ -155,12 +161,13 @@ public class Sensors extends Base<Object>
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]));
alterCurrentData(RideService.press, event.values[0]);
}
};
mSensorManager.registerListener(pressListner, mPress, 3000000);
}
if(mTemp != null) {
tempListner = new SensorEventListener() {
@Override
@@ -170,24 +177,27 @@ public class Sensors extends Base<Object>
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]));
alterCurrentData(RideService.temp, event.values[0]);
}
};
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) {
LinkedHashMap<String, String> map = new LinkedHashMap<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);
public final void onSensorChanged(SensorEvent event) {
int[] keys = {
RideService.uTx,
RideService.uTy,
RideService.uTz
};
alterCurrentData(keys, event.values);
}
};