More comments and more cleaning

This commit is contained in:
Chet Henry
2014-11-20 21:23:25 -07:00
parent a222df1a40
commit 84f394f766
6 changed files with 93 additions and 96 deletions

View File

@@ -48,18 +48,18 @@ public class RideService extends Service
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; //All other Android sensor class
public static 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
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
NotificationManager mNotificationManager; //Manager class to setup and alter our notification
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
public Boolean snoop = false; //should we log others ant+ devices
Set<String> pairedAnts; //list of ant devices to pair with
/**
* starts the ride on service start
@@ -110,19 +110,18 @@ public class RideService extends Service
SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
String utc = f.format(new Date(startTime));
String utc = f.format(new Date(startTime));
Calendar cal = Calendar.getInstance();
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);
String year = Integer.toString(cal.get(Calendar.YEAR));
settings = getSharedPreferences(StartActivity.PREFS_NAME, 0);
String rider_name = settings.getString(StartActivity.RIDER_NAME, "");
String month = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US);
String week_day = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);
String year = Integer.toString(cal.get(Calendar.YEAR));
settings = getSharedPreferences(StartActivity.PREFS_NAME, 0);
String rider_name = settings.getString(StartActivity.RIDER_NAME, "");
final Set<String> pairedAnts = settings.getStringSet(StartActivity.PAIRED_ANTS, null);
currentValues.put("SECS", "0.0");
String rideHeadder = "{" +
@@ -170,9 +169,7 @@ public class RideService extends Service
buf = new BufferedWriter(new FileWriter(file, true));
buf.write(rideHeadder);
sensors = new HashMap<String, Base<?>>();
sensors.put("GPS", new Gps(this));
sensors.put("GPS", new Gps(this));
sensors.put("AndroidSensors", new Sensors(this));
mCallback = new MultiDeviceSearch.SearchCallbacks(){
@@ -197,36 +194,26 @@ public class RideService extends Service
};
// start the multi-device search
mSearch = new MultiDeviceSearch(this, EnumSet.allOf(DeviceType.class), mCallback, mRssiCallback);
mSearch = new MultiDeviceSearch(this, EnumSet.allOf(DeviceType.class), mCallback, mRssiCallback);
} catch (IOException e) {}
}
rideStarted = true;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Ride On")
.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);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(StartActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
if(mNotificationManager == null) {
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
//build the notification in the top android drawer
NotificationCompat.Builder mBuilder = new NotificationCompat
.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Ride On")
.setContentText("Building ride: " + fileName + " Click to stop ride.")
.setProgress(0, 0, true)
.setContentIntent(
TaskStackBuilder
.create(this)
.addParentStack(StartActivity.class)
.addNextIntent(new Intent(this, StartActivity.class))
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
);
startForeground(notifyID, mBuilder.build());
}
@@ -236,9 +223,7 @@ public class RideService extends Service
protected void stopRide() {
if(!rideStarted) return;
for (Map.Entry<String, Base<?>> entry : sensors.entrySet())
{
for (Map.Entry<String, Base<?>> entry : sensors.entrySet()) {
entry.getValue().onDestroy();
}
@@ -251,6 +236,7 @@ public class RideService extends Service
} catch (IOException e) {}
rideStarted = false;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(notifyID);
}
@@ -262,10 +248,8 @@ public class RideService extends Service
//launch ant+ connection
public void launchConnection(MultiDeviceSearchResult result, Boolean snoop)
{
switch (result.getAntDeviceType())
{
public void launchConnection(MultiDeviceSearchResult result, Boolean snoop) {
switch (result.getAntDeviceType()) {
case BIKE_CADENCE:
break;
case BIKE_POWER:

View File

@@ -29,19 +29,19 @@ public class StartActivity extends FragmentActivity
public static final String RIDER_NAME = "RiderName";
public static final String PAIRED_ANTS = "PairedAnts";
SharedPreferences settings;
AlertDialog dialog;
public MultiDeviceSearch mSearch;
/**
* start up our class
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
rsi = new Intent(this, RideService.class);
rsi = new Intent(this, RideService.class);
settings = getSharedPreferences(PREFS_NAME, 0);
final String riderName = settings.getString(RIDER_NAME, "");
settings = getSharedPreferences(PREFS_NAME, 0);
String rider_name = settings.getString(RIDER_NAME, "");
if(rider_name == "") {
if(riderName == "") {
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(this);
@@ -50,17 +50,17 @@ public class StartActivity extends FragmentActivity
.setTitle("Chose Rider Name");
// Set up the input
final EditText input = new EditText(this);
final EditText riderNameInput = new EditText(this);
// 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);
riderNameInput.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(riderNameInput);
builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String name = input.getText().toString();
if(name != "" && name != null) {
String riderName = riderNameInput.getText().toString();
if(riderName != "" && riderName != null) {
SharedPreferences.Editor editor = settings.edit();
editor.putString(RIDER_NAME, name);
editor.putString(RIDER_NAME, riderName);
editor.commit();
setupAnt();
}
@@ -68,7 +68,7 @@ public class StartActivity extends FragmentActivity
});
// 3. Get the AlertDialog from create()
dialog = builder.create();
AlertDialog dialog = builder.create();
dialog.show();
} else {
toggleRide();
@@ -76,8 +76,11 @@ public class StartActivity extends FragmentActivity
}
}
/**
* try to pair some ant+ devices
*/
protected void setupAnt() {
MultiDeviceSearch.SearchCallbacks mCallback;
MultiDeviceSearch.RssiCallback mRssiCallback;
final ArrayList<MultiDeviceSearchResult> foundDevices = new ArrayList<MultiDeviceSearchResult>();
@@ -100,9 +103,14 @@ public class StartActivity extends FragmentActivity
};
// start the multi-device search
mSearch = new MultiDeviceSearch(this, EnumSet.allOf(DeviceType.class), mCallback, mRssiCallback);
MultiDeviceSearch mSearch = new MultiDeviceSearch(this, EnumSet.allOf(DeviceType.class), mCallback, mRssiCallback);
}
/**
* dialog of soon to be paired ant devices
* @param foundDevices
*/
protected void selectDevicesDialog(final ArrayList<MultiDeviceSearchResult> foundDevices) {
final ArrayList<Integer> mSelectedItems = new ArrayList<Integer>(); // Where we track the selected items
final ArrayList<CharSequence> foundDevicesString = new ArrayList<CharSequence>();
@@ -147,7 +155,7 @@ public class StartActivity extends FragmentActivity
}
});
dialog = builder.create();
AlertDialog dialog = builder.create();
dialog.show();
}
@@ -156,8 +164,11 @@ public class StartActivity extends FragmentActivity
// TODO Auto-generated method stub
super.onDestroy();
}
/**
* stop ride and clean up references
*/
private void stopRide() {
Toast toast = Toast.makeText(getApplicationContext(), "Stoping Ride!", Toast.LENGTH_LONG);
toast.show();
@@ -165,6 +176,9 @@ public class StartActivity extends FragmentActivity
}
/**
* start or stop ride
*/
protected void toggleRide() {
if(!isServiceRunning(RideService.class)) {
startRide();
@@ -174,6 +188,9 @@ public class StartActivity extends FragmentActivity
}
/**
* start the ride and notify the user of success
*/
private void startRide() {
if(!isServiceRunning(RideService.class)) {
this.startService(rsi);
@@ -183,6 +200,11 @@ public class StartActivity extends FragmentActivity
}
/**
* is a service running or not
* @param serviceClass
* @return
*/
private boolean isServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {

View File

@@ -1,7 +1,6 @@
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;

View File

@@ -149,12 +149,13 @@ public class Base<T>
} catch (IOException e) {}
}
//get current time stamp
public String getTs() {
return reduceNumberToString((double) (System.currentTimeMillis() - RideService.startTime) / 1000.0);
}
//reduce number data types to consistently formatted strings
public static String reduceNumberToString(double d)
{
if(d == (long) d)
@@ -164,6 +165,7 @@ public class Base<T>
}
//reduce number data types to consistently formatted strings
public static String reduceNumberToString(float d)
{
if(d == (long) d)
@@ -173,18 +175,14 @@ public class Base<T>
}
//reduce number data types to consistently formatted strings
public static String reduceNumberToString(BigDecimal d)
{
try {
long test = d.longValueExact();
return String.format("%d", test);
} catch (Exception e) {
// TODO: handle exception
return String.format("%s", d.toPlainString());
}
return String.format("%s", d.toPlainString());
}
//Clean up my listners here
//Clean up my listeners here
public void onDestroy() {}
}

