1 /* 2 * Copyright (C) 2017 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 package com.android.wallpaper.util; 17 18 import android.content.Context; 19 import android.content.res.Configuration; 20 import android.content.res.Resources; 21 import android.graphics.Point; 22 import android.os.Build.VERSION; 23 import android.os.Build.VERSION_CODES; 24 import android.util.Log; 25 import android.view.Display; 26 import android.view.WindowManager; 27 28 /** 29 * Calculates the size of the device's screen. 30 */ 31 public class ScreenSizeCalculator { 32 33 private static final String TAG = "ScreenSizeCalculator"; 34 35 private static ScreenSizeCalculator sInstance; 36 37 private Point mPortraitScreenSize; 38 private Point mLandscapeScreenSize; 39 getInstance()40 public static ScreenSizeCalculator getInstance() { 41 if (sInstance == null) { 42 sInstance = new ScreenSizeCalculator(); 43 } 44 return sInstance; 45 } 46 47 /** 48 * Clears the static instance of ScreenSizeCalculator. Used in test when display metrics are 49 * manipulated between test cases. 50 */ clearInstance()51 static void clearInstance() { 52 sInstance = null; 53 } 54 55 /** 56 * Calculates the device's screen size, in physical pixels. 57 * 58 * @return Screen size unadjusted for window decor or compatibility scale factors if API level is 59 * 17+, otherwise return adjusted screen size. In both cases, returns size in units of 60 * physical pixels. 61 */ getScreenSize(Display display)62 public Point getScreenSize(Display display) { 63 switch (Resources.getSystem().getConfiguration().orientation) { 64 case Configuration.ORIENTATION_PORTRAIT: 65 return getPortraitScreenSize(display); 66 case Configuration.ORIENTATION_LANDSCAPE: 67 return getLandscapeScreenSize(display); 68 default: 69 Log.e(TAG, "Unknown device orientation: " 70 + Resources.getSystem().getConfiguration().orientation); 71 return getPortraitScreenSize(display); 72 } 73 } 74 75 /** 76 * Calculates the device's aspect ratio (height/width). 77 * 78 * Note: The screen size is getting from {@link #getScreenSize}. 79 */ getScreenAspectRatio(Context context)80 public float getScreenAspectRatio(Context context) { 81 final WindowManager windowManager = context.getSystemService(WindowManager.class); 82 final Point screenSize = getScreenSize(windowManager.getDefaultDisplay()); 83 return (float) screenSize.y / screenSize.x; 84 } 85 getPortraitScreenSize(Display display)86 private Point getPortraitScreenSize(Display display) { 87 if (mPortraitScreenSize == null) { 88 mPortraitScreenSize = new Point(); 89 } 90 writeDisplaySizeToPoint(display, mPortraitScreenSize); 91 return mPortraitScreenSize; 92 } 93 getLandscapeScreenSize(Display display)94 private Point getLandscapeScreenSize(Display display) { 95 if (mLandscapeScreenSize == null) { 96 mLandscapeScreenSize = new Point(); 97 } 98 writeDisplaySizeToPoint(display, mLandscapeScreenSize); 99 return mLandscapeScreenSize; 100 } 101 102 /** 103 * Writes the screen size of the provided display object to the provided Point object. 104 */ writeDisplaySizeToPoint(Display display, Point outPoint)105 private void writeDisplaySizeToPoint(Display display, Point outPoint) { 106 if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { 107 display.getRealSize(outPoint); 108 } else { 109 display.getSize(outPoint); 110 } 111 } 112 } 113