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

[ads] RichNTT: Android #27645

Merged
merged 1 commit into from
Feb 27, 2025
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
1 change: 1 addition & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/ntp/IncognitoNewTabPageView.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp/NtpUtil.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp/OnBraveNtpListener.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp/SponsoredRichMediaWebView.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_background_images/NTPBackgroundImagesBridge.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_background_images/model/BackgroundImage.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_background_images/model/ImageCredit.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
Expand All @@ -39,6 +40,7 @@
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.airbnb.lottie.LottieAnimationView;
Expand Down Expand Up @@ -132,7 +134,12 @@ public class BraveNewTabPageLayout
private Integer mInitialTileNum;

// Own members.
private WindowAndroid mWindowAndroid;

private ImageView mBgImageView;
private SponsoredRichMediaWebView mSponsoredRichMediaWebView;
private FrameLayout mBackgroundSponsoredRichMediaView;

private Profile mProfile;
private SponsoredTab mSponsoredTab;
private boolean mIsTablet;
Expand Down Expand Up @@ -441,6 +448,27 @@ private void setNtpRecyclerView(LinearLayoutManager linearLayoutManager) {
}

mPrevVisibleNewsCardPosition = firstNewsFeedPosition() - 1;

mRecyclerView.addOnItemTouchListener(
new OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(
RecyclerView recyclerView, MotionEvent event) {
final View childView =
recyclerView.findChildViewUnder(event.getX(), event.getY());
if (childView == null && mSponsoredRichMediaWebView != null) {
mSponsoredRichMediaWebView.getView().dispatchTouchEvent(event);
}
return false;
}

@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) {}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
});

