Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite deprecated registerView() in test examples #671

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.android.gms.ads.formats.OnAdManagerAdViewLoadedListener;
import com.google.android.gms.ads.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeCustomFormatAd;
import com.google.common.collect.Lists;

import org.prebid.mobile.NativeAdUnit;
import org.prebid.mobile.NativeDataAsset;
Expand Down Expand Up @@ -69,34 +70,24 @@ private void inflatePrebidNativeAd(
ViewGroup wrapper
) {
View nativeContainer = View.inflate(wrapper.getContext(), R.layout.layout_native, null);
ad.registerView(nativeContainer, new PrebidNativeAdEventListener() {
@Override
public void onAdClicked() {
}

@Override
public void onAdImpression() {
}

@Override
public void onAdExpired() {
}
});
ImageView icon = nativeContainer.findViewById(R.id.imgIcon);

ImageView icon = nativeContainer.findViewById(R.id.imgIcon);
ImageUtils.download(ad.getIconUrl(), icon);

TextView title = nativeContainer.findViewById(R.id.tvTitle);
title.setText(ad.getTitle());
ImageView image = nativeContainer.findViewById(R.id.imgImage);

ImageView image = nativeContainer.findViewById(R.id.imgImage);
ImageUtils.download(ad.getImageUrl(), image);

TextView description = nativeContainer.findViewById(R.id.tvDesc);
description.setText(ad.getDescription());

Button cta = nativeContainer.findViewById(R.id.btnCta);
cta.setText(ad.getCallToAction());
wrapper.addView(nativeContainer);

ad.registerView(nativeContainer, Lists.newArrayList(icon, title, image, description, cta), new SafeNativeListener());
}

private AdLoader createAdLoader(
Expand Down Expand Up @@ -203,4 +194,24 @@ protected void onDestroy() {
unifiedNativeAd.destroy();
}
}

