1 /*
2  * Copyright (C) 2020 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.search;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.annotation.SystemApi;
21 import android.os.Bundle;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.Objects;
26 import java.util.concurrent.Executor;
27 import java.util.function.Consumer;
28 
29 /**
30  * When {@link SearchSession} is created, {@link SearchContext} object is created
31  * to pass the result types from the {@link SearchSession#query(Query, Executor, Consumer)}
32  * method that the client wants.
33  *
34  * @hide
35  */
36 @SystemApi
37 public final class SearchContext implements Parcelable {
38 
39     /**
40      * Result types the client UI is expecting the service to return.
41      */
42     private final int mResultTypes;
43 
44     /**
45      * Timeout constraint imposed from the client UI for the first search result.
46      */
47     private final int mTimeoutMillis;
48 
49     /**
50      * Send other client UI configurations in extras.
51      */
52     @Nullable
53     private final Bundle mExtras;
54 
55     /**
56      * Package name of the client.
57      */
58     @Nullable
59     private String mPackageName;
60 
61     /**
62      * @param resultTypes {@link SearchTarget.SearchResultType}s combined using bit OR operation
63      * @param timeoutMillis timeout before client renders its own fallback result
64      */
SearchContext(@earchTarget.SearchResultType int resultTypes, int timeoutMillis)65     public SearchContext(@SearchTarget.SearchResultType int resultTypes, int timeoutMillis) {
66         this(resultTypes, timeoutMillis, new Bundle());
67     }
68 
69     /**
70      * @param resultTypes {@link SearchTarget.SearchResultType}s combined using bit OR operation
71      * @param timeoutMillis timeout before client renders its own fallback result
72      * @param extras other client constraints (e.g., height of the search surface)
73      */
SearchContext(int resultTypes, int timeoutMillis, @NonNull Bundle extras)74     public SearchContext(int resultTypes,
75             int timeoutMillis,
76             @NonNull Bundle extras) {
77         mResultTypes = resultTypes;
78         mTimeoutMillis = timeoutMillis;
79         mExtras = Objects.requireNonNull(extras);
80     }
81 
SearchContext(Parcel parcel)82     private SearchContext(Parcel parcel) {
83         mResultTypes = parcel.readInt();
84         mTimeoutMillis = parcel.readInt();
85         mPackageName = parcel.readString();
86         mExtras = parcel.readBundle();
87     }
88 
89     @Nullable
getPackageName()90     public String getPackageName() {
91         return mPackageName;
92     }
93 
94     /**
95      * @hide
96      */
setPackageName(@ullable String packageName)97     void setPackageName(@Nullable String packageName) {
98         mPackageName = packageName;
99     }
100 
101     @NonNull
getTimeoutMillis()102     public int getTimeoutMillis() {
103         return mTimeoutMillis;
104     }
105 
106     @NonNull
getExtras()107     public Bundle getExtras() {
108         return mExtras;
109     }
110 
111     @NonNull
getResultTypes()112     public int getResultTypes() {
113         return mResultTypes;
114     }
115 
116     @Override
describeContents()117     public int describeContents() {
118         return 0;
119     }
120 
121     @Override
writeToParcel(@onNull Parcel dest, int flags)122     public void writeToParcel(@NonNull Parcel dest, int flags) {
123         dest.writeInt(mResultTypes);
124         dest.writeInt(mTimeoutMillis);
125         dest.writeString(mPackageName);
126         dest.writeBundle(mExtras);
127     }
128 
129     /**
130      * @see Parcelable.Creator
131      */
132     @NonNull
133     public static final Parcelable.Creator<SearchContext> CREATOR =
134             new Parcelable.Creator<SearchContext>() {
135                 public SearchContext createFromParcel(Parcel parcel) {
136                     return new SearchContext(parcel);
137                 }
138 
139                 public SearchContext[] newArray(int size) {
140                     return new SearchContext[size];
141                 }
142             };
143 }
144