Skip to content

Commit

Permalink
Merge pull request #3459 from brave/show_real_brave_stats_on_android_ntp
Browse files Browse the repository at this point in the history
Show real brave stats on brave NTP
  • Loading branch information
simonhong authored Sep 18, 2019
2 parents 4717f25 + 1a8bfb9 commit 66639ff
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
package org.chromium.chrome.browser.ntp;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.chromium.base.ContextUtils;
import org.chromium.base.TraceEvent;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ntp.NewTabPageView;
import org.chromium.chrome.browser.profiles.Profile;

@JNINamespace("chrome::android")
public class BraveNewTabPageView extends NewTabPageView {
private static final String TAG = "BraveNewTabPageView";

Expand All @@ -28,10 +30,11 @@ public class BraveNewTabPageView extends NewTabPageView {
private TextView mAdsBlockedCountTextView;
private TextView mHttpsUpgradesCountTextView;
private TextView mEstTimeSavedTextView;
private SharedPreferences mSharedPreferences;
private Profile mProfile;

public BraveNewTabPageView(Context context, AttributeSet attrs) {
super(context, attrs);
mProfile = Profile.getLastUsedProfile();
}

@Override
Expand All @@ -42,7 +45,6 @@ protected void onFinishInflate() {
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);
mSharedPreferences = ContextUtils.getAppSharedPreferences();
}

@Override
Expand All @@ -58,10 +60,9 @@ protected void onWindowVisibilityChanged(int visibility) {
*/
private void updateBraveStats() {
TraceEvent.begin(TAG + ".updateBraveStats()");

long trackersBlockedCount = mSharedPreferences.getLong(PREF_TRACKERS_BLOCKED_COUNT, 0);
long adsBlockedCount = mSharedPreferences.getLong(PREF_ADS_BLOCKED_COUNT, 0);
long httpsUpgradesCount = mSharedPreferences.getLong(PREF_HTTPS_UPGRADES_COUNT, 0);
long trackersBlockedCount = BraveNewTabPageViewJni.get().getTrackersBlockedCount(mProfile);
long adsBlockedCount = BraveNewTabPageViewJni.get().getAdsBlockedCount(mProfile);
long httpsUpgradesCount = BraveNewTabPageViewJni.get().getHttpsUpgradesCount(mProfile);
long estimatedMillisecondsSaved = (trackersBlockedCount + adsBlockedCount) * MILLISECONDS_PER_ITEM;

mAdsBlockedCountTextView.setText(getBraveStatsStringFormNumber(adsBlockedCount));
Expand Down Expand Up @@ -128,4 +129,11 @@ else if (seconds > 60) {
}
return result;
}

@NativeMethods
interface Natives {
int getTrackersBlockedCount(Profile profile);
int getAdsBlockedCount(Profile profile);
int getHttpsUpgradesCount(Profile profile);
}
}
2 changes: 2 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ source_set("browser_process") {
} else {
sources += [
"android/brave_shields_content_settings.cc",
"android/brave_shields_content_settings.h",
"android/brave_stats_updater_android.cc",
"android/ntp/brave_new_tab_page_view_android.cc",
"android/preferences/brave_pref_service_bridge.cc"
]

Expand Down
38 changes: 38 additions & 0 deletions browser/android/ntp/brave_new_tab_page_view_android.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* 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/. */

#include "brave/build/android/jni_headers/BraveNewTabPageView_jni.h"

#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
#include "components/prefs/pref_service.h"

namespace chrome {
namespace android {

int JNI_BraveNewTabPageView_GetTrackersBlockedCount(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_profile) {
Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
return profile->GetPrefs()->GetUint64(kTrackersBlocked);
}

int JNI_BraveNewTabPageView_GetAdsBlockedCount(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_profile) {
Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
return profile->GetPrefs()->GetUint64(kAdsBlocked);
}

int JNI_BraveNewTabPageView_GetHttpsUpgradesCount(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_profile) {
Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
return profile->GetPrefs()->GetUint64(kHttpsUpgrades);
}

} // namespace android
} // namespace chrome
1 change: 1 addition & 0 deletions build/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ java_strings_grd("android_brave_strings_grd") {
generate_jni("jni_headers") {
sources = [
"//brave/android/java/org/chromium/chrome/browser/BraveActivity.java",
"//brave/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageView.java",
"//brave/android/java/org/chromium/chrome/browser/preferences/BravePrefServiceBridge.java",
"//brave/android/java/org/chromium/chrome/browser/preferences/website/BraveShieldsContentSettings.java",
]
Expand Down

0 comments on commit 66639ff

Please sign in to comment.