Search in sources :

Example 71 with Twitter

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

the class CatchupPull method onHandleIntent.

@Override
public void onHandleIntent(Intent intent) {
    if (CatchupPull.isRunning || WidgetRefreshService.isRunning || TimelineRefreshService.isRunning || !MainActivity.canSwitch) {
        return;
    }
    CatchupPull.isRunning = true;
    Log.v("talon_pull", "catchup pull started");
    sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    final Context context = getApplicationContext();
    int unreadNow = sharedPrefs.getInt("pull_unread", 0);
    // stop it just in case
    context.sendBroadcast(new Intent("com.klinker.android.twitter.STOP_PUSH_SERVICE"));
    AppSettings settings = AppSettings.getInstance(context);
    if (settings.liveStreaming) {
        Log.v("talon_pull", "into the try for catchup service");
        Twitter twitter = Utils.getTwitter(context, settings);
        HomeDataSource dataSource = HomeDataSource.getInstance(context);
        int currentAccount = sharedPrefs.getInt("current_account", 1);
        List<Status> statuses = new ArrayList<Status>();
        boolean foundStatus = false;
        Paging paging = new Paging(1, 200);
        long[] lastId;
        long id;
        try {
            lastId = dataSource.getLastIds(currentAccount);
            id = lastId[0];
        } catch (Exception e) {
            context.startService(new Intent(context, TalonPullNotificationService.class));
            CatchupPull.isRunning = false;
            return;
        }
        try {
            paging.setSinceId(id);
        } catch (Exception e) {
            paging.setSinceId(1l);
        }
        for (int i = 0; i < settings.maxTweetsRefresh; i++) {
            try {
                if (!foundStatus) {
                    paging.setPage(i + 1);
                    List<Status> list = twitter.getHomeTimeline(paging);
                    statuses.addAll(list);
                    if (statuses.size() <= 1 || statuses.get(statuses.size() - 1).getId() == lastId[0]) {
                        Log.v("talon_inserting", "found status");
                        foundStatus = true;
                    } else {
                        Log.v("talon_inserting", "haven't found status");
                        foundStatus = false;
                    }
                }
            } catch (Exception e) {
                // the page doesn't exist
                foundStatus = true;
                e.printStackTrace();
            } catch (OutOfMemoryError o) {
                // don't know why...
                o.printStackTrace();
            }
        }
        Log.v("talon_pull", "got statuses, new = " + statuses.size());
        // hash set to remove duplicates I guess
        HashSet hs = new HashSet();
        hs.addAll(statuses);
        statuses.clear();
        statuses.addAll(hs);
        Log.v("talon_inserting", "tweets after hashset: " + statuses.size());
        lastId = dataSource.getLastIds(currentAccount);
        int inserted = dataSource.insertTweets(statuses, currentAccount, lastId);
        if (inserted > 0 && statuses.size() > 0) {
            sharedPrefs.edit().putLong("account_" + currentAccount + "_lastid", statuses.get(0).getId()).commit();
            unreadNow += statuses.size();
        }
        if (settings.preCacheImages) {
            // delay it 15 seconds so that we can finish checking mentions first
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    startService(new Intent(context, PreCacheService.class));
                }
            }, 15000);
        }
        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
    }
    try {
        Twitter twitter = Utils.getTwitter(context, settings);
        int currentAccount = sharedPrefs.getInt("current_account", 1);
        User user = twitter.verifyCredentials();
        MentionsDataSource dataSource = MentionsDataSource.getInstance(context);
        long[] lastId = dataSource.getLastIds(currentAccount);
        Paging paging;
        paging = new Paging(1, 200);
        if (lastId[0] > 0) {
            paging.sinceId(lastId[0]);
        }
        List<twitter4j.Status> statuses = twitter.getMentionsTimeline(paging);
        int numNew = dataSource.insertTweets(statuses, currentAccount);
        sharedPrefs.edit().putBoolean("refresh_me", true).commit();
        sharedPrefs.edit().putBoolean("refresh_me_mentions", true).commit();
        if (settings.notifications && settings.mentionsNot && numNew > 0) {
            NotificationUtils.refreshNotification(context);
        }
    } catch (TwitterException e) {
        // Error in updating status
        Log.d("Twitter Update Error", e.getMessage());
    }
    sharedPrefs.edit().putInt("pull_unread", unreadNow).commit();
    context.startService(new Intent(context, TalonPullNotificationService.class));
    context.sendBroadcast(new Intent("com.klinker.android.talon.UPDATE_WIDGET"));
    getContentResolver().notifyChange(HomeContentProvider.CONTENT_URI, null);
    Log.v("talon_pull", "finished with the catchup service");
    CatchupPull.isRunning = false;
}
Also used : Context(android.content.Context) Status(twitter4j.Status) User(twitter4j.User) AppSettings(com.klinker.android.twitter.settings.AppSettings) Paging(twitter4j.Paging) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) Handler(android.os.Handler) Intent(android.content.Intent) MentionsDataSource(com.klinker.android.twitter.data.sq_lite.MentionsDataSource) TwitterException(twitter4j.TwitterException) HomeDataSource(com.klinker.android.twitter.data.sq_lite.HomeDataSource) TwitterException(twitter4j.TwitterException) HashSet(java.util.HashSet)

