1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settingslib.bluetooth 18 19 import android.bluetooth.BluetoothLeBroadcast 20 import android.bluetooth.BluetoothLeBroadcastMetadata 21 import com.android.internal.util.ConcurrentUtils 22 import kotlinx.coroutines.channels.Channel 23 import kotlinx.coroutines.channels.awaitClose 24 import kotlinx.coroutines.flow.Flow 25 import kotlinx.coroutines.flow.buffer 26 import kotlinx.coroutines.flow.callbackFlow 27 import kotlinx.coroutines.launch 28 29 /** [Flow] for [BluetoothLeBroadcast.Callback] source start/stop events */ 30 val LocalBluetoothLeBroadcast.onBroadcastStartedOrStopped: Flow<Unit> 31 get() = <lambda>null32 callbackFlow { 33 val listener = 34 object : BluetoothLeBroadcast.Callback { 35 override fun onBroadcastStarted(reason: Int, broadcastId: Int) { 36 launch { trySend(Unit) } 37 } 38 39 override fun onBroadcastStartFailed(reason: Int) { 40 launch { trySend(Unit) } 41 } 42 43 override fun onBroadcastStopped(reason: Int, broadcastId: Int) { 44 launch { trySend(Unit) } 45 } 46 47 override fun onBroadcastStopFailed(reason: Int) { 48 launch { trySend(Unit) } 49 } 50 51 override fun onPlaybackStarted(reason: Int, broadcastId: Int) {} 52 53 override fun onPlaybackStopped(reason: Int, broadcastId: Int) {} 54 55 override fun onBroadcastUpdated(reason: Int, broadcastId: Int) {} 56 57 override fun onBroadcastUpdateFailed(reason: Int, broadcastId: Int) {} 58 59 override fun onBroadcastMetadataChanged( 60 broadcastId: Int, 61 metadata: BluetoothLeBroadcastMetadata 62 ) {} 63 } 64 registerServiceCallBack( 65 ConcurrentUtils.DIRECT_EXECUTOR, 66 listener, 67 ) 68 awaitClose { unregisterServiceCallBack(listener) } 69 } 70 .buffer(capacity = Channel.CONFLATED) 71 72 /** [Flow] for [BluetoothLeBroadcast.Callback] onBroadcastMetadataChanged event */ 73 val LocalBluetoothLeBroadcast.onBroadcastMetadataChanged: Flow<Unit> 74 get() = <lambda>null75 callbackFlow { 76 val listener = 77 object : BluetoothLeBroadcast.Callback { 78 override fun onBroadcastStarted(reason: Int, broadcastId: Int) {} 79 80 override fun onBroadcastStartFailed(reason: Int) { 81 } 82 83 override fun onBroadcastStopped(reason: Int, broadcastId: Int) { 84 } 85 86 override fun onBroadcastStopFailed(reason: Int) { 87 } 88 89 override fun onPlaybackStarted(reason: Int, broadcastId: Int) { 90 } 91 92 override fun onPlaybackStopped(reason: Int, broadcastId: Int) { 93 } 94 95 override fun onBroadcastUpdated(reason: Int, broadcastId: Int) {} 96 97 override fun onBroadcastUpdateFailed(reason: Int, broadcastId: Int) {} 98 99 override fun onBroadcastMetadataChanged( 100 broadcastId: Int, 101 metadata: BluetoothLeBroadcastMetadata 102 ) { 103 trySend(Unit) 104 } 105 } 106 registerServiceCallBack( 107 ConcurrentUtils.DIRECT_EXECUTOR, 108 listener, 109 ) 110 awaitClose { unregisterServiceCallBack(listener) } 111 } 112 .buffer(capacity = Channel.CONFLATED) 113