mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-02-10 19:52:24 +00:00
android: listen for airpods connection in UI (fix #118)
This commit is contained in:
@@ -102,6 +102,7 @@ import me.kavishdevar.librepods.utils.AirPodsNotifications
|
|||||||
@Composable
|
@Composable
|
||||||
fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService,
|
fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService,
|
||||||
navController: NavController, isConnected: Boolean, isRemotelyConnected: Boolean) {
|
navController: NavController, isConnected: Boolean, isRemotelyConnected: Boolean) {
|
||||||
|
var isLocallyConnected by remember { mutableStateOf(isConnected) }
|
||||||
var isRemotelyConnected by remember { mutableStateOf(isRemotelyConnected) }
|
var isRemotelyConnected by remember { mutableStateOf(isRemotelyConnected) }
|
||||||
val sharedPreferences = LocalContext.current.getSharedPreferences("settings", MODE_PRIVATE)
|
val sharedPreferences = LocalContext.current.getSharedPreferences("settings", MODE_PRIVATE)
|
||||||
var device by remember { mutableStateOf(dev) }
|
var device by remember { mutableStateOf(dev) }
|
||||||
@@ -113,6 +114,10 @@ fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService,
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(service) {
|
||||||
|
isLocallyConnected = service.isConnectedLocally
|
||||||
|
}
|
||||||
|
|
||||||
val nameChangeListener = remember {
|
val nameChangeListener = remember {
|
||||||
SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
|
SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
|
||||||
if (key == "name") {
|
if (key == "name") {
|
||||||
@@ -144,22 +149,37 @@ fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService,
|
|||||||
}
|
}
|
||||||
|
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val bluetoothReceiver = remember {
|
|
||||||
|
val connectionReceiver = remember {
|
||||||
object : BroadcastReceiver() {
|
object : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
if (intent?.action == "me.kavishdevar.librepods.AIRPODS_CONNECTED_REMOTELY") {
|
when (intent?.action) {
|
||||||
coroutineScope.launch {
|
"me.kavishdevar.librepods.AIRPODS_CONNECTED_REMOTELY" -> {
|
||||||
handleRemoteConnection(true)
|
coroutineScope.launch {
|
||||||
|
handleRemoteConnection(true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (intent?.action == "me.kavishdevar.librepods.AIRPODS_DISCONNECTED_REMOTELY") {
|
"me.kavishdevar.librepods.AIRPODS_DISCONNECTED_REMOTELY" -> {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
handleRemoteConnection(false)
|
handleRemoteConnection(false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (intent?.action == AirPodsNotifications.DISCONNECT_RECEIVERS) {
|
AirPodsNotifications.AIRPODS_CONNECTED -> {
|
||||||
try {
|
coroutineScope.launch {
|
||||||
context?.unregisterReceiver(this)
|
isLocallyConnected = true
|
||||||
} catch (e: IllegalArgumentException) {
|
}
|
||||||
e.printStackTrace()
|
}
|
||||||
|
AirPodsNotifications.AIRPODS_DISCONNECTED -> {
|
||||||
|
coroutineScope.launch {
|
||||||
|
isLocallyConnected = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AirPodsNotifications.DISCONNECT_RECEIVERS -> {
|
||||||
|
try {
|
||||||
|
context?.unregisterReceiver(this)
|
||||||
|
} catch (e: IllegalArgumentException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -170,16 +190,22 @@ fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService,
|
|||||||
val filter = IntentFilter().apply {
|
val filter = IntentFilter().apply {
|
||||||
addAction("me.kavishdevar.librepods.AIRPODS_CONNECTED_REMOTELY")
|
addAction("me.kavishdevar.librepods.AIRPODS_CONNECTED_REMOTELY")
|
||||||
addAction("me.kavishdevar.librepods.AIRPODS_DISCONNECTED_REMOTELY")
|
addAction("me.kavishdevar.librepods.AIRPODS_DISCONNECTED_REMOTELY")
|
||||||
|
addAction(AirPodsNotifications.AIRPODS_CONNECTED)
|
||||||
|
addAction(AirPodsNotifications.AIRPODS_DISCONNECTED)
|
||||||
addAction(AirPodsNotifications.DISCONNECT_RECEIVERS)
|
addAction(AirPodsNotifications.DISCONNECT_RECEIVERS)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
context.registerReceiver(bluetoothReceiver, filter, RECEIVER_EXPORTED)
|
context.registerReceiver(connectionReceiver, filter, RECEIVER_EXPORTED)
|
||||||
} else {
|
} else {
|
||||||
context.registerReceiver(bluetoothReceiver, filter)
|
context.registerReceiver(connectionReceiver, filter)
|
||||||
}
|
}
|
||||||
onDispose {
|
onDispose {
|
||||||
context.unregisterReceiver(bluetoothReceiver)
|
try {
|
||||||
|
context.unregisterReceiver(connectionReceiver)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,7 +292,7 @@ fun AirPodsSettingsScreen(dev: BluetoothDevice?, service: AirPodsService,
|
|||||||
},
|
},
|
||||||
snackbarHost = { SnackbarHost(snackbarHostState) }
|
snackbarHost = { SnackbarHost(snackbarHostState) }
|
||||||
) { paddingValues ->
|
) { paddingValues ->
|
||||||
if (isConnected == true || isRemotelyConnected == true) {
|
if (isLocallyConnected || isRemotelyConnected) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.haze(hazeState)
|
.haze(hazeState)
|
||||||
|
|||||||
Reference in New Issue
Block a user