From 08e43b148f05006af61431dbdb7a35db86610c21 Mon Sep 17 00:00:00 2001 From: Abhas Vohra Date: Thu, 30 Sep 2021 19:13:04 +0000 Subject: [PATCH 1/2] Fix: issues in merged develop Squashed commit of the following: commit a65057670fcbfb3329b2ff53d11f13b755986568 Author: Abhas Vohra Date: Thu Sep 30 16:57:40 2021 +0530 Found issue with ImageService with BG threading --- .../appnexus/opensdk/VisibilityDetector.java | 2 +- .../appnexus/opensdk/utils/ImageService.java | 3 ++- .../com/appnexussdktestapp/util/Utility.kt | 26 +++++++++++-------- .../MARLoadAndDisplayActivity.kt | 6 +++-- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/sdk/src/com/appnexus/opensdk/VisibilityDetector.java b/sdk/src/com/appnexus/opensdk/VisibilityDetector.java index d94f0f16..060531da 100644 --- a/sdk/src/com/appnexus/opensdk/VisibilityDetector.java +++ b/sdk/src/com/appnexus/opensdk/VisibilityDetector.java @@ -67,7 +67,7 @@ private boolean remove(List> list, View reference) for (Iterator> iterator = list.iterator(); iterator.hasNext(); ) { WeakReference weakRef = iterator.next(); if (weakRef.get() == reference) { - if (!(reference instanceof BannerAdView)) { + if (reference != null && !(reference instanceof BannerAdView)) { reference.setTag(R.string.native_view_tag, null); } iterator.remove(); diff --git a/sdk/src/com/appnexus/opensdk/utils/ImageService.java b/sdk/src/com/appnexus/opensdk/utils/ImageService.java index b0fd4c6a..738112dd 100644 --- a/sdk/src/com/appnexus/opensdk/utils/ImageService.java +++ b/sdk/src/com/appnexus/opensdk/utils/ImageService.java @@ -74,7 +74,8 @@ public void execute() { return; } if (imageUrlMap != null && !imageUrlMap.isEmpty()) { - for (Map.Entry pairs : imageUrlMap.entrySet()) { + HashMap imageUrlMapCopy = new HashMap<>(imageUrlMap); + for (Map.Entry pairs : imageUrlMapCopy.entrySet()) { ImageDownloader downloader = new ImageDownloader(imageReceiver, (String) pairs.getKey(), (String) pairs.getValue(), this); Clog.d(Clog.baseLogTag, "Downloading " + pairs.getKey() + " from url: " + pairs.getValue()); downloader.execute(); diff --git a/tests/AppNexusSDKTestApp/app/src/androidTest/java/appnexus/com/appnexussdktestapp/util/Utility.kt b/tests/AppNexusSDKTestApp/app/src/androidTest/java/appnexus/com/appnexussdktestapp/util/Utility.kt index 03a907d2..e3ec32ad 100644 --- a/tests/AppNexusSDKTestApp/app/src/androidTest/java/appnexus/com/appnexussdktestapp/util/Utility.kt +++ b/tests/AppNexusSDKTestApp/app/src/androidTest/java/appnexus/com/appnexussdktestapp/util/Utility.kt @@ -5,7 +5,7 @@ import android.os.Handler import android.os.Looper import android.util.Log import android.view.View -import java.lang.ref.WeakReference +import org.junit.Assert class Utility { companion object { @@ -14,21 +14,25 @@ class Utility { Handler(Looper.getMainLooper()).post({ val vDetector = Class.forName("com.appnexus.opensdk.VisibilityDetector") - val create = vDetector.getDeclaredMethod("create", WeakReference::class.java) - create.isAccessible = true - val weakReference = WeakReference(View(context)) - val vDetInst = create.invoke(null, weakReference) + val getInstance = vDetector.getDeclaredMethod("getInstance") + getInstance.isAccessible = true + val viewReference = View(context) + val vDetInst = getInstance.invoke(null) - val destroy = vDetector.getDeclaredMethod("destroy", WeakReference::class.java) + val addVisibilityListener = vDetector.getDeclaredMethod("addVisibilityListener", View::class.java) + addVisibilityListener.isAccessible = true + addVisibilityListener.invoke(vDetInst, viewReference) + + val destroy = vDetector.getDeclaredMethod("destroy", View::class.java) destroy.isAccessible = true - destroy.invoke(vDetInst, weakReference) + destroy.invoke(vDetInst, viewReference) - val declaredField = vDetector.getDeclaredField("viewListenerMap") + val declaredField = vDetector.getDeclaredField("viewList") declaredField.isAccessible = true - var map = declaredField.get(vDetInst) as HashMap - Log.e("VISIBILITY", " Size: ${checkZero} ${map.size}"); + var list = declaredField.get(vDetInst) as List + Log.e("VISIBILITY", " Size: ${checkZero} ${list.size}"); - junit.framework.Assert.assertTrue(map.size == checkZero) + Assert.assertEquals(checkZero, list.size) }) } diff --git a/tests/AppNexusSDKTestApp/app/src/main/java/appnexus/com/appnexussdktestapp/MARLoadAndDisplayActivity.kt b/tests/AppNexusSDKTestApp/app/src/main/java/appnexus/com/appnexussdktestapp/MARLoadAndDisplayActivity.kt index a2682c92..0eec6f22 100644 --- a/tests/AppNexusSDKTestApp/app/src/main/java/appnexus/com/appnexussdktestapp/MARLoadAndDisplayActivity.kt +++ b/tests/AppNexusSDKTestApp/app/src/main/java/appnexus/com/appnexussdktestapp/MARLoadAndDisplayActivity.kt @@ -179,7 +179,7 @@ class MARLoadAndDisplayActivity : Activity() { idlingResource.decrement() } - override fun onAdFailed(errorcode: ResultCode, adResponseInfo: ANAdResponseInfo) { + override fun onAdFailed(errorcode: ResultCode, adResponseInfo: ANAdResponseInfo?) { msg += "Native Ad Failed:$errorcode\n" toast() if (!idlingResource.isIdleNow) @@ -313,7 +313,9 @@ class MARLoadAndDisplayActivity : Activity() { onLazyAdLoaded = true arrayListAd.add(adView) if (displayAd) - recyclerListAdView.adapter!!.notifyDataSetChanged() + Handler(Looper.getMainLooper()).post({ + recyclerListAdView.adapter!!.notifyDataSetChanged() + }) if (!idlingResource.isIdleNow) idlingResource.decrement() } From d718b6a37acedc9cc992db50f13af376c98522c2 Mon Sep 17 00:00:00 2001 From: Abhas Vohra Date: Fri, 1 Oct 2021 02:25:20 +0530 Subject: [PATCH 2/2] Updated SDK version --- .../java/appnexus/com/appnexussdktestapp/util/Utility.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/AppNexusSDKTestApp/app/src/androidTest/java/appnexus/com/appnexussdktestapp/util/Utility.kt b/tests/AppNexusSDKTestApp/app/src/androidTest/java/appnexus/com/appnexussdktestapp/util/Utility.kt index e3ec32ad..05812d70 100644 --- a/tests/AppNexusSDKTestApp/app/src/androidTest/java/appnexus/com/appnexussdktestapp/util/Utility.kt +++ b/tests/AppNexusSDKTestApp/app/src/androidTest/java/appnexus/com/appnexussdktestapp/util/Utility.kt @@ -15,6 +15,7 @@ class Utility { Handler(Looper.getMainLooper()).post({ val vDetector = Class.forName("com.appnexus.opensdk.VisibilityDetector") val getInstance = vDetector.getDeclaredMethod("getInstance") + getInstance.isAccessible = true val viewReference = View(context) val vDetInst = getInstance.invoke(null)