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.view; 18 19 import android.annotation.NonNull; 20 import android.graphics.Point; 21 import android.graphics.Rect; 22 23 /** 24 * Metrics about a Window, consisting of the bounds and {@link WindowInsets}. 25 * <p> 26 * This is usually obtained from {@link WindowManager#getCurrentWindowMetrics()} and 27 * {@link WindowManager#getMaximumWindowMetrics()}. 28 * 29 * @see WindowInsets#getInsets(int) 30 * @see WindowManager#getCurrentWindowMetrics() 31 * @see WindowManager#getMaximumWindowMetrics() 32 */ 33 public final class WindowMetrics { 34 private final @NonNull Rect mBounds; 35 private final @NonNull WindowInsets mWindowInsets; 36 WindowMetrics(@onNull Rect bounds, @NonNull WindowInsets windowInsets)37 public WindowMetrics(@NonNull Rect bounds, @NonNull WindowInsets windowInsets) { 38 mBounds = bounds; 39 mWindowInsets = windowInsets; 40 } 41 42 /** 43 * Returns the bounds of the area associated with this window or visual context. 44 * <p> 45 * <b>Note that the size of the reported bounds can have different size than 46 * {@link Display#getSize(Point)}.</b> This method reports the window size including all system 47 * bar areas, while {@link Display#getSize(Point)} reports the area excluding navigation bars 48 * and display cutout areas. The value reported by {@link Display#getSize(Point)} can be 49 * obtained by using: 50 * <pre class="prettyprint"> 51 * final WindowMetrics metrics = windowManager.getCurrentWindowMetrics(); 52 * // Gets all excluding insets 53 * final WindowInsets windowInsets = metrics.getWindowInsets(); 54 * Insets insets = windowInsets.getInsetsIgnoreVisibility(WindowInsets.Type.navigationBars() 55 * | WindowInsets.Type.displayCutout()); 56 * 57 * int insetsWidth = insets.right + insets.left; 58 * int insetsHeight = insets.top + insets.bottom; 59 * 60 * // Legacy size that Display#getSize reports 61 * final Rect bounds = metrics.getBounds(); 62 * final Size legacySize = new Size(bounds.width() - insetsWidth, 63 * bounds.height() - insetsHeight); 64 * </pre> 65 * </p> 66 * 67 * @return window bounds in pixels. 68 */ getBounds()69 public @NonNull Rect getBounds() { 70 return mBounds; 71 } 72 73 /** 74 * Returns the {@link WindowInsets} of the area associated with this window or visual context. 75 * 76 * @return the {@link WindowInsets} of the visual area. 77 */ getWindowInsets()78 public @NonNull WindowInsets getWindowInsets() { 79 return mWindowInsets; 80 } 81 } 82