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