From 337acc92ea5443991adde1dfd0eb2c89fd050682 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Mon, 17 Nov 2008 11:12:16 -0800 Subject: [PATCH 1/3] Changing the classname of the Android Branch. The package should be reflective of the company that wrote it. (Note: This needs to be changed for every app on the android) --- src/com/nitobi/droidgap/AccelTuple.java | 7 ++ src/com/nitobi/droidgap/DroidGap.java | 89 ++++++++++++++++++++ src/com/nitobi/droidgap/GeoTuple.java | 7 ++ src/com/nitobi/droidgap/PhoneGap.java | 107 ++++++++++++++++++++++++ src/com/nitobi/droidgap/R.java | 26 ++++++ 5 files changed, 236 insertions(+) create mode 100644 src/com/nitobi/droidgap/AccelTuple.java create mode 100644 src/com/nitobi/droidgap/DroidGap.java create mode 100644 src/com/nitobi/droidgap/GeoTuple.java create mode 100644 src/com/nitobi/droidgap/PhoneGap.java create mode 100644 src/com/nitobi/droidgap/R.java diff --git a/src/com/nitobi/droidgap/AccelTuple.java b/src/com/nitobi/droidgap/AccelTuple.java new file mode 100644 index 00000000..490a186b --- /dev/null +++ b/src/com/nitobi/droidgap/AccelTuple.java @@ -0,0 +1,7 @@ +package com.nitobi.droidgap; + +public class AccelTuple { + public long accelX; + public long accelY; + public long accelZ; +} diff --git a/src/com/nitobi/droidgap/DroidGap.java b/src/com/nitobi/droidgap/DroidGap.java new file mode 100644 index 00000000..36ea3e03 --- /dev/null +++ b/src/com/nitobi/droidgap/DroidGap.java @@ -0,0 +1,89 @@ +package com.nitobi.droidgap; + +import java.io.IOException; +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); + + /* 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); + + /* Bind the appView object to the gap class methods */ + bindBrowser(appView); + + /* + * We need to decide whether this is a local or a remote app. For the sake of clarity + * we can use HTML with both local and remote applications, but it means that we have to open the local file + */ + + appView.loadUrl("http://www.infil00p.org/gap/demo/"); + + } + + private void loadFile(WebView appView){ + try { + InputStream is = getAssets().open("index.html"); + + int size = is.available(); + + // Read the entire asset into a local byte buffer. + byte[] buffer = new byte[size]; + is.read(buffer); + is.close(); + + // Convert the buffer into a Java string. + String text = new String(buffer); + + // Load the local file into the webview + appView.loadData(text, "text/html", "UTF-8"); + + } catch (IOException e) { + // Should never happen! + throw new RuntimeException(e); + } + } + + private void bindBrowser(WebView appView) + { + 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/nitobi/droidgap/GeoTuple.java b/src/com/nitobi/droidgap/GeoTuple.java new file mode 100644 index 00000000..50196314 --- /dev/null +++ b/src/com/nitobi/droidgap/GeoTuple.java @@ -0,0 +1,7 @@ +package com.nitobi.droidgap; + +public class GeoTuple { + public double lat; + public double lng; + public double ele; +} diff --git a/src/com/nitobi/droidgap/PhoneGap.java b/src/com/nitobi/droidgap/PhoneGap.java new file mode 100644 index 00000000..4fb6faa0 --- /dev/null +++ b/src/com/nitobi/droidgap/PhoneGap.java @@ -0,0 +1,107 @@ +package com.nitobi.droidgap; + +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 { + + /* + * 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, Handler handler, WebView appView) { + this.mCtx = ctx; + this.mHandler = handler; + this.mAppView = appView; + } + + + 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){ + // Start the vibration + Vibrator vibrator = (Vibrator) mCtx.getSystemService(Context.VIBRATOR_SERVICE); + vibrator.vibrate(pattern); + } + + /* + * 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); + GeoTuple geoloc = new GeoTuple(); + if (locMan.isProviderEnabled(provider)) + { + Location myLoc = (Location) locMan.getLastKnownLocation(provider); + geoloc.lat = myLoc.getLatitude(); + geoloc.lng = myLoc.getLongitude(); + geoloc.ele = myLoc.getAltitude(); + } + else + { + geoloc.lat = 0; + geoloc.lng = 0; + geoloc.ele = 0; + } + mAppView.loadUrl("javascript:gotLocation(" + geoloc.lat + ", " + geoloc.lng + ")"); + } + }); + } + + public String outputText(){ + String test = "

Test

"; + return test; + } + + + public String getUuid() + { + + TelephonyManager operator = (TelephonyManager) mCtx.getSystemService(Context.TELEPHONY_SERVICE); + String uuid = operator.getDeviceId(); + return uuid; + } + + public String getVersion() + { + return version; + } + + public boolean exists() + { + return true; + } + +} diff --git a/src/com/nitobi/droidgap/R.java b/src/com/nitobi/droidgap/R.java new file mode 100644 index 00000000..a4deb51e --- /dev/null +++ b/src/com/nitobi/droidgap/R.java @@ -0,0 +1,26 @@ +/* AUTO-GENERATED FILE. DO NOT MODIFY. + * + * This class was automatically generated by the + * aapt tool from the resource data it found. It + * should not be modified by hand. + */ + +package com.nitobi.droidgap; + +public final class R { + public static final class attr { + } + public static final class drawable { + public static final int icon=0x7f020000; + } + public static final class id { + public static final int appView=0x7f050000; + } + public static final class layout { + public static final int main=0x7f030000; + } + public static final class string { + public static final int app_name=0x7f040001; + public static final int hello=0x7f040000; + } +} From ceb2dd61c248e61f2f408b06af2e486e24d27de6 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Mon, 17 Nov 2008 14:01:45 -0800 Subject: [PATCH 2/3] Fixed the Geolocation Functionality. If it is able to get the location, it will get it from the GPS first, then the network. --- src/com/nitobi/droidgap/GpsListener.java | 73 ++++++++++++++++++++ src/com/nitobi/droidgap/NetworkListener.java | 69 ++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 src/com/nitobi/droidgap/GpsListener.java create mode 100644 src/com/nitobi/droidgap/NetworkListener.java diff --git a/src/com/nitobi/droidgap/GpsListener.java b/src/com/nitobi/droidgap/GpsListener.java new file mode 100644 index 00000000..e592d020 --- /dev/null +++ b/src/com/nitobi/droidgap/GpsListener.java @@ -0,0 +1,73 @@ +package com.nitobi.droidgap; + +import android.content.Context; +import android.hardware.SensorManager; +import android.location.Location; +import android.location.LocationManager; +import android.location.LocationListener; +import android.os.Bundle; +import android.os.Handler; +import android.os.Vibrator; +import android.telephony.TelephonyManager; +import android.util.Log; +import android.webkit.WebView; + +public class GpsListener implements LocationListener { + + private Context mCtx; + private Location cLoc; + private LocationManager mLocMan; + private static final String LOG_TAG = "PhoneGap"; + + public GpsListener(Context ctx) + { + mCtx = ctx; + mLocMan = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE); + mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this); + cLoc = mLocMan.getLastKnownLocation(LocationManager.GPS_PROVIDER); + } + + public Location getLocation() + { + return cLoc; + } + + public void onProviderDisabled(String provider) { + // TODO Auto-generated method stub + Log.d(LOG_TAG, "The provider " + provider + " is disabled"); + } + + public void onProviderEnabled(String provider) { + // TODO Auto-generated method stub + Log.d(LOG_TAG, "The provider "+ provider + " is enabled"); + } + + + public void onStatusChanged(String provider, int status, Bundle extras) { + // TODO Auto-generated method stub + Log.d(LOG_TAG, "The status of the provider " + provider + " has changed"); + if(status == 0) + { + Log.d(LOG_TAG, provider + " is OUT OF SERVICE"); + } + else if(status == 1) + { + Log.d(LOG_TAG, provider + " is TEMPORARILY_UNAVAILABLE"); + } + else + { + Log.d(LOG_TAG, provider + " is Available"); + } + } + + + public void onLocationChanged(Location location) { + Log.d(LOG_TAG, "The location has been updated!"); + cLoc = location; + } + + public boolean hasLocation() { + return (cLoc != null); + } + +} diff --git a/src/com/nitobi/droidgap/NetworkListener.java b/src/com/nitobi/droidgap/NetworkListener.java new file mode 100644 index 00000000..46c2390a --- /dev/null +++ b/src/com/nitobi/droidgap/NetworkListener.java @@ -0,0 +1,69 @@ +package com.nitobi.droidgap; + +import android.content.Context; +import android.hardware.SensorManager; +import android.location.Location; +import android.location.LocationManager; +import android.location.LocationListener; +import android.os.Bundle; +import android.os.Handler; +import android.os.Vibrator; +import android.telephony.TelephonyManager; +import android.util.Log; +import android.webkit.WebView; + +public class NetworkListener implements LocationListener { + + private Context mCtx; + private Location cLoc; + private LocationManager mLocMan; + private static final String LOG_TAG = "PhoneGap"; + + public NetworkListener(Context ctx) + { + mCtx = ctx; + mLocMan = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE); + mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, this); + cLoc = mLocMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + } + + public Location getLocation() + { + return cLoc; + } + + public void onProviderDisabled(String provider) { + // TODO Auto-generated method stub + Log.d(LOG_TAG, "The provider " + provider + " is disabled"); + } + + + public void onProviderEnabled(String provider) { + // TODO Auto-generated method stub + Log.d(LOG_TAG, "The provider "+ provider + " is enabled"); + } + + + public void onStatusChanged(String provider, int status, Bundle extras) { + // TODO Auto-generated method stub + Log.d(LOG_TAG, "The status of the provider " + provider + " has changed"); + if(status == 0) + { + Log.d(LOG_TAG, provider + " is OUT OF SERVICE"); + } + else if(status == 1) + { + Log.d(LOG_TAG, provider + " is TEMPORARILY_UNAVAILABLE"); + } + else + { + Log.d(LOG_TAG, provider + " is Available"); + } + } + + + public void onLocationChanged(Location location) { + Log.d(LOG_TAG, "The location has been updated!"); + cLoc = location; + } +} From b75fdf8610e4cf7dd0c4446c6d3d65982c234cc0 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Mon, 17 Nov 2008 14:03:21 -0800 Subject: [PATCH 3/3] More changes to the namespaces --- AndroidManifest.xml | 8 ++++++-- src/com/nitobi/droidgap/PhoneGap.java | 26 +++++++++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 9b92f368..fcf5c5bc 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,16 +1,20 @@ + + + - + diff --git a/src/com/nitobi/droidgap/PhoneGap.java b/src/com/nitobi/droidgap/PhoneGap.java index 4fb6faa0..4319cd67 100644 --- a/src/com/nitobi/droidgap/PhoneGap.java +++ b/src/com/nitobi/droidgap/PhoneGap.java @@ -4,13 +4,17 @@ import android.content.Context; import android.hardware.SensorManager; import android.location.Location; import android.location.LocationManager; +import android.location.LocationListener; +import android.os.Bundle; import android.os.Handler; import android.os.Vibrator; import android.telephony.TelephonyManager; +import android.util.Log; import android.webkit.WebView; -public class PhoneGap { +public class PhoneGap{ + private static final String LOG_TAG = "PhoneGap"; /* * UUID, version and availability */ @@ -19,14 +23,17 @@ public class PhoneGap { private Context mCtx; private Handler mHandler; private WebView mAppView; - + private GpsListener mGps; + private NetworkListener mNetwork; + public PhoneGap(Context ctx, Handler handler, WebView appView) { this.mCtx = ctx; this.mHandler = handler; this.mAppView = appView; + mGps = new GpsListener(ctx); + mNetwork = new NetworkListener(ctx); } - public void updateAccel(){ mHandler.post(new Runnable() { public void run() { @@ -60,14 +67,13 @@ public class PhoneGap { public void getLocation(final String provider){ mHandler.post(new Runnable() { public void run() { - LocationManager locMan = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE); GeoTuple geoloc = new GeoTuple(); - if (locMan.isProviderEnabled(provider)) + Location loc = mGps.hasLocation() ? mGps.getLocation() : mNetwork.getLocation(); + if (loc != null) { - Location myLoc = (Location) locMan.getLastKnownLocation(provider); - geoloc.lat = myLoc.getLatitude(); - geoloc.lng = myLoc.getLongitude(); - geoloc.ele = myLoc.getAltitude(); + geoloc.lat = loc.getLatitude(); + geoloc.lng = loc.getLongitude(); + geoloc.ele = loc.getAltitude(); } else { @@ -104,4 +110,6 @@ public class PhoneGap { return true; } + + }