@@ -10,6 +10,7 @@ import android.content.ServiceConnection
10
10
import android.graphics.drawable.Icon
11
11
import android.os.Build
12
12
import android.os.IBinder
13
+ import android.os.RemoteException
13
14
import android.service.quicksettings.Tile
14
15
import android.service.quicksettings.TileService
15
16
import androidx.annotation.RequiresApi
@@ -53,24 +54,35 @@ class TileActionService : TileService() {
53
54
serviceConnection = object : ServiceConnection {
54
55
override fun onServiceConnected (name : ComponentName ? , binder : IBinder ) {
55
56
XLog .d(this @TileActionService.getLog(" onServiceConnected" ))
56
- coroutineScope?.cancel()
57
- coroutineScope = CoroutineScope (Job () + Dispatchers .Main .immediate).apply {
58
- launch(CoroutineName (" TileActionService.ServiceMessageFlow" )) {
59
- (binder as ForegroundService .ForegroundServiceBinder ).serviceMessageFlow.onEach { serviceMessage ->
60
- XLog .d(this @TileActionService.getLog(" onServiceMessage" , " $serviceMessage " ))
61
- when (serviceMessage) {
62
- is ServiceMessage .ServiceState -> {
63
- isStreaming = serviceMessage.isStreaming; updateTile()
64
- }
65
- is ServiceMessage .FinishActivity -> {
66
- isStreaming = false ; updateTile()
57
+
58
+ try {
59
+ val foregroundServiceBinder = binder as ForegroundService .ForegroundServiceBinder
60
+
61
+ coroutineScope?.cancel()
62
+ coroutineScope = CoroutineScope (Job () + Dispatchers .Main .immediate)
63
+ coroutineScope!! .launch {
64
+ foregroundServiceBinder.serviceMessageFlow
65
+ .onEach { serviceMessage ->
66
+ XLog .d(this @TileActionService.getLog(" onServiceMessage" , " $serviceMessage " ))
67
+ when (serviceMessage) {
68
+ is ServiceMessage .ServiceState -> {
69
+ isStreaming = serviceMessage.isStreaming; updateTile()
70
+ }
71
+ is ServiceMessage .FinishActivity -> {
72
+ isStreaming = false ; updateTile()
73
+ }
74
+ else -> Unit
67
75
}
68
- else -> Unit
69
76
}
70
- }
71
- .catch { cause -> XLog .e(this @TileActionService.getLog(" onServiceMessage" ), cause) }
77
+ .catch { cause ->
78
+ XLog .e(this @TileActionService.getLog(" onServiceConnected.serviceMessageFlow: $cause " ))
79
+ XLog .e(this @TileActionService.getLog(" onServiceConnected.serviceMessageFlow" ), cause)
80
+ }
72
81
.collect()
73
82
}
83
+ } catch (cause: RemoteException ) {
84
+ XLog .e(this @TileActionService.getLog(" onServiceConnected" , " Failed to bind" ), cause)
85
+ return
74
86
}
75
87
isBound = true
76
88
IntentAction .GetServiceState .sendToAppService(this @TileActionService)
@@ -83,7 +95,8 @@ class TileActionService : TileService() {
83
95
isBound = false
84
96
}
85
97
}
86
- serviceConnection?.let { bindService(ForegroundService .getForegroundServiceIntent(this ), it, Context .BIND_AUTO_CREATE ) }
98
+
99
+ bindService(ForegroundService .getForegroundServiceIntent(this ), serviceConnection!! , Context .BIND_AUTO_CREATE )
87
100
} else {
88
101
isStreaming = false
89
102
updateTile()
0 commit comments