Android: split native template creation from asset binding; exact WebView CSS size

NativeAdTemplate.create / fill / build so the layout and styling can be exercised without a filled ad.
webViewCss divides in double precision (frames no longer carry float rounding noise).
This commit is contained in:
CRTkafa 2026-10-10 04:25:56 +08:00
parent 1e0be782da
commit 213b17b5c6
2 changed files with 55 additions and 17 deletions

View file

@ -50,7 +50,45 @@ final class NativeAdTemplate {
private NativeAdTemplate() {} private NativeAdTemplate() {}
/** The template's asset views, created and styled but not filled. */
static final class Views {
final NativeAdLayout root;
final TextView title;
final TextView advertiser;
final TextView body;
final ImageView icon;
final Button cta;
@Nullable
final LevelPlayMediaView media;
Views(NativeAdLayout root, TextView title, TextView advertiser, TextView body, ImageView icon, Button cta, @Nullable LevelPlayMediaView media) {
this.root = root;
this.title = title;
this.advertiser = advertiser;
this.body = body;
this.icon = icon;
this.cta = cta;
this.media = media;
}
}
/** Renders a loaded ad: create, fill from the ad's assets, hand the views to the SDK and register them. */
static NativeAdLayout build(Context ctx, String template, NativeAdStyle style, LevelPlayNativeAd ad) { static NativeAdLayout build(Context ctx, String template, NativeAdStyle style, LevelPlayNativeAd ad) {
Views v = create(ctx, template, style);
fill(v, ad.getTitle(), ad.getAdvertiser(), ad.getBody(), ad.getCallToAction(), ad.getIcon());
v.root.setTitleView(v.title);
v.root.setAdvertiserView(v.advertiser);
v.root.setBodyView(v.body);
v.root.setIconView(v.icon);
v.root.setCallToActionView(v.cta);
if (v.media != null) v.root.setMediaView(v.media);
v.root.registerNativeAdViews(ad);
return v.root;
}
static Views create(Context ctx, String template, NativeAdStyle style) {
boolean medium = "medium".equals(template); boolean medium = "medium".equals(template);
NativeAdLayout root = new NativeAdLayout(ctx); NativeAdLayout root = new NativeAdLayout(ctx);
GradientDrawable bg = new GradientDrawable(); GradientDrawable bg = new GradientDrawable();
@ -172,23 +210,23 @@ final class NativeAdTemplate {
root.addView(row, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); root.addView(row, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
} }
// Assets. return new Views(root, title, advertiser, body, icon, cta, media);
setOrHide(title, ad.getTitle()); }
setOrHide(advertiser, ad.getAdvertiser());
setOrHide(body, ad.getBody());
String ctaText = ad.getCallToAction();
if (ctaText != null && !ctaText.isEmpty()) cta.setText(ctaText);
else cta.setText(R.string.levelplay_native_ad_cta_fallback);
bindIcon(icon, ad.getIcon());
root.setTitleView(title); static void fill(
root.setAdvertiserView(advertiser); Views v,
root.setBodyView(body); @Nullable String title,
root.setIconView(icon); @Nullable String advertiser,
root.setCallToActionView(cta); @Nullable String body,
if (media != null) root.setMediaView(media); @Nullable String callToAction,
root.registerNativeAdViews(ad); @Nullable NativeAdDataInterface.Image icon
return root; ) {
setOrHide(v.title, title);
setOrHide(v.advertiser, advertiser);
setOrHide(v.body, body);
if (callToAction != null && !callToAction.isEmpty()) v.cta.setText(callToAction);
else v.cta.setText(R.string.levelplay_native_ad_cta_fallback);
bindIcon(v.icon, icon);
} }
private static TextView text(Context ctx, int color, double defaultSize, NativeAdStyle.Font font, boolean bold) { private static TextView text(Context ctx, int color, double defaultSize, NativeAdStyle.Font font, boolean bold) {

View file

@ -94,7 +94,7 @@ final class OverlayHost {
/** The WebView's own rect in CSS px (origin 0, 0). */ /** The WebView's own rect in CSS px (origin 0, 0). */
double[] webViewCss() { double[] webViewCss() {
return OverlayMath.rect(0, 0, webView.getWidth() / density, webView.getHeight() / density); return OverlayMath.rect(0, 0, webView.getWidth() / (double) density, webView.getHeight() / (double) density);
} }
/** WebView rect ∩ (window − system bars − display cutout), in WebView-local CSS px (design §6.3). */ /** WebView rect ∩ (window − system bars − display cutout), in WebView-local CSS px (design §6.3). */