package org.shokai.voicetweet;

import android.content.Context;
import android.content.SharedPreferences;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.OAuth2Token;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterUtil {

    private Context mContext;
    private Properties mProperties;
    private SharedPreferences mSharedPreferences;

    public TwitterUtil(Context context){
        mContext = context;
        mProperties = new Properties();
        try {
            InputStream is = mContext.getResources().openRawResource(R.raw.twitter4j);
            mProperties.load(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Twitter getTwitterInstance(){
        ConfigurationBuilder confBuilder = new ConfigurationBuilder()
                .setOAuthConsumerKey(getConsumerKey())
                .setOAuthConsumerSecret(getConsumerSecret());

        if(hasToken()) {
            confBuilder
                    .setOAuthAccessToken(getAccessToken())
                    .setOAuthAccessTokenSecret(getAccessTokenSecret());
        }
        return new TwitterFactory(confBuilder.build()).getInstance();
    }

    public String getConsumerKey(){
        return mProperties.getProperty("oauth.consumerKey");
    }

    public String getConsumerSecret(){
        return mProperties.getProperty("oauth.consumerSecret");
    }

    public SharedPreferences getPreference(){
        if(mSharedPreferences != null) return mSharedPreferences;
        return mContext.getSharedPreferences("twitter", Context.MODE_PRIVATE);
    }

    public String getAccessToken(){
        return getPreference().getString("ACCESS_TOKEN", null);
    }

    public String getAccessTokenSecret(){
        return getPreference().getString("ACCESS_TOKEN_SECRET", null);
    }

    public void setAccessToken(String token){
        getPreference().edit().putString("ACCESS_TOKEN", token).commit();
    }

    public void setAccessTokenSecret(String tokenSecret){
        getPreference().edit().putString("ACCESS_TOKEN_SECRET", tokenSecret).commit();
    }

    public void logout(){
        setAccessToken(null);
        setAccessTokenSecret(null);
    }

    public boolean hasToken(){
        return (getAccessToken() != null) && (getAccessTokenSecret() != null);
    }
}

@@ -249,78 +249,8 @@
 ry;%0A
-import twitter4j.auth.AccessToken;%0Aimport twitter4j.auth.OAuth2Token;%0A
 impo
@@ -343,24 +343,47 @@
 xt mContext;
+ // Activity or Service
 %0A    private
@@ -406,16 +406,46 @@
 perties;
+ // save consumer key & secret
 %0A    pri
@@ -486,16 +486,46 @@
 erences;
+ // save access token & secret
 %0A%0A    pu
@@ -587,16 +587,109 @@
 ontext;%0A
+        mSharedPreferences = mContext.getSharedPreferences(%22twitter%22, Context.MODE_PRIVATE);%0A
         
@@ -1677,206 +1677,8 @@
  %7D%0A%0A
-    public SharedPreferences getPreference()%7B%0A        if(mSharedPreferences != null) return mSharedPreferences;%0A        return mContext.getSharedPreferences(%22twitter%22, Context.MODE_PRIVATE);%0A    %7D%0A%0A
     
@@ -1716,35 +1716,39 @@
 %0A        return 
-get
+mShared
 Preference().get
@@ -1733,34 +1733,33 @@
 SharedPreference
-()
+s
 .getString(%22ACCE
@@ -1840,19 +1840,23 @@
  return 
-get
+mShared
 Preferen
@@ -1857,18 +1857,17 @@
 eference
-()
+s
 .getStri
@@ -1952,35 +1952,39 @@
 token)%7B%0A        
-get
+mShared
 Preference().edi
@@ -1969,34 +1969,33 @@
 SharedPreference
-()
+s
 .edit().putStrin
@@ -2019,22 +2019,21 @@
  token).
-commit
+apply
 ();%0A    
@@ -2101,19 +2101,23 @@
         
-get
+mShared
 Preferen
@@ -2118,18 +2118,17 @@
 eference
-()
+s
 .edit().
@@ -2177,14 +2177,13 @@
 et).
-commit
+apply
 ();%0A

refactor TwitterUtil.java