Search in sources :

Example 51 with TwitterException

use of twitter4j.TwitterException in project Talon-for-Twitter by klinker24.

the class DirectMessageRefreshService method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    Context context = getApplicationContext();
    AppSettings settings = AppSettings.getInstance(context);
    // if they have mobile data on and don't want to sync over mobile data
    if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
        return;
    }
    boolean update = false;
    int numberNew = 0;
    try {
        Twitter twitter = Utils.getTwitter(context, settings);
        int currentAccount = sharedPrefs.getInt("current_account", 1);
        User user = twitter.verifyCredentials();
        long lastId = sharedPrefs.getLong("last_direct_message_id_" + currentAccount, 0);
        Paging paging;
        if (lastId != 0) {
            paging = new Paging(1).sinceId(lastId);
        } else {
            paging = new Paging(1, 500);
        }
        List<DirectMessage> dm = twitter.getDirectMessages(paging);
        List<DirectMessage> sent = twitter.getSentDirectMessages(paging);
        if (dm.size() != 0) {
            sharedPrefs.edit().putLong("last_direct_message_id_" + currentAccount, dm.get(0).getId()).commit();
            numberNew = dm.size();
        } else {
            numberNew = 0;
        }
        DMDataSource dataSource = DMDataSource.getInstance(context);
        int inserted = 0;
        for (DirectMessage directMessage : dm) {
            try {
                dataSource.createDirectMessage(directMessage, currentAccount);
            } catch (Exception e) {
                dataSource = DMDataSource.getInstance(context);
                dataSource.createDirectMessage(directMessage, currentAccount);
            }
            inserted++;
        }
        for (DirectMessage directMessage : sent) {
            try {
                dataSource.createDirectMessage(directMessage, currentAccount);
            } catch (Exception e) {
                dataSource = DMDataSource.getInstance(context);
                dataSource.createDirectMessage(directMessage, currentAccount);
            }
        }
        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
        sharedPrefs.edit().putBoolean("refresh_me_dm", true).commit();
        if (settings.notifications && settings.dmsNot && inserted > 0) {
            int currentUnread = sharedPrefs.getInt("dm_unread_" + currentAccount, 0);
            sharedPrefs.edit().putInt("dm_unread_" + currentAccount, numberNew + currentUnread).commit();
            NotificationUtils.refreshNotification(context);
        }
        if (settings.syncSecondMentions) {
            startService(new Intent(context, SecondDMRefreshService.class));
        }
        sendBroadcast(new Intent("com.klinker.android.twitter.NEW_DIRECT_MESSAGE"));
    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
}
Also used : Context(android.content.Context) DirectMessage(twitter4j.DirectMessage) User(twitter4j.User) AppSettings(com.klinker.android.twitter.settings.AppSettings) Paging(twitter4j.Paging) Twitter(twitter4j.Twitter) Intent(android.content.Intent) DMDataSource(com.klinker.android.twitter.data.sq_lite.DMDataSource) TwitterException(twitter4j.TwitterException) TwitterException(twitter4j.TwitterException)

Example 52 with TwitterException

use of twitter4j.TwitterException in project Talon-for-Twitter by klinker24.

the class SecondMentionsRefreshService method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    Context context = getApplicationContext();
    AppSettings settings = AppSettings.getInstance(context);
    // if they have mobile data on and don't want to sync over mobile data
    if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
        return;
    }
    boolean update = false;
    int numberNew = 0;
    try {
        Twitter twitter = Utils.getSecondTwitter(context);
        int currentAccount = sharedPrefs.getInt("current_account", 1);
        if (currentAccount == 1) {
            currentAccount = 2;
        } else {
            currentAccount = 1;
        }
        MentionsDataSource dataSource = MentionsDataSource.getInstance(context);
        long lastId = dataSource.getLastIds(currentAccount)[0];
        Paging paging;
        paging = new Paging(1, 200);
        if (lastId > 0) {
            paging.sinceId(lastId);
        }
        List<Status> statuses = twitter.getMentionsTimeline(paging);
        numberNew = MentionsDataSource.getInstance(context).insertTweets(statuses, currentAccount);
        if (numberNew > 0) {
            sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();
            if (settings.notifications && settings.mentionsNot) {
                NotificationUtils.notifySecondMentions(context, currentAccount);
            }
            sendBroadcast(new Intent("com.klinker.android.twitter.REFRESH_SECOND_MENTIONS"));
        }
    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
}
Also used : Context(android.content.Context) Status(twitter4j.Status) AppSettings(com.klinker.android.twitter.settings.AppSettings) Paging(twitter4j.Paging) Twitter(twitter4j.Twitter) Intent(android.content.Intent) MentionsDataSource(com.klinker.android.twitter.data.sq_lite.MentionsDataSource) TwitterException(twitter4j.TwitterException)

Example 53 with TwitterException

use of twitter4j.TwitterException in project AndroidSDK-RecipeBook by gabu.

the class Auth method onOKButton.

