Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 61bdfe42 authored by Shawn Lin's avatar Shawn Lin Committed by Automerger Merge Worker
Browse files

Merge "Add APIs to get system bar heights" into sc-v2-dev am: 59f51a95 am: 65c8136d

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15611697

Change-Id: Ia433011dcfdab2c27533dffa5caad31c47751e46
parents 4adb5658 65c8136d
Loading
Loading
Loading
Loading
+94 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.policy;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Insets;
import android.util.RotationUtils;
import android.view.DisplayCutout;
import android.view.Surface;

import com.android.internal.R;

/**
 * Utility functions for system bars used by both window manager and System UI.
 *
 * @hide
 */
public final class SystemBarUtils {

    /**
     * Gets the status bar height.
     */
    public static int getStatusBarHeight(Context context) {
        return getStatusBarHeight(context.getResources(), context.getDisplay().getCutout());
    }

    /**
     * Gets the status bar height with a specific display cutout.
     */
    public static int getStatusBarHeight(Resources res, DisplayCutout cutout) {
        final int defaultSize = res.getDimensionPixelSize(R.dimen.status_bar_height);
        final int safeInsetTop = cutout == null ? 0 : cutout.getSafeInsetTop();
        final int waterfallInsetTop = cutout == null ? 0 : cutout.getWaterfallInsets().top;
        // The status bar height should be:
        // Max(top cutout size, (status bar default height + waterfall top size))
        return Math.max(safeInsetTop, defaultSize + waterfallInsetTop);
    }

    /**
     * Gets the status bar height for a specific rotation.
     */
    public static int getStatusBarHeightForRotation(
            Context context, @Surface.Rotation int targetRot) {
        final int rotation = context.getDisplay().getRotation();
        final DisplayCutout cutout = context.getDisplay().getCutout();

        Insets insets = cutout == null ? Insets.NONE : Insets.of(cutout.getSafeInsets());
        Insets waterfallInsets = cutout == null ? Insets.NONE : cutout.getWaterfallInsets();
        // rotate insets to target rotation if needed.
        if (rotation != targetRot) {
            if (!insets.equals(Insets.NONE)) {
                insets = RotationUtils.rotateInsets(
                        insets, RotationUtils.deltaRotation(rotation, targetRot));
            }
            if (!waterfallInsets.equals(Insets.NONE)) {
                waterfallInsets = RotationUtils.rotateInsets(
                        waterfallInsets, RotationUtils.deltaRotation(rotation, targetRot));
            }
        }
        final int defaultSize =
                context.getResources().getDimensionPixelSize(R.dimen.status_bar_height);
        // The status bar height should be:
        // Max(top cutout size, (status bar default height + waterfall top size))
        return Math.max(insets.top, defaultSize + waterfallInsets.top);
    }

    /**
     * Gets the height of area above QQS where battery/time go in notification panel. The height
     * equals to status bar height if status bar height is bigger than the
     * {@link R.dimen#quick_qs_offset_height}.
     */
    public static int getQuickQsOffsetHeight(Context context) {
        final int defaultSize = context.getResources().getDimensionPixelSize(
                R.dimen.quick_qs_offset_height);
        final int statusBarHeight = getStatusBarHeight(context);
        // Equals to status bar height if status bar height is bigger.
        return Math.max(defaultSize, statusBarHeight);
    }
}
+0 −2
Original line number Diff line number Diff line
@@ -27,8 +27,6 @@
    <dimen name="password_keyboard_spacebar_vertical_correction">2dip</dimen>
    <dimen name="preference_widget_width">72dp</dimen>

    <!-- Height of the status bar -->
    <dimen name="status_bar_height">@dimen/status_bar_height_landscape</dimen>
    <!-- Height of area above QQS where battery/time go -->
    <dimen name="quick_qs_offset_height">48dp</dimen>
    <!-- Default height of an action bar. -->
