Wednesday 30 May 2012

Twitter Integration in Android

       
First you have to register your application in https://dev.twitter.com/ and have to get consumer key and secret key.

      In order to integrate twitter in your application you need four jar files.
1. signpost-commonshttp4-1.2.1.1.jar
2. signpost-core-1.2.1.1.jar
3. signpost-jetty6-1.2.1.1.jar
4. twitter4j-core-android-2.2.5.jar
  • First Download these four jar files
  • Right Click on your project. Select "Build Path". From that select "Configure Build Path".
  • Then in Libraries Tab select "Add External Jars" then select the path where you downloaded jar files.Add all the four jar files and click ok.

TwitterActivity.class

package com.twitter;
import com.twitter.TwitterApp.TwDialogListener;
import android.widget.CheckBox;
import android.widget.Toast;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;

public class TwitterActivity extends Activity {
private TwitterApp mTwitter;
private CheckBox mTwitterBtn;

private static final String twitter_consumer_key = "Ur Consumer Key";
private static final String twitter_secret_key = "Ur Secret Key";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mTwitterBtn = (CheckBox) findViewById(R.id.twitterCheck);
mTwitterBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mTwitterBtn.setChecked(false);
mTwitter.authorize();
}
});
mTwitter = new TwitterApp(this, twitter_consumer_key,twitter_secret_key);
mTwitter.setListener(mTwLoginDialogListener);
if (mTwitter.hasAccessToken()) {
mTwitterBtn.setVisibility(View.GONE);
Toast.makeText(TwitterActivity.this, "Connected to Twitter" , Toast.LENGTH_LONG).show();
}
}
private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
public void onComplete(String value) {
Toast.makeText(TwitterActivity.this, "Connected to Twitter" , Toast.LENGTH_LONG).show();
}
public void onError(String value) {
mTwitterBtn.setChecked(false);
Toast.makeText(TwitterActivity.this, "Twitter connection failed", Toast.LENGTH_LONG).show();
}
};
}

Now you have to create three more classes.

TwitterApp .class

package com.twitter;

import java.net.MalformedURLException;
import java.net.URLDecoder;

import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;

import android.os.Handler;
import android.os.Message;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.Window;

import java.net.URL;

public class TwitterApp {
private Twitter mTwitter;
private TwitterSession mSession;
private AccessToken mAccessToken;
private CommonsHttpOAuthConsumer mHttpOauthConsumer;
private OAuthProvider mHttpOauthprovider;
private String mConsumerKey;
private String mSecretKey;
private ProgressDialog mProgressDlg;
private TwDialogListener mListener;
private Context context;
private boolean mInit = true;

public static final String CALLBACK_URL = "twitterapp://connect";
private static final String TAG = "TwitterApp";

public TwitterApp(Context context, String consumerKey, String secretKey) {
this.context = context;

mTwitter = new TwitterFactory().getInstance();
mSession = new TwitterSession(context);
mProgressDlg = new ProgressDialog(context);

mProgressDlg.requestWindowFeature(Window.FEATURE_NO_TITLE);

mConsumerKey = consumerKey;
mSecretKey = secretKey;

mHttpOauthConsumer = new CommonsHttpOAuthConsumer(mConsumerKey, mSecretKey);
mHttpOauthprovider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token",
"http://twitter.com/oauth/authorize");

mAccessToken = mSession.getAccessToken();

configureToken();
}

public void setListener(TwDialogListener listener) {
mListener = listener;
}

@SuppressWarnings("deprecation")
private void configureToken() {
if (mAccessToken != null) {
if (mInit) {
mTwitter.setOAuthConsumer(mConsumerKey, mSecretKey);
mInit = false;
}

mTwitter.setOAuthAccessToken(mAccessToken);
}
}

public boolean hasAccessToken() {
return (mAccessToken == null) ? false : true;
}

public void resetAccessToken() {
if (mAccessToken != null) {
mSession.resetAccessToken();

mAccessToken = null;
}
}

public String getUsername() {
return mSession.getUsername();
}

public void updateStatus(String status) throws Exception {
try {
mTwitter.updateStatus(status);
} catch (TwitterException e) {
throw e;
}
}

public void authorize() {
mProgressDlg.setMessage("Initializing ...");
mProgressDlg.show();

new Thread() {
@Override
public void run() {
String authUrl = "";
int what = 1;

try {
authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);

what = 0;

Log.d(TAG, "Request token url " + authUrl);
} catch (Exception e) {
Log.d(TAG, "Failed to get request token");

e.printStackTrace();
}

mHandler.sendMessage(mHandler.obtainMessage(what, 1, 0, authUrl));
}
}.start();
}

public void processToken(String callbackUrl)  {
mProgressDlg.setMessage("Finalizing ...");
mProgressDlg.show();

final String verifier = getVerifier(callbackUrl);

new Thread() {
@Override
public void run() {
int what = 1;

try {
mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, verifier);

mAccessToken = new AccessToken(mHttpOauthConsumer.getToken(), mHttpOauthConsumer.getTokenSecret());

configureToken();

User user = mTwitter.verifyCredentials();

       mSession.storeAccessToken(mAccessToken, user.getName());
     
       what = 0;
} catch (Exception e){
Log.d(TAG, "Error getting access token");

e.printStackTrace();
}

mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0));
}
}.start();
}

