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 17 package com.android.internal.policy; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.graphics.Insets; 22 import android.util.RotationUtils; 23 import android.view.DisplayCutout; 24 import android.view.Surface; 25 26 import com.android.internal.R; 27 28 /** 29 * Utility functions for system bars used by both window manager and System UI. 30 * 31 * @hide 32 */ 33 public final class SystemBarUtils { 34 35 /** 36 * Gets the status bar height. 37 */ getStatusBarHeight(Context context)38 public static int getStatusBarHeight(Context context) { 39 return getStatusBarHeight(context.getResources(), context.getDisplay().getCutout()); 40 } 41 42 /** 43 * Gets the status bar height with a specific display cutout. 44 */ getStatusBarHeight(Resources res, DisplayCutout cutout)45 public static int getStatusBarHeight(Resources res, DisplayCutout cutout) { 46 final int defaultSize = res.getDimensionPixelSize(R.dimen.status_bar_height_default); 47 final int safeInsetTop = cutout == null ? 0 : cutout.getSafeInsetTop(); 48 final int waterfallInsetTop = cutout == null ? 0 : cutout.getWaterfallInsets().top; 49 // The status bar height should be: 50 // Max(top cutout size, (status bar default height + waterfall top size)) 51 return Math.max(safeInsetTop, defaultSize + waterfallInsetTop); 52 } 53 54 /** 55 * Gets the status bar height for a specific rotation. 56 */ getStatusBarHeightForRotation( Context context, @Surface.Rotation int targetRot)57 public static int getStatusBarHeightForRotation( 58 Context context, @Surface.Rotation int targetRot) { 59 final int rotation = context.getDisplay().getRotation(); 60 final DisplayCutout cutout = context.getDisplay().getCutout(); 61 62 Insets insets = cutout == null ? Insets.NONE : Insets.of(cutout.getSafeInsets()); 63 Insets waterfallInsets = cutout == null ? Insets.NONE : cutout.getWaterfallInsets(); 64 // rotate insets to target rotation if needed. 65 if (rotation != targetRot) { 66 if (!insets.equals(Insets.NONE)) { 67 insets = RotationUtils.rotateInsets( 68 insets, RotationUtils.deltaRotation(rotation, targetRot)); 69 } 70 if (!waterfallInsets.equals(Insets.NONE)) { 71 waterfallInsets = RotationUtils.rotateInsets( 72 waterfallInsets, RotationUtils.deltaRotation(rotation, targetRot)); 73 } 74 } 75 final int defaultSize = 76 context.getResources().getDimensionPixelSize(R.dimen.status_bar_height_default); 77 // The status bar height should be: 78 // Max(top cutout size, (status bar default height + waterfall top size)) 79 return Math.max(insets.top, defaultSize + waterfallInsets.top); 80 } 81 82 /** 83 * Gets the height of area above QQS where battery/time go in notification panel. The height 84 * equals to status bar height if status bar height is bigger than the 85 * {@link R.dimen#quick_qs_offset_height}. 86 */ getQuickQsOffsetHeight(Context context)87 public static int getQuickQsOffsetHeight(Context context) { 88 final int defaultSize = context.getResources().getDimensionPixelSize( 89 R.dimen.quick_qs_offset_height); 90 final int statusBarHeight = getStatusBarHeight(context); 91 // Equals to status bar height if status bar height is bigger. 92 return Math.max(defaultSize, statusBarHeight); 93 } 94 } 95