fix: when both airpods are not worn, the settings screen would say not connected

This commit is contained in:
Kavish Devar
2025-01-06 22:22:15 +05:30
parent 140ef0869b
commit 7ed8f9b09c

View File

@@ -40,12 +40,14 @@ import android.os.IBinder
import android.os.ParcelUuid import android.os.ParcelUuid
import android.util.Log import android.util.Log
import android.widget.RemoteViews import android.widget.RemoteViews
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import me.kavishdevar.aln.BatteryWidget import me.kavishdevar.aln.BatteryWidget
import me.kavishdevar.aln.MainActivity
import me.kavishdevar.aln.R import me.kavishdevar.aln.R
import me.kavishdevar.aln.utils.AirPodsNotifications import me.kavishdevar.aln.utils.AirPodsNotifications
import me.kavishdevar.aln.utils.Battery import me.kavishdevar.aln.utils.Battery
@@ -67,10 +69,18 @@ object ServiceManager {
fun setService(service: AirPodsService?) { fun setService(service: AirPodsService?) {
this.service = service this.service = service
} }
@OptIn(ExperimentalMaterial3Api::class)
@Synchronized @Synchronized
fun restartService(context: Context) { fun restartService(context: Context) {
service?.stopSelf() service?.stopSelf()
context.startService(Intent(context, AirPodsService::class.java)) Log.d("ServiceManager", "Restarting service, service is null: ${service == null}")
val intent = Intent(context, AirPodsService::class.java)
context.stopService(intent)
CoroutineScope(Dispatchers.IO).launch {
delay(1000)
context.startService(intent)
context.startActivity(Intent(context, MainActivity::class.java))
}
} }
} }
@@ -119,13 +129,6 @@ class AirPodsService: Service() {
context?.sendBroadcast(intent) context?.sendBroadcast(intent)
} }
} }
if (BluetoothDevice.ACTION_ACL_DISCONNECTED == action
|| BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED == action
) {
context?.sendBroadcast(
Intent(AirPodsNotifications.Companion.AIRPODS_DISCONNECTED)
)
}
} }
} }
} }
@@ -256,8 +259,6 @@ class AirPodsService: Service() {
// .setOngoing(true) // .setOngoing(true)
// .build() // .build()
// Instead have something like L: 50% | R: 50% | C: 12% with the emojis, without using remote views, in thte title itself.
updatedNotification = NotificationCompat.Builder(this, "background_service_status") updatedNotification = NotificationCompat.Builder(this, "background_service_status")
.setSmallIcon(R.drawable.airpods) .setSmallIcon(R.drawable.airpods)
.setContentTitle("""L: ${batteryList?.find { it.component == BatteryComponent.LEFT }?.let { .setContentTitle("""L: ${batteryList?.find { it.component == BatteryComponent.LEFT }?.let {
@@ -474,7 +475,6 @@ class AirPodsService: Service() {
Log.d("AirPods Data", "Data received: $formattedHex") Log.d("AirPods Data", "Data received: $formattedHex")
} else if (bytesRead == -1) { } else if (bytesRead == -1) {
Log.d("AirPods Service", "Socket closed (bytesRead = -1)") Log.d("AirPods Service", "Socket closed (bytesRead = -1)")
// socket.close()
sendBroadcast(Intent(AirPodsNotifications.Companion.AIRPODS_DISCONNECTED)) sendBroadcast(Intent(AirPodsNotifications.Companion.AIRPODS_DISCONNECTED))
return@launch return@launch
} }