diff --git a/TravelPost/AndroidManifest.xml b/TravelPost/AndroidManifest.xml
index b3e74f5..ca33e1b 100644
--- a/TravelPost/AndroidManifest.xml
+++ b/TravelPost/AndroidManifest.xml
@@ -5,7 +5,8 @@
android:versionCode="1"
android:versionName="1.0">
-
+ * You need to authorize your application first + * @param msg + * @throws TwitterException + * @author ricky barrette + * @return Status + */ + public static Status tweet(Context context, String msg) throws TwitterException{ + shared_prefs = context.getSharedPreferences(TWITTER, 0); + if (Debug.LOGGING) + Log.d(TAG, "Tweeting: "+ msg); + ConfigurationBuilder cb = new ConfigurationBuilder(); + cb.setDebugEnabled(true); + cb.setOAuthConsumerKey(TRAVEL_PORT_OAUTH_KEY); + cb.setOAuthConsumerSecret(TRAVEL_PORT_OAUTH_SECRET); + cb.setOAuthAccessToken(shared_prefs.getString(TWITTER_AUTH_TOKEN, "")); + cb.setOAuthAccessTokenSecret(shared_prefs.getString(TWITTER_AUTH_SECRET, "")); + TwitterFactory tf = new TwitterFactory(cb.build()); + Twitter twitter = tf.getInstance(); + return twitter.updateStatus(msg); + } + + /** + * @return Authorization URL + * @throws TwitterException + * @author ricky barrette + */ + public String getAuthorizationURL() throws TwitterException{ + requestToken = twitter.getOAuthRequestToken(); + return requestToken.getAuthorizationURL(); + } + + /** + * Saves the authorization tokens for future use + * @param authorizationCode + * @return true if save was successful + * @throws TwitterException + * @author ricky barrette + */ + public boolean saveAuthorizationTokens(String authorizationCode) throws TwitterException{ + AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, authorizationCode); + if (Debug.LOGGING) + Log.v(TAG,"saving twitter tokens"); + Editor e = shared_prefs.edit(); + e.putString(TWITTER_AUTH_TOKEN, accessToken.getToken()).commit(); + e.putString(TWITTER_AUTH_SECRET, accessToken.getTokenSecret()).commit(); + return e.commit(); + } + + +} diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/SocialSites/TwitterWebAuth.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/SocialSites/TwitterWebAuth.java new file mode 100644 index 0000000..94be7e2 --- /dev/null +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/SocialSites/TwitterWebAuth.java @@ -0,0 +1,52 @@ +package com.TwentyCodes.android.TravelPost.SocialSites; + +import com.TwentyCodes.android.TravelPost.R; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.view.View.OnClickListener; +import android.webkit.WebView; +import android.widget.EditText; + +/** + * a simple activity that will be started to display the social client auth code for a result (user entered auth code). + * @author ricky barrette + */ +public class TwitterWebAuth extends Activity implements OnClickListener { + private static final String AUTH_URL = "auth_url"; + private static final String AUTH_CODE = "auth_code"; + private EditText mAuthCode; + + /** + * called when the activity is first created + * (non-Javadoc) + * @see android.app.Activity#onCreate(android.os.Bundle) + * @author ricky barrette + */ + @Override + public void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + this.setContentView(R.layout.twitterwebauth); + WebView webView = (WebView) findViewById(R.id.authWebView); + webView.loadUrl(this.getIntent().getStringExtra(AUTH_URL)); + webView.requestFocus(View.FOCUS_DOWN); + + findViewById(R.id.authOkButton).setOnClickListener(this); + mAuthCode = (EditText) findViewById(R.id.authCode); + } + + /** + * called when the ok button is clicked + * (non-Javadoc) + * @see android.view.View.OnClickListener#onClick(android.view.View) + * @author ricky barrette + */ + @Override + public void onClick(View v) { + setResult(RESULT_OK, new Intent().putExtra(AUTH_CODE, mAuthCode.getText().toString())); + finish(); + } + +} diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostMain.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostMain.java index a901910..ce66d49 100644 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostMain.java +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostMain.java @@ -1,6 +1,9 @@ package com.TwentyCodes.android.TravelPost; +import twitter4j.TwitterException; +import android.app.Activity; import android.app.AlertDialog; +import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -10,8 +13,11 @@ import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity; import android.preference.PreferenceCategory; import android.util.Log; +import android.widget.Toast; import com.TwentyCodes.android.TravelPost.Debug.Debug; +import com.TwentyCodes.android.TravelPost.SocialSites.TwitterServices; +import com.TwentyCodes.android.TravelPost.SocialSites.TwitterWebAuth; import com.TwentyCodes.android.exception.ExceptionHandler; public class TravelPostMain extends PreferenceActivity implements OnPreferenceClickListener { @@ -21,9 +27,12 @@ public class TravelPostMain extends PreferenceActivity implements OnPreferenceCl private static final String FACEBOOK = "Facebook"; //the facebook shared pref private static final String TWITTER = "Twitter"; //the twitter shared pref private static final String TAG = "TravelPostMain"; + private static final String AUTH_URL = "auth_url"; + private static final String AUTH_CODE = "auth_code"; private static PreferenceCategory mSocialSites; private static SharedPreferences mFacebookPrefs; + private TwitterServices mTwitterServices; /** Called when the activity is first created. */ @@ -43,6 +52,34 @@ public class TravelPostMain extends PreferenceActivity implements OnPreferenceCl protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (Debug.LOGGING) Log.i(TAG, "onActivityResult()"); + + SharedPreferences prefs = this.getSharedPreferences(SETTINGS, 0); + if (intent != null) { /* This if block is required. If the user hits back while Twitter is being authorized the intent returned is null */ + String authCode = intent.getStringExtra(AUTH_CODE); + boolean authSaved = true; + if(resultCode == Activity.RESULT_OK) { + if (Debug.LOGGING) + Log.i(TAG, "onActivityResult.RESULT_OK"); + if (requestCode == 0) { + try { + mTwitterServices.saveAuthorizationTokens(authCode); + } catch (TwitterException e) { + e.printStackTrace(); + Toast.makeText(this, this.getString(R.string.twitter_authorization_failure), Toast.LENGTH_LONG).show(); + authSaved = false; + } + } + getSharedPreferences(TWITTER, 0).edit().putBoolean(AUTHORIZED, authSaved).commit(); + if (authSaved) { + if (Debug.LOGGING) + Log.i(TAG, "onActivityResult.authSaved is true"); + + int auths = prefs.getInt(AUTHORIZED, 0); + prefs.edit().putInt(AUTHORIZED, auths + 1).commit(); + } + } + } + this.initPreferences(); } /** @@ -118,11 +155,37 @@ public class TravelPostMain extends PreferenceActivity implements OnPreferenceCl Log.i(TAG, "addSocialSite.site" + site); if (site.equals(FACEBOOK)) { - Intent intent = new Intent(this.getApplicationContext(), com.TwentyCodes.android.TravelPost.SocialSites.FacebookActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - this.startActivityForResult(intent, 1010); + if (!mFacebookPrefs.getBoolean(AUTHORIZED, false)) { + Intent intent = new Intent(this.getApplicationContext(), com.TwentyCodes.android.TravelPost.SocialSites.FacebookActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + this.startActivityForResult(intent, 1010); + } else { + Toast.makeText(this, this.getString(R.string.facebook_already_authorized), Toast.LENGTH_LONG).show(); + } } else if (site.equals(TWITTER)) { - + if (!this.getSharedPreferences(TWITTER, 0).getBoolean(AUTHORIZED, false)) { + mTwitterServices = new TwitterServices(this); + final ProgressDialog progress = ProgressDialog.show(this, "", this.getText(R.string.loading), true, true); + //Connect to twitter in a new thread to prevent ANRs + new Thread( new Runnable(){ + @Override + public void run(){ + android.os.Looper.prepare(); + try { + String url = mTwitterServices.getAuthorizationURL(); + progress.dismiss(); + TravelPostMain.this.startActivityForResult(new Intent(TravelPostMain.this, TwitterWebAuth.class).putExtra(TravelPostMain.AUTH_URL, url), 0); + } catch (TwitterException e) { + e.printStackTrace(); + /* This can be thrown due to invalid authorization to Twitter (bad password or auth code??) */ + progress.dismiss(); + Toast.makeText(TravelPostMain.this, TravelPostMain.this.getString(R.string.twitter_exception), Toast.LENGTH_LONG).show(); + } + } + }).start(); + } else { + Toast.makeText(this, this.getString(R.string.twitter_already_authorized), Toast.LENGTH_LONG).show(); + } } }