Skip to content

Commit

Permalink
Merge pull request #5000 from brave/migrate_ntp_si_dp_1_7_x
Browse files Browse the repository at this point in the history
Migrate ntp si (uplift to 1.7.x)
  • Loading branch information
kjozwiak authored Mar 23, 2020
2 parents 5608927 + 7578efb commit 2836faa
Show file tree
Hide file tree
Showing 85 changed files with 2,877 additions and 84 deletions.
15 changes: 15 additions & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageView.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp/IncognitoNewTabPageView.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_sponsored_images/BackgroundImage.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_sponsored_images/NTPImage.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_sponsored_images/ImageCredit.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_sponsored_images/SponsoredImageUtil.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_sponsored_images/NTPUtil.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_sponsored_images/SponsoredLogo.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_sponsored_images/SponsoredTab.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_sponsored_images/NewTabPageListener.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_sponsored_images/RewardsBottomSheetDialogFragment.java",
"../../brave/android/java/org/chromium/chrome/browser/ntp_sponsored_images/NTPSponsoredImagesBridge.java",
"../../brave/android/java/org/chromium/chrome/browser/onboarding/BraveRewardsOnboardingFragment.java",
"../../brave/android/java/org/chromium/chrome/browser/onboarding/BraveRewardsService.java",
"../../brave/android/java/org/chromium/chrome/browser/onboarding/BraveShieldsOnboardingFragment.java",
Expand All @@ -65,6 +75,7 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/qrreader/CameraSource.java",
"../../brave/android/java/org/chromium/chrome/browser/qrreader/CameraSourcePreview.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/AppearancePreferences.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/BackgroundImagesPreferences.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/BackgroundVideoPlaybackPreference.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/BraveHomepagePreferences.java",
"../../brave/android/java/org/chromium/chrome/browser/settings/BraveLicensePreferences.java",
Expand Down Expand Up @@ -104,6 +115,10 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/util/BraveReferrer.java",
"../../brave/android/java/org/chromium/chrome/browser/util/PackageUtils.java",
"../../brave/android/java/org/chromium/chrome/browser/util/TabUtils.java",
"../../brave/android/java/org/chromium/chrome/browser/util/ConfigurationUtils.java",
"../../brave/android/java/org/chromium/chrome/browser/util/ImageUtils.java",
"../../brave/android/java/org/chromium/chrome/browser/widget/tile/BraveTileWithTextView.java",
"../../brave/android/java/org/chromium/chrome/browser/tabmodel/BraveTabCreator.java"
]

