1 /*
2  * Copyright (C) 2022 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 distributed under the
11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12  * KIND, either express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 package com.android.systemui.unfold.system
16 
17 import android.content.Context
18 import android.hardware.devicestate.DeviceStateManager
19 import com.android.systemui.unfold.updates.FoldProvider
20 import com.android.systemui.unfold.updates.FoldProvider.FoldCallback
21 import java.util.concurrent.ConcurrentHashMap
22 import java.util.concurrent.Executor
23 import javax.inject.Inject
24 import javax.inject.Singleton
25 
26 @Singleton
27 class DeviceStateManagerFoldProvider
28 @Inject
29 constructor(private val deviceStateManager: DeviceStateManager, private val context: Context) :
30     FoldProvider {
31 
32     private val callbacks =
33         ConcurrentHashMap<FoldCallback, DeviceStateManager.DeviceStateCallback>()
34 
registerCallbacknull35     override fun registerCallback(callback: FoldCallback, executor: Executor) {
36         val listener = FoldStateListener(context, callback)
37         deviceStateManager.registerCallback(executor, listener)
38         callbacks[callback] = listener
39     }
40 
unregisterCallbacknull41     override fun unregisterCallback(callback: FoldCallback) {
42         val listener = callbacks.remove(callback)
43         listener?.let { deviceStateManager.unregisterCallback(it) }
44     }
45 
46     private inner class FoldStateListener(context: Context, listener: FoldCallback) :
<lambda>null47         DeviceStateManager.FoldStateListener(context, { listener.onFoldUpdated(it) })
48 }
49