Android Open Source - AndroidWifiServer Twitter Utils






From Project

Back to project page AndroidWifiServer.

License

The source code is released under:

Apache License

If you think the Android project AndroidWifiServer 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

package jp.maju.wifiserver.twitter;
//from w  w  w. ja va2  s  .c om
import jp.maju.wifiserver.R;
import jp.maju.wifiserver.util.CommonUtil;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.ConfigurationBuilder;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class TwitterUtils {

    private static final String KEY_CLIENT_ACCESS_TOKEN = "clientToken";
    private static final String KEY_SERVER_ACCESS_TOKEN = "serverToken";
    private static final String KEY_CLIENT_ACCESS_TOKEN_SECRET = "clientSecretToken";
    private static final String KEY_SERVER_ACCESS_TOKEN_SECRET = "serverSecretToken";

    public static final String PREF_NAME = TwitterUtils.class.getSimpleName();

    public static Twitter getRegisteredTwitterInstance(Application app, boolean isServer) {
        ConfigurationBuilder confBuilder = new ConfigurationBuilder();
        confBuilder.setIncludeMyRetweetEnabled(true);
        
        ProxyWrapper proxy = ProxyWrapper.getRegisteredProxy(app, CommonUtil.getCurrentSSID(app));

        if (proxy != null && proxy.port > 0) {
            confBuilder.setHttpProxyHost(proxy.host)
                    .setHttpProxyPassword(proxy.pass)
                    .setHttpProxyPort(proxy.port).setHttpProxyUser(proxy.user);
        }

        TwitterFactory tf = new TwitterFactory(confBuilder.build());
        Twitter twitter = tf.getInstance();
        
        String consumerKey = app.getString(R.string.twitter_consumer_key);
        String consumerSecret = app
                .getString(R.string.twitter_consumer_secret);
        twitter.setOAuthConsumer(consumerKey, consumerSecret);

        if (hasAccessToken(app, isServer)) {
            twitter.setOAuthAccessToken(loadAccessToken(app, isServer));
        }

        return twitter;
    }

    public static void storeAccessToken(Context context, AccessToken accessToken, boolean isServer) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        if (isServer) {
            editor.putString(KEY_SERVER_ACCESS_TOKEN, accessToken.getToken());
            editor.putString(KEY_SERVER_ACCESS_TOKEN_SECRET, accessToken.getTokenSecret());
        } else {
            editor.putString(KEY_CLIENT_ACCESS_TOKEN, accessToken.getToken());
            editor.putString(KEY_CLIENT_ACCESS_TOKEN_SECRET, accessToken.getTokenSecret());
        }
        editor.commit();
    }

    public static AccessToken loadAccessToken(Context context, boolean isServer) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME,
                Context.MODE_PRIVATE);
        
        String token;
        String tokenSecret;
        
        if (isServer) {
            token = preferences.getString(KEY_SERVER_ACCESS_TOKEN, null);
            tokenSecret = preferences.getString(KEY_SERVER_ACCESS_TOKEN_SECRET,
                    null);
        } else {
            token = preferences.getString(KEY_CLIENT_ACCESS_TOKEN, null);
            tokenSecret = preferences.getString(KEY_CLIENT_ACCESS_TOKEN_SECRET,
                    null);
        }

        if (token != null && tokenSecret != null) {
            return new AccessToken(token, tokenSecret);
        } else {
            return null;
        }
    }

    public static boolean hasAccessToken(Context context, boolean isServer) {
        return loadAccessToken(context, isServer) != null;
    }
}




Java Source Code List

jp.maju.wifiserver.AsyncExecutionTask.java
jp.maju.wifiserver.CustomWebView.java
jp.maju.wifiserver.DBAdapter.java
jp.maju.wifiserver.GateActivity.java
jp.maju.wifiserver.HTMLBuilder.java
jp.maju.wifiserver.SocketInfo.java
jp.maju.wifiserver.camera.CameraSurfaceView.java
jp.maju.wifiserver.camera.QRReaderActivity.java
jp.maju.wifiserver.client.ClientActivity.java
jp.maju.wifiserver.client.ClientService.java
jp.maju.wifiserver.server.InitServerActivity.java
jp.maju.wifiserver.server.ServerActivity.java
jp.maju.wifiserver.server.ServerService.java
jp.maju.wifiserver.twitter.ProxyDialogFragment.java
jp.maju.wifiserver.twitter.ProxyWrapper.java
jp.maju.wifiserver.twitter.TweetTask.java
jp.maju.wifiserver.twitter.TwitterOAuthActivity.java
jp.maju.wifiserver.twitter.TwitterUtils.java
jp.maju.wifiserver.util.CommonUtil.java
jp.maju.wifiserver.util.Logger.java
jp.maju.wifiserver.util.PreferenceUtil.java