diff --git a/android/java/org/chromium/chrome/browser/ntp_background_images/RewardsBottomSheetDialogFragment.java b/android/java/org/chromium/chrome/browser/ntp_background_images/RewardsBottomSheetDialogFragment.java index c69e503ec4b..29d2ce02bd1 100644 --- a/android/java/org/chromium/chrome/browser/ntp_background_images/RewardsBottomSheetDialogFragment.java +++ b/android/java/org/chromium/chrome/browser/ntp_background_images/RewardsBottomSheetDialogFragment.java @@ -51,7 +51,7 @@ import org.chromium.chrome.browser.ntp_background_images.util.NTPUtil; import static org.chromium.ui.base.ViewUtils.dpToPx; -public class RewardsBottomSheetDialogFragment extends BottomSheetDialogFragment{ +public class RewardsBottomSheetDialogFragment extends BottomSheetDialogFragment { private static final String BRAVE_TERMS_PAGE = "https://basicattentiontoken.org/user-terms-of-service/"; private static final String BRAVE_REWARDS_LEARN_MORE = "https://brave.com/faq-rewards"; @@ -65,12 +65,12 @@ public class RewardsBottomSheetDialogFragment extends BottomSheetDialogFragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, - @Nullable ViewGroup container, - @Nullable Bundle savedInstanceState) { - if (getArguments()!=null) { - ntpType = getArguments().getInt(SponsoredImageUtil.NTP_TYPE,1); - } - return inflater.inflate(R.layout.ntp_bottom_sheet, container,false); + @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { + if (getArguments() != null) { + ntpType = getArguments().getInt(SponsoredImageUtil.NTP_TYPE, 1); + } + return inflater.inflate(R.layout.ntp_bottom_sheet, container, false); } @Override @@ -84,7 +84,7 @@ public class RewardsBottomSheetDialogFragment extends BottomSheetDialogFragment{ super.onConfigurationChanged(newConfig); boolean isTablet = DeviceFormFactor.isNonMultiDisplayContextOnTablet(getActivity()); - if(isTablet || (!isTablet && newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)) { + if (isTablet || (!isTablet && newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)) { getDialog().getWindow().setLayout(dpToPx(getActivity(), 400), -1); } else { getDialog().getWindow().setLayout(-1, -1); @@ -98,7 +98,7 @@ public class RewardsBottomSheetDialogFragment extends BottomSheetDialogFragment{ newTabPageListener.updateInteractableFlag(false); boolean isTablet = DeviceFormFactor.isNonMultiDisplayContextOnTablet(getActivity()); - if(isTablet || (!isTablet && ConfigurationUtils.isLandscape(getActivity()))) { + if (isTablet || (!isTablet && ConfigurationUtils.isLandscape(getActivity()))) { getDialog().getWindow().setLayout(dpToPx(getActivity(), 400), -1); } else { getDialog().getWindow().setLayout(-1, -1); @@ -124,14 +124,22 @@ public class RewardsBottomSheetDialogFragment extends BottomSheetDialogFragment{ }); ImageView btnClose = view.findViewById(R.id.ntp_bottom_sheet_close); - btnClose.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dismiss(); - } - }); - Button turnOnAdsButton = view.findViewById(R.id.btn_turn_on_ads); + TextView bottomSheetTitleText = view.findViewById(R.id.ntp_bottom_sheet_title); + TextView bottomSheetText = view.findViewById(R.id.ntp_bottom_sheet_text); + TextView bottomSheetTosText = view.findViewById(R.id.ntp_bottom_sheet_tos_text); + TextView learnMoreText = view.findViewById(R.id.learn_more_text); + TextView hideSponsoredImagesText = view.findViewById(R.id.hide_sponsored_images_text); + + if (btnClose != null) { + btnClose.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dismiss(); + } + }); + } + if (turnOnAdsButton != null) { turnOnAdsButton.setOnClickListener(new View.OnClickListener() { @Override @@ -142,20 +150,8 @@ public class RewardsBottomSheetDialogFragment extends BottomSheetDialogFragment{ }); } - Button turnOnRewardsButton = view.findViewById(R.id.btn_turn_on_rewards); - if (turnOnRewardsButton != null) { - turnOnRewardsButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - turnOnRewards(); - dismiss(); - } - }); - } - - Button learnMoreButton = view.findViewById(R.id.btn_learn_more); - if (learnMoreButton != null) { - learnMoreButton.setOnClickListener(new View.OnClickListener() { + if (learnMoreText != null) { + learnMoreText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NTPUtil.openUrlInSameTab(BRAVE_REWARDS_LEARN_MORE); @@ -164,126 +160,48 @@ public class RewardsBottomSheetDialogFragment extends BottomSheetDialogFragment{ }); } - TextView bottomSheetTitleText= view.findViewById(R.id.ntp_bottom_sheet_title); - TextView bottomSheetText= view.findViewById(R.id.ntp_bottom_sheet_text); + if (hideSponsoredImagesText != null) { + hideSponsoredImagesText.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + hideSponsoredImages(); + dismiss(); + } + }); + } switch (ntpType) { - case SponsoredImageUtil.BR_OFF: - bottomSheetTitleText.setText(getResources().getString(R.string.turn_on_brave_ads_to_claim)); - turnOnRewardsButton.setVisibility(View.VISIBLE); - learnMoreButton.setVisibility(View.VISIBLE); - String brOffText = String.format(getResources().getString(R.string.ntp_tos_text), getResources().getString(R.string.terms_of_service), getResources().getString(R.string.hide_sponsored_images)); - int termsOfServiceIndex = brOffText.indexOf(getResources().getString(R.string.terms_of_service)); - Spanned brOffTextSpanned = BraveRewardsHelper.spannedFromHtmlString(brOffText); - SpannableString brOffTextSS = new SpannableString(brOffTextSpanned.toString()); + case SponsoredImageUtil.BR_ON_ADS_OFF: + bottomSheetTitleText.setText(getResources().getString(R.string.earn_tokens_for_viewing_title)); + turnOnAdsButton.setVisibility(View.VISIBLE); - ClickableSpan tosClickableSpan = new ClickableSpan() { - @Override - public void onClick(@NonNull View textView) { - CustomTabActivity.showInfoPage(getActivity(), BRAVE_TERMS_PAGE); - } - @Override - public void updateDrawState(@NonNull TextPaint ds) { - super.updateDrawState(ds); - ds.setUnderlineText(false); - } - }; + bottomSheetTosText.setVisibility(View.VISIBLE); + String tosText = String.format(getResources().getString(R.string.ntp_tos_text), getResources().getString(R.string.terms_of_service)); + int termsOfServiceIndex = tosText.indexOf(getResources().getString(R.string.terms_of_service)); + Spanned tosTextSpanned = BraveRewardsHelper.spannedFromHtmlString(tosText); + SpannableString tosTextSS = new SpannableString(tosTextSpanned.toString()); - ClickableSpan brOffHideSponsoredImagesClickableSpan = new ClickableSpan() { - @Override - public void onClick(@NonNull View textView) { - hideSponsoredImages(); - dismiss(); - } - @Override - public void updateDrawState(@NonNull TextPaint ds) { - super.updateDrawState(ds); - ds.setUnderlineText(false); - } - }; + ClickableSpan tosClickableSpan = new ClickableSpan() { + @Override + public void onClick(@NonNull View textView) { + CustomTabActivity.showInfoPage(getActivity(), BRAVE_TERMS_PAGE); + } + @Override + public void updateDrawState(@NonNull TextPaint ds) { + super.updateDrawState(ds); + ds.setUnderlineText(false); + } + }; - brOffTextSS.setSpan(tosClickableSpan, termsOfServiceIndex, termsOfServiceIndex + getResources().getString(R.string.terms_of_service).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - brOffTextSS.setSpan(brOffHideSponsoredImagesClickableSpan, (brOffTextSS.length()-1) - getResources().getString(R.string.hide_sponsored_images).length() , brOffTextSS.length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - - ForegroundColorSpan brOffForegroundSpan = new ForegroundColorSpan(getResources().getColor(R.color.brave_theme_color)); - ForegroundColorSpan brOffForegroundSpan2 = new ForegroundColorSpan(getResources().getColor(R.color.brave_theme_color)); - brOffTextSS.setSpan(brOffForegroundSpan, termsOfServiceIndex, termsOfServiceIndex + getResources().getString(R.string.terms_of_service).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - brOffTextSS.setSpan(brOffForegroundSpan2, (brOffTextSS.length()-1) - getResources().getString(R.string.hide_sponsored_images).length() , brOffTextSS.length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - - bottomSheetText.setMovementMethod(LinkMovementMethod.getInstance()); - bottomSheetText.setText(brOffTextSS); - break; - case SponsoredImageUtil.BR_ON_ADS_OFF: - bottomSheetTitleText.setText(getResources().getString(R.string.turn_on_brave_ads_to_claim)); - turnOnAdsButton.setVisibility(View.VISIBLE); - String brOnAdsOffText = String.format( getResources().getString(R.string.br_on_ads_on_text2), getResources().getString(R.string.hide_sponsored_images)); - Spanned brOnAdsOffSpanned = BraveRewardsHelper.spannedFromHtmlString(brOnAdsOffText); - SpannableString brOnAdsOffSS = new SpannableString(brOnAdsOffSpanned.toString()); - - ClickableSpan brOnAdsOffHideSponsoredImagesClickableSpan = new ClickableSpan() { - @Override - public void onClick(@NonNull View textView) { - hideSponsoredImages(); - dismiss(); - } - @Override - public void updateDrawState(@NonNull TextPaint ds) { - super.updateDrawState(ds); - ds.setUnderlineText(false); - } - }; - - brOnAdsOffSS.setSpan(brOnAdsOffHideSponsoredImagesClickableSpan, (brOnAdsOffSS.length()-1) - getResources().getString(R.string.hide_sponsored_images).length() , brOnAdsOffSS.length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - - ForegroundColorSpan brOnAdsOffForegroundSpan = new ForegroundColorSpan(getResources().getColor(R.color.brave_theme_color)); - brOnAdsOffSS.setSpan(brOnAdsOffForegroundSpan, (brOnAdsOffSS.length()-1) - getResources().getString(R.string.hide_sponsored_images).length() , brOnAdsOffSS.length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - - bottomSheetText.setMovementMethod(LinkMovementMethod.getInstance()); - bottomSheetText.setText(brOnAdsOffSS); - break; - case SponsoredImageUtil.BR_ON_ADS_ON: - bottomSheetTitleText.setText(getResources().getString(R.string.you_are_getting_paid)); - String brOnAdsOnText = String.format(getResources().getString(R.string.br_on_ads_on_text), getResources().getString(R.string.learn_more), getResources().getString(R.string.hide_sponsored_images)); - Spanned brOnAdsOnSpanned = BraveRewardsHelper.spannedFromHtmlString(brOnAdsOnText); - SpannableString brOnAdsOnSS = new SpannableString(brOnAdsOnSpanned.toString()); - - ClickableSpan learnMoreClickableSpan = new ClickableSpan() { - @Override - public void onClick(@NonNull View textView) { - NTPUtil.openUrlInSameTab(BRAVE_REWARDS_LEARN_MORE); - dismiss(); - } - @Override - public void updateDrawState(@NonNull TextPaint ds) { - super.updateDrawState(ds); - ds.setUnderlineText(false); - } - }; - - ClickableSpan brOnAdsOnHideSponsoredImagesClickableSpan = new ClickableSpan() { - @Override - public void onClick(@NonNull View textView) { - hideSponsoredImages(); - dismiss(); - } - @Override - public void updateDrawState(@NonNull TextPaint ds) { - super.updateDrawState(ds); - ds.setUnderlineText(false); - } - }; - - brOnAdsOnSS.setSpan(learnMoreClickableSpan, 0, getResources().getString(R.string.learn_more).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - brOnAdsOnSS.setSpan(brOnAdsOnHideSponsoredImagesClickableSpan, (brOnAdsOnSS.length()-1) - getResources().getString(R.string.hide_sponsored_images).length() , brOnAdsOnSS.length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - - ForegroundColorSpan brOnAdsOnForegroundSpan = new ForegroundColorSpan(getResources().getColor(R.color.brave_theme_color)); - ForegroundColorSpan brOnAdsOnForegroundSpan2 = new ForegroundColorSpan(getResources().getColor(R.color.brave_theme_color)); - brOnAdsOnSS.setSpan(brOnAdsOnForegroundSpan, 0, getResources().getString(R.string.learn_more).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - brOnAdsOnSS.setSpan(brOnAdsOnForegroundSpan2, (brOnAdsOnSS.length()-1) - getResources().getString(R.string.hide_sponsored_images).length() , brOnAdsOnSS.length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - - bottomSheetText.setMovementMethod(LinkMovementMethod.getInstance()); - bottomSheetText.setText(brOnAdsOnSS); - break; + tosTextSS.setSpan(tosClickableSpan, termsOfServiceIndex, termsOfServiceIndex + getResources().getString(R.string.terms_of_service).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + ForegroundColorSpan brOffForegroundSpan = new ForegroundColorSpan(getResources().getColor(R.color.brave_theme_color)); + tosTextSS.setSpan(brOffForegroundSpan, termsOfServiceIndex, termsOfServiceIndex + getResources().getString(R.string.terms_of_service).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + bottomSheetTosText.setMovementMethod(LinkMovementMethod.getInstance()); + bottomSheetTosText.setText(tosTextSS); + break; + case SponsoredImageUtil.BR_ON_ADS_ON: + bottomSheetTitleText.setText(getResources().getString(R.string.you_are_earning_tokens2)); + break; } } @@ -303,7 +221,7 @@ public class RewardsBottomSheetDialogFragment extends BottomSheetDialogFragment{ private void reloadTab() { ChromeTabbedActivity chromeTabbedActivity = BraveRewardsHelper.getChromeTabbedActivity(); - if(chromeTabbedActivity != null) { + if (chromeTabbedActivity != null) { Tab currentTab = chromeTabbedActivity.getActivityTab(); SponsoredTab sponsoredTab = TabAttributes.from(currentTab).get(String.valueOf(((TabImpl)currentTab).getId())); sponsoredTab.setNTPImage(SponsoredImageUtil.getBackgroundImage()); diff --git a/android/java/org/chromium/chrome/browser/ntp_background_images/util/NTPUtil.java b/android/java/org/chromium/chrome/browser/ntp_background_images/util/NTPUtil.java index a1c51147e07..f2b9ae3235e 100644 --- a/android/java/org/chromium/chrome/browser/ntp_background_images/util/NTPUtil.java +++ b/android/java/org/chromium/chrome/browser/ntp_background_images/util/NTPUtil.java @@ -7,6 +7,7 @@ package org.chromium.chrome.browser.ntp_background_images.util; import android.os.Bundle; import android.os.Build; +import android.support.annotation.NonNull; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -20,6 +21,12 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Button; +import android.text.style.ClickableSpan; +import android.text.style.ForegroundColorSpan; +import android.text.SpannableString; +import android.text.Spanned; +import android.text.method.LinkMovementMethod; +import android.text.TextPaint; import android.os.Handler; import android.net.Uri; import java.io.IOException; @@ -58,23 +65,23 @@ import org.chromium.chrome.browser.ntp_background_images.RewardsBottomSheetDialo import static org.chromium.ui.base.ViewUtils.dpToPx; public class NTPUtil { - private static final int BOTTOM_TOOLBAR_HEIGHT = 56; + private static final int BOTTOM_TOOLBAR_HEIGHT = 56; private static final String REMOVED_SITES = "removed_sites"; - public static HashMap> imageCache = - new HashMap>(); + public static HashMap> imageCache = + new HashMap>(); public static void turnOnAds() { BraveAdsNativeHelper.nativeSetAdsEnabled(Profile.getLastUsedProfile()); } public static void updateOrientedUI(Context context, ViewGroup view) { - LinearLayout parentLayout= (LinearLayout)view.findViewById(R.id.parent_layout); + LinearLayout parentLayout = (LinearLayout)view.findViewById(R.id.parent_layout); ViewGroup mainLayout = view.findViewById(R.id.ntp_main_layout); ViewGroup imageCreditLayout = view.findViewById(R.id.image_credit_layout); ImageView sponsoredLogo = (ImageView)view.findViewById(R.id.sponsored_logo); - FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(dpToPx(context,170), dpToPx(context,170)); + FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(dpToPx(context, 170), dpToPx(context, 170)); boolean isTablet = DeviceFormFactor.isNonMultiDisplayContextOnTablet(context); @@ -89,7 +96,7 @@ public class NTPUtil { parentLayout.setOrientation(LinearLayout.VERTICAL); - LinearLayout.LayoutParams mainLayoutLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0); + LinearLayout.LayoutParams mainLayoutLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0); mainLayoutLayoutParams.weight = 1f; mainLayout.setLayoutParams(mainLayoutLayoutParams); @@ -113,7 +120,7 @@ public class NTPUtil { imageCreditLayoutParams.weight = 0.4f; imageCreditLayout.setLayoutParams(imageCreditLayoutParams); - layoutParams.setMargins(dpToPx(context,32), 0, 0, 0); + layoutParams.setMargins(dpToPx(context, 32), 0, 0, 0); layoutParams.gravity = Gravity.BOTTOM | Gravity.START; sponsoredLogo.setLayoutParams(layoutParams); } @@ -127,7 +134,7 @@ public class NTPUtil { parentLayout.setOrientation(LinearLayout.VERTICAL); - LinearLayout.LayoutParams mainLayoutLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0); + LinearLayout.LayoutParams mainLayoutLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0); mainLayoutLayoutParams.weight = 1f; mainLayout.setLayoutParams(mainLayoutLayoutParams); @@ -147,16 +154,16 @@ public class NTPUtil { if (ntpImage instanceof Wallpaper) { return SponsoredImageUtil.BR_ON_ADS_ON; } - } else if(BraveAdsNativeHelper.nativeIsLocaleValid(Profile.getLastUsedProfile())) { + } else if (BraveAdsNativeHelper.nativeIsLocaleValid(Profile.getLastUsedProfile())) { if (ntpImage instanceof Wallpaper) { return SponsoredImageUtil.BR_ON_ADS_OFF ; } else { - return SponsoredImageUtil.BR_ON_ADS_OFF_BG_IMAGE; + return SponsoredImageUtil.BR_INVALID_OPTION; } } } else { if (ntpImage instanceof Wallpaper && !mBraveRewardsNativeWorker.IsCreateWalletInProcess()) { - return SponsoredImageUtil.BR_OFF; + return SponsoredImageUtil.BR_INVALID_OPTION; } } } @@ -171,10 +178,10 @@ public class NTPUtil { handler.postDelayed(new Runnable() { @Override public void run() { - BackgroundImagesPreferences.setOnPreferenceValue(BackgroundImagesPreferences.PREF_SHOW_NON_DISTRUPTIVE_BANNER,false); + BackgroundImagesPreferences.setOnPreferenceValue(BackgroundImagesPreferences.PREF_SHOW_NON_DISTRUPTIVE_BANNER, false); boolean isTablet = DeviceFormFactor.isNonMultiDisplayContextOnTablet(chromeActivity); - if(isTablet || (!isTablet && ConfigurationUtils.isLandscape(chromeActivity))) { + if (isTablet || (!isTablet && ConfigurationUtils.isLandscape(chromeActivity))) { FrameLayout.LayoutParams nonDistruptiveBannerLayoutParams = new FrameLayout.LayoutParams(dpToPx(chromeActivity, 400), FrameLayout.LayoutParams.WRAP_CONTENT); nonDistruptiveBannerLayoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; nonDistruptiveBannerLayout.setLayoutParams(nonDistruptiveBannerLayoutParams); @@ -187,7 +194,6 @@ public class NTPUtil { TextView bannerHeader = nonDistruptiveBannerLayout.findViewById(R.id.ntp_banner_header); TextView bannerText = nonDistruptiveBannerLayout.findViewById(R.id.ntp_banner_text); - TextView learnMoreText = nonDistruptiveBannerLayout.findViewById(R.id.ntp_banner_learn_more_text); Button turnOnAdsButton = nonDistruptiveBannerLayout.findViewById(R.id.btn_turn_on_ads); ImageView bannerClose = nonDistruptiveBannerLayout.findViewById(R.id.ntp_banner_close); bannerClose.setOnClickListener(new View.OnClickListener() { @@ -198,23 +204,6 @@ public class NTPUtil { } }); - learnMoreText.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - nonDistruptiveBannerLayout.setVisibility(View.GONE); - - RewardsBottomSheetDialogFragment rewardsBottomSheetDialogFragment = RewardsBottomSheetDialogFragment.newInstance(); - Bundle bundle = new Bundle(); - bundle.putInt(SponsoredImageUtil.NTP_TYPE, ntpType); - rewardsBottomSheetDialogFragment.setArguments(bundle); - rewardsBottomSheetDialogFragment.setNewTabPageListener(newTabPageListener); - rewardsBottomSheetDialogFragment.show(chromeActivity.getSupportFragmentManager(), "rewards_bottom_sheet_dialog_fragment"); - rewardsBottomSheetDialogFragment.setCancelable(false); - - sponsoredTab.updateBannerPref(); - } - }); - turnOnAdsButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -225,28 +214,69 @@ public class NTPUtil { } }); - switch(ntpType) { - case SponsoredImageUtil.BR_OFF: - bannerText.setText(chromeActivity.getResources().getString(R.string.get_paid_to_see_image)); - learnMoreText.setVisibility(View.VISIBLE); - break; - case SponsoredImageUtil.BR_ON_ADS_OFF: - bannerText.setText(chromeActivity.getResources().getString(R.string.get_paid_to_see_image)); - learnMoreText.setVisibility(View.VISIBLE); - break; - case SponsoredImageUtil.BR_ON_ADS_OFF_BG_IMAGE: - bannerText.setText(chromeActivity.getResources().getString(R.string.you_can_support_creators)); - turnOnAdsButton.setVisibility(View.VISIBLE); - break; - case SponsoredImageUtil.BR_ON_ADS_ON: - bannerText.setText(chromeActivity.getResources().getString(R.string.you_are_getting_paid)); - learnMoreText.setVisibility(View.VISIBLE); - break; + switch (ntpType) { + case SponsoredImageUtil.BR_OFF: + bannerText.setText(chromeActivity.getResources().getString(R.string.get_paid_to_see_image)); + break; + case SponsoredImageUtil.BR_ON_ADS_OFF: + bannerText.setMovementMethod(LinkMovementMethod.getInstance()); + bannerText.setText(getBannerText(chromeActivity, ntpType, nonDistruptiveBannerLayout, sponsoredTab, newTabPageListener)); + break; + case SponsoredImageUtil.BR_ON_ADS_OFF_BG_IMAGE: + bannerText.setText(chromeActivity.getResources().getString(R.string.you_can_support_creators)); + turnOnAdsButton.setVisibility(View.VISIBLE); + break; + case SponsoredImageUtil.BR_ON_ADS_ON: + bannerText.setMovementMethod(LinkMovementMethod.getInstance()); + bannerText.setText(getBannerText(chromeActivity, ntpType, nonDistruptiveBannerLayout, sponsoredTab, newTabPageListener)); + break; } } }, 1500); } + private static SpannableString getBannerText(ChromeActivity chromeActivity, int ntpType, + View bannerLayout, SponsoredTab sponsoredTab, NewTabPageListener newTabPageListener) { + String bannerText = ""; + if (ntpType == SponsoredImageUtil.BR_ON_ADS_ON) { + bannerText = String.format(chromeActivity.getResources().getString(R.string.you_are_earning_tokens), + chromeActivity.getResources().getString(R.string.learn_more)); + } else if (ntpType == SponsoredImageUtil.BR_ON_ADS_OFF) { + bannerText = String.format(chromeActivity.getResources().getString(R.string.earn_tokens_for_viewing), + chromeActivity.getResources().getString(R.string.learn_more)); + } + int learnMoreIndex = bannerText.indexOf(chromeActivity.getResources().getString(R.string.learn_more)); + Spanned learnMoreSpanned = BraveRewardsHelper.spannedFromHtmlString(bannerText); + SpannableString learnMoreTextSS = new SpannableString(learnMoreSpanned.toString()); + + ClickableSpan learnMoreClickableSpan = new ClickableSpan() { + @Override + public void onClick(@NonNull View textView) { + bannerLayout.setVisibility(View.GONE); + + RewardsBottomSheetDialogFragment rewardsBottomSheetDialogFragment = RewardsBottomSheetDialogFragment.newInstance(); + Bundle bundle = new Bundle(); + bundle.putInt(SponsoredImageUtil.NTP_TYPE, ntpType); + rewardsBottomSheetDialogFragment.setArguments(bundle); + rewardsBottomSheetDialogFragment.setNewTabPageListener(newTabPageListener); + rewardsBottomSheetDialogFragment.show(chromeActivity.getSupportFragmentManager(), "rewards_bottom_sheet_dialog_fragment"); + rewardsBottomSheetDialogFragment.setCancelable(false); + + sponsoredTab.updateBannerPref(); + } + @Override + public void updateDrawState(@NonNull TextPaint ds) { + super.updateDrawState(ds); + ds.setUnderlineText(false); + } + }; + + learnMoreTextSS.setSpan(learnMoreClickableSpan, learnMoreIndex, learnMoreIndex + chromeActivity.getResources().getString(R.string.learn_more).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + ForegroundColorSpan brOffForegroundSpan = new ForegroundColorSpan(chromeActivity.getResources().getColor(R.color.brave_theme_color)); + learnMoreTextSS.setSpan(brOffForegroundSpan, learnMoreIndex, learnMoreIndex + chromeActivity.getResources().getString(R.string.learn_more).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + return learnMoreTextSS; + } + public static Bitmap getWallpaperBitmap(NTPImage ntpImage, int layoutWidth, int layoutHeight) { Context mContext = ContextUtils.getApplicationContext(); @@ -263,24 +293,24 @@ public class NTPUtil { Wallpaper mWallpaper = (Wallpaper) ntpImage; InputStream inputStream = null; try { - Uri imageFileUri = Uri.parse("file://"+mWallpaper.getImagePath()); + Uri imageFileUri = Uri.parse("file://" + mWallpaper.getImagePath()); inputStream = mContext.getContentResolver().openInputStream(imageFileUri); imageBitmap = BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); - } catch(IOException exc) { + } catch (IOException exc) { Log.e("NTP", exc.getMessage()); } finally { - try { - if (inputStream != null) { - inputStream.close(); + try { + if (inputStream != null) { + inputStream.close(); + } + } catch (IOException exception) { + Log.e("NTP", exception.getMessage()); + return null; } - } catch (IOException exception) { - Log.e("NTP", exception.getMessage()); - return null; - } } - centerPointX = mWallpaper.getFocalPointX() == 0 ? (imageBitmap.getWidth()/2) : mWallpaper.getFocalPointX(); - centerPointY = mWallpaper.getFocalPointY() == 0 ? (imageBitmap.getHeight()/2) : mWallpaper.getFocalPointY(); + centerPointX = mWallpaper.getFocalPointX() == 0 ? (imageBitmap.getWidth() / 2) : mWallpaper.getFocalPointX(); + centerPointY = mWallpaper.getFocalPointY() == 0 ? (imageBitmap.getHeight() / 2) : mWallpaper.getFocalPointY(); } else { BackgroundImage mBackgroundImage = (BackgroundImage) ntpImage; imageBitmap = BitmapFactory.decodeResource(mContext.getResources(), mBackgroundImage.getImageDrawable(), options); @@ -320,7 +350,7 @@ public class NTPUtil { startX = newImageWidth - layoutWidth; } - int startY = (newImageHeight - layoutHeight)/2; + int startY = (newImageHeight - layoutHeight) / 2; if (centerPointY > 0) { float centerRatioY = centerPointY / imageHeight; newImageWidth = layoutWidth; @@ -358,11 +388,11 @@ public class NTPUtil { InputStream inputStream = null; Bitmap topSiteIcon = null; try { - Uri imageFileUri = Uri.parse("file://"+ iconPath); + Uri imageFileUri = Uri.parse("file://" + iconPath); inputStream = mContext.getContentResolver().openInputStream(imageFileUri); topSiteIcon = BitmapFactory.decodeStream(inputStream); inputStream.close(); - } catch(IOException exc) { + } catch (IOException exc) { Log.e("NTP", exc.getMessage()); topSiteIcon = null; } finally { @@ -396,7 +426,7 @@ public class NTPUtil { if (!urlSet.contains(url)) { urlSet.add(url); } - + SharedPreferences mSharedPreferences = ContextUtils.getAppSharedPreferences(); SharedPreferences.Editor sharedPreferencesEditor = mSharedPreferences.edit(); sharedPreferencesEditor.putStringSet(REMOVED_SITES, urlSet); @@ -404,20 +434,20 @@ public class NTPUtil { } public static boolean shouldEnableNTPFeature(boolean isMoreTabs) { - if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M - || (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !isMoreTabs)) { - return true; - } - return false; + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M + || (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !isMoreTabs)) { + return true; + } + return false; } public static NTPImage getNTPImage(NTPBackgroundImagesBridge mNTPBackgroundImagesBridge) { - Wallpaper mWallpaper = mNTPBackgroundImagesBridge.getCurrentWallpaper(); - if (mWallpaper != null) { - return mWallpaper; - } else { - return SponsoredImageUtil.getBackgroundImage(); - } + Wallpaper mWallpaper = mNTPBackgroundImagesBridge.getCurrentWallpaper(); + if (mWallpaper != null) { + return mWallpaper; + } else { + return SponsoredImageUtil.getBackgroundImage(); + } } public static boolean isReferralEnabled() { @@ -429,14 +459,14 @@ public class NTPUtil { public static void openNewTab(boolean isIncognito, String url) { ChromeTabbedActivity chromeTabbedActivity = BraveRewardsHelper.getChromeTabbedActivity(); - if(chromeTabbedActivity != null) { + if (chromeTabbedActivity != null) { chromeTabbedActivity.getTabCreator(isIncognito).launchUrl(url, TabLaunchType.FROM_CHROME_UI); } } public static void openUrlInSameTab(String url) { ChromeTabbedActivity chromeTabbedActivity = BraveRewardsHelper.getChromeTabbedActivity(); - if(chromeTabbedActivity != null) { + if (chromeTabbedActivity != null) { LoadUrlParams loadUrlParams = new LoadUrlParams(url); chromeTabbedActivity.getActivityTab().loadUrl(loadUrlParams); } diff --git a/android/java/res/layout/ntp_bottom_sheet.xml b/android/java/res/layout/ntp_bottom_sheet.xml index 1d267593d6b..f78194e7ea1 100644 --- a/android/java/res/layout/ntp_bottom_sheet.xml +++ b/android/java/res/layout/ntp_bottom_sheet.xml @@ -9,11 +9,10 @@ android:id="@+id/ntp_content" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:maxWidth="400dp" - android:padding="16dp"> + android:maxWidth="400dp"> @@ -25,7 +24,7 @@ android:drawableStart="@drawable/btn_bat" android:drawablePadding="8dp" android:textSize="20sp" - android:layout_marginBottom="16dp" + android:layout_margin="16dp" android:textColor="@android:color/black" android:text="@string/brave_ui_brave_rewards"/> @@ -35,63 +34,103 @@ android:layout_height="wrap_content" android:textStyle="bold" android:textSize="16sp" + android:layout_marginStart="16dp" + android:layout_marginEnd="16dp" android:textColor="@android:color/black" android:layout_marginBottom="16dp"/> +