// OKボタンが押されたら呼び出される
public void onOKButton(View view) {
    // 入力されたユーザIDとパスワードを取得
    String userID = ((EditText) findViewById(R.id.user_id)).getText().toString();
    String password = ((EditText) findViewById(R.id.password)).getText().toString();
    // とりあえずユーザIDとパスワードでTwitterインスタンスを生成
    Twitter twitter = new TwitterFactory().getInstance(userID, password);
    try {
        // AccessTokenを取得
        AccessToken accessToken = twitter.getOAuthAccessToken();
        // tokenとtokenSecretを取得
        String token = accessToken.getToken();
        String tokenSecret = accessToken.getTokenSecret();
        // プリファレンスのEditorを取得
        Editor e = getSharedPreferences(Recipe102.PREF_NAME, MODE_PRIVATE).edit();
        // tokenとtokenSecretを書き込んで
        e.putString(Recipe102.PREF_KEY_TOKEN, token);
        e.putString(Recipe102.PREF_KEY_TOKEN_SECRET, tokenSecret);
        // 保存!
        e.commit();
        // Authアクティビティを終了
        finish();
    } catch (TwitterException e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "ユーザIDかパスワードが間違っています。", Toast.LENGTH_SHORT).show();
    }
}
Also used : AccessToken(twitter4j.http.AccessToken) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) Editor(android.content.SharedPreferences.Editor) TwitterException(twitter4j.TwitterException)

Example 54 with TwitterException

use of twitter4j.TwitterException in project AndroidSDK-RecipeBook by gabu.

the class Recipe102 method onResume.

public void onResume() {
    super.onResume();
    // プリファレンスを取得
    SharedPreferences sp = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    // tokenとtokenSecretを取得
    String token = sp.getString(PREF_KEY_TOKEN, "");
    String tokenSecret = sp.getString(PREF_KEY_TOKEN_SECRET, "");
    // 値がなければAuthアクティビティを起動
    if ("".equals(token) || "".equals(tokenSecret)) {
        Intent intent = new Intent(this, Auth.class);
        startActivity(intent);
    }
    // twitter4jのConfigurationを取得
    Configuration conf = ConfigurationContext.getInstance();
    // AccessTokenを生成
    AccessToken accessToken = new AccessToken(token, tokenSecret);
    // OAuthAuthorizationを生成
    Authorization auth = new OAuthAuthorization(conf, conf.getOAuthConsumerKey(), conf.getOAuthConsumerSecret(), accessToken);
    // OAuthAuthorizationを使ってTwitterインスタンスを生成
    Twitter twitter = new TwitterFactory().getInstance(auth);
    try {
        // とりあえずテストのためTLをログ出力
        ResponseList<Status> statuses = twitter.getHomeTimeline();
        for (Status status : statuses) {
            Log.d(TAG, status.getUser().getName() + ":" + status.getText());
        }
    } catch (TwitterException e) {
        e.printStackTrace();
    }
}
Also used : Authorization(twitter4j.http.Authorization) OAuthAuthorization(twitter4j.http.OAuthAuthorization) Status(twitter4j.Status) Configuration(twitter4j.conf.Configuration) SharedPreferences(android.content.SharedPreferences) AccessToken(twitter4j.http.AccessToken) Twitter(twitter4j.Twitter) Intent(android.content.Intent) TwitterFactory(twitter4j.TwitterFactory) OAuthAuthorization(twitter4j.http.OAuthAuthorization) TwitterException(twitter4j.TwitterException)

Example 55 with TwitterException

use of twitter4j.TwitterException in project Talon-for-Twitter by klinker24.

the class FavoriterUtils method getFavoriters.

public List<User> getFavoriters(Context context, long tweetId) {
    List<User> users = new ArrayList<User>();
    Twitter twitter = Utils.getTwitter(context);
    try {
        Status stat = twitter.showStatus(tweetId);
        if (stat.isRetweet()) {
            tweetId = stat.getRetweetedStatus().getId();
        }
        long[] ids = getFavoritersIds(tweetId);
        users = twitter.lookupUsers(ids);
    } catch (TwitterException e) {
        e.printStackTrace();
    }
    return users;
}
Also used : Status(twitter4j.Status) User(twitter4j.User) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) TwitterException(twitter4j.TwitterException)

Aggregations

TwitterException (twitter4j.TwitterException)96 Twitter (twitter4j.Twitter)69 TwitterFactory (twitter4j.TwitterFactory)54 Status (twitter4j.Status)21 User (twitter4j.User)12 Intent (android.content.Intent)11 ArrayList (java.util.ArrayList)9 File (java.io.File)6 IDs (twitter4j.IDs)6 Context (android.content.Context)5 Date (java.util.Date)4 DirectMessage (twitter4j.DirectMessage)4 Paging (twitter4j.Paging)4 AccessToken (twitter4j.auth.AccessToken)4 Activity (android.app.Activity)3 SharedPreferences (android.content.SharedPreferences)3 ActivityOptionsCompat (android.support.v4.app.ActivityOptionsCompat)3 View (android.view.View)3 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3