1 /* 2 * Copyright 2022 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 17 package android.app.appsearch; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.app.appsearch.annotation.CanIgnoreReturnValue; 23 import android.app.appsearch.safeparcel.AbstractSafeParcelable; 24 import android.app.appsearch.safeparcel.SafeParcelable; 25 import android.os.Parcel; 26 import android.os.Parcelable; 27 28 import com.android.appsearch.flags.Flags; 29 import com.android.internal.util.Preconditions; 30 31 import java.util.Objects; 32 33 /** The result class of the {@link AppSearchSession#searchSuggestion}. */ 34 @SafeParcelable.Class(creator = "SearchSuggestionResultCreator") 35 // TODO(b/384721898): Switch to JSpecify annotations 36 @SuppressWarnings({"HiddenSuperclass", "JSpecifyNullness"}) 37 public final class SearchSuggestionResult extends AbstractSafeParcelable { 38 39 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 40 public static final @NonNull Parcelable.Creator<SearchSuggestionResult> CREATOR = 41 new SearchSuggestionResultCreator(); 42 43 @Field(id = 1, getter = "getSuggestedResult") 44 private final String mSuggestedResult; 45 46 private @Nullable Integer mHashCode; 47 48 @Constructor SearchSuggestionResult(@aramid = 1) String suggestedResult)49 SearchSuggestionResult(@Param(id = 1) String suggestedResult) { 50 mSuggestedResult = Objects.requireNonNull(suggestedResult); 51 } 52 53 /** 54 * Returns the suggested result that could be used as query expression in the {@link 55 * AppSearchSession#search}. 56 * 57 * <p>The suggested result will never be empty. 58 * 59 * <p>The suggested result only contains lowercase or special characters. 60 */ getSuggestedResult()61 public @NonNull String getSuggestedResult() { 62 return mSuggestedResult; 63 } 64 65 @Override equals(@ullable Object other)66 public boolean equals(@Nullable Object other) { 67 if (this == other) { 68 return true; 69 } 70 if (!(other instanceof SearchSuggestionResult)) { 71 return false; 72 } 73 SearchSuggestionResult otherResult = (SearchSuggestionResult) other; 74 return mSuggestedResult.equals(otherResult.mSuggestedResult); 75 } 76 77 @Override hashCode()78 public int hashCode() { 79 if (mHashCode == null) { 80 mHashCode = mSuggestedResult.hashCode(); 81 } 82 return mHashCode; 83 } 84 85 /** The Builder class of {@link SearchSuggestionResult}. */ 86 public static final class Builder { 87 private String mSuggestedResult = ""; 88 89 /** 90 * Sets the suggested result that could be used as query expression in the {@link 91 * AppSearchSession#search}. 92 * 93 * <p>The suggested result should only contain lowercase or special characters. 94 */ 95 @CanIgnoreReturnValue setSuggestedResult(@onNull String suggestedResult)96 public @NonNull Builder setSuggestedResult(@NonNull String suggestedResult) { 97 Objects.requireNonNull(suggestedResult); 98 Preconditions.checkStringNotEmpty(suggestedResult); 99 mSuggestedResult = suggestedResult; 100 return this; 101 } 102 103 /** Build a {@link SearchSuggestionResult} object */ build()104 public @NonNull SearchSuggestionResult build() { 105 return new SearchSuggestionResult(mSuggestedResult); 106 } 107 } 108 109 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 110 @Override writeToParcel(@onNull Parcel dest, int flags)111 public void writeToParcel(@NonNull Parcel dest, int flags) { 112 SearchSuggestionResultCreator.writeToParcel(this, dest, flags); 113 } 114 } 115