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