1 /*
2  * Copyright (C) 2022 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.safetycenter;
18 
19 import static android.os.Build.VERSION_CODES.TIRAMISU;
20 
21 import static java.util.Objects.requireNonNull;
22 
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25 import android.annotation.SystemApi;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 
29 import androidx.annotation.RequiresApi;
30 
31 import java.util.Objects;
32 
33 /**
34  * Contains either a single {@link SafetyCenterEntry} or a group of them in a {@link
35  * SafetyCenterEntryGroup}.
36  *
37  * @hide
38  */
39 @SystemApi
40 @RequiresApi(TIRAMISU)
41 public final class SafetyCenterEntryOrGroup implements Parcelable {
42 
43     @NonNull
44     public static final Creator<SafetyCenterEntryOrGroup> CREATOR =
45             new Creator<SafetyCenterEntryOrGroup>() {
46                 @Override
47                 public SafetyCenterEntryOrGroup createFromParcel(Parcel in) {
48                     SafetyCenterEntry maybeEntry = in.readTypedObject(SafetyCenterEntry.CREATOR);
49                     SafetyCenterEntryGroup maybeEntryGroup =
50                             in.readTypedObject(SafetyCenterEntryGroup.CREATOR);
51                     return maybeEntry != null
52                             ? new SafetyCenterEntryOrGroup(maybeEntry)
53                             : new SafetyCenterEntryOrGroup(maybeEntryGroup);
54                 }
55 
56                 @Override
57                 public SafetyCenterEntryOrGroup[] newArray(int size) {
58                     return new SafetyCenterEntryOrGroup[size];
59                 }
60             };
61 
62     @Nullable private final SafetyCenterEntry mEntry;
63     @Nullable private final SafetyCenterEntryGroup mEntryGroup;
64 
65     /** Create for a {@link SafetyCenterEntry}. */
SafetyCenterEntryOrGroup(@onNull SafetyCenterEntry entry)66     public SafetyCenterEntryOrGroup(@NonNull SafetyCenterEntry entry) {
67         mEntry = requireNonNull(entry);
68         mEntryGroup = null;
69     }
70 
71     /** Create for a {@link SafetyCenterEntryGroup}. */
SafetyCenterEntryOrGroup(@onNull SafetyCenterEntryGroup entryGroup)72     public SafetyCenterEntryOrGroup(@NonNull SafetyCenterEntryGroup entryGroup) {
73         mEntry = null;
74         mEntryGroup = requireNonNull(entryGroup);
75     }
76 
77     /**
78      * Returns the {@link SafetyCenterEntry} if this holder contains one, {@code null} otherwise.
79      *
80      * <p>If this returns {@code null}, {@link #getEntryGroup()} must return a non-null value.
81      */
82     @Nullable
getEntry()83     public SafetyCenterEntry getEntry() {
84         return mEntry;
85     }
86 
87     /**
88      * Returns the {@link SafetyCenterEntryGroup} if this holder contains one, {@code null}
89      * otherwise.
90      *
91      * <p>If this returns {@code null}, {@link #getEntry()} must return a non-null value.
92      */
93     @Nullable
getEntryGroup()94     public SafetyCenterEntryGroup getEntryGroup() {
95         return mEntryGroup;
96     }
97 
98     @Override
equals(Object o)99     public boolean equals(Object o) {
100         if (this == o) return true;
101         if (!(o instanceof SafetyCenterEntryOrGroup)) return false;
102         SafetyCenterEntryOrGroup that = (SafetyCenterEntryOrGroup) o;
103         return Objects.equals(mEntry, that.mEntry) && Objects.equals(mEntryGroup, that.mEntryGroup);
104     }
105 
106     @Override
hashCode()107     public int hashCode() {
108         return Objects.hash(mEntry, mEntryGroup);
109     }
110 
111     @Override
toString()112     public String toString() {
113         return "SafetyCenterEntryOrGroup{"
114                 + "mEntry="
115                 + mEntry
116                 + ", mEntryGroup="
117                 + mEntryGroup
118                 + '}';
119     }
120 
121     @Override
describeContents()122     public int describeContents() {
123         return 0;
124     }
125 
126     @Override
writeToParcel(@onNull Parcel dest, int flags)127     public void writeToParcel(@NonNull Parcel dest, int flags) {
128         dest.writeTypedObject(mEntry, flags);
129         dest.writeTypedObject(mEntryGroup, flags);
130     }
131 }
132