• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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;
18 
19 import android.util.DisplayMetrics;
20 
21 public class Display
22 {
23     /**
24      * Specify the default Display
25      */
26     public static final int DEFAULT_DISPLAY = 0;
27 
28 
29     /**
30      * Use {@link android.view.WindowManager#getDefaultDisplay()
31      * WindowManager.getDefaultDisplay()} to create a Display object.
32      * Display gives you access to some information about a particular display
33      * connected to the device.
34      */
Display(int display)35     Display(int display) {
36         // initalize the statics when this class is first instansiated. This is
37         // done here instead of in the static block because Zygote
38         synchronized (mStaticInit) {
39             if (!mInitialized) {
40                 nativeClassInit();
41                 mInitialized = true;
42             }
43         }
44         mDisplay = display;
45         init(display);
46     }
47 
48     /**
49      * Returns the index of this display.  This is currently undefined; do
50      * not use.
51      */
getDisplayId()52     public int getDisplayId() {
53         return mDisplay;
54     }
55 
56     /**
57      * Returns the number of displays connected to the device.  This is
58      * currently undefined; do not use.
59      */
getDisplayCount()60     native static int getDisplayCount();
61 
62     /**
63      * Returns the raw width of the display, in pixels.  Note that this
64      * should <em>not</em> generally be used for computing layouts, since
65      * a device will typically have screen decoration (such as a status bar)
66      * along the edges of the display that reduce the amount of application
67      * space available from the raw size returned here.  This value is
68      * adjusted for you based on the current rotation of the display.
69      */
getWidth()70     native public int getWidth();
71 
72     /**
73      * Returns the raw height of the display, in pixels.  Note that this
74      * should <em>not</em> generally be used for computing layouts, since
75      * a device will typically have screen decoration (such as a status bar)
76      * along the edges of the display that reduce the amount of application
77      * space available from the raw size returned here.  This value is
78      * adjusted for you based on the current rotation of the display.
79      */
getHeight()80     native public int getHeight();
81 
82     /**
83      * Returns the rotation of the screen from its "natural" orientation.
84      * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
85      * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
86      * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
87      * {@link Surface#ROTATION_270 Surface.ROTATION_270}.  For
88      * example, if a device has a naturally tall screen, and the user has
89      * turned it on its side to go into a landscape orientation, the value
90      * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
91      * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
92      * the direction it was turned.  The angle is the rotation of the drawn
93      * graphics on the screen, which is the opposite direction of the physical
94      * rotation of the device.  For example, if the device is rotated 90
95      * degrees counter-clockwise, to compensate rendering will be rotated by
96      * 90 degrees clockwise and thus the returned value here will be
97      * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
98      */
getRotation()99     public int getRotation() {
100         return getOrientation();
101     }
102 
103     /**
104      * @deprecated use {@link #getRotation}
105      * @return orientation of this display.
106      */
getOrientation()107     @Deprecated native public int getOrientation();
108 
109     /**
110      * Return the native pixel format of the display.  The returned value
111      * may be one of the constants int {@link android.graphics.PixelFormat}.
112      */
getPixelFormat()113     public int getPixelFormat() {
114         return mPixelFormat;
115     }
116 
117     /**
118      * Return the refresh rate of this display in frames per second.
119      */
getRefreshRate()120     public float getRefreshRate() {
121         return mRefreshRate;
122     }
123 
124     /**
125      * Initialize a DisplayMetrics object from this display's data.
126      *
127      * @param outMetrics
128      */
getMetrics(DisplayMetrics outMetrics)129     public void getMetrics(DisplayMetrics outMetrics) {
130         outMetrics.widthPixels  = getWidth();
131         outMetrics.heightPixels = getHeight();
132         outMetrics.density      = mDensity;
133         outMetrics.densityDpi   = (int)((mDensity*DisplayMetrics.DENSITY_DEFAULT)+.5f);
134         outMetrics.scaledDensity= outMetrics.density;
135         outMetrics.xdpi         = mDpiX;
136         outMetrics.ydpi         = mDpiY;
137     }
138 
139     /*
140      * We use a class initializer to allow the native code to cache some
141      * field offsets.
142      */
nativeClassInit()143     native private static void nativeClassInit();
144 
init(int display)145     private native void init(int display);
146 
147     private int         mDisplay;
148     // Following fields are initialized from native code
149     private int         mPixelFormat;
150     private float       mRefreshRate;
151     private float       mDensity;
152     private float       mDpiX;
153     private float       mDpiY;
154 
155     private static final Object mStaticInit = new Object();
156     private static boolean mInitialized = false;
157 
158     /**
159      * Returns a display object which uses the metric's width/height instead.
160      * @hide
161      */
createMetricsBasedDisplay(int displayId, DisplayMetrics metrics)162     public static Display createMetricsBasedDisplay(int displayId, DisplayMetrics metrics) {
163         return new CompatibleDisplay(displayId, metrics);
164     }
165 
166     private static class CompatibleDisplay extends Display {
167         private final DisplayMetrics mMetrics;
168 
CompatibleDisplay(int displayId, DisplayMetrics metrics)169         private CompatibleDisplay(int displayId, DisplayMetrics metrics) {
170             super(displayId);
171             mMetrics = metrics;
172         }
173 
174         @Override
getWidth()175         public int getWidth() {
176             return mMetrics.widthPixels;
177         }
178 
179         @Override
getHeight()180         public int getHeight() {
181             return mMetrics.heightPixels;
182         }
183     }
184 }
185 
186