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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* Copyright (C) 2019 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
*
* http://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.android.systemui.controls.controller
import android.content.ComponentName
import android.content.Context
import android.os.IBinder
import android.os.UserHandle
import android.service.controls.Control
import android.service.controls.IControlsActionCallback
import android.service.controls.IControlsSubscriber
import android.service.controls.IControlsSubscription
import android.service.controls.actions.ControlAction
import android.util.Log
import com.android.internal.annotations.VisibleForTesting
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.settings.UserTracker
import com.android.systemui.util.concurrency.DelayableExecutor
import dagger.Lazy
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject
@SysUISingleton
@VisibleForTesting
open class ControlsBindingControllerImpl @Inject constructor(
private val context: Context,
@Background private val backgroundExecutor: DelayableExecutor,
private val lazyController: Lazy<ControlsController>,
userTracker: UserTracker
) : ControlsBindingController {
companion object {
private const val TAG = "ControlsBindingControllerImpl"
private const val MAX_CONTROLS_REQUEST = 100000L
private const val SUGGESTED_STRUCTURES = 6L
private const val SUGGESTED_CONTROLS_REQUEST =
ControlsControllerImpl.SUGGESTED_CONTROLS_PER_STRUCTURE * SUGGESTED_STRUCTURES
private val emptyCallback = object : ControlsBindingController.LoadCallback {
override fun accept(controls: List<Control>) {}
override fun error(message: String) {}
}
}
private var currentUser = userTracker.userHandle
override val currentUserId: Int
get() = currentUser.identifier
private var currentProvider: ControlsProviderLifecycleManager? = null
/*
* Will track any active subscriber for subscribe/unsubscribe requests coming into
* this controller. Only one can be active at any time
*/
private var statefulControlSubscriber: StatefulControlSubscriber? = null
/*
* Will track any active load subscriber. Only one can be active at any time.
*/
private var loadSubscriber: LoadSubscriber? = null
private val actionCallbackService = object : IControlsActionCallback.Stub() {
override fun accept(
token: IBinder,
controlId: String,
@ControlAction.ResponseResult response: Int
) {
backgroundExecutor.execute(OnActionResponseRunnable(token, controlId, response))
}
}
@VisibleForTesting
internal open fun createProviderManager(component: ComponentName):
ControlsProviderLifecycleManager {
return ControlsProviderLifecycleManager(
context,
backgroundExecutor,
actionCallbackService,
currentUser,
component
)
}
private fun retrieveLifecycleManager(component: ComponentName):
ControlsProviderLifecycleManager {
if (currentProvider != null && currentProvider?.componentName != component) {
unbind()
}
val provider = currentProvider ?: createProviderManager(component)
currentProvider = provider
return provider
}
override fun bindAndLoad(
component: ComponentName,
callback: ControlsBindingController.LoadCallback
): Runnable {
loadSubscriber?.loadCancel()
val ls = LoadSubscriber(callback, MAX_CONTROLS_REQUEST)
loadSubscriber = ls
retrieveLifecycleManager(component).maybeBindAndLoad(ls)
return ls.loadCancel()
}
override fun bindAndLoadSuggested(
component: ComponentName,
callback: ControlsBindingController.LoadCallback
) {
loadSubscriber?.loadCancel()
val ls = LoadSubscriber(callback, SUGGESTED_CONTROLS_REQUEST)
loadSubscriber = ls
retrieveLifecycleManager(component).maybeBindAndLoadSuggested(ls)
}
override fun subscribe(structureInfo: StructureInfo) {
// make sure this has happened. only allow one active subscription
unsubscribe()
val provider = retrieveLifecycleManager(structureInfo.componentName)
val scs = StatefulControlSubscriber(
lazyController.get(),
provider,
backgroundExecutor,
MAX_CONTROLS_REQUEST
)
statefulControlSubscriber = scs
provider.maybeBindAndSubscribe(structureInfo.controls.map { it.controlId }, scs)
}
override fun unsubscribe() {
statefulControlSubscriber?.cancel()
statefulControlSubscriber = null
}
override fun action(
componentName: ComponentName,
controlInfo: ControlInfo,
action: ControlAction
) {
if (statefulControlSubscriber == null) {
Log.w(TAG, "No actions can occur outside of an active subscription. Ignoring.")
} else {
retrieveLifecycleManager(componentName)
.maybeBindAndSendAction(controlInfo.controlId, action)
}
}
override fun bindService(component: ComponentName) {
retrieveLifecycleManager(component).bindService()
}
override fun changeUser(newUser: UserHandle) {
if (newUser == currentUser) return
unbind()
currentUser = newUser
}
private fun unbind() {
unsubscribe()
loadSubscriber?.loadCancel()
loadSubscriber = null
currentProvider?.unbindService()
currentProvider = null
}
override fun onComponentRemoved(componentName: ComponentName) {
backgroundExecutor.execute {
currentProvider?.let {
if (it.componentName == componentName) {
unbind()
}
}
}
}
override fun toString(): String {
return StringBuilder(" ControlsBindingController:\n").apply {
append(" currentUser=$currentUser\n")
append(" StatefulControlSubscriber=$statefulControlSubscriber")
append(" Providers=$currentProvider\n")
}.toString()
}
private abstract inner class CallbackRunnable(val token: IBinder) : Runnable {
protected val provider: ControlsProviderLifecycleManager? = currentProvider
override fun run() {
if (provider == null) {
Log.e(TAG, "No current provider set")
return
}
if (provider.user != currentUser) {
Log.e(TAG, "User ${provider.user} is not current user")
return
}
if (token != provider.token) {
Log.e(TAG, "Provider for token:$token does not exist anymore")
return
}
doRun()
}
abstract fun doRun()
}
private inner class OnLoadRunnable(
token: IBinder,
val list: List<Control>,
val callback: ControlsBindingController.LoadCallback
) : CallbackRunnable(token) {
override fun doRun() {
Log.d(TAG, "LoadSubscription: Complete and loading controls")
callback.accept(list)
}
}
private inner class OnCancelAndLoadRunnable(
token: IBinder,
val list: List<Control>,
val subscription: IControlsSubscription,
val callback: ControlsBindingController.LoadCallback
) : CallbackRunnable(token) {
override fun doRun() {
Log.d(TAG, "LoadSubscription: Canceling and loading controls")
provider?.cancelSubscription(subscription)
callback.accept(list)
}
}
private inner class OnSubscribeRunnable(
token: IBinder,
val subscription: IControlsSubscription,
val requestLimit: Long
) : CallbackRunnable(token) {
override fun doRun() {
Log.d(TAG, "LoadSubscription: Starting subscription")
provider?.startSubscription(subscription, requestLimit)
}
}
private inner class OnActionResponseRunnable(
token: IBinder,
val controlId: String,
@ControlAction.ResponseResult val response: Int
) : CallbackRunnable(token) {
override fun doRun() {
provider?.let {
lazyController.get().onActionResponse(it.componentName, controlId, response)
}
}
}
private inner class OnLoadErrorRunnable(
token: IBinder,
val error: String,
val callback: ControlsBindingController.LoadCallback
) : CallbackRunnable(token) {
override fun doRun() {
callback.error(error)
provider?.let {
Log.e(TAG, "onError receive from '${it.componentName}': $error")
}
}
}
private inner class LoadSubscriber(
var callback: ControlsBindingController.LoadCallback,
val requestLimit: Long
) : IControlsSubscriber.Stub() {
val loadedControls = ArrayList<Control>()
private var isTerminated = AtomicBoolean(false)
private var _loadCancelInternal: (() -> Unit)? = null
private lateinit var subscription: IControlsSubscription
/**
* Potentially cancel a subscriber. The subscriber may also have terminated, in which case
* the request is ignored.
*/
fun loadCancel() = Runnable {
_loadCancelInternal?.let {
Log.d(TAG, "Canceling loadSubscribtion")
it.invoke()
}
}
override fun onSubscribe(token: IBinder, subs: IControlsSubscription) {
subscription = subs
_loadCancelInternal = { currentProvider?.cancelSubscription(subscription) }
backgroundExecutor.execute(OnSubscribeRunnable(token, subs, requestLimit))
}
override fun onNext(token: IBinder, c: Control) {
backgroundExecutor.execute {
if (isTerminated.get()) return@execute
loadedControls.add(c)
// Once we have reached our requestLimit, send a request to cancel, and immediately
// load the results. Calls to onError() and onComplete() are not required after
// cancel.
if (loadedControls.size >= requestLimit) {
maybeTerminateAndRun(
OnCancelAndLoadRunnable(token, loadedControls, subscription, callback)
)
}
}
}
override fun onError(token: IBinder, s: String) {
maybeTerminateAndRun(OnLoadErrorRunnable(token, s, callback))
}
override fun onComplete(token: IBinder) {
maybeTerminateAndRun(OnLoadRunnable(token, loadedControls, callback))
}
private fun maybeTerminateAndRun(postTerminateFn: Runnable) {
if (isTerminated.get()) return
_loadCancelInternal = {}
// Reassign the callback to clear references to other areas of code. Binders such as
// this may not be GC'd right away, so do not hold onto these references.
callback = emptyCallback
currentProvider?.cancelLoadTimeout()
backgroundExecutor.execute {
isTerminated.compareAndSet(false, true)
postTerminateFn.run()
}
}
}
}
private data class Key(val component: ComponentName, val user: UserHandle)
|