1 /*
2  * Copyright (C) 2019 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.systemui.assist.ui;
18 
19 import android.content.Context;
20 import android.util.DisplayMetrics;
21 import android.view.Display;
22 import android.view.Surface;
23 
24 import com.android.systemui.R;
25 
26 /**
27  * Utility class for determining screen and corner dimensions.
28  */
29 public class DisplayUtils {
30     /**
31      * Converts given distance from dp to pixels.
32      */
convertDpToPx(float dp, Context context)33     public static int convertDpToPx(float dp, Context context) {
34         Display d = context.getDisplay();
35 
36         DisplayMetrics dm = new DisplayMetrics();
37         d.getRealMetrics(dm);
38 
39         return (int) Math.ceil(dp * dm.density);
40     }
41 
42     /**
43      * The width of the display.
44      *
45      * - Not affected by rotation.
46      * - Includes system decor.
47      */
getWidth(Context context)48     public static int getWidth(Context context) {
49         Display d = context.getDisplay();
50 
51         DisplayMetrics dm = new DisplayMetrics();
52         d.getRealMetrics(dm);
53 
54         int rotation = d.getRotation();
55         if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
56             return dm.widthPixels;
57         } else {
58             return dm.heightPixels;
59         }
60     }
61 
62     /**
63      * The height of the display.
64      *
65      * - Not affected by rotation.
66      * - Includes system decor.
67      */
getHeight(Context context)68     public static int getHeight(Context context) {
69         Display d = context.getDisplay();
70 
71         DisplayMetrics dm = new DisplayMetrics();
72         d.getRealMetrics(dm);
73 
74         int rotation = d.getRotation();
75         if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
76             return dm.heightPixels;
77         } else {
78             return dm.widthPixels;
79         }
80     }
81 
82     /**
83      * Returns the radius of the bottom corners (the distance from the true corner to the point
84      * where the curve ends), in pixels.
85      */
getCornerRadiusBottom(Context context)86     public static int getCornerRadiusBottom(Context context) {
87         int radius = context.getResources().getDimensionPixelSize(
88                 R.dimen.config_rounded_mask_size_bottom);
89 
90         if (radius == 0) {
91             radius = getCornerRadiusDefault(context);
92         }
93 
94         return radius;
95     }
96 
97     /**
98      * Returns the radius of the top corners (the distance from the true corner to the point where
99      * the curve ends), in pixels.
100      */
getCornerRadiusTop(Context context)101     public static int getCornerRadiusTop(Context context) {
102         int radius = context.getResources().getDimensionPixelSize(
103                 R.dimen.config_rounded_mask_size_top);
104 
105         if (radius == 0) {
106             radius = getCornerRadiusDefault(context);
107         }
108 
109         return radius;
110     }
111 
getCornerRadiusDefault(Context context)112     private static int getCornerRadiusDefault(Context context) {
113         return context.getResources().getDimensionPixelSize(R.dimen.config_rounded_mask_size);
114     }
115 }
116