1 /*
2  * Copyright (C) 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 com.android.ondevicepersonalization.libraries.plugin.internal;
18 
19 import android.os.Bundle;
20 import android.os.RemoteException;
21 
22 import com.android.ondevicepersonalization.libraries.plugin.FailureType;
23 import com.android.ondevicepersonalization.libraries.plugin.PluginCallback;
24 import com.android.ondevicepersonalization.libraries.plugin.PluginState;
25 import com.android.ondevicepersonalization.libraries.plugin.PluginStateCallback;
26 
27 /**
28  * Util to convert between public callback types like {@link PluginCallback} & {@link
29  * PluginStateCallback}, and Parcelable callback types like {@link IPluginCallback} & {@link
30  * IPluginStateCallback}.
31  */
32 public final class CallbackConverter {
33     /** Converts {@link PluginCallback} to {@link IPluginCallback} */
toIPluginCallback(PluginCallback callback)34     public static IPluginCallback toIPluginCallback(PluginCallback callback) {
35         return new IPluginCallback.Stub() {
36             @Override
37             public void onSuccess(Bundle input) throws RemoteException {
38                 callback.onSuccess(input);
39             }
40 
41             @Override
42             public void onFailure(FailureType failureType) throws RemoteException {
43                 callback.onFailure(failureType);
44             }
45         };
46     }
47 
48     /** Converts {@link IPluginCallback} to {@link PluginCallback} */
49     public static PluginCallback toPublicCallback(IPluginCallback callback) {
50         return new PluginCallback() {
51             @Override
52             public void onSuccess(Bundle input) throws RemoteException {
53                 callback.onSuccess(input);
54             }
55 
56             @Override
57             public void onFailure(FailureType failureType) throws RemoteException {
58                 callback.onFailure(failureType);
59             }
60         };
61     }
62 
63     /** Converts {@link PluginStateCallback} to {@link IPluginStateCallback} */
64     public static IPluginStateCallback toIPluginStateCallback(PluginStateCallback callback) {
65         return new IPluginStateCallback.Stub() {
66             @Override
67             public void onState(PluginState state) {
68                 callback.onState(state);
69             }
70         };
71     }
72 
73     private CallbackConverter() {}
74 }
75