+14 −8
Original line number Diff line number Diff line
@@ -39,15 +39,21 @@
    <!-- Elevation of toast view -->
    <dimen name="toast_elevation">2dp</dimen>

    <!-- Height of the status bar -->
    <dimen name="status_bar_height">@dimen/status_bar_height_portrait</dimen>
    <!-- Height of the status bar in portrait. The height should be
         Max((status bar content height + waterfall top size), top cutout size) -->
    <dimen name="status_bar_height_portrait">24dp</dimen>
    <!-- Height of the status bar in landscape. The height should be
         Max((status bar content height + waterfall top size), top cutout size) -->
    <!-- Height of the status bar.
         Do not read this dimen directly. Use {@link SystemBarUtils#getStatusBarHeight} instead.
         -->
    <dimen name="status_bar_height">24dp</dimen>
    <!-- Height of the status bar in portrait.
         Do not read this dimen directly. Use {@link SystemBarUtils#getStatusBarHeight} instead.
         -->
    <dimen name="status_bar_height_portrait">@dimen/status_bar_height</dimen>
    <!-- Height of the status bar in landscape.
         Do not read this dimen directly. Use {@link SystemBarUtils#getStatusBarHeight} instead.
         -->
    <dimen name="status_bar_height_landscape">@dimen/status_bar_height_portrait</dimen>
    <!-- Height of area above QQS where battery/time go -->
    <!-- Height of area above QQS where battery/time go.
         Do not read this dimen directly. Use {@link SystemBarUtils#getQuickQsOffsetHeight} instead.
         -->
    <dimen name="quick_qs_offset_height">48dp</dimen>
    <!-- Height of the bottom navigation / system bar. -->
    <dimen name="navigation_bar_height">48dp</dimen>
+9 −35
Original line number Diff line number Diff line
@@ -48,9 +48,11 @@ import android.view.Gravity;
import android.view.InsetsSource;
import android.view.InsetsState;
import android.view.Surface;
import android.view.WindowInsets;

import androidx.annotation.VisibleForTesting;

import com.android.internal.R;
import com.android.internal.policy.SystemBarUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -198,12 +200,13 @@ public class DisplayLayout {
        recalcInsets(res);
    }

    private void recalcInsets(Resources res) {
    @VisibleForTesting
    void recalcInsets(Resources res) {
        computeNonDecorInsets(res, mRotation, mWidth, mHeight, mCutout, mInsetsState, mUiMode,
                mNonDecorInsets, mHasNavigationBar);
        mStableInsets.set(mNonDecorInsets);
        if (mHasStatusBar) {
            convertNonDecorInsetsToStableInsets(res, mStableInsets, mWidth, mHeight, mHasStatusBar);
            convertNonDecorInsetsToStableInsets(res, mStableInsets, mCutout, mHasStatusBar);
        }
        mNavBarFrameHeight = getNavigationBarFrameHeight(res, mWidth > mHeight);
    }
@@ -323,12 +326,12 @@ public class DisplayLayout {
    /**
     * Calculates the stable insets if we already have the non-decor insets.
     */
    private static void convertNonDecorInsetsToStableInsets(Resources res, Rect inOutInsets,
            int displayWidth, int displayHeight, boolean hasStatusBar) {
    private void convertNonDecorInsetsToStableInsets(Resources res, Rect inOutInsets,
            DisplayCutout cutout, boolean hasStatusBar) {
        if (!hasStatusBar) {
            return;
        }
        int statusBarHeight = getStatusBarHeight(displayWidth > displayHeight, res);
        int statusBarHeight = SystemBarUtils.getStatusBarHeight(res, cutout);
        inOutInsets.top = Math.max(inOutInsets.top, statusBarHeight);
    }

@@ -377,35 +380,6 @@ public class DisplayLayout {
        }
    }

    /**
     * Calculates the stable insets without running a layout.
     *
     * @param displayRotation the current display rotation
     * @param displayWidth the current display width
     * @param displayHeight the current display height
     * @param displayCutout the current display cutout
     * @param outInsets the insets to return
     */
    static void computeStableInsets(Resources res, int displayRotation, int displayWidth,
            int displayHeight, DisplayCutout displayCutout, InsetsState insetsState, int uiMode,
            Rect outInsets, boolean hasNavigationBar, boolean hasStatusBar) {
        outInsets.setEmpty();

        // Navigation bar and status bar.
        computeNonDecorInsets(res, displayRotation, displayWidth, displayHeight, displayCutout,
                insetsState, uiMode, outInsets, hasNavigationBar);
        convertNonDecorInsetsToStableInsets(res, outInsets, displayWidth, displayHeight,
                hasStatusBar);
    }

    /** Retrieve the statusbar height from resources. */
    static int getStatusBarHeight(boolean landscape, Resources res) {
        return landscape ? res.getDimensionPixelSize(
                com.android.internal.R.dimen.status_bar_height_landscape)
                : res.getDimensionPixelSize(
                        com.android.internal.R.dimen.status_bar_height_portrait);
    }

    /** Calculate the DisplayCutout for a particular display size/rotation. */
    public static DisplayCutout calculateDisplayCutoutForRotation(
            DisplayCutout cutout, int rotation, int displayWidth, int displayHeight) {
+4 −7
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import com.android.internal.R;
import com.android.internal.policy.SystemBarUtils;
import com.android.wm.shell.common.DisplayController;
import com.android.wm.shell.common.DisplayLayout;
import com.android.wm.shell.common.ShellExecutor;
@@ -307,12 +307,9 @@ class HideDisplayCutoutOrganizer extends DisplayAreaOrganizer {
        t.apply();
    }

    private int getStatusBarHeight() {
        final boolean isLandscape =
                mIsDefaultPortrait ? isDisplaySizeFlipped() : !isDisplaySizeFlipped();
        return mContext.getResources().getDimensionPixelSize(
                isLandscape ? R.dimen.status_bar_height_landscape
                        : R.dimen.status_bar_height_portrait);
    @VisibleForTesting
    int getStatusBarHeight() {
        return SystemBarUtils.getStatusBarHeight(mContext);
    }

    void dump(@NonNull PrintWriter pw) {
Loading