View File

@@ -8,12 +8,7 @@ import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.ICalculatedCrankCaden
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.ICalculatedPowerReceiver;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.ICalculatedTorqueReceiver;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.IInstantaneousCadenceReceiver;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.IPedalPowerBalanceReceiver;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.IPedalSmoothnessReceiver;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.IRawCrankTorqueDataReceiver;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.IRawPowerOnlyDataReceiver;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.IRawWheelTorqueDataReceiver;
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.ITorqueEffectivenessReceiver;
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;
@@ -32,7 +27,7 @@ import java.util.Map;
*/
public class Power extends Ant
{
public BigDecimal wheelCircumferenceInMeters = new BigDecimal("2.07"); //size of wheel to calculate speed
public BigDecimal wheelCircumferenceInMeters = new BigDecimal("2.07"); //size of wheel to calculate speed
//setup listeners and logging
public Power(MultiDeviceSearchResult result, RideService mContext) {
@@ -40,18 +35,18 @@ public class Power extends Ant
releaseHandle = AntPlusBikePowerPcc.requestAccess(context, result.getAntDeviceNumber(), 0, mResultReceiver, mDeviceStateChangeReceiver);
}
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
public void onResultReceived(AntPlusBikePowerPcc result, RequestAccessResult resultCode, DeviceState initialDeviceState)
{
public void onResultReceived(AntPlusBikePowerPcc result, RequestAccessResult resultCode, DeviceState initialDeviceState) {
if(resultCode == com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult.SUCCESS) {
result.subscribeCalculatedPowerEvent(new ICalculatedPowerReceiver() {
@Override
@@ -119,12 +114,12 @@ public class Power extends Ant
}
);
result.subscribePedalPowerBalanceEvent(
/*result.subscribePedalPowerBalanceEvent(
new IPedalPowerBalanceReceiver() {
@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));
}
}
);
@@ -154,10 +149,10 @@ 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)
{
//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);
}
}
@@ -167,13 +162,13 @@ 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)
{
//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);
}
}
);
);*/
}
}
};

View File

@@ -11,7 +11,6 @@ import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
/**
* Sensors
* @author Chet Henry