Example usage for twitter4j Paging setMaxId

List of usage examples for twitter4j Paging setMaxId

Introduction

In this page you can find the example usage for twitter4j Paging setMaxId.

Prototype

public void setMaxId(long maxId) 

Source Link

Usage

From source file:org.mariotaku.twidere.loader.support.Twitter4JStatusesLoader.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  w ww. j  a v a2 s . c o m*/
public final List<ParcelableStatus> loadInBackground() {
    final File serializationFile = getSerializationFile();
    final List<ParcelableStatus> data = getData();
    if (isFirstLoad() && getTabPosition() >= 0 && serializationFile != null) {
        final List<ParcelableStatus> cached = getCachedData(serializationFile);
        if (cached != null) {
            data.addAll(cached);
            if (mComparator != null) {
                Collections.sort(data, mComparator);
            } else {
                Collections.sort(data);
            }
            return new CopyOnWriteArrayList<>(data);
        }
    }
    if (!isFromUser())
        return data;
    final List<Status> statuses;
    final boolean truncated;
    final Context context = getContext();
    final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT);
    try {
        final Paging paging = new Paging();
        paging.setCount(loadItemLimit);
        if (mMaxId > 0) {
            paging.setMaxId(mMaxId);
        }
        if (mSinceId > 0) {
            paging.setSinceId(mSinceId - 1);
        }
        statuses = new ArrayList<>();
        final Twitter twitter = getTwitter();
        if (twitter == null) {
            throw new TwitterException("Account is null");
        }
        truncated = truncateStatuses(getStatuses(twitter, paging), statuses, mSinceId);
    } catch (final TwitterException e) {
        // mHandler.post(new ShowErrorRunnable(e));
        Log.w(LOGTAG, e);
        return new CopyOnWriteArrayList<>(data);
    }
    final long minStatusId = statuses.isEmpty() ? -1 : Collections.min(statuses).getId();
    final boolean insertGap = minStatusId > 0 && statuses.size() > 1 && !data.isEmpty() && !truncated;
    mHandler.post(CacheUsersStatusesTask.getRunnable(context, new StatusListResponse(mAccountId, statuses)));
    for (final Status status : statuses) {
        final long id = status.getId();
        final boolean deleted = deleteStatus(data, id);
        data.add(new ParcelableStatus(status, mAccountId, minStatusId == id && insertGap && !deleted));
    }
    final ParcelableStatus[] array = data.toArray(new ParcelableStatus[data.size()]);
    for (int i = 0, size = array.length; i < size; i++) {
        final ParcelableStatus status = array[i];
        if (shouldFilterStatus(mDatabase, status) && !status.is_gap && i != size - 1) {
            deleteStatus(data, status.id);
        }
    }
    if (mComparator != null) {
        Collections.sort(data, mComparator);
    } else {
        Collections.sort(data);
    }
    saveCachedData(serializationFile, data);
    return new CopyOnWriteArrayList<>(data);
}

From source file:org.mariotaku.twidere.loader.support.TwitterAPIStatusesLoader.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w w w  . j ava2s  .  co  m*/
public final List<ParcelableStatus> loadInBackground() {
    final File serializationFile = getSerializationFile();
    final List<ParcelableStatus> data = getData();
    if (isFirstLoad() && getTabPosition() >= 0 && serializationFile != null) {
        final List<ParcelableStatus> cached = getCachedData(serializationFile);
        if (cached != null) {
            data.addAll(cached);
            if (mComparator != null) {
                Collections.sort(data, mComparator);
            } else {
                Collections.sort(data);
            }
            return new CopyOnWriteArrayList<>(data);
        }
    }
    if (!isFromUser())
        return data;
    final Twitter twitter = getTwitter();
    if (twitter == null)
        return null;
    final List<Status> statuses;
    final boolean truncated;
    final Context context = getContext();
    final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT);
    final boolean noItemsBefore = data == null || data.isEmpty();
    try {
        final Paging paging = new Paging();
        paging.setCount(loadItemLimit);
        if (mMaxId > 0) {
            paging.setMaxId(mMaxId);
        }
        if (mSinceId > 0) {
            paging.setSinceId(mSinceId - 1);
        }
        statuses = new ArrayList<>();
        truncated = truncateStatuses(getStatuses(twitter, paging), statuses, mSinceId);
    } catch (final TwitterException e) {
        // mHandler.post(new ShowErrorRunnable(e));
        Log.w(LOGTAG, e);
        return new CopyOnWriteArrayList<>(data);
    }

    final long[] statusIds = new long[statuses.size()];
    long minId = -1;
    int minIdx = -1;
    int rowsDeleted = 0;
    for (int i = 0, j = statuses.size(); i < j; i++) {
        final twitter4j.Status status = statuses.get(i);
        final long id = status.getId();
        if (minId == -1 || id < minId) {
            minId = id;
            minIdx = i;
        }
        statusIds[i] = id;

        if (deleteStatus(data, status.getId())) {
            rowsDeleted++;
        }
    }

    // Insert a gap.
    final boolean deletedOldGap = rowsDeleted > 0 && ArrayUtils.contains(statusIds, mMaxId);
    final boolean noRowsDeleted = rowsDeleted == 0;
    final boolean insertGap = minId > 0 && (noRowsDeleted || deletedOldGap) && !truncated && !noItemsBefore
            && statuses.size() > 1;
    for (int i = 0, j = statuses.size(); i < j; i++) {
        final Status status = statuses.get(i);
        data.add(new ParcelableStatus(status, mAccountId, insertGap && isGapEnabled() && minIdx == i));
    }

    final ParcelableStatus[] array = data.toArray(new ParcelableStatus[data.size()]);
    for (int i = 0, size = array.length; i < size; i++) {
        final ParcelableStatus status = array[i];
        if (shouldFilterStatus(mDatabase, status) && !status.is_gap && i != size - 1) {
            deleteStatus(data, status.id);
        }
    }
    if (mComparator != null) {
        Collections.sort(data, mComparator);
    } else {
        Collections.sort(data);
    }
    saveCachedData(serializationFile, data);
    return new CopyOnWriteArrayList<>(data);
}

