From f9daa0131cf84bf6193623a0752e1772b4d824c6 Mon Sep 17 00:00:00 2001 From: "nakirekommula@google.com" Date: Thu, 7 Sep 2023 14:49:40 -0700 Subject: [PATCH 1/2] Innternal Updates --- gma/src/android/gma_android.cc | 19 ++++++++++++++++--- gma/src/common/gma_common.cc | 6 +++--- gma/src/common/gma_common.h | 4 ++-- gma/src/common/native_ad.cc | 4 ++++ gma/src/common/native_ad_internal.cc | 7 +++++-- gma/src/common/native_ad_internal.h | 9 ++++++++- .../include/firebase/gma/internal/native_ad.h | 3 +++ gma/src/ios/GADNativeAdCpp.h | 3 +++ gma/src/ios/native_ad_internal_ios.mm | 12 ++++++++++-- .../gma/internal/cpp/NativeAdHelper.java | 14 ++++++++++++-- 10 files changed, 66 insertions(+), 15 deletions(-) diff --git a/gma/src/android/gma_android.cc b/gma/src/android/gma_android.cc index bcc7e35949..0e5b999dc3 100644 --- a/gma/src/android/gma_android.cc +++ b/gma/src/android/gma_android.cc @@ -904,6 +904,7 @@ void JNI_completeLoadedAd(JNIEnv* env, jclass clazz, jlong data_ptr, void JNI_NativeAd_completeLoadedAd(JNIEnv* env, jclass clazz, jlong data_ptr, jlong native_internal_data_ptr, jobject j_icon, jobjectArray j_images, + jobject j_adchoices_icon, jobject j_response_info) { FIREBASE_ASSERT(env); FIREBASE_ASSERT(data_ptr); @@ -922,10 +923,22 @@ void JNI_NativeAd_completeLoadedAd(JNIEnv* env, jclass clazz, jlong data_ptr, // Invoke a friend of NativeAdInternal to update its icon image asset. GmaInternal::InsertNativeInternalImage(native_ad_internal, icon_internal, - true, true); + "icon", true); env->DeleteLocalRef(j_icon); } + // getAdChoicesInfo().getImages() can return an empty list and a valid ad can + // exist without an adchoices icon image. + if (j_adchoices_icon != nullptr) { + NativeAdImageInternal adchoices_icon_internal; + adchoices_icon_internal.native_ad_image = j_adchoices_icon; + + // Invoke a friend of NativeAdInternal to update its icon image asset. + GmaInternal::InsertNativeInternalImage( + native_ad_internal, adchoices_icon_internal, "adchoices_icon", true); + env->DeleteLocalRef(j_adchoices_icon); + } + const size_t len = env->GetArrayLength(j_images); // Loop through images array. for (size_t i = 0; i < len; ++i) { @@ -933,7 +946,7 @@ void JNI_NativeAd_completeLoadedAd(JNIEnv* env, jclass clazz, jlong data_ptr, NativeAdImageInternal image_internal; image_internal.native_ad_image = j_image; GmaInternal::InsertNativeInternalImage(native_ad_internal, image_internal, - false, false); + "image", false); } FutureCallbackData* callback_data = @@ -1224,7 +1237,7 @@ bool RegisterNatives() { {"completeNativeLoadedAd", "(JJLcom/google/android/gms/ads/nativead/NativeAd$Image;[Lcom/google/" "android/gms/ads/nativead/NativeAd$Image;Lcom/google/android/gms/ads/" - "ResponseInfo;)V", + "nativead/NativeAd$Image;Lcom/google/android/gms/ads/ResponseInfo;)V", reinterpret_cast(&JNI_NativeAd_completeLoadedAd)}, {"completeNativeLoadAdError", "(JLcom/google/android/gms/ads/LoadAdError;ILjava/lang/String;)V", diff --git a/gma/src/common/gma_common.cc b/gma/src/common/gma_common.cc index eed08ca387..891086de17 100644 --- a/gma/src/common/gma_common.cc +++ b/gma/src/common/gma_common.cc @@ -123,15 +123,15 @@ void GmaInternal::UpdateAdViewInternalAdSizeDimensions( void GmaInternal::InsertNativeInternalImage( internal::NativeAdInternal* native_ad_internal, - const NativeAdImageInternal& native_image_internal, const bool& is_icon, - const bool& clear_existing_images) { + const NativeAdImageInternal& native_image_internal, + const std::string& image_type, const bool& clear_existing_images) { assert(native_ad_internal); if (clear_existing_images) { native_ad_internal->clear_existing_images(); } NativeAdImage image_asset = NativeAdImage(native_image_internal); - native_ad_internal->insert_image(image_asset, is_icon); + native_ad_internal->insert_image(image_asset, image_type); } // AdInspectorClosedListener diff --git a/gma/src/common/gma_common.h b/gma/src/common/gma_common.h index 38fcf0a185..ad1343ed41 100644 --- a/gma/src/common/gma_common.h +++ b/gma/src/common/gma_common.h @@ -200,8 +200,8 @@ class GmaInternal { // NativeAdInternal. static void InsertNativeInternalImage( internal::NativeAdInternal* native_ad_internal, - const NativeAdImageInternal& native_image_internal, const bool& is_icon, - const bool& clear_existing_images); + const NativeAdImageInternal& native_image_internal, + const std::string& image_type, const bool& clear_existing_images); }; } // namespace gma diff --git a/gma/src/common/native_ad.cc b/gma/src/common/native_ad.cc index fd110f1d70..45f379b53b 100644 --- a/gma/src/common/native_ad.cc +++ b/gma/src/common/native_ad.cc @@ -85,6 +85,10 @@ const std::vector& NativeAd::images() const { return internal_->images(); } +const NativeAdImage& NativeAd::adchoices_icon() const { + return internal_->adchoices_icon(); +}; + Future NativeAd::RecordImpression(const Variant& impression_data) { if (!impression_data.is_map()) { return CreateAndCompleteFuture( diff --git a/gma/src/common/native_ad_internal.cc b/gma/src/common/native_ad_internal.cc index 84804860d6..c0a568beed 100644 --- a/gma/src/common/native_ad_internal.cc +++ b/gma/src/common/native_ad_internal.cc @@ -62,13 +62,16 @@ Future NativeAdInternal::GetLoadAdLastResult() { } void NativeAdInternal::insert_image(const NativeAdImage& image, - const bool& is_icon) { - if (is_icon) { + const std::string& image_type) { + if (image_type == "icon") { icon_ = image; + } else if (image_type == "adchoices_icon") { + adchoices_icon_ = image; } else { images_.push_back(image); } } + void NativeAdInternal::clear_existing_images() { images_.clear(); } } // namespace internal diff --git a/gma/src/common/native_ad_internal.h b/gma/src/common/native_ad_internal.h index f07843d7a2..2f60433d22 100644 --- a/gma/src/common/native_ad_internal.h +++ b/gma/src/common/native_ad_internal.h @@ -70,6 +70,9 @@ class NativeAdInternal { // Returns the associated image assets of the native ad. const std::vector& images() const { return images_; } + // Returns the associated icon asset of the native ad. + const NativeAdImage& adchoices_icon() const { return adchoices_icon_; } + // Only used by allowlisted ad units. virtual Future RecordImpression(const Variant& impression_data) = 0; @@ -86,7 +89,8 @@ class NativeAdInternal { explicit NativeAdInternal(NativeAd* base); // Invoked after a native ad has been loaded to fill native ad image assets. - void insert_image(const NativeAdImage& ad_image, const bool& is_icon); + void insert_image(const NativeAdImage& ad_image, + const std::string& image_type); // Invoked before filling native ad image assets. void clear_existing_images(); @@ -102,6 +106,9 @@ class NativeAdInternal { // Tracks the native ad image assets. std::vector images_; + + // Tracks the native ad choices icon asset. + NativeAdImage adchoices_icon_; }; } // namespace internal diff --git a/gma/src/include/firebase/gma/internal/native_ad.h b/gma/src/include/firebase/gma/internal/native_ad.h index bc60a6ce68..674cbcac82 100644 --- a/gma/src/include/firebase/gma/internal/native_ad.h +++ b/gma/src/include/firebase/gma/internal/native_ad.h @@ -73,6 +73,9 @@ class NativeAd { /// Returns the associated image assets of the native ad. const std::vector& images() const; + // Returns the associated adchoices icon asset of the native ad. + const NativeAdImage& adchoices_icon() const; + /// Only allowlisted ad units use this api. Future RecordImpression(const Variant& impression_data); diff --git a/gma/src/ios/GADNativeAdCpp.h b/gma/src/ios/GADNativeAdCpp.h index c1e769c725..d1b521f4b7 100644 --- a/gma/src/ios/GADNativeAdCpp.h +++ b/gma/src/ios/GADNativeAdCpp.h @@ -20,6 +20,9 @@ @interface GADNativeAd() +/// AdChoices icon image. +@property(nonatomic, readonly, strong, nullable) GADNativeAdImage *adChoicesIcon; + /// Used only by allowlisted ad units. Provide a dictionary containing click data. - (void)performClickWithData:(nonnull NSDictionary *)clickData; diff --git a/gma/src/ios/native_ad_internal_ios.mm b/gma/src/ios/native_ad_internal_ios.mm index 0aac7ff0e4..70c705e937 100644 --- a/gma/src/ios/native_ad_internal_ios.mm +++ b/gma/src/ios/native_ad_internal_ios.mm @@ -227,7 +227,15 @@ { NativeAdImageInternal icon_internal; icon_internal.native_ad_image = gad_icon; - GmaInternal::InsertNativeInternalImage(this, icon_internal, true, true ); + GmaInternal::InsertNativeInternalImage(this, icon_internal, "icon", true ); + } + + NSObject *gad_choices_icon = ad.adChoicesIcon; + if (gad_choices_icon != nil) + { + NativeAdImageInternal adchoices_icon_internal; + adchoices_icon_internal.native_ad_image = gad_choices_icon; + GmaInternal::InsertNativeInternalImage(this, adchoices_icon_internal, "adchoices_icon", true ); } NSArray *gad_images = ad.images; @@ -235,7 +243,7 @@ { NativeAdImageInternal image_internal; image_internal.native_ad_image = gad_image; - GmaInternal::InsertNativeInternalImage(this, image_internal, false, false ); + GmaInternal::InsertNativeInternalImage(this, image_internal, "image", false ); } if (ad_load_callback_data_ != nil) { diff --git a/gma/src_java/com/google/firebase/gma/internal/cpp/NativeAdHelper.java b/gma/src_java/com/google/firebase/gma/internal/cpp/NativeAdHelper.java index d002cc1723..69fca0eaa9 100644 --- a/gma/src_java/com/google/firebase/gma/internal/cpp/NativeAdHelper.java +++ b/gma/src_java/com/google/firebase/gma/internal/cpp/NativeAdHelper.java @@ -247,8 +247,18 @@ public void onNativeAdLoaded(NativeAd ad) { NativeAd.Image[] imgArray = new NativeAd.Image[imgList.size()]; imgArray = imgList.toArray(imgArray); + NativeAd.Image adChoicesIcon = null; + NativeAd.AdChoicesInfo adChoicesInfo = ad.getAdChoicesInfo(); + if (adChoicesInfo != null) { + List adChoicesImgList = adChoicesInfo.getImages(); + if (!adChoicesImgList.isEmpty()) { + // Gets only the first image to keep the api in sync with its ios counterpart. + adChoicesIcon = adChoicesImgList.get(0); + } + } + completeNativeLoadedAd(mLoadAdCallbackDataPtr, mNativeAdInternalPtr, ad.getIcon(), - imgArray, ad.getResponseInfo()); + imgArray, adChoicesIcon, ad.getResponseInfo()); mLoadAdCallbackDataPtr = CPP_NULLPTR; } } @@ -262,7 +272,7 @@ public static native void completeNativeAdFutureCallback( /** Native callback invoked upon successfully loading an ad. */ public static native void completeNativeLoadedAd(long nativeInternalPtr, long mNativeAdInternalPtr, NativeAd.Image icon, NativeAd.Image[] images, - ResponseInfo responseInfo); + NativeAd.Image adChoicesIcon, ResponseInfo responseInfo); /** * Native callback upon encountering an error loading an Ad Request. Returns Android Google Mobile From 4c4a3a585fa677b3716c6ca536f773a0cbcc8612 Mon Sep 17 00:00:00 2001 From: "nakirekommula@google.com" Date: Thu, 7 Sep 2023 14:54:45 -0700 Subject: [PATCH 2/2] Fix lint warning. --- gma/src/common/native_ad.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gma/src/common/native_ad.cc b/gma/src/common/native_ad.cc index 45f379b53b..c479293b41 100644 --- a/gma/src/common/native_ad.cc +++ b/gma/src/common/native_ad.cc @@ -87,7 +87,7 @@ const std::vector& NativeAd::images() const { const NativeAdImage& NativeAd::adchoices_icon() const { return internal_->adchoices_icon(); -}; +} Future NativeAd::RecordImpression(const Variant& impression_data) { if (!impression_data.is_map()) {