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.WindowInsets.Type.displayCutout;
20  import static android.view.WindowInsets.Type.navigationBars;
21  
22  import android.annotation.NonNull;
23  import android.graphics.Point;
24  import android.graphics.Rect;
25  import android.view.Display;
26  import android.view.WindowInsets;
27  import android.view.WindowMetrics;
28  
29  /**
30   * Helper class to calculate size with {@link android.graphics.Insets} based on provided
31   * {@link WindowMetrics}
32   *
33   * @hide
34   */
35  public final class WindowMetricsHelper {
WindowMetricsHelper()36      private WindowMetricsHelper() {}
37  
38      /**
39       * Returns bounds excluding navigation bar and display cutout (but including status bar).
40       * This has the same behavior as {@link Display#getSize(Point)}.
41       */
getBoundsExcludingNavigationBarAndCutout( @onNull WindowMetrics windowMetrics)42      public static Rect getBoundsExcludingNavigationBarAndCutout(
43              @NonNull WindowMetrics windowMetrics) {
44          final WindowInsets windowInsets = windowMetrics.getWindowInsets();
45          final Rect result = new Rect(windowMetrics.getBounds());
46          result.inset(windowInsets.getInsetsIgnoringVisibility(navigationBars() | displayCutout()));
47          return result;
48      }
49  }
50