Display.java revision 98365d7663cbd82979a5700faf0050220b01084d
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    /**
7098365d7663cbd82979a5700faf0050220b01084dJeff Brown     * The default Display id, which is the id of the built-in primary display
7198365d7663cbd82979a5700faf0050220b01084dJeff Brown     * assuming there is one.
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int DEFAULT_DISPLAY = 0;
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
76fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Internal method to create a display.
77fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Applications should use {@link android.view.WindowManager#getDefaultDisplay()}
7898365d7663cbd82979a5700faf0050220b01084dJeff Brown     * or {@link android.hardware.display.DisplayManager#getDisplay}
7998365d7663cbd82979a5700faf0050220b01084dJeff Brown     * to get a display object.
80fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
81fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @hide
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
83fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public Display(int displayId, CompatibilityInfoHolder compatibilityInfo) {
84fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        mDisplayId = displayId;
85fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        mCompatibilityInfo = compatibilityInfo;
865fd2169eabd77e6bfafaf456e58051a3bafb2bcaDianne Hackborn    }
875fd2169eabd77e6bfafaf456e58051a3bafb2bcaDianne Hackborn
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
89fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the display id.
90fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * <p>
91fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Each logical display has a unique id.
92fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The default display has id {@link #DEFAULT_DISPLAY}.
93fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p>
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getDisplayId() {
96fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        return mDisplayId;
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1002ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown     * Gets a full copy of the display information.
1012ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown     *
1022ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown     * @param outDisplayInfo The object to receive the copy of the display information.
1032ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown     * @hide
1042ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown     */
1052ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown    public void getDisplayInfo(DisplayInfo outDisplayInfo) {
1062ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown        synchronized (this) {
1072ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown            updateDisplayInfoLocked();
1082ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown            outDisplayInfo.copyFrom(mDisplayInfo);
1092ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown        }
1102ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown    }
1112ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown
1122ab1b7d9abc1b720b63ae01abcf1df0dc780eed4Jeff Brown    /**
11398365d7663cbd82979a5700faf0050220b01084dJeff Brown     * Gets the display's layer stack.
11498365d7663cbd82979a5700faf0050220b01084dJeff Brown     *
11598365d7663cbd82979a5700faf0050220b01084dJeff Brown     * Each display has its own independent layer stack upon which surfaces
11698365d7663cbd82979a5700faf0050220b01084dJeff Brown     * are placed to be managed by surface flinger.
11798365d7663cbd82979a5700faf0050220b01084dJeff Brown     *
11898365d7663cbd82979a5700faf0050220b01084dJeff Brown     * @return The layer stack number.
11998365d7663cbd82979a5700faf0050220b01084dJeff Brown     * @hide
12098365d7663cbd82979a5700faf0050220b01084dJeff Brown     */
12198365d7663cbd82979a5700faf0050220b01084dJeff Brown    public int getLayerStack() {
12298365d7663cbd82979a5700faf0050220b01084dJeff Brown        // Note: This is the current convention but there is no requirement that
12398365d7663cbd82979a5700faf0050220b01084dJeff Brown        // the display id and layer stack id be the same.
12498365d7663cbd82979a5700faf0050220b01084dJeff Brown        return mDisplayId;
12598365d7663cbd82979a5700faf0050220b01084dJeff Brown    }
12698365d7663cbd82979a5700faf0050220b01084dJeff Brown
12798365d7663cbd82979a5700faf0050220b01084dJeff Brown    /**
12898365d7663cbd82979a5700faf0050220b01084dJeff Brown     * Gets the compatibility info used by this display instance.
12998365d7663cbd82979a5700faf0050220b01084dJeff Brown     *
13098365d7663cbd82979a5700faf0050220b01084dJeff Brown     * @return The compatibility info holder, or null if none is required.
13198365d7663cbd82979a5700faf0050220b01084dJeff Brown     * @hide
13298365d7663cbd82979a5700faf0050220b01084dJeff Brown     */
13398365d7663cbd82979a5700faf0050220b01084dJeff Brown    public CompatibilityInfoHolder getCompatibilityInfo() {
13498365d7663cbd82979a5700faf0050220b01084dJeff Brown        return mCompatibilityInfo;
13598365d7663cbd82979a5700faf0050220b01084dJeff Brown    }
13698365d7663cbd82979a5700faf0050220b01084dJeff Brown
13798365d7663cbd82979a5700faf0050220b01084dJeff Brown    /**
138bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Gets the size of the display, in pixels.
139bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * <p>
140bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Note that this value should <em>not</em> be used for computing layouts,
141bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * since a device will typically have screen decoration (such as a status bar)
1425cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * along the edges of the display that reduce the amount of application
143bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * space available from the size returned here.  Layouts should instead use
144bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * the window size.
145bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p><p>
146bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size is adjusted based on the current rotation of the display.
147bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p><p>
148bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size returned by this method does not necessarily represent the
149bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * actual raw size (native resolution) of the display.  The returned size may
150fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * be adjusted to exclude certain system decoration elements that are always visible.
151bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * It may also be scaled to provide compatibility with older applications that
152bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * were originally designed for smaller displays.
153bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p>
154bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     *
155bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @param outSize A {@link Point} object to receive the size information.
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
157ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public void getSize(Point outSize) {
158fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
159fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
160fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo);
161fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.x = mTempMetrics.widthPixels;
162fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.y = mTempMetrics.heightPixels;
163ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
164ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
165fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
167bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Gets the size of the display as a rectangle, in pixels.
168bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     *
169bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @param outSize A {@link Rect} object to receive the size information.
170bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @see #getSize(Point)
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
172ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public void getRectSize(Rect outSize) {
173fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
174fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
175fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo);
176fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.set(0, 0, mTempMetrics.widthPixels, mTempMetrics.heightPixels);
177ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
178ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
180ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    /**
18168c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * Return the range of display sizes an application can expect to encounter
18268c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * under normal operation, as long as there is no physical change in screen
18368c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * size.  This is basically the sizes you will see as the orientation
18468c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * changes, taking into account whatever screen decoration there is in
18568c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * each rotation.  For example, the status bar is always at the top of the
18668c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * screen, so it will reduce the height both in landscape and portrait, and
18768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * the smallest height returned here will be the smaller of the two.
18868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     *
18968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * This is intended for applications to get an idea of the range of sizes
19068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * they will encounter while going through device rotations, to provide a
19168c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * stable UI through rotation.  The sizes here take into account all standard
19268c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * system decorations that reduce the size actually available to the
19368c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * application: the status bar, navigation bar, system bar, etc.  It does
19468c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * <em>not</em> take into account more transient elements like an IME
19568c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * soft keyboard.
19668c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     *
19768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * @param outSmallestSize Filled in with the smallest width and height
19868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * that the application will encounter, in pixels (not dp units).  The x
19968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * (width) dimension here directly corresponds to
20068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * {@link android.content.res.Configuration#smallestScreenWidthDp
20168c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * Configuration.smallestScreenWidthDp}, except the value here is in raw
20268c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * screen pixels rather than dp units.  Your application may of course
20368c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * still get smaller space yet if, for example, a soft keyboard is
20468c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * being displayed.
20568c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * @param outLargestSize Filled in with the largest width and height
20668c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * that the application will encounter, in pixels (not dp units).  Your
20768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * application may of course still get larger space than this if,
20868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * for example, screen decorations like the status bar are being hidden.
20968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     */
21068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn    public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) {
211fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
212fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
213fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSmallestSize.x = mDisplayInfo.smallestNominalAppWidth;
214fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSmallestSize.y = mDisplayInfo.smallestNominalAppHeight;
215fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outLargestSize.x = mDisplayInfo.largestNominalAppWidth;
216fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outLargestSize.y = mDisplayInfo.largestNominalAppHeight;
21768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn        }
21868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn    }
21968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn
22068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn    /**
221ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * Return the maximum screen size dimension that will happen.  This is
222ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * mostly for wallpapers.
223ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * @hide
224ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     */
225ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public int getMaximumSizeDimension() {
226fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
227fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
228fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return Math.max(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
229ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
230ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
231ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn
232ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    /**
233ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * @deprecated Use {@link #getSize(Point)} instead.
234ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     */
235ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    @Deprecated
236ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public int getWidth() {
237fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
238fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateCachedAppSizeIfNeededLocked();
239fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mCachedAppWidthCompat;
240ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
241ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
242ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn
243ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    /**
244ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * @deprecated Use {@link #getSize(Point)} instead.
245ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     */
246ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    @Deprecated
247ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public int getHeight() {
248fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
249fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateCachedAppSizeIfNeededLocked();
250fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mCachedAppHeightCompat;
251bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown        }
252bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    }
253ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn
254bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    /**
2555cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * Returns the rotation of the screen from its "natural" orientation.
2565cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
2575cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
2585cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
2595cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * {@link Surface#ROTATION_270 Surface.ROTATION_270}.  For
2605cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * example, if a device has a naturally tall screen, and the user has
2615cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * turned it on its side to go into a landscape orientation, the value
2625cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
2635cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
2645cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * the direction it was turned.  The angle is the rotation of the drawn
2655cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * graphics on the screen, which is the opposite direction of the physical
2665cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * rotation of the device.  For example, if the device is rotated 90
2675cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * degrees counter-clockwise, to compensate rendering will be rotated by
2685cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * 90 degrees clockwise and thus the returned value here will be
2695cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
2705cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     */
2715cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn    public int getRotation() {
272fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
273fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
274fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mDisplayInfo.rotation;
275fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
2765cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn    }
277fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
2785cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn    /**
2794c904a3bf3dbe98607b5e3f706ee8ef8887ee104Joe Onorato     * @deprecated use {@link #getRotation}
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return orientation of this display.
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
282fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    @Deprecated
283fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public int getOrientation() {
284fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        return getRotation();
285fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    }
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
288fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the pixel format of the display.
289fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @return One of the constants defined in {@link android.graphics.PixelFormat}.
290fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
291fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @deprecated This method is no longer supported.
292fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The result is always {@link PixelFormat#RGBA_8888}.
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
294fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    @Deprecated
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getPixelFormat() {
296fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        return PixelFormat.RGBA_8888;
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
298fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
300fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the refresh rate of this display in frames per second.
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public float getRefreshRate() {
303fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
304fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
305fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mDisplayInfo.refreshRate;
306fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
308fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
310bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Gets display metrics that describe the size and density of this display.
311bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * <p>
312bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size is adjusted based on the current rotation of the display.
313bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p><p>
314bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size returned by this method does not necessarily represent the
315bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * actual raw size (native resolution) of the display.  The returned size may
316bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * be adjusted to exclude certain system decor elements that are always visible.
317bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * It may also be scaled to provide compatibility with older applications that
318bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * were originally designed for smaller displays.
319bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p>
320bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     *
321bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void getMetrics(DisplayMetrics outMetrics) {
324fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
325fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
326fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(outMetrics, mCompatibilityInfo);
32768066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn        }
32868066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn    }
32968066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn
33068066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn    /**
331fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the real size of the display without subtracting any window decor or
332fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * applying any compatibility scale factors.
333fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * <p>
334fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The size is adjusted based on the current rotation of the display.
335fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p><p>
336fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The real size may be smaller than the physical size of the screen when the
337fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * window manager is emulating a smaller display (using adb shell am display-size).
338fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p>
339fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
340fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @param outSize Set to the real size of the display.
34168066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn     */
342fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public void getRealSize(Point outSize) {
343fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
344fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
345fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.x = mDisplayInfo.logicalWidth;
346fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.y = mDisplayInfo.logicalHeight;
347bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown        }
348bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    }
349bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown
350bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    /**
351fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets display metrics based on the real size of this display.
352fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * <p>
353fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The size is adjusted based on the current rotation of the display.
354fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p><p>
355fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The real size may be smaller than the physical size of the screen when the
356fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * window manager is emulating a smaller display (using adb shell am display-size).
357fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p>
358fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
359fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
36093de746e5554bc9397ca8109f57875d92e64eabcJeff Brown     */
361fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public void getRealMetrics(DisplayMetrics outMetrics) {
362fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
363fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
364fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getLogicalMetrics(outMetrics, null);
365fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
36693de746e5554bc9397ca8109f57875d92e64eabcJeff Brown    }
36793de746e5554bc9397ca8109f57875d92e64eabcJeff Brown
368fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private void updateDisplayInfoLocked() {
369fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        // TODO: only refresh the display information when needed
370fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        if (!DisplayManager.getInstance().getDisplayInfo(mDisplayId, mDisplayInfo)) {
371fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            Log.e(TAG, "Could not get information about logical display " + mDisplayId);
372fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
375fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private void updateCachedAppSizeIfNeededLocked() {
376fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        long now = SystemClock.uptimeMillis();
377fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        if (now > mLastCachedAppSizeUpdate + CACHED_APP_SIZE_DURATION_MILLIS) {
378fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
379fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo);
380fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mCachedAppWidthCompat = mTempMetrics.widthPixels;
381fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mCachedAppHeightCompat = mTempMetrics.heightPixels;
382fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mLastCachedAppSizeUpdate = now;
383fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
384ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
387