Example 72 with Twitter

use of twitter4j.Twitter 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 73 with Twitter

use of twitter4j.Twitter 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 74 with Twitter

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

the class TwitterSearchFragment method doSearch.

public void doSearch(final String mQuery) {
    spinner.setVisibility(View.VISIBLE);
    if (listView.getVisibility() != View.GONE) {
        listView.setVisibility(View.GONE);
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);
                Log.v("talon_searching", "query in frag: " + mQuery);
                query = new Query(mQuery);
                if (topTweets) {
                    query.setResultType(Query.ResultType.popular);
                } else {
                    query.setResultType(null);
                }
                QueryResult result = twitter.search(query);
                tweets.clear();
                for (twitter4j.Status status : result.getTweets()) {
                    tweets.add(status);
                }
                if (result.hasNext()) {
                    query = result.nextQuery();
                    hasMore = true;
                } else {
                    hasMore = false;
                }
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        adapter = new TimelineArrayAdapter(context, tweets, onlyStatus);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);
                        spinner.setVisibility(View.GONE);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                    }
                });
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                    }
                });
            }
        }
    }).start();
}
Also used : QueryResult(twitter4j.QueryResult) Query(twitter4j.Query) Status(twitter4j.Status) Twitter(twitter4j.Twitter) Activity(android.app.Activity) TimelineArrayAdapter(com.klinker.android.twitter.adapters.TimelineArrayAdapter)

Example 75 with Twitter

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

the class TwitterSearchFragment method getMore.

public void getMore() {
    if (hasMore) {
        canRefresh = false;
        mPullToRefreshLayout.setRefreshing(true);
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Twitter twitter = Utils.getTwitter(context, settings);
                    QueryResult result = twitter.search(query);
                    for (twitter4j.Status status : result.getTweets()) {
                        tweets.add(status);
                    }
                    if (result.hasNext()) {
                        query = result.nextQuery();
                        hasMore = true;
                    } else {
                        hasMore = false;
                    }
                    ((Activity) context).runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            mPullToRefreshLayout.setRefreshing(false);
                            canRefresh = true;
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    ((Activity) context).runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            mPullToRefreshLayout.setRefreshing(false);
                            canRefresh = true;
                        }
                    });
                }
            }
        }).start();
    }
}
Also used : QueryResult(twitter4j.QueryResult) Status(twitter4j.Status) Twitter(twitter4j.Twitter) Activity(android.app.Activity)

Aggregations

Twitter (twitter4j.Twitter)127 TwitterException (twitter4j.TwitterException)76 TwitterFactory (twitter4j.TwitterFactory)60 Status (twitter4j.Status)44 Activity (android.app.Activity)35 QueryResult (twitter4j.QueryResult)17 TimelineArrayAdapter (com.klinker.android.twitter.adapters.TimelineArrayAdapter)16 Intent (android.content.Intent)13 ArrayList (java.util.ArrayList)13 User (twitter4j.User)12 Query (twitter4j.Query)11 IDs (twitter4j.IDs)8 LinearLayout (android.widget.LinearLayout)7 IOException (java.io.IOException)7 Context (android.content.Context)6 DrawerActivity (com.klinker.android.twitter.activities.drawer_activities.DrawerActivity)6 LoginActivity (com.klinker.android.twitter.activities.setup.LoginActivity)6 AppSettings (com.klinker.android.twitter.settings.AppSettings)5 GeoLocation (twitter4j.GeoLocation)5 Paging (twitter4j.Paging)5