Debian Codegraph @

core/java/android/app/search/SearchTarget.java

raw symbols SCIP
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.FloatRange;
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.StringDef;
23 import android.annotation.SystemApi;
24 import android.app.slice.SliceManager;
25 import android.appwidget.AppWidgetProviderInfo;
26 import android.content.pm.PackageManager;
27 import android.content.pm.ShortcutInfo;
28 import android.content.pm.ShortcutManager;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.os.Parcel;
32 import android.os.Parcelable;
33 import android.os.UserHandle;
34
35 import java.lang.annotation.Retention;
36 import java.lang.annotation.RetentionPolicy;
37 import java.util.Objects;
38
39 /**
40 * A representation of a search result. Search result can be expressed in one of the following:
41 * app icon, shortcut, slice, widget, or a custom object using {@link SearchAction}. While
42 * app icon ({@link PackageManager}, shortcut {@link ShortcutManager}, slice {@link SliceManager},
43 * or widget (@link AppWidgetManager} are published content backed by the system service,
44 * {@link SearchAction} is a custom object that the service can use to send search result to the
45 * client.
46 *
47 * These various types of Android primitives could be defined as {@link SearchResultType}. Some
48 * times, the result type can define the layout type that that this object can be rendered in.
49 * (e.g., app widget). Most times, {@link #getLayoutType()} assigned by the service
50 * can recommend which layout this target should be rendered in.
51 *
52 * The service can also use fields such as {@link #getScore()} to indicate
53 * how confidence the search result is and {@link #isHidden()} to indicate
54 * whether it is recommended to be shown by default.
55 *
56 * Finally, {@link #getId()} is the unique identifier of this search target and a single
57 * search target is defined by being able to express a single launcheable item. In case the
58 * service want to recommend how to combine multiple search target objects to render in a group
59 * (e.g., same row), {@link #getParentId()} can be assigned on the sub targets of the group
60 * using the primary search target's identifier.
61 *
62 * @hide
63 */
64 @SystemApi
65 public final class SearchTarget implements Parcelable {
66
67 public static final int RESULT_TYPE_APPLICATION = 1 << 0;
68 public static final int RESULT_TYPE_SHORTCUT = 1 << 1;
69 public static final int RESULT_TYPE_SLICE = 1 << 2;
70 public static final int RESULT_TYPE_WIDGETS = 1 << 3;
71
72 // ------
73 // | icon |
74 // ------
75 // text
76 public static final String LAYOUT_TYPE_ICON = "icon";
77
78 // ------ ------ ------
79 // | | title |(opt)| |(opt)|
80 // | icon | subtitle (optional) | icon| | icon|
81 // ------ ------ ------
82 public static final String LAYOUT_TYPE_ICON_ROW = "icon_row";
83
84 // ------
85 // | icon | title / subtitle (optional)
86 // ------
87 public static final String LAYOUT_TYPE_SHORT_ICON_ROW = "short_icon_row";
88
89 /**
90 * @hide
91 */
92 @IntDef(prefix = {"RESULT_TYPE_"}, value = {
93 RESULT_TYPE_APPLICATION,
94 RESULT_TYPE_SHORTCUT,
95 RESULT_TYPE_SLICE,
96 RESULT_TYPE_WIDGETS
97 })
98 @Retention(RetentionPolicy.SOURCE)
99 public @interface SearchResultType {}
100 private final int mResultType;
101
102 /**
103 * @hide
104 */
105 @StringDef(prefix = {"LAYOUT_TYPE_"}, value = {
106 LAYOUT_TYPE_ICON,
107 LAYOUT_TYPE_ICON_ROW,
108 LAYOUT_TYPE_SHORT_ICON_ROW,
109 })
110 @Retention(RetentionPolicy.SOURCE)
111 public @interface SearchLayoutType {}
112
113 /**
114 * Constant to express how the group of {@link SearchTarget} should be rendered on
115 * the client side. (e.g., "icon", "icon_row", "short_icon_row")
116 */
117 @NonNull
118 private final String mLayoutType;
119
120 @NonNull
121 private final String mId;
122
123 @Nullable
124 private String mParentId;
125
126 private final float mScore;
127
128 private final boolean mHidden;
129
130 @NonNull
131 private final String mPackageName;
132 @NonNull
133 private final UserHandle mUserHandle;
134 @Nullable
135 private final SearchAction mSearchAction;
136 @Nullable
137 private final ShortcutInfo mShortcutInfo;
138 @Nullable
139 private final AppWidgetProviderInfo mAppWidgetProviderInfo;
140 @Nullable
141 private final Uri mSliceUri;
142
143 @NonNull
144 private final Bundle mExtras;
145
146 private SearchTarget(Parcel parcel) {
147 mResultType = parcel.readInt();
148 mLayoutType = parcel.readString();
149 mId = parcel.readString();
150 mParentId = parcel.readString();
151 mScore = parcel.readFloat();
152 mHidden = parcel.readBoolean();
153
154 mPackageName = parcel.readString();
155 mUserHandle = UserHandle.of(parcel.readInt());
156 mSearchAction = parcel.readTypedObject(SearchAction.CREATOR);
157 mShortcutInfo = parcel.readTypedObject(ShortcutInfo.CREATOR);
158 mAppWidgetProviderInfo = parcel.readTypedObject(AppWidgetProviderInfo.CREATOR);
159 mSliceUri = parcel.readTypedObject(Uri.CREATOR);
160 mExtras = parcel.readBundle(getClass().getClassLoader());
161 }
162
163 private SearchTarget(
164 int resultType,
165 @NonNull String layoutType,
166 @NonNull String id,
167 @Nullable String parentId,
168 float score, boolean hidden,
169 @NonNull String packageName,
170 @NonNull UserHandle userHandle,
171 @Nullable SearchAction action,
172 @Nullable ShortcutInfo shortcutInfo,
173 @Nullable Uri sliceUri,
174 @Nullable AppWidgetProviderInfo appWidgetProviderInfo,
175 @NonNull Bundle extras) {
176 mResultType = resultType;
177 mLayoutType = Objects.requireNonNull(layoutType);
178 mId = Objects.requireNonNull(id);
179 mParentId = parentId;
180 mScore = score;
181 mHidden = hidden;
182 mPackageName = Objects.requireNonNull(packageName);
183 mUserHandle = Objects.requireNonNull(userHandle);
184 mSearchAction = action;
185 mShortcutInfo = shortcutInfo;
186 mAppWidgetProviderInfo = appWidgetProviderInfo;
187 mSliceUri = sliceUri;
188 mExtras = extras != null ? extras : new Bundle();
189
190 int published = 0;
191 if (mSearchAction != null) published++;
192 if (mShortcutInfo != null) published++;
193 if (mAppWidgetProviderInfo != null) published++;
194 if (mSliceUri != null) published++;
195 if (published > 1) {
196 throw new IllegalStateException("Only one of SearchAction, ShortcutInfo,"
197 + " AppWidgetProviderInfo, SliceUri can be assigned in a SearchTarget.");
198 }
199 }
200
201 /**
202 * Retrieves the result type {@see SearchResultType}.
203 */
204 public @SearchResultType int getResultType() {
205 return mResultType;
206 }
207
208 /**
209 * Retrieves the layout type.
210 */
211 @NonNull
212 public @SearchLayoutType String getLayoutType() {
213 return mLayoutType;
214 }
215
216 /**
217 * Retrieves the id of the target.
218 */
219 @NonNull
220 public String getId() {
221 return mId;
222 }
223
224 /**
225 * Retrieves the parent id of the target.
226 */
227 @NonNull
228 public String getParentId() {
229 return mParentId;
230 }
231
232 /**
233 * Retrieves the score of the target.
234 */
235 public float getScore() {
236 return mScore;
237 }
238
239 /**
240 * Indicates whether this object should be hidden and shown only on demand.
241 *
242 * @deprecated will be removed once SDK drops
243 * @removed
244 */
245 @Deprecated
246 public boolean shouldHide() {
247 return mHidden;
248 }
249
250 /**
251 * Indicates whether this object should be hidden and shown only on demand.
252 */
253 public boolean isHidden() {
254 return mHidden;
255 }
256
257 /**
258 * Retrieves the package name of the target.
259 */
260 @NonNull
261 public String getPackageName() {
262 return mPackageName;
263 }
264
265 /**
266 * Retrieves the user handle of the target.
267 */
268 @NonNull
269 public UserHandle getUserHandle() {
270 return mUserHandle;
271 }
272
273 /**
274 * Retrieves the shortcut info of the target.
275 */
276 @Nullable
277 public ShortcutInfo getShortcutInfo() {
278 return mShortcutInfo;
279 }
280
281 /**
282 * Return a widget provider info.
283 */
284 @Nullable
285 public AppWidgetProviderInfo getAppWidgetProviderInfo() {
286 return mAppWidgetProviderInfo;
287 }
288
289 /**
290 * Returns a slice uri.
291 */
292 @Nullable
293 public Uri getSliceUri() {
294 return mSliceUri;
295 }
296
297 /**
298 * Returns a search action.
299 */
300 @Nullable
301 public SearchAction getSearchAction() {
302 return mSearchAction;
303 }
304
305 /**
306 * Return extra bundle.
307 */
308 @NonNull
309 public Bundle getExtras() {
310 return mExtras;
311 }
312
313 @Override
314 public int describeContents() {
315 return 0;
316 }
317
318 @Override
319 public void writeToParcel(@NonNull Parcel parcel, int flags) {
320 parcel.writeInt(mResultType);
321 parcel.writeString(mLayoutType);
322 parcel.writeString(mId);
323 parcel.writeString(mParentId);
324 parcel.writeFloat(mScore);
325 parcel.writeBoolean(mHidden);
326 parcel.writeString(mPackageName);
327 parcel.writeInt(mUserHandle.getIdentifier());
328 parcel.writeTypedObject(mSearchAction, flags);
329 parcel.writeTypedObject(mShortcutInfo, flags);
330 parcel.writeTypedObject(mAppWidgetProviderInfo, flags);
331 parcel.writeTypedObject(mSliceUri, flags);
332 parcel.writeBundle(mExtras);
333 }
334
335 /**
336 * @see Parcelable.Creator
337 */
338 @NonNull
339 public static final Parcelable.Creator<SearchTarget> CREATOR =
340 new Parcelable.Creator<SearchTarget>() {
341 public SearchTarget createFromParcel(Parcel parcel) {
342 return new SearchTarget(parcel);
343 }
344
345 public SearchTarget[] newArray(int size) {
346 return new SearchTarget[size];
347 }
348 };
349
350 /**
351 * A builder for search target object.
352 *
353 * @hide
354 */
355 @SystemApi
356 public static final class Builder {
357 private int mResultType;
358 @NonNull
359 private String mLayoutType;
360 @NonNull
361 private String mId;
362 @Nullable
363 private String mParentId;
364 private float mScore;
365 private boolean mHidden;
366 @NonNull
367 private String mPackageName;
368 @NonNull
369 private UserHandle mUserHandle;
370 @Nullable
371 private SearchAction mSearchAction;
372 @Nullable
373 private ShortcutInfo mShortcutInfo;
374 @Nullable
375 private Uri mSliceUri;
376 @Nullable
377 private AppWidgetProviderInfo mAppWidgetProviderInfo;
378 @NonNull
379 private Bundle mExtras;
380
381 public Builder(@SearchResultType int resultType,
382 @SearchLayoutType @NonNull String layoutType,
383 @NonNull String id) {
384 mId = id;
385 mLayoutType = Objects.requireNonNull(layoutType);
386 mResultType = resultType;
387 mScore = 1f;
388 mHidden = false;
389 }
390
391 /**
392 * Sets the parent id.
393 */
394 @NonNull
395 public Builder setParentId(@NonNull String parentId) {
396 mParentId = Objects.requireNonNull(parentId);
397 return this;
398 }
399
400 /**
401 * Sets the package name.
402 */
403 @NonNull
404 public Builder setPackageName(@NonNull String packageName) {
405 mPackageName = Objects.requireNonNull(packageName);
406 return this;
407 }
408
409 /**
410 * Sets the user handle.
411 */
412 @NonNull
413 public Builder setUserHandle(@NonNull UserHandle userHandle) {
414 mUserHandle = Objects.requireNonNull(userHandle);
415 return this;
416 }
417
418 /**
419 * Sets the shortcut info.
420 */
421 @NonNull
422 public Builder setShortcutInfo(@NonNull ShortcutInfo shortcutInfo) {
423 mShortcutInfo = Objects.requireNonNull(shortcutInfo);
424 if (mPackageName != null && !mPackageName.equals(shortcutInfo.getPackage())) {
425 throw new IllegalStateException("SearchTarget packageName is different from "
426 + "shortcut's packageName");
427 }
428 mPackageName = shortcutInfo.getPackage();
429 return this;
430 }
431
432 /**
433 * Sets the app widget provider info.
434 */
435 @NonNull
436 public Builder setAppWidgetProviderInfo(
437 @NonNull AppWidgetProviderInfo appWidgetProviderInfo) {
438 mAppWidgetProviderInfo = Objects.requireNonNull(appWidgetProviderInfo);
439 if (mPackageName != null
440 && !mPackageName.equals(appWidgetProviderInfo.provider.getPackageName())) {
441 throw new IllegalStateException("SearchTarget packageName is different from "
442 + "appWidgetProviderInfo's packageName");
443 }
444 return this;
445 }
446
447 /**
448 * Sets the slice URI.
449 */
450 @NonNull
451 public Builder setSliceUri(@NonNull Uri sliceUri) {
452 mSliceUri = sliceUri;
453 return this;
454 }
455
456 /**
457 * Set the {@link SearchAction} object to this target.
458 */
459 @NonNull
460 public Builder setSearchAction(@Nullable SearchAction searchAction) {
461 mSearchAction = searchAction;
462 return this;
463 }
464
465 /**
466 * Set any extra information that needs to be shared between service and the client.
467 */
468 @NonNull
469 public Builder setExtras(@NonNull Bundle extras) {
470 mExtras = Objects.requireNonNull(extras);
471 return this;
472 }
473
474 /**
475 * Sets the score of the object.
476 */
477 @NonNull
478 public Builder setScore(@FloatRange(from = 0.0f, to = 1.0f) float score) {
479 mScore = score;
480 return this;
481 }
482
483 /**
484 * Sets whether the result should be hidden (e.g. not visible) by default inside client.
485 */
486 @NonNull
487 public Builder setHidden(boolean hidden) {
488 mHidden = hidden;
489 return this;
490 }
491
492 /**
493 * Sets whether the result should be hidden by default inside client.
494 * @deprecated will be removed once SDK drops
495 * @removed
496 */
497 @NonNull
498 @Deprecated
499 public Builder setShouldHide(boolean shouldHide) {
500 mHidden = shouldHide;
501 return this;
502 }
503
504 /**
505 * Builds a new SearchTarget instance.
506 *
507 * @throws IllegalStateException if no target is set
508 */
509 @NonNull
510 public SearchTarget build() {
511 return new SearchTarget(mResultType, mLayoutType, mId, mParentId, mScore, mHidden,
512 mPackageName, mUserHandle,
513 mSearchAction, mShortcutInfo, mSliceUri, mAppWidgetProviderInfo,
514 mExtras);
515 }
516 }
517 }