diff options
author | Jack He <siyuanh@google.com> | 2022-03-03 06:50:22 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-03-03 06:50:22 +0000 |
commit | 2b130e18935864a10ea554c96c217a07c09d295a (patch) | |
tree | bf0f0c8fd92d0a2d6ece056ea120e944ece9a25a /framework/java/android/bluetooth/BluetoothLeBroadcastAssistantCallback.java | |
parent | 84e3f271b6248b9a2b646464d776640d92ee4b24 (diff) | |
parent | 43c9825758098f8a8effbabaf358ee36a15a5aab (diff) |
Merge "BT LE broadcast assistant implementation" am: 4024c5ee49 am: 627c75137f am: 43c9825758
Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/1952637
Change-Id: I537e81d8bb6c9fd8306e367f9f599412d047ed40
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothLeBroadcastAssistantCallback.java')
-rwxr-xr-x | framework/java/android/bluetooth/BluetoothLeBroadcastAssistantCallback.java | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/framework/java/android/bluetooth/BluetoothLeBroadcastAssistantCallback.java b/framework/java/android/bluetooth/BluetoothLeBroadcastAssistantCallback.java new file mode 100755 index 0000000000..96e04a9424 --- /dev/null +++ b/framework/java/android/bluetooth/BluetoothLeBroadcastAssistantCallback.java @@ -0,0 +1,271 @@ +/* + * Copyright 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.bluetooth; + +import android.annotation.NonNull; +import android.os.Binder; +import android.os.RemoteException; +import android.util.Log; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executor; + +/** + * @hide + */ +public class BluetoothLeBroadcastAssistantCallback + extends IBluetoothLeBroadcastAssistantCallback.Stub { + private static final String TAG = BluetoothLeBroadcastAssistantCallback.class.getSimpleName(); + private boolean mIsRegistered = false; + private final Map<BluetoothLeBroadcastAssistant.Callback, + Executor> mCallbackMap = new HashMap<>(); + IBluetoothLeBroadcastAssistant mAdapter; + + public BluetoothLeBroadcastAssistantCallback(IBluetoothLeBroadcastAssistant adapter) { + mAdapter = adapter; + } + + /** + * @hide + * @param executor an {@link Executor} to execute given callback + * @param callback user implementation of the {@link BluetoothLeBroadcastAssistant#Callback} + */ + public void register(@NonNull Executor executor, + @NonNull BluetoothLeBroadcastAssistant.Callback callback) { + synchronized (this) { + if (mCallbackMap.containsKey(callback)) { + return; + } + mCallbackMap.put(callback, executor); + + if (!mIsRegistered) { + try { + mAdapter.registerCallback(this); + mIsRegistered = true; + } catch (RemoteException e) { + Log.w(TAG, "Failed to register broaddcast assistant callback"); + Log.e(TAG, Log.getStackTraceString(new Throwable())); + } + } + } + } + + /** + * @hide + * @param callback user implementation of the {@link BluetoothLeBroadcastAssistant#Callback} + */ + public void unregister(@NonNull BluetoothLeBroadcastAssistant.Callback callback) { + synchronized (this) { + if (!mCallbackMap.containsKey(callback)) { + return; + } + mCallbackMap.remove(callback); + if (mCallbackMap.isEmpty() && mIsRegistered) { + try { + mAdapter.unregisterCallback(this); + mIsRegistered = false; + } catch (RemoteException e) { + Log.w(TAG, "Failed to unregister broaddcast assistant with service"); + Log.e(TAG, Log.getStackTraceString(new Throwable())); + } + } + } + } + + @Override + public void onSearchStarted(int reason) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSearchStarted(reason)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onSearchStartFailed(int reason) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSearchStartFailed(reason)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onSearchStopped(int reason) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSearchStopped(reason)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onSearchStopFailed(int reason) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSearchStopFailed(reason)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onSourceFound(BluetoothLeBroadcastMetadata source) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSourceFound(source)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onSourceAdded(BluetoothDevice sink, int sourceId, int reason) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSourceAdded(sink, sourceId, reason)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onSourceAddFailed(BluetoothDevice sink, BluetoothLeBroadcastMetadata source, + int reason) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSourceAddFailed(sink, source, reason)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onSourceModified(BluetoothDevice sink, int sourceId, int reason) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSourceModified(sink, sourceId, reason)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onSourceModifyFailed(BluetoothDevice sink, int sourceId, int reason) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSourceModifyFailed(sink, sourceId, reason)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onSourceRemoved(BluetoothDevice sink, int sourceId, int reason) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSourceRemoved(sink, sourceId, reason)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onSourceRemoveFailed(BluetoothDevice sink, int sourceId, int reason) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onSourceRemoveFailed(sink, sourceId, reason)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } + + @Override + public void onReceiveStateChanged(BluetoothDevice sink, int sourceId, + BluetoothLeBroadcastReceiveState state) { + synchronized (this) { + for (BluetoothLeBroadcastAssistant.Callback cb : mCallbackMap.keySet()) { + Executor executor = mCallbackMap.get(cb); + final long identity = Binder.clearCallingIdentity(); + try { + executor.execute(() -> cb.onReceiveStateChanged(sink, sourceId, state)); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + } + } +} |