blob: 89767f2a13e18753a429ab2064a09b42bc817382 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.view;
18
19import android.util.DisplayMetrics;
20
21public class Display
22{
23 /**
24 * Specify the default Display
25 */
26 public static final int DEFAULT_DISPLAY = 0;
27
28
29 /**
Mathias Agopian833533c2010-08-16 13:28:55 -070030 * Use {@link android.view.WindowManager#getDefaultDisplay()
31 * WindowManager.getDefaultDisplay()} to create a Display object.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 * Display gives you access to some information about a particular display
33 * connected to the device.
34 */
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 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -080049 * Returns the index of this display. This is currently undefined; do
50 * not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 */
52 public int getDisplayId() {
53 return mDisplay;
54 }
55
56 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -080057 * Returns the number of displays connected to the device. This is
58 * currently undefined; do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 */
60 native static int getDisplayCount();
61
62 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -080063 * 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.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 */
70 native public int getWidth();
71
72 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -080073 * 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.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 */
80 native public int getHeight();
81
Dianne Hackborn99aac7b2011-02-25 17:33:02 -080082 /** @hide special for when we are faking the screen size. */
83 native public int getRealWidth();
84 /** @hide special for when we are faking the screen size. */
85 native public int getRealHeight();
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -080088 * Returns the rotation of the screen from its "natural" orientation.
89 * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
90 * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
91 * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
92 * {@link Surface#ROTATION_270 Surface.ROTATION_270}. For
93 * example, if a device has a naturally tall screen, and the user has
94 * turned it on its side to go into a landscape orientation, the value
95 * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
96 * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
97 * the direction it was turned. The angle is the rotation of the drawn
98 * graphics on the screen, which is the opposite direction of the physical
99 * rotation of the device. For example, if the device is rotated 90
100 * degrees counter-clockwise, to compensate rendering will be rotated by
101 * 90 degrees clockwise and thus the returned value here will be
102 * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
103 */
104 public int getRotation() {
105 return getOrientation();
106 }
107
108 /**
Joe Onorato4c904a32010-02-26 12:35:55 -0800109 * @deprecated use {@link #getRotation}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * @return orientation of this display.
111 */
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800112 @Deprecated native public int getOrientation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800115 * Return the native pixel format of the display. The returned value
116 * may be one of the constants int {@link android.graphics.PixelFormat}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 */
118 public int getPixelFormat() {
119 return mPixelFormat;
120 }
121
122 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800123 * Return the refresh rate of this display in frames per second.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 */
125 public float getRefreshRate() {
126 return mRefreshRate;
127 }
128
129 /**
130 * Initialize a DisplayMetrics object from this display's data.
131 *
132 * @param outMetrics
133 */
134 public void getMetrics(DisplayMetrics outMetrics) {
135 outMetrics.widthPixels = getWidth();
136 outMetrics.heightPixels = getHeight();
137 outMetrics.density = mDensity;
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700138 outMetrics.densityDpi = (int)((mDensity*DisplayMetrics.DENSITY_DEFAULT)+.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 outMetrics.scaledDensity= outMetrics.density;
140 outMetrics.xdpi = mDpiX;
141 outMetrics.ydpi = mDpiY;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400142
143 outMetrics.realWidthPixels = outMetrics.widthPixels;
144 outMetrics.realHeightPixels = outMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 }
146
147 /*
148 * We use a class initializer to allow the native code to cache some
149 * field offsets.
150 */
151 native private static void nativeClassInit();
152
153 private native void init(int display);
154
155 private int mDisplay;
156 // Following fields are initialized from native code
157 private int mPixelFormat;
158 private float mRefreshRate;
159 private float mDensity;
160 private float mDpiX;
161 private float mDpiY;
162
163 private static final Object mStaticInit = new Object();
164 private static boolean mInitialized = false;
Mitsuru Oshimaddd12532009-07-14 10:41:13 -0700165
166 /**
167 * Returns a display object which uses the metric's width/height instead.
168 * @hide
169 */
170 public static Display createMetricsBasedDisplay(int displayId, DisplayMetrics metrics) {
171 return new CompatibleDisplay(displayId, metrics);
172 }
173
174 private static class CompatibleDisplay extends Display {
175 private final DisplayMetrics mMetrics;
176
177 private CompatibleDisplay(int displayId, DisplayMetrics metrics) {
178 super(displayId);
179 mMetrics = metrics;
180 }
181
182 @Override
183 public int getWidth() {
184 return mMetrics.widthPixels;
185 }
186
187 @Override
188 public int getHeight() {
189 return mMetrics.heightPixels;
190 }
191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192}
193