diff options
Diffstat (limited to 'main/src/cgeo/geocaching/twitter/Twitter.java')
| -rw-r--r-- | main/src/cgeo/geocaching/twitter/Twitter.java | 97 |
1 files changed, 51 insertions, 46 deletions
diff --git a/main/src/cgeo/geocaching/twitter/Twitter.java b/main/src/cgeo/geocaching/twitter/Twitter.java index c69d560..7233764 100644 --- a/main/src/cgeo/geocaching/twitter/Twitter.java +++ b/main/src/cgeo/geocaching/twitter/Twitter.java @@ -1,7 +1,6 @@ package cgeo.geocaching.twitter; import cgeo.geocaching.Geocache; -import cgeo.geocaching.Settings; import cgeo.geocaching.Trackable; import cgeo.geocaching.cgData; import cgeo.geocaching.cgeoapplication; @@ -11,18 +10,29 @@ import cgeo.geocaching.geopoint.GeopointFormatter.Format; import cgeo.geocaching.network.Network; import cgeo.geocaching.network.OAuth; import cgeo.geocaching.network.Parameters; +import cgeo.geocaching.settings.Settings; import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.HttpResponse; +import org.apache.commons.lang3.StringUtils; + public final class Twitter { - public static final int MAX_TWEET_SIZE = 140; + private static final String HASH_PREFIX_WITH_BLANK = " #"; + private static final int MAX_TWEET_SIZE = 140; - public static void postTweet(final cgeoapplication app, final String status, final Geopoint coords) { - if (app == null) { - return; - } - if (!Settings.isTwitterLoginValid()) { + public static void postTweetCache(String geocode) { + final Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); + postTweet(cgeoapplication.getInstance(), getStatusMessage(cache), null); + } + + public static void postTweetTrackable(String geocode) { + final Trackable trackable = cgData.loadTrackable(geocode); + postTweet(cgeoapplication.getInstance(), getStatusMessage(trackable), null); + } + + private static void postTweet(final cgeoapplication app, final String status, final Geopoint coords) { + if (app == null || !Settings.isUseTwitter() || !Settings.isTwitterLoginValid()) { return; } @@ -35,61 +45,56 @@ public final class Twitter { "display_coordinates", "true"); } - OAuth.signOAuth("api.twitter.com", "/1.1/statuses/update.json", "POST", false, parameters, Settings.getTokenPublic(), Settings.getTokenSecret()); + OAuth.signOAuth("api.twitter.com", "/1.1/statuses/update.json", "POST", false, parameters, Settings.getTokenPublic(), Settings.getTokenSecret(), Settings.getKeyConsumerPublic(), Settings.getKeyConsumerSecret()); final HttpResponse httpResponse = Network.postRequest("http://api.twitter.com/1.1/statuses/update.json", parameters); - if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == 200) { - Log.i("Tweet posted"); + if (httpResponse != null) { + if (httpResponse.getStatusLine().getStatusCode() == 200) { + Log.i("Tweet posted"); + } else { + Log.e("Tweet could not be posted. Reason: " + httpResponse.toString()); + } } else { - Log.e("Tweet could not be posted"); + Log.e("Tweet could not be posted. Reason: httpResponse Object is null"); } } catch (Exception e) { - Log.e("cgBase.postTweet", e); + Log.e("Twitter.postTweet", e); } } - public static String appendHashTag(final String status, final String tag) { - String result = status; - if (result.length() + 2 + tag.length() <= 140) { - result += " #" + tag; + private static void appendHashTag(final StringBuilder status, final String tag) { + if (status.length() + HASH_PREFIX_WITH_BLANK.length() + tag.length() <= MAX_TWEET_SIZE) { + final String tagWithPrefix = HASH_PREFIX_WITH_BLANK + tag; + if (status.indexOf(tagWithPrefix, 0) == -1) { + status.append(tagWithPrefix); + } } - return result; } - public static void postTweetCache(String geocode) { - final Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); - String status; - final String url = cache.getUrl(); - if (url.length() >= 100) { - status = "I found " + url; - } - else { - String name = cache.getName(); - status = "I found " + name + " (" + url + ")"; - if (status.length() > MAX_TWEET_SIZE) { - name = name.substring(0, name.length() - (status.length() - MAX_TWEET_SIZE) - 1) + '…'; + static String getStatusMessage(Geocache cache) { + String name = cache.getName(); + if (name.length() > 100) { + name = name.substring(0, 100) + '…'; } - status = "I found " + name + " (" + url + ")"; - status = appendHashTag(status, "cgeo"); - status = appendHashTag(status, "geocaching"); - } - - postTweet(cgeoapplication.getInstance(), status, null); + final String url = StringUtils.defaultString(cache.getUrl()); + return fillTemplate(Settings.getCacheTwitterMessage(), name, url); } - public static void postTweetTrackable(String geocode) { - final Trackable trackable = cgData.loadTrackable(geocode); + static String getStatusMessage(Trackable trackable) { String name = trackable.getName(); if (name.length() > 82) { name = name.substring(0, 81) + '…'; } - StringBuilder builder = new StringBuilder("I touched "); - builder.append(name); - if (trackable.getUrl() != null) { - builder.append(" (").append(trackable.getUrl()).append(')'); - } - builder.append('!'); - String status = appendHashTag(builder.toString(), "cgeo"); - status = appendHashTag(status, "geocaching"); - postTweet(cgeoapplication.getInstance(), status, null); + String url = StringUtils.defaultString(trackable.getUrl()); + String status = Settings.getTrackableTwitterMessage(); + return fillTemplate(status, name, url); + } + + private static String fillTemplate(String template, String name, final String url) { + String result = StringUtils.replace(template, "[NAME]", name); + result = StringUtils.replace(result, "[URL]", url); + StringBuilder builder = new StringBuilder(result); + appendHashTag(builder, "cgeo"); + appendHashTag(builder, "geocaching"); + return builder.toString(); } } |
