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 Action that are includes as either start or end actions in {@link QCRow} 28 */ 29 public class QCActionItem extends QCItem { 30 private final boolean mIsChecked; 31 private final boolean mIsAvailable; 32 private Icon mIcon; 33 private PendingIntent mAction; 34 private PendingIntent mDisabledClickAction; 35 QCActionItem(@onNull @CItemType String type, boolean isChecked, boolean isEnabled, boolean isAvailable, boolean isClickableWhileDisabled, @Nullable Icon icon, @Nullable PendingIntent action, @Nullable PendingIntent disabledClickAction)36 public QCActionItem(@NonNull @QCItemType String type, boolean isChecked, boolean isEnabled, 37 boolean isAvailable, boolean isClickableWhileDisabled, @Nullable Icon icon, 38 @Nullable PendingIntent action, @Nullable PendingIntent disabledClickAction) { 39 super(type, isEnabled, isClickableWhileDisabled); 40 mIsChecked = isChecked; 41 mIsAvailable = isAvailable; 42 mIcon = icon; 43 mAction = action; 44 mDisabledClickAction = disabledClickAction; 45 } 46 QCActionItem(@onNull Parcel in)47 public QCActionItem(@NonNull Parcel in) { 48 super(in); 49 mIsChecked = in.readBoolean(); 50 mIsAvailable = in.readBoolean(); 51 boolean hasIcon = in.readBoolean(); 52 if (hasIcon) { 53 mIcon = Icon.CREATOR.createFromParcel(in); 54 } 55 boolean hasAction = in.readBoolean(); 56 if (hasAction) { 57 mAction = PendingIntent.CREATOR.createFromParcel(in); 58 } 59 boolean hasDisabledClickAction = in.readBoolean(); 60 if (hasDisabledClickAction) { 61 mDisabledClickAction = PendingIntent.CREATOR.createFromParcel(in); 62 } 63 } 64 65 @Override writeToParcel(Parcel dest, int flags)66 public void writeToParcel(Parcel dest, int flags) { 67 super.writeToParcel(dest, flags); 68 dest.writeBoolean(mIsChecked); 69 dest.writeBoolean(mIsAvailable); 70 boolean includeIcon = getType().equals(QC_TYPE_ACTION_TOGGLE) && mIcon != null; 71 dest.writeBoolean(includeIcon); 72 if (includeIcon) { 73 mIcon.writeToParcel(dest, flags); 74 } 75 boolean hasAction = mAction != null; 76 dest.writeBoolean(hasAction); 77 if (hasAction) { 78 mAction.writeToParcel(dest, flags); 79 } 80 boolean hasDisabledClickAction = mDisabledClickAction != null; 81 dest.writeBoolean(hasDisabledClickAction); 82 if (hasDisabledClickAction) { 83 mDisabledClickAction.writeToParcel(dest, flags); 84 } 85 } 86 87 @Override getPrimaryAction()88 public PendingIntent getPrimaryAction() { 89 return mAction; 90 } 91 92 @Override getDisabledClickAction()93 public PendingIntent getDisabledClickAction() { 94 return mDisabledClickAction; 95 } 96 isChecked()97 public boolean isChecked() { 98 return mIsChecked; 99 } 100 isAvailable()101 public boolean isAvailable() { 102 return mIsAvailable; 103 } 104 105 @Nullable getIcon()106 public Icon getIcon() { 107 return mIcon; 108 } 109 110 public static Creator<QCActionItem> CREATOR = new Creator<QCActionItem>() { 111 @Override 112 public QCActionItem createFromParcel(Parcel source) { 113 return new QCActionItem(source); 114 } 115 116 @Override 117 public QCActionItem[] newArray(int size) { 118 return new QCActionItem[size]; 119 } 120 }; 121 122 /** 123 * Builder for {@link QCActionItem}. 124 */ 125 public static class Builder { 126 private final String mType; 127 private boolean mIsChecked; 128 private boolean mIsEnabled = true; 129 private boolean mIsAvailable = true; 130 private boolean mIsClickableWhileDisabled = false; 131 private Icon mIcon; 132 private PendingIntent mAction; 133 private PendingIntent mDisabledClickAction; 134 Builder(@onNull @CItemType String type)135 public Builder(@NonNull @QCItemType String type) { 136 if (!isValidType(type)) { 137 throw new IllegalArgumentException("Invalid QCActionItem type provided" + type); 138 } 139 mType = type; 140 } 141 142 /** 143 * Sets whether or not the action item should be checked. 144 */ setChecked(boolean checked)145 public Builder setChecked(boolean checked) { 146 mIsChecked = checked; 147 return this; 148 } 149 150 /** 151 * Sets whether or not the action item should be enabled. 152 */ setEnabled(boolean enabled)153 public Builder setEnabled(boolean enabled) { 154 mIsEnabled = enabled; 155 return this; 156 } 157 158 /** 159 * Sets whether or not the action item is available. 160 */ setAvailable(boolean available)161 public Builder setAvailable(boolean available) { 162 mIsAvailable = available; 163 return this; 164 } 165 166 /** 167 * Sets whether or not an action item should be clickable while disabled. 168 */ setClickableWhileDisabled(boolean clickable)169 public Builder setClickableWhileDisabled(boolean clickable) { 170 mIsClickableWhileDisabled = clickable; 171 return this; 172 } 173 174 /** 175 * Sets the icon for {@link QC_TYPE_ACTION_TOGGLE} actions 176 */ setIcon(@ullable Icon icon)177 public Builder setIcon(@Nullable Icon icon) { 178 mIcon = icon; 179 return this; 180 } 181 182 /** 183 * Sets the PendingIntent to be sent when the action item is clicked. 184 */ setAction(@ullable PendingIntent action)185 public Builder setAction(@Nullable PendingIntent action) { 186 mAction = action; 187 return this; 188 } 189 190 /** 191 * Sets the PendingIntent to be sent when the action item is clicked while disabled. 192 */ setDisabledClickAction(@ullable PendingIntent action)193 public Builder setDisabledClickAction(@Nullable PendingIntent action) { 194 mDisabledClickAction = action; 195 return this; 196 } 197 198 /** 199 * Builds the final {@link QCActionItem}. 200 */ build()201 public QCActionItem build() { 202 return new QCActionItem(mType, mIsChecked, mIsEnabled, mIsAvailable, 203 mIsClickableWhileDisabled, mIcon, mAction, mDisabledClickAction); 204 } 205 isValidType(String type)206 private boolean isValidType(String type) { 207 return type.equals(QC_TYPE_ACTION_SWITCH) || type.equals(QC_TYPE_ACTION_TOGGLE); 208 } 209 } 210 } 211