Android Open Source - karin Twitter Provider






From Project

Back to project page karin.

License

The source code is released under:

MIT License

If you think the Android project karin 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 cc.wanko.karin.app.client;
/*from  w w w.jav a  2 s  . c  o  m*/
import android.content.Context;
import android.content.SharedPreferences;

import cc.wanko.karin.app.R;
import roboguice.util.Ln;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;

/**
 * Created by eagletmt on 14/04/29.
 */
public class TwitterProvider {
    private static final String PREF_NAME = "twitter-access-token";
    private static final String PREF_TOKEN = "access-token";
    private static final String PREF_TOKEN_SECRET = "access-token-secret";

    public static Twitter get(Context context) {
        Twitter twitter = newInstance(context);
        AccessToken accessToken = loadAccessToken(context);
        if (accessToken != null) {
            twitter.setOAuthAccessToken(accessToken);
        }
        return twitter;
    }

    public static Twitter newInstance(Context context) {
        String consumerKey = context.getString(R.string.twitter_consumer_key);
        String consumerSecret = context.getString(R.string.twitter_consumer_secret);
        Twitter twitter = new TwitterFactory().getInstance();
        Ln.d("Set twitter consumer_key=" + consumerKey + ", consumer_secret=" + consumerSecret);
        twitter.setOAuthConsumer(consumerKey, consumerSecret);
        return twitter;
    }

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

    private static AccessToken loadAccessToken(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String token = prefs.getString(PREF_TOKEN, null);
        String secret = prefs.getString(PREF_TOKEN_SECRET, null);
        if (token != null && secret != null) {
            return new AccessToken(token, secret);
        } else {
            return null;
        }
    }

    public static void storeAccessToken(Context context, AccessToken accessToken) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PREF_TOKEN, accessToken.getToken());
        editor.putString(PREF_TOKEN_SECRET, accessToken.getTokenSecret());
        editor.commit();
    }
}




Java Source Code List

cc.wanko.karin.app.activities.MainActivity.java
cc.wanko.karin.app.activities.OAuthActivity.java
cc.wanko.karin.app.activities.UserListActivity.java
cc.wanko.karin.app.activities.UserStatusesActivity.java
cc.wanko.karin.app.adapters.MainPagerAdapter.java
cc.wanko.karin.app.adapters.StatusListAdapter.java
cc.wanko.karin.app.adapters.UserListListAdapter.java
cc.wanko.karin.app.client.HomeTimelineSource.java
cc.wanko.karin.app.client.StatusSource.java
cc.wanko.karin.app.client.TwitterProvider.java
cc.wanko.karin.app.client.UserListSource.java
cc.wanko.karin.app.client.UserStatusSource.java
cc.wanko.karin.app.database.Database.java
cc.wanko.karin.app.fragments.StatusListFragment.java
cc.wanko.karin.app.fragments.UserListListFragment.java
cc.wanko.karin.app.textbuilder.MediaSegment.java
cc.wanko.karin.app.textbuilder.MentionSegment.java
cc.wanko.karin.app.textbuilder.Segment.java
cc.wanko.karin.app.textbuilder.StatusTextBuilder.java
cc.wanko.karin.app.textbuilder.UrlSegment.java
cc.wanko.karin.app.utils.LruImageCache.java
cc.wanko.karin.app.utils.RoboViewHolder.java