From source file:org.mariotaku.twidere.loader.Twitter4JStatusesLoader.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  w w w . j av  a  2  s .  c  o m*/
public final List<ParcelableStatus> loadInBackground() {
    final File serializationFile = getSerializationFile();
    final List<ParcelableStatus> data = getData();
    if (isFirstLoad() && getTabPosition() >= 0 && serializationFile != null) {
        final List<ParcelableStatus> cached = getCachedData(serializationFile);
        if (cached != null) {
            data.addAll(cached);
            Collections.sort(data);
            return new CopyOnWriteArrayList<ParcelableStatus>(data);
        }
    }
    final List<Status> statuses;
    final boolean truncated;
    final Context context = getContext();
    final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT);
    try {
        final Paging paging = new Paging();
        paging.setCount(loadItemLimit);
        if (mMaxId > 0) {
            paging.setMaxId(mMaxId);
        }
        if (mSinceId > 0) {
            paging.setSinceId(mSinceId - 1);
        }
        statuses = new ArrayList<Status>();
        truncated = truncateStatuses(getStatuses(getTwitter(), paging), statuses, mSinceId);
    } catch (final TwitterException e) {
        // mHandler.post(new ShowErrorRunnable(e));
        e.printStackTrace();
        return new CopyOnWriteArrayList<ParcelableStatus>(data);
    }
    final long minStatusId = statuses.isEmpty() ? -1 : Collections.min(statuses).getId();
    final boolean insertGap = minStatusId > 0 && statuses.size() > 1 && !data.isEmpty() && !truncated;
    mHandler.post(CacheUsersStatusesTask.getRunnable(context, new StatusListResponse(mAccountId, statuses)));
    for (final Status status : statuses) {
        final long id = status.getId();
        final boolean deleted = deleteStatus(data, id);
        data.add(new ParcelableStatus(status, mAccountId, minStatusId == id && insertGap && !deleted,
                mHiResProfileImage));
    }
    Collections.sort(data);
    final ParcelableStatus[] array = data.toArray(new ParcelableStatus[data.size()]);
    for (int i = 0, size = array.length; i < size; i++) {
        final ParcelableStatus status = array[i];
        if (shouldFilterStatus(mDatabase, status) && !status.is_gap && i != size - 1) {
            deleteStatus(data, status.id);
        }
    }
    saveCachedData(serializationFile, data);
    return new CopyOnWriteArrayList<ParcelableStatus>(data);
}

