/src/se/istenes/twitter/utils/TwitterAuth.java
https://bitbucket.org/nistenes/twooter-app · Java · 116 lines · 77 code · 16 blank · 23 comment · 2 complexity · 2388f2fece5b294e7f68ac3baa101c85 MD5 · raw file
- package se.istenes.twitter.utils;
- import java.io.InputStream;
- import org.brickred.socialauth.AuthProvider;
- import org.brickred.socialauth.SocialAuthConfig;
- import org.brickred.socialauth.android.DialogListener;
- import org.brickred.socialauth.android.SocialAuthAdapter;
- import org.brickred.socialauth.android.SocialAuthAdapter.Provider;
- import org.brickred.socialauth.android.SocialAuthError;
- import twitter4j.Twitter;
- import twitter4j.TwitterFactory;
- import twitter4j.conf.ConfigurationBuilder;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- public class TwitterAuth {
-
- private Context ctx;
- private SocialAuthAdapter adapter;
- private static TwitterFactory factory;
- public final String AUTH_SETTINGS = "auth_settings";
-
- /***
- * Creates a dialog for the user to authenticate
- * @param ctx The current activity context
- * @param tal A listener to check for successes or fails
- */
- public TwitterAuth(final Context ctx, final TwitterAuthListener tal) {
- //Store Context to access shared preferences later
- this.ctx = ctx;
- final SharedPreferences preferences = ctx.getSharedPreferences(AUTH_SETTINGS, Context.MODE_PRIVATE);
-
- adapter = new SocialAuthAdapter(new DialogListener() {
- @Override
- public void onError(SocialAuthError arg0) {
- preferences.edit().clear().commit();
- tal.Fail(arg0.getInnerException());
- }
-
- @Override
- public void onComplete(Bundle arg0) {
- AuthProvider provider = adapter.getCurrentProvider();
- SocialAuthConfig config = new SocialAuthConfig();
- InputStream input;
- try {
- //piggybacks the consumer properties from the oauth_consumer file
- input = ctx.getAssets().open("oauth_consumer.properties");
- config.load(input);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- //Sets up the preferences so we don't need to authenticate again
- preferences.edit()
- .putString("consumer_key", config.getApplicationProperties().getProperty("twitter.com.consumer_key"))
- .putString("consumer_secret", config.getApplicationProperties().getProperty("twitter.com.consumer_secret"))
- .putString("access_key", provider.getAccessGrant().getKey())
- .putString("access_secret", provider.getAccessGrant().getSecret())
- .commit();
-
- tal.Success(setUpTwitter());
- }
-
- @Override
- public void onCancel() {
- preferences.edit().clear().commit();
- tal.Fail(new Exception("user aborted"));
- }
- });
-
- if(!preferences.contains("consumer_key")||
- !preferences.contains("consumer_secret")||
- !preferences.contains("access_key")||
- !preferences.contains("access_secret")) {
- //Bring up the authorization dialog
- adapter.authorize(ctx, Provider.TWITTER);
- } else {
- tal.Success(setUpTwitter());
- }
- }
-
- /***
- * Sets up twitter4j with all the required parameters such as
- * consumer and accesstokens. Also stores them privatly to the
- * shared preferences.
- * @return a new instance of twitter if successful.
- * <br />NOTE: This instance is synchronous and must be used with AsyncTask
- */
- private Twitter setUpTwitter() {
- //loads the keys and secrets
- SharedPreferences settings = ctx.getSharedPreferences(AUTH_SETTINGS, Context.MODE_PRIVATE);
-
- //Sets up twitter4j, set debug to false when deploying application
- ConfigurationBuilder cb = new ConfigurationBuilder();
- cb.setDebugEnabled(true)
- .setOAuthConsumerKey(settings.getString("consumer_key", ""))
- .setOAuthConsumerSecret(settings.getString("consumer_secret", ""))
- .setOAuthAccessToken(settings.getString("access_key", ""))
- .setOAuthAccessTokenSecret(settings.getString("access_secret", ""));
-
- //builds a new factory based on the configurations, this is used to create a twitter instance
- factory = new TwitterFactory(cb.build());
- return factory.getInstance();
- }
-
- /***
- * Get's the twitter "singleton"
- * @return Authorized twitter instance
- */
- public static Twitter getInstance() {
- return factory.getInstance();
- }
- }