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.pdf.flags.Flags;
22 import android.graphics.pdf.utils.Preconditions;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import java.util.Objects;
27 
28 /** Represents a single option in a combo box or list box PDF form widget. */
29 @FlaggedApi(Flags.FLAG_ENABLE_FORM_FILLING)
30 public final class ListItem implements Parcelable {
31     @NonNull
32     public static final Creator<ListItem> CREATOR =
33             new Creator<ListItem>() {
34                 @Override
35                 public ListItem createFromParcel(Parcel in) {
36                     return new ListItem(in);
37                 }
38 
39                 @Override
40                 public ListItem[] newArray(int size) {
41                     return new ListItem[size];
42                 }
43             };
44 
45     private final String mLabel;
46     private final boolean mSelected;
47 
48     /**
49      * Creates a new choice option with the specified label, and selected state.
50      *
51      * @param label    Label for choice option.
52      * @param selected Determines if the option is selected or not.
53      * @throws NullPointerException if {@code label} is null
54      */
ListItem(@onNull String label, boolean selected)55     public ListItem(@NonNull String label, boolean selected) {
56         Preconditions.checkNotNull(label, "Label cannot be null");
57         this.mLabel = label;
58         this.mSelected = selected;
59     }
60 
ListItem(@onNull Parcel in)61     private ListItem(@NonNull Parcel in) {
62         mLabel = in.readString();
63         mSelected = in.readInt() != 0;
64     }
65 
66     /** @return the label for the choice option in the list */
67     @NonNull
getLabel()68     public String getLabel() {
69         return mLabel;
70     }
71 
72     /** @return {@code true} if the choice option is selected in the list */
isSelected()73     public boolean isSelected() {
74         return mSelected;
75     }
76 
77     @Override
hashCode()78     public int hashCode() {
79         return Objects.hash(mLabel, mSelected);
80     }
81 
82     @Override
equals(Object obj)83     public boolean equals(Object obj) {
84         if (obj instanceof ListItem other) {
85             return mLabel.equals(other.mLabel) && mSelected == other.mSelected;
86         }
87         return false;
88     }
89 
90     @Override
toString()91     public String toString() {
92         return "ChoiceOption{" + "\tlabel=" + mLabel + "\n\tselected=" + mSelected + "\n" + "}";
93     }
94 
95     @Override
describeContents()96     public int describeContents() {
97         return 0;
98     }
99 
100     @Override
writeToParcel(@onNull Parcel dest, int flags)101     public void writeToParcel(@NonNull Parcel dest, int flags) {
102         dest.writeString(mLabel);
103         dest.writeInt(mSelected ? 1 : 0);
104     }
105 }
106