Android Open Source - twitter-login-button Twitter Login Button






From Project

Back to project page twitter-login-button.

License

The source code is released under:

Copyright (C) 2013 Xavier Balloy https://github.com/xballoy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Soft...

If you think the Android project twitter-login-button listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2011 Atos Wordline/*ww w .ja va2  s. co  m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.awl.tumlabs.twitter.android;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import com.awl.tumlabs.twitter.android.TwitterAuth.AuthListener;
import com.awl.tumlabs.twitter.android.TwitterAuth.TwitterLoginListener;
import com.twitter.android.R;

/***
 * Create a login button to sign in with Twitter
 * 
 * @author Xavier Balloy
 * 
 */
public class TwitterLoginButton extends ImageButton {

  private static final String TAG = TwitterLoginButton.class.getSimpleName();

  private TwitterAuth twitter;
  private Activity activity;
  private AuthListener listener;
  private SessionListener sessionListener;

  private Handler hander = new Handler();

  public TwitterLoginButton(Context context) {
    super(context);
  }

  public TwitterLoginButton(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public TwitterLoginButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  /***
   * Initialize the button
   * 
   * @param activiy
   *            The activity where the button is set
   * @param twitter
   *            The Twitter used to store the token and token secret
   */
  public void init(final Activity activiy, final TwitterAuth twitter,
      final AuthListener listener) {
    this.twitter = twitter;
    this.activity = activiy;
    this.listener = listener;
    this.sessionListener = new SessionListener();

    setBackgroundColor(Color.TRANSPARENT);
    setAdjustViewBounds(true);

    TwitterSessionStore.restore(twitter, getContext());

    setImageResource(twitter.isSessionValid() ? R.drawable.twitter_logout
        : R.drawable.twitter_login);
    drawableStateChanged();

    setOnClickListener(new ButtonOnClickListener());
  }

  final class ButtonOnClickListener implements OnClickListener {

    @Override
    public void onClick(View v) {

      if (twitter.isSessionValid()) {
        Log.d(TAG, "Session valid: disconnection");
        TwitterSessionStore.clear(twitter, getContext());
        setImageResource(R.drawable.twitter_login);
        listener.onLogoutSucceed();
      } else {
        Log.d(TAG, "Session not valid: connection");
        twitter.getAuthorizeUrl(sessionListener);
      }
    }
  }

  protected final class SessionListener implements TwitterLoginListener {

    @Override
    public void onAuthorizeUrlRetrieved(String url) {
      Log.d(TAG, "onAuthorizeUrlRetrieved: " + url);
      final String _url = url;
      hander.post(new Runnable() {

        @Override
        public void run() {
          twitter.autorize(activity, _url,
              sessionListener);
        }
      });
    }

    @Override
    public void onDialogComplete(String oauthVerifier) {
      Log.d(TAG, "onDialogComplete: " + oauthVerifier);
      twitter.retrieveAccessToken(oauthVerifier, sessionListener);
    }
    
    @Override
    public void onAccessTokenRetrieved() {
      Log.d(TAG, "onAccessTokenRetrieved");
      hander.post(new Runnable() {
        
        @Override
        public void run() {
          TwitterSessionStore.save(twitter, getContext());
          setImageResource(R.drawable.twitter_logout);
          listener.onAuthSucceed();
        }
      });
    }

    @Override
    public void onError(String message) {
      listener.onError(message);
    }

  }
}




Java Source Code List

com.awl.tumlabs.twitter.android.TwitterAuth.java
com.awl.tumlabs.twitter.android.TwitterDialog.java
com.awl.tumlabs.twitter.android.TwitterLoginButton.java
com.awl.tumlabs.twitter.android.TwitterSessionStore.java
com.awl.tumlabs.twitter.android.Util.java