Skip to content

Commit 6fcefd7

Browse files
committed
android(fix): do not crash if phone's MAC not available
also removed crossdevice code
1 parent 8ccfa1d commit 6fcefd7

File tree

1 file changed

+84
-50
lines changed

1 file changed

+84
-50
lines changed

android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsService.kt

Lines changed: 84 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ import me.kavishdevar.librepods.utils.AirPodsInstance
9393
import me.kavishdevar.librepods.utils.AirPodsModels
9494
import me.kavishdevar.librepods.utils.BLEManager
9595
import me.kavishdevar.librepods.utils.BluetoothConnectionManager
96-
import me.kavishdevar.librepods.utils.CrossDevice
97-
import me.kavishdevar.librepods.utils.CrossDevicePackets
96+
//import me.kavishdevar.librepods.utils.CrossDevice
97+
//import me.kavishdevar.librepods.utils.CrossDevicePackets
9898
import me.kavishdevar.librepods.utils.GestureDetector
9999
import me.kavishdevar.librepods.utils.HeadTracking
100100
import me.kavishdevar.librepods.utils.IslandType
@@ -193,7 +193,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
193193
var leftLongPressAction: StemAction = StemAction.defaultActions[StemPressType.LONG_PRESS]!!,
194194
var rightLongPressAction: StemAction = StemAction.defaultActions[StemPressType.LONG_PRESS]!!,
195195

196-
var cameraAction: AACPManager.Companion.StemPressType? = null,
196+
var cameraAction: StemPressType? = null,
197197

198198
// AirPods device information
199199
var airpodsName: String = "",
@@ -207,6 +207,9 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
207207
var airpodsVersion3: String = "",
208208
var airpodsHardwareRevision: String = "",
209209
var airpodsUpdaterIdentifier: String = "",
210+
211+
// phone's mac, needed for tipi
212+
var selfMacAddress: String = ""
210213
)
211214

212215
private lateinit var config: ServiceConfig
@@ -368,9 +371,29 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
368371

369372
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
370373

371-
val process = Runtime.getRuntime().exec(arrayOf("su", "-c", "settings", "get", "secure", "bluetooth_address"))
372-
val output = process.inputStream.bufferedReader().use { it.readLine() }
373-
localMac = output.trim()
374+
localMac = config.selfMacAddress
375+
if (localMac.isEmpty()) {
376+
localMac = try {
377+
val process = Runtime.getRuntime().exec(
378+
arrayOf("su", "-c", "settings get secure bluetooth_address")
379+
)
380+
381+
val exitCode = process.waitFor()
382+
383+
if (exitCode == 0) {
384+
process.inputStream.bufferedReader().use { it.readLine()?.trim().orEmpty() }
385+
} else {
386+
""
387+
}
388+
} catch (e: Exception) {
389+
Log.e(TAG, "Error retrieving local MAC address: ${e.message}. We probably aren't rooted.")
390+
""
391+
}
392+
config.selfMacAddress = localMac
393+
sharedPreferences.edit {
394+
putString("self_mac_address", localMac)
395+
}
396+
}
374397

375398
ServiceManager.setService(this)
376399
startForegroundNotification()
@@ -556,11 +579,11 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
556579
MODE_PRIVATE
557580
)
558581
)
559-
Log.d(TAG, "Initializing CrossDevice")
560-
CoroutineScope(Dispatchers.IO).launch {
561-
CrossDevice.init(this@AirPodsService)
562-
Log.d(TAG, "CrossDevice initialized")
563-
}
582+
// Log.d(TAG, "Initializing CrossDevice")
583+
// CoroutineScope(Dispatchers.IO).launch {
584+
// CrossDevice.init(this@AirPodsService)
585+
// Log.d(TAG, "CrossDevice initialized")
586+
// }
564587

