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 androidx.appsearch.app; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 import androidx.annotation.RestrictTo; 23 import androidx.appsearch.flags.FlaggedApi; 24 import androidx.appsearch.flags.Flags; 25 import androidx.appsearch.safeparcel.AbstractSafeParcelable; 26 import androidx.appsearch.safeparcel.SafeParcelable; 27 import androidx.appsearch.safeparcel.stub.StubCreators.InternalSetSchemaResponseCreator; 28 import androidx.core.util.Preconditions; 29 30 import org.jspecify.annotations.NonNull; 31 import org.jspecify.annotations.Nullable; 32 33 /** 34 * An internal wrapper class of {@link SetSchemaResponse}. 35 * 36 * <p>For public users, if the {@link androidx.appsearch.app.AppSearchSession#setSchemaAsync} 37 * failed, we will directly throw an Exception. But AppSearch internal need to divert the 38 * incompatible changes form other call flows. This class adds a {@link #isSuccess()} to indicate 39 * if the call fails because of incompatible change. 40 * 41 * @exportToFramework:hide 42 */ 43 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 44 @SafeParcelable.Class(creator = "InternalSetSchemaResponseCreator") 45 public class InternalSetSchemaResponse extends AbstractSafeParcelable { 46 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 47 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 48 public static final Parcelable.@NonNull Creator<InternalSetSchemaResponse> CREATOR = 49 new InternalSetSchemaResponseCreator(); 50 51 @Field(id = 1, getter = "isSuccess") 52 private final boolean mIsSuccess; 53 54 @Field(id = 2, getter = "getSetSchemaResponse") 55 private final SetSchemaResponse mSetSchemaResponse; 56 @Field(id = 3, getter = "getErrorMessage") 57 private final @Nullable String mErrorMessage; 58 59 @Constructor InternalSetSchemaResponse( @aramid = 1) boolean isSuccess, @Param(id = 2) @NonNull SetSchemaResponse setSchemaResponse, @Param(id = 3) @Nullable String errorMessage)60 public InternalSetSchemaResponse( 61 @Param(id = 1) boolean isSuccess, 62 @Param(id = 2) @NonNull SetSchemaResponse setSchemaResponse, 63 @Param(id = 3) @Nullable String errorMessage) { 64 Preconditions.checkNotNull(setSchemaResponse); 65 mIsSuccess = isSuccess; 66 mSetSchemaResponse = setSchemaResponse; 67 mErrorMessage = errorMessage; 68 } 69 70 /** 71 * Creates a new successful {@link InternalSetSchemaResponse}. 72 * 73 * @param setSchemaResponse The object this internal object represents. 74 */ newSuccessfulSetSchemaResponse( @onNull SetSchemaResponse setSchemaResponse)75 public static @NonNull InternalSetSchemaResponse newSuccessfulSetSchemaResponse( 76 @NonNull SetSchemaResponse setSchemaResponse) { 77 return new InternalSetSchemaResponse(/*isSuccess=*/ true, setSchemaResponse, 78 /*errorMessage=*/null); 79 } 80 81 /** 82 * Creates a new failed {@link InternalSetSchemaResponse}. 83 * 84 * @param setSchemaResponse The object this internal object represents. 85 * @param errorMessage An string describing the reason or nature of the failure. 86 */ newFailedSetSchemaResponse( @onNull SetSchemaResponse setSchemaResponse, @NonNull String errorMessage)87 public static @NonNull InternalSetSchemaResponse newFailedSetSchemaResponse( 88 @NonNull SetSchemaResponse setSchemaResponse, 89 @NonNull String errorMessage) { 90 return new InternalSetSchemaResponse(/*isSuccess=*/ false, setSchemaResponse, 91 errorMessage); 92 } 93 94 /** Returns {@code true} if the schema request is proceeded successfully. */ isSuccess()95 public boolean isSuccess() { 96 return mIsSuccess; 97 } 98 99 /** 100 * Returns the {@link SetSchemaResponse} of the set schema call. 101 * 102 * <p>The call may or may not success. Check {@link #isSuccess()} before call this method. 103 */ getSetSchemaResponse()104 public @NonNull SetSchemaResponse getSetSchemaResponse() { 105 return mSetSchemaResponse; 106 } 107 108 109 /** 110 * Returns the error message associated with this response. 111 * 112 * <p>If {@link #isSuccess} is {@code true}, the error message is always {@code null}. 113 */ getErrorMessage()114 public @Nullable String getErrorMessage() { 115 return mErrorMessage; 116 } 117 118 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 119 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 120 @Override writeToParcel(@onNull Parcel dest, int flags)121 public void writeToParcel(@NonNull Parcel dest, int flags) { 122 InternalSetSchemaResponseCreator.writeToParcel(this, dest, flags); 123 } 124 } 125