-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWakeQuickTileService.kt
More file actions
104 lines (91 loc) · 3.32 KB
/
Copy pathWakeQuickTileService.kt
File metadata and controls
104 lines (91 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package dev.neikon.kineticwol.actions
import android.app.PendingIntent
import android.content.Intent
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import android.widget.Toast
import dev.neikon.kineticwol.KineticWolApplication
import dev.neikon.kineticwol.MainActivity
import dev.neikon.kineticwol.R
import dev.neikon.kineticwol.domain.model.WakeDevice
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
class WakeQuickTileService : TileService() {
private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
private val appContainer
get() = (application as KineticWolApplication).appContainer
override fun onStartListening() {
super.onStartListening()
serviceScope.launch {
refreshTile()
}
}
override fun onClick() {
super.onClick()
serviceScope.launch {
val devices = appContainer.deviceRepository.observeDevices().first()
when {
devices.isEmpty() -> openMainApp()
devices.size == 1 && !devices.single().remoteShutdown.isReady -> wakeDevice(devices.single())
else -> openDevicePicker()
}
}
}
override fun onDestroy() {
serviceScope.cancel()
super.onDestroy()
}
private suspend fun refreshTile() {
val tile = qsTile ?: return
val devices = appContainer.deviceRepository.observeDevices().first()
tile.label = getString(R.string.quick_tile_label)
tile.state = if (devices.isEmpty()) Tile.STATE_INACTIVE else Tile.STATE_ACTIVE
tile.subtitle = when {
devices.isEmpty() -> getString(R.string.quick_tile_subtitle_empty)
devices.size == 1 -> devices.single().name
else -> getString(R.string.quick_tile_subtitle_many, devices.size)
}
tile.updateTile()
}
private suspend fun wakeDevice(device: WakeDevice) {
runCatching {
appContainer.wakeOnLanSender.send(device)
}.onSuccess {
appContainer.deviceShortcutPublisher.reportUsed(device.id)
Toast.makeText(
this,
getString(R.string.manual_wake_success, device.name),
Toast.LENGTH_SHORT,
).show()
refreshTile()
}.onFailure {
Toast.makeText(
this,
getString(R.string.manual_wake_error, device.name),
Toast.LENGTH_SHORT,
).show()
}
}
private fun openMainApp() {
val pendingIntent = PendingIntent.getActivity(
this,
0,
Intent(this, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
startActivityAndCollapse(pendingIntent)
}
private fun openDevicePicker() {
val pendingIntent = PendingIntent.getActivity(
this,
1,
Intent(this, QuickTileDevicePickerActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
startActivityAndCollapse(pendingIntent)
}
}