diff --git a/android/brave_java_sources.gni b/android/brave_java_sources.gni index bce45c9cb3b7..c1b568e8b645 100644 --- a/android/brave_java_sources.gni +++ b/android/brave_java_sources.gni @@ -12,11 +12,13 @@ brave_java_sources = [ "../../brave/android/java/org/chromium/chrome/browser/document/BraveLauncherActivity.java", "../../brave/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageLayout.java", "../../brave/android/java/org/chromium/chrome/browser/ntp/BraveNewTabPageView.java", + "../../brave/android/java/org/chromium/chrome/browser/preferences/BraveMainPreferencesBase.java", "../../brave/android/java/org/chromium/chrome/browser/preferences/BravePreferenceManager.java", "../../brave/android/java/org/chromium/chrome/browser/preferences/BravePrefServiceBridge.java", "../../brave/android/java/org/chromium/chrome/browser/preferences/privacy/BravePrivacyPreferences.java", "../../brave/android/java/org/chromium/chrome/browser/preferences/website/BraveShieldsContentSettings.java", "../../brave/android/java/org/chromium/chrome/browser/preferences/website/BraveShieldsContentSettingsObserver.java", + "../../brave/android/java/org/chromium/chrome/browser/signin/BraveSigninManager.java", "../../brave/android/java/org/chromium/chrome/browser/toolbar/bottom/BookmarksButton.java", "../../brave/android/java/org/chromium/chrome/browser/toolbar/top/BraveToolbarLayout.java", ] diff --git a/android/java/org/chromium/chrome/browser/preferences/BraveMainPreferencesBase.java b/android/java/org/chromium/chrome/browser/preferences/BraveMainPreferencesBase.java new file mode 100644 index 000000000000..7c674e649168 --- /dev/null +++ b/android/java/org/chromium/chrome/browser/preferences/BraveMainPreferencesBase.java @@ -0,0 +1,66 @@ +/* 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.preferences; + +import android.os.Bundle; +import android.os.Handler; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceFragmentCompat; + +import java.util.HashMap; + +// This exculdes some settings in main settings screen. +public class BraveMainPreferencesBase extends PreferenceFragmentCompat { + // Below prefs are removed from main settings. + private static final String PREF_ACCOUNT_SECTION = "account_section"; + private static final String PREF_SIGN_IN = "sign_in"; + private static final String PREF_DATA_REDUCTION = "data_reduction"; + private static final String PREF_AUTOFILL_ASSISTANT = "autofill_assistant"; + private static final String PREF_SYNC_AND_SERVICES = "sync_and_services"; + private static final String PREF_DEVELOPER = "developer"; + + private final HashMap mRemovedPreferences = new HashMap<>(); + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {} + + @Override + public void onResume() { + super.onResume(); + // Run updateBravePreferences() after fininshing MainPreferences::updatePreferences(). + // Otherwise, some prefs could be added after finishing updateBravePreferences(). + new Handler().post(() -> updateBravePreferences()); + } + + private void updateBravePreferences() { + removePreferenceIfPresent(PREF_SIGN_IN); + removePreferenceIfPresent(PREF_ACCOUNT_SECTION); + removePreferenceIfPresent(PREF_DATA_REDUCTION); + removePreferenceIfPresent(PREF_AUTOFILL_ASSISTANT); + removePreferenceIfPresent(PREF_SYNC_AND_SERVICES); + removePreferenceIfPresent(PREF_DEVELOPER); + } + + /** + * We need to override it to avoid NullPointerException in Chromium's child classes + */ + @Override + public Preference findPreference(CharSequence key) { + Preference result = super.findPreference(key); + if (result == null) { + result = mRemovedPreferences.get(key); + } + return result; + } + + private void removePreferenceIfPresent(String key) { + Preference preference = getPreferenceScreen().findPreference(key); + if (preference != null) { + getPreferenceScreen().removePreference(preference); + mRemovedPreferences.put(preference.getKey(), preference); + } + } +} diff --git a/android/java/org/chromium/chrome/browser/signin/BraveSigninManager.java b/android/java/org/chromium/chrome/browser/signin/BraveSigninManager.java new file mode 100644 index 000000000000..3e291819b94f --- /dev/null +++ b/android/java/org/chromium/chrome/browser/signin/BraveSigninManager.java @@ -0,0 +1,39 @@ +/* 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.signin; + +import android.content.Context; + +import org.chromium.base.ContextUtils; +import org.chromium.base.annotations.CalledByNative; +import org.chromium.components.signin.AccountTrackerService; + +public class BraveSigninManager extends SigninManager { + BraveSigninManager(Context context, long nativeSigninManagerAndroid, SigninManagerDelegate delegate, + AccountTrackerService accountTrackerService) { + super(context, nativeSigninManagerAndroid, delegate, accountTrackerService); + } + + @Override + public boolean isSignInAllowed() { + return false; + } + + @Override + public boolean isSigninSupported() { + return false; + } + + @CalledByNative + private static SigninManager create(long nativeSigninManagerAndroid, + SigninManagerDelegate delegate, AccountTrackerService accountTrackerService) { + assert nativeSigninManagerAndroid != 0; + assert delegate != null; + assert accountTrackerService != null; + return new BraveSigninManager(ContextUtils.getApplicationContext(), nativeSigninManagerAndroid, + delegate, accountTrackerService); + } +} diff --git a/build/android/BUILD.gn b/build/android/BUILD.gn index 2be64c352150..8203c2dbed0c 100755 --- a/build/android/BUILD.gn +++ b/build/android/BUILD.gn @@ -96,5 +96,6 @@ generate_jni("jni_headers") { "//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", + "//brave/android/java/org/chromium/chrome/browser/signin/BraveSigninManager.java", ] } diff --git a/chromium_src/chrome/browser/android/signin/signin_manager_android.cc b/chromium_src/chrome/browser/android/signin/signin_manager_android.cc new file mode 100644 index 000000000000..89cfb9e50052 --- /dev/null +++ b/chromium_src/chrome/browser/android/signin/signin_manager_android.cc @@ -0,0 +1,23 @@ +/* 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/BraveSigninManager_jni.h" +#include "chrome/browser/android/signin/signin_manager_android.h" +#include "chrome/android/chrome_jni_headers/SigninManager_jni.h" + +namespace { +// For preventing "unused Java_SigninManager_create method" compile error. +class UnusedClass { + private: + void test() { + Java_SigninManager_create(nullptr, 0ll, nullptr, nullptr); + } +}; +} // namespace + +#define Java_SigninManager_create Java_BraveSigninManager_create +#include "../../../../../../chrome/browser/android/signin/signin_manager_android.cc" // NOLINT +#undef Java_SigninManager_create + diff --git a/patches/chrome-android-java-src-org-chromium-chrome-browser-preferences-MainPreferences.java.patch b/patches/chrome-android-java-src-org-chromium-chrome-browser-preferences-MainPreferences.java.patch new file mode 100644 index 000000000000..0614d8073a92 --- /dev/null +++ b/patches/chrome-android-java-src-org-chromium-chrome-browser-preferences-MainPreferences.java.patch @@ -0,0 +1,13 @@ +diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/MainPreferences.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/MainPreferences.java +index 56f952c159ffb94c0b51a47d6949e8bc3f99871c..ea90e440c35ae95c6579d27d22feeefd421a376f 100644 +--- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/MainPreferences.java ++++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/MainPreferences.java +@@ -41,7 +41,7 @@ import java.util.Map; + /** + * The main settings screen, shown when the user first opens Settings. + */ +-public class MainPreferences extends PreferenceFragmentCompat ++public class MainPreferences extends BraveMainPreferencesBase + implements TemplateUrlService.LoadListener, ProfileSyncService.SyncStateChangedListener, + SigninManager.SignInStateObserver { + public static final String PREF_ACCOUNT_SECTION = "account_section";