From source file:org.mariotaku.twidere.loader.Twitter4JStatusLoader.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from  w w w. j av  a  2s  . c  o  m
public SynchronizedStateSavedList<ParcelableStatus, Long> loadInBackground() {
    final SynchronizedStateSavedList<ParcelableStatus, Long> data = getData();
    List<Status> statuses = null;
    final Context context = getContext();
    final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    final int load_item_limit = prefs.getInt(PREFERENCE_KEY_LOAD_ITEM_LIMIT,
            PREFERENCE_DEFAULT_LOAD_ITEM_LIMIT);
    try {
        final Paging paging = new Paging();
        paging.setCount(load_item_limit);
        if (mMaxId > 0) {
            paging.setMaxId(mMaxId);
        }
        if (mSinceId > 0) {
            paging.setSinceId(mSinceId);
        }
        statuses = getStatuses(paging);
    } catch (final TwitterException e) {
        e.printStackTrace();
    }
    if (statuses != null) {
        final boolean insert_gap = load_item_limit == statuses.size() && data.size() > 0;
        final Status min_status = statuses.size() > 0 ? Collections.min(statuses) : null;
        final long min_status_id = min_status != null ? min_status.getId() : -1;
        if (context instanceof Activity) {
            ((Activity) context).runOnUiThread(
                    CacheUsersStatusesTask.getRunnable(context, new StatusListResponse(mAccountId, statuses)));
        }
        for (final Status status : statuses) {
            final long id = status.getId();
            deleteStatus(id);
            data.add(new ParcelableStatus(status, mAccountId,
                    min_status_id > 0 && min_status_id == id && insert_gap, mHiResProfileImage,
                    mLargeInlineImagePreview));
        }
    }
    try {
        final List<ParcelableStatus> statuses_to_remove = new ArrayList<ParcelableStatus>();
        for (final ParcelableStatus status : data) {
            if (isFiltered(mDatabase, status) && !status.is_gap) {
                statuses_to_remove.add(status);
            }
        }
        data.removeAll(statuses_to_remove);
        Collections.sort(data);
    } catch (final ConcurrentModificationException e) {
        Log.w(LOGTAG, e);
    }
    return data;
}

From source file:org.tweetalib.android.TwitterPaging.java

License:Apache License

public Paging getT4JPaging() {
    Paging result = new Paging();
    if (mMaxId == null && mSinceId == null) {

        if (mPage != null) {
            result.setPage(mPage);/*w  w w  .j  a va  2  s. c o  m*/
        } else {
            result.setPage(1);
        }
    } else {
        if (mMaxId != null) {
            if (mMaxId.longValue() >= 0) {
                result.setMaxId(mMaxId);
            } else {
                Log.d("ERROR", "mMaxId is " + mMaxId.longValue() + ", must be >= 0");
            }
        }
        if (mSinceId != null) {
            if (mSinceId.longValue() >= 0) {
                result.setSinceId(mSinceId);
            } else {
                Log.d("ERROR", "mSinceId is " + mSinceId.longValue() + ", must be >= 0");
            }
        }
    }

    if (mCount != null) {
        result.setCount(mCount);
    } else {
        result.setCount(DEFAULT_STATUS_COUNT);
    }

    return result;
}

From source file:org.wso2.cep.uima.demo.TweetExtractor.java

License:Open Source License

/***
 *
 *//* w  w  w  . j a  v a 2 s  . co  m*/
private void retrieveTweets(int maxTweets) {

    Paging paging;

    // set the lowest value of the tweet ID initially to one less than Long.MAX_VALUE
    long min_id = Long.MAX_VALUE - 1;
    int count = 0;
    int index = 0;
    boolean maxValueReached = false;

    logger.info("Started Extracting Tweets of user: " + userToSearch);
    // iterate through the timeline untill the iteration returns no tweets

    while (true) {
        try {

            //count = tweetList.size();

            // paging tweets at a rate of 100 per page
            paging = new Paging(1, 100);

            // if this is not the first iteration set the new min_id value for the page
            if (count != 0) {
                logger.info("Extracted Tweet Count : " + count);
                paging.setMaxId(min_id - 1);
            }

            // get a page of the tweet timeline with tweets with ids less than the min_id value
            List<Status> tweetTempList = twitterApp.getUserTimeline(userToSearch, paging);

            // iterate the results and add to tweetList
            for (Status s : tweetTempList) {
                if (count == maxTweets) {
                    maxValueReached = true;
                    break;
                }
                count++;
                Tweet tweet = new Tweet(s.getId(), s.getCreatedAt(), s.getText());
                tweetList.add(tweet);
                logger.debug(" " + (index++) + " " + tweet.toString());

                // set the value for the min value for the next iteration
                if (s.getId() < min_id) {
                    min_id = s.getId();
                }
            }

            // if the results for this iteration is zero, means we have reached the API limit or we have extracted the maximum
            // possible, so break
            if (tweetTempList.size() == 0 || maxValueReached) {
                break;
            }

        } catch (TwitterException e) {
            e.printStackTrace();
            break;
        } catch (Exception e) {
            e.printStackTrace();
            break;
        }
    }

}