Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit da624dbe authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

CsipSetCoordinatorService: Fix race on bluetooth turning off

When stressing BT ON/OFF it might happen that service is not available
when CsipSetCoordinatorService is trying to get it.
In such case, let's be nice and do not throw exception

Bug: 324533075
Test: atest BluetoothCsipSetCooridinatorTest
Flags: Exempt, trivial fix.
Change-Id: Ib72decacfa397ee716d85cd2a4a0bfed02774a40
parent 183a5225
Loading
Loading
Loading
Loading
+59 −80
Original line number Diff line number Diff line
@@ -1077,15 +1077,13 @@ public class CsipSetCoordinatorService extends ProfileService {
                Objects.requireNonNull(source, "source cannot be null");
                Objects.requireNonNull(receiver, "receiver cannot be null");

                boolean defaultValue = false;
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);

                boolean result = service.connect(device);
                receiver.send(result);
                    defaultValue = service.connect(device);
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
@@ -1099,15 +1097,13 @@ public class CsipSetCoordinatorService extends ProfileService {
                Objects.requireNonNull(source, "source cannot be null");
                Objects.requireNonNull(receiver, "receiver cannot be null");

                boolean defaultValue = false;
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);

                boolean result = service.disconnect(device);
                receiver.send(result);
                    defaultValue = service.disconnect(device);
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
@@ -1120,15 +1116,13 @@ public class CsipSetCoordinatorService extends ProfileService {
                Objects.requireNonNull(source, "source cannot be null");
                Objects.requireNonNull(receiver, "receiver cannot be null");

                List<BluetoothDevice> defaultValue = new ArrayList<>();
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);

                List<BluetoothDevice> result = service.getConnectedDevices();
                receiver.send(result);
                    defaultValue = service.getConnectedDevices();
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
@@ -1141,15 +1135,13 @@ public class CsipSetCoordinatorService extends ProfileService {
                Objects.requireNonNull(source, "source cannot be null");
                Objects.requireNonNull(receiver, "receiver cannot be null");

                List<BluetoothDevice> defaultValue = new ArrayList<>();
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);

                List<BluetoothDevice> result = service.getDevicesMatchingConnectionStates(states);
                receiver.send(result);
                    defaultValue = service.getDevicesMatchingConnectionStates(states);
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
@@ -1163,14 +1155,13 @@ public class CsipSetCoordinatorService extends ProfileService {
                Objects.requireNonNull(source, "source cannot be null");
                Objects.requireNonNull(receiver, "receiver cannot be null");

                int defaultValue = BluetoothProfile.STATE_DISCONNECTED;
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);
                int result = service.getConnectionState(device);
                receiver.send(result);
                    defaultValue = service.getConnectionState(device);
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
@@ -1184,15 +1175,13 @@ public class CsipSetCoordinatorService extends ProfileService {
                Objects.requireNonNull(source, "source cannot be null");
                Objects.requireNonNull(receiver, "receiver cannot be null");

                boolean defaultValue = false;
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);

                boolean result = service.setConnectionPolicy(device, connectionPolicy);
                receiver.send(result);
                    defaultValue = service.setConnectionPolicy(device, connectionPolicy);
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
@@ -1206,15 +1195,13 @@ public class CsipSetCoordinatorService extends ProfileService {
                Objects.requireNonNull(source, "source cannot be null");
                Objects.requireNonNull(receiver, "receiver cannot be null");

                int defaultValue = BluetoothProfile.CONNECTION_POLICY_UNKNOWN;
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);

                int result = service.getConnectionPolicy(device);
                receiver.send(result);
                    defaultValue = service.getConnectionPolicy(device);
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
@@ -1229,16 +1216,14 @@ public class CsipSetCoordinatorService extends ProfileService {
                Objects.requireNonNull(source, "source cannot be null");
                Objects.requireNonNull(receiver, "receiver cannot be null");

                ParcelUuid defaultValue = null;
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);

                    UUID lockUuid = service.lockGroup(groupId, callback);
                ParcelUuid result = lockUuid == null ? null : new ParcelUuid(lockUuid);
                receiver.send(result);
                    defaultValue = lockUuid == null ? null : new ParcelUuid(lockUuid);
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
@@ -1253,13 +1238,11 @@ public class CsipSetCoordinatorService extends ProfileService {
                Objects.requireNonNull(receiver, "receiver cannot be null");

                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);

                    service.unlockGroup(lockUuid.getUuid());
                }

                receiver.send(null);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
@@ -1274,13 +1257,13 @@ public class CsipSetCoordinatorService extends ProfileService {
                Objects.requireNonNull(source, "source cannot be null");
                Objects.requireNonNull(receiver, "receiver cannot be null");

                List<Integer> defaultValue = new ArrayList<Integer>();
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }
                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);
                List<Integer> result = service.getAllGroupIds(uuid);
                receiver.send(result);
                    defaultValue = service.getAllGroupIds(uuid);
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
@@ -1290,15 +1273,13 @@ public class CsipSetCoordinatorService extends ProfileService {
        public void getGroupUuidMapByDevice(BluetoothDevice device,
                AttributionSource source, SynchronousResultReceiver receiver) {
            try {
                Map<Integer, ParcelUuid> defaultValue = null;
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);

                Map<Integer, ParcelUuid> result = service.getGroupUuidMapByDevice(device);
                receiver.send(result);
                    defaultValue = service.getGroupUuidMapByDevice(device);
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }
@@ -1308,15 +1289,13 @@ public class CsipSetCoordinatorService extends ProfileService {
        public void getDesiredGroupSize(int groupId, AttributionSource source,
                SynchronousResultReceiver receiver) {
            try {
                int defaultValue = IBluetoothCsipSetCoordinator.CSIS_GROUP_SIZE_UNKNOWN;
                CsipSetCoordinatorService service = getService();
                if (service == null) {
                    throw new IllegalStateException("service is null");
                }

                if (service != null) {
                    enforceBluetoothPrivilegedPermission(service);

                int result = service.getDesiredGroupSize(groupId);
                receiver.send(result);
                    defaultValue = service.getDesiredGroupSize(groupId);
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
                receiver.propagateException(e);
            }