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

Commit b87c7f16 authored by chelseahao's avatar chelseahao Committed by Chelsea Hao
Browse files

Introduce a function to derive a more meaningful state for the UI from...

Introduce a function to derive a more meaningful state for the UI from `BluetoothLeBroadcastReceiveState`.

Test: atest
Bug: 308368124
Flag: com.android.settingslib.flags.enable_le_audio_sharing
Change-Id: I08be93310f6e5bc0624259879239a8f8a6b40caf
parent bc454980
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -52,6 +52,14 @@ import java.util.concurrent.Executors;
 * BluetoothLeBroadcastAssistant.Callback} to get the result callback.
 */
public class LocalBluetoothLeBroadcastAssistant implements LocalBluetoothProfile {
    /** A derived source state based on {@link BluetoothLeBroadcastReceiveState}. */
    public enum LocalBluetoothLeBroadcastSourceState {
        UNKNOWN,
        STREAMING,
        DECRYPTION_FAILED,
        PAUSED,
    }

    private static final String TAG = "LocalBluetoothLeBroadcastAssistant";
    private static final int UNKNOWN_VALUE_PLACEHOLDER = -1;
    private static final boolean DEBUG = BluetoothUtils.D;
@@ -59,6 +67,13 @@ public class LocalBluetoothLeBroadcastAssistant implements LocalBluetoothProfile
    static final String NAME = "LE_AUDIO_BROADCAST_ASSISTANT";
    // Order of this profile in device profiles list
    private static final int ORDINAL = 1;
    // Referring to Broadcast Audio Scan Service 1.0
    // Table 3.9: Broadcast Receive State characteristic format
    // 0x00000000: 0b0 = Not synchronized to BIS_index[x]
    // 0xFFFFFFFF: Failed to sync to BIG
    private static final long BIS_SYNC_NOT_SYNC_TO_BIS = 0x00000000L;
    private static final long BIS_SYNC_FAILED_SYNC_TO_BIG = 0xFFFFFFFFL;
    private static final String EMPTY_DEVICE_ADDRESS = "00:00:00:00:00:00";

    private LocalBluetoothProfileManager mProfileManager;
    private BluetoothLeBroadcastAssistant mService;
@@ -558,4 +573,30 @@ public class LocalBluetoothLeBroadcastAssistant implements LocalBluetoothProfile
            }
        }
    }

    /** Checks the source connection status based on the provided broadcast receive state. */
    public static LocalBluetoothLeBroadcastSourceState getLocalSourceState(
            BluetoothLeBroadcastReceiveState state) {
        // Source is actively streaming
        if (state.getBisSyncState().stream()
                .anyMatch(
                        bitmap ->
                                (bitmap != BIS_SYNC_NOT_SYNC_TO_BIS
                                        && bitmap != BIS_SYNC_FAILED_SYNC_TO_BIG))) {
            return LocalBluetoothLeBroadcastSourceState.STREAMING;
        }
        // Wrong password is used for the source
        if (state.getPaSyncState() == BluetoothLeBroadcastReceiveState.PA_SYNC_STATE_SYNCHRONIZED
                && state.getBigEncryptionState()
                == BluetoothLeBroadcastReceiveState.BIG_ENCRYPTION_STATE_BAD_CODE) {
            return LocalBluetoothLeBroadcastSourceState.DECRYPTION_FAILED;
        }
        // Source in hysteresis mode
        if (!state.getSourceDevice().getAddress().equals(EMPTY_DEVICE_ADDRESS)) {
            // Referring to Broadcast Audio Scan Service 1.0
            // All zero address means no source on the sink device
            return LocalBluetoothLeBroadcastSourceState.PAUSED;
        }
        return LocalBluetoothLeBroadcastSourceState.UNKNOWN;
    }
}