Skip to content

Commit

Permalink
Merge pull request #5724 from brave/SR_components_update_dp
Browse files Browse the repository at this point in the history
Update UI on component update
  • Loading branch information
deeppandya authored Jun 3, 2020
2 parents 24abc11 + ae2f9f9 commit f862599
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
public class BraveNewTabPageLayout extends NewTabPageLayout {
private ViewGroup mBraveStatsView;
private NTPBackgroundImagesBridge mNTPBackgroundImagesBridge;
private ViewGroup mainLayout;

public BraveNewTabPageLayout(Context context, AttributeSet attrs) {
super(context, attrs);
Expand All @@ -52,7 +53,7 @@ protected void onFinishInflate() {

@Override
protected void insertSiteSectionView() {
ViewGroup mainLayout = findViewById(R.id.ntp_main_layout);
mainLayout = findViewById(R.id.ntp_main_layout);

mSiteSectionView = SiteSection.inflateSiteSection(mainLayout);
ViewGroup.LayoutParams layoutParams = mSiteSectionView.getLayoutParams();
Expand All @@ -61,33 +62,43 @@ protected void insertSiteSectionView() {
int variation = ExploreSitesBridge.getVariation();
if (ExploreSitesBridge.isEnabled(variation)) {
((MarginLayoutParams) layoutParams).bottomMargin =
getResources().getDimensionPixelOffset(
R.dimen.tile_grid_layout_vertical_spacing);
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;
if (!mNTPBackgroundImagesBridge.isSuperReferral()
|| !NTPBackgroundImagesBridge.enableSponsoredImages()
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
|| !NTPBackgroundImagesBridge.enableSponsoredImages()
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
mainLayout.addView(mSiteSectionView, insertionPoint);
}

@Override
protected int getMaxTileRows() {
boolean isMoreTabs = false;
ChromeTabbedActivity chromeTabbedActivity = BraveRewardsHelper.getChromeTabbedActivity();
if(chromeTabbedActivity != null) {
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)) {
if (BravePrefServiceBridge.getInstance().getBoolean(BravePref.NTP_SHOW_BACKGROUND_IMAGE)
&& NTPUtil.shouldEnableNTPFeature(isMoreTabs)) {
return 1;
} else {
return 2;
}
}

public void removeDefaultTopSites() {
if (mainLayout != null && hasParent(mSiteSectionView)) {
mainLayout.removeView(mSiteSectionView);
}
}

private boolean hasParent(View view) {
return view != null && view.getParent() != null;
}
}
115 changes: 65 additions & 50 deletions android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,34 +106,36 @@ protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (sponsoredTab == null)
initilizeSponsoredTab();
checkAndShowNTPImage();
checkAndShowNTPImage(false);
mNTPBackgroundImagesBridge.addObserver(mNTPBackgroundImageServiceObserver);
}

@Override
protected void onDetachedFromWindow() {
if(mWorkerTask != null && mWorkerTask.getStatus() == AsyncTask.Status.RUNNING) {
if (mWorkerTask != null && mWorkerTask.getStatus() == AsyncTask.Status.RUNNING) {
mWorkerTask.cancel(true);
mWorkerTask = null;
}

if(!isFromBottomSheet){
if (!isFromBottomSheet) {
mNewTabPageLayout.setBackgroundResource(0);
if (imageDrawable != null && imageDrawable.getBitmap() != null && !imageDrawable.getBitmap().isRecycled()) {
imageDrawable.getBitmap().recycle();
}
}
mNTPBackgroundImagesBridge.removeObserver(mNTPBackgroundImageServiceObserver);
super.onDetachedFromWindow();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
if (sponsoredTab != null && NTPUtil.shouldEnableNTPFeature(sponsoredTab.isMoreTabs())) {
NTPImage ntpImage = sponsoredTab.getTabNTPImage();
if(ntpImage == null) {
NTPImage ntpImage = sponsoredTab.getTabNTPImage(false);
if (ntpImage == null) {
sponsoredTab.setNTPImage(SponsoredImageUtil.getBackgroundImage());
} else if (ntpImage instanceof Wallpaper) {
Wallpaper mWallpaper = (Wallpaper) ntpImage;
if(mWallpaper == null) {
if (mWallpaper == null) {
sponsoredTab.setNTPImage(SponsoredImageUtil.getBackgroundImage());
}
}
Expand All @@ -147,11 +149,11 @@ public void onConfigurationChanged(Configuration newConfig) {

@Override
public void initialize(NewTabPageManager manager, Tab tab, TileGroup.Delegate tileGroupDelegate,
boolean searchProviderHasLogo, boolean searchProviderIsGoogle, int scrollPosition,
long constructedTimeNs, ActivityLifecycleDispatcher activityLifecycleDispatcher) {
boolean searchProviderHasLogo, boolean searchProviderIsGoogle, int scrollPosition,
long constructedTimeNs, ActivityLifecycleDispatcher activityLifecycleDispatcher) {
super.initialize(manager, tab, tileGroupDelegate,
searchProviderHasLogo, searchProviderIsGoogle, scrollPosition,
constructedTimeNs, activityLifecycleDispatcher);
searchProviderHasLogo, searchProviderIsGoogle, scrollPosition,
constructedTimeNs, activityLifecycleDispatcher);

mTabImpl = (TabImpl) tab;
mTab = tab;
Expand Down Expand Up @@ -193,9 +195,9 @@ private void updateBraveStats() {
mDataSavedValueTextView.setText(getBraveStatsStringFormNumber(dataSaved, true));
mEstTimeSavedCountTextView.setText(getBraveStatsStringFromTime(estimatedMillisecondsSaved / 1000));

if((BravePrefServiceBridge.getInstance().getBoolean(BravePref.NTP_SHOW_BACKGROUND_IMAGE)
|| NTPUtil.isReferralEnabled())
&& sponsoredTab != null && NTPUtil.shouldEnableNTPFeature(sponsoredTab.isMoreTabs())) {
if ((BravePrefServiceBridge.getInstance().getBoolean(BravePref.NTP_SHOW_BACKGROUND_IMAGE)
|| NTPUtil.isReferralEnabled())
&& sponsoredTab != null && NTPUtil.shouldEnableNTPFeature(sponsoredTab.isMoreTabs())) {
mAdsBlockedTextView.setTextColor(mNewTabPageLayout.getResources().getColor(android.R.color.white));
mDataSavedTextView.setTextColor(mNewTabPageLayout.getResources().getColor(android.R.color.white));
mEstTimeSavedTextView.setTextColor(mNewTabPageLayout.getResources().getColor(android.R.color.white));
Expand Down Expand Up @@ -247,14 +249,11 @@ private String getBraveStatsStringFromTime(long seconds) {
String result = "";
if (seconds > 24 * 60 * 60) {
result = result + (seconds / (24 * 60 * 60)) + "d";
}
else if (seconds > 60 * 60) {
} else if (seconds > 60 * 60) {
result = result + (seconds / (60 * 60)) + "h";
}
else if (seconds > 60) {
} else if (seconds > 60) {
result = result + (seconds / 60) + "m";
}
else {
} else {
result = result + seconds + "s";
}
return result;
Expand All @@ -264,14 +263,16 @@ private void showNTPImage(NTPImage ntpImage) {
NTPUtil.updateOrientedUI(mTabImpl.getActivity(), mNewTabPageLayout);
ImageView mSponsoredLogo = (ImageView)mNewTabPageLayout.findViewById(R.id.sponsored_logo);
FloatingActionButton mSuperReferralLogo = (FloatingActionButton) mNewTabPageLayout.findViewById(R.id.super_referral_logo);
if (ntpImage instanceof Wallpaper
TextView mCreditText = (TextView)mNewTabPageLayout.findViewById(R.id.credit_text);
if (ntpImage instanceof Wallpaper
&& NTPUtil.isReferralEnabled()
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setBackgroundImage(ntpImage);
mSuperReferralLogo.setVisibility(View.VISIBLE);
mCreditText.setVisibility(View.GONE);
int floatingButtonIcon = GlobalNightModeStateProviderHolder.getInstance().isInNightMode()
? R.drawable.qrcode_dark
: R.drawable.qrcode_light;
? R.drawable.qrcode_dark
: R.drawable.qrcode_light;
mSuperReferralLogo.setImageResource(floatingButtonIcon);
mSuperReferralLogo.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -280,23 +281,22 @@ public void onClick(View view) {
mSuperReferralShareDialogFragment.show(mTabImpl.getActivity().getSupportFragmentManager(), "SuperReferralShareDialogFragment");
}
});
} else if(BravePrefServiceBridge.getInstance().getBoolean(BravePref.NTP_SHOW_BACKGROUND_IMAGE)
&& sponsoredTab != null && NTPUtil.shouldEnableNTPFeature(sponsoredTab.isMoreTabs())) {
} else if (BravePrefServiceBridge.getInstance().getBoolean(BravePref.NTP_SHOW_BACKGROUND_IMAGE)
&& sponsoredTab != null && NTPUtil.shouldEnableNTPFeature(sponsoredTab.isMoreTabs())) {
setBackgroundImage(ntpImage);
if (ntpImage instanceof BackgroundImage){
if (ntpImage instanceof BackgroundImage) {
BackgroundImage backgroundImage = (BackgroundImage) ntpImage;
mSponsoredLogo.setVisibility(View.GONE);
mSuperReferralLogo.setVisibility(View.GONE);
if (backgroundImage.getImageCredit() != null) {
String imageCreditStr = String.format(mNewTabPageLayout.getResources().getString(R.string.photo_by, backgroundImage.getImageCredit().getName()));

SpannableStringBuilder spannableString = new SpannableStringBuilder(imageCreditStr);
spannableString.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), ((imageCreditStr.length()-1) - (backgroundImage.getImageCredit().getName().length()-1)), imageCreditStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), ((imageCreditStr.length() - 1) - (backgroundImage.getImageCredit().getName().length() - 1)), imageCreditStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView creditText = (TextView)mNewTabPageLayout.findViewById(R.id.credit_text);
creditText.setText(spannableString);
creditText.setVisibility(View.VISIBLE);
creditText.setOnClickListener(new View.OnClickListener() {
mCreditText.setText(spannableString);
mCreditText.setVisibility(View.VISIBLE);
mCreditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (backgroundImage.getImageCredit() != null) {
Expand Down Expand Up @@ -327,19 +327,19 @@ public void onGlobalLayout() {

private void checkForNonDistruptiveBanner(NTPImage ntpImage) {
int brOption = NTPUtil.checkForNonDistruptiveBanner(ntpImage, sponsoredTab);
if (SponsoredImageUtil.BR_INVALID_OPTION != brOption
&& !NTPUtil.isReferralEnabled()) {
if (SponsoredImageUtil.BR_INVALID_OPTION != brOption
&& !NTPUtil.isReferralEnabled()) {
NTPUtil.showNonDistruptiveBanner(mTabImpl.getActivity(), mNewTabPageLayout, brOption, sponsoredTab, newTabPageListener);
}
}

private void checkAndShowNTPImage() {
NTPImage ntpImage = sponsoredTab.getTabNTPImage();
if(ntpImage == null) {
private void checkAndShowNTPImage(boolean isReset) {
NTPImage ntpImage = sponsoredTab.getTabNTPImage(isReset);
if (ntpImage == null) {
sponsoredTab.setNTPImage(SponsoredImageUtil.getBackgroundImage());
} else if (ntpImage instanceof Wallpaper) {
Wallpaper mWallpaper = (Wallpaper) ntpImage;
if(mWallpaper == null) {
if (mWallpaper == null) {
sponsoredTab.setNTPImage(SponsoredImageUtil.getBackgroundImage());
}
}
Expand All @@ -354,8 +354,8 @@ private void initilizeSponsoredTab() {
}
sponsoredTab = TabAttributes.from(mTab).get(String.valueOf((mTabImpl).getId()));
if (mNTPBackgroundImagesBridge.isSuperReferral()
&& NTPBackgroundImagesBridge.enableSponsoredImages()
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
&& NTPBackgroundImagesBridge.enableSponsoredImages()
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
mNTPBackgroundImagesBridge.getTopSites();
}

Expand All @@ -366,11 +366,11 @@ public void onInteractabilityChanged(Tab tab, boolean interactable) {
if (interactable) {
if (sponsoredTab == null)
initilizeSponsoredTab();
if(!sponsoredTab.isMoreTabs()) {
checkAndShowNTPImage();
if (!sponsoredTab.isMoreTabs()) {
checkAndShowNTPImage(false);
}
} else {
if(!isFromBottomSheet){
if (!isFromBottomSheet) {
mNewTabPageLayout.setBackgroundResource(0);
if (imageDrawable != null && imageDrawable.getBitmap() != null && !imageDrawable.getBitmap().isRecycled()) {
imageDrawable.getBitmap().recycle();
Expand All @@ -390,15 +390,15 @@ public void updateInteractableFlag(boolean isBottomSheet) {
public void updateNTPImage() {
if (sponsoredTab == null)
initilizeSponsoredTab();
checkAndShowNTPImage();
checkAndShowNTPImage(false);
}

@Override
public void updateTopSites(List<TopSite> topSites) {
new AsyncTask<Void>() {
@Override
protected Void doInBackground() {
for(TopSite topSite : topSites) {
for (TopSite topSite : topSites) {
mDatabaseHelper.insertTopSite(topSite);
}
return null;
Expand All @@ -412,19 +412,34 @@ protected void onPostExecute(Void result) {
List<TopSiteTable> topSites = mDatabaseHelper.getAllTopSites();
loadTopSites(topSites);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
};

private FetchWallpaperWorkerTask.WallpaperRetrievedCallback wallpaperRetrievedCallback= new FetchWallpaperWorkerTask.WallpaperRetrievedCallback() {
private NTPBackgroundImagesBridge.NTPBackgroundImageServiceObserver mNTPBackgroundImageServiceObserver = new NTPBackgroundImagesBridge.NTPBackgroundImageServiceObserver() {
@Override
public void onUpdated() {
checkAndShowNTPImage(true);
if (mNewTabPageLayout != null && mNewTabPageLayout.getPlaceholder() != null) {
mNewTabPageLayout.getPlaceholder().setVisibility(View.GONE);
}
((BraveNewTabPageLayout)mNewTabPageLayout).removeDefaultTopSites();
if (mNTPBackgroundImagesBridge.isSuperReferral()
&& NTPBackgroundImagesBridge.enableSponsoredImages()
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
mNTPBackgroundImagesBridge.getTopSites();
}
};

private FetchWallpaperWorkerTask.WallpaperRetrievedCallback wallpaperRetrievedCallback = new FetchWallpaperWorkerTask.WallpaperRetrievedCallback() {
@Override
public void bgWallpaperRetrieved(Bitmap bgWallpaper) {
bgImageView.setImageBitmap(bgWallpaper);
}

@Override
public void logoRetrieved(Wallpaper mWallpaper, Bitmap logoWallpaper) {
if(!NTPUtil.isReferralEnabled()) {
if (!NTPUtil.isReferralEnabled()) {
FloatingActionButton mSuperReferralLogo = (FloatingActionButton) mNewTabPageLayout.findViewById(R.id.super_referral_logo);
mSuperReferralLogo.setVisibility(View.GONE);

Expand All @@ -447,15 +462,15 @@ private void loadTopSites(List<TopSiteTable> topSites) {
LinearLayout superReferralSitesLayout = (LinearLayout) mNewTabPageLayout.findViewById(R.id.ntp_super_referral_sites_layout);
LayoutInflater inflater = (LayoutInflater) mTabImpl.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for(TopSiteTable topSite : topSites) {
for (TopSiteTable topSite : topSites) {
final View view = inflater.inflate(R.layout.suggestions_tile_view, null);

TextView tileViewTitleTv = view.findViewById(R.id.tile_view_title);
tileViewTitleTv.setText(topSite.getName());

if (!GlobalNightModeStateProviderHolder.getInstance().isInNightMode()
&& !BravePrefServiceBridge.getInstance().getBoolean(BravePref.NTP_SHOW_BACKGROUND_IMAGE)
&& !NTPUtil.isReferralEnabled()) {
&& !BravePrefServiceBridge.getInstance().getBoolean(BravePref.NTP_SHOW_BACKGROUND_IMAGE)
&& !NTPUtil.isReferralEnabled()) {
tileViewTitleTv.setTextColor(getResources().getColor(android.R.color.black));
} else {
tileViewTitleTv.setTextColor(getResources().getColor(android.R.color.white));
Expand Down Expand Up @@ -507,7 +522,7 @@ public boolean onMenuItemClick(MenuItem item) {
OfflinePageBridge.NTP_SUGGESTIONS_NAMESPACE, topSite.getDestinationUrl(), DownloadUiActionFlags.ALL);
} else {
RequestCoordinatorBridge.getForProfile(mProfile).savePageLater(
topSite.getDestinationUrl(), OfflinePageBridge.NTP_SUGGESTIONS_NAMESPACE, true /* userRequested */);
topSite.getDestinationUrl(), OfflinePageBridge.NTP_SUGGESTIONS_NAMESPACE, true /* userRequested */);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,23 @@ public class SponsoredTab {
public SponsoredTab(NTPBackgroundImagesBridge mNTPBackgroundImagesBridge) {
this.mNTPBackgroundImagesBridge = mNTPBackgroundImagesBridge;
ChromeTabbedActivity chromeTabbedActivity = BraveRewardsHelper.getChromeTabbedActivity();
if(chromeTabbedActivity != null) {
if (chromeTabbedActivity != null) {
TabModel tabModel = chromeTabbedActivity.getCurrentTabModel();
isMoreTabs = tabModel.getCount() > SponsoredImageUtil.MAX_TABS ? true : false;
}

if (NTPUtil.shouldEnableNTPFeature(isMoreTabs)){
if (NTPUtil.shouldEnableNTPFeature(isMoreTabs)) {
ntpImage = NTPUtil.getNTPImage(mNTPBackgroundImagesBridge);
tabIndex = SponsoredImageUtil.getTabIndex();
updateBannerPref();
}
}

public NTPImage getTabNTPImage() {
if (ntpImage != null) {
return ntpImage;
} else {
return NTPUtil.getNTPImage(mNTPBackgroundImagesBridge);
public NTPImage getTabNTPImage(boolean isReset) {
if (ntpImage == null || isReset) {
ntpImage = NTPUtil.getNTPImage(mNTPBackgroundImagesBridge);
}
return ntpImage;
}

public void setNTPImage(NTPImage ntpImage) {
Expand Down

0 comments on commit f862599

Please sign in to comment.