• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.model
18 
19 import com.android.systemui.shared.system.QuickStepContract.getSystemUiStateString
20 
21 /**
22  * Represents a set of state changes. A bit can either be set to `true` or `false`.
23  *
24  * This is used in [SysUIStateDisplaysInteractor] to selectively change bits.
25  */
26 class StateChange {
27     private var flagsToSet: Long = 0
28     private var flagsToClear: Long = 0
29 
30     /**
31      * Sets the [state] of the given [bit].
32      *
33      * @return `this` for chaining purposes
34      */
35     fun setFlag(bit: Long, state: Boolean): StateChange {
36         if (state) {
37             flagsToSet = flagsToSet or bit
38             flagsToClear = flagsToClear and bit.inv()
39         } else {
40             flagsToClear = flagsToClear or bit
41             flagsToSet = flagsToSet and bit.inv()
42         }
43         return this
44     }
45 
46     /**
47      * Applies all changed flags to [sysUiState].
48      *
49      * Note this doesn't call [SysUiState.commitUpdate].
50      */
51     fun applyTo(sysUiState: SysUiState) {
52         iterateBits(flagsToSet or flagsToClear) { bit ->
53             val isBitSetInNewState = flagsToSet and bit != 0L
54             sysUiState.setFlag(bit, isBitSetInNewState)
55         }
56     }
57 
58     fun applyTo(sysUiState: Long): Long {
59         var newState = sysUiState
60         newState = newState or flagsToSet
61         newState = newState and flagsToClear.inv()
62         return newState
63     }
64 
65     private inline fun iterateBits(flags: Long, action: (bit: Long) -> Unit) {
66         var remaining = flags
67         while (remaining != 0L) {
68             val lowestBit = remaining and -remaining
69             action(lowestBit)
70 
71             remaining -= lowestBit
72         }
73     }
74 
75     /**
76      * Clears all the flags changed in a [sysUiState].
77      *
78      * Note this doesn't call [SysUiState.commitUpdate].
79      */
80     fun clearFrom(sysUiState: SysUiState) {
81         iterateBits(flagsToSet or flagsToClear) { bit -> sysUiState.setFlag(bit, false) }
82     }
83 
84     /** Resets all the pending changes. */
85     fun clear() {
86         flagsToSet = 0
87         flagsToClear = 0
88     }
89 
90     override fun toString(): String {
91         return """StateChange(flagsToSet=${getSystemUiStateString(flagsToSet)}, flagsToClear=${
92             getSystemUiStateString(
93                 flagsToClear
94             )
95         })"""
96     }
97 }
98