if (brave_rewards_enabled) {
Expand Down
62 changes: 51 additions & 11 deletions android/java/org/chromium/chrome/browser/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.chromium.chrome.browser.toolbar.top.BraveToolbarLayout;
import org.chromium.chrome.browser.util.BraveReferrer;
import org.chromium.chrome.browser.util.UrlConstants;
import org.chromium.chrome.browser.settings.BackgroundImagesPreferences;
import org.chromium.components.bookmarks.BookmarkId;
import org.chromium.components.bookmarks.BookmarkType;
import org.chromium.ui.widget.Toast;
Expand Down Expand Up @@ -180,6 +181,9 @@ protected void initializeStartupMetrics() {
public void finishNativeInitialization() {
super.finishNativeInitialization();

int appOpenCount = ContextUtils.getAppSharedPreferences().getInt(BackgroundImagesPreferences.PREF_APP_OPEN_COUNT, 0);
BackgroundImagesPreferences.setOnPreferenceValue(BackgroundImagesPreferences.PREF_APP_OPEN_COUNT , appOpenCount + 1);

Context app = ContextUtils.getApplicationContext();
if (null != app && (this instanceof ChromeTabbedActivity)) {
mBraveSyncWorker = new BraveSyncWorker(app);
Expand Down Expand Up @@ -313,24 +317,60 @@ public void OnRewardsPanelDismiss() {
}
}

public void openNewOrSelectExistingTab(String url) {
public Tab selectExistingTab(String url) {
Tab tab = getActivityTab();
if (tab != null && tab.getUrl().equals(url)) {
return tab;
}

TabModel tabModel = getCurrentTabModel();
int tabRewardsIndex = TabModelUtils.getTabIndexByUrl(tabModel, url);
int tabIndex = TabModelUtils.getTabIndexByUrl(tabModel, url);

// Find if tab exists
if (tabRewardsIndex != TabModel.INVALID_TAB_INDEX){
Tab tab = tabModel.getTabAt(tabRewardsIndex);
if (tabIndex != TabModel.INVALID_TAB_INDEX){
tab = tabModel.getTabAt(tabIndex);
// Moving tab forward
if (!getActivityTab().equals(tab)){
tabModel.moveTab(tab.getId(), tabModel.getCount());
tabModel.setIndex(
TabModelUtils.getTabIndexById(tabModel, tab.getId()),
TabSelectionType.FROM_USER);
}
tabModel.moveTab(tab.getId(), tabModel.getCount());
tabModel.setIndex(
TabModelUtils.getTabIndexById(tabModel, tab.getId()),
TabSelectionType.FROM_USER);
return tab;
} else {
return null;
}
}

public Tab openNewOrSelectExistingTab(String url) {
TabModel tabModel = getCurrentTabModel();
int tabRewardsIndex = TabModelUtils.getTabIndexByUrl(tabModel, url);

Tab tab = selectExistingTab(url);
if (tab != null) {
return tab;
} else { // Open a new tab
getTabCreator(false).launchUrl(url, TabLaunchType.FROM_CHROME_UI);
return getTabCreator(false).launchUrl(url, TabLaunchType.FROM_CHROME_UI);
}
}

private native void nativeRestartStatsUpdater();

static public ChromeTabbedActivity getChromeTabbedActivity() {
for (Activity ref : ApplicationStatus.getRunningActivities()) {
if (!(ref instanceof ChromeTabbedActivity)) continue;

return (ChromeTabbedActivity)ref;
}

return null;
}

static public BraveActivity getBraveActivity() {
for (Activity ref : ApplicationStatus.getRunningActivities()) {
if (!(ref instanceof BraveActivity)) continue;

return (BraveActivity)ref;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

public abstract class BraveFeatureList {
public static final String BRAVE_REWARDS = "BraveRewards";
public static final String USE_DEV_UPDATER_URL = "UseDevUpdaterUrl";
}
16 changes: 2 additions & 14 deletions android/java/org/chromium/chrome/browser/BraveRewardsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,11 @@ public static Bitmap getCircularBitmap(Bitmap bitmap) {
}

static public ChromeTabbedActivity getChromeTabbedActivity() {
for (Activity ref : ApplicationStatus.getRunningActivities()) {
if (!(ref instanceof ChromeTabbedActivity)) continue;

return (ChromeTabbedActivity)ref;
}

return null;
return BraveActivity.getChromeTabbedActivity();
}

static public BraveActivity getBraveActivity() {
for (Activity ref : ApplicationStatus.getRunningActivities()) {
if (!(ref instanceof BraveActivity)) continue;

return (BraveActivity)ref;
}

return null;
return BraveActivity.getBraveActivity();
}

static public String getCurrentMonth(Calendar currentTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.chromium.chrome.R;
import org.chromium.chrome.browser.ntp.NewTabPageLayout;
import org.chromium.chrome.browser.suggestions.tile.SiteSection;
import org.chromium.chrome.browser.explore_sites.ExploreSitesBridge;
import org.chromium.chrome.browser.BraveRewardsHelper;
import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.tabmodel.TabModel;
import org.chromium.chrome.browser.ntp_sponsored_images.SponsoredImageUtil;
import org.chromium.chrome.browser.ntp_sponsored_images.NTPUtil;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;

public class BraveNewTabPageLayout extends NewTabPageLayout {
private ViewGroup mBraveStatsLayout;
private ViewGroup mBraveStatsView;

public BraveNewTabPageLayout(Context context, AttributeSet attrs) {
super(context, attrs);
Expand All @@ -27,20 +35,51 @@ public void setSearchProviderInfo(boolean hasLogo, boolean isGoogle) {
// Make brave stats visibile always on NTP.
// NewTabPageLayout::setSearchProviderInfo() makes it invisible.
// So, explicitly set it as visible.
mBraveStatsLayout.setVisibility(View.VISIBLE);
mBraveStatsView.setVisibility(View.VISIBLE);
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();
insertBraveStatsLayout();
mBraveStatsView = (ViewGroup) findViewById(R.id.brave_stats);
}

private void insertBraveStatsLayout() {
mBraveStatsLayout = (ViewGroup) LayoutInflater.from(getContext())
.inflate(R.layout.brave_stats_layout, this, false);
ViewGroup logo = (ViewGroup) findViewById(R.id.search_provider_logo);
int insertionPoint = indexOfChild(logo) + 1;
addView(mBraveStatsLayout, insertionPoint);
@Override
protected void insertSiteSectionView() {
ViewGroup mainLayout = findViewById(R.id.ntp_main_layout);

mSiteSectionView = SiteSection.inflateSiteSection(mainLayout);
ViewGroup.LayoutParams layoutParams = mSiteSectionView.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
// If the explore sites section exists as its own section, then space it more closely.
int variation = ExploreSitesBridge.getVariation();
if (ExploreSitesBridge.isEnabled(variation)
&& !ExploreSitesBridge.isIntegratedWithMostLikely(variation)) {
((MarginLayoutParams) layoutParams).bottomMargin =
getResources().getDimensionPixelOffset(
R.dimen.tile_grid_layout_vertical_spacing);
}
mSiteSectionView.setLayoutParams(layoutParams);

ViewGroup mBraveStatsView = (ViewGroup) findViewById(R.id.brave_stats_layout);
int insertionPoint = mainLayout.indexOfChild(mBraveStatsView) + 1;
mainLayout.addView(mSiteSectionView, insertionPoint);
}

@Override
protected int getMaxTileRows() {
boolean isMoreTabs = false;
ChromeTabbedActivity chromeTabbedActivity = BraveRewardsHelper.getChromeTabbedActivity();
if(chromeTabbedActivity != null) {
TabModel tabModel = chromeTabbedActivity.getCurrentTabModel();
isMoreTabs = tabModel.getCount() >= SponsoredImageUtil.MAX_TABS ? true : false;
}

if(BravePrefServiceBridge.getInstance().getBoolean(BravePref.NTP_SHOW_BACKGROUND_IMAGE)
&& NTPUtil.shouldEnableNTPFeature(isMoreTabs)) {
return 1;
} else {
return 2;
}
}
}
Loading

0 comments on commit 2836faa

Please sign in to comment.