1 /* 2 * Copyright (C) 2020 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.view.cts.util; 18 19 import android.app.Activity; 20 import android.content.pm.PackageManager; 21 import android.content.res.Resources; 22 import android.util.DisplayMetrics; 23 24 /** Utilities class for querying display info. */ 25 public class DisplayUtils { 26 27 /** Whether the device supports orientation request from apps. */ supportOrientationRequest(final Activity activity)28 public static boolean supportOrientationRequest(final Activity activity) { 29 final PackageManager pm = activity.getPackageManager(); 30 return pm.hasSystemFeature(PackageManager.FEATURE_SCREEN_LANDSCAPE) 31 && pm.hasSystemFeature(PackageManager.FEATURE_SCREEN_PORTRAIT) 32 && !isCloseToSquareDisplay(activity); 33 } 34 35 /** Checks whether the display dimension is close to square. */ isCloseToSquareDisplay(final Activity activity)36 public static boolean isCloseToSquareDisplay(final Activity activity) { 37 final Resources resources = activity.getResources(); 38 final float closeToSquareMaxAspectRatio; 39 try { 40 closeToSquareMaxAspectRatio = resources.getFloat(resources.getIdentifier( 41 "config_closeToSquareDisplayMaxAspectRatio", "dimen", "android")); 42 } catch (Resources.NotFoundException e) { 43 // Assume device is not close to square. 44 return false; 45 } 46 final DisplayMetrics displayMetrics = new DisplayMetrics(); 47 activity.getDisplay().getRealMetrics(displayMetrics); 48 final int w = displayMetrics.widthPixels; 49 final int h = displayMetrics.heightPixels; 50 final float aspectRatio = Math.max(w, h) / (float) Math.min(w, h); 51 return aspectRatio <= closeToSquareMaxAspectRatio; 52 } 53 54 /** Whether the device is in portrait. */ isDevicePortrait(final Activity activity)55 public static boolean isDevicePortrait(final Activity activity) { 56 final DisplayMetrics displayMetrics = new DisplayMetrics(); 57 activity.getDisplay().getRealMetrics(displayMetrics); 58 return displayMetrics.widthPixels < displayMetrics.heightPixels; 59 } 60 DisplayUtils()61 private DisplayUtils() {} 62 } 63