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.IntRange;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.annotation.TestApi;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 
28 /**
29  * Class that provides contextual information about the environment in which the app prediction is
30  * used, such as package name, UI in which the app targets are shown, and number of targets.
31  *
32  * @hide
33  */
34 @SystemApi
35 @TestApi
36 public final class AppPredictionContext implements Parcelable {
37 
38     private final int mPredictedTargetCount;
39     @NonNull
40     private final String mUiSurface;
41     @NonNull
42     private final String mPackageName;
43     @Nullable
44     private final Bundle mExtras;
45 
AppPredictionContext(@onNull String uiSurface, int numPredictedTargets, @NonNull String packageName, @Nullable Bundle extras)46     private AppPredictionContext(@NonNull String uiSurface, int numPredictedTargets,
47             @NonNull String packageName, @Nullable Bundle extras) {
48         mUiSurface = uiSurface;
49         mPredictedTargetCount = numPredictedTargets;
50         mPackageName = packageName;
51         mExtras = extras;
52     }
53 
AppPredictionContext(@onNull Parcel parcel)54     private AppPredictionContext(@NonNull Parcel parcel) {
55         mUiSurface = parcel.readString();
56         mPredictedTargetCount = parcel.readInt();
57         mPackageName = parcel.readString();
58         mExtras = parcel.readBundle();
59     }
60 
61     /**
62      * Returns the UI surface of the prediction context.
63      */
64     @NonNull
getUiSurface()65     public String getUiSurface() {
66         return mUiSurface;
67     }
68 
69     /**
70      * Returns the predicted target count
71      */
getPredictedTargetCount()72     public @IntRange(from = 0) int getPredictedTargetCount() {
73         return mPredictedTargetCount;
74     }
75 
76     /**
77      * Returns the package name of the prediction context.
78      */
79     @NonNull
getPackageName()80     public String getPackageName() {
81         return mPackageName;
82     }
83 
84     /**
85      * Returns the extras of the prediction context.
86      */
87     @Nullable
getExtras()88     public Bundle getExtras() {
89         return mExtras;
90     }
91 
92     @Override
equals(@ullable Object o)93     public boolean equals(@Nullable Object o) {
94         if (o == this) return true;
95         if (!getClass().equals(o != null ? o.getClass() : null)) return false;
96 
97         AppPredictionContext other = (AppPredictionContext) o;
98         return mPredictedTargetCount == other.mPredictedTargetCount
99                 && mUiSurface.equals(other.mUiSurface)
100                 && mPackageName.equals(other.mPackageName);
101     }
102 
103     @Override
describeContents()104     public int describeContents() {
105         return 0;
106     }
107 
108     @Override
writeToParcel(@onNull Parcel dest, int flags)109     public void writeToParcel(@NonNull Parcel dest, int flags) {
110         dest.writeString(mUiSurface);
111         dest.writeInt(mPredictedTargetCount);
112         dest.writeString(mPackageName);
113         dest.writeBundle(mExtras);
114     }
115 
116     public static final @android.annotation.NonNull Parcelable.Creator<AppPredictionContext> CREATOR =
117             new Parcelable.Creator<AppPredictionContext>() {
118                 public AppPredictionContext createFromParcel(Parcel parcel) {
119                     return new AppPredictionContext(parcel);
120                 }
121 
122                 public AppPredictionContext[] newArray(int size) {
123                     return new AppPredictionContext[size];
124                 }
125             };
126 
127     /**
128      * A builder for app prediction contexts.
129      * @hide
130      */
131     @SystemApi
132     @TestApi
133     public static final class Builder {
134 
135         @NonNull
136         private final String mPackageName;
137 
138         private int mPredictedTargetCount;
139         @Nullable
140         private String mUiSurface;
141         @Nullable
142         private Bundle mExtras;
143 
144         /**
145          * @param context The {@link Context} of the prediction client.
146          *
147          * @hide
148          */
149         @SystemApi
150         @TestApi
Builder(@onNull Context context)151         public Builder(@NonNull Context context) {
152             mPackageName = context.getPackageName();
153         }
154 
155 
156         /**
157          * Sets the number of prediction targets as a hint.
158          */
159         @NonNull
setPredictedTargetCount(@ntRangefrom = 0) int predictedTargetCount)160         public Builder setPredictedTargetCount(@IntRange(from = 0) int predictedTargetCount) {
161             mPredictedTargetCount = predictedTargetCount;
162             return this;
163         }
164 
165         /**
166          * Sets the UI surface.
167          */
168         @NonNull
setUiSurface(@onNull String uiSurface)169         public Builder setUiSurface(@NonNull String uiSurface) {
170             mUiSurface = uiSurface;
171             return this;
172         }
173 
174         /**
175          * Sets the extras.
176          */
177         @NonNull
setExtras(@ullable Bundle extras)178         public Builder setExtras(@Nullable Bundle extras) {
179             mExtras = extras;
180             return this;
181         }
182 
183         /**
184          * Builds a new context instance.
185          */
186         @NonNull
build()187         public AppPredictionContext build() {
188             return new AppPredictionContext(mUiSurface, mPredictedTargetCount, mPackageName,
189                     mExtras);
190         }
191     }
192 }
193