1 /*
2  * Copyright 2025 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.appfunctions
18 
19 import android.os.Build
20 import androidx.annotation.RequiresApi
21 import androidx.annotation.RestrictTo
22 
23 /** Represents a response of an execution of an app function. */
24 public sealed interface ExecuteAppFunctionResponse {
25 
26     /** Represents a successful execution of an app function. */
27     public class Success(
28         /**
29          * The return value of the executed function. An [AppFunctionData.EMPTY] indicates that the
30          * function does not produce a return value.
31          */
32         public val returnValue: AppFunctionData,
33     ) : ExecuteAppFunctionResponse {
34         @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
toPlatformExtensionClassnull35         public fun toPlatformExtensionClass():
36             com.android.extensions.appfunctions.ExecuteAppFunctionResponse {
37             return com.android.extensions.appfunctions.ExecuteAppFunctionResponse(
38                 returnValue.genericDocument,
39                 returnValue.extras,
40             )
41         }
42 
43         @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
copynull44         public fun copy(returnValue: AppFunctionData = this.returnValue): Success =
45             Success(returnValue)
46 
47         public companion object {
48             /**
49              * The key name of the property that stores the function return value within `result`.
50              *
51              * See [AppFunctionData] documentation on retrieving expected fields.
52              */
53             public const val PROPERTY_RETURN_VALUE: String =
54                 com.android.extensions.appfunctions.ExecuteAppFunctionResponse.PROPERTY_RETURN_VALUE
55 
56             @RequiresApi(Build.VERSION_CODES.TIRAMISU)
57             @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
58             public fun fromPlatformExtensionClass(
59                 response: com.android.extensions.appfunctions.ExecuteAppFunctionResponse
60             ): Success {
61                 return Success(AppFunctionData(response.resultDocument, response.extras))
62             }
63         }
64     }
65 
66     /** Represents a failed execution of an app function. */
67     public class Error(
68         /** The [AppFunctionException] when the function execution failed. */
69         public val error: AppFunctionException,
70     ) : ExecuteAppFunctionResponse {
toStringnull71         override fun toString(): String {
72             return "AppFunctionResponse.Error(error=$error)"
73         }
74     }
75 }
76