Skip to content
Browse files

Added battery status and source of location fix to test messages.

  • Loading branch information...
1 parent cf037aa commit 1104956ed341963abc9519856873f8504532a658 @jones139 committed May 5, 2012
View
15 WhereAreYou/.settings/org.eclipse.jdt.core.prefs
@@ -1,4 +1,11 @@
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
-org.eclipse.jdt.core.compiler.compliance=1.5
-org.eclipse.jdt.core.compiler.source=1.5
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
View
23 WhereAreYou/src/uk/org/maps3/BatteryMonitor.java
@@ -0,0 +1,23 @@
+/**
+ *
+ */
+package uk.org.maps3;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+
+/**
+ * @author graham
+ *
+ */
+public class BatteryMonitor {
+ public float getBatteryState(Context context) {
+ Intent bat = context.registerReceiver(null, new
+ IntentFilter(Intent.ACTION_BATTERY_CHANGED));
+ int level = bat.getIntExtra("level", 0);
+ int scale = bat.getIntExtra("scale", 100);
+ return level * 100 / scale;
+ }
+
+}
View
35 WhereAreYou/src/uk/org/maps3/LocationFinder.java
@@ -38,6 +38,8 @@
long mTimeStart;
Context mContext;
boolean mTimedOut = false;
+ int timeOutCount = 0;
+ int timeOutSec;
Handler mHandler;
boolean mUseGPS = true;
@@ -63,6 +65,7 @@ public LocationFinder(Context contextArg) {
public void getLocationLL(LocationReceiver lr, int timeOutSec, boolean useGPS) {
+ this.timeOutSec = timeOutSec;
mUseGPS = useGPS;
if (mUseGPS) {
mProvider = LocationManager.GPS_PROVIDER;
@@ -90,6 +93,7 @@ public void startFixSearch() {
public void onLocationChanged(Location loc) {
Log.v("locationListener","onLocationChanged");
+ lr.msgBox("onLocationChanged - mTimedOut ="+mTimedOut+" Provider="+loc.getProvider());
if (loc!=null) {
lr.msgBox("onLocationChanged - mTimedOut ="+mTimedOut+" Provider="+loc.getProvider());
if ((loc.getProvider().equals(mProvider)) || mTimedOut) {
@@ -99,7 +103,8 @@ public void onLocationChanged(Location loc) {
LonLat ll;
ll = new LonLat(loc.getLongitude(),
loc.getLatitude(),
- loc.getAccuracy());
+ loc.getAccuracy(),
+ loc.getProvider());
lr.onLocationFound(ll);
} else {
lr.msgBox("Skipping location update by "+loc.getProvider()+" mProvider="+mProvider+".");
@@ -122,14 +127,34 @@ public void onStatusChanged(String provider, int status, Bundle extras) {
Log.v("locationListener","onStatusChanged");
}
- // Called by the mHandler timer to signify timeout. In which case we give up on GPS and fall back onto NETWORK_PROVIDER.
+ // Called by the mHandler timer to signify timeout.
+ // In which case we give up on GPS and fall back onto NETWORK_PROVIDER.
+ // At the moment, if we time out on NETWORK_PROVIDER too, it just
+ // keeps trying over and over - maybe set a count in future to
+ // make it give up altogether eventually?
public void run() {
Log.v("WAYN","timeout runnable.run");
mTimedOut = true;
- lr.msgBox("TimedOut!");
- Location loc = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- onLocationChanged(loc);
+ timeOutCount ++;
+ lr.msgBox("TimedOut Number "+timeOutCount+
+ "! - using network location instead.");
+ //Location loc = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
+ //onLocationChanged(loc);
+ // switch off GPS monitoring to save battery.
+ locMgr.removeGpsStatusListener(this);
+ locMgr.removeUpdates(this);
+
+ // Re-start search using network rather than GPS.
+ mProvider = LocationManager.NETWORK_PROVIDER;
+ Log.v("mProvider",mProvider);
+ // Ask for location updates to be sent to the onLocationChanged() method of this class.
+ locMgr.requestLocationUpdates(mProvider, 0,0, this);
+
+ // Set a timer running to allow us to give up on getting a fix.
+ // this.run() is called when the timer times out.
+ mHandler.removeCallbacks(this);
+ mHandler.postDelayed(this, timeOutSec*1000);
}
public void onGpsStatusChanged(int eventNo) {
View
6 WhereAreYou/src/uk/org/maps3/LonLat.java
@@ -9,15 +9,17 @@
public final float _acc; // fix accuracy in metres.
public double lon() {return _lon;};
public double lat() {return _lat;};
- public LonLat(double llon,double llat, float lacc) {
+ public String provider;
+ public LonLat(double llon,double llat, float lacc, String provider) {
_lon = llon;
_lat = llat;
_acc = lacc;
+ this.provider = provider;
}
String toStr() {
NumberFormat df = new DecimalFormat("#0.000");
- return ("lon="+df.format(_lon)+", lat="+df.format(_lat)+": accuracy="+df.format(_acc)+" m");
+ return ("lon="+df.format(_lon)+", lat="+df.format(_lat)+": accuracy="+df.format(_acc)+" m ("+provider+")");
}
}
View
31 WhereAreYou/src/uk/org/maps3/TileUtils.java
@@ -0,0 +1,31 @@
+/**
+ *
+ */
+package uk.org.maps3;
+
+/**
+ * @author graham
+ *
+ */
+class XY {
+ int x;
+ int y;
+}
+
+public class TileUtils {
+ public static XY ll2TileXy(LonLat ll) {
+ int z = 12; // tile numbers refer to zoom level 12;
+ XY xy = new XY();
+ double lon = ll.lon();
+ double lat = ll.lat();
+ xy.x = (int)Math.floor( (lon + 180) / 360 * (1<<z) ) ;
+ xy.y = (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(lat))
+ + 1 / Math.cos(Math.toRadians(lat))) / Math.PI) / 2 * (1<<z) ) ;
+ return xy;
+ }
+ public static String getTileNumber(final double lat, final double lon, final int zoom) {
+ int xtile = (int)Math.floor( (lon + 180) / 360 * (1<<zoom) ) ;
+ int ytile = (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(lat)) + 1 / Math.cos(Math.toRadians(lat))) / Math.PI) / 2 * (1<<zoom) ) ;
+ return("" + zoom + "/" + xtile + "/" + ytile);
+ }
+}
View
3 WhereAreYou/src/uk/org/maps3/WhereAreYouActivity.java
@@ -159,6 +159,9 @@ public void onClick(View arg0) {
/* Write a message box to the screen, and the log */
public void msgBox(String msg) {
+ BatteryMonitor bm=new BatteryMonitor();
+ float batState = bm.getBatteryState(this);
+ msg = msg + "\nBattery="+batState+"%";
TextView tv = (TextView)( findViewById(R.id.msgText));
tv.setText(msg);
//Toast.makeText(this,

0 comments on commit 1104956

Please sign in to comment.
Something went wrong with that request. Please try again.