• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Bundle;
22 
23 import java.util.Objects;
24 
25 /**
26  * An internal wrapper class of {@link SetSchemaResponse}.
27  *
28  * <p>For public users, if the {@link android.app.appsearch.AppSearchSession#setSchema} failed, we
29  * will directly throw an Exception. But AppSearch internal need to divert the incompatible changes
30  * form other call flows. This class adds a {@link #isSuccess()} to indicate if the call fails
31  * because of incompatible change.
32  *
33  * @hide
34  */
35 public class InternalSetSchemaResponse {
36 
37     private static final String IS_SUCCESS_FIELD = "isSuccess";
38     private static final String SET_SCHEMA_RESPONSE_BUNDLE_FIELD = "setSchemaResponseBundle";
39     private static final String ERROR_MESSAGE_FIELD = "errorMessage";
40 
41     private final Bundle mBundle;
42 
InternalSetSchemaResponse(@onNull Bundle bundle)43     public InternalSetSchemaResponse(@NonNull Bundle bundle) {
44         mBundle = Objects.requireNonNull(bundle);
45     }
46 
InternalSetSchemaResponse( boolean isSuccess, @NonNull SetSchemaResponse setSchemaResponse, @Nullable String errorMessage)47     private InternalSetSchemaResponse(
48             boolean isSuccess,
49             @NonNull SetSchemaResponse setSchemaResponse,
50             @Nullable String errorMessage) {
51         Objects.requireNonNull(setSchemaResponse);
52         mBundle = new Bundle();
53         mBundle.putBoolean(IS_SUCCESS_FIELD, isSuccess);
54         mBundle.putBundle(SET_SCHEMA_RESPONSE_BUNDLE_FIELD, setSchemaResponse.getBundle());
55         mBundle.putString(ERROR_MESSAGE_FIELD, errorMessage);
56     }
57 
58     /**
59      * Returns the {@link Bundle} populated by this builder.
60      *
61      * @hide
62      */
63     @NonNull
getBundle()64     public Bundle getBundle() {
65         return mBundle;
66     }
67 
68     /**
69      * Creates a new successful {@link InternalSetSchemaResponse}.
70      *
71      * @param setSchemaResponse The object this internal object represents.
72      */
73     @NonNull
newSuccessfulSetSchemaResponse( @onNull SetSchemaResponse setSchemaResponse)74     public static 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      */
86     @NonNull
newFailedSetSchemaResponse( @onNull SetSchemaResponse setSchemaResponse, @NonNull String errorMessage)87     public static InternalSetSchemaResponse newFailedSetSchemaResponse(
88             @NonNull SetSchemaResponse setSchemaResponse, @NonNull String errorMessage) {
89         return new InternalSetSchemaResponse(/*isSuccess=*/ false, setSchemaResponse, errorMessage);
90     }
91 
92     /** Returns {@code true} if the schema request is proceeded successfully. */
isSuccess()93     public boolean isSuccess() {
94         return mBundle.getBoolean(IS_SUCCESS_FIELD);
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      */
102     @NonNull
getSetSchemaResponse()103     public SetSchemaResponse getSetSchemaResponse() {
104         return new SetSchemaResponse(mBundle.getBundle(SET_SCHEMA_RESPONSE_BUNDLE_FIELD));
105     }
106 
107     /**
108      * Returns the error message associated with this response.
109      *
110      * <p>If {@link #isSuccess} is {@code true}, the error message is always {@code null}.
111      */
112     @Nullable
getErrorMessage()113     public String getErrorMessage() {
114         return mBundle.getString(ERROR_MESSAGE_FIELD);
115     }
116 }
117