/
PhoneSettingsListener.kt
executable file
·95 lines (83 loc) · 3.67 KB
/
PhoneSettingsListener.kt
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
package io.homeassistant.companion.android.phone
import android.net.Uri
import android.util.Log
import com.google.android.gms.tasks.Tasks
import com.google.android.gms.wearable.DataClient
import com.google.android.gms.wearable.DataEvent
import com.google.android.gms.wearable.DataEventBuffer
import com.google.android.gms.wearable.DataMap
import com.google.android.gms.wearable.DataMapItem
import com.google.android.gms.wearable.MessageEvent
import com.google.android.gms.wearable.PutDataMapRequest
import com.google.android.gms.wearable.Wearable
import com.google.android.gms.wearable.WearableListenerService
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import kotlinx.coroutines.runBlocking
import javax.inject.Inject
@AndroidEntryPoint
class PhoneSettingsListener : WearableListenerService(), DataClient.OnDataChangedListener {
@Inject
lateinit var integrationUseCase: IntegrationRepository
companion object {
private const val TAG = "PhoneSettingsListener"
}
override fun onCreate() {
super.onCreate()
Log.d(TAG, "We have favorite message listener")
}
override fun onMessageReceived(event: MessageEvent) {
Log.d(TAG, "Message received: $event")
if (event.path == "/send_home_favorites") {
val nodeId = event.sourceNodeId
sendHomeFavorites(nodeId)
}
}
override fun onDataChanged(dataEvents: DataEventBuffer) {
Log.d(TAG, "onDataChanged ${dataEvents.count}")
dataEvents.forEach { event ->
if (event.type == DataEvent.TYPE_CHANGED) {
event.dataItem.also { item ->
if (item.uri.path?.compareTo("/save_home_favorites") == 0) {
val data = getHomeFavorites(DataMapItem.fromDataItem(item).dataMap)
Log.d(TAG, "onDataChanged: Received home favorites: $data")
saveFavorites()
}
}
}
}
}
private fun getHomeFavorites(map: DataMap): String {
map.apply {
return getString("favorites", "")
}
}
private fun sendHomeFavorites(nodeId: String) = runBlocking {
Log.d(TAG, "sendHomeFavorites to: $nodeId")
val currentFavorites = integrationUseCase.getWearHomeFavorites().toList()
val putDataRequest = PutDataMapRequest.create("/home_favorites").run {
dataMap.putString("favorites", currentFavorites.toString())
setUrgent()
asPutDataRequest()
}
Wearable.getDataClient(this@PhoneSettingsListener).putDataItem(putDataRequest).apply {
addOnSuccessListener { Log.d(TAG, "Successfully sent favorites to device") }
addOnFailureListener { Log.d(TAG, "Failed to send favorites to device") }
}
}
private fun saveFavorites() {
Log.d(TAG, "Finding existing favorites")
Tasks.await(Wearable.getDataClient(this).getDataItems(Uri.parse("wear://*/save_home_favorites"))).apply {
Log.d(TAG, "Found existing favorites: ${this.count}")
this.forEach {
val data = getHomeFavorites(DataMapItem.fromDataItem(this.first()).dataMap)
Log.d(TAG, "Favorites: ${data.removeSurrounding("[", "]").split(", ").map { it }}")
runBlocking {
integrationUseCase.setWearHomeFavorites(
data.removeSurrounding("[", "]").split(", ").map { it }.toSet()
)
}
}
}
}
}