Display.java revision fa25bf5382467b1018bd9af7f1cb30a23d7d59f7
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
195be8de3420ba4c9d816b98e29bdec11715f6b626Dianne Hackbornimport android.content.res.CompatibilityInfo;
20fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brownimport android.graphics.PixelFormat;
21ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackbornimport android.graphics.Point;
22ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackbornimport android.graphics.Rect;
23fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brownimport android.hardware.display.DisplayManager;
24ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackbornimport android.os.RemoteException;
25ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackbornimport android.os.SystemClock;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.DisplayMetrics;
27fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brownimport android.util.Log;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
29bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown/**
30fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * Provides information about the size and density of a logical display.
31fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * <p>
32fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * The display area is described in two different ways.
33fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * <ul>
34fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * <li>The application display area specifies the part of the display that may contain
35fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * an application window, excluding the system decorations.  The application display area may
36fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * be smaller than the real display area because the system subtracts the space needed
37fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * for decor elements such as the status bar.  Use the following methods to query the
38fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * application display area: {@link #getSize}, {@link #getRectSize} and {@link #getMetrics}.</li>
39fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * <li>The real display area specifies the part of the display that contains content
40fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * including the system decorations.  Even so, the real display area may be smaller than the
41fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * physical size of the display if the window manager is emulating a smaller display
42fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * using (adb shell am display-size).  Use the following methods to query the
43fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * real display area: {@link #getRealSize}, {@link #getRealMetrics}.</li>
44fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * </ul>
45fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * </p><p>
46fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * A logical display does not necessarily represent a particular physical display device
47fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * such as the built-in screen or an external monitor.  The contents of a logical
48fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * display may be presented on one or more physical displays according to the devices
49fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * that are currently attached and whether mirroring has been enabled.
50fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown * </p>
51bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown */
52fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brownpublic final class Display {
53fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private static final String TAG = "Display";
54fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
55fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private final int mDisplayId;
56fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private final CompatibilityInfoHolder mCompatibilityInfo;
57fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private final DisplayInfo mDisplayInfo = new DisplayInfo();
58fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
59fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    // Temporary display metrics structure used for compatibility mode.
60fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private final DisplayMetrics mTempMetrics = new DisplayMetrics();
61fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
62fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    // We cache the app width and height properties briefly between calls
63fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    // to getHeight() and getWidth() to ensure that applications perceive
64fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    // consistent results when the size changes (most of the time).
65fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    // Applications should now be using getSize() instead.
66fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private static final int CACHED_APP_SIZE_DURATION_MILLIS = 20;
67fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private long mLastCachedAppSizeUpdate;
68fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private int mCachedAppWidthCompat;
69fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private int mCachedAppHeightCompat;
705fd2169eabd77e6bfafaf456e58051a3bafb2bcaDianne Hackborn
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
72bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The default Display id.
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int DEFAULT_DISPLAY = 0;
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
77fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Internal method to create a display.
78fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Applications should use {@link android.view.WindowManager#getDefaultDisplay()}
79fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * to get a display object for the default display.
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    /**
100bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Gets the size of the display, in pixels.
101bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * <p>
102bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Note that this value should <em>not</em> be used for computing layouts,
103bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * since a device will typically have screen decoration (such as a status bar)
1045cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * along the edges of the display that reduce the amount of application
105bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * space available from the size returned here.  Layouts should instead use
106bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * the window size.
107bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p><p>
108bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size is adjusted based on the current rotation of the display.
109bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p><p>
110bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size returned by this method does not necessarily represent the
111bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * actual raw size (native resolution) of the display.  The returned size may
112fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * be adjusted to exclude certain system decoration elements that are always visible.
113bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * It may also be scaled to provide compatibility with older applications that
114bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * were originally designed for smaller displays.
115bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p>
116bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     *
117bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @param outSize A {@link Point} object to receive the size information.
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
119ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public void getSize(Point outSize) {
120fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
121fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
122fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo);
123fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.x = mTempMetrics.widthPixels;
124fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.y = mTempMetrics.heightPixels;
125ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
126ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
127fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
129bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Gets the size of the display as a rectangle, in pixels.
130bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     *
131bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @param outSize A {@link Rect} object to receive the size information.
132bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @see #getSize(Point)
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
134ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public void getRectSize(Rect outSize) {
135fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
136fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
137fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo);
138fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.set(0, 0, mTempMetrics.widthPixels, mTempMetrics.heightPixels);
139ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
140ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
142ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    /**
14368c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * Return the range of display sizes an application can expect to encounter
14468c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * under normal operation, as long as there is no physical change in screen
14568c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * size.  This is basically the sizes you will see as the orientation
14668c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * changes, taking into account whatever screen decoration there is in
14768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * each rotation.  For example, the status bar is always at the top of the
14868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * screen, so it will reduce the height both in landscape and portrait, and
14968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * the smallest height returned here will be the smaller of the two.
15068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     *
15168c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * This is intended for applications to get an idea of the range of sizes
15268c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * they will encounter while going through device rotations, to provide a
15368c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * stable UI through rotation.  The sizes here take into account all standard
15468c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * system decorations that reduce the size actually available to the
15568c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * application: the status bar, navigation bar, system bar, etc.  It does
15668c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * <em>not</em> take into account more transient elements like an IME
15768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * soft keyboard.
15868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     *
15968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * @param outSmallestSize Filled in with the smallest width and height
16068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * that the application will encounter, in pixels (not dp units).  The x
16168c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * (width) dimension here directly corresponds to
16268c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * {@link android.content.res.Configuration#smallestScreenWidthDp
16368c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * Configuration.smallestScreenWidthDp}, except the value here is in raw
16468c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * screen pixels rather than dp units.  Your application may of course
16568c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * still get smaller space yet if, for example, a soft keyboard is
16668c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * being displayed.
16768c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * @param outLargestSize Filled in with the largest width and height
16868c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * that the application will encounter, in pixels (not dp units).  Your
16968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * application may of course still get larger space than this if,
17068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     * for example, screen decorations like the status bar are being hidden.
17168c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn     */
17268c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn    public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) {
173fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
174fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
175fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSmallestSize.x = mDisplayInfo.smallestNominalAppWidth;
176fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSmallestSize.y = mDisplayInfo.smallestNominalAppHeight;
177fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outLargestSize.x = mDisplayInfo.largestNominalAppWidth;
178fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outLargestSize.y = mDisplayInfo.largestNominalAppHeight;
17968c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn        }
18068c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn    }
18168c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn
18268c33ca7ce1f142eb5f1e1f90118aeba4c9db1e3Dianne Hackborn    /**
183ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * Return the maximum screen size dimension that will happen.  This is
184ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * mostly for wallpapers.
185ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * @hide
186ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     */
187ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public int getMaximumSizeDimension() {
188fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
189fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
190fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return Math.max(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
191ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
192ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
193ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn
194ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    /**
195ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * @deprecated Use {@link #getSize(Point)} instead.
196ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     */
197ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    @Deprecated
198ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public int getWidth() {
199fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
200fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateCachedAppSizeIfNeededLocked();
201fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mCachedAppWidthCompat;
202ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn        }
203ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
204ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn
205ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    /**
206ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     * @deprecated Use {@link #getSize(Point)} instead.
207ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn     */
208ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    @Deprecated
209ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    public int getHeight() {
210fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
211fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateCachedAppSizeIfNeededLocked();
212fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mCachedAppHeightCompat;
213bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown        }
214bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    }
215ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn
216bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    /**
2175cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * Returns the rotation of the screen from its "natural" orientation.
2185cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0}
2195cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90},
2205cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or
2215cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * {@link Surface#ROTATION_270 Surface.ROTATION_270}.  For
2225cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * example, if a device has a naturally tall screen, and the user has
2235cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * turned it on its side to go into a landscape orientation, the value
2245cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90}
2255cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on
2265cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * the direction it was turned.  The angle is the rotation of the drawn
2275cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * graphics on the screen, which is the opposite direction of the physical
2285cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * rotation of the device.  For example, if the device is rotated 90
2295cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * degrees counter-clockwise, to compensate rendering will be rotated by
2305cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * 90 degrees clockwise and thus the returned value here will be
2315cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     * {@link Surface#ROTATION_90 Surface.ROTATION_90}.
2325cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn     */
2335cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn    public int getRotation() {
234fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
235fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
236fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mDisplayInfo.rotation;
237fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
2385cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn    }
239fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
2405cb70b54156fb305d579a1cc167424c8705bfdf7Dianne Hackborn    /**
2414c904a3bf3dbe98607b5e3f706ee8ef8887ee104Joe Onorato     * @deprecated use {@link #getRotation}
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return orientation of this display.
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
244fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    @Deprecated
245fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public int getOrientation() {
246fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        return getRotation();
247fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    }
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
250fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the pixel format of the display.
251fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @return One of the constants defined in {@link android.graphics.PixelFormat}.
252fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
253fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @deprecated This method is no longer supported.
254fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The result is always {@link PixelFormat#RGBA_8888}.
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
256fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    @Deprecated
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getPixelFormat() {
258fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        return PixelFormat.RGBA_8888;
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
260fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
262fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the refresh rate of this display in frames per second.
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public float getRefreshRate() {
265fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
266fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
267fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            return mDisplayInfo.refreshRate;
268fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
270fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
272bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * Gets display metrics that describe the size and density of this display.
273bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * <p>
274bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size is adjusted based on the current rotation of the display.
275bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p><p>
276bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * The size returned by this method does not necessarily represent the
277bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * actual raw size (native resolution) of the display.  The returned size may
278bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * be adjusted to exclude certain system decor elements that are always visible.
279bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * It may also be scaled to provide compatibility with older applications that
280bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * were originally designed for smaller displays.
281bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * </p>
282bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     *
283bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown     * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void getMetrics(DisplayMetrics outMetrics) {
286fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
287fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
288fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(outMetrics, mCompatibilityInfo);
28968066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn        }
2905be8de3420ba4c9d816b98e29bdec11715f6b626Dianne Hackborn
291fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        final CompatibilityInfo ci = mCompatibilityInfo.getIfNeeded();
2925fd2169eabd77e6bfafaf456e58051a3bafb2bcaDianne Hackborn        if (ci != null) {
2935fd2169eabd77e6bfafaf456e58051a3bafb2bcaDianne Hackborn            ci.applyToDisplayMetrics(outMetrics);
2945be8de3420ba4c9d816b98e29bdec11715f6b626Dianne Hackborn        }
29568066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn    }
29668066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn
29768066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn    /**
298fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets the real size of the display without subtracting any window decor or
299fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * applying any compatibility scale factors.
300fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * <p>
301fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The size is adjusted based on the current rotation of the display.
302fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p><p>
303fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The real size may be smaller than the physical size of the screen when the
304fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * window manager is emulating a smaller display (using adb shell am display-size).
305fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p>
306fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
307fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @param outSize Set to the real size of the display.
30868066c2f38e47b56f0510c56eafd827731a0dc08Dianne Hackborn     */
309fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public void getRealSize(Point outSize) {
310fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
311fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
312fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.x = mDisplayInfo.logicalWidth;
313fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            outSize.y = mDisplayInfo.logicalHeight;
314bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown        }
315bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    }
316bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown
317bc68a59c024bdb745dac8e2ec7408a9f30595f1aJeff Brown    /**
318fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * Gets display metrics based on the real size of this display.
319fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * <p>
320fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The size is adjusted based on the current rotation of the display.
321fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p><p>
322fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * The real size may be smaller than the physical size of the screen when the
323fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * window manager is emulating a smaller display (using adb shell am display-size).
324fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * </p>
325fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     *
326fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown     * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
32793de746e5554bc9397ca8109f57875d92e64eabcJeff Brown     */
328fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    public void getRealMetrics(DisplayMetrics outMetrics) {
329fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        synchronized (this) {
330fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
331fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getLogicalMetrics(outMetrics, null);
332fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
33393de746e5554bc9397ca8109f57875d92e64eabcJeff Brown    }
33493de746e5554bc9397ca8109f57875d92e64eabcJeff Brown
335fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private void updateDisplayInfoLocked() {
336fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        // TODO: only refresh the display information when needed
337fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        if (!DisplayManager.getInstance().getDisplayInfo(mDisplayId, mDisplayInfo)) {
338fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            Log.e(TAG, "Could not get information about logical display " + mDisplayId);
339fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
342fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown    private void updateCachedAppSizeIfNeededLocked() {
343fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        long now = SystemClock.uptimeMillis();
344fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        if (now > mLastCachedAppSizeUpdate + CACHED_APP_SIZE_DURATION_MILLIS) {
345fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            updateDisplayInfoLocked();
346fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mDisplayInfo.getAppMetrics(mTempMetrics, mCompatibilityInfo);
347fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mCachedAppWidthCompat = mTempMetrics.widthPixels;
348fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mCachedAppHeightCompat = mTempMetrics.heightPixels;
349fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown            mLastCachedAppSizeUpdate = now;
350fa25bf5382467b1018bd9af7f1cb30a23d7d59f7Jeff Brown        }
351ac8dea12c17aa047e03a358110aeb60401d36aa2Dianne Hackborn    }
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
354