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