1 /*
2  * Copyright (C) 2018 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.prediction;
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 import java.util.Objects;
28 
29 /**
30  * A representation of an app target event.
31  *
32  * @hide
33  */
34 @SystemApi
35 public final class AppTargetEvent implements Parcelable {
36 
37     /**
38      * @hide
39      */
40     @IntDef({ACTION_LAUNCH, ACTION_DISMISS, ACTION_PIN, ACTION_UNPIN})
41     @Retention(RetentionPolicy.SOURCE)
42     public @interface ActionType {}
43 
44     /**
45      * Event type constant indicating an app target has been launched.
46      */
47     public static final int ACTION_LAUNCH = 1;
48 
49     /**
50      * Event type constant indicating an app target has been dismissed.
51      */
52     public static final int ACTION_DISMISS = 2;
53 
54     /**
55      * Event type constant indicating an app target has been pinned.
56      */
57     public static final int ACTION_PIN = 3;
58 
59     /**
60      * Event type constant indicating an app target has been un-pinned.
61      */
62     public static final int ACTION_UNPIN = 4;
63 
64     /**
65      * Event type constant indicating an app target has been un-dismissed.
66      */
67     public static final int ACTION_UNDISMISS = 5;
68 
69     private final AppTarget mTarget;
70     private final String mLocation;
71     private final int mAction;
72 
AppTargetEvent(@ullable AppTarget target, @Nullable String location, @ActionType int actionType)73     private AppTargetEvent(@Nullable AppTarget target, @Nullable String location,
74             @ActionType int actionType) {
75         mTarget = target;
76         mLocation = location;
77         mAction = actionType;
78     }
79 
AppTargetEvent(Parcel parcel)80     private AppTargetEvent(Parcel parcel) {
81         mTarget = parcel.readParcelable(null, android.app.prediction.AppTarget.class);
82         mLocation = parcel.readString();
83         mAction = parcel.readInt();
84     }
85 
86     /**
87      * Returns the app target.
88      */
89     @Nullable
getTarget()90     public AppTarget getTarget() {
91         return mTarget;
92     }
93 
94     /**
95      * Returns the launch location.
96      */
97     @Nullable
getLaunchLocation()98     public String getLaunchLocation() {
99         return mLocation;
100     }
101 
102     /**
103      * Returns the action type.
104      */
getAction()105     public @ActionType int getAction() {
106         return mAction;
107     }
108 
109     @Override
equals(@ullable Object o)110     public boolean equals(@Nullable Object o) {
111         if (!getClass().equals(o != null ? o.getClass() : null)) return false;
112 
113         AppTargetEvent other = (AppTargetEvent) o;
114         return mTarget.equals(other.mTarget)
115                 && mLocation.equals(other.mLocation)
116                 && mAction == other.mAction;
117     }
118 
119     @Override
hashCode()120     public int hashCode() {
121         int hashCode = Objects.hash(mTarget, mLocation);
122         hashCode = 31 * hashCode + mAction;
123         return hashCode;
124     }
125 
126     @Override
describeContents()127     public int describeContents() {
128         return 0;
129     }
130 
131     @Override
writeToParcel(Parcel dest, int flags)132     public void writeToParcel(Parcel dest, int flags) {
133         dest.writeParcelable(mTarget, 0);
134         dest.writeString(mLocation);
135         dest.writeInt(mAction);
136     }
137 
138     public static final @android.annotation.NonNull Creator<AppTargetEvent> CREATOR =
139             new Creator<AppTargetEvent>() {
140                 public AppTargetEvent createFromParcel(Parcel parcel) {
141                     return new AppTargetEvent(parcel);
142                 }
143 
144                 public AppTargetEvent[] newArray(int size) {
145                     return new AppTargetEvent[size];
146                 }
147             };
148 
149     /**
150      * A builder for app target events.
151      *
152      * @hide
153      */
154     @SystemApi
155     public static final class Builder {
156         private AppTarget mTarget;
157         private String mLocation;
158         private @ActionType int mAction;
159 
160         /**
161          * @param target The app target that is associated with this event.
162          * @param actionType The event type, which is one of the values in {@link ActionType}.
163          */
Builder(@ullable AppTarget target, @ActionType int actionType)164         public Builder(@Nullable AppTarget target, @ActionType int actionType) {
165             mTarget = target;
166             mAction = actionType;
167         }
168 
169         /**
170          * Sets the launch location.
171          */
172         @NonNull
setLaunchLocation(@ullable String location)173         public Builder setLaunchLocation(@Nullable String location) {
174             mLocation = location;
175             return this;
176         }
177 
178         /**
179          * Builds a new event instance.
180          */
181         @NonNull
build()182         public AppTargetEvent build() {
183             return new AppTargetEvent(mTarget, mLocation, mAction);
184         }
185     }
186 }
187