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 
20 import android.app.PendingIntent;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.annotation.StringDef;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 
33 /**
34  * Base class for all quick controls elements.
35  */
36 public abstract class QCItem implements Parcelable {
37     public static final String QC_TYPE_LIST = "QC_TYPE_LIST";
38     public static final String QC_TYPE_ROW = "QC_TYPE_ROW";
39     public static final String QC_TYPE_TILE = "QC_TYPE_TILE";
40     public static final String QC_TYPE_SLIDER = "QC_TYPE_SLIDER";
41     public static final String QC_TYPE_ACTION_SWITCH = "QC_TYPE_ACTION_SWITCH";
42     public static final String QC_TYPE_ACTION_TOGGLE = "QC_TYPE_ACTION_TOGGLE";
43 
44     public static final String QC_ACTION_TOGGLE_STATE = "QC_ACTION_TOGGLE_STATE";
45     public static final String QC_ACTION_SLIDER_VALUE = "QC_ACTION_SLIDER_VALUE";
46 
47     @StringDef(value = {
48             QC_TYPE_LIST,
49             QC_TYPE_ROW,
50             QC_TYPE_TILE,
51             QC_TYPE_SLIDER,
52             QC_TYPE_ACTION_SWITCH,
53             QC_TYPE_ACTION_TOGGLE,
54     })
55     @Retention(RetentionPolicy.SOURCE)
56     public @interface QCItemType {
57     }
58 
59     private final String mType;
60     private final boolean mIsEnabled;
61     private final boolean mIsClickableWhileDisabled;
62     private ActionHandler mActionHandler;
63     private ActionHandler mDisabledClickActionHandler;
64 
QCItem(@onNull @CItemType String type)65     public QCItem(@NonNull @QCItemType String type) {
66         this(type, /* isEnabled= */true, /* isClickableWhileDisabled= */ false);
67     }
68 
QCItem(@onNull @CItemType String type, boolean isEnabled, boolean isClickableWhileDisabled)69     public QCItem(@NonNull @QCItemType String type, boolean isEnabled,
70             boolean isClickableWhileDisabled) {
71         mType = type;
72         mIsEnabled = isEnabled;
73         mIsClickableWhileDisabled = isClickableWhileDisabled;
74     }
75 
QCItem(@onNull Parcel in)76     public QCItem(@NonNull Parcel in) {
77         mType = in.readString();
78         mIsEnabled = in.readBoolean();
79         mIsClickableWhileDisabled = in.readBoolean();
80     }
81 
82     @NonNull
83     @QCItemType
getType()84     public String getType() {
85         return mType;
86     }
87 
isEnabled()88     public boolean isEnabled() {
89         return mIsEnabled;
90     }
91 
isClickableWhileDisabled()92     public boolean isClickableWhileDisabled() {
93         return mIsClickableWhileDisabled;
94     }
95 
96     @Override
describeContents()97     public int describeContents() {
98         return 0;
99     }
100 
101     @Override
writeToParcel(Parcel dest, int flags)102     public void writeToParcel(Parcel dest, int flags) {
103         dest.writeString(mType);
104         dest.writeBoolean(mIsEnabled);
105         dest.writeBoolean(mIsClickableWhileDisabled);
106     }
107 
setActionHandler(@ullable ActionHandler handler)108     public void setActionHandler(@Nullable ActionHandler handler) {
109         mActionHandler = handler;
110     }
111 
setDisabledClickActionHandler(@ullable ActionHandler handler)112     public void setDisabledClickActionHandler(@Nullable ActionHandler handler) {
113         mDisabledClickActionHandler = handler;
114     }
115 
116     @Nullable
getActionHandler()117     public ActionHandler getActionHandler() {
118         return mActionHandler;
119     }
120 
121     @Nullable
getDisabledClickActionHandler()122     public ActionHandler getDisabledClickActionHandler() {
123         return mDisabledClickActionHandler;
124     }
125 
126     /**
127      * Returns the PendingIntent that is sent when the item is clicked.
128      */
129     @Nullable
getPrimaryAction()130     public abstract PendingIntent getPrimaryAction();
131 
132     /**
133      * Returns the PendingIntent that is sent when the item is clicked while disabled.
134      */
135     @Nullable
getDisabledClickAction()136     public abstract PendingIntent getDisabledClickAction();
137 
138     /**
139      * Action handler that can listen for an action to occur and notify listeners.
140      */
141     public interface ActionHandler {
142         /**
143          * Callback when an action occurs.
144          * @param item the QCItem that sent the action
145          * @param context the context for the action
146          * @param intent the intent that was sent with the action
147          */
onAction(@onNull QCItem item, @NonNull Context context, @NonNull Intent intent)148         void onAction(@NonNull QCItem item, @NonNull Context context, @NonNull Intent intent);
149 
isActivity()150         default boolean isActivity() {
151             return false;
152         }
153     }
154 }
155