• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.util;
18 
19 import android.content.res.Resources;
20 import android.view.Display;
21 
22 import com.android.internal.R;
23 
24 /**
25  * Utils for loading display related resources and calculations.
26  *
27  * @hide
28  */
29 public class DisplayUtils {
30 
31     /**
32      * Gets the index of the given display unique id in {@link R.array#config_displayUniqueIdArray}
33      * which is used to get the related cutout configs for that display.
34      *
35      * For multi-display device, {@link R.array#config_displayUniqueIdArray} should be set for each
36      * display if there are different type of cutouts on each display.
37      * For single display device, {@link R.array#config_displayUniqueIdArray} should not to be set
38      * and the system will load the default configs for main built-in display.
39      */
getDisplayUniqueIdConfigIndex(Resources res, String displayUniqueId)40     public static int getDisplayUniqueIdConfigIndex(Resources res, String displayUniqueId) {
41         int index = -1;
42         if (displayUniqueId == null || displayUniqueId.isEmpty()) {
43             return index;
44         }
45         final String[] ids = res.getStringArray(R.array.config_displayUniqueIdArray);
46         final int size = ids.length;
47         for (int i = 0; i < size; i++) {
48             if (displayUniqueId.equals(ids[i])) {
49                 index = i;
50                 break;
51             }
52         }
53         return index;
54     }
55 
56     /**
57      * Returns the Display.Mode with maximum resolution.
58      */
getMaximumResolutionDisplayMode(Display.Mode[] modes)59     public static Display.Mode getMaximumResolutionDisplayMode(Display.Mode[] modes) {
60         if (modes == null || modes.length == 0) {
61             return null;
62         }
63         int maxWidth = 0;
64         Display.Mode target = null;
65         for (Display.Mode mode : modes) {
66             if (mode.getPhysicalWidth() > maxWidth) {
67                 maxWidth = mode.getPhysicalWidth();
68                 target = mode;
69             }
70         }
71         return target;
72     }
73 
74     /**
75      * Get the display size ratio based on the physical display size.
76      */
getPhysicalPixelDisplaySizeRatio( int physicalWidth, int physicalHeight, int currentWidth, int currentHeight)77     public static float getPhysicalPixelDisplaySizeRatio(
78             int physicalWidth, int physicalHeight, int currentWidth, int currentHeight) {
79         if (physicalWidth == currentWidth && physicalHeight == currentHeight) {
80             return 1f;
81         }
82         final float widthRatio = (float) currentWidth / physicalWidth;
83         final float heightRatio = (float) currentHeight / physicalHeight;
84         return Math.min(widthRatio, heightRatio);
85     }
86 }
87