(Wallet) Add new configuration options to set default wallet (#21340)

Adds new two configuration options to set default Ethereum wallet and default Solana wallet. 

We only support Brave Wallet and None on Android. Brave Wallet is set as a default option.
Setting a default wallet to None will disable the DApp detection.
This commit is contained in:
Simone Arpe
2023-12-14 12:08:24 +01:00
committed by GitHub
parent ad226f9456
commit 74668ee2b8
6 changed files with 196 additions and 57 deletions
@@ -415,7 +415,7 @@ public class BravePrivacySettings extends PrivacySettings implements ConnectionE
public void onDisplayPreferenceDialog(Preference preference) {
if (preference instanceof BraveDialogPreference) {
BravePreferenceDialogFragment dialogFragment =
BravePreferenceDialogFragment.newInstance((BraveDialogPreference) preference);
BravePreferenceDialogFragment.newInstance(preference);
dialogFragment.setTargetFragment(this, 0);
if (getFragmentManager() != null) {
dialogFragment.show(getFragmentManager(), BravePreferenceDialogFragment.TAG);
@@ -13,6 +13,7 @@ import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceDialogFragmentCompat;
@@ -37,7 +38,8 @@ public class BravePreferenceDialogFragment extends PreferenceDialogFragmentCompa
this.onPreferenceChangeListener = listener;
}
public static BravePreferenceDialogFragment newInstance(BraveDialogPreference preference) {
@NonNull
public static BravePreferenceDialogFragment newInstance(@NonNull Preference preference) {
BravePreferenceDialogFragment fragment = new BravePreferenceDialogFragment();
Bundle bundle = new Bundle(1);
bundle.putString(PreferenceDialogFragmentCompat.ARG_KEY, preference.getKey());
@@ -7,13 +7,16 @@ package org.chromium.chrome.browser.settings;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.SpannableString;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.recyclerview.widget.RecyclerView;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.brave_wallet.mojom.DefaultWallet;
import org.chromium.brave_wallet.mojom.KeyringService;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.BraveActivity;
@@ -29,11 +32,16 @@ import org.chromium.mojo.system.MojoException;
import org.chromium.ui.text.NoUnderlineClickableSpan;
import org.chromium.ui.text.SpanApplier;
public class BraveWalletPreferences
extends BravePreferenceFragment implements ConnectionErrorHandler {
public class BraveWalletPreferences extends BravePreferenceFragment
implements ConnectionErrorHandler, Preference.OnPreferenceChangeListener {
private static final String TAG = "WalletPreferences";
private static final String PREF_BRAVE_WALLET_AUTOLOCK = "pref_brave_wallet_autolock";
/**
* @noinspection unused
*/
private static final String PREF_BRAVE_WALLET_RESET = "pref_brave_wallet_reset";
private static final String BRAVE_WALLET_WEB3_NOTIFICATION_SWITCH = "web3_notifications_switch";
private static final String BRAVE_WALLET_WEB3_NFT_DISCOVERY_SWITCH =
"nft_auto_discovery_switch";
@@ -43,11 +51,16 @@ public class BraveWalletPreferences
public static final String PREF_BRAVE_WALLET_WEB3_NOTIFICATIONS =
"pref_brave_wallet_web3_notifications";
private static final String PREF_DEFAULT_ETHEREUM_WALLET = "default_ethereum_wallet";
private static final String PREF_DEFAULT_SOLANA_WALLET = "default_solana_wallet";
private BraveDialogPreference mDefaultEthereumWallet;
private BraveDialogPreference mDefaultSolanaWallet;
private BraveWalletAutoLockPreferences mPrefAutolock;
private KeyringService mKeyringService;
private ChromeSwitchPreference mWeb3NotificationsSwitch;
private ChromeSwitchPreference mWeb3NftDiscoverySwitch;
private KeyringService mKeyringService;
private WalletModel mWalletModel;
public static boolean getPrefWeb3NotificationsEnabled() {
@@ -57,55 +70,122 @@ public class BraveWalletPreferences
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
try {
BraveActivity activity = BraveActivity.getBraveActivity();
mWalletModel = activity.getWalletModel();
setUpNftDiscoveryPreference();
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "onCreate ", e);
Log.e(TAG, "onCreatePreferences", e);
}
mPrefAutolock = (BraveWalletAutoLockPreferences) findPreference(PREF_BRAVE_WALLET_AUTOLOCK);
mWeb3NotificationsSwitch =
(ChromeSwitchPreference) findPreference(BRAVE_WALLET_WEB3_NOTIFICATION_SWITCH);
mWeb3NotificationsSwitch.setChecked(
BraveWalletPreferences.getPrefWeb3NotificationsEnabled());
mWeb3NotificationsSwitch.setOnPreferenceChangeListener(
(Preference preference, Object newValue) -> {
setPrefWeb3NotificationsEnabled((boolean) newValue);
requireActivity().setTitle(R.string.brave_ui_brave_wallet);
SettingsUtils.addPreferencesFromResource(this, R.xml.brave_wallet_preferences);
return true;
});
setUpNftDiscoveryPreference();
mDefaultEthereumWallet = findPreference(PREF_DEFAULT_ETHEREUM_WALLET);
if (mDefaultEthereumWallet != null) {
mDefaultEthereumWallet.setOnPreferenceChangeListener(this);
mDefaultEthereumWallet.setEnabled(false);
if (mWalletModel != null) {
mWalletModel
.getBraveWalletService()
.getDefaultEthereumWallet(
(@DefaultWallet.EnumType Integer defaultEthereumWallet) ->
setupDefaultWalletPreference(
mDefaultEthereumWallet, defaultEthereumWallet));
}
}
mDefaultSolanaWallet = findPreference(PREF_DEFAULT_SOLANA_WALLET);
if (mDefaultSolanaWallet != null) {
mDefaultSolanaWallet.setOnPreferenceChangeListener(this);
mDefaultSolanaWallet.setEnabled(false);
if (mWalletModel != null) {
mWalletModel
.getBraveWalletService()
.getDefaultSolanaWallet(
(@DefaultWallet.EnumType Integer defaultSolanaWallet) ->
setupDefaultWalletPreference(
mDefaultSolanaWallet, defaultSolanaWallet));
}
}
InitKeyringService();
mPrefAutolock = findPreference(PREF_BRAVE_WALLET_AUTOLOCK);
mWeb3NotificationsSwitch = findPreference(BRAVE_WALLET_WEB3_NOTIFICATION_SWITCH);
if (mWeb3NotificationsSwitch != null) {
mWeb3NotificationsSwitch.setChecked(
BraveWalletPreferences.getPrefWeb3NotificationsEnabled());
mWeb3NotificationsSwitch.setOnPreferenceChangeListener(this);
}
initKeyringService();
}
private void setupDefaultWalletPreference(
@NonNull final BraveDialogPreference walletPreference,
@DefaultWallet.EnumType final Integer defaultWallet) {
walletPreference.setEnabled(true);
if (defaultWallet == DefaultWallet.BRAVE_WALLET_PREFER_EXTENSION) {
walletPreference.setSummary(
requireActivity()
.getResources()
.getString(R.string.settings_default_wallet_option_2));
walletPreference.setCheckedIndex(1);
} else {
walletPreference.setSummary(
requireActivity()
.getResources()
.getString(R.string.settings_default_wallet_option_1));
walletPreference.setCheckedIndex(0);
}
}
@Override
public void onDisplayPreferenceDialog(@NonNull Preference preference) {
if (preference instanceof BraveDialogPreference) {
BravePreferenceDialogFragment dialogFragment =
BravePreferenceDialogFragment.newInstance(preference);
// `setTargetFragment()` must be called even if Lint says the method is deprecated.
// https://issuetracker.google.com/issues/181793702
// noinspection deprecation
dialogFragment.setTargetFragment(this, 0);
dialogFragment.show(getParentFragmentManager(), BravePreferenceDialogFragment.TAG);
dialogFragment.setPreferenceDialogListener(this);
} else {
super.onDisplayPreferenceDialog(preference);
}
}
private void setUpNftDiscoveryPreference() {
if (mWalletModel == null) return;
mWeb3NftDiscoverySwitch =
(ChromeSwitchPreference) findPreference(BRAVE_WALLET_WEB3_NFT_DISCOVERY_SWITCH);
mWalletModel.getCryptoModel().isNftDiscoveryEnabled(isNftDiscoveryEnabled -> {
mWeb3NftDiscoverySwitch.setChecked(isNftDiscoveryEnabled);
});
mWeb3NftDiscoverySwitch.setOnPreferenceChangeListener(
(Preference preference, Object newValue) -> {
mWalletModel.getCryptoModel().updateNftDiscovery((boolean) newValue);
return true;
});
mWeb3NftDiscoverySwitch = findPreference(BRAVE_WALLET_WEB3_NFT_DISCOVERY_SWITCH);
mWalletModel
.getCryptoModel()
.isNftDiscoveryEnabled(
isNftDiscoveryEnabled ->
mWeb3NftDiscoverySwitch.setChecked(isNftDiscoveryEnabled));
mWeb3NftDiscoverySwitch.setOnPreferenceChangeListener(this);
TextMessagePreference learnMorePreference =
findPreference(BRAVE_WALLET_WEB3_NFT_DISCOVERY_LEARN_MORE);
var learnMoreDesc =
SpanApplier.applySpans(getString(R.string.settings_enable_nft_discovery_desc),
new SpanApplier.SpanInfo("<LINK_1>", "</LINK_1>",
new NoUnderlineClickableSpan(
requireContext(), R.color.brave_link, result -> {
TabUtils.openUrlInCustomTab(requireContext(),
WalletConstants.NFT_DISCOVERY_LEARN_MORE_LINK);
})));
learnMorePreference.setSummary(learnMoreDesc);
if (learnMorePreference != null) {
SpannableString learnMoreDesc =
SpanApplier.applySpans(
getString(R.string.settings_enable_nft_discovery_desc),
new SpanApplier.SpanInfo(
"<LINK_1>",
"</LINK_1>",
new NoUnderlineClickableSpan(
requireContext(),
R.color.brave_link,
result -> {
TabUtils.openUrlInCustomTab(
requireContext(),
WalletConstants
.NFT_DISCOVERY_LEARN_MORE_LINK);
})));
learnMorePreference.setSummary(learnMoreDesc);
}
}
@Override
@@ -126,10 +206,10 @@ public class BraveWalletPreferences
public void onConnectionError(MojoException e) {
mKeyringService.close();
mKeyringService = null;
InitKeyringService();
initKeyringService();
}
private void InitKeyringService() {
private void initKeyringService() {
if (mKeyringService != null) {
return;
}
@@ -137,24 +217,22 @@ public class BraveWalletPreferences
mKeyringService = KeyringServiceFactory.getInstance().getKeyringService(this);
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
getActivity().setTitle(R.string.brave_ui_brave_wallet);
SettingsUtils.addPreferencesFromResource(this, R.xml.brave_wallet_preferences);
}
private void refreshAutolockView() {
if (mKeyringService != null) {
mKeyringService.getAutoLockMinutes(minutes -> {
mPrefAutolock.setSummary(getContext().getResources().getQuantityString(
R.plurals.time_long_mins, minutes, minutes));
RecyclerView.ViewHolder viewHolder =
(RecyclerView.ViewHolder) getListView().findViewHolderForAdapterPosition(
mPrefAutolock.getOrder());
if (viewHolder != null) {
viewHolder.itemView.invalidate();
}
});
mKeyringService.getAutoLockMinutes(
minutes -> {
mPrefAutolock.setSummary(
requireContext()
.getResources()
.getQuantityString(
R.plurals.time_long_mins, minutes, minutes));
RecyclerView.ViewHolder viewHolder =
getListView()
.findViewHolderForAdapterPosition(mPrefAutolock.getOrder());
if (viewHolder != null) {
viewHolder.itemView.invalidate();
}
});
}
}
@@ -164,4 +242,34 @@ public class BraveWalletPreferences
sharedPreferencesEditor.putBoolean(PREF_BRAVE_WALLET_WEB3_NOTIFICATIONS, enabled);
sharedPreferencesEditor.apply();
}
@Override
public boolean onPreferenceChange(@NonNull Preference preference, Object object) {
String key = preference.getKey();
if (PREF_DEFAULT_ETHEREUM_WALLET.equals(key) && mWalletModel != null) {
@DefaultWallet.EnumType
final int defaultEthereumWallet = convertToNativeDefaultWallet((Integer) object);
mWalletModel.getBraveWalletService().setDefaultEthereumWallet(defaultEthereumWallet);
setupDefaultWalletPreference(mDefaultEthereumWallet, defaultEthereumWallet);
} else if (PREF_DEFAULT_SOLANA_WALLET.equals(key) && mWalletModel != null) {
@DefaultWallet.EnumType
final int defaultSolanaWallet = convertToNativeDefaultWallet((Integer) object);
mWalletModel.getBraveWalletService().setDefaultSolanaWallet(defaultSolanaWallet);
setupDefaultWalletPreference(mDefaultSolanaWallet, defaultSolanaWallet);
} else if (BRAVE_WALLET_WEB3_NOTIFICATION_SWITCH.equals(key)) {
setPrefWeb3NotificationsEnabled((boolean) object);
} else if (BRAVE_WALLET_WEB3_NFT_DISCOVERY_SWITCH.equals(key) && mWalletModel != null) {
mWalletModel.getCryptoModel().updateNftDiscovery((boolean) object);
}
return true;
}
@DefaultWallet.EnumType
private int convertToNativeDefaultWallet(final Integer defaultWallet) {
if (defaultWallet == 1) {
return DefaultWallet.BRAVE_WALLET_PREFER_EXTENSION;
} else {
return DefaultWallet.NONE;
}
}
}
+5
View File
@@ -35,4 +35,9 @@
<item>@string/request_otr_option_3</item>
</string-array>
<string-array name="settingsDefaultWalletTexts">
<item>@string/settings_default_wallet_option_1</item>
<item>@string/settings_default_wallet_option_2</item>
</string-array>
</resources>
@@ -7,6 +7,18 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<org.chromium.chrome.browser.settings.BraveDialogPreference
android:key="default_ethereum_wallet"
android:title="@string/settings_default_ethereum_wallet_label"
app:dialog_entries="@array/settingsDefaultWalletTexts"
app:dialog_default_index="1"/>
<org.chromium.chrome.browser.settings.BraveDialogPreference
android:key="default_solana_wallet"
android:title="@string/settings_default_solana_wallet_label"
app:dialog_entries="@array/settingsDefaultWalletTexts"
app:dialog_default_index="1"/>
<org.chromium.chrome.browser.settings.BraveWalletAutoLockPreferences
android:key="pref_brave_wallet_autolock"
android:title="@string/brave_wallet_settings_autolock_option"