Add stub brave stats ui on NTP
Brave stats UI is displayed above the favorite/popular sites tile section. Most of codes are copied from existing brave android browser.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.chromium.chrome.browser.ntp;
|
||||
|
||||
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;
|
||||
|
||||
public class BraveNewTabPageLayout extends NewTabPageLayout {
|
||||
private ViewGroup mBraveStatsLayout;
|
||||
|
||||
public BraveNewTabPageLayout(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSearchProviderInfo(boolean hasLogo, boolean isGoogle) {
|
||||
super.setSearchProviderInfo(hasLogo, isGoogle);
|
||||
// Make brave stats visibile always on NTP.
|
||||
// NewTabPageLayout::setSearchProviderInfo() makes it invisible.
|
||||
// So, explicitly set it as visible.
|
||||
mBraveStatsLayout.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onFinishInflate() {
|
||||
super.onFinishInflate();
|
||||
insertBraveStatsLayout();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.chromium.chrome.browser.ntp;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.chromium.base.TraceEvent;
|
||||
import org.chromium.chrome.R;
|
||||
import org.chromium.chrome.browser.ntp.NewTabPageView;
|
||||
|
||||
public class BraveNewTabPageView extends NewTabPageView {
|
||||
private static final String TAG = "BraveNewTabPageView";
|
||||
|
||||
TextView mAdsBlockedCountTextView;
|
||||
TextView mHttpsUpgradesCountTextView;
|
||||
TextView mEstTimeSavedTextView;
|
||||
|
||||
public BraveNewTabPageView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onFinishInflate() {
|
||||
super.onFinishInflate();
|
||||
|
||||
ViewGroup braveStatsView = (ViewGroup) getNewTabPageLayout().findViewById(R.id.brave_stats);
|
||||
mAdsBlockedCountTextView = (TextView) braveStatsView.findViewById(R.id.brave_stats_text_ads_count);
|
||||
mHttpsUpgradesCountTextView = (TextView) braveStatsView.findViewById(R.id.brave_stats_text_https_count);
|
||||
mEstTimeSavedTextView = (TextView) braveStatsView.findViewById(R.id.brave_stats_text_time_count);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onWindowVisibilityChanged(int visibility) {
|
||||
super.onWindowVisibilityChanged(visibility);
|
||||
if (visibility == VISIBLE) {
|
||||
updateBraveStats();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up Brave stats.
|
||||
*/
|
||||
private void updateBraveStats() {
|
||||
TraceEvent.begin(TAG + ".updateBraveStats()");
|
||||
long adsBlockedCount = 0;
|
||||
long httpsUpgradesCount = 0;
|
||||
long estimatedMillisecondsSaved = 0;
|
||||
|
||||
mAdsBlockedCountTextView.setText(getBraveStatsStringFormNumber(adsBlockedCount));
|
||||
mHttpsUpgradesCountTextView.setText(getBraveStatsStringFormNumber(httpsUpgradesCount));
|
||||
mEstTimeSavedTextView.setText(getBraveStatsStringFromTime(estimatedMillisecondsSaved));
|
||||
TraceEvent.end(TAG + ".updateBraveStats()");
|
||||
}
|
||||
|
||||
/*
|
||||
* Gets string view of specific number for Brave stats
|
||||
*/
|
||||
private String getBraveStatsStringFormNumber(long number) {
|
||||
String result = "";
|
||||
String suffix = "";
|
||||
if (number >= 1000 * 1000 * 1000) {
|
||||
result = result + (number / (1000 * 1000 * 1000));
|
||||
number = number % (1000 * 1000 * 1000);
|
||||
result = result + "." + (number / (10 * 1000 * 1000));
|
||||
suffix = "B";
|
||||
}
|
||||
else if (number >= (10 * 1000 * 1000) && number < (1000 * 1000 * 1000)) {
|
||||
result = result + (number / (1000 * 1000));
|
||||
suffix = "M";
|
||||
}
|
||||
else if (number >= (1000 * 1000) && number < (10 * 1000 * 1000)) {
|
||||
result = result + (number / (1000 * 1000));
|
||||
number = number % (1000 * 1000);
|
||||
result = result + "." + (number / (100 * 1000));
|
||||
suffix = "M";
|
||||
}
|
||||
else if (number >= (10 * 1000) && number < (1000 * 1000)) {
|
||||
result = result + (number / 1000);
|
||||
suffix = "K";
|
||||
}
|
||||
else if (number >= 1000 && number < (10* 1000)) {
|
||||
result = result + (number / 1000);
|
||||
number = number % 1000;
|
||||
result = result + "." + (number / 100);
|
||||
suffix = "K";
|
||||
}
|
||||
else {
|
||||
result = result + number;
|
||||
}
|
||||
result = result + suffix;
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* Gets string view of specific time in seconds for Brave stats
|
||||
*/
|
||||
private String getBraveStatsStringFromTime(long seconds) {
|
||||
String result = "";
|
||||
if (seconds > 24 * 60 * 60) {
|
||||
result = result + (seconds / (24 * 60 * 60)) + "d";
|
||||
}
|
||||
else if (seconds > 60 * 60) {
|
||||
result = result + (seconds / (60 * 60)) + "h";
|
||||
}
|
||||
else if (seconds > 60) {
|
||||
result = result + (seconds / 60) + "m";
|
||||
}
|
||||
else {
|
||||
result = result + seconds + "s";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (c) 2019 The Brave Authors. All rights reserved.
|
||||
This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/brave_stats_layout"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<LinearLayout
|
||||
android:id="@+id/brave_stats"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_marginEnd="10dp"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:orientation="horizontal"
|
||||
android:baselineAligned="false" >
|
||||
<LinearLayout
|
||||
android:id="@+id/brave_stats_ads"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:baselineAligned="false"
|
||||
android:layout_weight="1" >
|
||||
<TextView
|
||||
android:id="@+id/brave_stats_text_ads_count"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:textColor="#FB542B"
|
||||
android:textStyle="bold"
|
||||
android:textSize="24sp" />
|
||||
<TextView
|
||||
android:id="@+id/brave_stats_text_ads"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:textSize="12sp"
|
||||
android:text="@string/brave_stats_text_ads" />
|
||||
</LinearLayout>
|
||||
<LinearLayout
|
||||
android:id="@+id/brave_stats_https"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:baselineAligned="false"
|
||||
android:layout_weight="1" >
|
||||
<TextView
|
||||
android:id="@+id/brave_stats_text_https_count"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:textColor="#22C976"
|
||||
android:textStyle="bold"
|
||||
android:textSize="24sp" />
|
||||
<TextView
|
||||
android:id="@+id/brave_stats_text_https"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:textSize="12sp"
|
||||
android:text="@string/brave_stats_text_https" />
|
||||
</LinearLayout>
|
||||
<LinearLayout
|
||||
android:id="@+id/brave_stats_time"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:baselineAligned="false"
|
||||
android:layout_weight="1" >
|
||||
<TextView
|
||||
android:id="@+id/brave_stats_text_time_count"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:textColor="#222326"
|
||||
android:textStyle="bold"
|
||||
android:textSize="24sp" />
|
||||
<TextView
|
||||
android:id="@+id/brave_stats_text_time"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:textSize="12sp"
|
||||
android:text="@string/brave_stats_text_time" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
<ImageView
|
||||
android:id="@+id/brave_stats_shadow"
|
||||
android:src="@drawable/toolbar_shadow"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="25dp"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_marginEnd="20dp"
|
||||
android:scaleType="fitXY"
|
||||
android:contentDescription="@null" />
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
Use of this source code is governed by a BSD-style license that can be
|
||||
found in the LICENSE file. -->
|
||||
|
||||
<org.chromium.chrome.browser.ntp.BraveNewTabPageView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingTop="@dimen/tab_strip_height" >
|
||||
|
||||
</org.chromium.chrome.browser.ntp.BraveNewTabPageView>
|
||||
@@ -123,6 +123,12 @@ This file contains all "about" strings. It is set to NOT be translated, in tran
|
||||
<message name="IDS_FINGERPRINTING_PROTECTION_SUMMARY" desc="Summary for fingerprinting protection.">
|
||||
Prevents browser from fingerprinting
|
||||
</message>
|
||||
<message name="IDS_BRAVE_STATS_TEXT_ADS" desc="Title for the number of blocked ads and trackers">Ads and Trackers
|
||||
Blocked</message>
|
||||
<message name="IDS_BRAVE_STATS_TEXT_HTTPS" desc="Title for the number of https upgrades">HTTPS
|
||||
Upgrades</message>
|
||||
<message name="IDS_BRAVE_STATS_TEXT_TIME" desc="Title for the estimated saved time">Est. Time
|
||||
Saved</message>
|
||||
</messages>
|
||||
</release>
|
||||
</grit>
|
||||
|
||||
Reference in New Issue
Block a user