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      * Note: The screen size is getting from {@link #getScreenSize}.
78      */
getScreenAspectRatio(Context context)79     public float getScreenAspectRatio(Context context) {
80         final WindowManager windowManager = context.getSystemService(WindowManager.class);
81         final Point screenSize = getScreenSize(windowManager.getDefaultDisplay());
82         return (float) screenSize.y / screenSize.x;
83     }
84 
85     /**
86      * Calculates the device's screen height.
87      * Note: The screen size is getting from {@link #getScreenSize}.
88      */
getScreenHeight(Context context)89     public int getScreenHeight(Context context) {
90         final WindowManager windowManager = context.getSystemService(WindowManager.class);
91         final Point screenSize = getScreenSize(windowManager.getDefaultDisplay());
92         return screenSize.y;
93     }
94 
getPortraitScreenSize(Display display)95     private Point getPortraitScreenSize(Display display) {
96         if (mPortraitScreenSize == null) {
97             mPortraitScreenSize = new Point();
98         }
99         writeDisplaySizeToPoint(display, mPortraitScreenSize);
100         return new Point(mPortraitScreenSize);
101     }
102 
getLandscapeScreenSize(Display display)103     private Point getLandscapeScreenSize(Display display) {
104         if (mLandscapeScreenSize == null) {
105             mLandscapeScreenSize = new Point();
106         }
107         writeDisplaySizeToPoint(display, mLandscapeScreenSize);
108         return new Point(mLandscapeScreenSize);
109     }
110 
111     /**
112      * Writes the screen size of the provided display object to the provided Point object.
113      */
writeDisplaySizeToPoint(Display display, Point outPoint)114     private void writeDisplaySizeToPoint(Display display, Point outPoint) {
115         if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
116             display.getRealSize(outPoint);
117         } else {
118             display.getSize(outPoint);
119         }
120     }
121 }
122