1 /* 2 * Copyright (C) 2018 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.util.DisplayUtils; 22 import android.view.Display; 23 import android.view.DisplayInfo; 24 import android.view.RoundedCorners; 25 26 import com.android.internal.R; 27 28 /** 29 * Utility functions for screen decorations used by both window manager and System UI. 30 */ 31 public class ScreenDecorationsUtils { 32 33 /** 34 * Corner radius that should be used on windows in order to cover the display. 35 * These values are expressed in pixels because they should not respect display or font 36 * scaling, this means that we don't have to reload them on config changes. 37 * 38 * Note that if the context is not an UI context(not associated with Display), it will use 39 * default display. 40 * 41 * If the associated display is not internal, will return 0. 42 */ getWindowCornerRadius(Context context)43 public static float getWindowCornerRadius(Context context) { 44 final Resources resources = context.getResources(); 45 if (!supportsRoundedCornersOnWindows(resources)) { 46 return 0f; 47 } 48 // Use Context#getDisplayNoVerify() in case the context is not an UI context. 49 final Display display = context.getDisplayNoVerify(); 50 // The radius is only valid for internal displays, since the corner radius of external or 51 // virtual displays is not known when window corners are configured or are not supported. 52 if (display.getType() != Display.TYPE_INTERNAL) { 53 return 0f; 54 } 55 final String displayUniqueId = display.getUniqueId(); 56 // Radius that should be used in case top or bottom aren't defined. 57 float defaultRadius = RoundedCorners.getRoundedCornerRadius(resources, displayUniqueId) 58 - RoundedCorners.getRoundedCornerRadiusAdjustment(resources, displayUniqueId); 59 60 float topRadius = RoundedCorners.getRoundedCornerTopRadius(resources, displayUniqueId) 61 - RoundedCorners.getRoundedCornerRadiusTopAdjustment(resources, displayUniqueId); 62 if (topRadius == 0f) { 63 topRadius = defaultRadius; 64 } 65 float bottomRadius = RoundedCorners.getRoundedCornerBottomRadius(resources, displayUniqueId) 66 - RoundedCorners.getRoundedCornerRadiusBottomAdjustment(resources, displayUniqueId); 67 if (bottomRadius == 0f) { 68 bottomRadius = defaultRadius; 69 } 70 71 // If the physical pixels are scaled, apply it here 72 float scale = getPhysicalPixelDisplaySizeRatio(context); 73 if (scale != 1f) { 74 topRadius = topRadius * scale; 75 bottomRadius = bottomRadius * scale; 76 } 77 78 // Always use the smallest radius to make sure the rounded corners will 79 // completely cover the display. 80 return Math.min(topRadius, bottomRadius); 81 } 82 getPhysicalPixelDisplaySizeRatio(Context context)83 static float getPhysicalPixelDisplaySizeRatio(Context context) { 84 DisplayInfo displayInfo = new DisplayInfo(); 85 context.getDisplay().getDisplayInfo(displayInfo); 86 final Display.Mode maxDisplayMode = 87 DisplayUtils.getMaximumResolutionDisplayMode(displayInfo.supportedModes); 88 if (maxDisplayMode == null) { 89 return 1f; 90 } 91 return DisplayUtils.getPhysicalPixelDisplaySizeRatio( 92 maxDisplayMode.getPhysicalWidth(), maxDisplayMode.getPhysicalHeight(), 93 displayInfo.getNaturalWidth(), displayInfo.getNaturalHeight()); 94 } 95 96 /** 97 * If live rounded corners are supported on windows. 98 */ supportsRoundedCornersOnWindows(Resources resources)99 public static boolean supportsRoundedCornersOnWindows(Resources resources) { 100 return resources.getBoolean(R.bool.config_supportsRoundedCornersOnWindows); 101 } 102 } 103