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;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.NonNull;
21 import android.graphics.RectF;
22 import android.graphics.pdf.flags.Flags;
23 import android.graphics.pdf.utils.Preconditions;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 import java.util.List;
28 
29 /**
30  * Represents the bounds of a single search match on a page of the PDF document.
31  */
32 @FlaggedApi(Flags.FLAG_ENABLE_PDF_VIEWER)
33 public final class PageMatchBounds implements Parcelable {
34     @NonNull
35     public static final Creator<PageMatchBounds> CREATOR = new Creator<PageMatchBounds>() {
36         @Override
37         public PageMatchBounds createFromParcel(Parcel in) {
38             return new PageMatchBounds(in);
39         }
40 
41         @Override
42         public PageMatchBounds[] newArray(int size) {
43             return new PageMatchBounds[size];
44         }
45     };
46     private final List<RectF> mBounds;
47     private final int mTextStartIndex;
48 
49     /**
50      * Creates a new instance of {@link PageMatchBounds} for the text match found on the page. The
51      * match is represented by bounds of the text match and the starting index of the character
52      * "stream" (0-based index).
53      *
54      * @param bounds         Bounds of the text match.
55      * @param textStartIndex starting index of the text match.
56      * @throws NullPointerException     If bounds if null.
57      * @throws IllegalArgumentException If bounds list is empty or if the text starting index is
58      *                                  negative.
59      */
PageMatchBounds(@onNull List<RectF> bounds, int textStartIndex)60     public PageMatchBounds(@NonNull List<RectF> bounds, int textStartIndex) {
61         Preconditions.checkNotNull(bounds, "Bounds cannot be null");
62         Preconditions.checkArgument(!bounds.isEmpty(), "Match bounds cannot be empty");
63         Preconditions.checkArgument(textStartIndex >= 0, "Index cannot be negative");
64         this.mBounds = bounds;
65         this.mTextStartIndex = textStartIndex;
66     }
67 
PageMatchBounds(Parcel in)68     private PageMatchBounds(Parcel in) {
69         mBounds = in.createTypedArrayList(RectF.CREATOR);
70         mTextStartIndex = in.readInt();
71     }
72 
73     /**
74      * <p>
75      * Represents the {@link RectF} bounds of a match. Matches which are spread across multiple
76      * lines will be represented by multiple {@link RectF} in order of viewing.
77      * <p><strong>Note:</strong> The bounds only represent the coordinates of the bounds of a
78      * single line using {@link RectF}. The developer will need to render the highlighter as well
79      * as
80      * intercept the touch events for any additional UI interactions.
81      *
82      * @return list of bounds for the match on the page.
83      */
84     @NonNull
getBounds()85     public List<RectF> getBounds() {
86         return mBounds;
87     }
88 
89     /**
90      * Gets the starting index of the match found on the page. Characters in a page form a "stream"
91      * and inside the stream, each character has an index starting from 0.
92      *
93      * @return the starting index of the match on the page.
94      */
getTextStartIndex()95     public int getTextStartIndex() {
96         return mTextStartIndex;
97     }
98 
99     @Override
describeContents()100     public int describeContents() {
101         return 0;
102     }
103 
104     @Override
writeToParcel(@ndroidx.annotation.NonNull Parcel dest, int flags)105     public void writeToParcel(@androidx.annotation.NonNull Parcel dest, int flags) {
106         dest.writeTypedList(mBounds);
107         dest.writeInt(mTextStartIndex);
108     }
109 }
110 
111