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

Commit ddead331 authored by William Escande's avatar William Escande
Browse files

Hap: early return and switch syntax

Simplify the code by using early return and try to have easier logic
flow

Bug: 311772251
Flag: Exempt refactor
Test: atest HapClientServiceTest
Change-Id: I80c5878e3c37fe92e119e7168e0ffcdf2bfcfeb2
parent 16ab0ced
Loading
Loading
Loading
Loading
+150 −169
Original line number Diff line number Diff line
@@ -207,7 +207,7 @@ public class HapClientService extends ProfileService {
        mDeviceFeaturesMap.clear();
        mPresetsMap.clear();

        if (mCallbacks != null) {
        synchronized (mCallbacks) {
            mCallbacks.kill();
        }
    }
@@ -550,16 +550,20 @@ public class HapClientService extends ProfileService {
     */
    public @Nullable BluetoothHapPresetInfo getActivePresetInfo(BluetoothDevice device) {
        int index = getActivePresetIndex(device);
        if (index == BluetoothHapClient.PRESET_INDEX_UNAVAILABLE) return null;
        if (index == BluetoothHapClient.PRESET_INDEX_UNAVAILABLE) {
            return null;
        }

        List<BluetoothHapPresetInfo> current_presets = mPresetsMap.get(device);
        if (current_presets != null) {
        if (current_presets == null) {
            return null;
        }

        for (BluetoothHapPresetInfo preset : current_presets) {
            if (preset.getIndex() == index) {
                return preset;
            }
        }
        }

        return null;
    }
@@ -754,39 +758,48 @@ public class HapClientService extends ProfileService {
    }

    private boolean isPresetIndexValid(BluetoothDevice device, int presetIndex) {
        if (presetIndex == BluetoothHapClient.PRESET_INDEX_UNAVAILABLE) return false;
        if (presetIndex == BluetoothHapClient.PRESET_INDEX_UNAVAILABLE) {
            return false;
        }

        List<BluetoothHapPresetInfo> device_presets = mPresetsMap.get(device);
        if (device_presets != null) {
        if (device_presets == null) {
            return false;
        }
        for (BluetoothHapPresetInfo preset : device_presets) {
            if (preset.getIndex() == presetIndex) {
                return true;
            }
        }
        }
        return false;
    }

    private boolean isPresetIndexValid(int groupId, int presetIndex) {
        List<BluetoothDevice> all_group_devices = getGroupDevices(groupId);
        if (all_group_devices.isEmpty()) return false;
        if (all_group_devices.isEmpty()) {
            return false;
        }

        for (BluetoothDevice device : all_group_devices) {
            if (!isPresetIndexValid(device, presetIndex)) return false;
            if (!isPresetIndexValid(device, presetIndex)) {
                return false;
            }
        }
        return true;
    }

    private boolean isGroupIdValid(int groupId) {
        if (groupId == BluetoothCsipSetCoordinator.GROUP_ID_INVALID) return false;
        if (groupId == BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
            return false;
        }

        CsipSetCoordinatorService csipClient = mFactory.getCsipSetCoordinatorService();
        if (csipClient != null) {
        if (csipClient == null) {
            return false;
        }
        List<Integer> groups = csipClient.getAllGroupIds(BluetoothUuid.CAP);
        return groups.contains(groupId);
    }
        return false;
    }

    /**
     * Sets the preset name
@@ -840,18 +853,16 @@ public class HapClientService extends ProfileService {
    void updateDevicePresetsCache(
            BluetoothDevice device, int infoReason, List<BluetoothHapPresetInfo> presets) {
        switch (infoReason) {
            case HapClientStackEvent.PRESET_INFO_REASON_ALL_PRESET_INFO:
            case HapClientStackEvent.PRESET_INFO_REASON_ALL_PRESET_INFO -> {
                mPresetsMap.put(device, presets);
                break;
            case HapClientStackEvent.PRESET_INFO_REASON_PRESET_INFO_UPDATE:
            case HapClientStackEvent.PRESET_INFO_REASON_PRESET_AVAILABILITY_CHANGED:
            case HapClientStackEvent.PRESET_INFO_REASON_PRESET_INFO_REQUEST_RESPONSE:
                {
            }
            case HapClientStackEvent.PRESET_INFO_REASON_PRESET_INFO_UPDATE,
                    HapClientStackEvent.PRESET_INFO_REASON_PRESET_AVAILABILITY_CHANGED,
                    HapClientStackEvent.PRESET_INFO_REASON_PRESET_INFO_REQUEST_RESPONSE -> {
                List current_presets = mPresetsMap.get(device);
                if (current_presets != null) {
                    for (BluetoothHapPresetInfo new_preset : presets) {
                            ListIterator<BluetoothHapPresetInfo> iter =
                                    current_presets.listIterator();
                        ListIterator<BluetoothHapPresetInfo> iter = current_presets.listIterator();
                        while (iter.hasNext()) {
                            if (iter.next().getIndex() == new_preset.getIndex()) {
                                iter.remove();
@@ -860,20 +871,15 @@ public class HapClientService extends ProfileService {
                        }
                    }
                    current_presets.addAll(presets);
                        mPresetsMap.put(device, current_presets);
                    } else {
                        mPresetsMap.put(device, presets);
                    presets = current_presets;
                }
                mPresetsMap.put(device, presets);
            }
                break;

            case HapClientStackEvent.PRESET_INFO_REASON_PRESET_DELETED:
                {
            case HapClientStackEvent.PRESET_INFO_REASON_PRESET_DELETED -> {
                List current_presets = mPresetsMap.get(device);
                if (current_presets != null) {
                    for (BluetoothHapPresetInfo new_preset : presets) {
                            ListIterator<BluetoothHapPresetInfo> iter =
                                    current_presets.listIterator();
                        ListIterator<BluetoothHapPresetInfo> iter = current_presets.listIterator();
                        while (iter.hasNext()) {
                            if (iter.next().getIndex() == new_preset.getIndex()) {
                                iter.remove();
@@ -884,23 +890,21 @@ public class HapClientService extends ProfileService {
                    mPresetsMap.put(device, current_presets);
                }
            }
                break;

            default:
                break;
            default -> {}
        }
    }

    private List<BluetoothDevice> getGroupDevices(int groupId) {
        List<BluetoothDevice> devices = new ArrayList<>();
        if (groupId == BluetoothLeAudio.GROUP_ID_INVALID) {
            return Collections.emptyList();
        }

        CsipSetCoordinatorService csipClient = mFactory.getCsipSetCoordinatorService();
        if (csipClient != null) {
            if (groupId != BluetoothLeAudio.GROUP_ID_INVALID) {
                devices = csipClient.getGroupDevicesOrdered(groupId);
            }
        if (csipClient == null) {
            return Collections.emptyList();
        }
        return devices;

        return csipClient.getGroupDevicesOrdered(groupId);
    }

    /**
@@ -918,8 +922,7 @@ public class HapClientService extends ProfileService {
        BluetoothDevice device = stackEvent.device;

        switch (stackEvent.type) {
            case (HapClientStackEvent.EVENT_TYPE_DEVICE_AVAILABLE):
                {
            case HapClientStackEvent.EVENT_TYPE_DEVICE_AVAILABLE -> {
                int features = stackEvent.valueInt1;

                if (device != null) {
@@ -933,10 +936,8 @@ public class HapClientService extends ProfileService {
                            intent, new String[] {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED});
                }
            }
                break;

            case (HapClientStackEvent.EVENT_TYPE_DEVICE_FEATURES):
                {
            case HapClientStackEvent.EVENT_TYPE_DEVICE_FEATURES -> {
                int features = stackEvent.valueInt1;

                if (device != null) {
@@ -947,10 +948,7 @@ public class HapClientService extends ProfileService {
                                    + (" features=" + String.format("0x%04X", features)));
                }
            }
                return;

            case (HapClientStackEvent.EVENT_TYPE_ON_ACTIVE_PRESET_SELECTED):
                {
            case HapClientStackEvent.EVENT_TYPE_ON_ACTIVE_PRESET_SELECTED -> {
                int presetIndex = stackEvent.valueInt1;
                int groupId = stackEvent.valueInt2;
                // FIXME: Add app request queueing to support other reasons
@@ -968,24 +966,17 @@ public class HapClientService extends ProfileService {
                    }
                }
            }
                return;

            case (HapClientStackEvent.EVENT_TYPE_ON_ACTIVE_PRESET_SELECT_ERROR):
                {
            case HapClientStackEvent.EVENT_TYPE_ON_ACTIVE_PRESET_SELECT_ERROR -> {
                int groupId = stackEvent.valueInt2;
                int status = stackEventStatusToProfileStatus(stackEvent.valueInt1);

                if (device != null) {
                    broadcastToClient(cb -> cb.onPresetSelectionFailed(device, status));
                } else if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
                        broadcastToClient(
                                cb -> cb.onPresetSelectionForGroupFailed(groupId, status));
                    broadcastToClient(cb -> cb.onPresetSelectionForGroupFailed(groupId, status));
                }
            }
                break;

            case (HapClientStackEvent.EVENT_TYPE_ON_PRESET_INFO):
                {
            case HapClientStackEvent.EVENT_TYPE_ON_PRESET_INFO -> {
                int infoReason = stackEvent.valueInt2;
                int groupId = stackEvent.valueInt3;
                ArrayList presets = stackEvent.valueList;
@@ -1002,10 +993,7 @@ public class HapClientService extends ProfileService {
                    }
                }
            }
                return;

            case (HapClientStackEvent.EVENT_TYPE_ON_PRESET_NAME_SET_ERROR):
                {
            case HapClientStackEvent.EVENT_TYPE_ON_PRESET_NAME_SET_ERROR -> {
                int status = stackEventStatusToProfileStatus(stackEvent.valueInt1);
                int groupId = stackEvent.valueInt3;

@@ -1015,17 +1003,11 @@ public class HapClientService extends ProfileService {
                    broadcastToClient(cb -> cb.onSetPresetNameForGroupFailed(groupId, status));
                }
            }
                break;

            case (HapClientStackEvent.EVENT_TYPE_ON_PRESET_INFO_ERROR):
                {
            case HapClientStackEvent.EVENT_TYPE_ON_PRESET_INFO_ERROR -> {
                // Used only to report back on hidden API calls used for testing.
                Log.d(TAG, stackEvent.toString());
            }
                break;

            default:
                return;
            default -> {}
        }
    }

@@ -1037,12 +1019,11 @@ public class HapClientService extends ProfileService {
            if (sm == null) {
                if (stackEvent.type == HapClientStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED) {
                    switch (stackEvent.valueInt1) {
                        case HapClientStackEvent.CONNECTION_STATE_CONNECTED:
                        case HapClientStackEvent.CONNECTION_STATE_CONNECTING:
                        case HapClientStackEvent.CONNECTION_STATE_CONNECTED,
                                HapClientStackEvent.CONNECTION_STATE_CONNECTING -> {
                            sm = getOrCreateStateMachine(device);
                            break;
                        default:
                            break;
                        }
                        default -> {}
                    }
                }
            }