drchen | 30649a7 | 2023-05-16 12:44:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 androidx.compose.material3.adaptive |
| 18 | |
drchen | 7540552 | 2023-05-23 10:04:54 -0700 | [diff] [blame] | 19 | import android.app.Activity |
drchen | d93af49 | 2023-05-31 14:03:59 -0700 | [diff] [blame] | 20 | import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi |
| 21 | import androidx.compose.material3.windowsizeclass.WindowSizeClass |
drchen | 30649a7 | 2023-05-16 12:44:29 -0700 | [diff] [blame] | 22 | import androidx.compose.runtime.Composable |
| 23 | import androidx.compose.runtime.State |
drchen | 7540552 | 2023-05-23 10:04:54 -0700 | [diff] [blame] | 24 | import androidx.compose.runtime.collectAsState |
drchen | 30649a7 | 2023-05-16 12:44:29 -0700 | [diff] [blame] | 25 | import androidx.compose.runtime.mutableStateOf |
| 26 | import androidx.compose.runtime.remember |
| 27 | import androidx.compose.ui.platform.LocalConfiguration |
| 28 | import androidx.compose.ui.platform.LocalContext |
drchen | d93af49 | 2023-05-31 14:03:59 -0700 | [diff] [blame] | 29 | import androidx.compose.ui.platform.LocalDensity |
drchen | 30649a7 | 2023-05-16 12:44:29 -0700 | [diff] [blame] | 30 | import androidx.compose.ui.unit.IntSize |
drchen | d93af49 | 2023-05-31 14:03:59 -0700 | [diff] [blame] | 31 | import androidx.compose.ui.unit.toSize |
drchen | 7540552 | 2023-05-23 10:04:54 -0700 | [diff] [blame] | 32 | import androidx.window.layout.FoldingFeature |
| 33 | import androidx.window.layout.WindowInfoTracker |
drchen | 30649a7 | 2023-05-16 12:44:29 -0700 | [diff] [blame] | 34 | import androidx.window.layout.WindowMetricsCalculator |
drchen | 7540552 | 2023-05-23 10:04:54 -0700 | [diff] [blame] | 35 | import kotlinx.coroutines.flow.map |
drchen | 30649a7 | 2023-05-16 12:44:29 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
drchen | d93af49 | 2023-05-31 14:03:59 -0700 | [diff] [blame] | 38 | * Calculates and returns [WindowAdaptiveInfo] of the provided context. It's a convenient function |
| 39 | * that uses the Material default [WindowSizeClass.calculateFromSize] and [calculatePosture] |
| 40 | * functions to retrieve [WindowSizeClass] and [Posture]. |
| 41 | * |
drchen | d93af49 | 2023-05-31 14:03:59 -0700 | [diff] [blame] | 42 | * @return [WindowAdaptiveInfo] of the provided context |
| 43 | */ |
| 44 | @ExperimentalMaterial3AdaptiveApi |
drchen | 69da28c | 2023-10-04 11:55:51 -0700 | [diff] [blame] | 45 | @OptIn(ExperimentalMaterial3WindowSizeClassApi::class) |
drchen | d93af49 | 2023-05-31 14:03:59 -0700 | [diff] [blame] | 46 | @Composable |
drchen | 830d703 | 2023-10-16 11:29:26 -0700 | [diff] [blame] | 47 | fun currentWindowAdaptiveInfo(): WindowAdaptiveInfo = |
drchen | 69da28c | 2023-10-04 11:55:51 -0700 | [diff] [blame] | 48 | WindowAdaptiveInfo( |
| 49 | WindowSizeClass.calculateFromSize( |
| 50 | with(LocalDensity.current) { |
drchen | 830d703 | 2023-10-16 11:29:26 -0700 | [diff] [blame] | 51 | collectWindowSizeAsState().value.toSize().toDpSize() |
drchen | 69da28c | 2023-10-04 11:55:51 -0700 | [diff] [blame] | 52 | } |
| 53 | ), |
drchen | 830d703 | 2023-10-16 11:29:26 -0700 | [diff] [blame] | 54 | calculatePosture(collectFoldingFeaturesAsState().value) |
drchen | 69da28c | 2023-10-04 11:55:51 -0700 | [diff] [blame] | 55 | ) |
drchen | d93af49 | 2023-05-31 14:03:59 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
drchen | 30649a7 | 2023-05-16 12:44:29 -0700 | [diff] [blame] | 58 | * Collects the current window size from [WindowMetricsCalculator] in to a [State]. |
| 59 | * |
drchen | 30649a7 | 2023-05-16 12:44:29 -0700 | [diff] [blame] | 60 | * @return a [State] of [IntSize] that represents the current window size. |
| 61 | */ |
| 62 | @ExperimentalMaterial3AdaptiveApi |
| 63 | @Composable |
drchen | 830d703 | 2023-10-16 11:29:26 -0700 | [diff] [blame] | 64 | fun collectWindowSizeAsState(): State<IntSize> { |
drchen | 30649a7 | 2023-05-16 12:44:29 -0700 | [diff] [blame] | 65 | val size = remember { |
| 66 | mutableStateOf(IntSize(0, 0)) |
| 67 | } |
| 68 | |
| 69 | // Observe view configuration changes and recalculate the size class on each change. We can't |
| 70 | // use Activity#onConfigurationChanged as this will sometimes fail to be called on different |
| 71 | // API levels, hence why this function needs to be @Composable so we can observe the |
| 72 | // ComposeView's configuration changes. |
drchen | 69da28c | 2023-10-04 11:55:51 -0700 | [diff] [blame] | 73 | val context = LocalContext.current |
drchen | 30649a7 | 2023-05-16 12:44:29 -0700 | [diff] [blame] | 74 | size.value = remember(context, LocalConfiguration.current) { |
| 75 | val windowBounds = |
| 76 | WindowMetricsCalculator |
| 77 | .getOrCreate() |
| 78 | .computeCurrentWindowMetrics(context) |
| 79 | .bounds |
| 80 | IntSize(windowBounds.width(), windowBounds.height()) |
| 81 | } |
| 82 | |
| 83 | return size |
| 84 | } |
drchen | 7540552 | 2023-05-23 10:04:54 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Collects the current window folding features from [WindowInfoTracker] in to a [State]. |
| 88 | * |
drchen | 7540552 | 2023-05-23 10:04:54 -0700 | [diff] [blame] | 89 | * @return a [State] of a [FoldingFeature] list. |
| 90 | */ |
| 91 | @ExperimentalMaterial3AdaptiveApi |
| 92 | @Composable |
drchen | 830d703 | 2023-10-16 11:29:26 -0700 | [diff] [blame] | 93 | fun collectFoldingFeaturesAsState(): State<List<FoldingFeature>> { |
drchen | 69da28c | 2023-10-04 11:55:51 -0700 | [diff] [blame] | 94 | val context = LocalContext.current |
drchen | 7540552 | 2023-05-23 10:04:54 -0700 | [diff] [blame] | 95 | return remember(context) { |
| 96 | if (context is Activity) { |
| 97 | // TODO(b/284347941) remove the instance check after the test bug is fixed. |
| 98 | WindowInfoTracker |
| 99 | .getOrCreate(context) |
| 100 | .windowLayoutInfo(context) |
| 101 | } else { |
| 102 | WindowInfoTracker |
| 103 | .getOrCreate(context) |
| 104 | .windowLayoutInfo(context) |
| 105 | }.map { it.displayFeatures.filterIsInstance<FoldingFeature>() } |
| 106 | }.collectAsState(emptyList()) |
| 107 | } |