• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 package android.hardware.location;
17 
18 import android.annotation.NonNull;
19 import android.util.Log;
20 
21 import java.util.List;
22 import java.util.Objects;
23 
24 /**
25  * Helper class to generate {@link IContextHubTransactionCallback}.
26  *
27  * @hide
28  */
29 public class ContextHubTransactionHelper {
30     private static final String TAG = "ContextHubTransactionHelper";
31 
32     /**
33      * Helper to generate a stub for a query nanoapp transaction callback.
34      *
35      * @param transaction the transaction to unblock when complete
36      * @return the callback
37      * @hide
38      */
createNanoAppQueryCallback( @onNull) ContextHubTransaction<List<NanoAppState>> transaction)39     public static IContextHubTransactionCallback createNanoAppQueryCallback(
40             @NonNull() ContextHubTransaction<List<NanoAppState>> transaction) {
41         Objects.requireNonNull(transaction, "transaction cannot be null");
42         return new IContextHubTransactionCallback.Stub() {
43             @Override
44             public void onQueryResponse(int result, List<NanoAppState> nanoappList) {
45                 transaction.setResponse(new ContextHubTransaction.Response<List<NanoAppState>>(
46                         result, nanoappList));
47             }
48 
49             @Override
50             public void onTransactionComplete(int result) {
51                 Log.e(TAG, "Received a non-query callback on a query request");
52                 transaction.setResponse(new ContextHubTransaction.Response<List<NanoAppState>>(
53                         ContextHubTransaction.RESULT_FAILED_SERVICE_INTERNAL_FAILURE, null));
54             }
55         };
56     }
57 
58     /**
59      * Helper to generate a stub for a non-query transaction callback.
60      *
61      * @param transaction the transaction to unblock when complete
62      * @return the callback
63      * @hide
64      */
65     public static IContextHubTransactionCallback createTransactionCallback(
66             @NonNull() ContextHubTransaction<Void> transaction) {
67         Objects.requireNonNull(transaction, "transaction cannot be null");
68         return new IContextHubTransactionCallback.Stub() {
69             @Override
70             public void onQueryResponse(int result, List<NanoAppState> nanoappList) {
71                 Log.e(TAG, "Received a query callback on a non-query request");
72                 transaction.setResponse(new ContextHubTransaction.Response<Void>(
73                         ContextHubTransaction.RESULT_FAILED_SERVICE_INTERNAL_FAILURE, null));
74             }
75 
76             @Override
77             public void onTransactionComplete(int result) {
78                 transaction.setResponse(new ContextHubTransaction.Response<Void>(result, null));
79             }
80         };
81     }
82 
83 }
84