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      */
getWindowCornerRadius(Context context)41     public static float getWindowCornerRadius(Context context) {
42         final Resources resources = context.getResources();
43         if (!supportsRoundedCornersOnWindows(resources)) {
44             return 0f;
45         }
46         // Use Context#getDisplayNoVerify() in case the context is not an UI context.
47         final String displayUniqueId = context.getDisplayNoVerify().getUniqueId();
48         // Radius that should be used in case top or bottom aren't defined.
49         float defaultRadius = RoundedCorners.getRoundedCornerRadius(resources, displayUniqueId)
50                 - RoundedCorners.getRoundedCornerRadiusAdjustment(resources, displayUniqueId);
51 
52         float topRadius = RoundedCorners.getRoundedCornerTopRadius(resources, displayUniqueId)
53                 - RoundedCorners.getRoundedCornerRadiusTopAdjustment(resources, displayUniqueId);
54         if (topRadius == 0f) {
55             topRadius = defaultRadius;
56         }
57         float bottomRadius = RoundedCorners.getRoundedCornerBottomRadius(resources, displayUniqueId)
58                 - RoundedCorners.getRoundedCornerRadiusBottomAdjustment(resources, displayUniqueId);
59         if (bottomRadius == 0f) {
60             bottomRadius = defaultRadius;
61         }
62 
63         // If the physical pixels are scaled, apply it here
64         float scale = getPhysicalPixelDisplaySizeRatio(context);
65         if (scale != 1f) {
66             topRadius = topRadius * scale;
67             bottomRadius = bottomRadius * scale;
68         }
69 
70         // Always use the smallest radius to make sure the rounded corners will
71         // completely cover the display.
72         return Math.min(topRadius, bottomRadius);
73     }
74 
getPhysicalPixelDisplaySizeRatio(Context context)75     static float getPhysicalPixelDisplaySizeRatio(Context context) {
76         DisplayInfo displayInfo = new DisplayInfo();
77         context.getDisplay().getDisplayInfo(displayInfo);
78         final Display.Mode maxDisplayMode =
79                 DisplayUtils.getMaximumResolutionDisplayMode(displayInfo.supportedModes);
80         if (maxDisplayMode == null) {
81             return 1f;
82         }
83         return DisplayUtils.getPhysicalPixelDisplaySizeRatio(
84                 maxDisplayMode.getPhysicalWidth(), maxDisplayMode.getPhysicalHeight(),
85                 displayInfo.getNaturalWidth(), displayInfo.getNaturalHeight());
86     }
87 
88     /**
89      * If live rounded corners are supported on windows.
90      */
supportsRoundedCornersOnWindows(Resources resources)91     public static boolean supportsRoundedCornersOnWindows(Resources resources) {
92         return resources.getBoolean(R.bool.config_supportsRoundedCornersOnWindows);
93     }
94 }
95