Skip to content

Commit

Permalink
WIP: Rich ntt on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Feb 13, 2025
1 parent bf80607 commit cfd3a3a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

import org.chromium.content_public.browser.WebContents;
import android.widget.FrameLayout;
import org.chromium.components.thinwebview.ThinWebView;
import org.chromium.components.thinwebview.ThinWebViewConstraints;
import org.chromium.components.thinwebview.ThinWebViewFactory;
import org.chromium.components.embedder_support.view.ContentView;
import org.chromium.ui.base.IntentRequestTracker;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.chrome.browser.content.WebContentsFactory;
import org.chromium.ui.base.ViewAndroidDelegate;
import org.chromium.base.version_info.VersionInfo;
import androidx.recyclerview.widget.RecyclerView.OnItemTouchListener;

public class BraveNewTabPageLayout
extends NewTabPageLayout implements ConnectionErrorHandler, OnBraveNtpListener {
private static final String TAG = "BraveNewTabPage";
Expand All @@ -132,7 +145,13 @@ public class BraveNewTabPageLayout
private Integer mInitialTileNum;

// Own members.
private WindowAndroid mWindowAndroid;

private ImageView mBgImageView;
private WebContents mSponsoredBackgroundWebContents;
private ThinWebView mSponsoredBackgroundWebView;
private FrameLayout mBackgroundSponsoredContentView;

private Profile mProfile;
private SponsoredTab mSponsoredTab;
private boolean mIsTablet;
Expand Down Expand Up @@ -441,6 +460,34 @@ 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());
final int action = event.getActionMasked();
if (childView == null &&
(action == MotionEvent.ACTION_MOVE ||
action == MotionEvent.ACTION_DOWN)) {
mSponsoredBackgroundWebView.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 +1228,8 @@ public void initialize(
tabStripHeightSupplier);

mIsTablet = isTablet;
mWindowAndroid = windowAndroid;


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

Expand All @@ -1199,6 +1248,10 @@ public void initialize(
mActivity = activity;
((BraveActivity) mActivity).dismissShieldsTooltip();
((BraveActivity) mActivity).setNewTabPageManager(manager);

setupSponsoredBackgroundContent();

navigateSponsoredBackgroundWebContents("chrome://about");
}

protected boolean useFixedMVTLayout() {
Expand Down Expand Up @@ -1230,6 +1283,49 @@ private void showNTPImage(NTPImage ntpImage) {
}
}

private void setupSponsoredBackgroundContent() {
mBackgroundSponsoredContentView = findViewById(R.id.bg_background_view);

mSponsoredBackgroundWebContents =
WebContentsFactory.createWebContents(mProfile, /* initiallyHidden= */ false, false);
final ContentView webContentView =
ContentView.createContentView(mActivity, mSponsoredBackgroundWebContents);
final ViewAndroidDelegate delegate =
ViewAndroidDelegate.createBasicDelegate(webContentView);
mSponsoredBackgroundWebContents.setDelegates(
VersionInfo.getProductVersion(),
delegate,
webContentView,
mWindowAndroid,
WebContents.createDefaultInternalsHolder());

final IntentRequestTracker intentRequestTracker = mWindowAndroid.getIntentRequestTracker();
mSponsoredBackgroundWebView =
ThinWebViewFactory.create(
mActivity, new ThinWebViewConstraints(), intentRequestTracker);
mSponsoredBackgroundWebView
.getView()
.setLayoutParams(
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mSponsoredBackgroundWebView.attachWebContents(mSponsoredBackgroundWebContents,
webContentView,
null);

mBackgroundSponsoredContentView.addView(mSponsoredBackgroundWebView.getView());
}

private void navigateSponsoredBackgroundWebContents(String url) {
// Hide the background image view for testing.
mBgImageView = (ImageView) findViewById(R.id.bg_image_view);
mBgImageView.setVisibility(View.GONE);

mSponsoredBackgroundWebContents
.getNavigationController()
.loadUrl(new LoadUrlParams(url));
}

private void setBackgroundImage(NTPImage ntpImage) {
mBgImageView = (ImageView) findViewById(R.id.bg_image_view);
mBgImageView.setScaleType(ImageView.ScaleType.MATRIX);
Expand Down
6 changes: 6 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,12 @@
android:layout_height="match_parent"
android:contentDescription="@null"/>

<FrameLayout
android:id="@+id/bg_background_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>

<FrameLayout
android:id="@+id/logo_holder"
android:layout_width="match_parent"
Expand Down

0 comments on commit cfd3a3a

Please sign in to comment.