565588
sharedPreferences = getSharedPreferences("settings", MODE_PRIVATE)
566589
macAddress = sharedPreferences.getString("mac_address", "") ?: ""
@@ -573,7 +596,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
573596
when (state) {
574597
TelephonyManager.CALL_STATE_RINGING -> {
575598
val leAvailableForAudio = bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true
576-
if ((CrossDevice.isAvailable && !isConnectedLocally && earDetectionNotification.status.contains(0x00)) || leAvailableForAudio) CoroutineScope(Dispatchers.IO).launch {
599+
// if ((CrossDevice.isAvailable && !isConnectedLocally && earDetectionNotification.status.contains(0x00)) || leAvailableForAudio) CoroutineScope(Dispatchers.IO).launch {
600+
if (leAvailableForAudio) runBlocking {
577601
takeOver("call")
578602
}
579603
if (config.headGestures) {
@@ -583,7 +607,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
583607
}
584608
TelephonyManager.CALL_STATE_OFFHOOK -> {
585609
val leAvailableForAudio = bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true
586-
if ((CrossDevice.isAvailable && !isConnectedLocally && earDetectionNotification.status.contains(0x00)) || leAvailableForAudio) CoroutineScope(
610+
// if ((CrossDevice.isAvailable && !isConnectedLocally && earDetectionNotification.status.contains(0x00)) || leAvailableForAudio) CoroutineScope(
611+
if (leAvailableForAudio) CoroutineScope(
587612
Dispatchers.IO).launch {
588613
takeOver("call")
589614
}
@@ -641,8 +666,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
641666
sharedPreferences.edit { putString("name", config.deviceName) }
642667
}
643668

644-
Log.d("AirPodsCrossDevice", CrossDevice.isAvailable.toString())
645-
if (!CrossDevice.isAvailable) {
669+
// Log.d("AirPodsCrossDevice", CrossDevice.isAvailable.toString())
670+
// if (!CrossDevice.isAvailable) {
646671
Log.d(TAG, "${config.deviceName} connected")
647672
CoroutineScope(Dispatchers.IO).launch {
648673
connectToSocket(device!!)
@@ -654,7 +679,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
654679
sharedPreferences.edit {
655680
putString("mac_address", macAddress)
656681
}
657-
}
682+
// }
683+
658684
} else if (intent?.action == AirPodsNotifications.AIRPODS_DISCONNECTED) {
659685
device = null
660686
isConnectedLocally = false
@@ -719,7 +745,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
719745
if (profile == BluetoothProfile.A2DP) {
720746
val connectedDevices = proxy.connectedDevices
721747
if (connectedDevices.isNotEmpty()) {
722-
if (!CrossDevice.isAvailable) {
748+
// if (!CrossDevice.isAvailable) {
723749
CoroutineScope(Dispatchers.IO).launch {
724750
connectToSocket(device)
725751
}
@@ -728,7 +754,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
728754
sharedPreferences.edit {
729755
putString("mac_address", macAddress)
730756
}
731-
}
757+
// }
732758
this@AirPodsService.sendBroadcast(
733759
Intent(AirPodsNotifications.AIRPODS_CONNECTED)
734760
)
@@ -745,9 +771,9 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
745771
}
746772
}
747773

748-
if (!isConnectedLocally && !CrossDevice.isAvailable) {
749-
clearPacketLogs()
750-
}
774+
// if (!isConnectedLocally && !CrossDevice.isAvailable) {
775+
// clearPacketLogs()
776+
// }
751777

752778
CoroutineScope(Dispatchers.IO).launch {
753779
bleManager.startScanning()
@@ -819,8 +845,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
819845
.getString("name", device?.name),
820846
batteryNotification.getBattery()
821847
)
822-
CrossDevice.sendRemotePacket(batteryInfo)
823-
CrossDevice.batteryBytes = batteryInfo
848+
// CrossDevice.sendRemotePacket(batteryInfo)
849+
// CrossDevice.batteryBytes = batteryInfo
824850

825851
for (battery in batteryNotification.getBattery()) {
826852
Log.d(
@@ -1229,7 +1255,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
12291255
leftLongPressAction = StemAction.fromString(sharedPreferences.getString("left_long_press_action", "CYCLE_NOISE_CONTROL_MODES") ?: "CYCLE_NOISE_CONTROL_MODES")!!,
12301256
rightLongPressAction = StemAction.fromString(sharedPreferences.getString("right_long_press_action", "DIGITAL_ASSISTANT") ?: "DIGITAL_ASSISTANT")!!,
12311257

1232-
cameraAction = sharedPreferences.getString("camera_action", null)?.let { AACPManager.Companion.StemPressType.valueOf(it) },
1258+
cameraAction = sharedPreferences.getString("camera_action", null)?.let { StemPressType.valueOf(it) },
12331259

12341260
// AirPods device information
12351261
airpodsName = sharedPreferences.getString("airpods_name", "") ?: "",
@@ -1243,6 +1269,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
12431269
airpodsVersion3 = sharedPreferences.getString("airpods_version3", "") ?: "",
12441270
airpodsHardwareRevision = sharedPreferences.getString("airpods_hardware_revision", "") ?: "",
12451271
airpodsUpdaterIdentifier = sharedPreferences.getString("airpods_updater_identifier", "") ?: "",
1272+
1273+
selfMacAddress = sharedPreferences.getString("self_mac_address", "") ?: ""
12461274
)
12471275
}
12481276

@@ -1251,6 +1279,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
12511279

12521280
when(key) {
12531281
"name" -> config.deviceName = preferences.getString(key, "AirPods") ?: "AirPods"
1282+
"mac_address" -> macAddress = preferences.getString(key, "") ?: ""
12541283
"automatic_ear_detection" -> config.earDetectionEnabled = preferences.getBoolean(key, true)
12551284
"conversational_awareness_pause_music" -> config.conversationalAwarenessPauseMusic = preferences.getBoolean(key, false)
12561285
"show_phone_battery_in_widget" -> {
@@ -1323,7 +1352,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
13231352
)!!
13241353
setupStemActions()
13251354
}
1326-
"camera_action" -> config.cameraAction = preferences.getString(key, null)?.let { AACPManager.Companion.StemPressType.valueOf(it) }
1355+
"camera_action" -> config.cameraAction = preferences.getString(key, null)?.let { StemPressType.valueOf(it) }
13271356

13281357
// AirPods device information
13291358
"airpods_name" -> config.airpodsName = preferences.getString(key, "") ?: ""
@@ -1337,10 +1366,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
13371366
"airpods_version3" -> config.airpodsVersion3 = preferences.getString(key, "") ?: ""
13381367
"airpods_hardware_revision" -> config.airpodsHardwareRevision = preferences.getString(key, "") ?: ""
13391368
"airpods_updater_identifier" -> config.airpodsUpdaterIdentifier = preferences.getString(key, "") ?: ""
1340-
}
13411369

1342-
if (key == "mac_address") {
1343-
macAddress = preferences.getString(key, "") ?: ""
1370+
"self_mac_address" -> config.selfMacAddress = preferences.getString(key, "") ?: ""
13441371
}
13451372
}
13461373

@@ -2096,7 +2123,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
20962123
SystemApisUtils.setMetadata(
20972124
device,
20982125
device.METADATA_COMPANION_APP,
2099-
"me.kavisdevar.librepods".toByteArray()
2126+
"me.kavishdevar.librepods".toByteArray()
21002127
) &&
21012128
SystemApisUtils.setMetadata(
21022129
device,
@@ -2266,14 +2293,19 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
22662293
return
22672294
}
22682295

2269-
if (CrossDevice.isAvailable) {
2270-
Log.d(TAG, "CrossDevice is available, continuing")
2271-
}
2272-
else if (bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true) {
2273-
Log.d(TAG, "At least one AirPod is in ear, continuing")
2274-
}
2275-
else {
2276-
Log.d(TAG, "CrossDevice not available and AirPods not in ear, skipping")
2296+
// if (CrossDevice.isAvailable) {
2297+
// Log.d(TAG, "CrossDevice is available, continuing")
2298+
// }
2299+
// else if (bleManager.getMostRecentStatus()?.isLeftInEar == true || bleManager.getMostRecentStatus()?.isRightInEar == true) {
2300+
// Log.d(TAG, "At least one AirPod is in ear, continuing")
2301+
// }
2302+
// else {
2303+
// Log.d(TAG, "CrossDevice not available and AirPods not in ear, skipping")
2304+
// return
2305+
// }
2306+
2307+
if (bleManager.getMostRecentStatus()?.isLeftInEar == false && bleManager.getMostRecentStatus()?.isRightInEar == false) {
2308+
Log.d(TAG, "Both AirPods are out of ear, not taking over audio")
22772309
return
22782310
}
22792311

@@ -2312,10 +2344,10 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
23122344
}
23132345

23142346
Log.d(TAG, "Taking over audio")
2315-
CrossDevice.sendRemotePacket(CrossDevicePackets.REQUEST_DISCONNECT.packet)
2347+
// CrossDevice.sendRemotePacket(CrossDevicePackets.REQUEST_DISCONNECT.packet)
23162348
Log.d(TAG, macAddress)
23172349

2318-
sharedPreferences.edit { putBoolean("CrossDeviceIsAvailable", false) }
2350+
// sharedPreferences.edit { putBoolean("CrossDeviceIsAvailable", false) }
23192351
device = getSystemService(BluetoothManager::class.java).adapter.bondedDevices.find {
23202352
it.address == macAddress
23212353
}
@@ -2340,7 +2372,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
23402372
showIsland(this, batteryNotification.getBattery().find { it.component == BatteryComponent.LEFT}?.level!!.coerceAtMost(batteryNotification.getBattery().find { it.component == BatteryComponent.RIGHT}?.level!!),
23412373
IslandType.TAKING_OVER)
23422374

2343-
CrossDevice.isAvailable = false
2375+
// CrossDevice.isAvailable = false
23442376
}
23452377

23462378
private fun createBluetoothSocket(device: BluetoothDevice, uuid: ParcelUuid): BluetoothSocket {
@@ -2385,7 +2417,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
23852417
Log.d(TAG, "<LogCollector:Start> Connecting to socket")
23862418
HiddenApiBypass.addHiddenApiExemptions("Landroid/bluetooth/BluetoothSocket;")
23872419
val uuid: ParcelUuid = ParcelUuid.fromString("74ec2172-0bad-4d01-8f77-997b2be0722a")
2388-
if (!isConnectedLocally && !CrossDevice.isAvailable) {
2420+
if (!isConnectedLocally) {
23892421
socket = try {
23902422
createBluetoothSocket(device, uuid)
23912423
} catch (e: Exception) {
@@ -2503,7 +2535,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
25032535
})
25042536
val bytes = buffer.copyOfRange(0, bytesRead)
25052537
val formattedHex = bytes.joinToString(" ") { "%02X".format(it) }
2506-
CrossDevice.sendReceivedPacket(bytes)
2538+
// CrossDevice.sendReceivedPacket(bytes)
25072539
updateNotificationContent(
25082540
true,
25092541
sharedPreferences.getString("name", device.name),
@@ -2541,6 +2573,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
25412573
this@AirPodsService.device = device
25422574
updateNotificationContent(false)
25432575
}
2576+
} else {
2577+
Log.d(TAG, "Already connected locally, skipping socket connection (isConnectedLocally = $isConnectedLocally, socket.isConnected = ${this::socket.isInitialized && socket.isConnected})")
25442578
}
25452579
}
25462580

@@ -2566,7 +2600,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
25662600
override fun onServiceDisconnected(profile: Int) {}
25672601
}, BluetoothProfile.A2DP)
25682602
isConnectedLocally = false
2569-
CrossDevice.isAvailable = true
2603+
// CrossDevice.isAvailable = true
25702604
}
25712605

