1 /*
2  * Copyright (C) 2021 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 com.android.car.qc;
18 
19 import android.app.PendingIntent;
20 import android.graphics.drawable.Icon;
21 import android.os.Parcel;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 
26 /**
27  * Quick Control Tile Element
28  * ------------
29  * | -------- |
30  * | | Icon | |
31  * | -------- |
32  * | Subtitle |
33  * ------------
34  */
35 public class QCTile extends QCItem {
36     private final boolean mIsChecked;
37     private final boolean mIsAvailable;
38     private final String mSubtitle;
39     private Icon mIcon;
40     private PendingIntent mAction;
41     private PendingIntent mDisabledClickAction;
42 
QCTile(boolean isChecked, boolean isEnabled, boolean isAvailable, boolean isClickableWhileDisabled, @Nullable String subtitle, @Nullable Icon icon, @Nullable PendingIntent action, @Nullable PendingIntent disabledClickAction)43     public QCTile(boolean isChecked, boolean isEnabled, boolean isAvailable,
44             boolean isClickableWhileDisabled, @Nullable String subtitle, @Nullable Icon icon,
45             @Nullable PendingIntent action, @Nullable PendingIntent disabledClickAction) {
46         super(QC_TYPE_TILE, isEnabled, isClickableWhileDisabled);
47         mIsChecked = isChecked;
48         mIsAvailable = isAvailable;
49         mSubtitle = subtitle;
50         mIcon = icon;
51         mAction = action;
52         mDisabledClickAction = disabledClickAction;
53     }
54 
QCTile(@onNull Parcel in)55     public QCTile(@NonNull Parcel in) {
56         super(in);
57         mIsChecked = in.readBoolean();
58         mIsAvailable = in.readBoolean();
59         mSubtitle = in.readString();
60         boolean hasIcon = in.readBoolean();
61         if (hasIcon) {
62             mIcon = Icon.CREATOR.createFromParcel(in);
63         }
64         boolean hasAction = in.readBoolean();
65         if (hasAction) {
66             mAction = PendingIntent.CREATOR.createFromParcel(in);
67         }
68         boolean hasDisabledClickAction = in.readBoolean();
69         if (hasDisabledClickAction) {
70             mDisabledClickAction = PendingIntent.CREATOR.createFromParcel(in);
71         }
72     }
73 
74     @Override
writeToParcel(Parcel dest, int flags)75     public void writeToParcel(Parcel dest, int flags) {
76         super.writeToParcel(dest, flags);
77         dest.writeBoolean(mIsChecked);
78         dest.writeBoolean(mIsAvailable);
79         dest.writeString(mSubtitle);
80         boolean hasIcon = mIcon != null;
81         dest.writeBoolean(hasIcon);
82         if (hasIcon) {
83             mIcon.writeToParcel(dest, flags);
84         }
85         boolean hasAction = mAction != null;
86         dest.writeBoolean(hasAction);
87         if (hasAction) {
88             mAction.writeToParcel(dest, flags);
89         }
90         boolean hasDisabledClickAction = mDisabledClickAction != null;
91         dest.writeBoolean(hasDisabledClickAction);
92         if (hasDisabledClickAction) {
93             mDisabledClickAction.writeToParcel(dest, flags);
94         }
95     }
96 
97     @Override
getPrimaryAction()98     public PendingIntent getPrimaryAction() {
99         return mAction;
100     }
101 
102     @Override
getDisabledClickAction()103     public PendingIntent getDisabledClickAction() {
104         return mDisabledClickAction;
105     }
106 
isChecked()107     public boolean isChecked() {
108         return mIsChecked;
109     }
110 
isAvailable()111     public boolean isAvailable() {
112         return mIsAvailable;
113     }
114 
115     @Nullable
getSubtitle()116     public String getSubtitle() {
117         return mSubtitle;
118     }
119 
120     @Nullable
getIcon()121     public Icon getIcon() {
122         return mIcon;
123     }
124 
125     public static Creator<QCTile> CREATOR = new Creator<QCTile>() {
126         @Override
127         public QCTile createFromParcel(Parcel source) {
128             return new QCTile(source);
129         }
130 
131         @Override
132         public QCTile[] newArray(int size) {
133             return new QCTile[size];
134         }
135     };
136 
137     /**
138      * Builder for {@link QCTile}.
139      */
140     public static class Builder {
141         private boolean mIsChecked;
142         private boolean mIsEnabled = true;
143         private boolean mIsAvailable = true;
144         private boolean mIsClickableWhileDisabled = false;
145         private String mSubtitle;
146         private Icon mIcon;
147         private PendingIntent mAction;
148         private PendingIntent mDisabledClickAction;
149 
150         /**
151          * Sets whether or not the tile should be checked.
152          */
setChecked(boolean checked)153         public Builder setChecked(boolean checked) {
154             mIsChecked = checked;
155             return this;
156         }
157 
158         /**
159          * Sets whether or not the tile should be enabled.
160          */
setEnabled(boolean enabled)161         public Builder setEnabled(boolean enabled) {
162             mIsEnabled = enabled;
163             return this;
164         }
165 
166         /**
167          * Sets whether or not the action item is available.
168          */
setAvailable(boolean available)169         public Builder setAvailable(boolean available) {
170             mIsAvailable = available;
171             return this;
172         }
173 
174         /**
175          * Sets whether or not a tile should be clickable while disabled.
176          */
setClickableWhileDisabled(boolean clickable)177         public Builder setClickableWhileDisabled(boolean clickable) {
178             mIsClickableWhileDisabled = clickable;
179             return this;
180         }
181 
182         /**
183          * Sets the tile's subtitle.
184          */
setSubtitle(@ullable String subtitle)185         public Builder setSubtitle(@Nullable String subtitle) {
186             mSubtitle = subtitle;
187             return this;
188         }
189 
190         /**
191          * Sets the tile's icon.
192          */
setIcon(@ullable Icon icon)193         public Builder setIcon(@Nullable Icon icon) {
194             mIcon = icon;
195             return this;
196         }
197 
198         /**
199          * Sets the PendingIntent to be sent when the tile is clicked.
200          */
setAction(@ullable PendingIntent action)201         public Builder setAction(@Nullable PendingIntent action) {
202             mAction = action;
203             return this;
204         }
205 
206         /**
207          * Sets the PendingIntent to be sent when the action item is clicked while disabled.
208          */
setDisabledClickAction(@ullable PendingIntent action)209         public Builder setDisabledClickAction(@Nullable PendingIntent action) {
210             mDisabledClickAction = action;
211             return this;
212         }
213 
214         /**
215          * Builds the final {@link QCTile}.
216          */
build()217         public QCTile build() {
218             return new QCTile(mIsChecked, mIsEnabled, mIsAvailable, mIsClickableWhileDisabled,
219                     mSubtitle, mIcon, mAction, mDisabledClickAction);
220         }
221     }
222 }
223