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