diff --git a/twitter/libs/signpost-commonshttp4-1.2.1.1.jar b/twitter/libs/signpost-commonshttp4-1.2.1.1.jar deleted file mode 100644 index 50e9838..0000000 Binary files a/twitter/libs/signpost-commonshttp4-1.2.1.1.jar and /dev/null differ diff --git a/twitter/libs/signpost-core-1.2.1.1.jar b/twitter/libs/signpost-core-1.2.1.1.jar deleted file mode 100644 index 59e7537..0000000 Binary files a/twitter/libs/signpost-core-1.2.1.1.jar and /dev/null differ diff --git a/twitter/libs/twitter4j-core-android-2.2.1-SNAPSHOT.jar b/twitter/libs/twitter4j-core-android-2.2.1-SNAPSHOT.jar new file mode 100644 index 0000000..c2e86f6 Binary files /dev/null and b/twitter/libs/twitter4j-core-android-2.2.1-SNAPSHOT.jar differ diff --git a/twitter/src/com/sugree/twitter/TwDialog.java b/twitter/src/com/sugree/twitter/TwDialog.java index e5e925a..ca0dcee 100644 --- a/twitter/src/com/sugree/twitter/TwDialog.java +++ b/twitter/src/com/sugree/twitter/TwDialog.java @@ -16,12 +16,9 @@ package com.sugree.twitter; -import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; -import oauth.signpost.commonshttp.CommonsHttpOAuthProvider; -import oauth.signpost.exception.OAuthCommunicationException; -import oauth.signpost.exception.OAuthExpectationFailedException; -import oauth.signpost.exception.OAuthMessageSignerException; -import oauth.signpost.exception.OAuthNotAuthorizedException; +import twitter4j.TwitterException; +import twitter4j.auth.AccessToken; +import twitter4j.auth.RequestToken; import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; @@ -48,195 +45,181 @@ public class TwDialog extends Dialog { public static final String TAG = "twitter"; - static final int TW_BLUE = 0xFFC0DEED; - static final float[] DIMENSIONS_LANDSCAPE = {460, 260}; - static final float[] DIMENSIONS_PORTRAIT = {280, 420}; - static final FrameLayout.LayoutParams FILL = - new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, - ViewGroup.LayoutParams.FILL_PARENT); - static final int MARGIN = 4; - static final int PADDING = 2; - + static final int TW_BLUE = 0xFFC0DEED; + static final float[] DIMENSIONS_LANDSCAPE = { 460, 260 }; + static final float[] DIMENSIONS_PORTRAIT = { 280, 420 }; + static final FrameLayout.LayoutParams FILL = new FrameLayout.LayoutParams( + ViewGroup.LayoutParams.FILL_PARENT, + ViewGroup.LayoutParams.FILL_PARENT); + static final int MARGIN = 4; + static final int PADDING = 2; + private int mIcon; - private String mUrl; - private DialogListener mListener; - private ProgressDialog mSpinner; - private WebView mWebView; - private LinearLayout mContent; - private TextView mTitle; - private Handler mHandler; - - private CommonsHttpOAuthConsumer mConsumer; - private CommonsHttpOAuthProvider mProvider; - - public TwDialog(Context context, - CommonsHttpOAuthProvider provider, - CommonsHttpOAuthConsumer consumer, - DialogListener listener, int icon) { - super(context); - mProvider = provider; - mConsumer = consumer; - mListener = listener; + private String mUrl; + private DialogListener mListener; + private ProgressDialog mSpinner; + private WebView mWebView; + private LinearLayout mContent; + private TextView mTitle; + private Handler mHandler; + + private twitter4j.Twitter mTwitter; + + private RequestToken mRequestToken; + + public TwDialog(Context context, twitter4j.Twitter twitter, + DialogListener listener, int icon) { + super(context); + mTwitter = twitter; + mListener = listener; mIcon = icon; mHandler = new Handler(); - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mSpinner = new ProgressDialog(getContext()); - mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE); - mSpinner.setMessage("Loading..."); - - mContent = new LinearLayout(getContext()); - mContent.setOrientation(LinearLayout.VERTICAL); - setUpTitle(); - setUpWebView(); - - Display display = getWindow().getWindowManager().getDefaultDisplay(); - final float scale = getContext().getResources().getDisplayMetrics().density; - float[] dimensions = display.getWidth() < display.getHeight() ? - DIMENSIONS_PORTRAIT : DIMENSIONS_LANDSCAPE; - addContentView(mContent, new FrameLayout.LayoutParams( - (int) (dimensions[0] * scale + 0.5f), - (int) (dimensions[1] * scale + 0.5f))); - - retrieveRequestToken(); - } - - @Override + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mSpinner = new ProgressDialog(getContext()); + mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE); + mSpinner.setMessage("Loading..."); + + mContent = new LinearLayout(getContext()); + mContent.setOrientation(LinearLayout.VERTICAL); + setUpTitle(); + setUpWebView(); + + Display display = getWindow().getWindowManager().getDefaultDisplay(); + final float scale = getContext().getResources().getDisplayMetrics().density; + float[] dimensions = display.getWidth() < display.getHeight() ? DIMENSIONS_PORTRAIT + : DIMENSIONS_LANDSCAPE; + addContentView(mContent, new FrameLayout.LayoutParams( + (int) (dimensions[0] * scale + 0.5f), (int) (dimensions[1] + * scale + 0.5f))); + + retrieveRequestToken(); + } + + @Override public void show() { super.show(); mSpinner.show(); } private void setUpTitle() { - requestWindowFeature(Window.FEATURE_NO_TITLE); - Drawable icon = getContext().getResources().getDrawable(mIcon); - mTitle = new TextView(getContext()); - mTitle.setText("Twitter"); - mTitle.setTextColor(Color.WHITE); - mTitle.setTypeface(Typeface.DEFAULT_BOLD); - mTitle.setBackgroundColor(TW_BLUE); - mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN); - mTitle.setCompoundDrawablePadding(MARGIN + PADDING); - mTitle.setCompoundDrawablesWithIntrinsicBounds( - icon, null, null, null); - mContent.addView(mTitle); - } - - private void retrieveRequestToken() { - mSpinner.show(); - new Thread() { - @Override - public void run() { - try { - mUrl = mProvider.retrieveRequestToken(mConsumer, Twitter.CALLBACK_URI); - mWebView.loadUrl(mUrl); - } catch (OAuthMessageSignerException e) { - mListener.onError(new DialogError(e.getMessage(), -1, Twitter.OAUTH_REQUEST_TOKEN)); - } catch (OAuthNotAuthorizedException e) { - mListener.onError(new DialogError(e.getMessage(), -1, Twitter.OAUTH_REQUEST_TOKEN)); - } catch (OAuthExpectationFailedException e) { - mListener.onError(new DialogError(e.getMessage(), -1, Twitter.OAUTH_REQUEST_TOKEN)); - } catch (OAuthCommunicationException e) { - mListener.onError(new DialogError(e.getMessage(), -1, Twitter.OAUTH_REQUEST_TOKEN)); - } - } - }.start(); - } - - private void retrieveAccessToken(final String url) { - mSpinner.show(); - new Thread() { - @Override - public void run() { - Uri uri = Uri.parse(url); - String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); - final Bundle values = new Bundle(); - try { - mProvider.retrieveAccessToken(mConsumer, verifier); - values.putString(Twitter.ACCESS_TOKEN, mConsumer.getToken()); - values.putString(Twitter.SECRET_TOKEN, mConsumer.getTokenSecret()); + requestWindowFeature(Window.FEATURE_NO_TITLE); + Drawable icon = getContext().getResources().getDrawable(mIcon); + mTitle = new TextView(getContext()); + mTitle.setText("Twitter"); + mTitle.setTextColor(Color.WHITE); + mTitle.setTypeface(Typeface.DEFAULT_BOLD); + mTitle.setBackgroundColor(TW_BLUE); + mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN); + mTitle.setCompoundDrawablePadding(MARGIN + PADDING); + mTitle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); + mContent.addView(mTitle); + } + + private void retrieveRequestToken() { + mSpinner.show(); + new Thread() { + @Override + public void run() { + try { + mRequestToken = mTwitter.getOAuthRequestToken(Twitter.CALLBACK_URI); + mUrl = mRequestToken.getAuthorizationURL(); + mWebView.loadUrl(mUrl); + + } catch (TwitterException e) { + mListener.onError(new DialogError(e.getMessage(), -1, + Twitter.OAUTH_REQUEST_TOKEN)); + } + } + }.start(); + } + + private void retrieveAccessToken(final String url) { + mSpinner.show(); + new Thread() { + @Override + public void run() { + final Bundle values = new Bundle(); + try { + AccessToken at = mTwitter + .getOAuthAccessToken(mRequestToken); + values.putString(Twitter.ACCESS_TOKEN, at.getToken()); + values.putString(Twitter.SECRET_TOKEN, at.getTokenSecret()); mListener.onComplete(values); - } catch (OAuthMessageSignerException e) { - mListener.onError(new DialogError(e.getMessage(), -1, verifier)); - } catch (OAuthNotAuthorizedException e) { - mListener.onTwitterError(new TwitterError(e.getMessage())); - } catch (OAuthExpectationFailedException e) { + } catch (TwitterException e) { mListener.onTwitterError(new TwitterError(e.getMessage())); - } catch (OAuthCommunicationException e) { - mListener.onError(new DialogError(e.getMessage(), -1, verifier)); } mHandler.post(new Runnable() { - @Override public void run() { mSpinner.dismiss(); TwDialog.this.dismiss(); - } + } }); - } - }.start(); - } - - private void setUpWebView() { - mWebView = new WebView(getContext()); - mWebView.setVerticalScrollBarEnabled(false); - mWebView.setHorizontalScrollBarEnabled(false); - mWebView.setWebViewClient(new TwDialog.TwWebViewClient()); - mWebView.getSettings().setJavaScriptEnabled(true); - //mWebView.loadUrl(mUrl); - mWebView.setLayoutParams(FILL); - mContent.addView(mWebView); - } - - private class TwWebViewClient extends WebViewClient { - - @Override - public boolean shouldOverrideUrlLoading(WebView view, String url) { - Log.d(TAG, "Redirect URL: " + url); - if (url.startsWith(Twitter.CALLBACK_URI)) { - retrieveAccessToken(url); - return true; - } else if (url.startsWith(Twitter.CANCEL_URI)) { - mListener.onCancel(); - TwDialog.this.dismiss(); - return true; - } - // launch non-dialog URLs in a full browser - getContext().startActivity( - new Intent(Intent.ACTION_VIEW, Uri.parse(url))); - return true; - } - - @Override - public void onReceivedError(WebView view, int errorCode, - String description, String failingUrl) { - super.onReceivedError(view, errorCode, description, failingUrl); - mListener.onError( - new DialogError(description, errorCode, failingUrl)); - TwDialog.this.dismiss(); - } - - @Override - public void onPageStarted(WebView view, String url, Bitmap favicon) { - Log.d(TAG, "WebView loading URL: " + url); - super.onPageStarted(view, url, favicon); - if (mSpinner.isShowing()) { - mSpinner.dismiss(); - } - mSpinner.show(); - } - - @Override - public void onPageFinished(WebView view, String url) { - super.onPageFinished(view, url); - String title = mWebView.getTitle(); - if (title != null && title.length() > 0) { - mTitle.setText(title); - } - mSpinner.dismiss(); - } - - } + } + }.start(); + } + + private void setUpWebView() { + mWebView = new WebView(getContext()); + mWebView.setVerticalScrollBarEnabled(false); + mWebView.setHorizontalScrollBarEnabled(false); + mWebView.setWebViewClient(new TwDialog.TwWebViewClient()); + mWebView.getSettings().setJavaScriptEnabled(true); + // mWebView.loadUrl(mUrl); + mWebView.setLayoutParams(FILL); + mContent.addView(mWebView); + } + + private class TwWebViewClient extends WebViewClient { + + @Override + public boolean shouldOverrideUrlLoading(WebView view, String url) { + Log.d(TAG, "Redirect URL: " + url); + if (url.startsWith(Twitter.CALLBACK_URI)) { + retrieveAccessToken(url); + return true; + } else if (url.startsWith(Twitter.CANCEL_URI)) { + mListener.onCancel(); + TwDialog.this.dismiss(); + return true; + } + // launch non-dialog URLs in a full browser + getContext().startActivity( + new Intent(Intent.ACTION_VIEW, Uri.parse(url))); + return true; + } + + @Override + public void onReceivedError(WebView view, int errorCode, + String description, String failingUrl) { + super.onReceivedError(view, errorCode, description, failingUrl); + mListener.onError(new DialogError(description, errorCode, + failingUrl)); + TwDialog.this.dismiss(); + } + + @Override + public void onPageStarted(WebView view, String url, Bitmap favicon) { + Log.d(TAG, "WebView loading URL: " + url); + super.onPageStarted(view, url, favicon); + if (mSpinner.isShowing()) { + mSpinner.dismiss(); + } + mSpinner.show(); + } + + @Override + public void onPageFinished(WebView view, String url) { + super.onPageFinished(view, url); + String title = mWebView.getTitle(); + if (title != null && title.length() > 0) { + mTitle.setText(title); + } + mSpinner.dismiss(); + } + + } } diff --git a/twitter/src/com/sugree/twitter/Twitter.java b/twitter/src/com/sugree/twitter/Twitter.java index 4314d68..dac74a5 100644 --- a/twitter/src/com/sugree/twitter/Twitter.java +++ b/twitter/src/com/sugree/twitter/Twitter.java @@ -3,12 +3,7 @@ import java.io.IOException; import java.net.MalformedURLException; -import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; -import oauth.signpost.commonshttp.CommonsHttpOAuthProvider; -import oauth.signpost.exception.OAuthCommunicationException; -import oauth.signpost.exception.OAuthExpectationFailedException; -import oauth.signpost.exception.OAuthMessageSignerException; -import oauth.signpost.exception.OAuthNotAuthorizedException; +import twitter4j.TwitterFactory; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; @@ -38,8 +33,7 @@ public class Twitter { private String mSecretToken = null; private int mIcon; - private CommonsHttpOAuthConsumer mHttpOauthConsumer; - private CommonsHttpOAuthProvider mHttpOauthProvider; + private twitter4j.Twitter mTwitter; public Twitter(int icon) { mIcon = icon; @@ -50,14 +44,12 @@ public void authorize(Context ctx, String consumerKey, String consumerSecret, final DialogListener listener) { - mHttpOauthConsumer = new CommonsHttpOAuthConsumer( + mTwitter = new TwitterFactory().getInstance(); + mTwitter.setOAuthConsumer( consumerKey, consumerSecret); - mHttpOauthProvider = new CommonsHttpOAuthProvider( - OAUTH_REQUEST_TOKEN, OAUTH_ACCESS_TOKEN, OAUTH_AUTHORIZE); CookieSyncManager.createInstance(ctx); dialog(ctx, handler, new DialogListener() { - @Override public void onComplete(Bundle values) { CookieSyncManager.getInstance().sync(); setAccessToken(values.getString(ACCESS_TOKEN)); @@ -70,19 +62,16 @@ public void onComplete(Bundle values) { } } - @Override public void onTwitterError(TwitterError e) { Log.d(TAG, "Login failed: "+e); listener.onTwitterError(e); } - @Override public void onError(DialogError e) { Log.d(TAG, "Login failed: "+e); listener.onError(e); } - @Override public void onCancel() { Log.d(TAG, "Login cancelled"); listener.onCancel(); @@ -103,7 +92,7 @@ public void dialog(final Context ctx, Util.showAlert(ctx, "Error", "Application requires permission to access the Internet"); return; } - new TwDialog(ctx, mHttpOauthProvider, mHttpOauthConsumer, + new TwDialog(ctx, mTwitter, listener, mIcon).show(); }