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