The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 19 | import android.content.res.CompatibilityInfo; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 20 | import android.graphics.Point; |
| 21 | import android.graphics.Rect; |
| 22 | import android.os.RemoteException; |
| 23 | import android.os.ServiceManager; |
| 24 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.util.DisplayMetrics; |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 26 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 28 | public class Display { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Specify the default Display |
| 31 | */ |
| 32 | public static final int DEFAULT_DISPLAY = 0; |
| 33 | |
| 34 | |
| 35 | /** |
Mathias Agopian | 833533c | 2010-08-16 13:28:55 -0700 | [diff] [blame] | 36 | * Use {@link android.view.WindowManager#getDefaultDisplay() |
| 37 | * WindowManager.getDefaultDisplay()} to create a Display object. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | * Display gives you access to some information about a particular display |
| 39 | * connected to the device. |
| 40 | */ |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 41 | Display(int display, CompatibilityInfo compatInfo) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | // initalize the statics when this class is first instansiated. This is |
| 43 | // done here instead of in the static block because Zygote |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 44 | synchronized (sStaticInit) { |
| 45 | if (!sInitialized) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | nativeClassInit(); |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 47 | sInitialized = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | } |
| 49 | } |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 50 | if (compatInfo != null && (compatInfo.isScalingRequired() |
| 51 | || !compatInfo.supportsScreen())) { |
| 52 | mCompatibilityInfo = compatInfo; |
| 53 | } else { |
| 54 | mCompatibilityInfo = null; |
| 55 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | mDisplay = display; |
| 57 | init(display); |
| 58 | } |
| 59 | |
| 60 | /** |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 61 | * Returns the index of this display. This is currently undefined; do |
| 62 | * not use. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | */ |
| 64 | public int getDisplayId() { |
| 65 | return mDisplay; |
| 66 | } |
| 67 | |
| 68 | /** |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 69 | * Returns the number of displays connected to the device. This is |
| 70 | * currently undefined; do not use. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | */ |
| 72 | native static int getDisplayCount(); |
| 73 | |
| 74 | /** |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 75 | * Returns the raw size of the display, in pixels. Note that this |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 76 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | */ |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 82 | public void getSize(Point outSize) { |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame^] | 83 | 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 Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 95 | try { |
| 96 | IWindowManager wm = getWindowManager(); |
| 97 | if (wm != null) { |
| 98 | wm.getDisplaySize(outSize); |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame^] | 99 | 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 Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 109 | } 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | |
| 119 | /** |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 120 | * This is just easier for some parts of the framework. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | */ |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 122 | public void getRectSize(Rect outSize) { |
| 123 | synchronized (mTmpPoint) { |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame^] | 124 | getSizeInternal(mTmpPoint, true); |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 125 | 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 Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame^] | 152 | getSizeInternal(mTmpPoint, true); |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 153 | 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 Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame^] | 167 | getSizeInternal(mTmpPoint, true); |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 168 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 179 | /** @hide special for when we are faking the screen size. */ |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 180 | native public int getRawWidth(); |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 181 | /** @hide special for when we are faking the screen size. */ |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 182 | native public int getRawHeight(); |
Dianne Hackborn | 99aac7b | 2011-02-25 17:33:02 -0800 | [diff] [blame] | 183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | /** |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 185 | * 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 Onorato | 4c904a3 | 2010-02-26 12:35:55 -0800 | [diff] [blame] | 206 | * @deprecated use {@link #getRotation} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | * @return orientation of this display. |
| 208 | */ |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 209 | @Deprecated native public int getOrientation(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | |
| 211 | /** |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 212 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | */ |
| 215 | public int getPixelFormat() { |
| 216 | return mPixelFormat; |
| 217 | } |
| 218 | |
| 219 | /** |
Dianne Hackborn | 5cb70b5 | 2010-02-25 17:01:14 -0800 | [diff] [blame] | 220 | * Return the refresh rate of this display in frames per second. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | */ |
| 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 Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 232 | synchronized (mTmpPoint) { |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame^] | 233 | getSizeInternal(mTmpPoint, false); |
Dianne Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 234 | outMetrics.widthPixels = mTmpPoint.x; |
| 235 | outMetrics.heightPixels = mTmpPoint.y; |
| 236 | } |
| 237 | getNonSizeMetrics(outMetrics); |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 238 | |
| 239 | if (mCompatibilityInfo != null) { |
| 240 | mCompatibilityInfo.applyToDisplayMetrics(outMetrics); |
| 241 | } |
Dianne Hackborn | 68066c2 | 2011-04-21 17:26:39 -0700 | [diff] [blame] | 242 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | outMetrics.density = mDensity; |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 258 | outMetrics.densityDpi = (int)((mDensity*DisplayMetrics.DENSITY_DEFAULT)+.5f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | outMetrics.scaledDensity= outMetrics.density; |
| 260 | outMetrics.xdpi = mDpiX; |
| 261 | outMetrics.ydpi = mDpiY; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 262 | |
Dianne Hackborn | 81e56d5 | 2011-05-26 00:55:58 -0700 | [diff] [blame^] | 263 | outMetrics.unscaledWidthPixels = outMetrics.widthPixels; |
| 264 | outMetrics.unscaledHeightPixels = outMetrics.heightPixels; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 267 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | /* |
| 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 Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 285 | private final CompatibilityInfo mCompatibilityInfo; |
| 286 | private final int mDisplay; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | // 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 Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 294 | private final Point mTmpPoint = new Point(); |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 295 | private final DisplayMetrics mTmpMetrics = new DisplayMetrics(); |
Dianne Hackborn | ac8dea1 | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 296 | private float mLastGetTime; |
| 297 | |
| 298 | private static final Object sStaticInit = new Object(); |
| 299 | private static boolean sInitialized = false; |
| 300 | private static IWindowManager sWindowManager; |
Mitsuru Oshima | ddd1253 | 2009-07-14 10:41:13 -0700 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * Returns a display object which uses the metric's width/height instead. |
| 304 | * @hide |
| 305 | */ |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 306 | public static Display createCompatibleDisplay(int displayId, CompatibilityInfo compat) { |
| 307 | return new Display(displayId, compat); |
Mitsuru Oshima | ddd1253 | 2009-07-14 10:41:13 -0700 | [diff] [blame] | 308 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | } |
| 310 | |