1 /*
2  * Copyright 2021 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 com.android.server.appsearch.external.localstorage.converter;
18 
19 import android.annotation.NonNull;
20 import android.app.appsearch.AppSearchResult;
21 import android.util.Log;
22 
23 import com.google.android.icing.proto.StatusProto;
24 
25 /**
26  * Translates an {@link StatusProto.Code} into a {@link AppSearchResult.ResultCode}
27  *
28  * @hide
29  */
30 public final class ResultCodeToProtoConverter {
31 
32     private static final String TAG = "AppSearchResultCodeToPr";
33 
ResultCodeToProtoConverter()34     private ResultCodeToProtoConverter() {}
35 
36     /** Converts an {@link StatusProto.Code} into a {@link AppSearchResult.ResultCode}. */
37     @AppSearchResult.ResultCode
toResultCode(@onNull StatusProto.Code statusCode)38     public static int toResultCode(@NonNull StatusProto.Code statusCode) {
39         switch (statusCode) {
40             case OK:
41                 return AppSearchResult.RESULT_OK;
42             case OUT_OF_SPACE:
43                 return AppSearchResult.RESULT_OUT_OF_SPACE;
44             case INTERNAL:
45                 return AppSearchResult.RESULT_INTERNAL_ERROR;
46             case UNKNOWN:
47                 return AppSearchResult.RESULT_UNKNOWN_ERROR;
48             case NOT_FOUND:
49                 return AppSearchResult.RESULT_NOT_FOUND;
50             case INVALID_ARGUMENT:
51                 return AppSearchResult.RESULT_INVALID_ARGUMENT;
52             default:
53                 // Some unknown/unsupported error
54                 Log.e(
55                         TAG,
56                         "Cannot convert IcingSearchEngine status code: "
57                                 + statusCode
58                                 + " to AppSearchResultCode.");
59                 return AppSearchResult.RESULT_INTERNAL_ERROR;
60         }
61     }
62 }
63