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.annotation.DimenRes; 20 import android.annotation.NonNull; 21 import android.content.Context; 22 import android.content.res.Resources; 23 import android.graphics.Insets; 24 import android.view.Display; 25 import android.view.DisplayCutout; 26 import android.view.DisplayInfo; 27 import android.view.Surface; 28 29 import com.android.internal.R; 30 31 /** 32 * Utility functions for system bars used by both window manager and System UI. 33 * 34 * @hide 35 */ 36 public final class SystemBarUtils { 37 38 /** 39 * Gets the status bar height. 40 */ getStatusBarHeight(Context context)41 public static int getStatusBarHeight(Context context) { 42 return getStatusBarHeight(context.getResources(), context.getDisplay().getCutout()); 43 } 44 45 /** 46 * Gets the status bar height with a specific display cutout. 47 */ getStatusBarHeight(Resources res, DisplayCutout cutout)48 public static int getStatusBarHeight(Resources res, DisplayCutout cutout) { 49 final int defaultSize = res.getDimensionPixelSize(R.dimen.status_bar_height_default); 50 final int safeInsetTop = cutout == null ? 0 : cutout.getSafeInsetTop(); 51 final int waterfallInsetTop = cutout == null ? 0 : cutout.getWaterfallInsets().top; 52 // The status bar height should be: 53 // Max(top cutout size, (status bar default height + waterfall top size)) 54 return Math.max(safeInsetTop, defaultSize + waterfallInsetTop); 55 } 56 57 /** 58 * Gets the status bar height for a specific rotation. 59 */ getStatusBarHeightForRotation( Context context, @Surface.Rotation int targetRot)60 public static int getStatusBarHeightForRotation( 61 Context context, @Surface.Rotation int targetRot) { 62 final Display display = context.getDisplay(); 63 final int rotation = display.getRotation(); 64 final DisplayCutout cutout = display.getCutout(); 65 DisplayInfo info = new DisplayInfo(); 66 display.getDisplayInfo(info); 67 Insets insets; 68 Insets waterfallInsets; 69 final int localWidth = context.getResources().getDisplayMetrics().widthPixels; 70 final int localHeight = context.getResources().getDisplayMetrics().heightPixels; 71 if (cutout == null) { 72 insets = Insets.NONE; 73 waterfallInsets = Insets.NONE; 74 } else { 75 DisplayCutout rotated = 76 cutout.getRotated(localWidth, localHeight, rotation, targetRot); 77 insets = Insets.of(rotated.getSafeInsets()); 78 waterfallInsets = rotated.getWaterfallInsets(); 79 } 80 final int defaultSize = 81 context.getResources().getDimensionPixelSize(R.dimen.status_bar_height_default); 82 // The status bar height should be: 83 // Max(top cutout size, (status bar default height + waterfall top size)) 84 return Math.max(insets.top, defaultSize + waterfallInsets.top); 85 } 86 87 /** 88 * Gets the height of area above QQS where battery/time go in notification panel. The height 89 * equals to status bar height if status bar height is bigger than the 90 * {@link R.dimen#quick_qs_offset_height}. 91 */ getQuickQsOffsetHeight(Context context)92 public static int getQuickQsOffsetHeight(Context context) { 93 final int defaultSize = context.getResources().getDimensionPixelSize( 94 R.dimen.quick_qs_offset_height); 95 final int statusBarHeight = getStatusBarHeight(context); 96 // Equals to status bar height if status bar height is bigger. 97 return Math.max(defaultSize, statusBarHeight); 98 } 99 100 /** 101 * Gets the taskbar frame height. 102 */ getTaskbarHeight(Resources res)103 public static int getTaskbarHeight(Resources res) { 104 return res.getDimensionPixelSize(R.dimen.taskbar_frame_height); 105 } 106 107 /** 108 * Gets the default app header height in desktop view in pixels. 109 */ getDesktopViewAppHeaderHeightPx(@onNull Context context)110 public static int getDesktopViewAppHeaderHeightPx(@NonNull Context context) { 111 return context.getResources().getDimensionPixelSize(getDesktopViewAppHeaderHeightId()); 112 } 113 114 /** 115 * Gets the dimen resource id of the default app header height in desktop view. 116 */ 117 @DimenRes getDesktopViewAppHeaderHeightId()118 public static int getDesktopViewAppHeaderHeightId() { 119 return R.dimen.desktop_view_default_header_height; 120 } 121 } 122