From c050e00b8fda9559e9b7eaecbcba671414f68e87 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Wed, 15 Sep 2010 14:17:40 -0500 Subject: [PATCH] Use timeout to break out of possible infinite loop waiting for sensor to start. --- framework/src/com/phonegap/AccelListener.java | 7 ++++++- framework/src/com/phonegap/CompassListener.java | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/framework/src/com/phonegap/AccelListener.java b/framework/src/com/phonegap/AccelListener.java index 8896f018..4f4cae62 100755 --- a/framework/src/com/phonegap/AccelListener.java +++ b/framework/src/com/phonegap/AccelListener.java @@ -105,13 +105,18 @@ public class AccelListener implements SensorEventListener, Plugin{ return new PluginResult(PluginResult.Status.IO_EXCEPTION, AccelListener.ERROR_FAILED_TO_START); } // Wait until running - while (this.status == STARTING) { + long timeout = 2000; + while ((this.status == STARTING) && (timeout > 0)) { + timeout = timeout - 100; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } + if (timeout == 0) { + return new PluginResult(PluginResult.Status.IO_EXCEPTION, AccelListener.ERROR_FAILED_TO_START); + } } JSONObject r = new JSONObject(); r.put("x", this.x); diff --git a/framework/src/com/phonegap/CompassListener.java b/framework/src/com/phonegap/CompassListener.java index 2f3bf549..6ff6efe1 100755 --- a/framework/src/com/phonegap/CompassListener.java +++ b/framework/src/com/phonegap/CompassListener.java @@ -98,13 +98,18 @@ public class CompassListener implements SensorEventListener, Plugin{ return new PluginResult(PluginResult.Status.IO_EXCEPTION, ERROR_FAILED_TO_START); } // Wait until running - while (this.status == STARTING) { + long timeout = 2000; + while ((this.status == STARTING) && (timeout > 0)) { + timeout = timeout - 100; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } + if (timeout == 0) { + return new PluginResult(PluginResult.Status.IO_EXCEPTION, AccelListener.ERROR_FAILED_TO_START); + } } float f = this.getHeading(); return new PluginResult(status, f);