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 package android.app.smartspace;
17 
18 import android.annotation.IntDef;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * A representation of a smartspace event.
30  *
31  * @hide
32  */
33 @SystemApi
34 public final class SmartspaceTargetEvent implements Parcelable {
35 
36     /**
37      * User interacted with the target.
38      */
39     public static final int EVENT_TARGET_INTERACTION = 1;
40 
41     /**
42      * Smartspace target was brought into view.
43      */
44     public static final int EVENT_TARGET_SHOWN = 2;
45     /**
46      * Smartspace target went out of view.
47      */
48     public static final int EVENT_TARGET_HIDDEN = 3;
49     /**
50      * A dismiss action was issued by the user.
51      */
52     public static final int EVENT_TARGET_DISMISS = 4;
53     /**
54      * A block action was issued by the user.
55      */
56     public static final int EVENT_TARGET_BLOCK = 5;
57     /**
58      * The Ui surface came into view.
59      */
60     public static final int EVENT_UI_SURFACE_SHOWN = 6;
61     /**
62      * The Ui surface went out of view.
63      */
64     public static final int EVENT_UI_SURFACE_HIDDEN = 7;
65 
66     /**
67      * @see Parcelable.Creator
68      */
69     @NonNull
70     public static final Creator<SmartspaceTargetEvent> CREATOR =
71             new Creator<SmartspaceTargetEvent>() {
72                 public SmartspaceTargetEvent createFromParcel(Parcel parcel) {
73                     return new SmartspaceTargetEvent(parcel);
74                 }
75 
76                 public SmartspaceTargetEvent[] newArray(int size) {
77                     return new SmartspaceTargetEvent[size];
78                 }
79             };
80 
81     @Nullable
82     private final SmartspaceTarget mSmartspaceTarget;
83 
84     @Nullable
85     private final String mSmartspaceActionId;
86 
87     @EventType
88     private final int mEventType;
89 
SmartspaceTargetEvent(@ullable SmartspaceTarget smartspaceTarget, @Nullable String smartspaceActionId, @EventType int eventType)90     private SmartspaceTargetEvent(@Nullable SmartspaceTarget smartspaceTarget,
91             @Nullable String smartspaceActionId,
92             @EventType int eventType) {
93         mSmartspaceTarget = smartspaceTarget;
94         mSmartspaceActionId = smartspaceActionId;
95         mEventType = eventType;
96     }
97 
SmartspaceTargetEvent(Parcel parcel)98     private SmartspaceTargetEvent(Parcel parcel) {
99         mSmartspaceTarget = parcel.readParcelable(null, android.app.smartspace.SmartspaceTarget.class);
100         mSmartspaceActionId = parcel.readString();
101         mEventType = parcel.readInt();
102     }
103 
104     /**
105      * Get the {@link SmartspaceTarget} associated with this event.
106      */
107     @Nullable
getSmartspaceTarget()108     public SmartspaceTarget getSmartspaceTarget() {
109         return mSmartspaceTarget;
110     }
111 
112     /**
113      * Get the action id of the Smartspace Action associated with this event.
114      */
115     @Nullable
getSmartspaceActionId()116     public String getSmartspaceActionId() {
117         return mSmartspaceActionId;
118     }
119 
120     /**
121      * Get the {@link EventType} of this event.
122      */
123     @NonNull
124     @EventType
getEventType()125     public int getEventType() {
126         return mEventType;
127     }
128 
129     @Override
describeContents()130     public int describeContents() {
131         return 0;
132     }
133 
134     @Override
writeToParcel(@onNull Parcel dest, int flags)135     public void writeToParcel(@NonNull Parcel dest, int flags) {
136         dest.writeParcelable(mSmartspaceTarget, flags);
137         dest.writeString(mSmartspaceActionId);
138         dest.writeInt(mEventType);
139     }
140 
141     @Override
toString()142     public String toString() {
143         return "SmartspaceTargetEvent{"
144                 + "mSmartspaceTarget=" + mSmartspaceTarget
145                 + ", mSmartspaceActionId='" + mSmartspaceActionId + '\''
146                 + ", mEventType=" + mEventType
147                 + '}';
148     }
149 
150     /**
151      * @hide
152      */
153     @IntDef(prefix = {"EVENT_"}, value = {
154             EVENT_TARGET_INTERACTION,
155             EVENT_TARGET_SHOWN,
156             EVENT_TARGET_HIDDEN,
157             EVENT_TARGET_DISMISS,
158             EVENT_TARGET_BLOCK,
159             EVENT_UI_SURFACE_SHOWN,
160             EVENT_UI_SURFACE_HIDDEN
161     })
162     @Retention(RetentionPolicy.SOURCE)
163     public @interface EventType {
164     }
165 
166     /**
167      * A builder for {@link SmartspaceTargetEvent}.
168      *
169      * @hide
170      */
171     @SystemApi
172     public static final class Builder {
173         @EventType
174         private final int mEventType;
175         @Nullable
176         private SmartspaceTarget mSmartspaceTarget;
177         @Nullable
178         private String mSmartspaceActionId;
179 
180         /**
181          * A builder for {@link SmartspaceTargetEvent}.
182          */
Builder(@ventType int eventType)183         public Builder(@EventType int eventType) {
184             mEventType = eventType;
185         }
186 
187         /**
188          * Sets the SmartspaceTarget for this event.
189          */
190         @NonNull
setSmartspaceTarget(@onNull SmartspaceTarget smartspaceTarget)191         public Builder setSmartspaceTarget(@NonNull SmartspaceTarget smartspaceTarget) {
192             mSmartspaceTarget = smartspaceTarget;
193             return this;
194         }
195 
196         /**
197          * Sets the Smartspace action id for this event.
198          */
199         @NonNull
setSmartspaceActionId(@onNull String smartspaceActionId)200         public Builder setSmartspaceActionId(@NonNull String smartspaceActionId) {
201             mSmartspaceActionId = smartspaceActionId;
202             return this;
203         }
204 
205         /**
206          * Builds a new {@link SmartspaceTargetEvent} instance.
207          */
208         @NonNull
build()209         public SmartspaceTargetEvent build() {
210             return new SmartspaceTargetEvent(mSmartspaceTarget, mSmartspaceActionId, mEventType);
211         }
212     }
213 }
214