|
22 | 22 | import android.content.Intent; |
23 | 23 | import android.content.pm.PackageManager; |
24 | 24 | import android.content.res.Configuration; |
| 25 | +import android.graphics.Insets; |
25 | 26 | import android.test.ActivityInstrumentationTestCase2; |
26 | 27 | import android.util.DisplayMetrics; |
| 28 | +import android.util.Log; |
27 | 29 | import android.view.Display; |
| 30 | +import android.view.WindowInsets; |
28 | 31 | import android.view.WindowManager; |
29 | 32 |
|
30 | 33 | import androidx.test.uiautomator.UiDevice; |
@@ -247,7 +250,30 @@ private int getScreenDensity() { |
247 | 250 | private int getScreenSize() { |
248 | 251 | Context context = getInstrumentation().getTargetContext(); |
249 | 252 | Configuration config = context.getResources().getConfiguration(); |
250 | | - return config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; |
| 253 | + final int configScreenSize = config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; |
| 254 | + final int minScreenSizeDp = Math.min(config.screenWidthDp, config.screenHeightDp); |
| 255 | + // The insets size may affect screenSizeDp in different orientations. E.g., the short side |
| 256 | + // is 720dp as the width in portrait orientation, but when the short side is the height in |
| 257 | + // landscape orientation, the value will be smaller than 720dp because the insets of |
| 258 | + // system bars may occupy a little space. Then the screen size from Configuration will be |
| 259 | + // LARGE in landscape and XLARGE in portrait. So below calculation allows to return a |
| 260 | + // smaller size definition if the size excluding insets is lower than the size threshold. |
| 261 | + final Insets insets = getActivity().getWindowManager().getCurrentWindowMetrics() |
| 262 | + .getWindowInsets().getInsetsIgnoringVisibility(WindowInsets.Type.systemBars()); |
| 263 | + final int insetsSize = Math.max(insets.top + insets.bottom, insets.left + insets.right); |
| 264 | + final int toleranceSizeDp = (int) (insetsSize / |
| 265 | + ((float) config.densityDpi / DisplayMetrics.DENSITY_DEFAULT) + 0.5f); |
| 266 | + Log.i("ActivityManagerMemoryClassTest", "getScreenSize: config=" + config |
| 267 | + + " insets=" + insets + " toleranceSizeDp=" + toleranceSizeDp); |
| 268 | + if (configScreenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE |
| 269 | + && (minScreenSizeDp - toleranceSizeDp < 720)) { |
| 270 | + return Configuration.SCREENLAYOUT_SIZE_LARGE; |
| 271 | + } |
| 272 | + if (configScreenSize == Configuration.SCREENLAYOUT_SIZE_LARGE |
| 273 | + && (minScreenSizeDp - toleranceSizeDp < 480)) { |
| 274 | + return Configuration.SCREENLAYOUT_SIZE_NORMAL; |
| 275 | + } |
| 276 | + return configScreenSize; |
251 | 277 | } |
252 | 278 |
|
253 | 279 | private void assertMemoryForScreenDensity(int memoryClass, int screenDensity, int screenSize) { |
|
0 commit comments