Skip to content

Commit

Permalink
Merge pull request #16486 from brave/android_local_state
Browse files Browse the repository at this point in the history
Local state pref to use from Java
  • Loading branch information
AlexeyBarabash authored Jan 4, 2023
2 parents 2263fda + 429de76 commit 448d39f
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 291 deletions.
1 change: 1 addition & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/BraveHelper.java",
"../../brave/android/java/org/chromium/chrome/browser/BraveIntentHandler.java",
"../../brave/android/java/org/chromium/chrome/browser/BraveLaunchIntentDispatcher.java",
"../../brave/android/java/org/chromium/chrome/browser/BraveLocalState.java",
"../../brave/android/java/org/chromium/chrome/browser/BraveRelaunchUtils.java",
"../../brave/android/java/org/chromium/chrome/browser/BraveRewardsBalance.java",
"../../brave/android/java/org/chromium/chrome/browser/BraveRewardsDonationSentActivity.java",
Expand Down
35 changes: 35 additions & 0 deletions android/java/org/chromium/chrome/browser/BraveLocalState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright (c) 2022 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 https://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser;

import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.components.prefs.PrefService;

/**
* Helper for retrieving a {@link PrefService} from a browser local state.
*/
@JNINamespace("chrome::android")
public class BraveLocalState {
/** Returns the {@link PrefService} associated with the browser's local state */
public static PrefService get() {
return BraveLocalStateJni.get().getPrefService();
}

/** Makes local state data be written to the disk asap */
public static void commitPendingWrite() {
BraveLocalStateJni.get().commitPendingWrite();
}

@NativeMethods
public interface Natives {
// this method cannot be called 'get', because of the error:
// method get() is already defined in class BraveLocalStateJni
PrefService getPrefService();

void commitPendingWrite();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.chromium.brave_wallet.mojom.TxData1559;
import org.chromium.brave_wallet.mojom.TxDataUnion;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.BraveLocalState;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.app.domain.SendModel;
import org.chromium.chrome.browser.app.domain.WalletModel;
Expand All @@ -88,7 +89,7 @@
import org.chromium.chrome.browser.crypto_wallet.util.WalletUtils;
import org.chromium.chrome.browser.decentralized_dns.EnsOffchainResolveMethod;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.chrome.browser.qrreader.BarcodeTracker;
import org.chromium.chrome.browser.qrreader.BarcodeTrackerFactory;
import org.chromium.chrome.browser.qrreader.CameraSource;
Expand Down Expand Up @@ -1220,8 +1221,8 @@ private void onResolveWalletAddressDone(

Button mBtnEnableEnsOffchainLookup = findViewById(R.id.btn_enable_ens_offchain_lookup);
mBtnEnableEnsOffchainLookup.setOnClickListener(v -> {
BravePrefServiceBridge.getInstance().setENSOffchainResolveMethod(
EnsOffchainResolveMethod.ENABLED);
BraveLocalState.get().setInteger(
BravePref.ENS_OFFCHAIN_RESOLVE_METHOD, EnsOffchainResolveMethod.ENABLED);
mEnsOffchainLookupSection.setVisibility(View.GONE);
maybeResolveWalletAddress();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import org.chromium.brave_wallet.mojom.NetworkInfo;
import org.chromium.brave_wallet.mojom.ProviderError;
import org.chromium.chrome.browser.BraveConfig;
import org.chromium.chrome.browser.BraveLocalState;
import org.chromium.chrome.browser.crypto_wallet.activities.BraveWalletBaseActivity;
import org.chromium.chrome.browser.crypto_wallet.util.AssetUtils;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.mojo.bindings.Callbacks;

Expand Down Expand Up @@ -192,7 +194,7 @@ public static void getP3ABalances(BraveWalletBaseActivity activity, NetworkInfo[
&& jsonRpcService != null;

boolean P3AEnabled =
BraveConfig.P3A_ENABLED && BravePrefServiceBridge.getInstance().getP3AEnabled();
BraveConfig.P3A_ENABLED && BraveLocalState.get().getBoolean(BravePref.P3A_ENABLED);

HashMap<Integer, HashSet<String>> activeAddresses = new HashMap<Integer, HashSet<String>>();
for (int coinType : Utils.P3ACoinTypes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import androidx.preference.PreferenceFragmentCompat;

import org.chromium.chrome.R;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.chrome.browser.BraveLocalState;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.components.browser_ui.settings.SettingsUtils;

public class ENSSettingsFragment extends PreferenceFragmentCompat {
Expand All @@ -27,10 +28,10 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, String root
(RadioButtonGroupDDnsResolveMethodPreference) findPreference(
PREF_ENS_RESOLVE_METHOD);
radioButtonGroupDDnsResolveMethodPreference.initialize(
BravePrefServiceBridge.getInstance().getENSResolveMethod());
BraveLocalState.get().getInteger(BravePref.ENS_RESOLVE_METHOD));
radioButtonGroupDDnsResolveMethodPreference.setOnPreferenceChangeListener(
(preference, newValue) -> {
BravePrefServiceBridge.getInstance().setENSResolveMethod((int) newValue);
BraveLocalState.get().setInteger(BravePref.ENS_RESOLVE_METHOD, (int) newValue);
return true;
});

Expand All @@ -39,11 +40,11 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, String root
(RadioButtonGroupEnsOffchainResolveMethodPreference) findPreference(
PREF_ENS_OFFCHAIN_LOOKUP_METHOD);
radioButtonGroupEnsOffchainResolveMethodPreference.initialize(
BravePrefServiceBridge.getInstance().getENSOffchainResolveMethod());
BraveLocalState.get().getInteger(BravePref.ENS_OFFCHAIN_RESOLVE_METHOD));
radioButtonGroupEnsOffchainResolveMethodPreference.setOnPreferenceChangeListener(
(preference, newValue) -> {
BravePrefServiceBridge.getInstance().setENSOffchainResolveMethod(
(int) newValue);
BraveLocalState.get().setInteger(
BravePref.ENS_OFFCHAIN_RESOLVE_METHOD, (int) newValue);
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import androidx.preference.PreferenceFragmentCompat;

import org.chromium.chrome.R;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.chrome.browser.BraveLocalState;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.components.browser_ui.settings.SettingsUtils;

public class SnsSettingsFragment extends PreferenceFragmentCompat {
Expand All @@ -27,12 +28,12 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, String root
PREF_SNS_RESOLVE_METHOD);

radioButtonGroupDDnsResolveMethodPreference.initialize(
BravePrefServiceBridge.getInstance().getSnsResolveMethod());
BraveLocalState.get().getInteger(BravePref.SNS_RESOLVE_METHOD));

radioButtonGroupDDnsResolveMethodPreference.setOnPreferenceChangeListener(
(preference, newValue) -> {
int method = (int) newValue;
BravePrefServiceBridge.getInstance().setSnsResolveMethod(method);
BraveLocalState.get().setInteger(BravePref.SNS_RESOLVE_METHOD, method);
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import androidx.preference.PreferenceFragmentCompat;

import org.chromium.chrome.R;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.chrome.browser.BraveLocalState;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.components.browser_ui.settings.SettingsUtils;

public class UnstoppableDomainsSettingsFragment extends PreferenceFragmentCompat {
Expand All @@ -28,12 +29,13 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, String root
PREF_UNSTOPPABLE_DOMAINS_RESOLVE_METHOD);

radioButtonGroupDDnsResolveMethodPreference.initialize(
BravePrefServiceBridge.getInstance().getUnstoppableDomainsResolveMethod());
BraveLocalState.get().getInteger(BravePref.UNSTOPPABLE_DOMAINS_RESOLVE_METHOD));

radioButtonGroupDDnsResolveMethodPreference.setOnPreferenceChangeListener(
(preference, newValue) -> {
int method = (int) newValue;
BravePrefServiceBridge.getInstance().setUnstoppableDomainsResolveMethod(method);
BraveLocalState.get().setInteger(
BravePref.UNSTOPPABLE_DOMAINS_RESOLVE_METHOD, method);
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.BraveLocalState;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.metrics.UmaSessionStats;
import org.chromium.chrome.browser.onboarding.OnboardingPrefManager;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
Expand Down Expand Up @@ -233,7 +235,7 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
boolean isP3aEnabled = true;

try {
isP3aEnabled = BravePrefServiceBridge.getInstance().getP3AEnabled();
isP3aEnabled = BraveLocalState.get().getBoolean(BravePref.P3A_ENABLED);
} catch (Exception e) {
Log.e("P3aOnboarding", e.getMessage());
}
Expand All @@ -243,8 +245,9 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
try {
BravePrefServiceBridge.getInstance().setP3AEnabled(isChecked);
BravePrefServiceBridge.getInstance().setP3ANoticeAcknowledged(true);
BraveLocalState.get().setBoolean(BravePref.P3A_ENABLED, isChecked);
BraveLocalState.get().setBoolean(BravePref.P3A_NOTICE_ACKNOWLEDGED, true);
BraveLocalState.commitPendingWrite();
} catch (Exception e) {
Log.e("P3aOnboarding", e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

/**
* Please don't add anything in that file. We are going to refactor it soon.
* The only exceptions are preferences that are stored in a local_state
* g_browser_process->local_state().
* Check this PRs on how to handle preferences correctly:
* https://github.com/brave/brave-core/pull/16356
* https://github.com/brave/brave-core/pull/15905
* For the local_state based prefs please look on the PR:
* https://github.com/brave/brave-core/pull/16486
* Contact code owners if you have additional questions.
*/

Expand Down Expand Up @@ -142,10 +142,6 @@ public boolean getSafetynetCheckFailed() {
return BravePrefServiceBridgeJni.get().getSafetynetCheckFailed();
}

public void setSafetynetStatus(String status) {
BravePrefServiceBridgeJni.get().setSafetynetStatus(status);
}

public void resetPromotionLastFetchStamp() {
BravePrefServiceBridgeJni.get().resetPromotionLastFetchStamp();
}
Expand All @@ -166,86 +162,6 @@ public boolean GetBooleanForContentSetting(int content_type) {
return BravePrefServiceBridgeJni.get().getBooleanForContentSetting(content_type);
}

public void setReferralAndroidFirstRunTimestamp(long time) {
BravePrefServiceBridgeJni.get().setReferralAndroidFirstRunTimestamp(time);
}

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

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

public void setReferralPromoCode(String promoCode) {
BravePrefServiceBridgeJni.get().setReferralPromoCode(promoCode);
}

public void setReferralDownloadId(String downloadId) {
BravePrefServiceBridgeJni.get().setReferralDownloadId(downloadId);
}

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

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

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

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

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

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

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

public void setUnstoppableDomainsResolveMethod(int method) {
BravePrefServiceBridgeJni.get().setUnstoppableDomainsResolveMethod(method);
}

public int getUnstoppableDomainsResolveMethod() {
return BravePrefServiceBridgeJni.get().getUnstoppableDomainsResolveMethod();
}

public void setENSResolveMethod(int method) {
BravePrefServiceBridgeJni.get().setENSResolveMethod(method);
}

public int getENSResolveMethod() {
return BravePrefServiceBridgeJni.get().getENSResolveMethod();
}

public void setENSOffchainResolveMethod(int method) {
BravePrefServiceBridgeJni.get().setENSOffchainResolveMethod(method);
}

public int getENSOffchainResolveMethod() {
return BravePrefServiceBridgeJni.get().getENSOffchainResolveMethod();
}

public void setSnsResolveMethod(int method) {
BravePrefServiceBridgeJni.get().setSnsResolveMethod(method);
}

public int getSnsResolveMethod() {
return BravePrefServiceBridgeJni.get().getSnsResolveMethod();
}

public void setWebrtcPolicy(int policy) {
BravePrefServiceBridgeJni.get().setWebrtcPolicy(policy);
}
Expand Down Expand Up @@ -310,35 +226,9 @@ interface Natives {
void setSafetynetCheckFailed(boolean value);
boolean getSafetynetCheckFailed();

void setSafetynetStatus(String status);

void resetPromotionLastFetchStamp();
boolean getBooleanForContentSetting(int content_type);

void setReferralAndroidFirstRunTimestamp(long time);
void setReferralCheckedForPromoCodeFile(boolean value);
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();

void setStatsReportingEnabled(boolean value);
boolean getStatsReportingEnabled();

void setUnstoppableDomainsResolveMethod(int method);
int getUnstoppableDomainsResolveMethod();
void setENSResolveMethod(int method);
int getENSResolveMethod();
void setENSOffchainResolveMethod(int method);
int getENSOffchainResolveMethod();
void setSnsResolveMethod(int method);
int getSnsResolveMethod();

void setWebrtcPolicy(int policy);
int getWebrtcPolicy();

Expand Down
Loading

0 comments on commit 448d39f

Please sign in to comment.