/src/se/istenes/twitter/utils/TwitterAuth.java
Java | 116 lines | 77 code | 16 blank | 23 comment | 2 complexity | 2388f2fece5b294e7f68ac3baa101c85 MD5 | raw file
1package se.istenes.twitter.utils; 2 3import java.io.InputStream; 4 5import org.brickred.socialauth.AuthProvider; 6import org.brickred.socialauth.SocialAuthConfig; 7import org.brickred.socialauth.android.DialogListener; 8import org.brickred.socialauth.android.SocialAuthAdapter; 9import org.brickred.socialauth.android.SocialAuthAdapter.Provider; 10import org.brickred.socialauth.android.SocialAuthError; 11 12import twitter4j.Twitter; 13import twitter4j.TwitterFactory; 14import twitter4j.conf.ConfigurationBuilder; 15import android.content.Context; 16import android.content.SharedPreferences; 17import android.os.Bundle; 18 19public class TwitterAuth { 20 21 private Context ctx; 22 private SocialAuthAdapter adapter; 23 private static TwitterFactory factory; 24 public final String AUTH_SETTINGS = "auth_settings"; 25 26 /*** 27 * Creates a dialog for the user to authenticate 28 * @param ctx The current activity context 29 * @param tal A listener to check for successes or fails 30 */ 31 public TwitterAuth(final Context ctx, final TwitterAuthListener tal) { 32 //Store Context to access shared preferences later 33 this.ctx = ctx; 34 final SharedPreferences preferences = ctx.getSharedPreferences(AUTH_SETTINGS, Context.MODE_PRIVATE); 35 36 adapter = new SocialAuthAdapter(new DialogListener() { 37 @Override 38 public void onError(SocialAuthError arg0) { 39 preferences.edit().clear().commit(); 40 tal.Fail(arg0.getInnerException()); 41 } 42 43 @Override 44 public void onComplete(Bundle arg0) { 45 AuthProvider provider = adapter.getCurrentProvider(); 46 SocialAuthConfig config = new SocialAuthConfig(); 47 InputStream input; 48 try { 49 //piggybacks the consumer properties from the oauth_consumer file 50 input = ctx.getAssets().open("oauth_consumer.properties"); 51 config.load(input); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 56 //Sets up the preferences so we don't need to authenticate again 57 preferences.edit() 58 .putString("consumer_key", config.getApplicationProperties().getProperty("twitter.com.consumer_key")) 59 .putString("consumer_secret", config.getApplicationProperties().getProperty("twitter.com.consumer_secret")) 60 .putString("access_key", provider.getAccessGrant().getKey()) 61 .putString("access_secret", provider.getAccessGrant().getSecret()) 62 .commit(); 63 64 tal.Success(setUpTwitter()); 65 } 66 67 @Override 68 public void onCancel() { 69 preferences.edit().clear().commit(); 70 tal.Fail(new Exception("user aborted")); 71 } 72 }); 73 74 if(!preferences.contains("consumer_key")|| 75 !preferences.contains("consumer_secret")|| 76 !preferences.contains("access_key")|| 77 !preferences.contains("access_secret")) { 78 //Bring up the authorization dialog 79 adapter.authorize(ctx, Provider.TWITTER); 80 } else { 81 tal.Success(setUpTwitter()); 82 } 83 } 84 85 /*** 86 * Sets up twitter4j with all the required parameters such as 87 * consumer and accesstokens. Also stores them privatly to the 88 * shared preferences. 89 * @return a new instance of twitter if successful. 90 * <br />NOTE: This instance is synchronous and must be used with AsyncTask 91 */ 92 private Twitter setUpTwitter() { 93 //loads the keys and secrets 94 SharedPreferences settings = ctx.getSharedPreferences(AUTH_SETTINGS, Context.MODE_PRIVATE); 95 96 //Sets up twitter4j, set debug to false when deploying application 97 ConfigurationBuilder cb = new ConfigurationBuilder(); 98 cb.setDebugEnabled(true) 99 .setOAuthConsumerKey(settings.getString("consumer_key", "")) 100 .setOAuthConsumerSecret(settings.getString("consumer_secret", "")) 101 .setOAuthAccessToken(settings.getString("access_key", "")) 102 .setOAuthAccessTokenSecret(settings.getString("access_secret", "")); 103 104 //builds a new factory based on the configurations, this is used to create a twitter instance 105 factory = new TwitterFactory(cb.build()); 106 return factory.getInstance(); 107 } 108 109 /*** 110 * Get's the twitter "singleton" 111 * @return Authorized twitter instance 112 */ 113 public static Twitter getInstance() { 114 return factory.getInstance(); 115 } 116}