diff options
-rw-r--r-- | main/src/cgeo/geocaching/Twitter.java | 129 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/apps/AbstractLocusApp.java | 2 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 133 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgCache.java | 23 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgTrackable.java | 4 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgWaypoint.java | 4 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeodetail.java | 33 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/connector/GCConnector.java | 3 |
8 files changed, 188 insertions, 143 deletions
diff --git a/main/src/cgeo/geocaching/Twitter.java b/main/src/cgeo/geocaching/Twitter.java new file mode 100644 index 0000000..a1acbfb --- /dev/null +++ b/main/src/cgeo/geocaching/Twitter.java @@ -0,0 +1,129 @@ +package cgeo.geocaching; + +import cgeo.geocaching.geopoint.Geopoint; + +import org.apache.commons.lang3.StringUtils; + +import android.util.Log; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.util.HashMap; +import java.util.Map; +import java.util.zip.GZIPInputStream; +import java.util.zip.Inflater; +import java.util.zip.InflaterInputStream; + +public final class Twitter { + public static final int MAX_TWEET_SIZE = 140; + + public static void postTweet(cgeoapplication app, cgSettings settings, String status, final Geopoint coords) { + if (app == null) { + return; + } + if (settings == null || StringUtils.isBlank(settings.tokenPublic) || StringUtils.isBlank(settings.tokenSecret)) { + return; + } + + try { + Map<String, String> parameters = new HashMap<String, String>(); + + parameters.put("status", status); + if (coords != null) { + parameters.put("lat", String.format("%.6f", coords.getLatitude())); + parameters.put("long", String.format("%.6f", coords.getLongitude())); + parameters.put("display_coordinates", "true"); + } + + final String paramsDone = cgOAuth.signOAuth("api.twitter.com", "/1/statuses/update.json", "POST", false, parameters, settings.tokenPublic, settings.tokenSecret); + + HttpURLConnection connection = null; + try { + final StringBuffer buffer = new StringBuffer(); + final URL u = new URL("http://api.twitter.com/1/statuses/update.json"); + final URLConnection uc = u.openConnection(); + + uc.setRequestProperty("Host", "api.twitter.com"); + + connection = (HttpURLConnection) uc; + connection.setReadTimeout(30000); + connection.setRequestMethod("POST"); + HttpURLConnection.setFollowRedirects(true); + connection.setDoInput(true); + connection.setDoOutput(true); + + final OutputStream out = connection.getOutputStream(); + final OutputStreamWriter wr = new OutputStreamWriter(out); + wr.write(paramsDone); + wr.flush(); + wr.close(); + + Log.i(cgSettings.tag, "Twitter.com: " + connection.getResponseCode() + " " + connection.getResponseMessage()); + + InputStream ins; + final String encoding = connection.getContentEncoding(); + + if (encoding != null && encoding.equalsIgnoreCase("gzip")) { + ins = new GZIPInputStream(connection.getInputStream()); + } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { + ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true)); + } else { + ins = connection.getInputStream(); + } + + final InputStreamReader inr = new InputStreamReader(ins); + final BufferedReader br = new BufferedReader(inr); + + readIntoBuffer(br, buffer); + + br.close(); + ins.close(); + inr.close(); + connection.disconnect(); + } catch (IOException e) { + Log.e(cgSettings.tag, "cgBase.postTweet.IO: " + connection.getResponseCode() + ": " + connection.getResponseMessage() + " ~ " + e.toString()); + + final InputStream ins = connection.getErrorStream(); + final StringBuffer buffer = new StringBuffer(); + final InputStreamReader inr = new InputStreamReader(ins); + final BufferedReader br = new BufferedReader(inr); + + readIntoBuffer(br, buffer); + + br.close(); + ins.close(); + inr.close(); + } catch (Exception e) { + Log.e(cgSettings.tag, "cgBase.postTweet.inner: " + e.toString()); + } + + connection.disconnect(); + } catch (Exception e) { + Log.e(cgSettings.tag, "cgBase.postTweet: " + e.toString()); + } + } + + private static void readIntoBuffer(BufferedReader br, StringBuffer buffer) throws IOException { + final int bufferSize = 1024 * 16; + final char[] bytes = new char[bufferSize]; + int bytesRead; + while ((bytesRead = br.read(bytes)) > 0) { + buffer.append(bytes, 0, bytesRead); + } + } + + public static String appendHashTag(final String status, final String tag) { + String result = status; + if (result.length() + 2 + tag.length() <= 140) { + result += " #" + tag; + } + return result; + } +} diff --git a/main/src/cgeo/geocaching/apps/AbstractLocusApp.java b/main/src/cgeo/geocaching/apps/AbstractLocusApp.java index 3e79998..e56cce7 100644 --- a/main/src/cgeo/geocaching/apps/AbstractLocusApp.java +++ b/main/src/cgeo/geocaching/apps/AbstractLocusApp.java @@ -188,7 +188,7 @@ public abstract class AbstractLocusApp extends AbstractApp { loc.setLongitude(waypoint.coords.getLongitude()); Point p = new Point(waypoint.name, loc); - p.setDescription("<a href=\"http://coord.info/" + waypoint.geocode + "\">" + p.setDescription("<a href=\"" + waypoint.getUrl() + "\">" + waypoint.geocode + "</a>"); return p; diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index f4505ac..d048a44 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -57,20 +57,12 @@ import android.text.style.StrikethroughSpan; import android.util.Log; import android.widget.EditText; -import java.io.BufferedReader; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; -import java.net.URL; -import java.net.URLConnection; import java.net.URLDecoder; import java.net.URLEncoder; import java.security.cert.CertificateException; @@ -91,9 +83,6 @@ import java.util.Set; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.zip.GZIPInputStream; -import java.util.zip.Inflater; -import java.util.zip.InflaterInputStream; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; @@ -3358,13 +3347,23 @@ public class cgBase { public static void postTweetCache(cgeoapplication app, cgSettings settings, String geocode) { final cgCache cache = app.getCacheByGeocode(geocode); - String name = cache.name; - if (name.length() > 84) { - name = name.substring(0, 81) + "..."; + String status; + final String url = cache.getUrl(); + if (url.length() >= 100) { + status = "I found " + url; + } + else { + String name = cache.name; + status = "I found " + name + " (" + url + ")"; + if (status.length() > Twitter.MAX_TWEET_SIZE) { + name = name.substring(0, name.length() - (status.length() - Twitter.MAX_TWEET_SIZE) - 3) + "..."; + } + status = "I found " + name + " (" + url + ")"; + status = Twitter.appendHashTag(status, "cgeo"); + status = Twitter.appendHashTag(status, "geocaching"); } - final String status = "I found " + name + " (http://coord.info/" + cache.geocode.toUpperCase() + ")! #cgeo #geocaching"; // 56 chars + cache name - postTweet(app, settings, status, null); + Twitter.postTweet(app, settings, status, null); } public static void postTweetTrackable(cgeoapplication app, cgSettings settings, String geocode) { @@ -3373,104 +3372,10 @@ public class cgBase { if (name.length() > 82) { name = name.substring(0, 79) + "..."; } - final String status = "I touched " + name + " (http://coord.info/" + trackable.geocode.toUpperCase() + ")! #cgeo #geocaching"; // 58 chars + trackable name - - postTweet(app, settings, status, null); - } - - public static void postTweet(cgeoapplication app, cgSettings settings, String status, final Geopoint coords) { - if (app == null) { - return; - } - if (settings == null || StringUtils.isBlank(settings.tokenPublic) || StringUtils.isBlank(settings.tokenSecret)) { - return; - } - - try { - Map<String, String> parameters = new HashMap<String, String>(); - - parameters.put("status", status); - if (coords != null) { - parameters.put("lat", String.format("%.6f", coords.getLatitude())); - parameters.put("long", String.format("%.6f", coords.getLongitude())); - parameters.put("display_coordinates", "true"); - } - - final String paramsDone = cgOAuth.signOAuth("api.twitter.com", "/1/statuses/update.json", "POST", false, parameters, settings.tokenPublic, settings.tokenSecret); - - HttpURLConnection connection = null; - try { - final StringBuffer buffer = new StringBuffer(); - final URL u = new URL("http://api.twitter.com/1/statuses/update.json"); - final URLConnection uc = u.openConnection(); - - uc.setRequestProperty("Host", "api.twitter.com"); - - connection = (HttpURLConnection) uc; - connection.setReadTimeout(30000); - connection.setRequestMethod("POST"); - HttpURLConnection.setFollowRedirects(true); - connection.setDoInput(true); - connection.setDoOutput(true); - - final OutputStream out = connection.getOutputStream(); - final OutputStreamWriter wr = new OutputStreamWriter(out); - wr.write(paramsDone); - wr.flush(); - wr.close(); - - Log.i(cgSettings.tag, "Twitter.com: " + connection.getResponseCode() + " " + connection.getResponseMessage()); - - InputStream ins; - final String encoding = connection.getContentEncoding(); - - if (encoding != null && encoding.equalsIgnoreCase("gzip")) { - ins = new GZIPInputStream(connection.getInputStream()); - } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { - ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true)); - } else { - ins = connection.getInputStream(); - } - - final InputStreamReader inr = new InputStreamReader(ins); - final BufferedReader br = new BufferedReader(inr); - - readIntoBuffer(br, buffer); - - br.close(); - ins.close(); - inr.close(); - connection.disconnect(); - } catch (IOException e) { - Log.e(cgSettings.tag, "cgBase.postTweet.IO: " + connection.getResponseCode() + ": " + connection.getResponseMessage() + " ~ " + e.toString()); - - final InputStream ins = connection.getErrorStream(); - final StringBuffer buffer = new StringBuffer(); - final InputStreamReader inr = new InputStreamReader(ins); - final BufferedReader br = new BufferedReader(inr); - - readIntoBuffer(br, buffer); - - br.close(); - ins.close(); - inr.close(); - } catch (Exception e) { - Log.e(cgSettings.tag, "cgBase.postTweet.inner: " + e.toString()); - } - - connection.disconnect(); - } catch (Exception e) { - Log.e(cgSettings.tag, "cgBase.postTweet: " + e.toString()); - } - } - - private static void readIntoBuffer(BufferedReader br, StringBuffer buffer) throws IOException { - final int bufferSize = 1024 * 16; - final char[] bytes = new char[bufferSize]; - int bytesRead; - while ((bytesRead = br.read(bytes)) > 0) { - buffer.append(bytes, 0, bytesRead); - } + String status = "I touched " + name + " (" + trackable.getUrl() + ")!"; + status = Twitter.appendHashTag(status, "cgeo"); + status = Twitter.appendHashTag(status, "geocaching"); + Twitter.postTweet(app, settings, status, null); } public static String getLocalIpAddress() { diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index 8bea2e3..6fe7331 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -466,4 +466,27 @@ public class cgCache implements ICache { return getConnector().supportsCachesAround(); } + public void shareCache(Activity fromActivity, Resources res) { + if (geocode == null) { + return; + } + + StringBuilder subject = new StringBuilder("Geocache "); + subject.append(geocode.toUpperCase()); + if (StringUtils.isNotBlank(name)) { + subject.append(" - ").append(name); + } + + final Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/plain"); + intent.putExtra(Intent.EXTRA_SUBJECT, subject.toString()); + intent.putExtra(Intent.EXTRA_TEXT, getUrl()); + + fromActivity.startActivity(Intent.createChooser(intent, res.getText(R.string.action_bar_share_title))); + } + + public String getUrl() { + return getConnector().getCacheUrl(this); + } + } diff --git a/main/src/cgeo/geocaching/cgTrackable.java b/main/src/cgeo/geocaching/cgTrackable.java index a8e4621..2cdca39 100644 --- a/main/src/cgeo/geocaching/cgTrackable.java +++ b/main/src/cgeo/geocaching/cgTrackable.java @@ -34,4 +34,8 @@ public class cgTrackable { public String details = null; public String image = null; public List<cgLog> logs = new ArrayList<cgLog>(); + + public String getUrl() { + return "http://coord.info/" + geocode.toUpperCase(); + } } diff --git a/main/src/cgeo/geocaching/cgWaypoint.java b/main/src/cgeo/geocaching/cgWaypoint.java index 1291bba..61c2077 100644 --- a/main/src/cgeo/geocaching/cgWaypoint.java +++ b/main/src/cgeo/geocaching/cgWaypoint.java @@ -142,4 +142,8 @@ public class cgWaypoint implements Comparable<cgWaypoint> { this.prefix = prefix; cachedOrder = null; } + + public String getUrl() { + return "http://coord.info/" + geocode.toUpperCase(); + } }
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/cgeodetail.java b/main/src/cgeo/geocaching/cgeodetail.java index 676d719..3f05c82 100644 --- a/main/src/cgeo/geocaching/cgeodetail.java +++ b/main/src/cgeo/geocaching/cgeodetail.java @@ -560,8 +560,11 @@ public class cgeodetail extends AbstractActivity { addToCalendar(); return true; } else if (menuItem == 12) { - shareCache(); - return true; + if (cache != null) { + cache.shareCache(this, res); + return true; + } + return false; } if (NavigationAppFactory.onMenuItemSelected(item, geo, this, res, cache, searchId, null, null)) { return true; @@ -1504,8 +1507,7 @@ public class cgeodetail extends AbstractActivity { eventDate.setSeconds(0); StringBuilder description = new StringBuilder(); - description.append("http://coord.info/"); - description.append(cache.geocode.toUpperCase()); + description.append(cache.getUrl()); description.append("\n\n"); if (StringUtils.isNotBlank(cache.shortdesc)) { description.append(Html.fromHtml(cache.shortdesc).toString()); @@ -1572,29 +1574,6 @@ public class cgeodetail extends AbstractActivity { startActivity(navigateIntent); } - public void shareCache() { - if (geocode == null && cache == null) { - return; - } - - final Intent intent = new Intent(Intent.ACTION_SEND); - intent.setType("text/plain"); - - if (cache != null && cache.geocode != null) { - String subject = cache.geocode.toUpperCase(); - if (StringUtils.isNotBlank(cache.name)) { - subject = subject + " - " + cache.name; - } - intent.putExtra(Intent.EXTRA_SUBJECT, "Geocache " + subject); - intent.putExtra(Intent.EXTRA_TEXT, "http://coord.info/" + cache.geocode.toUpperCase()); - } else if (geocode != null) { - intent.putExtra(Intent.EXTRA_SUBJECT, "Geocache " + geocode.toUpperCase()); - intent.putExtra(Intent.EXTRA_TEXT, "http://coord.info/" + geocode.toUpperCase()); - } - - startActivity(Intent.createChooser(intent, res.getText(R.string.action_bar_share_title))); - } - private class waypointInfo implements View.OnClickListener { private int id = -1; diff --git a/main/src/cgeo/geocaching/connector/GCConnector.java b/main/src/cgeo/geocaching/connector/GCConnector.java index 27f3aae..14d619e 100644 --- a/main/src/cgeo/geocaching/connector/GCConnector.java +++ b/main/src/cgeo/geocaching/connector/GCConnector.java @@ -31,7 +31,8 @@ public class GCConnector extends AbstractConnector implements IConnector { @Override public String getCacheUrl(cgCache cache) { - return "http://www.geocaching.com/seek/cache_details.aspx?wp=" + cache.geocode; + // it would also be possible to use "http://www.geocaching.com/seek/cache_details.aspx?wp=" + cache.geocode; + return "http://coord.info/" + cache.geocode; } @Override |