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.compat
16 
17 import android.content.Context
18 import android.content.res.Configuration
19 import com.android.systemui.unfold.updates.FoldProvider
20 import com.android.systemui.unfold.updates.FoldProvider.FoldCallback
21 import java.util.concurrent.Executor
22 
23 /**
24  * Fold provider that notifies about fold state based on the screen size
25  * It could be used when no activity context is available
26  * TODO(b/232369816): use Jetpack WM library when non-activity contexts supported b/169740873
27  */
28 class ScreenSizeFoldProvider(context: Context) : FoldProvider {
29 
30     private var isFolded: Boolean = false
31     private var callbacks: MutableList<FoldCallback> = arrayListOf()
32 
33     init {
34         onConfigurationChange(context.resources.configuration)
35     }
36 
registerCallbacknull37     override fun registerCallback(callback: FoldCallback, executor: Executor) {
38         callbacks += callback
39         callback.onFoldUpdated(isFolded)
40     }
41 
unregisterCallbacknull42     override fun unregisterCallback(callback: FoldCallback) {
43         callbacks -= callback
44     }
45 
onConfigurationChangenull46     fun onConfigurationChange(newConfig: Configuration) {
47         val newIsFolded =
48                 newConfig.smallestScreenWidthDp < INNER_SCREEN_SMALLEST_SCREEN_WIDTH_THRESHOLD_DP
49         if (newIsFolded == isFolded) {
50             return
51         }
52 
53         isFolded = newIsFolded
54         callbacks.forEach { it.onFoldUpdated(isFolded) }
55     }
56 }
57 
58 internal const val INNER_SCREEN_SMALLEST_SCREEN_WIDTH_THRESHOLD_DP = 600
59