summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/controls/controller/StatefulControlSubscriber.kt
blob: e3eabff49d59eb07a85edb284e25ac45d00e7d3b (plain)
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
/*
 * Copyright (C) 2020 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.os.IBinder
import android.service.controls.Control
import android.service.controls.IControlsSubscriber
import android.service.controls.IControlsSubscription
import android.util.Log
import com.android.systemui.util.concurrency.DelayableExecutor

/**
 * A single subscriber, supporting stateful controls for publishers created by
 * {@link ControlsProviderService#createPublisherFor}. In general, this subscription will remain
 * active until the SysUi chooses to cancel it.
 */
class StatefulControlSubscriber(
    private val controller: ControlsController,
    private val provider: ControlsProviderLifecycleManager,
    private val bgExecutor: DelayableExecutor,
    private val requestLimit: Long
) : IControlsSubscriber.Stub() {
    private var subscriptionOpen = false
    private var subscription: IControlsSubscription? = null

    companion object {
        private const val TAG = "StatefulControlSubscriber"
    }

    private fun run(token: IBinder, f: () -> Unit) {
        if (provider.token == token) {
            bgExecutor.execute { f() }
        }
    }

    override fun onSubscribe(token: IBinder, subs: IControlsSubscription) {
        run(token) {
            subscriptionOpen = true
            subscription = subs
            provider.startSubscription(subs, requestLimit)
        }
    }

    override fun onNext(token: IBinder, control: Control) {
        run(token) {
            if (!subscriptionOpen) {
                Log.w(TAG, "Refresh outside of window for token:$token")
            } else {
                controller.refreshStatus(provider.componentName, control)
            }
        }
    }
    override fun onError(token: IBinder, error: String) {
        run(token) {
            if (subscriptionOpen) {
                subscriptionOpen = false
                Log.e(TAG, "onError receive from '${provider.componentName}': $error")
            }
        }
    }

    override fun onComplete(token: IBinder) {
        run(token) {
            if (subscriptionOpen) {
                subscriptionOpen = false
                Log.i(TAG, "onComplete receive from '${provider.componentName}'")
            }
        }
    }

    fun cancel() {
        if (!subscriptionOpen) return
        bgExecutor.execute {
            if (subscriptionOpen) {
                subscriptionOpen = false
                subscription?.let {
                    provider.cancelSubscription(it)
                }
                subscription = null
            }
        }
    }
}