aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/Twitter.java
blob: 28da23ee2f800ab2dd8c6b06530b06a239de255f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package cgeo.geocaching;

import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.network.OAuth;

import org.apache.http.HttpResponse;

import android.util.Log;

public final class Twitter {
    public 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()) {
            return;
        }

        try {
            Parameters parameters = new Parameters("status", status);
            if (coords != null) {
                parameters.put(
                        "lat", String.format("%.6f", coords.getLatitude()),
                        "long", String.format("%.6f", coords.getLongitude()),
                        "display_coordinates", "true");
            }

            OAuth.signOAuth("api.twitter.com", "/1/statuses/update.json", "POST", false, parameters, Settings.getTokenPublic(), Settings.getTokenSecret());
            final HttpResponse httpResponse = cgBase.postRequest("http://api.twitter.com/1/statuses/update.json", parameters);
            if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == 200) {
                Log.i(Settings.tag, "Tweet posted");
            } else {
                Log.e(Settings.tag, "Tweet could not be posted");
            }
        } catch (Exception e) {
            Log.e(Settings.tag, "cgBase.postTweet: " + e.toString());
        }
    }

    public static String appendHashTag(final String status, final String tag) {
        String result = status;
        if (result.length() + 2 + tag.length() <= 140) {
            result += " #" + tag;
        }
        return result;
    }
}