The code below is not working due to deprecated methods getDefaultDisplay();
and getMetrics()
in API 30
.
private AdSize getAdSize() {
Display display = getWindowManager().getDefaultDisplay();//deprecated
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);//deprecated
float density = outMetrics.density;
float widthPixels = adContainerView.getWidth();
if (widthPixels == 0) {
widthPixels = outMetrics.widthPixels;
}
int adWidth = (int) (widthPixels / density);
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
Google
requires use WindowMetrics#getBounds() to get the dimensions of the application window area, and Configuration#densityDpi to get the current density.
I found these methods for API 30
but I cannot figure out how to use them.
final WindowMetrics metrics = windowManager.getCurrentMetrics();
// Gets all excluding insets
final WindowInsets windowInsets = metrics.getWindowInsets();
Insets insets = windowInsets.getInsets(WindowInsets.Type.navigationBars());
final DisplayCutout cutout = windowInsets.getCutout();
if (cutout != null) {
final Insets cutoutSafeInsets = Insets.of(cutout.getSafeInsetsLeft(), ...);
insets = insets.max(insets, cutoutSafeInsets);
}
int insetsWidth = insets.right + insets.left;
int insetsHeight = insets.top + insets.bottom;
// Legacy size that Display#getSize reports
final Size legacySize = new Size(metrics.getWidth() - insetsWidth,
metrics.getHeight() - insetsHeight);
Has anyone faced this?