blob: ab5a06eb99de88a64325f4bbcdb82658afbc6830 [file] [log] [blame]
Charles Chenf65e0222020-01-17 11:51:34 +08001/*
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
17package android.view;
18
19import android.annotation.NonNull;
Charles Chen65da12a2020-02-19 10:33:48 +080020import android.graphics.Point;
Charles Chenf65e0222020-01-17 11:51:34 +080021import android.util.Size;
22
23/**
24 * Metrics about a Window, consisting of the size 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 */
33public final class WindowMetrics {
34 private final @NonNull Size mSize;
35 private final @NonNull WindowInsets mWindowInsets;
36
37 public WindowMetrics(@NonNull Size size, @NonNull WindowInsets windowInsets) {
38 mSize = size;
39 mWindowInsets = windowInsets;
40 }
41
42 /**
43 * Returns the size of the window.
Charles Chen65da12a2020-02-19 10:33:48 +080044 * <p>
45 * <b>Note that this reports a different size than {@link Display#getSize(Point)}.</b>
46 * This method reports the window size including all system bars area, while
47 * {@link Display#getSize(Point)} reports the area excluding navigation bars and display cutout
48 * areas. The value reported by {@link Display#getSize(Point)} can be obtained by using:
49 * <pre class="prettyprint">
50 * final WindowMetrics metrics = windowManager.getCurrentMetrics();
51 * // Gets all excluding insets
52 * final WindowInsets windowInsets = metrics.getWindowInsets();
53 * Insets insets = windowInsets.getInsets(WindowInsets.Type.navigationBars());
54 * final DisplayCutout cutout = windowInsets.getCutout();
55 * if (cutout != null) {
56 * final Insets cutoutSafeInsets = Insets.of(cutout.getSafeInsetsLeft(), ...);
57 * insets = insets.max(insets, cutoutSafeInsets);
58 * }
59 *
60 * int insetsWidth = insets.right + insets.left;
61 * int insetsHeight = insets.top + insets.bottom;
62 *
63 * // Legacy size that Display#getSize reports
64 * final Size legacySize = new Size(metrics.getWidth() - insetsWidth,
65 * metrics.getHeight() - insetsHeight);
66 * </pre>
67 * </p>
Charles Chenf65e0222020-01-17 11:51:34 +080068 *
69 * @return window size in pixel.
70 */
71 public @NonNull Size getSize() {
72 return mSize;
73 }
74
75 /**
76 * Returns the {@link WindowInsets} of the window.
77 *
78 * @return the {@link WindowInsets} of the window.
79 */
80 public @NonNull WindowInsets getWindowInsets() {
81 return mWindowInsets;
82 }
83}