Skip to content

Commit 68c33ca

Browse files
author
Dianne Hackborn
committed
Add new API to find smallest/largest screen size.
Change-Id: I790801fceaf84ee2e3b1c9d32828285ad3231d0e
1 parent 1b96594 commit 68c33ca

File tree

5 files changed

+102
-16
lines changed

5 files changed

+102
-16
lines changed

api/current.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22537,6 +22537,7 @@ package android.view {
2253722537
}
2253822538

2253922539
public class Display {
22540+
method public void getCurrentSizeRange(android.graphics.Point, android.graphics.Point);
2254022541
method public int getDisplayId();
2254122542
method public deprecated int getHeight();
2254222543
method public void getMetrics(android.util.DisplayMetrics);

core/java/android/view/Display.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,49 @@ public void getRectSize(Rect outSize) {
142142
}
143143
}
144144

145+
/**
146+
* Return the range of display sizes an application can expect to encounter
147+
* under normal operation, as long as there is no physical change in screen
148+
* size. This is basically the sizes you will see as the orientation
149+
* changes, taking into account whatever screen decoration there is in
150+
* each rotation. For example, the status bar is always at the top of the
151+
* screen, so it will reduce the height both in landscape and portrait, and
152+
* the smallest height returned here will be the smaller of the two.
153+
*
154+
* This is intended for applications to get an idea of the range of sizes
155+
* they will encounter while going through device rotations, to provide a
156+
* stable UI through rotation. The sizes here take into account all standard
157+
* system decorations that reduce the size actually available to the
158+
* application: the status bar, navigation bar, system bar, etc. It does
159+
* <em>not</em> take into account more transient elements like an IME
160+
* soft keyboard.
161+
*
162+
* @param outSmallestSize Filled in with the smallest width and height
163+
* that the application will encounter, in pixels (not dp units). The x
164+
* (width) dimension here directly corresponds to
165+
* {@link android.content.res.Configuration#smallestScreenWidthDp
166+
* Configuration.smallestScreenWidthDp}, except the value here is in raw
167+
* screen pixels rather than dp units. Your application may of course
168+
* still get smaller space yet if, for example, a soft keyboard is
169+
* being displayed.
170+
* @param outLargestSize Filled in with the largest width and height
171+
* that the application will encounter, in pixels (not dp units). Your
172+
* application may of course still get larger space than this if,
173+
* for example, screen decorations like the status bar are being hidden.
174+
*/
175+
public void getCurrentSizeRange(Point outSmallestSize, Point outLargestSize) {
176+
try {
177+
IWindowManager wm = getWindowManager();
178+
wm.getCurrentSizeRange(outSmallestSize, outLargestSize);
179+
} catch (RemoteException e) {
180+
Slog.w("Display", "Unable to get display size range", e);
181+
outSmallestSize.x = 0;
182+
outSmallestSize.y = 0;
183+
outLargestSize.x = 0;
184+
outLargestSize.y = 0;
185+
}
186+
}
187+
145188
/**
146189
* Return the maximum screen size dimension that will happen. This is
147190
* mostly for wallpapers.

core/java/android/view/IWindowManager.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ interface IWindowManager
5858
void getDisplaySize(out Point size);
5959
void getRealDisplaySize(out Point size);
6060
int getMaximumSizeDimension();
61+
void getCurrentSizeRange(out Point smallestSize, out Point largestSize);
6162

6263
void setForcedDisplaySize(int longDimen, int shortDimen);
6364
void clearForcedDisplaySize();

services/java/com/android/server/wm/WindowManagerService.java

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,10 @@ public void onReceive(Context context, Intent intent) {
461461
int mCurDisplayHeight = 0;
462462
int mAppDisplayWidth = 0;
463463
int mAppDisplayHeight = 0;
464+
int mSmallestDisplayWidth = 0;
465+
int mSmallestDisplayHeight = 0;
466+
int mLargestDisplayWidth = 0;
467+
int mLargestDisplayHeight = 0;
464468

465469
int mRotation = 0;
466470
int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
@@ -6068,12 +6072,21 @@ Configuration computeNewConfigurationLocked() {
60686072
return config;
60696073
}
60706074

6071-
private int reduceConfigWidthSize(int curSize, int rotation, float density, int dw, int dh) {
6072-
int size = (int)(mPolicy.getConfigDisplayWidth(dw, dh, rotation) / density);
6073-
if (size < curSize) {
6074-
curSize = size;
6075+
private void adjustDisplaySizeRanges(int rotation, int dw, int dh) {
6076+
final int width = mPolicy.getConfigDisplayWidth(dw, dh, rotation);
6077+
if (width < mSmallestDisplayWidth) {
6078+
mSmallestDisplayWidth = width;
6079+
}
6080+
if (width > mLargestDisplayWidth) {
6081+
mLargestDisplayWidth = width;
6082+
}
6083+
final int height = mPolicy.getConfigDisplayHeight(dw, dh, rotation);
6084+
if (height < mSmallestDisplayHeight) {
6085+
mSmallestDisplayHeight = height;
6086+
}
6087+
if (height > mLargestDisplayHeight) {
6088+
mLargestDisplayHeight = height;
60756089
}
6076-
return curSize;
60776090
}
60786091

60796092
private int reduceConfigLayout(int curLayout, int rotation, float density,
@@ -6155,7 +6168,7 @@ private int reduceConfigLayout(int curLayout, int rotation, float density,
61556168
return curLayout;
61566169
}
61576170

6158-
private void computeSmallestWidthAndScreenLayout(boolean rotated, int dw, int dh,
6171+
private void computeSizeRangesAndScreenLayout(boolean rotated, int dw, int dh,
61596172
float density, Configuration outConfig) {
61606173
// We need to determine the smallest width that will occur under normal
61616174
// operation. To this, start with the base screen size and compute the
@@ -6169,17 +6182,21 @@ private void computeSmallestWidthAndScreenLayout(boolean rotated, int dw, int dh
61696182
unrotDw = dw;
61706183
unrotDh = dh;
61716184
}
6172-
int sw = reduceConfigWidthSize(unrotDw, Surface.ROTATION_0, density, unrotDw, unrotDh);
6173-
sw = reduceConfigWidthSize(sw, Surface.ROTATION_90, density, unrotDh, unrotDw);
6174-
sw = reduceConfigWidthSize(sw, Surface.ROTATION_180, density, unrotDw, unrotDh);
6175-
sw = reduceConfigWidthSize(sw, Surface.ROTATION_270, density, unrotDh, unrotDw);
6185+
mSmallestDisplayWidth = 1<<30;
6186+
mSmallestDisplayHeight = 1<<30;
6187+
mLargestDisplayWidth = 0;
6188+
mLargestDisplayHeight = 0;
6189+
adjustDisplaySizeRanges(Surface.ROTATION_0, unrotDw, unrotDh);
6190+
adjustDisplaySizeRanges(Surface.ROTATION_90, unrotDh, unrotDw);
6191+
adjustDisplaySizeRanges(Surface.ROTATION_180, unrotDw, unrotDh);
6192+
adjustDisplaySizeRanges(Surface.ROTATION_270, unrotDh, unrotDw);
61766193
int sl = Configuration.SCREENLAYOUT_SIZE_XLARGE
61776194
| Configuration.SCREENLAYOUT_LONG_YES;
61786195
sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh);
61796196
sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw);
61806197
sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh);
61816198
sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw);
6182-
outConfig.smallestScreenWidthDp = sw;
6199+
outConfig.smallestScreenWidthDp = (int)(mSmallestDisplayWidth / density);
61836200
outConfig.screenLayout = sl;
61846201
}
61856202

@@ -6289,7 +6306,7 @@ boolean computeScreenConfigurationLocked(Configuration config) {
62896306
/ dm.density);
62906307
config.screenHeightDp = (int)(mPolicy.getConfigDisplayHeight(dw, dh, mRotation)
62916308
/ dm.density);
6292-
computeSmallestWidthAndScreenLayout(rotated, dw, dh, dm.density, config);
6309+
computeSizeRangesAndScreenLayout(rotated, dw, dh, dm.density, config);
62936310

62946311
config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
62956312
config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
@@ -7199,6 +7216,15 @@ public int getMaximumSizeDimension() {
71997216
}
72007217
}
72017218

7219+
public void getCurrentSizeRange(Point smallestSize, Point largestSize) {
7220+
synchronized(mDisplaySizeLock) {
7221+
smallestSize.x = mSmallestDisplayWidth;
7222+
smallestSize.y = mSmallestDisplayHeight;
7223+
largestSize.x = mLargestDisplayWidth;
7224+
largestSize.y = mLargestDisplayHeight;
7225+
}
7226+
}
7227+
72027228
public void setForcedDisplaySize(int longDimen, int shortDimen) {
72037229
synchronized(mWindowMap) {
72047230
int width, height;
@@ -9391,14 +9417,25 @@ void dumpWindowsLocked(FileDescriptor fd, PrintWriter pw, boolean dumpAll,
93919417
pw.println();
93929418
if (mDisplay != null) {
93939419
pw.print(" Display: init="); pw.print(mInitialDisplayWidth); pw.print("x");
9394-
pw.print(mInitialDisplayHeight); pw.print(" base=");
9395-
pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
9420+
pw.print(mInitialDisplayHeight);
9421+
if (mInitialDisplayWidth != mBaseDisplayWidth
9422+
|| mInitialDisplayHeight != mBaseDisplayHeight) {
9423+
pw.print(" base=");
9424+
pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
9425+
}
9426+
final int rawWidth = mDisplay.getRawWidth();
9427+
final int rawHeight = mDisplay.getRawHeight();
9428+
if (rawWidth != mCurDisplayWidth || rawHeight != mCurDisplayHeight) {
9429+
pw.print(" raw="); pw.print(rawWidth); pw.print("x"); pw.print(rawHeight);
9430+
}
93969431
pw.print(" cur=");
93979432
pw.print(mCurDisplayWidth); pw.print("x"); pw.print(mCurDisplayHeight);
93989433
pw.print(" app=");
93999434
pw.print(mAppDisplayWidth); pw.print("x"); pw.print(mAppDisplayHeight);
9400-
pw.print(" raw="); pw.print(mDisplay.getRawWidth());
9401-
pw.print("x"); pw.println(mDisplay.getRawHeight());
9435+
pw.print(" rng="); pw.print(mSmallestDisplayWidth);
9436+
pw.print("x"); pw.print(mSmallestDisplayHeight);
9437+
pw.print("-"); pw.print(mLargestDisplayWidth);
9438+
pw.print("x"); pw.println(mLargestDisplayHeight);
94029439
} else {
94039440
pw.println(" NO DISPLAY");
94049441
}

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeWindowManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public int getMaximumSizeDimension() throws RemoteException {
7777
return 0;
7878
}
7979

80+
@Override
81+
public void getCurrentSizeRange(Point smallestSize, Point largestSize) {
82+
}
83+
8084
@Override
8185
public void getDisplaySize(Point arg0) throws RemoteException {
8286
}

0 commit comments

Comments
 (0)