Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android P3A #7016

Merged
merged 10 commits into from
Nov 16, 2020
2 changes: 2 additions & 0 deletions android/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import("//build/config/android/rules.gni")
import("//brave/components/brave_sync/buildflags/buildflags.gni")
import("//brave/components/p3a/buildflags.gni")

declare_args() {
brave_android_developer_options_code = ""
Expand All @@ -17,5 +18,6 @@ java_cpp_template("brave_config_java") {
defines = [
"BRAVE_ANDROID_DEVELOPER_OPTIONS_CODE=\"$brave_android_developer_options_code\"",
"BRAVE_ANDROID_SYNC_ENABLED=$enable_brave_sync",
"BRAVE_ANDROID_P3A_ENABLED=$brave_p3a_enabled",
]
}
1 change: 1 addition & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/help/BraveHelpAndFeedback.java",
"../../brave/android/java/org/chromium/chrome/browser/homepage/BraveHomepageManager.java",
"../../brave/android/java/org/chromium/chrome/browser/homepage/settings/BraveHomepageSettings.java",
"../../brave/android/java/org/chromium/chrome/browser/informers/BraveP3AInformers.java",
"../../brave/android/java/org/chromium/chrome/browser/language/settings/BraveLanguageSettings.java",
"../../brave/android/java/org/chromium/chrome/browser/local_database/BraveStatsTable.java",
"../../brave/android/java/org/chromium/chrome/browser/local_database/DatabaseHelper.java",
Expand Down
37 changes: 28 additions & 9 deletions android/java/org/chromium/chrome/browser/app/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,39 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import org.chromium.base.Log;
import org.chromium.base.ApplicationStatus;
import org.chromium.base.CommandLine;
import org.chromium.base.ContextUtils;
import org.chromium.base.IntentUtils;
import org.chromium.base.Log;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.task.PostTask;
import org.chromium.base.task.TaskTraits;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ApplicationLifetime;
import org.chromium.chrome.browser.BraveConfig;
import org.chromium.chrome.browser.BraveHelper;
import org.chromium.chrome.browser.BraveSyncReflectionUtils;
import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.CrossPromotionalModalDialogFragment;
import org.chromium.chrome.browser.LaunchIntentDispatcher;
import org.chromium.chrome.browser.bookmarks.BookmarkBridge;
import org.chromium.chrome.browser.bookmarks.BookmarkModel;
import org.chromium.chrome.browser.brave_stats.BraveStatsUtil;
import org.chromium.chrome.browser.dependency_injection.ChromeActivityComponent;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.informers.BraveP3AInformers;
import org.chromium.chrome.browser.notifications.BraveSetDefaultBrowserNotificationService;
import org.chromium.chrome.browser.notifications.retention.RetentionNotificationUtil;
import org.chromium.chrome.browser.ntp.NewTabPage;
import org.chromium.chrome.browser.onboarding.OnboardingActivity;
import org.chromium.chrome.browser.onboarding.OnboardingPrefManager;
import org.chromium.chrome.browser.onboarding.v2.HighlightDialogFragment;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.chrome.browser.preferences.BravePreferenceKeys;
import org.chromium.chrome.browser.preferences.Pref;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
Expand All @@ -64,19 +73,12 @@
import org.chromium.chrome.browser.toolbar.top.BraveToolbarLayout;
import org.chromium.chrome.browser.util.BraveDbUtil;
import org.chromium.chrome.browser.util.BraveReferrer;
import org.chromium.chrome.browser.util.PackageUtils;
import org.chromium.components.bookmarks.BookmarkId;
import org.chromium.components.bookmarks.BookmarkType;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.components.user_prefs.UserPrefs;
import org.chromium.ui.widget.Toast;
import org.chromium.chrome.browser.util.PackageUtils;
import org.chromium.chrome.browser.onboarding.OnboardingPrefManager;
import org.chromium.chrome.browser.onboarding.OnboardingActivity;
import org.chromium.chrome.browser.CrossPromotionalModalDialogFragment;
import org.chromium.chrome.browser.onboarding.v2.HighlightDialogFragment;
import org.chromium.chrome.browser.notifications.retention.RetentionNotificationUtil;
import org.chromium.chrome.browser.brave_stats.BraveStatsUtil;
import org.chromium.chrome.browser.ntp.NewTabPage;

import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -266,6 +268,23 @@ public void finishNativeInitialization() {
}
BraveSyncReflectionUtils.showInformers();

if (BraveConfig.P3A_ENABLED) {
// // P3A informer should be shown not for the updated users only
// if (!PackageUtils.isFirstInstall(this)) {
// BraveP3AInformers.show();
// } else {
// // On the first run P3A is must be set from the onboarding wizard
// }
// Uncomment ^ when onboarding UI step will be ready for P3A

// Until we don't have P3A onboarding, P3A will be disabled,
// but available for activating through settings
if (!BravePrefServiceBridge.getInstance().hasPathP3AEnabled()) {
BravePrefServiceBridge.getInstance().setP3AEnabled(false);
}
// Remove lines above ^ when onboarding UI step will be ready for P3A
}

if (!OnboardingPrefManager.getInstance().isOneTimeNotificationStarted()
&& PackageUtils.isFirstInstall(this)) {
RetentionNotificationUtil.scheduleNotification(this, RetentionNotificationUtil.HOUR_3);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* Copyright (c) 2020 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.informers;

import org.chromium.base.ApplicationStatus;
import org.chromium.base.ContextUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.BraveConfig;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.infobar.InfoBarIdentifier;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.chrome.browser.settings.BraveSyncScreensPreference;
import org.chromium.chrome.browser.settings.SettingsLauncher;
import org.chromium.chrome.browser.settings.SettingsLauncherImpl;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.ui.messages.infobar.BraveSimpleConfirmInfoBarBuilder;
import org.chromium.chrome.browser.ui.messages.infobar.SimpleConfirmInfoBarBuilder;
import org.chromium.chrome.browser.util.TabUtils;

public class BraveP3AInformers {
public static final String P3A_LEARN_MORE_URL = "https://brave.com/P3A";
public static void show() {
showIfRequired();
}

private static void showIfRequired() {
if (!BraveConfig.P3A_ENABLED) {
return;
}

if (BravePrefServiceBridge.getInstance().getP3ANoticeAcknowledged()) {
return;
}

showP3AInformer();
}

private static void showP3AInformer() {
BraveActivity activity = BraveActivity.getBraveActivity();
if (activity == null) return;

Tab tab = activity.getActivityTabProvider().get();
if (tab == null) return;

BraveSimpleConfirmInfoBarBuilder.createInfobarWithDrawable(tab.getWebContents(),
new SimpleConfirmInfoBarBuilder.Listener() {
@Override
public void onInfoBarDismissed() {
// Pressing cross
BravePrefServiceBridge.getInstance().setP3AEnabled(true);
BravePrefServiceBridge.getInstance().setP3ANoticeAcknowledged(true);
}

@Override
public boolean onInfoBarButtonClicked(boolean isPrimary) {
if (isPrimary) {
// Pressing `Got it`
BravePrefServiceBridge.getInstance().setP3AEnabled(true);
} else {
// Pressing `Disable`
BravePrefServiceBridge.getInstance().setP3AEnabled(false);
}
BravePrefServiceBridge.getInstance().setP3ANoticeAcknowledged(true);

return false;
}

@Override
public boolean onInfoBarLinkClicked() {
// Pressing learn more
TabUtils.openUrlInNewTab(false /* isIncognito */, P3A_LEARN_MORE_URL);
return false;
}
},
// must be BRAVE_CONFIRM_P3A_INFOBAR_DELEGATE, but now it is introduced through
// src/brave/chromium_src/components/infobars/core/infobar_delegate.h and
// java enums are generated by //components/infobars/core:infobar_generated_enums
// who does not understand `brave/chromium_src`
InfoBarIdentifier.INLINE_UPDATE_READY_INFOBAR_ANDROID, activity,
R.drawable.btn_info /* drawableId */,
activity.getString(R.string.brave_confirm_p3a_infobar) /* message */,
activity.getString(R.string.brave_confirm_p3a_infobar_acknowledge) /* primaryText */,
activity.getString(R.string.brave_confirm_p3a_infobar_disable) /* secondaryText */,
activity.getString(R.string.brave_confirm_p3a_infobar_learn_more) /* linkText */,
false /* autoExpire */);
BravePrefServiceBridge.getInstance().setP3ANoticeAcknowledged(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@ public void setReferralDownloadId(String downloadId) {
BravePrefServiceBridgeJni.get().setReferralDownloadId(downloadId);
}

public void setP3AEnabled(boolean value) {
BravePrefServiceBridgeJni.get().setP3AEnabled(value);
}

public boolean getP3AEnabled() {
AlexeyBarabash marked this conversation as resolved.
Show resolved Hide resolved
return BravePrefServiceBridgeJni.get().getP3AEnabled();
}

public boolean hasPathP3AEnabled() {
return BravePrefServiceBridgeJni.get().hasPathP3AEnabled();
}

public void setP3ANoticeAcknowledged(boolean value) {
BravePrefServiceBridgeJni.get().setP3ANoticeAcknowledged(value);
}

public boolean getP3ANoticeAcknowledged() {
return BravePrefServiceBridgeJni.get().getP3ANoticeAcknowledged();
}

@NativeMethods
interface Natives {
void setHTTPSEEnabled(boolean enabled);
Expand Down Expand Up @@ -215,5 +235,11 @@ interface Natives {
void setReferralInitialization(boolean value);
void setReferralPromoCode(String promoCode);
void setReferralDownloadId(String downloadId);

void setP3AEnabled(boolean value);
boolean getP3AEnabled();
boolean hasPathP3AEnabled();
void setP3ANoticeAcknowledged(boolean value);
boolean getP3ANoticeAcknowledged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.chromium.base.ContextUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.BraveConfig;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.chrome.browser.preferences.Pref;
Expand All @@ -30,6 +31,7 @@ public class BravePrivacySettings extends PrivacySettings {
private static final String PREF_AD_BLOCK = "ad_block";
private static final String PREF_FINGERPRINTING_PROTECTION = "fingerprinting_protection";
private static final String PREF_CLOSE_TABS_ON_EXIT = "close_tabs_on_exit";
private static final String PREF_SEND_P3A = "send_p3a_analytics";
private static final String PREF_SYNC_AND_SERVICES_LINK = "sync_and_services_link";
private static final String PREF_SEARCH_SUGGESTIONS = "search_suggestions";
private static final String PREF_AUTOCOMPLETE_TOP_SITES = "autocomplete_top_sites";
Expand All @@ -51,6 +53,7 @@ public class BravePrivacySettings extends PrivacySettings {
private ChromeBaseCheckBoxPreference mAdBlockPref;
private ChromeBaseCheckBoxPreference mFingerprintingProtectionPref;
private ChromeBaseCheckBoxPreference mCloseTabsOnExitPref;
private ChromeBaseCheckBoxPreference mSendP3A;
private PreferenceCategory mSocialBlockingCategory;
private ChromeSwitchPreference mSocialBlockingGoogle;
private ChromeSwitchPreference mSocialBlockingFacebook;
Expand All @@ -77,6 +80,10 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
(ChromeBaseCheckBoxPreference) findPreference(PREF_CLOSE_TABS_ON_EXIT);
mCloseTabsOnExitPref.setOnPreferenceChangeListener(this);

mSendP3A =
(ChromeBaseCheckBoxPreference) findPreference(PREF_SEND_P3A);
mSendP3A.setOnPreferenceChangeListener(this);

mSearchSuggestions = (ChromeSwitchPreference) findPreference(PREF_SEARCH_SUGGESTIONS);
mSearchSuggestions.setOnPreferenceChangeListener(this);
mSearchSuggestions.setManagedPreferenceDelegate(mManagedPreferenceDelegate);
Expand Down Expand Up @@ -122,6 +129,8 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
ContextUtils.getAppSharedPreferences().edit();
sharedPreferencesEditor.putBoolean(PREF_CLOSE_TABS_ON_EXIT, (boolean) newValue);
sharedPreferencesEditor.apply();
} else if (PREF_SEND_P3A.equals(key)) {
BravePrefServiceBridge.getInstance().setP3AEnabled((boolean) newValue);
} else if (PREF_SEARCH_SUGGESTIONS.equals(key)) {
mPrefServiceBridge.setBoolean(Pref.SEARCH_SUGGEST_ENABLED, (boolean) newValue);
} else if (PREF_AUTOCOMPLETE_TOP_SITES.equals(key)) {
Expand Down Expand Up @@ -157,6 +166,12 @@ private void updatePreferences() {
mSearchSuggestions.setChecked(mPrefServiceBridge.getBoolean(Pref.SEARCH_SUGGEST_ENABLED));
int order = findPreference(PREF_CLEAR_BROWSING_DATA).getOrder();
mCloseTabsOnExitPref.setOrder(++order);
if (BraveConfig.P3A_ENABLED) {
mSendP3A.setOrder(++order);
mSendP3A.setChecked(BravePrefServiceBridge.getInstance().getP3AEnabled());
} else {
getPreferenceScreen().removePreference(mSendP3A);
}
mHttpsePref.setOrder(++order);
mAdBlockPref.setOrder(++order);
mFingerprintingProtectionPref.setOrder(++order);
Expand Down
7 changes: 6 additions & 1 deletion android/java/res/xml/brave_privacy_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
android:title="@string/close_tabs_on_exit_title"
android:summary="@string/close_tabs_on_exit_summary"
android:defaultValue="false" />
<org.chromium.components.browser_ui.settings.ChromeBaseCheckBoxPreference
android:key="send_p3a_analytics"
android:title="@string/send_p3a_analytics_title"
android:summary="@string/send_p3a_analytics_summary"
android:defaultValue="false" />
<org.chromium.components.browser_ui.settings.ChromeBaseCheckBoxPreference
android:key="httpse"
android:title="@string/httpse_title"
Expand Down Expand Up @@ -42,7 +47,7 @@
<PreferenceCategory
android:key="brave_shields_social_blocking"
android:title="@string/brave_shields_social_blocking_title">

<org.chromium.components.browser_ui.settings.ChromeSwitchPreference
android:key="social_blocking_google"
android:title="@string/brave_shields_social_blocking_google"
Expand Down
1 change: 1 addition & 0 deletions browser/android/preferences/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ source_set("preferences") {
"//brave/components/brave_rewards/common",
"//brave/components/brave_shields/browser",
"//brave/components/brave_sync",
"//brave/components/p3a",
"//components/content_settings/core/browser",
"//components/prefs",
"//content/public/browser",
Expand Down
55 changes: 55 additions & 0 deletions browser/android/preferences/brave_pref_service_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "brave/components/brave_rewards/common/pref_names.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_sync/brave_sync_prefs.h"
#include "brave/components/p3a/buildflags.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
Expand All @@ -25,6 +26,10 @@
#include "brave/components/brave_perf_predictor/common/pref_names.h"
#endif

#if BUILDFLAG(BRAVE_P3A_ENABLED)
#include "brave/components/p3a/pref_names.h"
#endif

using base::android::ConvertUTF8ToJavaString;
using base::android::JavaParamRef;
using base::android::ScopedJavaLocalRef;
Expand Down Expand Up @@ -279,5 +284,55 @@ void JNI_BravePrefServiceBridge_SetReferralDownloadId(
kReferralDownloadID, ConvertJavaStringToUTF8(env, downloadId));
}

#if BUILDFLAG(BRAVE_P3A_ENABLED)
void JNI_BravePrefServiceBridge_SetP3AEnabled(
JNIEnv* env,
jboolean value) {
return g_browser_process->local_state()->SetBoolean(
brave::kP3AEnabled, value);
}

jboolean JNI_BravePrefServiceBridge_GetP3AEnabled(
JNIEnv* env) {
return g_browser_process->local_state()->GetBoolean(
brave::kP3AEnabled);
}

jboolean JNI_BravePrefServiceBridge_HasPathP3AEnabled(
JNIEnv* env) {
return g_browser_process->local_state()->HasPrefPath(brave::kP3AEnabled);
}

void JNI_BravePrefServiceBridge_SetP3ANoticeAcknowledged(
JNIEnv* env,
jboolean value) {
return g_browser_process->local_state()->SetBoolean(
brave::kP3ANoticeAcknowledged, value);
}

jboolean JNI_BravePrefServiceBridge_GetP3ANoticeAcknowledged(
JNIEnv* env) {
return g_browser_process->local_state()->GetBoolean(
brave::kP3ANoticeAcknowledged);
}

#else

void JNI_BravePrefServiceBridge_SetP3AEnabled(JNIEnv* env, jboolean value) {}

jboolean JNI_BravePrefServiceBridge_GetP3AEnabled(JNIEnv* env) {
return false;
}

jboolean JNI_BravePrefServiceBridge_HasPathP3AEnabled(JNIEnv* env) {}

void JNI_BravePrefServiceBridge_SetP3ANoticeAcknowledged(JNIEnv* env,
jboolean value) {}

jboolean JNI_BravePrefServiceBridge_GetP3ANoticeAcknowledged(JNIEnv* env) {
return false;
}
#endif // BUILDFLAG(BRAVE_P3A_ENABLED)

} // namespace android
} // namespace chrome
Loading