diff --git a/TravelPost/ChangeLog.txt b/TravelPost/ChangeLog.txt index e69de29..139597f 100644 --- a/TravelPost/ChangeLog.txt +++ b/TravelPost/ChangeLog.txt @@ -0,0 +1,2 @@ + + diff --git a/TravelPost/default.properties b/TravelPost/default.properties index 558b85b..77f7ad7 100644 --- a/TravelPost/default.properties +++ b/TravelPost/default.properties @@ -9,4 +9,3 @@ # Project target. target=Google Inc.:Google APIs:9 -proguard.config=proguard.cfg diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java index e77d152..0087e2d 100644 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java @@ -14,7 +14,6 @@ import android.location.Location; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.util.Log; -import android.widget.Toast; /** * The broadcast receiver that works with LocationService.java @@ -46,12 +45,14 @@ public class LocationReceiver extends BroadcastReceiver{ */ private void onLocationUpdate(Location location) { //TODO something with the location i.e. report location to social services like twitter, ect... + try { - Log.d(TravelPost.TAG, TwitterServices.tweet(mContext, location.getLatitude() +", "+ location.getLongitude() +" ± "+ location.getAccuracy()+"m").toString()); + Log.d(TravelPost.TAG, TwitterServices.tweet(mContext, ReverseGeocode.getAddressFromLocation(location)).toString()); } catch (TwitterException e) { - Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG); + // TODO Auto-generated catch block e.printStackTrace(); } + removeWakeLock(); } diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/ReverseGeocode.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/ReverseGeocode.java new file mode 100644 index 0000000..efe4687 --- /dev/null +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/ReverseGeocode.java @@ -0,0 +1,166 @@ +/* +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/TwitterServices.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/TwitterServices.java index 4874c60..a6dd4c9 100644 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/TwitterServices.java +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/TwitterServices.java @@ -42,8 +42,7 @@ public class TwitterServices { */ public static Status tweet(Context context, String msg) throws TwitterException{ shared_prefs = context.getSharedPreferences(TravelPost.SETTINGS, 0); -// Log.d(TravelPost.TAG, shared_prefs.getString(TWITTER_AUTH_TOKEN, "")); -// Log.d(TravelPost.TAG, shared_prefs.getString(TWITTER_AUTH_SECRET, "")); + Log.d(TravelPost.TAG, "Tweeting: "+ msg); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(TC_TEST_OAUTH_KEY);