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 sub-card Ui Template.
30  *
31  * This template will add a sub-card card within the default-template card:
32  * <ul>
33  *     <li> sub-card icon </li>
34  *     <li> sub-card text </li>
35  * </ul>
36  *
37  * @hide
38  */
39 @SystemApi
40 public final class SubCardTemplateData extends BaseTemplateData {
41 
42     /** Icon for the sub-card. */
43     @NonNull
44     private final Icon mSubCardIcon;
45 
46     /** Text for the sub-card, which shows below the icon when being set. */
47     @Nullable
48     private final Text mSubCardText;
49 
50     /** Tap action for the sub-card secondary card. */
51     @Nullable
52     private final TapAction mSubCardAction;
53 
SubCardTemplateData(@onNull Parcel in)54     SubCardTemplateData(@NonNull Parcel in) {
55         super(in);
56         mSubCardIcon = in.readTypedObject(Icon.CREATOR);
57         mSubCardText = in.readTypedObject(Text.CREATOR);
58         mSubCardAction = in.readTypedObject(TapAction.CREATOR);
59     }
60 
SubCardTemplateData(int templateType, @Nullable SubItemInfo primaryItem, @Nullable SubItemInfo subtitleItem, @Nullable SubItemInfo subtitleSupplementalItem, @Nullable SubItemInfo supplementalLineItem, @Nullable SubItemInfo supplementalAlarmItem, int layoutWeight, @NonNull Icon subCardIcon, @Nullable Text subCardText, @Nullable TapAction subCardAction)61     private SubCardTemplateData(int templateType,
62             @Nullable SubItemInfo primaryItem,
63             @Nullable SubItemInfo subtitleItem,
64             @Nullable SubItemInfo subtitleSupplementalItem,
65             @Nullable SubItemInfo supplementalLineItem,
66             @Nullable SubItemInfo supplementalAlarmItem,
67             int layoutWeight,
68             @NonNull Icon subCardIcon,
69             @Nullable Text subCardText,
70             @Nullable TapAction subCardAction) {
71         super(templateType, primaryItem, subtitleItem, subtitleSupplementalItem,
72                 supplementalLineItem, supplementalAlarmItem, layoutWeight);
73 
74         mSubCardIcon = subCardIcon;
75         mSubCardText = subCardText;
76         mSubCardAction = subCardAction;
77     }
78 
79     /** Returns the sub-card card's icon. */
80     @NonNull
getSubCardIcon()81     public Icon getSubCardIcon() {
82         return mSubCardIcon;
83     }
84 
85     /** Returns the sub-card card's text. */
86     @Nullable
getSubCardText()87     public Text getSubCardText() {
88         return mSubCardText;
89     }
90 
91     /** Returns the sub-card card's tap action. */
92     @Nullable
getSubCardAction()93     public TapAction getSubCardAction() {
94         return mSubCardAction;
95     }
96 
97     /**
98      * @see Parcelable.Creator
99      */
100     @NonNull
101     public static final Creator<SubCardTemplateData> CREATOR =
102             new Creator<SubCardTemplateData>() {
103                 @Override
104                 public SubCardTemplateData createFromParcel(Parcel in) {
105                     return new SubCardTemplateData(in);
106                 }
107 
108                 @Override
109                 public SubCardTemplateData[] newArray(int size) {
110                     return new SubCardTemplateData[size];
111                 }
112             };
113 
114     @Override
describeContents()115     public int describeContents() {
116         return 0;
117     }
118 
119     @Override
writeToParcel(@onNull Parcel out, int flags)120     public void writeToParcel(@NonNull Parcel out, int flags) {
121         super.writeToParcel(out, flags);
122         out.writeTypedObject(mSubCardIcon, flags);
123         out.writeTypedObject(mSubCardText, flags);
124         out.writeTypedObject(mSubCardAction, flags);
125     }
126 
127     @Override
equals(Object o)128     public boolean equals(Object o) {
129         if (this == o) return true;
130         if (!(o instanceof SubCardTemplateData)) return false;
131         if (!super.equals(o)) return false;
132         SubCardTemplateData that = (SubCardTemplateData) o;
133         return mSubCardIcon.equals(that.mSubCardIcon) && SmartspaceUtils.isEqual(mSubCardText,
134                 that.mSubCardText) && Objects.equals(mSubCardAction,
135                 that.mSubCardAction);
136     }
137 
138     @Override
hashCode()139     public int hashCode() {
140         return Objects.hash(super.hashCode(), mSubCardIcon, mSubCardText, mSubCardAction);
141     }
142 
143     @Override
toString()144     public String toString() {
145         return super.toString() + " + SmartspaceSubCardUiTemplateData{"
146                 + "mSubCardIcon=" + mSubCardIcon
147                 + ", mSubCardText=" + mSubCardText
148                 + ", mSubCardAction=" + mSubCardAction
149                 + '}';
150     }
151 
152     /**
153      * A builder for {@link SubCardTemplateData} object.
154      *
155      * @hide
156      */
157     @SystemApi
158     public static final class Builder extends BaseTemplateData.Builder {
159 
160         private final Icon mSubCardIcon;
161         private Text mSubCardText;
162         private TapAction mSubCardAction;
163 
164         /**
165          * A builder for {@link SubCardTemplateData}.
166          */
Builder(@onNull Icon subCardIcon)167         public Builder(@NonNull Icon subCardIcon) {
168             super(SmartspaceTarget.UI_TEMPLATE_SUB_CARD);
169             mSubCardIcon = Objects.requireNonNull(subCardIcon);
170         }
171 
172         /**
173          * Sets the card text.
174          */
175         @NonNull
setSubCardText(@onNull Text subCardText)176         public Builder setSubCardText(@NonNull Text subCardText) {
177             mSubCardText = subCardText;
178             return this;
179         }
180 
181         /**
182          * Sets the card tap action.
183          */
184         @NonNull
setSubCardAction(@onNull TapAction subCardAction)185         public Builder setSubCardAction(@NonNull TapAction subCardAction) {
186             mSubCardAction = subCardAction;
187             return this;
188         }
189 
190         /**
191          * Builds a new SmartspaceSubCardUiTemplateData instance.
192          */
193         @NonNull
build()194         public SubCardTemplateData build() {
195             return new SubCardTemplateData(getTemplateType(), getPrimaryItem(),
196                     getSubtitleItem(), getSubtitleSupplemtnalItem(),
197                     getSupplementalLineItem(), getSupplementalAlarmItem(), getLayoutWeight(),
198                     mSubCardIcon,
199                     mSubCardText,
200                     mSubCardAction);
201         }
202     }
203 }
204