From 17f76029e582e3a70056e25375938504b1b1b531 Mon Sep 17 00:00:00 2001 From: ricky barrette Date: Thu, 3 Feb 2011 12:16:34 +0000 Subject: [PATCH] removed LocationService.java and ReverseGeocode.java as the have been intergrated into the Location Library svn+ssh://tcdevsvn1/svn/libraries/trunk/LocationLib --- TravelPost/.classpath | 6 + TravelPost/.codepro/deadCodeEntryPoints.xml | 12 + TravelPost/.project | 5 + TravelPost/default.properties | 1 + .../com/TwentyCodes/android/TravelPost/R.java | 32 ++- .../android/TravelPost/LocationReceiver.java | 4 +- .../android/TravelPost/LocationService.java | 238 ------------------ .../android/TravelPost/ReverseGeocode.java | 166 ------------ .../android/TravelPost/TravelPostWidget.java | 4 +- 9 files changed, 52 insertions(+), 416 deletions(-) create mode 100644 TravelPost/.codepro/deadCodeEntryPoints.xml delete mode 100644 TravelPost/src/com/TwentyCodes/android/TravelPost/LocationService.java delete mode 100644 TravelPost/src/com/TwentyCodes/android/TravelPost/ReverseGeocode.java diff --git a/TravelPost/.classpath b/TravelPost/.classpath index b450f30..a66f012 100644 --- a/TravelPost/.classpath +++ b/TravelPost/.classpath @@ -5,5 +5,11 @@ + + + + + + diff --git a/TravelPost/.codepro/deadCodeEntryPoints.xml b/TravelPost/.codepro/deadCodeEntryPoints.xml new file mode 100644 index 0000000..814d580 --- /dev/null +++ b/TravelPost/.codepro/deadCodeEntryPoints.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/TravelPost/.project b/TravelPost/.project index 09a8d0b..68cba3e 100644 --- a/TravelPost/.project +++ b/TravelPost/.project @@ -36,5 +36,10 @@ 2 _android_FacebookLib_9c84f9bf/src + + LocationLib_src + 2 + _android_LocationLib_84551a70/src + diff --git a/TravelPost/default.properties b/TravelPost/default.properties index 3d3665e..af7b5fb 100644 --- a/TravelPost/default.properties +++ b/TravelPost/default.properties @@ -12,3 +12,4 @@ target=Google Inc.:Google APIs:9 proguard.config=proguard.cfg android.library.reference.1=../FacebookLib/ +android.library.reference.2=../LocationLib diff --git a/TravelPost/gen/com/TwentyCodes/android/TravelPost/R.java b/TravelPost/gen/com/TwentyCodes/android/TravelPost/R.java index a81f12e..0c7329c 100644 --- a/TravelPost/gen/com/TwentyCodes/android/TravelPost/R.java +++ b/TravelPost/gen/com/TwentyCodes/android/TravelPost/R.java @@ -13,23 +13,35 @@ public final class R { public static final class drawable { public static final int facebook_icon=0x7f020000; public static final int icon=0x7f020001; - public static final int sign_post_icon=0x7f020002; - public static final int sign_post_icon_mod=0x7f020003; + public static final int logoidea3=0x7f020002; + public static final int sign_post_icon=0x7f020003; + public static final int sign_post_icon_mod=0x7f020004; + public static final int skyhook_logo=0x7f020005; + public static final int skyhook_logo_alpha=0x7f020006; + public static final int skyhook_logo_alpha_small=0x7f020007; + public static final int tc_logo_complete=0x7f020008; + public static final int user=0x7f020009; } public static final class id { - public static final int authCode=0x7f060002; - public static final int authOkButton=0x7f060001; - public static final int authWebView=0x7f060003; - public static final int widgetbutton=0x7f060000; + public static final int authCode=0x7f060005; + public static final int authOkButton=0x7f060004; + public static final int authWebView=0x7f060006; + public static final int skyhook_img=0x7f060000; + public static final int tc_img=0x7f060002; + public static final int text=0x7f060001; + public static final int widgetbutton=0x7f060003; } public static final class layout { public static final int main=0x7f030000; - public static final int travelpostwidget=0x7f030001; - public static final int webauth=0x7f030002; + public static final int powered_by_skyhook=0x7f030001; + public static final int travelpostwidget=0x7f030002; + public static final int webauth=0x7f030003; } public static final class string { - public static final int app_name=0x7f050001; - public static final int hello=0x7f050000; + public static final int app_name=0x7f050000; + public static final int gps_fix=0x7f050001; + public static final int hello=0x7f050003; + public static final int sorry_theres_trouble=0x7f050002; } public static final class xml { public static final int settings=0x7f040000; diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java index 0087e2d..c198afb 100644 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java @@ -6,6 +6,8 @@ */ package com.TwentyCodes.android.TravelPost; +import com.TwentyCodes.android.location.ReverseGeocoder; + import twitter4j.TwitterException; import android.content.BroadcastReceiver; import android.content.Context; @@ -47,7 +49,7 @@ public class LocationReceiver extends BroadcastReceiver{ //TODO something with the location i.e. report location to social services like twitter, ect... try { - Log.d(TravelPost.TAG, TwitterServices.tweet(mContext, ReverseGeocode.getAddressFromLocation(location)).toString()); + Log.d(TravelPost.TAG, TwitterServices.tweet(mContext, ReverseGeocoder.getAddressFromLocation(location)).toString()); } catch (TwitterException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationService.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationService.java deleted file mode 100644 index 4eb0474..0000000 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationService.java +++ /dev/null @@ -1,238 +0,0 @@ -/** - * LocationService.java - * @date Jan 21, 2011 - * @author ricky barrette - * @author Twenty Codes, LLC - */ -package com.TwentyCodes.android.TravelPost; - -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.Service; -import android.content.Context; -import android.content.Intent; -import android.location.Location; -import android.location.LocationListener; -import android.location.LocationManager; -import android.os.Bundle; -import android.os.Handler; -import android.os.IBinder; -import android.os.PowerManager; -import android.os.PowerManager.WakeLock; -import android.util.Log; - -/** - * A service that will gather the user's location in the background, and report it via Broadcast once the location gathered has reached the - * @author ricky barrette - */ -public class LocationService extends Service implements LocationListener { - - /** - * The desired accuracy in meters - */ - private static final float DESIRED_ACCURACY = 12.0f; - - /** - * Maximum running time in milliseconds - */ - private final long MAX_RUN_TIME = 180000L; - - private static final int SIMPLE_NOTFICATION_ID = 234098752; - private LocationManager mLocationManager; - private WakeLock mWakeLock; - private Location mLocation; - private int mStartId; - private Notification mNotification; - private NotificationManager mNotificationManager; - - /** - * this runnable will be qued when the service is created. this will be used as a fail safe - * @author ricky barrette - */ - private Runnable mFailSafe = new Runnable() { - @Override - public void run(){ - stopSelf(mStartId); - } - }; - - /** - * a convince method for starting the service - * @param context - * @return a runnable that will start the service - * @author ricky barrette - */ - public static Intent startService(final Context context){ - return new Intent(context, LocationService.class); - } - - /** - * a convince method for stopping the service - * @param context - * @return a runnable that will stop the service - * @author ricky barrette - */ - public static Runnable stopService(final Context context){ - return new Runnable(){ - @Override - public void run(){ - context.stopService(new Intent(context, LocationService.class)); - } - }; - } - - /** - * broadcasts location to anything listening for updates, - * since this is the last function of the service, we call finish()u - * @author ricky barrette - */ - private void broadcastLocation() { - Log.v(TravelPost.TAG, "broadcastLocation()"); - if (mLocation != null) { - Intent locationUpdate = new Intent(); - locationUpdate.setAction(LocationReceiver.ACTION_UPDATE); - locationUpdate.putExtra(LocationReceiver.LOCATION_PARCEL, mLocation); - sendBroadcast(locationUpdate); - } - } - - /** - * (non-Javadoc) - * @see android.app.Service#onBind(android.content.Intent) - * @param arg0 - * @return - * @author ricky barrette - */ - @Override - public IBinder onBind(Intent arg0) { - // UNUSED - return null; - } - - /** - * called when the service is created. this will initialize the location manager, and acquire a wakelock - * (non-Javadoc) - * @see android.app.Service#onCreate() - * @author ricky barrette - */ - @Override - public void onCreate(){ - Log.v(TravelPost.TAG, "onCreate()"); - mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); - PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); - mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TravelPost.TAG); - mWakeLock.acquire(); - - //display notification - PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, TravelPost.class), android.content.Intent.FLAG_ACTIVITY_NEW_TASK); - mNotification = new Notification(R.drawable.icon, "Getting Location", System.currentTimeMillis()); - mNotification.flags |= Notification.FLAG_ONGOING_EVENT; - mNotification.setLatestEventInfo(this, "Travel Post", "Gathering location...", intent); - mNotificationManager.notify(SIMPLE_NOTFICATION_ID, mNotification); - - /* - * que the fail safe runnable to kill the report location and kill it self after the MAX_RUN_TIME has been meet - */ - new Handler().postDelayed(mFailSafe, MAX_RUN_TIME); - } - - /** - * called when the service is destroyed. - * this will remove any wakelock or location service running, and register to be waken back up - * (non-Javadoc) - * @see android.app.Service#onDestroy() - * @author ricky barrette - */ - @Override - public void onDestroy(){ - broadcastLocation(); - mLocationManager.removeUpdates(this); - mNotificationManager.cancel(SIMPLE_NOTFICATION_ID); - if(mWakeLock.isHeld()) - mWakeLock.release(); - } - - /** - * (non-Javadoc) - * @see android.location.LocationListener#onLocationChanged(android.location.Location) - * @param location - * @author ricky barrette - */ - @Override - public void onLocationChanged(Location location) { -// Log.d(TAG, "got location +- "+ location.getAccuracy() +"m"); - mLocation = location; - if(location.getAccuracy() <= DESIRED_ACCURACY){ - stopSelf(mStartId); - } - } - - /** - * (non-Javadoc) - * @see android.location.LocationListener#onProviderDisabled(java.lang.String) - * @param arg0 - * @author ricky barrette - */ - @Override - public void onProviderDisabled(String arg0) { - // UNUSED - } - - /** - * (non-Javadoc) - * @see android.location.LocationListener#onProviderEnabled(java.lang.String) - * @param arg0 - * @author ricky barrette - */ - @Override - public void onProviderEnabled(String arg0) { - // UNUSED - } - - /** - * To keep backwards compatibility we override onStart which is the equivalent of onStartCommand in pre android 2.x - * @author ricky barrette - */ - @Override - public void onStart(Intent intent, int startId) { - Log.i(TravelPost.TAG, "onStart.Service started with start id of: " + startId); - mStartId = startId; - startLocationService(); - } - - /** - * This method is called when startService is called. only used in 2.x android. - * @author ricky barrette - */ - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - Log.i(TravelPost.TAG , "onStartCommand.Service started with start id of: " + startId); - mStartId = startId; - startLocationService(); - return START_STICKY; - } - - - /** - * (non-Javadoc) - * @see android.location.LocationListener#onStatusChanged(java.lang.String, int, android.os.Bundle) - * @author ricky barrette - */ - @Override - public void onStatusChanged(String arg0, int arg1, Bundle arg2) { - // UNUSED - } - - /** - * request periodic location updates from androids location services - * @author ricky barrette - */ - private void startLocationService() { - mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); - mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); - mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, this); - } - -} \ No newline at end of file diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/ReverseGeocode.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/ReverseGeocode.java deleted file mode 100644 index efe4687..0000000 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/ReverseGeocode.java +++ /dev/null @@ -1,166 +0,0 @@ -/* -Apparently geocoder is not working with the Emulated android. This is a workaround. - -Here is how I implemented it: - -try { - possibleAddresses = g.getFromLocation(location.getLatitude(), location.getLongitude(), 3); -} catch (IOException e) { - if("sdk".equals( Build.PRODUCT )) { - Log.d(TAG, "Geocoder doesn't work under emulation."); - possibleAddresses = ReverseGeocode.getFromLocation(location.getLatitude(), location.getLongitude(), 3); - } else - e.printStackTrace(); -} - -*/ - -package com.TwentyCodes.android.TravelPost; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import android.location.Address; -import android.location.Location; -import android.util.Log; - -/** - * Due to this bug http://code.google.com/p/android/issues/detail?id=8816 google's Geocoder class does not function. - * I found this source in one of the comments mentioning that it is a work around - * - * @author ricky barrette - */ -public class ReverseGeocode { - - public static List
getFromLocation(double lat, double lon, int maxResults) { - String urlStr = "http://maps.google.com/maps/geo?q=" + lat + "," + lon + "&output=json&sensor=false"; - String response = ""; - List
results = new ArrayList
(); - HttpClient client = new DefaultHttpClient(); - - Log.d("ReverseGeocode", urlStr); - try { - HttpResponse hr = client.execute(new HttpGet(urlStr)); - HttpEntity entity = hr.getEntity(); - - BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); - - String buff = null; - while ((buff = br.readLine()) != null) - response += buff; - } catch (IOException e) { - e.printStackTrace(); - } - - JSONArray responseArray = null; - try { - JSONObject jsonObject = new JSONObject(response); - responseArray = jsonObject.getJSONArray("Placemark"); - } catch (JSONException e) { - return results; - } - - Log.d("ReverseGeocode", "" + responseArray.length() + " result(s)"); - - for(int i = 0; i < responseArray.length() && i < maxResults-1; i++) { - Address addy = new Address(Locale.getDefault()); - - try { - JSONObject jsl = responseArray.getJSONObject(i); - - String addressLine = jsl.getString("address"); - - if(addressLine.contains(",")) - addressLine = addressLine.split(",")[0]; - - addy.setAddressLine(0, addressLine); - - jsl = jsl.getJSONObject("AddressDetails").getJSONObject("Country"); - addy.setCountryName(jsl.getString("CountryName")); - addy.setCountryCode(jsl.getString("CountryNameCode")); - - jsl = jsl.getJSONObject("AdministrativeArea"); - addy.setAdminArea(jsl.getString("AdministrativeAreaName")); - - jsl = jsl.getJSONObject("SubAdministrativeArea"); - addy.setSubAdminArea(jsl.getString("SubAdministrativeAreaName")); - - jsl = jsl.getJSONObject("Locality"); - addy.setLocality(jsl.getString("LocalityName")); - - addy.setPostalCode(jsl.getJSONObject("PostalCode").getString("PostalCodeNumber")); - addy.setThoroughfare(jsl.getJSONObject("Thoroughfare").getString("ThoroughfareName")); - - } catch (JSONException e) { - e.printStackTrace(); - continue; - } - - results.add(addy); - } - - return results; - } - - /** - * This is the hacked up method from above the will only return the string address of the first search result - * @param lat - * @param lon - * @return string address, or lat, lon if search fails - * @author ricky barrette - */ - public static String getAddressFromLocation(Location location) { - String urlStr = "http://maps.google.com/maps/geo?q=" + location.getLatitude() + "," + location.getLongitude() + "&output=json&sensor=false"; - String response = ""; - HttpClient client = new DefaultHttpClient(); - - Log.d("ReverseGeocode", urlStr); - try { - HttpResponse hr = client.execute(new HttpGet(urlStr)); - HttpEntity entity = hr.getEntity(); - - BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); - - String buff = null; - while ((buff = br.readLine()) != null) - response += buff; - } catch (IOException e) { - e.printStackTrace(); - } - - Log.d("ReverseGeocode", response); - - - JSONArray responseArray = null; - try { - JSONObject jsonObject = new JSONObject(response); - responseArray = jsonObject.getJSONArray("Placemark"); - } catch (JSONException e) { - return location.getLatitude() +", "+ location.getLongitude() +" ± "+ location.getAccuracy()+"m"; - } - - Log.d("ReverseGeocode", "" + responseArray.length() + " result(s)"); - - try { - JSONObject jsl = responseArray.getJSONObject(0); - return jsl.getString("address"); - } catch (JSONException e) { - e.printStackTrace(); - } - - return location.getLatitude() +", "+ location.getLongitude() +" ± "+ location.getAccuracy()+"m"; - } -} \ No newline at end of file diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostWidget.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostWidget.java index dd7c54a..cced080 100644 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostWidget.java +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostWidget.java @@ -5,6 +5,8 @@ */ package com.TwentyCodes.android.TravelPost; +import com.TwentyCodes.android.location.LocationService; + import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; @@ -46,7 +48,7 @@ public class TravelPostWidget extends AppWidgetProvider { int appWidgetId = appWidgetIds[i]; // Create a pending intent to start the location service; - PendingIntent pendingIntent = PendingIntent.getService(context, 0, LocationService.startService(context), 0); + PendingIntent pendingIntent = PendingIntent.getService(context, 0, LocationService.getStartServiceIntent(context, LocationReceiver.ACTION_UPDATE), 0); // Get the layout for the App Widget and attach an on-click listener to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.travelpostwidget);