private String getVerifier(String callbackUrl) {
String verifier = "";

try {
callbackUrl = callbackUrl.replace("twitterapp", "http");

URL url = new URL(callbackUrl);
String query = url.getQuery();

String array[] = query.split("&");

for (String parameter : array) {
            String v[] = parameter.split("=");
         
            if (URLDecoder.decode(v[0]).equals(oauth.signpost.OAuth.OAUTH_VERIFIER)) {
            verifier = URLDecoder.decode(v[1]);
            break;
            }
       }
} catch (MalformedURLException e) {
e.printStackTrace();
}

return verifier;
}

private void showLoginDialog(String url) {
final TwDialogListener listener = new TwDialogListener() {
public void onComplete(String value) {
processToken(value);
}

public void onError(String value) {
mListener.onError("Failed opening authorization page");
}
};

new TwitterDialog(context, url, listener).show();
}

private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
mProgressDlg.dismiss();

if (msg.what == 1) {
if (msg.arg1 == 1)
mListener.onError("Error getting request token");
else
mListener.onError("Error getting access token");
} else {
if (msg.arg1 == 1)
showLoginDialog((String) msg.obj);
else
mListener.onComplete("");
}
}
};

public interface TwDialogListener {
public void onComplete(String value);

public void onError(String value);
}
}

TwitterDialog.class


package com.twitter;

import com.twitter.TwitterApp.TwDialogListener;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.content.Context;
import android.view.Display;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

public class TwitterDialog extends Dialog {
    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 String mUrl;
    private TwDialogListener mListener;
    private ProgressDialog mSpinner;
    private WebView mWebView;
    private LinearLayout mContent;
    private TextView mTitle;

    private static final String TAG = "Twitter-WebView";
 
    public TwitterDialog(Context context, String url, TwDialogListener listener) {
        super(context);
     
        mUrl = url;
        mListener = listener;
    }

    @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();
        double[] dimensions = new double[2];
     
        if (display.getWidth() < display.getHeight()) {
        dimensions[0] = 0.87 * display.getWidth();
        dimensions[1] = 0.82 * display.getHeight();
        } else {
        dimensions[0] = 0.75 * display.getWidth();
        dimensions[1] = 0.75 * display.getHeight();      
        }
     
        addContentView(mContent, new FrameLayout.LayoutParams((int) dimensions[0], (int) dimensions[1]));
    }

    private void setUpTitle() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
     
        mTitle = new TextView(getContext());
     
        mTitle.setText("Twitter");
        mTitle.setTextColor(Color.WHITE);
        mTitle.setTypeface(Typeface.DEFAULT_BOLD);
        mTitle.setBackgroundColor(0xFFbbd7e9);
        mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);
        mTitle.setCompoundDrawablePadding(MARGIN + PADDING);      
        mContent.addView(mTitle);
    }

    private void setUpWebView() {
    CookieSyncManager.createInstance(getContext());
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
     
        mWebView = new WebView(getContext());
     
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new TwitterWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl(mUrl);
        mWebView.setLayoutParams(FILL);
     
        mContent.addView(mWebView);
    }

    private class TwitterWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.d(TAG, "Redirecting URL " + url);
       
        if (url.startsWith(TwitterApp.CALLBACK_URL)) {
        mListener.onComplete(url);
       
        TwitterDialog.this.dismiss();
       
        return true;
        }  else if (url.startsWith("authorize")) {
        return false;
        }
       
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.d(TAG, "Page error: " + description);
       
            super.onReceivedError(view, errorCode, description, failingUrl);
   
            mListener.onError(description);
         
            TwitterDialog.this.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.d(TAG, "Loading URL: " + url);
            super.onPageStarted(view, url, favicon);
            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();
        }

    }
}


TwitterSession.class

package com.twitter;

import twitter4j.auth.AccessToken;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.Context;

public class TwitterSession {
private SharedPreferences sharedPref;
private Editor editor;
private static final String TWEET_AUTH_KEY = "auth_key";
private static final String TWEET_AUTH_SECRET_KEY = "auth_secret_key";
private static final String TWEET_USER_NAME = "user_name";
private static final String SHARED = "Twitter_Preferences";
public TwitterSession(Context context) {
sharedPref  = context.getSharedPreferences(SHARED, Context.MODE_PRIVATE);
editor  = sharedPref.edit();
}
public void storeAccessToken(AccessToken accessToken, String username) {
editor.putString(TWEET_AUTH_KEY, accessToken.getToken());
editor.putString(TWEET_AUTH_SECRET_KEY, accessToken.getTokenSecret());
editor.putString(TWEET_USER_NAME, username);
editor.commit();
}
public void resetAccessToken() {
editor.putString(TWEET_AUTH_KEY, null);
editor.putString(TWEET_AUTH_SECRET_KEY, null);
editor.putString(TWEET_USER_NAME, null);
editor.commit();
}
public String getUsername() {
return sharedPref.getString(TWEET_USER_NAME, "");
}
public AccessToken getAccessToken() {
String token = sharedPref.getString(TWEET_AUTH_KEY, null);
String tokenSecret = sharedPref.getString(TWEET_AUTH_SECRET_KEY, null);
if (token != null && tokenSecret != null) 
return new AccessToken(token, tokenSecret);
else
return null;
}
}

main.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">

 
<CheckBox
android:id="@+id/twitterCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:text="  Twitter (Not connected) "
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#ccc"
android:clickable="true"
android:focusable="true" />
</LinearLayout>

AndroidManifest.xml

add
<uses-permission android:name="android.permission.INTERNET"/>