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 androidx.window.extensions.layout;
18 
19 import androidx.window.common.CommonFoldingFeature;
20 import androidx.window.common.DeviceStateManagerFoldingFeatureProducer;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 /**
26  * Util functions for working with {@link androidx.window.extensions.layout.DisplayFoldFeature}.
27  */
28 public class DisplayFoldFeatureUtil {
29 
DisplayFoldFeatureUtil()30     private DisplayFoldFeatureUtil() {}
31 
create(CommonFoldingFeature foldingFeature, boolean isHalfOpenedSupported)32     private static DisplayFoldFeature create(CommonFoldingFeature foldingFeature,
33             boolean isHalfOpenedSupported) {
34         final int foldType;
35         if (foldingFeature.getType() == CommonFoldingFeature.COMMON_TYPE_HINGE) {
36             foldType = DisplayFoldFeature.TYPE_HINGE;
37         } else {
38             foldType = DisplayFoldFeature.TYPE_SCREEN_FOLD_IN;
39         }
40         DisplayFoldFeature.Builder featureBuilder = new DisplayFoldFeature.Builder(foldType);
41 
42         if (isHalfOpenedSupported) {
43             featureBuilder.addProperty(DisplayFoldFeature.FOLD_PROPERTY_SUPPORTS_HALF_OPENED);
44         }
45         return featureBuilder.build();
46     }
47 
48     /**
49      * Returns the list of supported {@link DisplayFeature} calculated from the
50      * {@link DeviceStateManagerFoldingFeatureProducer}.
51      */
extractDisplayFoldFeatures( DeviceStateManagerFoldingFeatureProducer producer)52     public static List<DisplayFoldFeature> extractDisplayFoldFeatures(
53             DeviceStateManagerFoldingFeatureProducer producer) {
54         List<DisplayFoldFeature> foldFeatures = new ArrayList<>();
55         List<CommonFoldingFeature> folds = producer.getFoldsWithUnknownState();
56 
57         final boolean isHalfOpenedSupported = producer.isHalfOpenedSupported();
58         for (CommonFoldingFeature fold : folds) {
59             foldFeatures.add(DisplayFoldFeatureUtil.create(fold, isHalfOpenedSupported));
60         }
61         return foldFeatures;
62     }
63 }
64