blob: 80325463545fd370bfb67a5662da8728014162f8 [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
Dianne Hackborn5be8de32011-05-24 18:11:57 -070019import android.content.res.CompatibilityInfo;
Dianne Hackbornac8dea12011-04-20 18:18:51 -070020import android.graphics.Point;
21import android.graphics.Rect;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.util.DisplayMetrics;
Dianne Hackbornac8dea12011-04-20 18:18:51 -070026import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Dianne Hackbornac8dea12011-04-20 18:18:51 -070028public class Display {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 /**
30 * Specify the default Display
31 */
32 public static final int DEFAULT_DISPLAY = 0;
33
34
35 /**
Mathias Agopian833533c2010-08-16 13:28:55 -070036 * Use {@link android.view.WindowManager#getDefaultDisplay()
37 * WindowManager.getDefaultDisplay()} to create a Display object.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 * Display gives you access to some information about a particular display
39 * connected to the device.
40 */
Dianne Hackborn5be8de32011-05-24 18:11:57 -070041 Display(int display, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 // initalize the statics when this class is first instansiated. This is
43 // done here instead of in the static block because Zygote
Dianne Hackbornac8dea12011-04-20 18:18:51 -070044 synchronized (sStaticInit) {
45 if (!sInitialized) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 nativeClassInit();
Dianne Hackbornac8dea12011-04-20 18:18:51 -070047 sInitialized = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 }
49 }
Dianne Hackborn5be8de32011-05-24 18:11:57 -070050 if (compatInfo != null && (compatInfo.isScalingRequired()
51 || !compatInfo.supportsScreen())) {
52 mCompatibilityInfo = compatInfo;
53 } else {
54 mCompatibilityInfo = null;
55 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 mDisplay = display;
57 init(display);
58 }
59
60 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -080061 * Returns the index of this display. This is currently undefined; do
62 * not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 */
64 public int getDisplayId() {
65 return mDisplay;
66 }
67
68 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -080069 * Returns the number of displays connected to the device. This is
70 * currently undefined; do not use.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 */
72 native static int getDisplayCount();
73
74 /**
Dianne Hackbornac8dea12011-04-20 18:18:51 -070075 * Returns the raw size of the display, in pixels. Note that this
Dianne Hackborn5cb70b52010-02-25 17:01:14 -080076 * should <em>not</em> generally be used for computing layouts, since
77 * a device will typically have screen decoration (such as a status bar)
78 * along the edges of the display that reduce the amount of application
79 * space available from the raw size returned here. This value is
80 * adjusted for you based on the current rotation of the display.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 */
Dianne Hackbornac8dea12011-04-20 18:18:51 -070082 public void getSize(Point outSize) {
Dianne Hackborn81e56d52011-05-26 00:55:58 -070083 getSizeInternal(outSize, true);
84 }
85
86 /**
87 * Returns the raw size of the display, in pixels. Note that this
88 * should <em>not</em> generally be used for computing layouts, since
89 * a device will typically have screen decoration (such as a status bar)
90 * along the edges of the display that reduce the amount of application
91 * space available from the raw size returned here. This value is
92 * adjusted for you based on the current rotation of the display.
93 */
94 private void getSizeInternal(Point outSize, boolean doCompat) {
Dianne Hackbornac8dea12011-04-20 18:18:51 -070095 try {
96 IWindowManager wm = getWindowManager();
97 if (wm != null) {
98 wm.getDisplaySize(outSize);
Dianne Hackborn81e56d52011-05-26 00:55:58 -070099 if (doCompat && mCompatibilityInfo != null) {
100 synchronized (mTmpMetrics) {
101 mTmpMetrics.unscaledWidthPixels = outSize.x;
102 mTmpMetrics.unscaledHeightPixels = outSize.y;
103 mTmpMetrics.density = mDensity;
104 mCompatibilityInfo.applyToDisplayMetrics(mTmpMetrics);
105 outSize.x = mTmpMetrics.widthPixels;
106 outSize.y = mTmpMetrics.heightPixels;
107 }
108 }
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700109 } else {
110 // This is just for boot-strapping, initializing the
111 // system process before the window manager is up.
112 outSize.y = getRealHeight();
113 }
114 } catch (RemoteException e) {
115 Slog.w("Display", "Unable to get display size", e);
116 }
117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
119 /**
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700120 * This is just easier for some parts of the framework.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700122 public void getRectSize(Rect outSize) {
123 synchronized (mTmpPoint) {
Dianne Hackborn81e56d52011-05-26 00:55:58 -0700124 getSizeInternal(mTmpPoint, true);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700125 outSize.set(0, 0, mTmpPoint.x, mTmpPoint.y);
126 }
127 }
128
129 /**
130 * Return the maximum screen size dimension that will happen. This is
131 * mostly for wallpapers.
132 * @hide
133 */
134 public int getMaximumSizeDimension() {
135 try {
136 IWindowManager wm = getWindowManager();
137 return wm.getMaximumSizeDimension();
138 } catch (RemoteException e) {
139 Slog.w("Display", "Unable to get display maximum size dimension", e);
140 return 0;
141 }
142 }
143
144 /**
145 * @deprecated Use {@link #getSize(Point)} instead.
146 */
147 @Deprecated
148 public int getWidth() {
149 synchronized (mTmpPoint) {
150 long now = SystemClock.uptimeMillis();
151 if (now > (mLastGetTime+20)) {
Dianne Hackborn81e56d52011-05-26 00:55:58 -0700152 getSizeInternal(mTmpPoint, true);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700153 mLastGetTime = now;
154 }
155 return mTmpPoint.x;
156 }
157 }
158
159 /**
160 * @deprecated Use {@link #getSize(Point)} instead.
161 */
162 @Deprecated
163 public int getHeight() {
164 synchronized (mTmpPoint) {
165 long now = SystemClock.uptimeMillis();
166 if (now > (mLastGetTime+20)) {
Dianne Hackborn81e56d52011-05-26 00:55:58 -0700167 getSizeInternal(mTmpPoint, true);
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700168 mLastGetTime = now;
169 }
170 return mTmpPoint.y;
171 }
172 }
173
174 /** @hide Returns the actual screen size, not including any decor. */
175 native public int getRealWidth();
176 /** @hide Returns the actual screen size, not including any decor. */
177 native public int getRealHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800179 /** @hide special for when we are faking the screen size. */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700180 native public int getRawWidth();
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800181 /** @hide special for when we are faking the screen size. */
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700182 native public int getRawHeight();
Dianne Hackborn99aac7b2011-02-25 17:33:02 -0800183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800185 * Returns the rotation of the screen from its "natural" orientation.
186 * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
187 * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
188 * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
189 * {@link Surface#ROTATION_270 Surface.ROTATION_270}. For
190 * example, if a device has a naturally tall screen, and the user has
191 * turned it on its side to go into a landscape orientation, the value
192 * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
193 * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
194 * the direction it was turned. The angle is the rotation of the drawn
195 * graphics on the screen, which is the opposite direction of the physical
196 * rotation of the device. For example, if the device is rotated 90
197 * degrees counter-clockwise, to compensate rendering will be rotated by
198 * 90 degrees clockwise and thus the returned value here will be
199 * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
200 */
201 public int getRotation() {
202 return getOrientation();
203 }
204
205 /**
Joe Onorato4c904a32010-02-26 12:35:55 -0800206 * @deprecated use {@link #getRotation}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * @return orientation of this display.
208 */
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800209 @Deprecated native public int getOrientation();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
211 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800212 * Return the native pixel format of the display. The returned value
213 * may be one of the constants int {@link android.graphics.PixelFormat}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 */
215 public int getPixelFormat() {
216 return mPixelFormat;
217 }
218
219 /**
Dianne Hackborn5cb70b52010-02-25 17:01:14 -0800220 * Return the refresh rate of this display in frames per second.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 */
222 public float getRefreshRate() {
223 return mRefreshRate;
224 }
225
226 /**
227 * Initialize a DisplayMetrics object from this display's data.
228 *
229 * @param outMetrics
230 */
231 public void getMetrics(DisplayMetrics outMetrics) {
Dianne Hackborn68066c22011-04-21 17:26:39 -0700232 synchronized (mTmpPoint) {
Dianne Hackborn81e56d52011-05-26 00:55:58 -0700233 getSizeInternal(mTmpPoint, false);
Dianne Hackborn68066c22011-04-21 17:26:39 -0700234 outMetrics.widthPixels = mTmpPoint.x;
235 outMetrics.heightPixels = mTmpPoint.y;
236 }
237 getNonSizeMetrics(outMetrics);
Dianne Hackborn5be8de32011-05-24 18:11:57 -0700238
239 if (mCompatibilityInfo != null) {
240 mCompatibilityInfo.applyToDisplayMetrics(outMetrics);
241 }
Dianne Hackborn68066c22011-04-21 17:26:39 -0700242 }
243
244 /**
245 * Initialize a DisplayMetrics object from this display's data.
246 *
247 * @param outMetrics
248 * @hide
249 */
250 public void getRealMetrics(DisplayMetrics outMetrics) {
251 outMetrics.widthPixels = getRealWidth();
252 outMetrics.heightPixels = getRealHeight();
253 getNonSizeMetrics(outMetrics);
254 }
255
256 private void getNonSizeMetrics(DisplayMetrics outMetrics) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 outMetrics.density = mDensity;
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700258 outMetrics.densityDpi = (int)((mDensity*DisplayMetrics.DENSITY_DEFAULT)+.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 outMetrics.scaledDensity= outMetrics.density;
260 outMetrics.xdpi = mDpiX;
261 outMetrics.ydpi = mDpiY;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400262
Dianne Hackborn81e56d52011-05-26 00:55:58 -0700263 outMetrics.unscaledWidthPixels = outMetrics.widthPixels;
264 outMetrics.unscaledHeightPixels = outMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
266
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700267 static IWindowManager getWindowManager() {
268 synchronized (sStaticInit) {
269 if (sWindowManager == null) {
270 sWindowManager = IWindowManager.Stub.asInterface(
271 ServiceManager.getService("window"));
272 }
273 return sWindowManager;
274 }
275 }
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /*
278 * We use a class initializer to allow the native code to cache some
279 * field offsets.
280 */
281 native private static void nativeClassInit();
282
283 private native void init(int display);
284
Dianne Hackborn5be8de32011-05-24 18:11:57 -0700285 private final CompatibilityInfo mCompatibilityInfo;
286 private final int mDisplay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 // Following fields are initialized from native code
288 private int mPixelFormat;
289 private float mRefreshRate;
290 private float mDensity;
291 private float mDpiX;
292 private float mDpiY;
293
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700294 private final Point mTmpPoint = new Point();
Dianne Hackborn5be8de32011-05-24 18:11:57 -0700295 private final DisplayMetrics mTmpMetrics = new DisplayMetrics();
Dianne Hackbornac8dea12011-04-20 18:18:51 -0700296 private float mLastGetTime;
297
298 private static final Object sStaticInit = new Object();
299 private static boolean sInitialized = false;
300 private static IWindowManager sWindowManager;
Mitsuru Oshimaddd12532009-07-14 10:41:13 -0700301
302 /**
303 * Returns a display object which uses the metric's width/height instead.
304 * @hide
305 */
Dianne Hackborn5be8de32011-05-24 18:11:57 -0700306 public static Display createCompatibleDisplay(int displayId, CompatibilityInfo compat) {
307 return new Display(displayId, compat);
Mitsuru Oshimaddd12532009-07-14 10:41:13 -0700308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309}
310