1 /*
<lambda>null2  * Copyright (C) 2023 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.fold.ui.helper
18 
19 import androidx.annotation.VisibleForTesting
20 import androidx.window.layout.FoldingFeature
21 import androidx.window.layout.WindowLayoutInfo
22 
23 sealed interface FoldPosture {
24     /** A foldable device that's fully closed/folded or a device that doesn't support folding. */
25     data object Folded : FoldPosture
26     /** A foldable that's halfway open with the hinge held vertically. */
27     data object Book : FoldPosture
28     /** A foldable that's halfway open with the hinge held horizontally. */
29     data object Tabletop : FoldPosture
30     /** A foldable that's fully unfolded / flat. */
31     data object FullyUnfolded : FoldPosture
32 }
33 
34 /**
35  * Internal version of `foldPosture` in the System UI Compose library, extracted here to allow for
36  * testing that's not dependent on Compose.
37  */
38 @VisibleForTesting
foldPostureInternalnull39 fun foldPostureInternal(layoutInfo: WindowLayoutInfo?): FoldPosture {
40     return layoutInfo
41         ?.displayFeatures
42         ?.firstNotNullOfOrNull { it as? FoldingFeature }
43         .let { foldingFeature ->
44             when (foldingFeature?.state) {
45                 null -> FoldPosture.Folded
46                 FoldingFeature.State.HALF_OPENED -> foldingFeature.orientation.toHalfwayPosture()
47                 FoldingFeature.State.FLAT ->
48                     if (foldingFeature.isSeparating) {
49                         // Dual screen device.
50                         foldingFeature.orientation.toHalfwayPosture()
51                     } else {
52                         FoldPosture.FullyUnfolded
53                     }
54                 else -> error("Unsupported state \"${foldingFeature.state}\"")
55             }
56         }
57 }
58 
FoldingFeaturenull59 private fun FoldingFeature.Orientation.toHalfwayPosture(): FoldPosture {
60     return when (this) {
61         FoldingFeature.Orientation.HORIZONTAL -> FoldPosture.Tabletop
62         FoldingFeature.Orientation.VERTICAL -> FoldPosture.Book
63         else -> error("Unsupported orientation \"$this\"")
64     }
65 }
66