1 /* 2 * Copyright (C) 2020 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 android.window; 18 19 import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL; 20 import static android.view.WindowInsets.Type.displayCutout; 21 import static android.view.WindowInsets.Type.navigationBars; 22 23 import android.annotation.NonNull; 24 import android.graphics.Insets; 25 import android.graphics.Point; 26 import android.graphics.Rect; 27 import android.view.Display; 28 import android.view.DisplayCutout; 29 import android.view.ViewRootImpl; 30 import android.view.WindowInsets; 31 import android.view.WindowMetrics; 32 33 /** 34 * Helper class to calculate size with {@link android.graphics.Insets} based on provided 35 * {@link WindowMetrics} 36 * 37 * @hide 38 */ 39 public final class WindowMetricsHelper { WindowMetricsHelper()40 private WindowMetricsHelper() {} 41 42 /** 43 * Returns bounds excluding navigation bar and display cutout (but including status bar). 44 * This has the same behavior as {@link Display#getSize(Point)}. 45 */ getBoundsExcludingNavigationBarAndCutout( @onNull WindowMetrics windowMetrics)46 public static Rect getBoundsExcludingNavigationBarAndCutout( 47 @NonNull WindowMetrics windowMetrics) { 48 final WindowInsets windowInsets = windowMetrics.getWindowInsets(); 49 Insets insets; 50 if (ViewRootImpl.sNewInsetsMode == NEW_INSETS_MODE_FULL) { 51 insets = windowInsets.getInsetsIgnoringVisibility(navigationBars() | displayCutout()); 52 } else { 53 final Insets stableInsets = windowInsets.getStableInsets(); 54 insets = Insets.of(stableInsets.left, 0 /* top */, stableInsets.right, 55 stableInsets.bottom); 56 final DisplayCutout cutout = windowInsets.getDisplayCutout(); 57 insets = (cutout != null) ? Insets.max(insets, Insets.of(cutout.getSafeInsets())) 58 : insets; 59 } 60 final Rect result = new Rect(windowMetrics.getBounds()); 61 result.inset(insets); 62 return result; 63 } 64 } 65