Repository navigation
Expand file tree
/
Copy pathMyCustomControlService.kt
More file actions
253 lines (221 loc) · 8.77 KB
/
Copy pathMyCustomControlService.kt
File metadata and controls
253 lines (221 loc) · 8.77 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.example.snippet.views.devicecontrol
import android.app.Activity
import android.app.PendingIntent
import android.content.Intent
import android.service.controls.Control
import android.service.controls.ControlsProviderService
import android.service.controls.DeviceTypes
import android.service.controls.actions.BooleanAction
import android.service.controls.actions.ControlAction
import android.service.controls.actions.FloatAction
import android.service.controls.templates.ControlButton
import android.service.controls.templates.ControlTemplate
import android.service.controls.templates.RangeTemplate
import android.service.controls.templates.ToggleTemplate
import java.util.Locale
import java.util.concurrent.Flow
import java.util.function.Consumer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.jdk9.asPublisher
import kotlinx.coroutines.jdk9.flowPublish
import kotlinx.coroutines.launch
private const val EXTRA_MESSAGE = "com.example.devicecontrol.EXTRA_MESSAGE"
private class MainActivity : Activity()
private object DeclareServiceSnippet {
// [START android_views_device_control_declare_service]
class MyCustomControlService : ControlsProviderService() {
// [START_EXCLUDE]
override fun createPublisherForAllAvailable(): Flow.Publisher<Control> = TODO()
override fun createPublisherFor(controlIds: List<String>): Flow.Publisher<Control> = TODO()
override fun performControlAction(
controlId: String,
action: ControlAction,
consumer: Consumer<Int>,
) = TODO()
// [END_EXCLUDE]
}
// [END android_views_device_control_declare_service]
}
private object DeclareConstantsSnippet {
// [START android_views_device_control_declare_constants]
private const val LIGHT_ID = 1234
private const val LIGHT_TITLE = "My fancy light"
private const val LIGHT_TYPE = DeviceTypes.TYPE_LIGHT
private const val THERMOSTAT_ID = 5678
private const val THERMOSTAT_TITLE = "My fancy thermostat"
private const val THERMOSTAT_TYPE = DeviceTypes.TYPE_THERMOSTAT
class MyCustomControlService : ControlsProviderService() {
// [START_EXCLUDE]
override fun createPublisherForAllAvailable(): Flow.Publisher<Control> = TODO()
override fun createPublisherFor(controlIds: List<String>): Flow.Publisher<Control> = TODO()
override fun performControlAction(
controlId: String,
action: ControlAction,
consumer: Consumer<Int>,
) = TODO()
// [END_EXCLUDE]
}
// [END android_views_device_control_declare_constants]
}
@OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class)
private object CreatePublisherAllAvailableSnippet {
// [START android_views_device_control_create_publisher_all_available]
class MyCustomControlService : ControlsProviderService() {
override fun createPublisherForAllAvailable(): Flow.Publisher<Control> =
flowPublish {
send(createStatelessControl(LIGHT_ID, LIGHT_TITLE, LIGHT_TYPE))
send(createStatelessControl(THERMOSTAT_ID, THERMOSTAT_TITLE, THERMOSTAT_TYPE))
}
private fun createStatelessControl(id: Int, title: String, type: Int): Control {
val intent = Intent(this, MainActivity::class.java)
.putExtra(EXTRA_MESSAGE, title)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val action = PendingIntent.getActivity(
this,
id,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
return Control.StatelessBuilder(id.toString(), action)
.setTitle(title)
.setDeviceType(type)
.build()
}
override fun createPublisherFor(controlIds: List<String>): Flow.Publisher<Control> {
TODO()
}
override fun performControlAction(
controlId: String,
action: ControlAction,
consumer: Consumer<Int>,
) {
TODO()
}
}
// [END android_views_device_control_create_publisher_all_available]
}
private const val LIGHT_ID = 1234
private const val LIGHT_TITLE = "My fancy light"
private const val LIGHT_TYPE = DeviceTypes.TYPE_LIGHT
private const val THERMOSTAT_ID = 5678
private const val THERMOSTAT_TITLE = "My fancy thermostat"
private const val THERMOSTAT_TYPE = DeviceTypes.TYPE_THERMOSTAT
class MyCustomControlService : ControlsProviderService() {
override fun createPublisherForAllAvailable(): Flow.Publisher<Control> = TODO()
// [START android_views_device_control_create_publisher_for]
private val job = SupervisorJob()
private val scope = CoroutineScope(Dispatchers.IO + job)
private val controlFlows = mutableMapOf<String, MutableSharedFlow<Control>>()
private var toggleState = false
private var rangeState = 18f
override fun createPublisherFor(controlIds: List<String>): Flow.Publisher<Control> {
val flow = MutableSharedFlow<Control>(replay = 2, extraBufferCapacity = 2)
controlIds.forEach { controlFlows[it] = flow }
scope.launch {
delay(1000) // Retrieving the toggle state.
flow.tryEmit(createLight())
delay(1000) // Retrieving the range state.
flow.tryEmit(createThermostat())
}
return flow.asPublisher()
}
private fun createLight() = createStatefulControl(
LIGHT_ID,
LIGHT_TITLE,
LIGHT_TYPE,
toggleState,
ToggleTemplate(
LIGHT_ID.toString(),
ControlButton(
toggleState,
toggleState.toString().uppercase(Locale.getDefault()),
),
),
)
private fun createThermostat() = createStatefulControl(
THERMOSTAT_ID,
THERMOSTAT_TITLE,
THERMOSTAT_TYPE,
rangeState,
RangeTemplate(
THERMOSTAT_ID.toString(),
15f,
25f,
rangeState,
0.1f,
"%1.1f",
),
)
private fun <T> createStatefulControl(
id: Int,
title: String,
type: Int,
state: T,
template: ControlTemplate,
): Control {
val intent = Intent(this, MainActivity::class.java)
.putExtra(EXTRA_MESSAGE, "$title $state")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val action = PendingIntent.getActivity(
this,
id,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
return Control.StatefulBuilder(id.toString(), action)
.setTitle(title)
.setDeviceType(type)
.setStatus(Control.STATUS_OK)
.setControlTemplate(template)
.build()
}
override fun onDestroy() {
super.onDestroy()
job.cancel()
}
// [END android_views_device_control_create_publisher_for]
// [START android_views_device_control_perform_control_action]
override fun performControlAction(
controlId: String,
action: ControlAction,
consumer: Consumer<Int>,
) {
controlFlows[controlId]?.let { flow ->
when (controlId) {
LIGHT_ID.toString() -> {
consumer.accept(ControlAction.RESPONSE_OK)
if (action is BooleanAction) toggleState = action.newState
flow.tryEmit(createLight())
}
THERMOSTAT_ID.toString() -> {
consumer.accept(ControlAction.RESPONSE_OK)
if (action is FloatAction) rangeState = action.newValue
flow.tryEmit(createThermostat())
}
else -> consumer.accept(ControlAction.RESPONSE_FAIL)
}
} ?: consumer.accept(ControlAction.RESPONSE_FAIL)
}
// [END android_views_device_control_perform_control_action]
}