1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.statusbar.disableflags
16 
17 import com.android.systemui.statusbar.CommandQueue
18 
19 /**
20  * Tracks the relevant DISABLE_* flags provided in [mask1] and [mask2] and sets [isDisabled] based
21  * on those masks. [callback] will be notified whenever [isDisabled] changes.
22  *
23  * Users are responsible for adding and removing this tracker from [CommandQueue] callbacks.
24  */
25 class DisableStateTracker(
26     private val mask1: Int,
27     private val mask2: Int,
28     private val callback: Callback,
29 ) : CommandQueue.Callbacks {
30     /**
31      * True if any of the bits in [mask1] or [mask2] are on for the current disable flags, and false
32      * otherwise.
33      */
34     var isDisabled = false
35         private set(value) {
36             if (field == value) return
37             field = value
38             callback.onDisabledChanged()
39         }
40 
41     private var displayId: Int? = null
42 
43     /** Start tracking the disable flags and updating [isDisabled] accordingly. */
startTrackingnull44     fun startTracking(commandQueue: CommandQueue, displayId: Int) {
45         // A view will only have its displayId once it's attached to a window, so we can only
46         // provide the displayId when we start tracking.
47         this.displayId = displayId
48         commandQueue.addCallback(this)
49     }
50 
51     /**
52      * Stop tracking the disable flags.
53      *
54      * [isDisabled] will stay at the same value until we start tracking again.
55      */
stopTrackingnull56     fun stopTracking(commandQueue: CommandQueue) {
57         this.displayId = null
58         commandQueue.removeCallback(this)
59     }
60 
disablenull61     override fun disable(displayId: Int, state1: Int, state2: Int, animate: Boolean) {
62         if (this.displayId == null || displayId != this.displayId) {
63             return
64         }
65         isDisabled = state1 and mask1 != 0 || state2 and mask2 != 0
66     }
67 
68     /** Callback triggered whenever the value of [isDisabled] changes. */
interfacenull69     fun interface Callback {
70         fun onDisabledChanged()
71     }
72 }
73