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 android.car.builtin.bluetooth.le;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.bluetooth.le.AdvertisingSet;
22 import android.bluetooth.le.AdvertisingSetCallback;
23 
24 /**
25  * Provides access to {@code onOwnAddressRead} in
26  * {@code android.bluetooth.le.AdvertisingSetCallback}.
27  * @hide
28  */
29 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
30 public final class AdvertisingSetCallbackHelper {
31 
32     /**
33      * A proxy to {@code android.bluetooth.le.AdvertisingSetCallback}, since one of its methods
34      * is a hidden API. {@code AdvertisingSetCallback} is the Bluetooth LE advertising set
35      * callbacks, used to deliver advertising operation status.
36      */
37     public abstract static class Callback {
38 
39         /**
40          * Callback triggered in response to {@link BluetoothLeAdvertiser#startAdvertisingSet}
41          * indicating result of the operation. If status is ADVERTISE_SUCCESS, then advertisingSet
42          * contains the started set and it is advertising. If error occurred, advertisingSet is
43          * null, and status will be set to proper error code.
44          *
45          * @param advertisingSet The advertising set that was started or null if error.
46          * @param txPower tx power that will be used for this set.
47          * @param status Status of the operation.
48          */
onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, int status)49         public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower,
50                 int status) {
51         }
52 
53         /**
54          * Callback triggered in response to {@link BluetoothLeAdvertiser#stopAdvertisingSet}
55          * indicating advertising set is stopped.
56          *
57          * @param advertisingSet The advertising set.
58          */
onAdvertisingSetStopped(AdvertisingSet advertisingSet)59         public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
60         }
61 
62         /**
63          * Callback triggered in response to {@link BluetoothLeAdvertiser#startAdvertisingSet}
64          * indicating result of the operation. If status is ADVERTISE_SUCCESS, then advertising
65          * set is advertising.
66          *
67          * @param advertisingSet The advertising set.
68          * @param status Status of the operation.
69          */
onAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, int status)70         public void onAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable,
71                 int status) {
72         }
73 
74         /**
75          * Callback triggered in response to {@link AdvertisingSet#setAdvertisingData} indicating
76          * result of the operation. If status is ADVERTISE_SUCCESS, then data was changed.
77          *
78          * @param advertisingSet The advertising set.
79          * @param status Status of the operation.
80          */
onAdvertisingDataSet(AdvertisingSet advertisingSet, int status)81         public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) {
82         }
83 
84         /**
85          * Callback triggered in response to {@link AdvertisingSet#setAdvertisingData} indicating
86          * result of the operation.
87          *
88          * @param advertisingSet The advertising set.
89          * @param status Status of the operation.
90          */
onScanResponseDataSet(AdvertisingSet advertisingSet, int status)91         public void onScanResponseDataSet(AdvertisingSet advertisingSet, int status) {
92         }
93 
94         /**
95          * Callback triggered in response to {@link AdvertisingSet#setAdvertisingParameters}
96          * indicating result of the operation.
97          *
98          * @param advertisingSet The advertising set.
99          * @param txPower tx power that will be used for this set.
100          * @param status Status of the operation.
101          */
onAdvertisingParametersUpdated(AdvertisingSet advertisingSet, int txPower, int status)102         public void onAdvertisingParametersUpdated(AdvertisingSet advertisingSet,
103                 int txPower, int status) {
104         }
105 
106         /**
107          * Callback triggered in response to {@link
108          * AdvertisingSet#setPeriodicAdvertisingParameters} indicating result of the operation.
109          *
110          * @param advertisingSet The advertising set.
111          * @param status Status of the operation.
112          */
onPeriodicAdvertisingParametersUpdated(AdvertisingSet advertisingSet, int status)113         public void onPeriodicAdvertisingParametersUpdated(AdvertisingSet advertisingSet,
114                 int status) {
115         }
116 
117         /**
118          * Callback triggered in response to {@link AdvertisingSet#setPeriodicAdvertisingData}
119          * indicating result of the operation.
120          *
121          * @param advertisingSet The advertising set.
122          * @param status Status of the operation.
123          */
onPeriodicAdvertisingDataSet(AdvertisingSet advertisingSet, int status)124         public void onPeriodicAdvertisingDataSet(AdvertisingSet advertisingSet,
125                 int status) {
126         }
127 
128         /**
129          * Callback triggered in response to {@link AdvertisingSet#setPeriodicAdvertisingEnabled}
130          * indicating result of the operation.
131          *
132          * @param advertisingSet The advertising set.
133          * @param status Status of the operation.
134          */
onPeriodicAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable, int status)135         public void onPeriodicAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable,
136                 int status) {
137         }
138 
139         /**
140          * Callback triggered in response to {@link AdvertisingSet#getOwnAddress()}
141          * indicating result of the operation. (In the real callback, this was hidden API).
142          *
143          * @param advertisingSet The advertising set.
144          * @param addressType type of address.
145          * @param address advertising set bluetooth address.
146          */
onOwnAddressRead(AdvertisingSet advertisingSet, int addressType, String address)147         public void onOwnAddressRead(AdvertisingSet advertisingSet, int addressType,
148                 String address) {
149         }
150     }
151 
AdvertisingSetCallbackHelper()152     private AdvertisingSetCallbackHelper() {
153         throw new UnsupportedOperationException("contains only static members");
154     }
155 
156     /**
157      * Creates a real {@link AdvertisingSetCallback} by wrapping a {@link Callback}.
158      * Wrapping is needed because some of the methods in {@link AdvertisingSetCallback} are
159      * hidden APIs, so cannot be overridden within a Mainline module.
160      *
161      * @param proxy The proxy of {@link AdvertisingSetCallback} that is to be wrapped into a
162      *              real one.
163      * @return A real {@link AdvertisingSetCallback}.
164      */
createRealCallbackFromProxy( @onNull Callback proxy)165     public static AdvertisingSetCallback createRealCallbackFromProxy(
166             @NonNull Callback proxy) {
167 
168         AdvertisingSetCallback realCallback = new AdvertisingSetCallback() {
169             @Override
170             public void onAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower,
171                     int status) {
172                 proxy.onAdvertisingSetStarted(advertisingSet, txPower, status);
173             }
174 
175             @Override
176             public void onAdvertisingSetStopped(AdvertisingSet advertisingSet) {
177                 proxy.onAdvertisingSetStopped(advertisingSet);
178             }
179 
180             @Override
181             public void onAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable,
182                     int status) {
183                 proxy.onAdvertisingEnabled(advertisingSet, enable, status);
184             }
185 
186             @Override
187             public void onAdvertisingDataSet(AdvertisingSet advertisingSet, int status) {
188                 proxy.onAdvertisingDataSet(advertisingSet, status);
189             }
190 
191             @Override
192             public void onScanResponseDataSet(AdvertisingSet advertisingSet, int status) {
193                 proxy.onScanResponseDataSet(advertisingSet, status);
194             }
195 
196             @Override
197             public void onAdvertisingParametersUpdated(AdvertisingSet advertisingSet,
198                     int txPower, int status) {
199                 proxy.onAdvertisingParametersUpdated(advertisingSet, txPower, status);
200             }
201 
202             @Override
203             public void onPeriodicAdvertisingParametersUpdated(AdvertisingSet advertisingSet,
204                     int status) {
205                 proxy.onPeriodicAdvertisingParametersUpdated(advertisingSet, status);
206             }
207 
208             @Override
209             public void onPeriodicAdvertisingDataSet(AdvertisingSet advertisingSet,
210                     int status) {
211                 proxy.onPeriodicAdvertisingDataSet(advertisingSet, status);
212             }
213 
214             @Override
215             public void onPeriodicAdvertisingEnabled(AdvertisingSet advertisingSet, boolean enable,
216                     int status) {
217                 proxy.onPeriodicAdvertisingEnabled(advertisingSet, enable, status);
218             }
219 
220             @Override
221             public void onOwnAddressRead(AdvertisingSet advertisingSet, int addressType,
222                     String address) {
223                 proxy.onOwnAddressRead(advertisingSet, addressType, address);
224             }
225         };
226 
227         return realCallback;
228     }
229 }
230