Merge pull request #16205 from brave/dp_option_to_remove_stats

Add an option to hide brave stats
This commit is contained in:
Deep Pandya
2022-12-02 16:46:58 -05:00
committed by GitHub
9 changed files with 96 additions and 30 deletions
@@ -183,6 +183,7 @@ public class BraveNewTabPageLayout
private SharedPreferencesManager.Observer mPreferenceObserver;
private boolean mComesFromNewTab;
private boolean mIsTopSitesEnabled;
private boolean mIsBraveStatsEnabled;
private boolean mIsDisplayNews;
private boolean mIsDisplayNewsOptin;
@@ -371,15 +372,21 @@ public class BraveNewTabPageLayout
BackgroundImagesPreferences.PREF_SHOW_TOP_SITES, true);
}
private boolean shouldDisplayBraveStats() {
return ContextUtils.getAppSharedPreferences().getBoolean(
BackgroundImagesPreferences.PREF_SHOW_BRAVE_STATS, true);
}
private void setNtpRecyclerView(LinearLayoutManager linearLayoutManager) {
mIsTopSitesEnabled = shouldDisplayTopSites();
mIsBraveStatsEnabled = shouldDisplayBraveStats();
if (mNtpAdapter == null) {
mNtpAdapter = new BraveNtpAdapter(mActivity, this, Glide.with(mActivity),
mNewsItemsFeedCard, mBraveNewsController, mMvTilesContainerLayout,
mNtpImageGlobal, mSponsoredTab, mWallpaper, mSponsoredLogo,
mNTPBackgroundImagesBridge, false, mRecyclerView.getHeight(),
mIsTopSitesEnabled, mIsDisplayNews, mIsDisplayNewsOptin);
mIsTopSitesEnabled, mIsBraveStatsEnabled, mIsDisplayNews, mIsDisplayNewsOptin);
mRecyclerView.setAdapter(mNtpAdapter);
@@ -393,6 +400,7 @@ public class BraveNewTabPageLayout
} else {
mNtpAdapter.setRecyclerViewHeight(mRecyclerView.getHeight());
mNtpAdapter.setTopSitesEnabled(mIsTopSitesEnabled);
mNtpAdapter.setBraveStatsEnabled(mIsBraveStatsEnabled);
mNtpAdapter.setDisplayNews(mIsDisplayNews);
}
@@ -767,6 +775,9 @@ public class BraveNewTabPageLayout
} else if (TextUtils.equals(key, BackgroundImagesPreferences.PREF_SHOW_TOP_SITES)) {
mIsTopSitesEnabled = shouldDisplayTopSites();
mNtpAdapter.setTopSitesEnabled(mIsTopSitesEnabled);
} else if (TextUtils.equals(key, BackgroundImagesPreferences.PREF_SHOW_BRAVE_STATS)) {
mIsBraveStatsEnabled = shouldDisplayBraveStats();
mNtpAdapter.setBraveStatsEnabled(mIsBraveStatsEnabled);
}
};
}
@@ -48,7 +48,9 @@ import org.chromium.chrome.browser.ntp_background_images.model.SponsoredTab;
import org.chromium.chrome.browser.ntp_background_images.model.Wallpaper;
import org.chromium.chrome.browser.ntp_background_images.util.NTPUtil;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.settings.BackgroundImagesPreferences;
import org.chromium.chrome.browser.util.BraveConstants;
import org.chromium.chrome.browser.util.TabUtils;
import org.chromium.components.user_prefs.UserPrefs;
@@ -74,6 +76,7 @@ public class BraveNtpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
private boolean mIsNewContent;
private boolean mIsNewContentLoading;
private boolean mIsTopSitesEnabled;
private boolean mIsBraveStatsEnabled;
private int mRecyclerViewHeight;
private int mStatsHeight;
private int mTopSitesHeight;
@@ -97,8 +100,8 @@ public class BraveNtpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
BraveNewsController braveNewsController, View mvTilesContainerLayout, NTPImage ntpImage,
SponsoredTab sponsoredTab, Wallpaper wallpaper, Bitmap sponsoredLogo,
NTPBackgroundImagesBridge nTPBackgroundImagesBridge, boolean isNewsLoading,
int recyclerViewHeight, boolean isTopSitesEnabled, boolean isDisplayNews,
boolean isDisplayNewsOptin) {
int recyclerViewHeight, boolean isTopSitesEnabled, boolean isBraveStatsEnabled,
boolean isDisplayNews, boolean isDisplayNewsOptin) {
mActivity = activity;
mOnBraveNtpListener = onBraveNtpListener;
mGlide = glide;
@@ -113,6 +116,7 @@ public class BraveNtpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
mIsNewsLoading = isNewsLoading;
mRecyclerViewHeight = recyclerViewHeight;
mIsTopSitesEnabled = isTopSitesEnabled;
mIsBraveStatsEnabled = isBraveStatsEnabled;
mIsDisplayNews = isDisplayNews;
mIsDisplayNewsOptin = isDisplayNewsOptin;
}
@@ -122,7 +126,10 @@ public class BraveNtpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
if (holder instanceof StatsViewHolder) {
StatsViewHolder statsViewHolder = (StatsViewHolder) holder;
statsViewHolder.titleLayout.setVisibility(View.GONE);
statsViewHolder.hideStatsImg.setOnClickListener(view -> {
SharedPreferencesManager.getInstance().writeBoolean(
BackgroundImagesPreferences.PREF_SHOW_BRAVE_STATS, false);
});
List<Pair<String, String>> statsPairs = BraveStatsUtil.getStatsPairs();
statsViewHolder.adsBlockedCountTv.setText(statsPairs.get(0).first);
@@ -402,7 +409,7 @@ public class BraveNtpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
// Will be used in privacy hub feature
private boolean isStatsEnabled() {
return true;
return mIsBraveStatsEnabled;
}
public int getTopSitesCount() {
@@ -422,6 +429,17 @@ public class BraveNtpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
}
}
public void setBraveStatsEnabled(boolean isBraveStatsEnabled) {
if (mIsBraveStatsEnabled != isBraveStatsEnabled) {
mIsBraveStatsEnabled = isBraveStatsEnabled;
if (mIsBraveStatsEnabled) {
notifyItemInserted(getStatsCount());
} else {
notifyItemRemoved(getStatsCount());
}
}
}
public void setDisplayNews(boolean isDisplayNews) {
if (mIsDisplayNews != isDisplayNews) {
mIsDisplayNews = isDisplayNews;
@@ -517,6 +535,7 @@ public class BraveNtpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
public static class StatsViewHolder extends RecyclerView.ViewHolder {
LinearLayout ntpStatsLayout;
LinearLayout titleLayout;
ImageView hideStatsImg;
TextView adsBlockedCountTv;
TextView adsBlockedCountTextTv;
TextView dataSavedValueTv;
@@ -528,6 +547,7 @@ public class BraveNtpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
super(itemView);
this.ntpStatsLayout = (LinearLayout) itemView.findViewById(R.id.ntp_stats_layout);
this.titleLayout = (LinearLayout) itemView.findViewById(R.id.brave_stats_title_layout);
this.hideStatsImg = (ImageView) itemView.findViewById(R.id.widget_more_option);
this.adsBlockedCountTv =
(TextView) itemView.findViewById(R.id.brave_stats_text_ads_count);
this.adsBlockedCountTextTv =
@@ -32,6 +32,7 @@ public class BackgroundImagesPreferences
public static final String PREF_SHOW_BACKGROUND_IMAGES = "show_background_images";
public static final String PREF_SHOW_SPONSORED_IMAGES = "show_sponsored_images";
public static final String PREF_SHOW_TOP_SITES = "show_top_sites";
public static final String PREF_SHOW_BRAVE_STATS = "show_brave_stats";
public static final String PREF_SHOW_NON_DISRUPTIVE_BANNER = "show_non_disruptive_banner";
public static final String PREF_SHOW_BRE_BANNER = "show_bre_banner";
@@ -86,6 +87,13 @@ public class BackgroundImagesPreferences
ContextUtils.getAppSharedPreferences().getBoolean(PREF_SHOW_TOP_SITES, true));
mShowTopSitesPref.setOnPreferenceChangeListener(this);
}
mShowBraveStatsPref = (ChromeSwitchPreference) findPreference(PREF_SHOW_BRAVE_STATS);
if (mShowBraveStatsPref != null) {
mShowBraveStatsPref.setEnabled(true);
mShowBraveStatsPref.setChecked(
ContextUtils.getAppSharedPreferences().getBoolean(PREF_SHOW_BRAVE_STATS, true));
mShowBraveStatsPref.setOnPreferenceChangeListener(this);
}
}
@Override