Skip to content

Commit

Permalink
enabling debug mode. hunting for battery issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobSheehy committed Feb 17, 2013
1 parent 4704fed commit 34309ba
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 48 deletions.
5 changes: 3 additions & 2 deletions AndroidManifest.xml
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ca.cumulonimbus.barometernetwork"
android:versionCode="301"
android:versionName="3.0.1">
android:versionCode="302"
android:versionName="3.0.2">
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:name="ca.cumulonimbus.barometernetwork.SettingsActivity"
Expand Down
35 changes: 16 additions & 19 deletions src/ca/cumulonimbus/barometernetwork/BarometerNetworkActivity.java
Expand Up @@ -462,12 +462,12 @@ private void getStoredPreferences() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
mUpdateServerAutomatically = settings.getBoolean("autoupdate", true);
mUpdateServerFrequency = settings.getString("autofrequency", "10 minutes");
log("shared prefs: " + mUpdateServerFrequency + " " + mUpdateServerAutomatically);
//log("shared prefs: " + mUpdateServerFrequency + " " + mUpdateServerAutomatically);

// Units
String abbrev = settings.getString("units", "mbar");
mUnit = new Unit(abbrev);
log("abbrev: " + abbrev);
//log("abbrev: " + abbrev);

} catch(Exception e) {
log(e.getMessage() + "");
Expand All @@ -494,17 +494,16 @@ public void setId() {
}
}



// Used to write a log to SD card. Not used unless logging enabled.
public void setUpFiles() {
try {
File homeDirectory = getExternalFilesDir(null);
if(homeDirectory!=null) {
mAppDir = homeDirectory.getAbsolutePath();

}
} catch (Exception e) {
//log(e.getMessage());
log(e.getMessage());
}
}