mRecyclerView.addOnScrollListener(
new RecyclerView.OnScrollListener() {
@Override
Expand Down Expand Up @@ -1181,6 +1209,7 @@ public void initialize(
tabStripHeightSupplier);

mIsTablet = isTablet;
mWindowAndroid = windowAndroid;

assert mMvTilesContainerLayout != null : "Something has changed in the upstream!";

Expand Down Expand Up @@ -1217,7 +1246,9 @@ private void showNTPImage(NTPImage ntpImage) {
if (mNtpAdapter != null) {
mNtpAdapter.setNtpImage(ntpImage);
}
if (ntpImage instanceof Wallpaper
if (ntpImage instanceof Wallpaper && ((Wallpaper) ntpImage).isRichMedia()) {
setupSponsoredBackgroundContent();
} else if (ntpImage instanceof Wallpaper
&& NTPImageUtil.isReferralEnabled()
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setBackgroundImage(ntpImage);
Expand All @@ -1230,6 +1261,21 @@ private void showNTPImage(NTPImage ntpImage) {
}
}

private void setupSponsoredBackgroundContent() {
if (mSponsoredRichMediaWebView != null) {
return;
}

mSponsoredRichMediaWebView =
new SponsoredRichMediaWebView(mActivity, mWindowAndroid, mProfile);

mBackgroundSponsoredRichMediaView = findViewById(R.id.bg_sponsored_rich_media_view);
mBackgroundSponsoredRichMediaView.setVisibility(View.VISIBLE);
mBackgroundSponsoredRichMediaView.addView(mSponsoredRichMediaWebView.getView());

mSponsoredRichMediaWebView.loadSponsoredRichMedia();
}

private void setBackgroundImage(NTPImage ntpImage) {
mBgImageView = (ImageView) findViewById(R.id.bg_image_view);
mBgImageView.setScaleType(ImageView.ScaleType.MATRIX);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Copyright (c) 2025 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.ntp;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import org.chromium.base.version_info.VersionInfo;
import org.chromium.chrome.browser.content.WebContentsFactory;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.components.embedder_support.view.ContentView;
import org.chromium.components.thinwebview.ThinWebView;
import org.chromium.components.thinwebview.ThinWebViewConstraints;
import org.chromium.components.thinwebview.ThinWebViewFactory;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.browser.WebContents;
import org.chromium.net.NetId;
import org.chromium.ui.base.ViewAndroidDelegate;
import org.chromium.ui.base.WindowAndroid;

public class SponsoredRichMediaWebView {
private static final String NEW_TAB_TAKEOVER_URL = "chrome://new-tab-takeover";

private WebContents mWebContents;
private ThinWebView mWebView;

public SponsoredRichMediaWebView(
Activity activity, WindowAndroid windowAndroid, Profile profile) {
mWebContents =
WebContentsFactory.createWebContentsWithWarmRenderer(
profile, /* initiallyHidden= */ false, /* targetNetwork= */ NetId.INVALID);

final ContentView webContentView = ContentView.createContentView(activity, mWebContents);
mWebContents.setDelegates(
VersionInfo.getProductVersion(),
ViewAndroidDelegate.createBasicDelegate(webContentView),
webContentView,
windowAndroid,
WebContents.createDefaultInternalsHolder());

mWebView =
ThinWebViewFactory.create(
activity,
new ThinWebViewConstraints(),
windowAndroid.getIntentRequestTracker());
mWebView.getView()
.setLayoutParams(
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mWebView.attachWebContents(mWebContents, webContentView, null);
}

public void loadSponsoredRichMedia() {
mWebContents.getNavigationController().loadUrl(new LoadUrlParams(NEW_TAB_TAKEOVER_URL));
}

public View getView() {
return mWebView.getView();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,28 @@ public static BackgroundImage createWallpaper(String imagePath, String author, S
}

@CalledByNative
public static Wallpaper createBrandedWallpaper(String imagePath, int focalPointX,
int focalPointY, String logoPath, String logoDestinationUrl, String themeName,
boolean isSponsored, String creativeInstanceId, String wallpaperId) {
return new Wallpaper(imagePath, focalPointX, focalPointY,
logoPath, logoDestinationUrl,
themeName, isSponsored, creativeInstanceId,
wallpaperId);
public static Wallpaper createBrandedWallpaper(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting of existing Java code was required by presubmit checks on CI.

String imagePath,
int focalPointX,
int focalPointY,
String logoPath,
String logoDestinationUrl,
String themeName,
boolean isSponsored,
String creativeInstanceId,
String wallpaperId,
boolean isRichMedia) {
return new Wallpaper(
imagePath,
focalPointX,
focalPointY,
logoPath,
logoDestinationUrl,
themeName,
isSponsored,
creativeInstanceId,
wallpaperId,
isRichMedia);
}

@CalledByNative
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ public class Wallpaper extends NTPImage {
private boolean mIsSponsored;
private String mCreativeInstanceId;
private String mWallpaperId;
private boolean mIsRichMedia;

public Wallpaper(String imagePath, int focalPointX, int focalPointY, String logoPath,
String logoDestinationUrl, String themeName, boolean isSponsored,
String creativeInstanceId, String wallpaperId) {
public Wallpaper(
String imagePath,
int focalPointX,
int focalPointY,
String logoPath,
String logoDestinationUrl,
String themeName,
boolean isSponsored,
String creativeInstanceId,
String wallpaperId,
boolean isRichMedia) {
mImagePath = imagePath;
mFocalPointX = focalPointX;
mFocalPointY = focalPointY;
Expand All @@ -28,6 +37,7 @@ public Wallpaper(String imagePath, int focalPointX, int focalPointY, String logo
mIsSponsored = isSponsored;
mCreativeInstanceId = creativeInstanceId;
mWallpaperId = wallpaperId;
mIsRichMedia = isRichMedia;
}

public String getImagePath() {
Expand Down Expand Up @@ -65,4 +75,8 @@ public String getCreativeInstanceId() {
public String getWallpaperId() {
return mWallpaperId;
}

public boolean isRichMedia() {
return mIsRichMedia;
}
}
7 changes: 7 additions & 0 deletions android/java/res/layout/new_tab_page_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
android:layout_height="match_parent"
android:contentDescription="@null"/>

<FrameLayout
android:id="@+id/bg_sponsored_rich_media_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
</FrameLayout>

<FrameLayout
android:id="@+id/logo_holder"
android:layout_width="match_parent"
Expand Down
6 changes: 5 additions & 1 deletion browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ using extensions::ChromeContentBrowserClientExtensionsPart;

#if BUILDFLAG(IS_ANDROID)
#include "brave/browser/ui/webui/brave_wallet/android/android_wallet_page_ui.h"
#include "brave/browser/ui/webui/new_tab_takeover/android/new_tab_takeover_ui.h"
#endif // BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(IS_ANDROID)
Expand Down Expand Up @@ -681,7 +682,10 @@ void BraveContentBrowserClient::RegisterWebUIInterfaceBrokers(
.Add<brave_news::mojom::BraveNewsController>()
.Add<brave_news::mojom::BraveNewsInternals>();
}
#endif
#else // !BUILDFLAG(IS_ANDROID)
registry.ForWebUI<NewTabTakeoverUI>()
.Add<new_tab_takeover::mojom::NewTabTakeover>();
#endif // !BUILDFLAG(IS_ANDROID)
}

std::optional<base::UnguessableToken>
Expand Down
10 changes: 9 additions & 1 deletion browser/ntp_background/android/ntp_background_images_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(
const std::string* wallpaper_id =
data.FindString(ntp_background_images::kWallpaperIDKey);

bool is_rich_media = false;
if (const std::string* sponsored_rich_media_type =
data.FindString(ntp_background_images::kWallpaperTypeKey)) {
is_rich_media = *sponsored_rich_media_type ==
ntp_background_images::kRichMediaWallpaperType;
}

view_counter_service_->BrandedWallpaperWillBeDisplayed(
wallpaper_id ? *wallpaper_id : "",
creative_instance_id ? *creative_instance_id : "",
Expand All @@ -190,7 +197,8 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(
ConvertUTF8ToJavaString(env, *theme_name), is_sponsored,
ConvertUTF8ToJavaString(
env, creative_instance_id ? *creative_instance_id : ""),
ConvertUTF8ToJavaString(env, wallpaper_id ? *wallpaper_id : ""));
ConvertUTF8ToJavaString(env, wallpaper_id ? *wallpaper_id : ""),
is_rich_media);
}

void NTPBackgroundImagesBridge::GetTopSites(JNIEnv* env,
Expand Down
1 change: 1 addition & 0 deletions browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ if (is_android) {
"//brave/browser/brave_ads/android",
"//brave/browser/download/android:jni_headers",
"//brave/browser/ntp_background/android",
"//brave/browser/ui/webui/new_tab_takeover/android:new_tab_takeover",
"//brave/build/android:jni_headers",
"//brave/components/brave_sync:sync_service_impl_helper",
"//chrome/android:jni_headers",
Expand Down
5 changes: 4 additions & 1 deletion browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ source_set("ui") {
"android/ai_chat/brave_leo_settings_launcher_helper.h",
]

deps += [ "//brave/build/android:jni_headers" ]
deps += [
"//brave/browser/ui/webui/new_tab_takeover/android:new_tab_takeover",
"//brave/build/android:jni_headers",
]
}

if (enable_ai_rewriter) {
Expand Down
4 changes: 4 additions & 0 deletions browser/ui/config.gni
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ if (!is_android) {
"//brave/browser/ui/webui/brave_news_internals",
]
}
if (is_android) {
brave_ui_allow_circular_includes_from +=
[ "//brave/browser/ui/webui/new_tab_takeover/android:new_tab_takeover" ]
}
if (toolkit_views) {
brave_ui_allow_circular_includes_from +=
[ "//brave/browser/ui/views/split_view" ]
Expand Down
3 changes: 3 additions & 0 deletions browser/ui/webui/new_tab_takeover/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_rules = [
"+brave/components/new_tab_takeover",
]
40 changes: 40 additions & 0 deletions browser/ui/webui/new_tab_takeover/android/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) 2025 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.

import("//brave/build/config.gni")

assert(is_android)

source_set("new_tab_takeover") {
sources = [
"new_tab_takeover_ui.cc",
"new_tab_takeover_ui.h",
"new_tab_takeover_ui_config.cc",
"new_tab_takeover_ui_config.h",
]

deps = [
"//base",
"//brave/browser:browser_process",
"//brave/browser/brave_ads:brave_ads",
"//brave/browser/ntp_background",
"//brave/components/constants",
"//brave/components/new_tab_takeover:new_tab_takeover_generated_resources",
"//brave/components/ntp_background_images/browser",
"//chrome/browser:browser_process",
"//chrome/browser:browser_public_dependencies",
"//chrome/browser/profiles:profile",
"//content/public/browser",
"//content/public/common",
"//ui/webui",
"//url",
]

public_deps = [
"//brave/components/new_tab_takeover/mojom",
"//brave/components/ntp_background_images/browser/mojom",
"//mojo/public/cpp/bindings",
]
}
Loading
Loading