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.app.smartspace.uitemplatedata;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.app.smartspace.SmartspaceTarget;
23 import android.app.smartspace.SmartspaceUtils;
24 import android.os.Parcel;
25 
26 import java.util.Objects;
27 
28 /**
29  * Holds all the relevant data needed to render a Smartspace card with the head-to-head Ui Template.
30  *
31  * This template will add a head-to-head card within the default-template card:
32  * <ul>
33  *                     <li> head-to-head title </li>
34  *     <li> first-competitor icon       second-competitor icon </li>
35  *     <li> first-competitor text       second-competitor text </li>
36  * </ul>
37  *
38  * @hide
39  */
40 @SystemApi
41 public final class HeadToHeadTemplateData extends BaseTemplateData {
42 
43     @Nullable
44     private final Text mHeadToHeadTitle;
45     @Nullable
46     private final Icon mHeadToHeadFirstCompetitorIcon;
47     @Nullable
48     private final Icon mHeadToHeadSecondCompetitorIcon;
49     @Nullable
50     private final Text mHeadToHeadFirstCompetitorText;
51     @Nullable
52     private final Text mHeadToHeadSecondCompetitorText;
53 
54     /** Tap action for the head-to-head secondary card. */
55     @Nullable
56     private final TapAction mHeadToHeadAction;
57 
HeadToHeadTemplateData(@onNull Parcel in)58     HeadToHeadTemplateData(@NonNull Parcel in) {
59         super(in);
60         mHeadToHeadTitle = in.readTypedObject(Text.CREATOR);
61         mHeadToHeadFirstCompetitorIcon = in.readTypedObject(Icon.CREATOR);
62         mHeadToHeadSecondCompetitorIcon = in.readTypedObject(Icon.CREATOR);
63         mHeadToHeadFirstCompetitorText = in.readTypedObject(Text.CREATOR);
64         mHeadToHeadSecondCompetitorText = in.readTypedObject(Text.CREATOR);
65         mHeadToHeadAction = in.readTypedObject(TapAction.CREATOR);
66     }
67 
HeadToHeadTemplateData(@martspaceTarget.UiTemplateType int templateType, @Nullable SubItemInfo primaryItem, @Nullable SubItemInfo subtitleItem, @Nullable SubItemInfo subtitleSupplementalItem, @Nullable SubItemInfo supplementalLineItem, @Nullable SubItemInfo supplementalAlarmItem, int layoutWeight, @Nullable Text headToHeadTitle, @Nullable Icon headToHeadFirstCompetitorIcon, @Nullable Icon headToHeadSecondCompetitorIcon, @Nullable Text headToHeadFirstCompetitorText, @Nullable Text headToHeadSecondCompetitorText, @Nullable TapAction headToHeadAction)68     private HeadToHeadTemplateData(@SmartspaceTarget.UiTemplateType int templateType,
69             @Nullable SubItemInfo primaryItem,
70             @Nullable SubItemInfo subtitleItem,
71             @Nullable SubItemInfo subtitleSupplementalItem,
72             @Nullable SubItemInfo supplementalLineItem,
73             @Nullable SubItemInfo supplementalAlarmItem,
74             int layoutWeight,
75             @Nullable Text headToHeadTitle,
76             @Nullable Icon headToHeadFirstCompetitorIcon,
77             @Nullable Icon headToHeadSecondCompetitorIcon,
78             @Nullable Text headToHeadFirstCompetitorText,
79             @Nullable Text headToHeadSecondCompetitorText,
80             @Nullable TapAction headToHeadAction) {
81         super(templateType, primaryItem, subtitleItem, subtitleSupplementalItem,
82                 supplementalLineItem, supplementalAlarmItem, layoutWeight);
83 
84         mHeadToHeadTitle = headToHeadTitle;
85         mHeadToHeadFirstCompetitorIcon = headToHeadFirstCompetitorIcon;
86         mHeadToHeadSecondCompetitorIcon = headToHeadSecondCompetitorIcon;
87         mHeadToHeadFirstCompetitorText = headToHeadFirstCompetitorText;
88         mHeadToHeadSecondCompetitorText = headToHeadSecondCompetitorText;
89         mHeadToHeadAction = headToHeadAction;
90     }
91 
92     /** Returns the head-to-head card's title. */
93     @Nullable
getHeadToHeadTitle()94     public Text getHeadToHeadTitle() {
95         return mHeadToHeadTitle;
96     }
97 
98     /** Returns the first competitor's icon. */
99     @Nullable
getHeadToHeadFirstCompetitorIcon()100     public Icon getHeadToHeadFirstCompetitorIcon() {
101         return mHeadToHeadFirstCompetitorIcon;
102     }
103 
104     /** Returns the second competitor's icon. */
105     @Nullable
getHeadToHeadSecondCompetitorIcon()106     public Icon getHeadToHeadSecondCompetitorIcon() {
107         return mHeadToHeadSecondCompetitorIcon;
108     }
109 
110     /** Returns the first competitor's text. */
111     @Nullable
getHeadToHeadFirstCompetitorText()112     public Text getHeadToHeadFirstCompetitorText() {
113         return mHeadToHeadFirstCompetitorText;
114     }
115 
116     /** Returns the second competitor's text. */
117     @Nullable
getHeadToHeadSecondCompetitorText()118     public Text getHeadToHeadSecondCompetitorText() {
119         return mHeadToHeadSecondCompetitorText;
120     }
121 
122     /** Returns the head-to-head card's tap action. */
123     @Nullable
getHeadToHeadAction()124     public TapAction getHeadToHeadAction() {
125         return mHeadToHeadAction;
126     }
127 
128     /**
129      * @see Parcelable.Creator
130      */
131     @NonNull
132     public static final Creator<HeadToHeadTemplateData> CREATOR =
133             new Creator<HeadToHeadTemplateData>() {
134                 @Override
135                 public HeadToHeadTemplateData createFromParcel(Parcel in) {
136                     return new HeadToHeadTemplateData(in);
137                 }
138 
139                 @Override
140                 public HeadToHeadTemplateData[] newArray(int size) {
141                     return new HeadToHeadTemplateData[size];
142                 }
143             };
144 
145     @Override
describeContents()146     public int describeContents() {
147         return 0;
148     }
149 
150     @Override
writeToParcel(@onNull Parcel out, int flags)151     public void writeToParcel(@NonNull Parcel out, int flags) {
152         super.writeToParcel(out, flags);
153         out.writeTypedObject(mHeadToHeadTitle, flags);
154         out.writeTypedObject(mHeadToHeadFirstCompetitorIcon, flags);
155         out.writeTypedObject(mHeadToHeadSecondCompetitorIcon, flags);
156         out.writeTypedObject(mHeadToHeadFirstCompetitorText, flags);
157         out.writeTypedObject(mHeadToHeadSecondCompetitorText, flags);
158         out.writeTypedObject(mHeadToHeadAction, flags);
159     }
160 
161     @Override
equals(Object o)162     public boolean equals(Object o) {
163         if (this == o) return true;
164         if (!(o instanceof HeadToHeadTemplateData)) return false;
165         if (!super.equals(o)) return false;
166         HeadToHeadTemplateData that = (HeadToHeadTemplateData) o;
167         return SmartspaceUtils.isEqual(mHeadToHeadTitle, that.mHeadToHeadTitle) && Objects.equals(
168                 mHeadToHeadFirstCompetitorIcon, that.mHeadToHeadFirstCompetitorIcon)
169                 && Objects.equals(
170                 mHeadToHeadSecondCompetitorIcon, that.mHeadToHeadSecondCompetitorIcon)
171                 && SmartspaceUtils.isEqual(mHeadToHeadFirstCompetitorText,
172                 that.mHeadToHeadFirstCompetitorText)
173                 && SmartspaceUtils.isEqual(mHeadToHeadSecondCompetitorText,
174                 that.mHeadToHeadSecondCompetitorText)
175                 && Objects.equals(
176                 mHeadToHeadAction, that.mHeadToHeadAction);
177     }
178 
179     @Override
hashCode()180     public int hashCode() {
181         return Objects.hash(super.hashCode(), mHeadToHeadTitle, mHeadToHeadFirstCompetitorIcon,
182                 mHeadToHeadSecondCompetitorIcon, mHeadToHeadFirstCompetitorText,
183                 mHeadToHeadSecondCompetitorText,
184                 mHeadToHeadAction);
185     }
186 
187     @Override
toString()188     public String toString() {
189         return super.toString() + " + SmartspaceHeadToHeadUiTemplateData{"
190                 + "mH2HTitle=" + mHeadToHeadTitle
191                 + ", mH2HFirstCompetitorIcon=" + mHeadToHeadFirstCompetitorIcon
192                 + ", mH2HSecondCompetitorIcon=" + mHeadToHeadSecondCompetitorIcon
193                 + ", mH2HFirstCompetitorText=" + mHeadToHeadFirstCompetitorText
194                 + ", mH2HSecondCompetitorText=" + mHeadToHeadSecondCompetitorText
195                 + ", mH2HAction=" + mHeadToHeadAction
196                 + '}';
197     }
198 
199     /**
200      * A builder for {@link HeadToHeadTemplateData} object.
201      *
202      * @hide
203      */
204     @SystemApi
205     public static final class Builder extends BaseTemplateData.Builder {
206 
207         private Text mHeadToHeadTitle;
208         private Icon mHeadToHeadFirstCompetitorIcon;
209         private Icon mHeadToHeadSecondCompetitorIcon;
210         private Text mHeadToHeadFirstCompetitorText;
211         private Text mHeadToHeadSecondCompetitorText;
212         private TapAction mHeadToHeadAction;
213 
214         /**
215          * A builder for {@link HeadToHeadTemplateData}.
216          */
Builder()217         public Builder() {
218             super(SmartspaceTarget.UI_TEMPLATE_HEAD_TO_HEAD);
219         }
220 
221         /**
222          * Sets the head-to-head card's title
223          */
224         @NonNull
setHeadToHeadTitle(@ullable Text headToHeadTitle)225         public Builder setHeadToHeadTitle(@Nullable Text headToHeadTitle) {
226             mHeadToHeadTitle = headToHeadTitle;
227             return this;
228         }
229 
230         /**
231          * Sets the head-to-head card's first competitor icon
232          */
233         @NonNull
setHeadToHeadFirstCompetitorIcon( @ullable Icon headToHeadFirstCompetitorIcon)234         public Builder setHeadToHeadFirstCompetitorIcon(
235                 @Nullable Icon headToHeadFirstCompetitorIcon) {
236             mHeadToHeadFirstCompetitorIcon = headToHeadFirstCompetitorIcon;
237             return this;
238         }
239 
240         /**
241          * Sets the head-to-head card's second competitor icon
242          */
243         @NonNull
setHeadToHeadSecondCompetitorIcon( @ullable Icon headToHeadSecondCompetitorIcon)244         public Builder setHeadToHeadSecondCompetitorIcon(
245                 @Nullable Icon headToHeadSecondCompetitorIcon) {
246             mHeadToHeadSecondCompetitorIcon = headToHeadSecondCompetitorIcon;
247             return this;
248         }
249 
250         /**
251          * Sets the head-to-head card's first competitor text
252          */
253         @NonNull
setHeadToHeadFirstCompetitorText( @ullable Text headToHeadFirstCompetitorText)254         public Builder setHeadToHeadFirstCompetitorText(
255                 @Nullable Text headToHeadFirstCompetitorText) {
256             mHeadToHeadFirstCompetitorText = headToHeadFirstCompetitorText;
257             return this;
258         }
259 
260         /**
261          * Sets the head-to-head card's second competitor text
262          */
263         @NonNull
setHeadToHeadSecondCompetitorText( @ullable Text headToHeadSecondCompetitorText)264         public Builder setHeadToHeadSecondCompetitorText(
265                 @Nullable Text headToHeadSecondCompetitorText) {
266             mHeadToHeadSecondCompetitorText = headToHeadSecondCompetitorText;
267             return this;
268         }
269 
270         /**
271          * Sets the head-to-head card's tap action
272          */
273         @NonNull
setHeadToHeadAction(@ullable TapAction headToHeadAction)274         public Builder setHeadToHeadAction(@Nullable TapAction headToHeadAction) {
275             mHeadToHeadAction = headToHeadAction;
276             return this;
277         }
278 
279         /**
280          * Builds a new SmartspaceHeadToHeadUiTemplateData instance.
281          */
282         @NonNull
build()283         public HeadToHeadTemplateData build() {
284             return new HeadToHeadTemplateData(getTemplateType(), getPrimaryItem(),
285                     getSubtitleItem(), getSubtitleSupplemtnalItem(),
286                     getSupplementalLineItem(), getSupplementalAlarmItem(), getLayoutWeight(),
287                     mHeadToHeadTitle,
288                     mHeadToHeadFirstCompetitorIcon,
289                     mHeadToHeadSecondCompetitorIcon, mHeadToHeadFirstCompetitorText,
290                     mHeadToHeadSecondCompetitorText,
291                     mHeadToHeadAction);
292         }
293     }
294 }
295