1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.graphics.pdf.models.jni; 18 19 import android.annotation.FlaggedApi; 20 import android.graphics.Rect; 21 import android.graphics.RectF; 22 import android.graphics.pdf.content.PdfPageTextContent; 23 import android.graphics.pdf.flags.Flags; 24 25 import java.util.List; 26 import java.util.stream.Collectors; 27 28 /** 29 * Represents text selection on a particular page of a PDF coming from the JNI layer. 30 * 31 * @hide 32 */ 33 // TODO(b/324536951): Remove this class after updating the native code to directly use 34 // android.graphics.pdf.models.PageSelection 35 public class PageSelection { 36 /** The page the selection is on. */ 37 private final int mPage; 38 39 /** The left edge of the selection - index is inclusive. */ 40 private final SelectionBoundary mLeft; 41 42 /** The right edge of the selection - index is exclusive. */ 43 private final SelectionBoundary mRight; 44 45 /** The bounding boxes of the highlighted text. */ 46 private final List<Rect> mBounds; 47 48 /** The highlighted text. */ 49 private final String mText; 50 PageSelection(int page, SelectionBoundary left, SelectionBoundary right, List<Rect> rects, String text)51 public PageSelection(int page, SelectionBoundary left, SelectionBoundary right, 52 List<Rect> rects, String text) { 53 this.mPage = page; 54 this.mLeft = left; 55 this.mRight = right; 56 this.mBounds = rects; 57 this.mText = text; 58 } 59 getPage()60 public int getPage() { 61 return mPage; 62 } 63 getLeft()64 public SelectionBoundary getLeft() { 65 return mLeft; 66 } 67 getRight()68 public SelectionBoundary getRight() { 69 return mRight; 70 } 71 getBounds()72 public List<Rect> getBounds() { 73 return mBounds; 74 } 75 getText()76 public String getText() { 77 return mText; 78 } 79 80 /** Converts JNI models to the public class */ 81 @FlaggedApi(Flags.FLAG_ENABLE_PDF_VIEWER) convert()82 public android.graphics.pdf.models.selection.PageSelection convert() { 83 PdfPageTextContent selectedTextContent = new PdfPageTextContent(mText, 84 mBounds.stream().map(RectF::new).collect(Collectors.toList())); 85 return new android.graphics.pdf.models.selection.PageSelection(mPage, mLeft.convert(), 86 mRight.convert(), 87 /* selectedContents = */ List.of(selectedTextContent)); 88 } 89 }