• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 package com.android.launcher3.util;
17 
18 import static com.android.launcher3.ResourceUtils.INVALID_RESOURCE_HANDLE;
19 import static com.android.launcher3.Utilities.dpiFromPx;
20 
21 import android.annotation.TargetApi;
22 import android.content.Context;
23 import android.content.res.Configuration;
24 import android.graphics.Insets;
25 import android.graphics.Rect;
26 import android.os.Build;
27 import android.view.WindowInsets;
28 import android.view.WindowInsets.Type;
29 import android.view.WindowManager;
30 import android.view.WindowMetrics;
31 
32 import com.android.launcher3.R;
33 import com.android.launcher3.ResourceUtils;
34 import com.android.launcher3.util.DisplayController.PortraitSize;
35 
36 import java.util.Collection;
37 import java.util.HashSet;
38 import java.util.Set;
39 
40 /**
41  * Utility class to simulate window manager APIs until proper APIs are available
42  */
43 @TargetApi(Build.VERSION_CODES.S)
44 public class WindowManagerCompat {
45 
46     public static final int MIN_TABLET_WIDTH = 600;
47 
48     /**
49      * Returns a set of supported render sizes for a set of internal displays.
50      * This is a temporary workaround which assumes only nav-bar insets change across displays
51      * @param consumeTaskBar if true, it assumes that task bar is part of the app window
52      *                       and ignores any insets because of task bar.
53      */
getDisplayProfiles( Context windowContext, Collection<PortraitSize> allDisplaySizes, int densityDpi, boolean consumeTaskBar)54     public static Set<WindowMetrics> getDisplayProfiles(
55             Context windowContext, Collection<PortraitSize> allDisplaySizes,
56             int densityDpi, boolean consumeTaskBar) {
57         WindowInsets metrics = windowContext.getSystemService(WindowManager.class)
58                 .getMaximumWindowMetrics().getWindowInsets();
59         boolean hasNavbar = ResourceUtils.getIntegerByName(
60                 "config_navBarInteractionMode",
61                 windowContext.getResources(),
62                 INVALID_RESOURCE_HANDLE) != 0;
63 
64         WindowInsets.Builder insetsBuilder = new WindowInsets.Builder(metrics);
65 
66         Set<WindowMetrics> result = new HashSet<>();
67         for (PortraitSize size : allDisplaySizes) {
68             int swDP = (int) dpiFromPx(size.width, densityDpi);
69             boolean isTablet = swDP >= MIN_TABLET_WIDTH;
70 
71             final Insets portraitNav, landscapeNav;
72             if (isTablet && !consumeTaskBar) {
73                 portraitNav = landscapeNav = Insets.of(0, 0, 0, windowContext.getResources()
74                         .getDimensionPixelSize(R.dimen.taskbar_size));
75             } else if (hasNavbar) {
76                 portraitNav = Insets.of(0, 0, 0,
77                         getSystemResource(windowContext, "navigation_bar_height", swDP));
78                 landscapeNav = isTablet
79                         ? Insets.of(0, 0, 0, getSystemResource(windowContext,
80                                 "navigation_bar_height_landscape", swDP))
81                         : Insets.of(0, 0, getSystemResource(windowContext,
82                                 "navigation_bar_width", swDP), 0);
83             } else {
84                 portraitNav = landscapeNav = Insets.of(0, 0, 0, 0);
85             }
86 
87             result.add(new WindowMetrics(
88                     new Rect(0, 0, size.width, size.height),
89                     insetsBuilder.setInsets(Type.navigationBars(), portraitNav).build()));
90             result.add(new WindowMetrics(
91                     new Rect(0, 0, size.height, size.width),
92                     insetsBuilder.setInsets(Type.navigationBars(), landscapeNav).build()));
93         }
94         return result;
95     }
96 
getSystemResource(Context context, String key, int swDp)97     private static int getSystemResource(Context context, String key, int swDp) {
98         int resourceId = context.getResources().getIdentifier(key, "dimen", "android");
99         if (resourceId > 0) {
100             Configuration conf = new Configuration();
101             conf.smallestScreenWidthDp = swDp;
102             return context.createConfigurationContext(conf)
103                     .getResources().getDimensionPixelSize(resourceId);
104         }
105         return 0;
106     }
107 }
108