use of android.print.PageRange in project android_frameworks_base by ResurrectionRemix.
the class PageRangeUtils method getNormalizedPageCount.
/**
* Gets the number of pages in a normalized range array.
*
* @param pageRanges Normalized page ranges.
* @param layoutPageCount Page count after reported after layout pass.
* @return The page count in the ranges.
*/
public static int getNormalizedPageCount(PageRange[] pageRanges, int layoutPageCount) {
int pageCount = 0;
if (pageRanges != null) {
final int pageRangeCount = pageRanges.length;
for (int i = 0; i < pageRangeCount; i++) {
PageRange pageRange = pageRanges[i];
if (PageRange.ALL_PAGES.equals(pageRange)) {
return layoutPageCount;
}
pageCount += pageRange.getSize();
}
}
return pageCount;
}
use of android.print.PageRange in project android_frameworks_base by ResurrectionRemix.
the class PageAdapter method computePageIndexInDocument.
private int computePageIndexInDocument(int indexInAdapter) {
int skippedAdapterPages = 0;
final int selectedPagesCount = mSelectedPages.length;
for (int i = 0; i < selectedPagesCount; i++) {
PageRange pageRange = PageRangeUtils.asAbsoluteRange(mSelectedPages[i], mDocumentPageCount);
skippedAdapterPages += pageRange.getSize();
if (skippedAdapterPages > indexInAdapter) {
final int overshoot = skippedAdapterPages - indexInAdapter - 1;
return pageRange.getEnd() - overshoot;
}
}
return INVALID_PAGE_INDEX;
}
use of android.print.PageRange in project android_frameworks_base by ResurrectionRemix.
the class PageAdapter method computeSelectedPages.
private PageRange[] computeSelectedPages() {
ArrayList<PageRange> selectedPagesList = new ArrayList<>();
int startPageIndex = INVALID_PAGE_INDEX;
int endPageIndex = INVALID_PAGE_INDEX;
final int pageCount = mConfirmedPagesInDocument.size();
for (int i = 0; i < pageCount; i++) {
final int pageIndex = mConfirmedPagesInDocument.keyAt(i);
if (startPageIndex == INVALID_PAGE_INDEX) {
startPageIndex = endPageIndex = pageIndex;
}
if (endPageIndex + 1 < pageIndex) {
PageRange pageRange = new PageRange(startPageIndex, endPageIndex);
selectedPagesList.add(pageRange);
startPageIndex = pageIndex;
}
endPageIndex = pageIndex;
}
if (startPageIndex != INVALID_PAGE_INDEX && endPageIndex != INVALID_PAGE_INDEX) {
PageRange pageRange = new PageRange(startPageIndex, endPageIndex);
selectedPagesList.add(pageRange);
}
PageRange[] selectedPages = new PageRange[selectedPagesList.size()];
selectedPagesList.toArray(selectedPages);
return selectedPages;
}
use of android.print.PageRange in project android_frameworks_base by ResurrectionRemix.
the class PageAdapter method computePageIndexInFile.
private int computePageIndexInFile(int pageIndexInDocument) {
if (!PageRangeUtils.contains(mSelectedPages, pageIndexInDocument)) {
return INVALID_PAGE_INDEX;
}
if (mWrittenPages == null) {
return INVALID_PAGE_INDEX;
}
int indexInFile = INVALID_PAGE_INDEX;
final int rangeCount = mWrittenPages.length;
for (int i = 0; i < rangeCount; i++) {
PageRange pageRange = mWrittenPages[i];
if (!pageRange.contains(pageIndexInDocument)) {
indexInFile += pageRange.getSize();
} else {
indexInFile += pageIndexInDocument - pageRange.getStart() + 1;
return indexInFile;
}
}
return INVALID_PAGE_INDEX;
}
use of android.print.PageRange in project android_frameworks_base by ResurrectionRemix.
the class PageAdapter method computeRequestedPages.
private PageRange[] computeRequestedPages(int pageInDocument) {
if (mRequestedPages != null && PageRangeUtils.contains(mRequestedPages, pageInDocument)) {
return mRequestedPages;
}
List<PageRange> pageRangesList = new ArrayList<>();
int remainingPagesToRequest = MAX_PREVIEW_PAGES_BATCH;
final int selectedPagesCount = mSelectedPages.length;
// We always request the pages that are bound, i.e. shown on screen.
PageRange[] boundPagesInDocument = computeBoundPagesInDocument();
final int boundRangeCount = boundPagesInDocument.length;
for (int i = 0; i < boundRangeCount; i++) {
PageRange boundRange = boundPagesInDocument[i];
pageRangesList.add(boundRange);
}
remainingPagesToRequest -= PageRangeUtils.getNormalizedPageCount(boundPagesInDocument, mDocumentPageCount);
final boolean requestFromStart = mRequestedPages == null || pageInDocument > mRequestedPages[mRequestedPages.length - 1].getEnd();
if (!requestFromStart) {
if (DEBUG) {
Log.i(LOG_TAG, "Requesting from end");
}
// Reminder that ranges are always normalized.
for (int i = selectedPagesCount - 1; i >= 0; i--) {
if (remainingPagesToRequest <= 0) {
break;
}
PageRange selectedRange = PageRangeUtils.asAbsoluteRange(mSelectedPages[i], mDocumentPageCount);
if (pageInDocument < selectedRange.getStart()) {
continue;
}
PageRange pagesInRange;
int rangeSpan;
if (selectedRange.contains(pageInDocument)) {
rangeSpan = pageInDocument - selectedRange.getStart() + 1;
rangeSpan = Math.min(rangeSpan, remainingPagesToRequest);
final int fromPage = Math.max(pageInDocument - rangeSpan - 1, 0);
rangeSpan = Math.max(rangeSpan, 0);
pagesInRange = new PageRange(fromPage, pageInDocument);
} else {
rangeSpan = selectedRange.getSize();
rangeSpan = Math.min(rangeSpan, remainingPagesToRequest);
rangeSpan = Math.max(rangeSpan, 0);
final int fromPage = Math.max(selectedRange.getEnd() - rangeSpan - 1, 0);
final int toPage = selectedRange.getEnd();
pagesInRange = new PageRange(fromPage, toPage);
}
pageRangesList.add(pagesInRange);
remainingPagesToRequest -= rangeSpan;
}
} else {
if (DEBUG) {
Log.i(LOG_TAG, "Requesting from start");
}
// Reminder that ranges are always normalized.
for (int i = 0; i < selectedPagesCount; i++) {
if (remainingPagesToRequest <= 0) {
break;
}
PageRange selectedRange = PageRangeUtils.asAbsoluteRange(mSelectedPages[i], mDocumentPageCount);
if (pageInDocument > selectedRange.getEnd()) {
continue;
}
PageRange pagesInRange;
int rangeSpan;
if (selectedRange.contains(pageInDocument)) {
rangeSpan = selectedRange.getEnd() - pageInDocument + 1;
rangeSpan = Math.min(rangeSpan, remainingPagesToRequest);
final int toPage = Math.min(pageInDocument + rangeSpan - 1, mDocumentPageCount - 1);
pagesInRange = new PageRange(pageInDocument, toPage);
} else {
rangeSpan = selectedRange.getSize();
rangeSpan = Math.min(rangeSpan, remainingPagesToRequest);
final int fromPage = selectedRange.getStart();
final int toPage = Math.min(selectedRange.getStart() + rangeSpan - 1, mDocumentPageCount - 1);
pagesInRange = new PageRange(fromPage, toPage);
}
if (DEBUG) {
Log.i(LOG_TAG, "computeRequestedPages() Adding range:" + pagesInRange);
}
pageRangesList.add(pagesInRange);
remainingPagesToRequest -= rangeSpan;
}
}
PageRange[] pageRanges = new PageRange[pageRangesList.size()];
pageRangesList.toArray(pageRanges);
return PageRangeUtils.normalize(pageRanges);
}
Aggregations