aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/twitter
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2011-12-04 08:09:00 +0100
committerBananeweizen <bananeweizen@gmx.de>2011-12-04 08:09:00 +0100
commite5f89f9a9a94f2c38abd249a7b000f2a4c408582 (patch)
treefc6937139fe71db2ed35d3812721fe0e56eb8e6d /main/src/cgeo/geocaching/twitter
parent8a89f929f181289e7b62b8ae7ad27801c8fd9d31 (diff)
downloadcgeo-e5f89f9a9a94f2c38abd249a7b000f2a4c408582.zip
cgeo-e5f89f9a9a94f2c38abd249a7b000f2a4c408582.tar.gz
cgeo-e5f89f9a9a94f2c38abd249a7b000f2a4c408582.tar.bz2
refactoring: move twitter stuff into separate package
Diffstat (limited to 'main/src/cgeo/geocaching/twitter')
-rw-r--r--main/src/cgeo/geocaching/twitter/Twitter.java54
-rw-r--r--main/src/cgeo/geocaching/twitter/TwitterAuthorizationActivity.java288
2 files changed, 342 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/twitter/Twitter.java b/main/src/cgeo/geocaching/twitter/Twitter.java
new file mode 100644
index 0000000..2f653f5
--- /dev/null
+++ b/main/src/cgeo/geocaching/twitter/Twitter.java
@@ -0,0 +1,54 @@
+package cgeo.geocaching.twitter;
+
+import cgeo.geocaching.Parameters;
+import cgeo.geocaching.Settings;
+import cgeo.geocaching.cgBase;
+import cgeo.geocaching.cgeoapplication;
+import cgeo.geocaching.geopoint.Geopoint;
+import cgeo.geocaching.geopoint.GeopointFormatter.Format;
+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", coords.format(Format.LAT_DECDEGREE_RAW),
+ "long", coords.format(Format.LON_DECDEGREE_RAW),
+ "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;
+ }
+}
diff --git a/main/src/cgeo/geocaching/twitter/TwitterAuthorizationActivity.java b/main/src/cgeo/geocaching/twitter/TwitterAuthorizationActivity.java
new file mode 100644
index 0000000..9066dae
--- /dev/null
+++ b/main/src/cgeo/geocaching/twitter/TwitterAuthorizationActivity.java
@@ -0,0 +1,288 @@
+package cgeo.geocaching.twitter;
+
+import cgeo.geocaching.Parameters;
+import cgeo.geocaching.R;
+import cgeo.geocaching.Settings;
+import cgeo.geocaching.cgBase;
+import cgeo.geocaching.activity.AbstractActivity;
+import cgeo.geocaching.network.OAuth;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.util.EntityUtils;
+
+import android.app.ProgressDialog;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.util.Log;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class TwitterAuthorizationActivity extends AbstractActivity {
+ private String OAtoken = null;
+ private String OAtokenSecret = null;
+ private final Pattern paramsPattern1 = Pattern.compile("oauth_token=([a-zA-Z0-9\\-\\_.]+)");
+ private final Pattern paramsPattern2 = Pattern.compile("oauth_token_secret=([a-zA-Z0-9\\-\\_.]+)");
+ private Button startButton = null;
+ private EditText pinEntry = null;
+ private Button pinEntryButton = null;
+ private ProgressDialog requestTokenDialog = null;
+ private ProgressDialog changeTokensDialog = null;
+ private Handler requestTokenHandler = new Handler() {
+
+ @Override
+ public void handleMessage(Message msg) {
+ if (requestTokenDialog != null && requestTokenDialog.isShowing()) {
+ requestTokenDialog.dismiss();
+ }
+
+ startButton.setOnClickListener(new startListener());
+ startButton.setEnabled(true);
+
+ if (msg.what == 1) {
+ startButton.setText(res.getString(R.string.auth_again));
+
+ pinEntry.setVisibility(View.VISIBLE);
+ pinEntryButton.setVisibility(View.VISIBLE);
+ pinEntryButton.setOnClickListener(new confirmPINListener());
+ } else {
+ showToast(res.getString(R.string.err_auth_initialize));
+ startButton.setText(res.getString(R.string.auth_start));
+ }
+ }
+ };
+ private Handler changeTokensHandler = new Handler() {
+
+ @Override
+ public void handleMessage(Message msg) {
+ if (changeTokensDialog != null && changeTokensDialog.isShowing()) {
+ changeTokensDialog.dismiss();
+ }
+
+ pinEntryButton.setOnClickListener(new confirmPINListener());
+ pinEntryButton.setEnabled(true);
+
+ if (msg.what == 1) {
+ showToast(res.getString(R.string.auth_dialog_completed));
+
+ pinEntryButton.setVisibility(View.GONE);
+
+ finish();
+ } else {
+ showToast(res.getString(R.string.err_auth_process));
+
+ pinEntry.setVisibility(View.GONE);
+ pinEntryButton.setVisibility(View.GONE);
+ startButton.setText(res.getString(R.string.auth_start));
+ }
+ }
+ };
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // init
+ app.setAction("setting up");
+
+ setTheme();
+ setContentView(R.layout.auth);
+ setTitle(res.getString(R.string.auth_twitter));
+
+ init();
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+
+ }
+
+ private void init() {
+ startButton = (Button) findViewById(R.id.start);
+ pinEntry = (EditText) findViewById(R.id.pin);
+ pinEntryButton = (Button) findViewById(R.id.pin_button);
+
+ OAtoken = prefs.getString("temp-token-public", null);
+ OAtokenSecret = prefs.getString("temp-token-secret", null);
+
+ startButton.setEnabled(true);
+ startButton.setOnClickListener(new startListener());
+
+ if (StringUtils.isBlank(OAtoken) && StringUtils.isBlank(OAtokenSecret)) {
+ // start authorization process
+ startButton.setText(res.getString(R.string.auth_start));
+ } else {
+ // already have temporary tokens, continue from pin
+ startButton.setText(res.getString(R.string.auth_again));
+
+ pinEntry.setVisibility(View.VISIBLE);
+ pinEntryButton.setVisibility(View.VISIBLE);
+ pinEntryButton.setOnClickListener(new confirmPINListener());
+ }
+ }
+
+ private void requestToken() {
+ final String host = "api.twitter.com";
+ final String pathRequest = "/oauth/request_token";
+ final String pathAuthorize = "/oauth/authorize";
+ final String method = "GET";
+
+ int status = 0;
+ try {
+ final Parameters params = new Parameters();
+ OAuth.signOAuth(host, pathRequest, method, true, params, null, null);
+ final String line = cgBase.getResponseData(cgBase.request("https://" + host + pathRequest, params, false));
+
+
+ if (StringUtils.isNotBlank(line)) {
+ final Matcher paramsMatcher1 = paramsPattern1.matcher(line);
+ if (paramsMatcher1.find()) {
+ OAtoken = paramsMatcher1.group(1);
+ }
+ final Matcher paramsMatcher2 = paramsPattern2.matcher(line);
+ if (paramsMatcher2.find()) {
+ OAtokenSecret = paramsMatcher2.group(1);
+ }
+
+ if (StringUtils.isNotBlank(OAtoken) && StringUtils.isNotBlank(OAtokenSecret)) {
+ final SharedPreferences.Editor prefsEdit = getSharedPreferences(Settings.preferences, 0).edit();
+ prefsEdit.putString("temp-token-public", OAtoken);
+ prefsEdit.putString("temp-token-secret", OAtokenSecret);
+ prefsEdit.commit();
+
+ try {
+ final Parameters paramsBrowser = new Parameters();
+ paramsBrowser.put("oauth_callback", "oob");
+ OAuth.signOAuth(host, pathAuthorize, "GET", true, paramsBrowser, OAtoken, OAtokenSecret);
+ final String encodedParams = EntityUtils.toString(new UrlEncodedFormEntity(paramsBrowser));
+ startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://" + host + pathAuthorize + "?" + encodedParams)));
+ status = 1;
+ } catch (Exception e) {
+ Log.e(Settings.tag, "TwitterAuthorizationActivity.requestToken(2): " + e.toString());
+ }
+ }
+ }
+ } catch (Exception e) {
+ Log.e(Settings.tag, "TwitterAuthorizationActivity.requestToken(1): " + e.toString());
+ }
+
+ requestTokenHandler.sendEmptyMessage(status);
+ }
+
+ private void changeToken() {
+ final String host = "api.twitter.com";
+ final String path = "/oauth/access_token";
+ final String method = "POST";
+
+ int status = 0;
+
+ try {
+ final Parameters params = new Parameters("oauth_verifier", pinEntry.getText().toString());
+
+ OAuth.signOAuth(host, path, method, true, params, OAtoken, OAtokenSecret);
+ final String line = StringUtils.defaultString(cgBase.getResponseData(cgBase.postRequest("https://" + host + path, params)));
+
+ OAtoken = "";
+ OAtokenSecret = "";
+
+ final Matcher paramsMatcher1 = paramsPattern1.matcher(line);
+ if (paramsMatcher1.find()) {
+ OAtoken = paramsMatcher1.group(1);
+ }
+ final Matcher paramsMatcher2 = paramsPattern2.matcher(line);
+ if (paramsMatcher2.find() && paramsMatcher2.groupCount() > 0) {
+ OAtokenSecret = paramsMatcher2.group(1);
+ }
+
+ if (StringUtils.isBlank(OAtoken) && StringUtils.isBlank(OAtokenSecret)) {
+ OAtoken = "";
+ OAtokenSecret = "";
+
+ final SharedPreferences.Editor prefs = getSharedPreferences(Settings.preferences, 0).edit();
+ prefs.putString("tokenpublic", null);
+ prefs.putString("tokensecret", null);
+ prefs.putInt("twitter", 0);
+ prefs.commit();
+ } else {
+ final SharedPreferences.Editor prefs = getSharedPreferences(Settings.preferences, 0).edit();
+ prefs.remove("temp-token-public");
+ prefs.remove("temp-token-secret");
+ prefs.putString("tokenpublic", OAtoken);
+ prefs.putString("tokensecret", OAtokenSecret);
+ prefs.putInt("twitter", 1);
+ prefs.commit();
+
+ status = 1;
+ }
+ } catch (Exception e) {
+ Log.e(Settings.tag, "TwitterAuthorizationActivity.changeToken: " + e.toString());
+ }
+
+ changeTokensHandler.sendEmptyMessage(status);
+ }
+
+ private class startListener implements View.OnClickListener {
+
+ public void onClick(View arg0) {
+ if (requestTokenDialog == null) {
+ requestTokenDialog = new ProgressDialog(TwitterAuthorizationActivity.this);
+ requestTokenDialog.setCancelable(false);
+ requestTokenDialog.setMessage(res.getString(R.string.auth_dialog_wait));
+ }
+ requestTokenDialog.show();
+ startButton.setEnabled(false);
+ startButton.setOnTouchListener(null);
+ startButton.setOnClickListener(null);
+
+ final SharedPreferences.Editor prefs = getSharedPreferences(Settings.preferences, 0).edit();
+ prefs.putString("temp-token-public", null);
+ prefs.putString("temp-token-secret", null);
+ prefs.commit();
+
+ (new Thread() {
+
+ @Override
+ public void run() {
+ requestToken();
+ }
+ }).start();
+ }
+ }
+
+ private class confirmPINListener implements View.OnClickListener {
+
+ public void onClick(View arg0) {
+ if (((EditText) findViewById(R.id.pin)).getText().toString().length() == 0) {
+ helpDialog(res.getString(R.string.auth_dialog_pin_title), res.getString(R.string.auth_dialog_pin_message));
+ return;
+ }
+
+ if (changeTokensDialog == null) {
+ changeTokensDialog = new ProgressDialog(TwitterAuthorizationActivity.this);
+ changeTokensDialog.setCancelable(false);
+ changeTokensDialog.setMessage(res.getString(R.string.auth_dialog_wait));
+ }
+ changeTokensDialog.show();
+ pinEntryButton.setEnabled(false);
+ pinEntryButton.setOnTouchListener(null);
+ pinEntryButton.setOnClickListener(null);
+
+ (new Thread() {
+
+ @Override
+ public void run() {
+ changeToken();
+ }
+ }).start();
+ }
+ }
+}