Skip to content

Commit

Permalink
Merge pull request #7256 from brave/pr7255_fallback_ui_for_top_tiles_…
Browse files Browse the repository at this point in the history
…android_1.18.x

Add layout for fall back UI for top tiles (uplift to 1.18.x)
  • Loading branch information
kjozwiak authored Nov 27, 2020
2 parents d94a1dd + e91c02f commit df10a4f
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
package org.chromium.chrome.browser.brave_stats;

import android.util.Pair;
import android.view.View;
import android.widget.TextView;

import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.brave_stats.BraveStatsBottomSheetDialogFragment;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.chrome.browser.profiles.Profile;

import java.text.SimpleDateFormat;
import java.util.Calendar;
Expand Down Expand Up @@ -86,4 +92,40 @@ public static String getCalculatedDate(String dateFormat, int days) {
cal.add(Calendar.DAY_OF_YEAR, days);
return s.format(new Date(cal.getTimeInMillis()));
}

public static void updateBraveStatsLayout(View view) {
Profile mProfile = Profile.getLastUsedRegularProfile();
TextView mAdsBlockedCountTextView =
(TextView) view.findViewById(R.id.brave_stats_text_ads_count);
TextView mDataSavedValueTextView =
(TextView) view.findViewById(R.id.brave_stats_data_saved_value);
TextView mEstTimeSavedCountTextView =
(TextView) view.findViewById(R.id.brave_stats_text_time_count);
TextView mAdsBlockedCountTextTextView =
(TextView) view.findViewById(R.id.brave_stats_text_ads_count_text);
TextView mDataSavedValueTextTextView =
(TextView) view.findViewById(R.id.brave_stats_data_saved_value_text);
TextView mEstTimeSavedCountTextTextView =
(TextView) view.findViewById(R.id.brave_stats_text_time_count_text);

long trackersBlockedCount =
BravePrefServiceBridge.getInstance().getTrackersBlockedCount(mProfile);
long adsBlockedCount = BravePrefServiceBridge.getInstance().getAdsBlockedCount(mProfile);
long dataSaved = BravePrefServiceBridge.getInstance().getDataSaved(mProfile);
long estimatedMillisecondsSaved =
(trackersBlockedCount + adsBlockedCount) * MILLISECONDS_PER_ITEM;

Pair<String, String> adsTrackersPair =
getBraveStatsStringFormNumberPair(adsBlockedCount, false);
Pair<String, String> dataSavedPair = getBraveStatsStringFormNumberPair(dataSaved, true);
Pair<String, String> timeSavedPair =
getBraveStatsStringFromTime(estimatedMillisecondsSaved / 1000);

mAdsBlockedCountTextView.setText(adsTrackersPair.first);
mDataSavedValueTextView.setText(dataSavedPair.first);
mEstTimeSavedCountTextView.setText(timeSavedPair.first);
mAdsBlockedCountTextTextView.setText(adsTrackersPair.second);
mDataSavedValueTextTextView.setText(dataSavedPair.second);
mEstTimeSavedCountTextTextView.setText(timeSavedPair.second);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public class BraveNewTabPageLayout extends NewTabPageLayout {
private static final String BRAVE_BINANCE = "https://brave.com/binance/";
private static final String BRAVE_REF_URL = "https://brave.com/r/";

private View mBraveStatsViewFallBackLayout;

private ImageView bgImageView;
private Profile mProfile;

Expand Down Expand Up @@ -169,6 +171,9 @@ public BraveNewTabPageLayout(Context context, AttributeSet attrs) {
protected void onFinishInflate() {
super.onFinishInflate();

// mBraveStatsViewFallBackLayout = (ViewGroup)
// findViewById(R.id.brave_stats_fallback_layout);

ntpWidgetLayout = findViewById(R.id.ntp_widget_layout);
indicatorLayout = findViewById(R.id.indicator_layout);
ntpWidgetViewPager = findViewById(R.id.ntp_widget_view_pager);
Expand Down Expand Up @@ -197,11 +202,65 @@ public void onPageScrollStateChanged(int state) {}
showWidgetBasedOnOrder();
}

private void showFallBackNTPLayout() {
if (mBraveStatsViewFallBackLayout != null
&& mBraveStatsViewFallBackLayout.getParent() != null) {
((ViewGroup) mBraveStatsViewFallBackLayout.getParent())
.removeView(mBraveStatsViewFallBackLayout);
}
LayoutInflater inflater =
(LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mBraveStatsViewFallBackLayout = inflater.inflate(R.layout.brave_stats_layout, null);

if (mBraveStatsViewFallBackLayout.getLayoutParams()
instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams layoutParams =
(ViewGroup.MarginLayoutParams) mBraveStatsViewFallBackLayout.getLayoutParams();
layoutParams.setMargins(0, dpToPx(mActivity, 16), 0, dpToPx(mActivity, 16));
mBraveStatsViewFallBackLayout.requestLayout();
}

mBraveStatsViewFallBackLayout.findViewById(R.id.brave_stats_title_layout)
.setVisibility(View.GONE);
((TextView) mBraveStatsViewFallBackLayout.findViewById(R.id.brave_stats_text_ads))
.setTextColor(mActivity.getResources().getColor(R.color.shield_text_color));
((TextView) mBraveStatsViewFallBackLayout.findViewById(R.id.brave_stats_data_saved_text))
.setTextColor(mActivity.getResources().getColor(R.color.shield_text_color));
((TextView) mBraveStatsViewFallBackLayout.findViewById(R.id.brave_stats_text_time))
.setTextColor(mActivity.getResources().getColor(R.color.shield_text_color));
((TextView) mBraveStatsViewFallBackLayout.findViewById(R.id.brave_stats_text_time_count))
.setTextColor(mActivity.getResources().getColor(R.color.shield_text_color));
((TextView) mBraveStatsViewFallBackLayout.findViewById(
R.id.brave_stats_text_time_count_text))
.setTextColor(mActivity.getResources().getColor(R.color.shield_text_color));
mBraveStatsViewFallBackLayout.setBackgroundColor(
mActivity.getResources().getColor(android.R.color.transparent));
mBraveStatsViewFallBackLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkForBraveStats();
}
});
BraveStatsUtil.updateBraveStatsLayout(mBraveStatsViewFallBackLayout);
if (mSiteSectionView.getParent() != null) {
((ViewGroup) mSiteSectionView.getParent()).removeView(mSiteSectionView);
}
mainLayout.addView(mBraveStatsViewFallBackLayout, 0);
int insertionPoint = mainLayout.indexOfChild(findViewById(R.id.ntp_middle_spacer)) + 1;
if (!mNTPBackgroundImagesBridge.isSuperReferral()
|| !NTPBackgroundImagesBridge.enableSponsoredImages()
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
mainLayout.addView(mSiteSectionView, insertionPoint);
}

private List<NTPWidgetItem> setWidgetList() {
NTPWidgetManager ntpWidgetManager = NTPWidgetManager.getInstance();
LayoutInflater inflater =
(LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Map<Integer, NTPWidgetItem> ntpWidgetMap = new TreeMap<>();
if (mSiteSectionView != null && mSiteSectionView.getParent() != null) {
((ViewGroup) mSiteSectionView.getParent()).removeView(mSiteSectionView);
}

for (String widget : ntpWidgetManager.getUsedWidgets()) {
NTPWidgetItem ntpWidgetItem = NTPWidgetManager.mWidgetsMap.get(widget);
Expand Down Expand Up @@ -237,9 +296,6 @@ public void onClick(View v) {
getTileGroup().hasReceivedData() && getTileGroup().isEmpty();
if (mSiteSectionView != null && !showPlaceholder) {
mTopsiteErrorMessage.setVisibility(View.GONE);
if (mSiteSectionView.getParent() != null) {
((ViewGroup) mSiteSectionView.getParent()).removeView(mSiteSectionView);
}
if (mSiteSectionView.getLayoutParams()
instanceof ViewGroup.MarginLayoutParams) {
mSiteSectionView.setPadding(0, dpToPx(mActivity, 8), 0, 0);
Expand Down Expand Up @@ -321,8 +377,17 @@ private void showWidgets() {
List<NTPWidgetItem> tempList = setWidgetList();
if (tempList.size() > 0) {
ntpWidgetLayout.setVisibility(View.VISIBLE);
if (mBraveStatsViewFallBackLayout != null
&& mBraveStatsViewFallBackLayout.getParent() != null) {
((ViewGroup) mBraveStatsViewFallBackLayout.getParent())
.removeView(mBraveStatsViewFallBackLayout);
}
} else {
ntpWidgetLayout.setVisibility(View.GONE);
if (!UserPrefs.get(Profile.getLastUsedRegularProfile())
.getBoolean(BravePref.NEW_TAB_PAGE_SHOW_BACKGROUND_IMAGE)) {
showFallBackNTPLayout();
}
}

if (ntpWidgetAdapter != null) {
Expand Down Expand Up @@ -357,7 +422,13 @@ protected void insertSiteSectionView() {
}

protected int getMaxRowsForMostVisitedTiles() {
return 1;
if (NTPWidgetManager.getInstance().getUsedWidgets().size() <= 0
&& !UserPrefs.get(Profile.getLastUsedRegularProfile())
.getBoolean(BravePref.NEW_TAB_PAGE_SHOW_BACKGROUND_IMAGE)) {
return 2;
} else {
return 1;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void onClick(View view) {
});
}
if (ntpWidgetItem.getWidgetType().equals(NTPWidgetManager.PREF_PRIVATE_STATS)) {
updateBraveStats(mainView);
BraveStatsUtil.updateBraveStatsLayout(mainView);
} else if (ntpWidgetItem.getWidgetType().equals(NTPWidgetManager.PREF_BINANCE)) {
Button connectButton = mainView.findViewById(R.id.btn_connect);
LinearLayout bianceDisconnectLayout =
Expand Down Expand Up @@ -143,42 +143,6 @@ public void removeWidgetItem(int position) {
notifyDataSetChanged();
}

private void updateBraveStats(View view) {
TextView mAdsBlockedCountTextView =
(TextView) view.findViewById(R.id.brave_stats_text_ads_count);
TextView mDataSavedValueTextView =
(TextView) view.findViewById(R.id.brave_stats_data_saved_value);
TextView mEstTimeSavedCountTextView =
(TextView) view.findViewById(R.id.brave_stats_text_time_count);
TextView mAdsBlockedCountTextTextView =
(TextView) view.findViewById(R.id.brave_stats_text_ads_count_text);
TextView mDataSavedValueTextTextView =
(TextView) view.findViewById(R.id.brave_stats_data_saved_value_text);
TextView mEstTimeSavedCountTextTextView =
(TextView) view.findViewById(R.id.brave_stats_text_time_count_text);

long trackersBlockedCount =
BravePrefServiceBridge.getInstance().getTrackersBlockedCount(mProfile);
long adsBlockedCount = BravePrefServiceBridge.getInstance().getAdsBlockedCount(mProfile);
long dataSaved = BravePrefServiceBridge.getInstance().getDataSaved(mProfile);
long estimatedMillisecondsSaved =
(trackersBlockedCount + adsBlockedCount) * BraveStatsUtil.MILLISECONDS_PER_ITEM;

Pair<String, String> adsTrackersPair =
BraveStatsUtil.getBraveStatsStringFormNumberPair(adsBlockedCount, false);
Pair<String, String> dataSavedPair =
BraveStatsUtil.getBraveStatsStringFormNumberPair(dataSaved, true);
Pair<String, String> timeSavedPair =
BraveStatsUtil.getBraveStatsStringFromTime(estimatedMillisecondsSaved / 1000);

mAdsBlockedCountTextView.setText(adsTrackersPair.first);
mDataSavedValueTextView.setText(dataSavedPair.first);
mEstTimeSavedCountTextView.setText(timeSavedPair.first);
mAdsBlockedCountTextTextView.setText(adsTrackersPair.second);
mDataSavedValueTextTextView.setText(dataSavedPair.second);
mEstTimeSavedCountTextTextView.setText(timeSavedPair.second);
}

private void showPopupMenu(Context context, View view, final int position) {
PopupMenu popup = new PopupMenu(context, view);
popup.getMenuInflater().inflate(R.menu.ntp_widget_menu, popup.getMenu());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.chromium.chrome.R;
import org.chromium.chrome.browser.BraveRewardsHelper;
import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.ntp.widget.NTPWidgetManager;
import org.chromium.chrome.browser.ntp_background_images.util.NTPUtil;
import org.chromium.chrome.browser.ntp_background_images.util.SponsoredImageUtil;
import org.chromium.chrome.browser.preferences.BravePref;
Expand All @@ -30,6 +31,10 @@ public BraveTileView(Context context, AttributeSet attrs) {
public void setTitle(String title, int titleLines) {
super.setTitle(title, titleLines);
TextView mTitleView = findViewById(R.id.tile_view_title);
mTitleView.setTextColor(getResources().getColor(android.R.color.black));
if (NTPWidgetManager.getInstance().getUsedWidgets().size() > 0
|| UserPrefs.get(Profile.getLastUsedRegularProfile())
.getBoolean(BravePref.NEW_TAB_PAGE_SHOW_BACKGROUND_IMAGE)) {
mTitleView.setTextColor(getResources().getColor(android.R.color.black));
}
}
}
1 change: 1 addition & 0 deletions android/java/res/layout/brave_stats_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
android:orientation="vertical">

<LinearLayout
android:id="@+id/brave_stats_title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="@color/topsite_bg_color"
android:layout_gravity="center_horizontal" />

0 comments on commit df10a4f

Please sign in to comment.