blob: 29ae950f0b2abe276920afd24b3595ba9f0249e8 [file] [log] [blame]
/*
* Copyright 2021 HIMSA II K/S - www.himsa.com.
* Represented by EHIMA - www.ehima.com
*
* 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 static android.bluetooth.BluetoothUtils.getSyncTimeout;
import android.Manifest;
import android.annotation.CallbackExecutor;
import android.annotation.FlaggedApi;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
import android.content.AttributionSource;
import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.CloseGuard;
import android.util.Log;
import com.android.bluetooth.flags.Flags;
import com.android.modules.utils.SynchronousResultReceiver;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeoutException;
/**
* This class provides the public APIs to control the Bluetooth Volume Control service.
*
* <p>BluetoothVolumeControl is a proxy object for controlling the Bluetooth VC Service via IPC. Use
* {@link BluetoothAdapter#getProfileProxy} to get the BluetoothVolumeControl proxy object.
*
* @hide
*/
@SystemApi
public final class BluetoothVolumeControl implements BluetoothProfile, AutoCloseable {
private static final String TAG = "BluetoothVolumeControl";
private static final boolean DBG = true;
private static final boolean VDBG = false;
private CloseGuard mCloseGuard;
private final Map<Callback, Executor> mCallbackExecutorMap = new HashMap<>();
/**
* This class provides a callback that is invoked when volume offset value changes on the remote
* device.
*
* <p>In order to balance volume on the group of Le Audio devices, Volume Offset Control Service
* (VOCS) shall be used. User can verify if the remote device supports VOCS by calling {@link
* #isVolumeOffsetAvailable(device)}.
*
* @hide
*/
@SystemApi
public interface Callback {
/**
* Callback invoked when callback is registered and when volume offset changes on the remote
* device. Change can be triggered autonomously by the remote device or after volume offset
* change on the user request done by calling {@link #setVolumeOffset(device, volumeOffset)}
*
* @param device remote device whose volume offset changed
* @param volumeOffset latest volume offset for this device
* @hide
*/
@SystemApi
void onVolumeOffsetChanged(
@NonNull BluetoothDevice device, @IntRange(from = -255, to = 255) int volumeOffset);
/**
* Callback for le audio connected device volume level change
*
* <p>The valid volume range is [0, 255], as defined in 2.3.1.1 Volume_Setting field of
* Volume Control Service, Version 1.0.
*
* @param device remote device whose volume changed
* @param volume level
* @hide
*/
@FlaggedApi(Flags.FLAG_LEAUDIO_BROADCAST_VOLUME_CONTROL_FOR_CONNECTED_DEVICES)
@SystemApi
default void onDeviceVolumeChanged(
@NonNull BluetoothDevice device, @IntRange(from = 0, to = 255) int volume) {}
}
@SuppressLint("AndroidFrameworkBluetoothPermission")
private final IBluetoothVolumeControlCallback mCallback =
new IBluetoothVolumeControlCallback.Stub() {
@Override
public void onVolumeOffsetChanged(
@NonNull BluetoothDevice device, int volumeOffset) {
Attributable.setAttributionSource(device, mAttributionSource);
for (Map.Entry<BluetoothVolumeControl.Callback, Executor>
callbackExecutorEntry : mCallbackExecutorMap.entrySet()) {
BluetoothVolumeControl.Callback callback = callbackExecutorEntry.getKey();
Executor executor = callbackExecutorEntry.getValue();
executor.execute(
() -> callback.onVolumeOffsetChanged(device, volumeOffset));
}
}
@Override
public void onDeviceVolumeChanged(@NonNull BluetoothDevice device, int volume) {
Attributable.setAttributionSource(device, mAttributionSource);
for (Map.Entry<BluetoothVolumeControl.Callback, Executor>
callbackExecutorEntry : mCallbackExecutorMap.entrySet()) {
BluetoothVolumeControl.Callback callback = callbackExecutorEntry.getKey();
Executor executor = callbackExecutorEntry.getValue();
executor.execute(() -> callback.onDeviceVolumeChanged(device, volume));
}
}
};
/**
* Intent used to broadcast the change in connection state of the Volume Control profile.
*
* <p>This intent will have 3 extras:
*
* <ul>
* <li>{@link #EXTRA_STATE} - The current state of the profile.
* <li>{@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.
* <li>{@link BluetoothDevice#EXTRA_DEVICE} - The remote device.
* </ul>
*
* <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of {@link
* #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, {@link #STATE_CONNECTED}, {@link
* #STATE_DISCONNECTING}.
*
* @hide
*/
@SystemApi
@SuppressLint("ActionValue")
@RequiresBluetoothConnectPermission
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_CONNECTION_STATE_CHANGED =
"android.bluetooth.volume-control.profile.action.CONNECTION_STATE_CHANGED";
private BluetoothAdapter mAdapter;
private final AttributionSource mAttributionSource;
private IBluetoothVolumeControl mService;
/**
* Create a BluetoothVolumeControl proxy object for interacting with the local Bluetooth Volume
* Control service.
*/
/*package*/ BluetoothVolumeControl(Context context, BluetoothAdapter adapter) {
mAdapter = adapter;
mAttributionSource = adapter.getAttributionSource();
mService = null;
mCloseGuard = new CloseGuard();
mCloseGuard.open("close");
}
@RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED)
@SuppressWarnings("Finalize") // TODO(b/314811467)
protected void finalize() {
if (mCloseGuard != null) {
mCloseGuard.warnIfOpen();
}
close();
}
/**
* Close this VolumeControl server instance.
*
* <p>Application should call this method as early as possible after it is done with this
* VolumeControl server.
*/
@RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED)
@Override
public void close() {
if (VDBG) log("close()");
mAdapter.closeProfileProxy(this);
}
/** @hide */
@Override
public void onServiceConnected(IBinder service) {
mService = IBluetoothVolumeControl.Stub.asInterface(service);
// re-register the service-to-app callback
synchronized (mCallbackExecutorMap) {
if (!mCallbackExecutorMap.isEmpty()) {
try {
if (service != null) {
final SynchronousResultReceiver<Integer> recv =
SynchronousResultReceiver.get();
mService.registerCallback(mCallback, mAttributionSource, recv);
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
}
} catch (RemoteException e) {
Log.e(
TAG,
"onBluetoothServiceUp: Failed to register" + "Volume Control callback",
e);
} catch (TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
}
}
/** @hide */
@Override
public void onServiceDisconnected() {
mService = null;
}
private IBluetoothVolumeControl getService() {
return mService;
}
/** @hide */
@Override
public BluetoothAdapter getAdapter() {
return mAdapter;
}
/**
* Get the list of connected devices. Currently at most one.
*
* @return list of connected devices
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(
allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public @NonNull List<BluetoothDevice> getConnectedDevices() {
if (DBG) log("getConnectedDevices()");
final IBluetoothVolumeControl service = getService();
final List<BluetoothDevice> defaultValue = new ArrayList<BluetoothDevice>();
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver<List<BluetoothDevice>> recv =
SynchronousResultReceiver.get();
service.getConnectedDevices(mAttributionSource, recv);
return Attributable.setAttributionSource(
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue),
mAttributionSource);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Get the list of devices matching specified states. Currently at most one.
*
* @return list of matching devices
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (DBG) log("getDevicesMatchingStates()");
final IBluetoothVolumeControl service = getService();
final List<BluetoothDevice> defaultValue = new ArrayList<BluetoothDevice>();
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver<List<BluetoothDevice>> recv =
SynchronousResultReceiver.get();
service.getDevicesMatchingConnectionStates(states, mAttributionSource, recv);
return Attributable.setAttributionSource(
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue),
mAttributionSource);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Get connection state of device
*
* @return device connection state
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
public int getConnectionState(BluetoothDevice device) {
if (DBG) log("getConnectionState(" + device + ")");
final IBluetoothVolumeControl service = getService();
final int defaultValue = BluetoothProfile.STATE_DISCONNECTED;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();
service.getConnectionState(device, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Register a {@link Callback} that will be invoked during the operation of this profile.
*
* <p>Repeated registration of the same <var>callback</var> object will have no effect after the
* first call to this method, even when the <var>executor</var> is different. API caller would
* have to call {@link #unregisterCallback(Callback)} with the same callback object before
* registering it again.
*
* @param executor an {@link Executor} to execute given callback
* @param callback user implementation of the {@link Callback}
* @throws IllegalArgumentException if a null executor, sink, or callback is given
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(
allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public void registerCallback(
@NonNull @CallbackExecutor Executor executor, @NonNull Callback callback) {
Objects.requireNonNull(executor, "executor cannot be null");
Objects.requireNonNull(callback, "callback cannot be null");
if (DBG) log("registerCallback");
synchronized (mCallbackExecutorMap) {
if (!mAdapter.isEnabled()) {
/* If Bluetooth is off, just store callback and it will be registered
* when Bluetooth is on
*/
mCallbackExecutorMap.put(callback, executor);
return;
}
// Adds the passed in callback to our map of callbacks to executors
if (mCallbackExecutorMap.containsKey(callback)) {
throw new IllegalArgumentException("This callback has already been registered");
}
try {
final IBluetoothVolumeControl service = getService();
if (service != null) {
final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();
/* If the callback map is empty, we register the service-to-app callback.
* Otherwise, callback is registered in mCallbackExecutorMap and we just notify
* user over callback with current values.
*/
boolean isRegisterCallbackRequired = mCallbackExecutorMap.isEmpty();
mCallbackExecutorMap.put(callback, executor);
if (isRegisterCallbackRequired) {
service.registerCallback(mCallback, mAttributionSource, recv);
} else {
service.notifyNewRegisteredCallback(
new IBluetoothVolumeControlCallback.Stub() {
@Override
public void onVolumeOffsetChanged(
BluetoothDevice device, int volumeOffset)
throws RemoteException {
Attributable.setAttributionSource(
device, mAttributionSource);
executor.execute(
() ->
callback.onVolumeOffsetChanged(
device, volumeOffset));
}
@Override
public void onDeviceVolumeChanged(
BluetoothDevice device, int volume)
throws RemoteException {
Attributable.setAttributionSource(
device, mAttributionSource);
executor.execute(
() ->
callback.onDeviceVolumeChanged(
device, volume));
}
},
mAttributionSource,
recv);
}
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
}
} catch (RemoteException e) {
mCallbackExecutorMap.remove(callback);
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
throw e.rethrowAsRuntimeException();
} catch (TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
}
/**
* Unregister the specified {@link Callback}.
*
* <p>The same {@link Callback} object used when calling {@link #registerCallback(Executor,
* Callback)} must be used.
*
* <p>Callbacks are automatically unregistered when application process goes away
*
* @param callback user implementation of the {@link Callback}
* @throws IllegalArgumentException when callback is null or when no callback is registered
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(
allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public void unregisterCallback(@NonNull Callback callback) {
Objects.requireNonNull(callback, "callback cannot be null");
if (DBG) log("unregisterCallback");
synchronized (mCallbackExecutorMap) {
if (mCallbackExecutorMap.remove(callback) == null) {
throw new IllegalArgumentException("This callback has not been registered");
}
}
// If the callback map is empty, we unregister the service-to-app callback
if (mCallbackExecutorMap.isEmpty()) {
try {
final IBluetoothVolumeControl service = getService();
if (service != null) {
final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();
service.unregisterCallback(mCallback, mAttributionSource, recv);
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
}
} catch (RemoteException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
throw e.rethrowAsRuntimeException();
} catch (IllegalStateException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
}
/**
* Tells the remote device to set a volume offset to the absolute volume.
*
* @param device {@link BluetoothDevice} representing the remote device
* @param volumeOffset volume offset to be set on the remote device
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(
allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public void setVolumeOffset(
@NonNull BluetoothDevice device, @IntRange(from = -255, to = 255) int volumeOffset) {
if (DBG) log("setVolumeOffset(" + device + " volumeOffset: " + volumeOffset + ")");
final IBluetoothVolumeControl service = getService();
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver recv = SynchronousResultReceiver.get();
service.setVolumeOffset(device, volumeOffset, mAttributionSource, recv);
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
}
/**
* Provides information about the possibility to set volume offset on the remote device. If the
* remote device supports Volume Offset Control Service, it is automatically connected.
*
* @param device {@link BluetoothDevice} representing the remote device
* @return {@code true} if volume offset function is supported and available to use on the
* remote device. When Bluetooth is off, the return value should always be {@code false}.
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(
allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public boolean isVolumeOffsetAvailable(@NonNull BluetoothDevice device) {
if (DBG) log("isVolumeOffsetAvailable(" + device + ")");
final IBluetoothVolumeControl service = getService();
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
return false;
}
if (!isEnabled()) {
return false;
}
final boolean defaultValue = false;
try {
final SynchronousResultReceiver recv = SynchronousResultReceiver.get();
service.isVolumeOffsetAvailable(device, mAttributionSource, recv);
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
return defaultValue;
}
/**
* Set connection policy of the profile
*
* <p>The device should already be paired. Connection policy can be one of {@link
* #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN}, {@link
* #CONNECTION_POLICY_UNKNOWN}
*
* @param device Paired bluetooth device
* @param connectionPolicy is the connection policy to set to for this profile
* @return true if connectionPolicy is set, false on error
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(
allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public boolean setConnectionPolicy(
@NonNull BluetoothDevice device, @ConnectionPolicy int connectionPolicy) {
if (DBG) log("setConnectionPolicy(" + device + ", " + connectionPolicy + ")");
final IBluetoothVolumeControl service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()
&& isValidDevice(device)
&& (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
|| connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED)) {
try {
final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
service.setConnectionPolicy(device, connectionPolicy, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Get the connection policy of the profile.
*
* <p>The connection policy can be any of: {@link #CONNECTION_POLICY_ALLOWED}, {@link
* #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN}
*
* @param device Bluetooth device
* @return connection policy of the device
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(
allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public @ConnectionPolicy int getConnectionPolicy(@NonNull BluetoothDevice device) {
if (VDBG) log("getConnectionPolicy(" + device + ")");
final IBluetoothVolumeControl service = getService();
final int defaultValue = BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();
service.getConnectionPolicy(device, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Set volume for the le audio device
*
* <p>This provides volume control for connected remote device directly by volume control
* service. The valid volume range is [0, 255], as defined in 2.3.1.1 Volume_Setting field of
* Volume Control Service, Version 1.0.
*
* <p>For le audio unicast devices volume control, application should consider to use {@link
* BluetoothLeAudio#setVolume} instead to control active device volume.
*
* @param device {@link BluetoothDevice} representing the remote device
* @param volume level to set
* @param isGroupOperation {@code true} if Application wants to perform this operation for all
* coordinated set members throughout this session. Otherwise, caller would have to control
* individual device volume.
* @throws IllegalArgumentException if volume is not in the range [0, 255].
* @hide
*/
@FlaggedApi(Flags.FLAG_LEAUDIO_BROADCAST_VOLUME_CONTROL_FOR_CONNECTED_DEVICES)
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(
allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public void setDeviceVolume(
@NonNull BluetoothDevice device,
@IntRange(from = 0, to = 255) int volume,
boolean isGroupOperation) {
if (volume < 0 || volume > 255) {
throw new IllegalArgumentException("illegal volume " + volume);
}
final IBluetoothVolumeControl service = getService();
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver recv = SynchronousResultReceiver.get();
service.setDeviceVolume(device, volume, isGroupOperation, mAttributionSource, recv);
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
}
private boolean isEnabled() {
return mAdapter.getState() == BluetoothAdapter.STATE_ON;
}
private static boolean isValidDevice(@Nullable BluetoothDevice device) {
return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
}
private static void log(String msg) {
Log.d(TAG, msg);
}
}