/**
* It's important to use class implementation instead of anonymous object.
*/
private static class SafeNativeListener implements PrebidNativeAdEventListener {

@Override
public void onAdClicked() {
}

@Override
public void onAdImpression() {
}

@Override
public void onAdExpired() {
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAd.OnNativeAdLoadedListener
import com.google.android.gms.ads.nativead.NativeCustomFormatAd
import com.google.android.gms.ads.nativead.NativeCustomFormatAd.OnCustomFormatAdLoadedListener
import com.google.common.collect.Lists
import org.prebid.mobile.*
import org.prebid.mobile.NativeEventTracker.EVENT_TRACKING_METHOD
import org.prebid.mobile.addendum.AdViewUtils
Expand Down Expand Up @@ -82,13 +83,7 @@ class GamOriginalApiNativeInAppActivity : BaseAdActivity() {
}

private fun inflatePrebidNativeAd(ad: PrebidNativeAd, wrapper: ViewGroup) {

val nativeContainer = View.inflate(wrapper.context, R.layout.layout_native, null)
ad.registerView(nativeContainer, object : PrebidNativeAdEventListener {
override fun onAdClicked() {}
override fun onAdImpression() {}
override fun onAdExpired() {}
})

val icon = nativeContainer.findViewById<ImageView>(R.id.imgIcon)
ImageUtils.download(ad.iconUrl, icon)
Expand All @@ -105,6 +100,8 @@ class GamOriginalApiNativeInAppActivity : BaseAdActivity() {
val cta = nativeContainer.findViewById<Button>(R.id.btnCta)
cta.text = ad.callToAction

ad.registerView(nativeContainer, Lists.newArrayList(icon, title, image, description, cta), SafeNativeListener())

wrapper.addView(nativeContainer)
}

Expand Down Expand Up @@ -202,6 +199,16 @@ class GamOriginalApiNativeInAppActivity : BaseAdActivity() {
}
}

/**
* It's important to use class implementation instead of anonymous object.
*/
private class SafeNativeListener : PrebidNativeAdEventListener {
override fun onAdClicked() {}
override fun onAdImpression() {}
override fun onAdExpired() {}
}


override fun onDestroy() {
super.onDestroy()
adView?.destroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.google.android.gms.ads.admanager.AdManagerAdView
import com.google.android.gms.ads.formats.OnAdManagerAdViewLoadedListener
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeCustomFormatAd
import com.google.common.collect.Lists
import org.prebid.mobile.*
import org.prebid.mobile.addendum.AdViewUtils
import org.prebid.mobile.prebidkotlindemo.R
Expand Down Expand Up @@ -153,26 +154,36 @@ class GamRenderingApiNativeActivity : BaseAdActivity() {
ad: PrebidNativeAd, wrapper: ViewGroup
) {
val nativeContainer = View.inflate(wrapper.context, R.layout.layout_native, null)
ad.registerView(nativeContainer, object : PrebidNativeAdEventListener {
override fun onAdClicked() {}

override fun onAdImpression() {}

override fun onAdExpired() {}
})
val icon = nativeContainer.findViewById<ImageView>(R.id.imgIcon)
ImageUtils.download(ad.iconUrl, icon)

val title = nativeContainer.findViewById<TextView>(R.id.tvTitle)
title.text = ad.title

val image = nativeContainer.findViewById<ImageView>(R.id.imgImage)
ImageUtils.download(ad.imageUrl, image)

val description = nativeContainer.findViewById<TextView>(R.id.tvDesc)
description.text = ad.description

val cta = nativeContainer.findViewById<Button>(R.id.btnCta)
cta.text = ad.callToAction

ad.registerView(nativeContainer, Lists.newArrayList(icon, title, image, description, cta), SafeNativeListener())

wrapper.addView(nativeContainer)
}

/**
* It's important to use class implementation instead of anonymous object.
*/
private class SafeNativeListener : PrebidNativeAdEventListener {
override fun onAdClicked() {}
override fun onAdImpression() {}
override fun onAdExpired() {}
}


override fun onDestroy() {
super.onDestroy()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.prebid.mobile.renderingtestapp.plugplay.bidding.gam.original

import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.AdSize
Expand All @@ -16,6 +14,7 @@ import com.google.android.gms.ads.admanager.AdManagerAdView
import com.google.android.gms.ads.formats.OnAdManagerAdViewLoadedListener
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeCustomFormatAd
import com.google.common.collect.Lists
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand All @@ -25,7 +24,7 @@ import org.prebid.mobile.eventhandlers.utils.GamUtils
import org.prebid.mobile.renderingtestapp.R
import org.prebid.mobile.renderingtestapp.plugplay.bidding.ppm.PpmNativeFragment
import org.prebid.mobile.renderingtestapp.utils.loadImage
import org.prebid.mobile.renderingtestapp.widgets.EventCounterView
import java.lang.ref.WeakReference

class GamOriginalNativeInAppFragment : PpmNativeFragment() {

Expand Down Expand Up @@ -70,35 +69,27 @@ class GamOriginalNativeInAppFragment : PpmNativeFragment() {
wrapper: ViewGroup
) {
val nativeContainer = View.inflate(wrapper.context, R.layout.lyt_native_ad, null)
ad.registerView(nativeContainer, object : PrebidNativeAdEventListener {
override fun onAdClicked() {
Log.d(TAG, "onAdClicked called: ")
onMainThread {
events.clicked(true)
}
}

override fun onAdImpression() {
Log.d(TAG, "onAdImpression called: ")
onMainThread {
events.impression(true)
}
}

override fun onAdExpired() {
Log.d(TAG, "onAdExpired called: ")
onMainThread {
events.expired(true)
}
}
})

loadImage(binding.ivNativeIcon!!, ad.iconUrl)
loadImage(binding.ivNativeIcon, ad.iconUrl)
binding.tvNativeTitle.text = ad.title
loadImage(binding.ivNativeMain!!, ad.imageUrl)
loadImage(binding.ivNativeMain, ad.imageUrl)
binding.tvNativeBody.text = ad.description
binding.btnNativeAction.text = ad.callToAction
binding.btnNativeAction.isEnabled = true

ad.registerView(
nativeContainer,
Lists.newArrayList(
binding.ivNativeIcon,
binding.ivNativeMain,
binding.tvNativeTitle,
binding.tvNativeBrand,
binding.tvNativeBody,
binding.btnNativeAction
),
SafeNativeListener(events)
)

binding.adContainer.addView(nativeContainer)
}

Expand Down Expand Up @@ -192,6 +183,42 @@ class GamOriginalNativeInAppFragment : PpmNativeFragment() {
return adUnit
}

/**
* It's important to use class implementation instead of anonymous object.
*/
private class SafeNativeListener(events: Events) : PrebidNativeAdEventListener {

private val eventsReference = WeakReference(events)

override fun onAdClicked() {
Log.d(TAG, "onAdClicked called: ")
runOnMainThread {
eventsReference.get()?.clicked(true)
}
}

override fun onAdImpression() {
Log.d(TAG, "onAdImpression called: ")
runOnMainThread {
eventsReference.get()?.impression(true)
}
}

override fun onAdExpired() {
Log.d(TAG, "onAdExpired called: ")
runOnMainThread {
eventsReference.get()?.expired(true)
}
}

private fun runOnMainThread(function: () -> Unit) {
Handler(Looper.getMainLooper()).postAtFrontOfQueue {
function()
}
}

}

override fun onDestroy() {
super.onDestroy()
adView?.destroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.prebid.mobile.renderingtestapp.databinding.FragmentNativeBinding
import org.prebid.mobile.renderingtestapp.plugplay.config.AdConfiguratorDialogFragment
import org.prebid.mobile.renderingtestapp.utils.BaseEvents
import org.prebid.mobile.renderingtestapp.utils.loadImage
import java.lang.ref.WeakReference

open class PpmNativeFragment : AdFragment() {

Expand Down Expand Up @@ -104,7 +105,7 @@ open class PpmNativeFragment : AdFragment() {
binding.ivNativeMain,
binding.ivNativeIcon
),
NativeListener(events)
SafeNativeListener(events)
)

binding.tvNativeTitle.text = nativeAd.title
Expand All @@ -120,20 +121,22 @@ open class PpmNativeFragment : AdFragment() {
}
}

protected class NativeListener(private val events: Events) : PrebidNativeAdEventListener {
protected class SafeNativeListener(events: Events) : PrebidNativeAdEventListener {

private val eventsReference = WeakReference(events)

override fun onAdClicked() {
events.clicked(true)
eventsReference.get()?.clicked(true)
}

override fun onAdImpression() {
Handler(Looper.getMainLooper()).post {
events.impression(true)
eventsReference.get()?.impression(true)
}
}

override fun onAdExpired() {
events.expired(true)
eventsReference.get()?.expired(true)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class PpmNativeLinksFragment : PpmNativeFragment() {
override fun inflateViewContent(nativeAd: PrebidNativeAd) {
findView<EventCounterView>(R.id.btnAdDisplayed).isEnabled = true

nativeAd.registerViewList(
nativeAd.registerView(
findView(R.id.adContainer),
listOf(
findView(R.id.btnNativeLinkRoot),
findView(R.id.btnNativeDeeplinkOk),
findView(R.id.btnNativeDeeplinkFallback),
findView(R.id.btnNativeLinkUrl)
),
NativeListener(events)
SafeNativeListener(events)
)

findView<Button>(R.id.btnNativeLinkRoot).text = nativeAd.callToAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class NativeFeedAdapter(
this.nativeAd = nativeAd

nativeAdLayout?.let {
nativeAd.registerViewList(
nativeAd.registerView(
it,
listOf(
it.findViewById(R.id.tvNativeTitle),
Expand All @@ -80,7 +80,7 @@ class NativeFeedAdapter(
it.findViewById(R.id.ivNativeIcon),
it.findViewById(R.id.ivNativeMain)
),
createListener()
SafeNativeListener()
)

it.findViewById<TextView>(R.id.tvNativeTitle)?.text = nativeAd.title
Expand All @@ -93,12 +93,10 @@ class NativeFeedAdapter(
}
}

private fun createListener(): PrebidNativeAdEventListener {
return object : PrebidNativeAdEventListener {
override fun onAdClicked() {}
override fun onAdImpression() {}
override fun onAdExpired() {}
}
private class SafeNativeListener : PrebidNativeAdEventListener {
override fun onAdClicked() {}
override fun onAdImpression() {}
override fun onAdExpired() {}
}

}
Loading