android: start head gestures after auto-connect

This commit is contained in:
Kavish Devar
2025-05-30 17:30:30 +05:30
parent 96e63cf35e
commit 445c999208

View File

@@ -183,6 +183,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
private val maxLogEntries = 1000 private val maxLogEntries = 1000
private val inMemoryLogs = mutableSetOf<String>() private val inMemoryLogs = mutableSetOf<String>()
private var handleIncomingCallOnceConnected = false
lateinit var bleManager: BLEManager lateinit var bleManager: BLEManager
private val bleStatusListener = object : BLEManager.AirPodsStatusListener { private val bleStatusListener = object : BLEManager.AirPodsStatusListener {
@SuppressLint("NewApi") @SuppressLint("NewApi")
@@ -197,7 +199,24 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
"mac_address", "") ?: "") "mac_address", "") ?: "")
connectToSocket(bluetoothDevice) connectToSocket(bluetoothDevice)
} }
Log.d("AirPodsBLEService", "Device status changed, inEar: ${device.isLeftInEar}, ${device.isRightInEar}") Log.d("AirPodsBLEService", "Device status changed")
if (isConnectedLocally) return
val leftLevel = bleManager.getMostRecentStatus()?.leftBattery?: 0
val rightLevel = bleManager.getMostRecentStatus()?.rightBattery?: 0
val caseLevel = bleManager.getMostRecentStatus()?.caseBattery?: 0
val leftCharging = bleManager.getMostRecentStatus()?.isLeftCharging
val rightCharging = bleManager.getMostRecentStatus()?.isRightCharging
val caseCharging = bleManager.getMostRecentStatus()?.isCaseCharging
batteryNotification.setBatteryDirect(
leftLevel = leftLevel,
leftCharging = leftCharging == true,
rightLevel = rightLevel,
rightCharging = rightCharging == true,
caseLevel = caseLevel,
caseCharging = caseCharging == true
)
updateBattery()
} }
override fun onBroadcastFromNewAddress(device: BLEManager.AirPodsStatus) { override fun onBroadcastFromNewAddress(device: BLEManager.AirPodsStatus) {
@@ -213,7 +232,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
this@AirPodsService, this@AirPodsService,
getSharedPreferences("settings", MODE_PRIVATE).getString("name", "AirPods Pro") ?: "AirPods" getSharedPreferences("settings", MODE_PRIVATE).getString("name", "AirPods Pro") ?: "AirPods"
) )
if (isConnectedLocally) return if (isConnectedLocally) return
val leftLevel = bleManager.getMostRecentStatus()?.leftBattery?: 0 val leftLevel = bleManager.getMostRecentStatus()?.leftBattery?: 0
val rightLevel = bleManager.getMostRecentStatus()?.rightBattery?: 0 val rightLevel = bleManager.getMostRecentStatus()?.rightBattery?: 0
val caseLevel = bleManager.getMostRecentStatus()?.caseBattery?: 0 val caseLevel = bleManager.getMostRecentStatus()?.caseBattery?: 0
@@ -1049,11 +1068,14 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
if (isInCall) return if (isInCall) return
if (config.headGestures) { if (config.headGestures) {
initGestureDetector() initGestureDetector()
aacpManager.sendStartHeadTracking()
gestureDetector?.startDetection { accepted -> gestureDetector?.startDetection { accepted ->
if (accepted) { if (accepted) {
answerCall() answerCall()
handleIncomingCallOnceConnected = false
} else { } else {
rejectCall() rejectCall()
handleIncomingCallOnceConnected = false
} }
} }
} }
@@ -1352,6 +1374,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
val ancModeFilter = IntentFilter("me.kavishdevar.librepods.SET_ANC_MODE") val ancModeFilter = IntentFilter("me.kavishdevar.librepods.SET_ANC_MODE")
var ancModeReceiver: BroadcastReceiver? = null var ancModeReceiver: BroadcastReceiver? = null
@SuppressLint("InlinedApi", "MissingPermission", "UnspecifiedRegisterReceiverFlag") @SuppressLint("InlinedApi", "MissingPermission", "UnspecifiedRegisterReceiverFlag")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("AirPodsService", "Service started") Log.d("AirPodsService", "Service started")
@@ -1683,7 +1707,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
if (CrossDevice.isAvailable) { if (CrossDevice.isAvailable) {
Log.d("AirPodsService", "CrossDevice is available, continuing") Log.d("AirPodsService", "CrossDevice is available, continuing")
} }
else if (bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true) { else if (bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true) {
Log.d("AirPodsService", "At least one AirPod is in ear, continuing") Log.d("AirPodsService", "At least one AirPod is in ear, continuing")
} }
@@ -1722,6 +1746,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
Log.d("AirPodsService", "Pausing music so that it doesn't play through speakers") Log.d("AirPodsService", "Pausing music so that it doesn't play through speakers")
MediaController.pausedForCrossDevice = true MediaController.pausedForCrossDevice = true
MediaController.sendPause(true) MediaController.sendPause(true)
} else {
handleIncomingCallOnceConnected = true
} }
Log.d("AirPodsService", "Taking over audio") Log.d("AirPodsService", "Taking over audio")
@@ -1839,13 +1865,13 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
aacpManager.sendNotificationRequest() aacpManager.sendNotificationRequest()
delay(200) delay(200)
aacpManager.sendRequestProximityKeys((AACPManager.Companion.ProximityKeyType.IRK.value+AACPManager.Companion.ProximityKeyType.ENC_KEY.value).toByte()) aacpManager.sendRequestProximityKeys((AACPManager.Companion.ProximityKeyType.IRK.value+AACPManager.Companion.ProximityKeyType.ENC_KEY.value).toByte())
startHeadTracking() if (!handleIncomingCallOnceConnected) startHeadTracking() else handleIncomingCall()
Handler(Looper.getMainLooper()).postDelayed({ Handler(Looper.getMainLooper()).postDelayed({
aacpManager.sendPacket(aacpManager.createHandshakePacket()) aacpManager.sendPacket(aacpManager.createHandshakePacket())
aacpManager.sendSetFeatureFlagsPacket() aacpManager.sendSetFeatureFlagsPacket()
aacpManager.sendNotificationRequest() aacpManager.sendNotificationRequest()
aacpManager.sendRequestProximityKeys(AACPManager.Companion.ProximityKeyType.IRK.value) aacpManager.sendRequestProximityKeys(AACPManager.Companion.ProximityKeyType.IRK.value)
stopHeadTracking() if (!handleIncomingCallOnceConnected) stopHeadTracking()
}, 5000) }, 5000)
sendBroadcast( sendBroadcast(