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