25722606
fun disconnectAirPods() {
@@ -2611,16 +2645,16 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
26112645
}
26122646

26132647
fun getBattery(): List<Battery> {
2614-
if (!isConnectedLocally && CrossDevice.isAvailable) {
2615-
batteryNotification.setBattery(CrossDevice.batteryBytes)
2616-
}
2648+
// if (!isConnectedLocally && CrossDevice.isAvailable) {
2649+
// batteryNotification.setBattery(CrossDevice.batteryBytes)
2650+
// }
26172651
return batteryNotification.getBattery()
26182652
}
26192653

26202654
fun getANC(): Int {
2621-
if (!isConnectedLocally && CrossDevice.isAvailable) {
2622-
ancNotification.setStatus(CrossDevice.ancBytes)
2623-
}
2655+
// if (!isConnectedLocally && CrossDevice.isAvailable) {
2656+
// ancNotification.setStatus(CrossDevice.ancBytes)
2657+
// }
26242658
return ancNotification.status
26252659
}
26262660

@@ -2761,7 +2795,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
27612795
}
27622796
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
27632797
isConnectedLocally = false
2764-
CrossDevice.isAvailable = true
2798+
// CrossDevice.isAvailable = true
27652799
super.onDestroy()
27662800
}
27672801

0 commit comments

Comments
 (0)