Display.java revision 2ab1b7d9abc1b720b63ae01abcf1df0dc780eed4
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.view;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brownimport android.graphics.PixelFormat;
20ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackbornimport android.graphics.Point;
21ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackbornimport android.graphics.Rect;
22fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brownimport android.hardware.display.DisplayManager;
23ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackbornimport android.os.SystemClock;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.DisplayMetrics;
25fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brownimport android.util.Log;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
27bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown/**
28fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * Provides information about the size and density of a logical display.
29fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * <p>
30fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * The display area is described in two different ways.
31fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * <ul>
32fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * <li>The application display area specifies the part of the display that may contain
33fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * an application window, excluding the system decorations.  The application display area may
34fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * be smaller than the real display area because the system subtracts the space needed
35fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * for decor elements such as the status bar.  Use the following methods to query the
36fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * application display area: {@link #getSize}, {@link #getRectSize} and {@link #getMetrics}.</li>
37fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * <li>The real display area specifies the part of the display that contains content
38fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * including the system decorations.  Even so, the real display area may be smaller than the
39fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * physical size of the display if the window manager is emulating a smaller display
40fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * using (adb shell am display-size).  Use the following methods to query the
41fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * real display area: {@link #getRealSize}, {@link #getRealMetrics}.</li>
42fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * </ul>
43fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * </p><p>
44fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * A logical display does not necessarily represent a particular physical display device
45fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * such as the built-in screen or an external monitor.  The contents of a logical
46fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * display may be presented on one or more physical displays according to the devices
47fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * that are currently attached and whether mirroring has been enabled.
48fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * </p>
49bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown */
50fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brownpublic final class Display {
51fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private static final String TAG = "Display";
52fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
53fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private final int mDisplayId;
54fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private final CompatibilityInfoHolder mCompatibilityInfo;
55fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private final DisplayInfo mDisplayInfo = new DisplayInfo();
56fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
57fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    // Temporary display metrics structure used for compatibility mode.
58fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private final DisplayMetrics mTempMetrics = new DisplayMetrics();
59fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
60fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    // We cache the app width and height properties briefly between calls
61fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    // to getHeight() and getWidth() to ensure that applications perceive
62fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    // consistent results when the size changes (most of the time).
63fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    // Applications should now be using getSize() instead.
64fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private static final int CACHED_APP_SIZE_DURATION_MILLIS = 20;
65fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private long mLastCachedAppSizeUpdate;
66fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private int mCachedAppWidthCompat;
67fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private int mCachedAppHeightCompat;
685fd2169eabd77e6bfafaf456e58051a3bafb2bcaDianne Hackborn
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
70bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The default Display id.
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int DEFAULT_DISPLAY = 0;
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
754f67ba6ba4e861b287a3ff0323c107aa77f66264Craig Mautner     * Uninitialized display.
764f67ba6ba4e861b287a3ff0323c107aa77f66264Craig Mautner     * @hide
774f67ba6ba4e861b287a3ff0323c107aa77f66264Craig Mautner     */
784f67ba6ba4e861b287a3ff0323c107aa77f66264Craig Mautner    public static final int NO_DISPLAY = -1;
794f67ba6ba4e861b287a3ff0323c107aa77f66264Craig Mautner
804f67ba6ba4e861b287a3ff0323c107aa77f66264Craig Mautner    /**
81fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Internal method to create a display.
82fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Applications should use {@link android.view.WindowManager#getDefaultDisplay()}
83fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * to get a display object for the default display.
84fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
85fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @hide
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
87fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public Display(int displayId, CompatibilityInfoHolder compatibilityInfo) {
88fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        mDisplayId = displayId;
89fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        mCompatibilityInfo = compatibilityInfo;
905fd2169eabd77e6bfafaf456e58051a3bafb2bcaDianne Hackborn    }
915fd2169eabd77e6bfafaf456e58051a3bafb2bcaDianne Hackborn
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
93fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the display id.
94fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * <p>
95fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Each logical display has a unique id.
96fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The default display has id {@link #DEFAULT_DISPLAY}.
97fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p>
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getDisplayId() {
100fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        return mDisplayId;
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1042ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown     * Gets a full copy of the display information.
1052ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown     *
1062ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown     * @param outDisplayInfo The object to receive the copy of the display information.
1072ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown     * @hide
1082ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown     */
1092ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown    public void getDisplayInfo(DisplayInfo outDisplayInfo) {
1102ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown        synchronized (this) {
1112ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown            updateDisplayInfoLocked();
1122ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown            outDisplayInfo.copyFrom(mDisplayInfo);
1132ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown        }
1142ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown    }
1152ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown
1162ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown    /**
117bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Gets the size of the display, in pixels.
118bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * <p>
119bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Note that this value should <em>not</em> be used for computing layouts,
120bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * since a device will typically have screen decoration (such as a status bar)
1215cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * along the edges of the display that reduce the amount of application
122bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * space available from the size returned here.  Layouts should instead use
123bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * the window size.
124bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p><p>
125bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size is adjusted based on the current rotation of the display.
126bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p><p>
127bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size returned by this method does not necessarily represent the
128bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * actual raw size (native resolution) of the display.  The returned size may
129fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * be adjusted to exclude certain system decoration elements that are always visible.
130bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * It may also be scaled to provide compatibility with older applications that
131bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * were originally designed for smaller displays.
132bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p>
133bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     *
134bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @param outSize A {@link Point} object to receive the size information.
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
136ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public void getSize(Point outSize) {
137fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
138fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
139fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo);
140fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.x = mTempMetrics.widthPixels;
141fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.y = mTempMetrics.heightPixels;
142ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
143ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
144fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
146bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Gets the size of the display as a rectangle, in pixels.
147bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     *
148bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @param outSize A {@link Rect} object to receive the size information.
149bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @see #getSize(Point)
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
151ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public void getRectSize(Rect outSize) {
152fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
153fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
154fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo);
155fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.set(0, 0, mTempMetrics.widthPixels, mTempMetrics.heightPixels);
156ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
157ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
159ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    /**
16068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * Return the range of display sizes an application can expect to encounter
16168c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * under normal operation, as long as there is no physical change in screen
16268c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * size.  This is basically the sizes you will see as the orientation
16368c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * changes, taking into account whatever screen decoration there is in
16468c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * each rotation.  For example, the status bar is always at the top of the
16568c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * screen, so it will reduce the height both in landscape and portrait, and
16668c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * the smallest height returned here will be the smaller of the two.
16768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     *
16868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * This is intended for applications to get an idea of the range of sizes
16968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * they will encounter while going through device rotations, to provide a
17068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * stable UI through rotation.  The sizes here take into account all standard
17168c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * system decorations that reduce the size actually available to the
17268c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * application: the status bar, navigation bar, system bar, etc.  It does
17368c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * <em>not</em> take into account more transient elements like an IME
17468c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * soft keyboard.
17568c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     *
17668c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * @param outSmallestSize Filled in with the smallest width and height
17768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * that the application will encounter, in pixels (not dp units).  The x
17868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * (width) dimension here directly corresponds to
17968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * {@link android.content.res.Configuration#smallestScreenWidthDp
18068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * Configuration.smallestScreenWidthDp}, except the value here is in raw
18168c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * screen pixels rather than dp units.  Your application may of course
18268c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * still get smaller space yet if, for example, a soft keyboard is
18368c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * being displayed.
18468c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * @param outLargestSize Filled in with the largest width and height
18568c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * that the application will encounter, in pixels (not dp units).  Your
18668c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * application may of course still get larger space than this if,
18768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * for example, screen decorations like the status bar are being hidden.
18868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     */
18968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn    public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) {
190fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
191fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
192fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSmallestSize.x = mDisplayInfo.smallestNominalAppWidth;
193fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSmallestSize.y = mDisplayInfo.smallestNominalAppHeight;
194fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outLargestSize.x = mDisplayInfo.largestNominalAppWidth;
195fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outLargestSize.y = mDisplayInfo.largestNominalAppHeight;
19668c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn        }
19768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn    }
19868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn
19968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn    /**
200ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * Return the maximum screen size dimension that will happen.  This is
201ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * mostly for wallpapers.
202ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * @hide
203ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     */
204ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public int getMaximumSizeDimension() {
205fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
206fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
207fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return Math.max(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
208ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
209ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
210ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn
211ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    /**
212ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * @deprecated Use {@link #getSize(Point)} instead.
213ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     */
214ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    @Deprecated
215ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public int getWidth() {
216fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
217fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateCachedAppSizeIfNeededLocked();
218fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mCachedAppWidthCompat;
219ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
220ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
221ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn
222ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    /**
223ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * @deprecated Use {@link #getSize(Point)} instead.
224ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     */
225ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    @Deprecated
226ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public int getHeight() {
227fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
228fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateCachedAppSizeIfNeededLocked();
229fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mCachedAppHeightCompat;
230bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown        }
231bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    }
232ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn
233bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    /**
2345cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * Returns the rotation of the screen from its "natural" orientation.
2355cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
2365cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
2375cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
2385cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * {@link Surface#ROTATION_270 Surface.ROTATION_270}.  For
2395cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * example, if a device has a naturally tall screen, and the user has
2405cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * turned it on its side to go into a landscape orientation, the value
2415cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
2425cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
2435cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * the direction it was turned.  The angle is the rotation of the drawn
2445cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * graphics on the screen, which is the opposite direction of the physical
2455cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * rotation of the device.  For example, if the device is rotated 90
2465cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * degrees counter-clockwise, to compensate rendering will be rotated by
2475cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * 90 degrees clockwise and thus the returned value here will be
2485cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
2495cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     */
2505cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn    public int getRotation() {
251fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
252fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
253fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mDisplayInfo.rotation;
254fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
2555cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn    }
256fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
2575cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn    /**
2584c904a3bf3dbe98607b5e3f706ee8ef8887ee104Joe Onorato     * @deprecated use {@link #getRotation}
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return orientation of this display.
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
261fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    @Deprecated
262fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public int getOrientation() {
263fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        return getRotation();
264fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    }
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
267fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the pixel format of the display.
268fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @return One of the constants defined in {@link android.graphics.PixelFormat}.
269fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
270fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @deprecated This method is no longer supported.
271fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The result is always {@link PixelFormat#RGBA_8888}.
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
273fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    @Deprecated
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getPixelFormat() {
275fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        return PixelFormat.RGBA_8888;
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
277fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
279fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the refresh rate of this display in frames per second.
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public float getRefreshRate() {
282fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
283fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
284fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mDisplayInfo.refreshRate;
285fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
287fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
289bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Gets display metrics that describe the size and density of this display.
290bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * <p>
291bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size is adjusted based on the current rotation of the display.
292bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p><p>
293bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size returned by this method does not necessarily represent the
294bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * actual raw size (native resolution) of the display.  The returned size may
295bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * be adjusted to exclude certain system decor elements that are always visible.
296bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * It may also be scaled to provide compatibility with older applications that
297bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * were originally designed for smaller displays.
298bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p>
299bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     *
300bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void getMetrics(DisplayMetrics outMetrics) {
303fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
304fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
305fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(outMetrics, mCompatibilityInfo);
30668066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn        }
30768066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn    }
30868066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn
30968066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn    /**
310fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the real size of the display without subtracting any window decor or
311fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * applying any compatibility scale factors.
312fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * <p>
313fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The size is adjusted based on the current rotation of the display.
314fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p><p>
315fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The real size may be smaller than the physical size of the screen when the
316fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * window manager is emulating a smaller display (using adb shell am display-size).
317fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p>
318fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
319fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @param outSize Set to the real size of the display.
32068066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn     */
321fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public void getRealSize(Point outSize) {
322fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
323fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
324fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.x = mDisplayInfo.logicalWidth;
325fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.y = mDisplayInfo.logicalHeight;
326bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown        }
327bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    }
328bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown
329bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    /**
330fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets display metrics based on the real size of this display.
331fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * <p>
332fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The size is adjusted based on the current rotation of the display.
333fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p><p>
334fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The real size may be smaller than the physical size of the screen when the
335fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * window manager is emulating a smaller display (using adb shell am display-size).
336fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p>
337fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
338fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
33993de746e5554bc9397ca8109f57875d92e64eabcJeff Brown     */
340fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public void getRealMetrics(DisplayMetrics outMetrics) {
341fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
342fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
343fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getLogicalMetrics(outMetrics, null);
344fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
34593de746e5554bc9397ca8109f57875d92e64eabcJeff Brown    }
34693de746e5554bc9397ca8109f57875d92e64eabcJeff Brown
347fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private void updateDisplayInfoLocked() {
348fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        // TODO: only refresh the display information when needed
349fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        if (!DisplayManager.getInstance().getDisplayInfo(mDisplayId, mDisplayInfo)) {
350fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            Log.e(TAG, "Could not get information about logical display " + mDisplayId);
351fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
354fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private void updateCachedAppSizeIfNeededLocked() {
355fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        long now = SystemClock.uptimeMillis();
356fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        if (now > mLastCachedAppSizeUpdate + CACHED_APP_SIZE_DURATION_MILLIS) {
357fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
358fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo);
359fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mCachedAppWidthCompat = mTempMetrics.widthPixels;
360fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mCachedAppHeightCompat = mTempMetrics.heightPixels;
361fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mLastCachedAppSizeUpdate = now;
362fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
363ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
366