Example usage for twitter4j Paging setCount

List of usage examples for twitter4j Paging setCount

Introduction

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

Prototype

public void setCount(int count) 

Source Link

Usage

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*  w  ww  .  j a v 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);
            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.Twitter4JActivitiesLoader.java

License:Open Source License

@Override
public final List<ParcelableActivity> loadInBackground() {
    final File serializationFile = getSerializationFile();
    if (mIsFirstLoad && mTabPosition >= 0 && serializationFile != null) {
        final List<ParcelableActivity> cached = getCachedData(serializationFile);
        if (cached != null) {
            Collections.sort(cached);
            return new CopyOnWriteArrayList<ParcelableActivity>(cached);
        }//from   w  ww  .j a va 2 s .  c o m
    }
    final SharedPreferences prefs = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
            Context.MODE_PRIVATE);
    final int loadItemLimit = prefs.getInt(KEY_LOAD_ITEM_LIMIT, DEFAULT_LOAD_ITEM_LIMIT);
    final List<Activity> activities;
    try {
        final Paging paging = new Paging();
        paging.setCount(Math.min(100, loadItemLimit));
        activities = getActivities(getTwitter(), paging);
    } catch (final TwitterException e) {
        e.printStackTrace();
        final List<ParcelableActivity> cached = getCachedData(serializationFile);
        if (cached == null)
            return Collections.emptyList();
        return new CopyOnWriteArrayList<ParcelableActivity>(cached);
    }
    if (activities == null)
        return new CopyOnWriteArrayList<ParcelableActivity>(mData);
    final List<ParcelableActivity> result = new ArrayList<ParcelableActivity>();
    for (final Activity activity : activities) {
        result.add(new ParcelableActivity(activity, mAccountId, mHiResProfileImage));
    }
    Collections.sort(result);
    saveCachedData(serializationFile, result);
    return new CopyOnWriteArrayList<ParcelableActivity>(result);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//ww  w . ja v  a  2s. 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);
            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 www. j  a v a  2s.  c  om*/
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.mariotaku.twidere.loader.UserRetweetedStatusLoader.java

License:Open Source License

@Override
public List<ParcelableUser> getUsers() throws TwitterException {
    final Twitter twitter = getTwitter();
    if (twitter == null)
        return null;
    if (mStatusId > 0) {
        final Paging paging = new Paging();
        paging.setCount(mLoadItemLimit);
        if (mPage > 0) {
            paging.setPage(mPage);/*w  w w.jav a 2s  . c o m*/
        }
        final ResponseList<User> users = twitter.getRetweetedBy(mStatusId, paging);
        if (users == null)
            return null;
        final List<ParcelableUser> result = new ArrayList<ParcelableUser>();
        final int users_list_size = mUsersList.size();
        final int size = users.size();
        for (int i = 0; i < size; i++) {
            final User user = users.get(i);
            result.add(new ParcelableUser(user, getAccountId(), users_list_size + i));
        }
        return result;
    }
    return null;
}

From source file:org.rhq.plugins.twitter.FeedComponent.java

License:Open Source License

/**
 * Gather measurement data/*from ww w  .  jav a2s . c o  m*/
 *  @see org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport, java.util.Set)
 */
public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception {

    for (MeasurementScheduleRequest req : metrics) {
        if (req.getName().equals("tweetCount")) {
            Twitter twitter = tFactory.getInstance();
            Paging paging = new Paging();

            MeasurementDataNumeric res;
            if (isSearch) {
                Query q = new Query(keyword);
                q.setSinceId(lastId);
                if (lastId == NOT_YET_SET)
                    q.setRpp(1);
                else
                    q.setRpp(20);
                QueryResult qr = twitter.search(q);
                List<Tweet> tweets = qr.getTweets();
                res = new MeasurementDataNumeric(req, (double) tweets.size());

                eventPoller.addTweets(tweets);
                if (tweets.size() > 0)
                    lastId = tweets.get(0).getId();
            } else {
                List<Status> statuses;
                if (lastId == NOT_YET_SET) {
                    paging.setCount(1);
                } else {
                    paging.setCount(100);
                }
                paging.setSinceId(lastId);
                statuses = twitter.getUserTimeline(keyword, paging);
                res = new MeasurementDataNumeric(req, (double) statuses.size());

                eventPoller.addStatuses(statuses);
                if (statuses.size() > 0)
                    lastId = statuses.get(0).getId();

            }
            report.addData(res);
        }
    }
}

From source file:org.rhq.plugins.twitter.TwitterComponent.java

License:Open Source License

/**
 * Gather measurement data/*from  w ww  .  j a  va 2  s.  c o m*/
 *  @see org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport, java.util.Set)
 */
public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) throws Exception {

    Twitter twitter = createTwitterInstance();

    for (MeasurementScheduleRequest req : metrics) {
        if (req.getName().equals("tweetCount")) {

            //             Twitter twitter = new Twitter(username,password,serverUrl);
            Paging paging = new Paging();
            if (lastId == NOT_YET_SET) {
                paging.setSinceId(1);
                paging.setCount(1);
            } else {
                paging.setSinceId(lastId);
                paging.setCount(100);
            }
            List<Status> statuses;
            statuses = twitter.getHomeTimeline(paging);
            if (lastId > 0) {
                MeasurementDataNumeric res;
                res = new MeasurementDataNumeric(req, (double) statuses.size());

                eventPoller.addStatuses(statuses);
                report.addData(res);
            }
            if (statuses.size() > 0)
                lastId = statuses.get(0).getId(); // This is always newest first
        } else if (req.getName().equals("followerCount")) {
            int count = twitter.getFollowersIDs(-1).getIDs().length;
            MeasurementDataNumeric res;
            res = new MeasurementDataNumeric(req, (double) count);
            report.addData(res);
        }
    }
}

From source file:org.sociotech.communitymashup.source.twitter.TwitterSourceService.java

License:Open Source License

/**
 * Adds the User Timeline//  ww  w . j  a  va2 s  .  c o m
 */
private void addUserTimeline() {

    List<Status> userTimeline = null;

    // List of statuses
    try {
        Paging userTimelineParam = new Paging();
        userTimelineParam.setCount(getNumberOfUserTimelineTweets());
        userTimeline = twitterAPI.getUserTimeline(userTimelineParam);
    } catch (TwitterException te) {
        log("Could not get User Timeline from Twitter. (" + te.getMessage() + ")", LogService.LOG_ERROR);
        return;
    }

    addTweetList(userTimeline);
}

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);//ww  w. ja v a 2s  .com
        } 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:proyectotwitter.AppTwitter.java

public void verTimeline() {
    try {//from   www . j  ava2  s .co m
        Paging pagina = new Paging();
        pagina.setCount(40);
        ResponseList listado = twitter.getHomeTimeline(pagina);
        for (int i = 0; i < listado.size(); i++) {
            System.out.println(listado.get(i).toString());
        }
    } catch (TwitterException ex) {
        System.out.println("Error al ver la linea de tiempo");
    }
}