1 /*
2  * Copyright (C) 2024 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.extensions.appfunctions;
18 
19 import android.annotation.NonNull;
20 
21 /**
22  * Utility class containing methods to convert Sidecar objects of AppFunctions API into the
23  * underlying platform classes.
24  *
25  * @hide
26  */
27 public final class SidecarConverter {
SidecarConverter()28     private SidecarConverter() {}
29 
30     /**
31      * Converts sidecar's {@link ExecuteAppFunctionRequest} into platform's {@link
32      * android.app.appfunctions.ExecuteAppFunctionRequest}
33      *
34      * @hide
35      */
36     @NonNull
37     public static android.app.appfunctions.ExecuteAppFunctionRequest
getPlatformExecuteAppFunctionRequest(@onNull ExecuteAppFunctionRequest request)38             getPlatformExecuteAppFunctionRequest(@NonNull ExecuteAppFunctionRequest request) {
39         return new android.app.appfunctions.ExecuteAppFunctionRequest.Builder(
40                         request.getTargetPackageName(), request.getFunctionIdentifier())
41                 .setExtras(request.getExtras())
42                 .setParameters(request.getParameters())
43                 .build();
44     }
45 
46     /**
47      * Converts sidecar's {@link ExecuteAppFunctionResponse} into platform's {@link
48      * android.app.appfunctions.ExecuteAppFunctionResponse}
49      *
50      * @hide
51      */
52     @NonNull
53     public static android.app.appfunctions.ExecuteAppFunctionResponse
getPlatformExecuteAppFunctionResponse(@onNull ExecuteAppFunctionResponse response)54             getPlatformExecuteAppFunctionResponse(@NonNull ExecuteAppFunctionResponse response) {
55         return new android.app.appfunctions.ExecuteAppFunctionResponse(
56                 response.getResultDocument(), response.getExtras());
57     }
58 
59     /**
60      * Converts sidecar's {@link AppFunctionException} into platform's {@link
61      * android.app.appfunctions.AppFunctionException}
62      *
63      * @hide
64      */
65     @NonNull
66     public static android.app.appfunctions.AppFunctionException
getPlatformAppFunctionException(@onNull AppFunctionException exception)67             getPlatformAppFunctionException(@NonNull AppFunctionException exception) {
68         return new android.app.appfunctions.AppFunctionException(
69                 exception.getErrorCode(), exception.getErrorMessage(), exception.getExtras());
70     }
71 
72     /**
73      * Converts platform's {@link android.app.appfunctions.ExecuteAppFunctionRequest} into sidecar's
74      * {@link ExecuteAppFunctionRequest}
75      *
76      * @hide
77      */
78     @NonNull
getSidecarExecuteAppFunctionRequest( @onNull android.app.appfunctions.ExecuteAppFunctionRequest request)79     public static ExecuteAppFunctionRequest getSidecarExecuteAppFunctionRequest(
80             @NonNull android.app.appfunctions.ExecuteAppFunctionRequest request) {
81         return new ExecuteAppFunctionRequest.Builder(
82                         request.getTargetPackageName(), request.getFunctionIdentifier())
83                 .setExtras(request.getExtras())
84                 .setParameters(request.getParameters())
85                 .build();
86     }
87 
88     /**
89      * Converts platform's {@link android.app.appfunctions.ExecuteAppFunctionResponse} into
90      * sidecar's {@link ExecuteAppFunctionResponse}
91      *
92      * @hide
93      */
94     @NonNull
getSidecarExecuteAppFunctionResponse( @onNull android.app.appfunctions.ExecuteAppFunctionResponse response)95     public static ExecuteAppFunctionResponse getSidecarExecuteAppFunctionResponse(
96             @NonNull android.app.appfunctions.ExecuteAppFunctionResponse response) {
97         return new ExecuteAppFunctionResponse(response.getResultDocument(), response.getExtras());
98     }
99 
100     /**
101      * Converts platform's {@link android.app.appfunctions.AppFunctionException} into
102      * sidecar's {@link AppFunctionException}
103      *
104      * @hide
105      */
106     @NonNull
getSidecarAppFunctionException( @onNull android.app.appfunctions.AppFunctionException exception)107     public static AppFunctionException getSidecarAppFunctionException(
108             @NonNull android.app.appfunctions.AppFunctionException exception) {
109         return new AppFunctionException(
110                 exception.getErrorCode(), exception.getErrorMessage(), exception.getExtras());
111     }
112 }
113