diff --git a/src/com/android/droidgap/DroidGap.java b/src/com/android/droidgap/DroidGap.java index 9ce4d3e9..c8c325ae 100644 --- a/src/com/android/droidgap/DroidGap.java +++ b/src/com/android/droidgap/DroidGap.java @@ -5,19 +5,30 @@ import java.io.InputStream; import android.app.Activity; import android.os.Bundle; +import android.os.Handler; +import android.util.Log; +import android.webkit.JsResult; +import android.webkit.WebChromeClient; import android.webkit.WebView; public class DroidGap extends Activity { + private static final String LOG_TAG = "DroidGap"; private WebView appView; + private Handler mHandler = new Handler(); + /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); - appView = (WebView) findViewById(R.id.appView); + appView = (WebView) findViewById(R.id.appView); + + /* This changes the setWebChromeClient to log alerts to LogCat! Important for Javascript Debugging */ + + appView.setWebChromeClient(new MyWebChromeClient()); appView.getSettings().setJavaScriptEnabled(true); appView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); @@ -29,7 +40,7 @@ public class DroidGap extends Activity { * we can use HTML with both local and remote applications, but it means that we have to open the local file */ - appView.loadUrl("http://infil00p.org/gap/"); + appView.loadUrl("http://www.infil00p.org/gap/demo/"); } @@ -58,8 +69,21 @@ public class DroidGap extends Activity { private void bindBrowser(WebView appView) { - PhoneGap gap = new PhoneGap(this); + PhoneGap gap = new PhoneGap(this, mHandler, appView); appView.addJavascriptInterface(gap, "DroidGap"); } + /** + * Provides a hook for calling "alert" from javascript. Useful for + * debugging your javascript. + */ + final class MyWebChromeClient extends WebChromeClient { + @Override + public boolean onJsAlert(WebView view, String url, String message, JsResult result) { + Log.d(LOG_TAG, message); + result.confirm(); + return true; + } + } + } \ No newline at end of file diff --git a/src/com/android/droidgap/PhoneGap.java b/src/com/android/droidgap/PhoneGap.java index c161f409..710701a1 100644 --- a/src/com/android/droidgap/PhoneGap.java +++ b/src/com/android/droidgap/PhoneGap.java @@ -4,34 +4,47 @@ import android.content.Context; import android.hardware.SensorManager; import android.location.Location; import android.location.LocationManager; +import android.os.Handler; import android.os.Vibrator; import android.telephony.TelephonyManager; +import android.webkit.WebView; public class PhoneGap { - public static GeoTuple location; - public static AccelTuple accel; - public String uuid = getDeviceId(); - public static String version = "0.1"; - + /* + * UUID, version and availability + */ + public boolean droid = true; + private String version = "0.1"; private Context mCtx; + private Handler mHandler; + private WebView mAppView; - public PhoneGap(Context ctx) { + public PhoneGap(Context ctx, Handler handler, WebView appView) { this.mCtx = ctx; + this.mHandler = handler; + this.mAppView = appView; } - public void updateAccel() { - accel.accelX = SensorManager.DATA_X; - accel.accelY = SensorManager.DATA_Y; - accel.accelZ = SensorManager.DATA_Z; + + public void updateAccel(){ + mHandler.post(new Runnable() { + public void run() { + int accelX = SensorManager.DATA_X; + int accelY = SensorManager.DATA_Y; + int accelZ = SensorManager.DATA_Z; + mAppView.loadUrl("javascript:gotAcceleration(" + accelX + ", " + accelY + "," + accelZ + ")"); + } + }); + } public void takePhoto(){ - + // TO-DO: Figure out what this should do } public void playSound(){ - + // TO-DO: Figure out what this should do } public void vibrate(long pattern){ @@ -40,12 +53,22 @@ public class PhoneGap { vibrator.vibrate(pattern); } - public void getLocation(String provider){ - LocationManager locMan = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE); - Location myLoc = (Location) locMan.getLastKnownLocation(provider); - location.lat = myLoc.getLatitude(); - location.lng = myLoc.getLongitude(); - location.ele = myLoc.getAltitude(); + /* + * Android requires a provider, since it can fall back on triangulation and other means as well as GPS + */ + + public void getLocation(final String provider){ + mHandler.post(new Runnable() { + public void run() { + LocationManager locMan = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE); + Location myLoc = (Location) locMan.getLastKnownLocation(provider); + GeoTuple geoloc = new GeoTuple(); + geoloc.lat = myLoc.getLatitude(); + geoloc.lng = myLoc.getLongitude(); + geoloc.ele = myLoc.getAltitude(); + mAppView.loadUrl("javascript:gotLocation(" + geoloc.lat + ", " + geoloc.lng + ")"); + } + }); } public String outputText(){ @@ -53,9 +76,23 @@ public class PhoneGap { return test; } - private String getDeviceId(){ + + public String getUuid() + { + TelephonyManager operator = (TelephonyManager) mCtx.getSystemService(Context.TELEPHONY_SERVICE); - String uniqueId = operator.getDeviceId(); - return uniqueId; + String uuid = operator.getDeviceId(); + return uuid; + } + + public String getVersion() + { + return version; } + + public boolean exists() + { + return true; + } + }