3using YandexMobileAds.Base;
12 internal static class YandexBannerSizeHelper
15 private const int AdaptiveMinHeightDp = 50;
18 private const float AdaptiveMaxScreenHeightMultiplier = 0.15f;
21 private const float AdaptiveBaselineHeightToWidthRatio = 50f / 320f;
24 private const float RuntimeSizeToleranceMultiplier = 1.1f;
33 public static BannerAdSize CreateRuntimeBannerSize(BannerOptions bannerOptions)
35 var width = GetBannerWidthDp(bannerOptions);
36#if !LEGACY_YANDEX_MOBILE_ADS
37 return bannerOptions.Type == BannerType.Inline
38 ? BannerAdSize.Inline(width, bannerOptions.InlineMaxHeight)
39 : BannerAdSize.Sticky(width);
41 return bannerOptions.Type == BannerType.Inline
42 ? BannerAdSize.InlineSize(width, bannerOptions.InlineMaxHeight)
43 : BannerAdSize.StickySize(width);
56 public static float GetBannerHeightPixels(
57 BannerOptions bannerOptions,
58 BannerAdSize runtimeBannerSize,
59 float adaptiveReserveBias) =>
60 bannerOptions.Type == BannerType.Inline
61 ? GetEstimatedInlineHeightPixels(bannerOptions, adaptiveReserveBias)
62 : GetEstimatedStickyHeightPixels(bannerOptions, runtimeBannerSize, adaptiveReserveBias);
72 public static float GetBannerWidthPixels(BannerOptions bannerOptions, BannerAdSize runtimeBannerSize)
74 var runtimeWidth = GetRuntimeBannerWidthPixels(runtimeBannerSize);
75 return runtimeWidth > 0f
77 : GetConfiguredBannerWidthPixels(bannerOptions);
86 private static float GetScreenWidthPixels() => Screen.safeArea.width > 0f
87 ? Screen.safeArea.width
96 private static float GetScreenHeightPixels() => Screen.safeArea.height > 0f
97 ? Screen.safeArea.height
104 private static int GetScreenWidthDp() => ScreenUtils.ConvertPixelsToDp((
int)GetScreenWidthPixels());
113 private static float ConvertDpToPixels(
int dp)
118 var safeAreaWidthDp = GetScreenWidthDp();
119 return safeAreaWidthDp > 0
120 ? dp * (GetScreenWidthPixels() / safeAreaWidthDp)
131 private static int GetBannerWidthDp(BannerOptions bannerOptions) => bannerOptions.Width < 0
133 : bannerOptions.Width;
142 private static float GetConfiguredBannerWidthPixels(BannerOptions bannerOptions) => bannerOptions.Width > 0
143 ? ConvertDpToPixels(bannerOptions.Width)
144 : GetScreenWidthPixels();
154 private static float ConvertRuntimeDimensionToPixels(
int value,
float maxPixels)
156 if (value <= 0 || maxPixels <= 0f)
159 var convertedValue = ConvertDpToPixels(value);
160 if (convertedValue <= maxPixels * RuntimeSizeToleranceMultiplier)
161 return convertedValue;
163 return value <= maxPixels * RuntimeSizeToleranceMultiplier
175 private static float GetRuntimeBannerWidthPixels(BannerAdSize bannerSize) =>
176 bannerSize is not { Width: > 0 }
178 : ConvertRuntimeDimensionToPixels(bannerSize.Width, GetScreenWidthPixels());
187 private static float GetRuntimeStickyHeightPixels(BannerAdSize bannerSize)
189 if (bannerSize is not { Height: > 0 })
192 var minHeight = GetAdaptiveMinHeightPixels();
193 var maxHeight = GetAdaptiveMaxHeightPixels();
194 var runtimeHeight = ConvertRuntimeDimensionToPixels(bannerSize.Height, maxHeight);
196 return runtimeHeight > 0f
197 ? ClampAdaptiveHeightPixels(runtimeHeight, minHeight, maxHeight)
207 private static float GetAdaptiveMinHeightPixels() => ConvertDpToPixels(AdaptiveMinHeightDp);
215 private static float GetAdaptiveMaxHeightPixels() =>
216 GetScreenHeightPixels() * AdaptiveMaxScreenHeightMultiplier;
225 private static float GetInlineReserveMaxHeightPixels(BannerOptions bannerOptions)
227 var screenMaxHeight = GetAdaptiveMaxHeightPixels();
229 var configuredMaxHeight = ConvertDpToPixels(bannerOptions.InlineMaxHeight);
230 return configuredMaxHeight > 0f
231 ? Mathf.Min(screenMaxHeight, configuredMaxHeight)
244 private static float ClampAdaptiveHeightPixels(
float height,
float minHeight,
float maxHeight) =>
245 maxHeight <= 0f ? Mathf.Max(0f, height) :
246 minHeight > maxHeight ? maxHeight : Mathf.Clamp(height, minHeight, maxHeight);
257 private static float EstimateAdaptiveHeightPixels(
258 BannerOptions bannerOptions,
259 float maxHeightPixels,
260 float adaptiveReserveBias)
262 var width = GetBannerWidthPixels(bannerOptions,
null);
263 var minHeight = GetAdaptiveMinHeightPixels();
264 var widthBasedHeight = width * AdaptiveBaselineHeightToWidthRatio;
265 var reserveBias = Mathf.Clamp01(adaptiveReserveBias);
266 var biasedHeight = Mathf.Lerp(widthBasedHeight, maxHeightPixels, reserveBias);
268 return ClampAdaptiveHeightPixels(biasedHeight, minHeight, maxHeightPixels);
279 private static float GetEstimatedInlineHeightPixels(BannerOptions bannerOptions,
float adaptiveReserveBias) =>
280 EstimateAdaptiveHeightPixels(
282 GetInlineReserveMaxHeightPixels(bannerOptions),
283 adaptiveReserveBias);
294 private static float GetEstimatedStickyHeightPixels(
295 BannerOptions bannerOptions,
296 BannerAdSize runtimeBannerSize,
297 float adaptiveReserveBias)
299 var runtimeHeight = GetRuntimeStickyHeightPixels(runtimeBannerSize);
300 return runtimeHeight > 0f
302 : EstimateAdaptiveHeightPixels(bannerOptions, GetAdaptiveMaxHeightPixels(), adaptiveReserveBias);