Expand All @@ -517,7 +516,7 @@ public void setUpBarometer() {

if(bar!=null) {
boolean running = sm.registerListener(this, bar, SensorManager.SENSOR_DELAY_NORMAL);
log(running + "");
//log(running + "");
barometerDetected = true;
} else {
barometerDetected = false;
Expand All @@ -539,14 +538,13 @@ public void setUpMap() {

// Set default coordinates (centered around the user's location)

log("about to get controller");
try {
MapController mc = mapView.getController();
LocationManager lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Location loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mc.setZoom(8);
if(loc.getLatitude()!=0) {
log("setting center " + loc.getLatitude() + " " + loc.getLongitude());
//log("setting center " + loc.getLatitude() + " " + loc.getLongitude());
mc.animateTo(new GeoPoint((int)(loc.getLatitude()*1E6), (int)(loc.getLongitude() * 1E6)));
} else {
log("no known last location");
Expand Down Expand Up @@ -586,10 +584,10 @@ public void onLocationChanged(Location location) {
mLatitude = latitude;
mLongitude = longitude;
if(first) {
log("first location start");
//log("first location start");
setUpMap();
loadAndShowData();
log("end");
//log("end");
}
} catch(Exception e) {
log("On Location change failed.");
Expand Down Expand Up @@ -738,7 +736,6 @@ public void addDataToMap(ArrayList<BarometerReading> list, boolean showTendencie
mapOverlays.clear();

// add a bunch of coords
log("starting adding total " + list.size());
try {
for(BarometerReading br : list) {
// log(br.getReading() + "");
Expand Down Expand Up @@ -772,7 +769,7 @@ public void addDataToMap(ArrayList<BarometerReading> list, boolean showTendencie
} catch(Exception e) {
log("add data error: " + e.getMessage());
}
log("end of adddatatomap");

}

// Runnable to refresh the map. Can be called when another
Expand Down Expand Up @@ -811,16 +808,16 @@ public ArrayList<BarometerReading> csvToBarometerReadings(String[] readings) {

// Assemble a HashMap of userIDs and tendencies. //This is the opposite of function barometerReadingToWeb in the servlet.
public HashMap<String, String> csvToBarometerTendencies(String[] readings) {
log("csv to barometer tendencies : " + readings[0] + ", " + readings.length);
//log("csv to barometer tendencies : " + readings[0] + ", " + readings.length);
HashMap<String, String> tendencies = new HashMap<String, String>();
for(int i = 0; i<readings.length; i++) {
try {
String[] values = readings[i].split(",");

for(String a : values) {
log(a);
//log(a);
}
log("tendency check " + values[5]);
//log("tendency check " + values[5]);
tendencies.put(values[5], values[6]); // userid, tendency
} catch(Exception e) {
// Likely, the server returned an error.
Expand Down Expand Up @@ -947,7 +944,7 @@ protected String doInBackground(String... arg0) {
String responseText = "";

try {
log("DataDownload doInBackground start try block");
//log("DataDownload doInBackground start try block");

// Instantiate the custom HttpClient
DefaultHttpClient client = new SecureHttpClient(getApplicationContext());
Expand Down Expand Up @@ -1010,7 +1007,7 @@ protected String doInBackground(String... arg0) {
return responseText;
}
protected void onPostExecute(String result) {
log("datadownload post execute " + result);
//log("datadownload post execute " + result);
processDownloadResult(result);

mapHandler.postDelayed(refreshMap, 100);
Expand Down Expand Up @@ -1193,7 +1190,7 @@ public void logToFile(String text) {
}

public void log(String text) {
//logToFile(text);
//System.out.println(text);
logToFile(text);
System.out.println(text);
}
}
48 changes: 23 additions & 25 deletions src/ca/cumulonimbus/barometernetwork/SubmitReadingService.java
Expand Up @@ -2,6 +2,7 @@

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -78,6 +79,21 @@ public final class SubmitReadingService extends Service implements SensorEventLi

private boolean waitingForReading = false;


// Used to write a log to SD card. Not used unless logging enabled.
public void setUpFiles() {
try {
File homeDirectory = getExternalFilesDir(null);
if(homeDirectory!=null) {
mAppDir = homeDirectory.getAbsolutePath();
}
log("setting up logging");
} catch (Exception e) {
//log(e.getMessage());
}
}


private boolean networkOnline() {
try {
// Test connectivity before attempting to send readings
Expand All @@ -103,7 +119,7 @@ private boolean networkOnline() {
LocationListener locationListener;
// Get the user's location from the location service, preferably GPS.
public void getLocationInformation() {

log("get location information");
// get the location
// Acquire a reference to the system Location Manager
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Expand All @@ -121,6 +137,7 @@ public void onLocationChanged(Location location) {
// no changes. stop for now.
if(mLatitude == latitude) {
if (mLongitude == longitude) {
log("pausing location lookup");
locationManager.removeUpdates(locationListener);
}
}
Expand Down Expand Up @@ -163,7 +180,6 @@ public void setUpBarometer() {
if(bar!=null) {
barometerReadingsActive = sm.registerListener(this, bar, SensorManager.SENSOR_DELAY_UI);

log("bar is not null, listening " + barometerReadingsActive);
} else {
log("bar is null");
}
Expand Down Expand Up @@ -302,7 +318,6 @@ protected Long doInBackground(String... arg0) {
try {
log("offline. adding to queue " + mReading + " of size " + submitList.size());
submitList.add(br);
log("added to queue");
} catch(Exception e) {
log(e.getMessage());
}
Expand All @@ -323,7 +338,7 @@ protected void onPostExecute(Long result) {

private Runnable mWaitForBarometer = new Runnable() {
public void run() {
log("rs attempt: " + mLatitude + " " + mLongitude + ": " + mReading);
//log("rs attempt: " + mLatitude + " " + mLongitude + ": " + mReading);
new ReadingSender().execute("");
}
};
Expand All @@ -337,7 +352,7 @@ public void sendBarometerReading() {
setId();

if(mReading == 0.0) {
log("active barometer: " + barometerReadingsActive);
//log("active barometer: " + barometerReadingsActive);
} else {
log("rs attempt: " + mLatitude + " " + mLongitude + ": " + mReading);
new ReadingSender().execute("");
Expand Down Expand Up @@ -396,8 +411,8 @@ public void logToFile(String text) {
}

public void log(String text) {
//System.out.println(text);
//logToFile(text);
System.out.println(text);
logToFile(text);
}

private long convertSettingsTextToSeconds(String text) {
Expand Down Expand Up @@ -451,19 +466,6 @@ public void onDestroy() {
super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
//System.out.println("on start submitreadingservice");
System.out.println("on start");
try {
//sendDataPeriodically();
} catch(Exception e) {
System.out.println(e.getMessage());
}
super.onStart(intent, startId);
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Expand All @@ -481,11 +483,7 @@ public boolean onUnbind(Intent intent) {
public int onStartCommand(Intent intent, int flags, int startId) {
log("on start command");
try {
//mAppDir = intent.getStringExtra("appdir");
} catch(Exception e) {

}
try {
setUpFiles(); // start logs
setUpDatabase();
} catch(Exception e) {

Expand Down
36 changes: 34 additions & 2 deletions src/ca/cumulonimbus/barometernetwork/WidgetButtonService.java
Expand Up @@ -2,12 +2,16 @@

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import android.app.Service;
import android.appwidget.AppWidgetManager;
Expand Down Expand Up @@ -44,11 +48,13 @@ public class WidgetButtonService extends Service implements SensorEventListener

private String mAppDir = "";



public void startListening() {
try {
sm = (SensorManager) getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
Sensor bar = sm.getDefaultSensor(Sensor.TYPE_PRESSURE);

log("widget start listening pressure");
if(bar!=null) {
running = sm.registerListener(this, bar, SensorManager.SENSOR_DELAY_NORMAL);
//Toast.makeText(getApplicationContext(), "starting listener", Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -113,6 +119,8 @@ public void update(Intent intent, double reading) {
recents = dbAdapter.fetchRecentReadings(5); // the last few hours
String tendency = ScienceHandler.findTendency(recents);

log("widget getting tendency, updating and sending");

if(tendency.contains("rising")) {
remoteView.setInt(R.id.widget_tendency_image_up, "setVisibility", View.VISIBLE);
remoteView.setInt(R.id.widget_tendency_image_down, "setVisibility", View.INVISIBLE);
Expand Down Expand Up @@ -262,6 +270,7 @@ public void onDestroy() {
public void onStart(Intent intent, int startId) {
try {
mAppDir = intent.getStringExtra("appdir");

} catch(Exception e) {

}
Expand All @@ -282,13 +291,15 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}



@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_PRESSURE:
mReading = event.values[0];
//Toast.makeText(getApplicationContext(), mReading + "", Toast.LENGTH_SHORT).show();
log("widget sensor changed " + mReading + " and unregistering");
update(new Intent(), mReading);
sm.unregisterListener(this);
mReading = 0.0;
Expand All @@ -297,4 +308,25 @@ public void onSensorChanged(SensorEvent event) {

}

// Log data to SD card for debug purposes.
// To enable logging, ensure the Manifest allows writing to SD card.
public void logToFile(String text) {
try {
OutputStream output = new FileOutputStream(mAppDir + "/log.txt", true);
String logString = (new Date()).toString() + ": " + text + "\n";
output.write(logString.getBytes());
output.close();

} catch(FileNotFoundException e) {

} catch(IOException ioe) {

}
}

public void log(String text) {
System.out.println(text);
logToFile(text);
}

}

0 comments on commit 34309ba

Please sign in to comment.