• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.util.concurrency
18 
19 import android.os.Handler
20 import android.os.Looper
21 import android.view.Choreographer
22 import com.android.systemui.util.Assert
23 import java.util.concurrent.Executor
24 import java.util.concurrent.atomic.AtomicReference
25 
26 private const val DEFAULT_TIMEOUT = 150L
27 
28 class UiThreadContext(
29     val looper: Looper,
30     val handler: Handler,
31     val executor: Executor,
32     val choreographer: Choreographer
33 ) {
isCurrentThreadnull34     fun isCurrentThread() {
35         Assert.isCurrentThread(looper)
36     }
37 
runWithScissorsnull38     fun <T> runWithScissors(block: () -> T): T {
39         return handler.runWithScissors(block)
40     }
41 
runWithScissorsnull42     fun runWithScissors(block: Runnable) {
43         handler.runWithScissors(block, DEFAULT_TIMEOUT)
44     }
45 }
46 
runWithScissorsnull47 fun <T> Handler.runWithScissors(block: () -> T): T {
48     val returnedValue = AtomicReference<T>()
49     runWithScissors({ returnedValue.set(block()) }, DEFAULT_TIMEOUT)
50     return returnedValue.get()!!
51 }
52