public Iterator<Integer> askServerForMore() { mCurrConf = mUnreadConfs.element(); Log.i( TAG, "PrefetchNextUnread askServerForMore mCurrConf: " + mCurrConf + " mCurrConfLocalNo: " + mCurrConfLastRead); final Membership membership; final TextMapping tm; final List<Integer> maybeUnread = new ArrayList<Integer>(); try { membership = mKom.getSession().queryReadTexts(mKom.getUserId(), mCurrConf, true); mCurrConfLastRead = lastTextReadFrom(membership, mCurrConfLastRead); Log.i(TAG, "PrefetchNextUnread mCurrConfLastRead: " + mCurrConfLastRead); final UConference conf = mKom.getSession().getUConfStat(mCurrConf); if (mCurrConfLastRead < conf.getHighestLocalNo()) { tm = mKom.getSession().localToGlobal(mCurrConf, mCurrConfLastRead + 1, ASK_AMOUNT); Log.i( TAG, "PrefetchNextUnread asked for " + ASK_AMOUNT + " texts in conf " + mCurrConf + ", got " + tm.size()); } else { Log.i(TAG, "PrefetchNextUnread too high local number in " + mCurrConf); tm = null; } } catch (final IOException e) { Log.i(TAG, "PrefetchNextUnread IOException"); return Collections.<Integer>emptyList().iterator(); } while (tm != null && tm.hasMoreElements()) { final int globalNo = (Integer) tm.nextElement(); final int localNo = tm.local(); if (!membership.isRead(localNo)) { Log.i( TAG, "PrefetchNextUnread adding localNo " + localNo + " (globalNo " + globalNo + ")"); maybeUnread.add(globalNo); } mCurrConfLastRead = Math.max(mCurrConfLastRead, localNo); } if (tm == null || !tm.laterTextsExists()) { Log.i(TAG, "PrefetchNextUnread no later texts exists in conf " + mCurrConf); mUnreadConfs.remove(); mCurrConfLastRead = -1; } return maybeUnread.iterator(); }
private int lastTextReadFrom(final Membership membership, final int from) { final List<Range> readRanges = membership.getReadRanges(); if (from < 0) { return membership.getLastTextRead(); } synchronized (readRanges) { for (final Range range : readRanges) { if (from < range.first) { return from; } else if (from <= range.last) { return range.last; } } return readRanges.get(readRanges.size() - 1).last; } }