[Code Health][Android] Clean up generic callback interfaces (#32163)

Here we remove the original interfaces and move them directly to the places they are used.
Interfaces are still genereric, replacing them to a specific interfaces should be done when these parts of code are touched.
In addition, in this PR were fixed presubmit warnings in the files that were touched.
This commit is contained in:
samartnik
2025-10-31 20:32:21 -04:00
committed by GitHub
parent 644492d808
commit 0d14b8f81f
15 changed files with 311 additions and 414 deletions
@@ -1,251 +0,0 @@
/* Copyright (c) 2024 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.base;
import org.chromium.build.annotations.NullMarked;
/** Contains a generic interface for callbacks. */
@NullMarked
public interface Callbacks {
/** A generic callback. */
interface Callback0 {
/** Call the callback. */
public void call();
}
/**
* A generic 1-argument callback.
*
* @param <T1> the type of argument 1.
*/
interface Callback1<T1> {
/** Call the callback. */
public void call(T1 arg1);
}
/**
* A generic 2-argument callback.
*
* @param <T1> the type of argument 1.
* @param <T2> the type of argument 2.
*/
interface Callback2<T1, T2> {
/** Call the callback. */
public void call(T1 arg1, T2 arg2);
}
/**
* A generic 3-argument callback.
*
* @param <T1> the type of argument 1.
* @param <T2> the type of argument 2.
* @param <T3> the type of argument 3.
*/
interface Callback3<T1, T2, T3> {
/** Call the callback. */
public void call(T1 arg1, T2 arg2, T3 arg3);
}
/**
* A generic 4-argument callback.
*
* @param <T1> the type of argument 1.
* @param <T2> the type of argument 2.
* @param <T3> the type of argument 3.
* @param <T4> the type of argument 4.
*/
interface Callback4<T1, T2, T3, T4> {
/** Call the callback. */
public void call(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}
/**
* A generic 5-argument callback.
*
* @param <T1> the type of argument 1.
* @param <T2> the type of argument 2.
* @param <T3> the type of argument 3.
* @param <T4> the type of argument 4.
* @param <T5> the type of argument 5.
*/
interface Callback5<T1, T2, T3, T4, T5> {
/** Call the callback. */
public void call(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
}
/**
* A generic 6-argument callback.
*
* @param <T1> the type of argument 1.
* @param <T2> the type of argument 2.
* @param <T3> the type of argument 3.
* @param <T4> the type of argument 4.
* @param <T5> the type of argument 5.
* @param <T6> the type of argument 6.
*/
interface Callback6<T1, T2, T3, T4, T5, T6> {
/** Call the callback. */
public void call(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6);
}
/**
* A generic 7-argument callback.
*
* @param <T1> the type of argument 1.
* @param <T2> the type of argument 2.
* @param <T3> the type of argument 3.
* @param <T4> the type of argument 4.
* @param <T5> the type of argument 5.
* @param <T6> the type of argument 6.
* @param <T7> the type of argument 7.
*/
interface Callback7<T1, T2, T3, T4, T5, T6, T7> {
/** Call the callback. */
public void call(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7);
}
/**
* A generic 11-argument callback.
*
* @param <T1> the type of argument 1.
* @param <T2> the type of argument 2.
* @param <T3> the type of argument 3.
* @param <T4> the type of argument 4.
* @param <T5> the type of argument 5.
* @param <T6> the type of argument 6.
* @param <T7> the type of argument 7.
* @param <T8> the type of argument 8.
* @param <T9> the type of argument 9.
* @param <T10> the type of argument 10.
* @param <T11> the type of argument 11.
*/
interface Callback11<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> {
/** Call the callback. */
public void call(
T1 arg1,
T2 arg2,
T3 arg3,
T4 arg4,
T5 arg5,
T6 arg6,
T7 arg7,
T8 arg8,
T9 arg9,
T10 arg10,
T11 arg11);
}
/**
* A generic 13-argument callback.
*
* @param <T1> the type of argument 1.
* @param <T2> the type of argument 2.
* @param <T3> the type of argument 3.
* @param <T4> the type of argument 4.
* @param <T5> the type of argument 5.
* @param <T6> the type of argument 6.
* @param <T7> the type of argument 7.
* @param <T8> the type of argument 8.
* @param <T9> the type of argument 9.
* @param <T10> the type of argument 10.
* @param <T11> the type of argument 11.
* @param <T12> the type of argument 12.
* @param <T13> the type of argument 13.
*/
interface Callback13<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> {
/** Call the callback. */
public void call(
T1 arg1,
T2 arg2,
T3 arg3,
T4 arg4,
T5 arg5,
T6 arg6,
T7 arg7,
T8 arg8,
T9 arg9,
T10 arg10,
T11 arg11,
T12 arg12,
T13 arg13);
}
/**
* A generic 22-argument callback.
*
* @param <T1> the type of argument 1.
* @param <T2> the type of argument 2.
* @param <T3> the type of argument 3.
* @param <T4> the type of argument 4.
* @param <T5> the type of argument 5.
* @param <T6> the type of argument 6.
* @param <T7> the type of argument 7.
* @param <T8> the type of argument 8.
* @param <T9> the type of argument 9.
* @param <T10> the type of argument 10.
* @param <T11> the type of argument 11.
* @param <T12> the type of argument 12.
* @param <T13> the type of argument 13.
* @param <T14> the type of argument 14.
* @param <T15> the type of argument 15.
* @param <T16> the type of argument 16.
* @param <T17> the type of argument 17.
* @param <T18> the type of argument 18.
* @param <T19> the type of argument 19.
* @param <T20> the type of argument 20.
* @param <T21> the type of argument 21.
* @param <T22> the type of argument 22.
*/
interface Callback22<
T1,
T2,
T3,
T4,
T5,
T6,
T7,
T8,
T9,
T10,
T11,
T12,
T13,
T14,
T15,
T16,
T17,
T18,
T19,
T20,
T21,
T22> {
/** Call the callback. */
public void call(
T1 arg1,
T2 arg2,
T3 arg3,
T4 arg4,
T5 arg5,
T6 arg6,
T7 arg7,
T8 arg8,
T9 arg9,
T10 arg10,
T11 arg11,
T12 arg12,
T13 arg13,
T14 arg14,
T15 arg15,
T16 arg16,
T17 arg17,
T18 arg18,
T19 arg19,
T20 arg20,
T21 arg21,
T22 arg22);
}
}
@@ -11,7 +11,6 @@ import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import org.chromium.base.Callbacks.Callback1;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.AssetRatioService;
import org.chromium.brave_wallet.mojom.BlockchainRegistry;
@@ -43,6 +42,16 @@ import java.util.List;
// - mAssetRatioService
@SuppressWarnings("UnusedVariable")
public class CryptoModel {
/**
* A generic 1-argument callback.
*
* @param <T1> The type of the first argument.
*/
public interface Callback1<T1> {
/** Call the callback. */
void call(T1 arg1);
}
private TxService mTxService;
private final PendingTxHelper mPendingTxHelper;
private KeyringService mKeyringService;
@@ -8,7 +8,6 @@ package org.chromium.chrome.browser.app.domain;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import org.chromium.base.Callbacks;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.CoinType;
@@ -33,6 +32,16 @@ import java.util.Collections;
import java.util.List;
public class DappsModel implements KeyringServiceObserver {
/**
* A generic 1-argument callback.
*
* @param <T1> The type of the first argument.
*/
public interface Callback1<T1> {
/** Call the callback. */
void call(T1 arg1);
}
private JsonRpcService mJsonRpcService;
private final KeyringService mKeyringService;
private BraveWalletService mBraveWalletService;
@@ -71,8 +80,9 @@ public class DappsModel implements KeyringServiceObserver {
mKeyringService.addObserver(this);
}
public void fetchAccountsForConnectionReq(@CoinType.EnumType int coinType,
Callbacks.Callback1<Pair<AccountInfo, List<AccountInfo>>> callback) {
public void fetchAccountsForConnectionReq(
@CoinType.EnumType int coinType,
Callback1<Pair<AccountInfo, List<AccountInfo>>> callback) {
if (coinType != CoinType.ETH && coinType != CoinType.SOL) {
callback.call(new Pair<>(null, Collections.emptyList()));
return;
@@ -213,7 +223,7 @@ public class DappsModel implements KeyringServiceObserver {
mBraveWalletService.notifySignMessageRequestProcessed(isApproved, id, null, error);
}
public void getPendingSignMessageRequests(Callbacks.Callback1<SignMessageRequest[]> callback) {
public void getPendingSignMessageRequests(Callback1<SignMessageRequest[]> callback) {
mBraveWalletService.getPendingSignMessageRequests(callback::call);
}
@@ -15,7 +15,6 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import org.chromium.base.Callbacks;
import org.chromium.base.Log;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.AllAccountsInfo;
@@ -41,6 +40,16 @@ import java.util.Locale;
import java.util.Set;
public class KeyringModel implements KeyringServiceObserver {
/**
* A generic 1-argument callback.
*
* @param <T1> The type of the first argument.
*/
public interface Callback1<T1> {
/** Call the callback. */
void call(T1 arg1);
}
private static final String TAG = "KeyringModel";
private final Object mLock = new Object();
@@ -119,8 +128,11 @@ public class KeyringModel implements KeyringServiceObserver {
}
}
public void getAccounts(Callbacks.Callback1<AccountInfo[]> callback1) {
mKeyringService.getAllAccounts(allAccounts -> { callback1.call(allAccounts.accounts); });
public void getAccounts(Callback1<AccountInfo[]> callback1) {
mKeyringService.getAllAccounts(
allAccounts -> {
callback1.call(allAccounts.accounts);
});
}
public void isWalletCreated(@NonNull final KeyringService.IsWalletCreated_Response callback) {
@@ -135,7 +147,7 @@ public class KeyringModel implements KeyringServiceObserver {
@CoinType.EnumType int coinType,
@KeyringId.EnumType int keyringId,
String accountName,
Callbacks.Callback1<Boolean> callback) {
Callback1<Boolean> callback) {
mKeyringService.addAccount(
coinType,
keyringId,
@@ -149,7 +161,7 @@ public class KeyringModel implements KeyringServiceObserver {
@CoinType.EnumType int coinType,
String chainId,
String accountName,
Callbacks.Callback1<Boolean> callback) {
Callback1<Boolean> callback) {
@KeyringId.EnumType int keyringId = AssetUtils.getKeyring(coinType, chainId);
if (accountName == null) {
LiveDataUtil.observeOnce(
@@ -167,7 +179,7 @@ public class KeyringModel implements KeyringServiceObserver {
}
}
public void isWalletLocked(@NonNull final Callbacks.Callback1<Boolean> callback) {
public void isWalletLocked(@NonNull final Callback1<Boolean> callback) {
mKeyringService.isLocked(callback::call);
}
@@ -185,7 +197,7 @@ public class KeyringModel implements KeyringServiceObserver {
}
private void handleAddAccountResult(
AccountInfo result, @NonNull final Callbacks.Callback1<Boolean> callback) {
AccountInfo result, @NonNull final Callback1<Boolean> callback) {
mCryptoSharedActions.updateCoinType();
mCryptoSharedActions.onNewAccountAdded();
callback.call(result != null);
@@ -216,7 +228,7 @@ public class KeyringModel implements KeyringServiceObserver {
@NonNull final Set<NetworkInfo> availableNetworks,
@NonNull final Set<NetworkInfo> selectedNetworks,
@NonNull final JsonRpcService jsonRpcService,
@NonNull final Callbacks.Callback1<Boolean> callback) {
@NonNull final Callback1<Boolean> callback) {
assertOnUiThread();
generateWallet(
password,
@@ -250,7 +262,7 @@ public class KeyringModel implements KeyringServiceObserver {
@NonNull final Set<NetworkInfo> availableNetworks,
@NonNull final Set<NetworkInfo> selectedNetworks,
@NonNull final JsonRpcService jsonRpcService,
@NonNull final Callbacks.Callback1<String> callback) {
@NonNull final Callback1<String> callback) {
assertOnUiThread();
generateWallet(
password,
@@ -270,8 +282,8 @@ public class KeyringModel implements KeyringServiceObserver {
@NonNull final Set<NetworkInfo> availableNetworks,
@NonNull final Set<NetworkInfo> selectedNetworks,
@NonNull final JsonRpcService jsonRpcService,
@Nullable final Callbacks.Callback1<String> createCallback,
@Nullable final Callbacks.Callback1<Boolean> restoreCallback) {
@Nullable final Callback1<String> createCallback,
@Nullable final Callback1<Boolean> restoreCallback) {
final Set<NetworkInfo> removeHiddenNetworks = new HashSet<>();
final Set<NetworkInfo> addHiddenNetworks = new HashSet<>();
@@ -386,7 +398,7 @@ public class KeyringModel implements KeyringServiceObserver {
@NonNull final String recoveryPhrase,
final boolean legacyRestoreEnabled,
@NonNull final Set<NetworkInfo> selectedNetworks,
@NonNull final Callbacks.Callback1<Boolean> callback) {
@NonNull final Callback1<Boolean> callback) {
assertOnUiThread();
mKeyringService.restoreWallet(
recoveryPhrase,
@@ -398,7 +410,7 @@ public class KeyringModel implements KeyringServiceObserver {
private void finalizeWalletCreation(
@NonNull final String password,
@NonNull final Set<NetworkInfo> selectedNetworks,
@NonNull final Callbacks.Callback1<String> callback) {
@NonNull final Callback1<String> callback) {
assertOnUiThread();
mKeyringService.createWallet(
password,
@@ -408,7 +420,7 @@ public class KeyringModel implements KeyringServiceObserver {
private <T> void createAccounts(
final T result,
@NonNull final Set<NetworkInfo> selectedNetworks,
@NonNull final Callbacks.Callback1<T> callback) {
@NonNull final Callback1<T> callback) {
final Set<NetworkInfo> createAccounts = new HashSet<>();
for (NetworkInfo networkInfo : selectedNetworks) {
@@ -455,8 +467,7 @@ public class KeyringModel implements KeyringServiceObserver {
}
}
private <T> void selectEthAccount(
final T result, @NonNull final Callbacks.Callback1<T> callback) {
private <T> void selectEthAccount(final T result, @NonNull final Callback1<T> callback) {
mKeyringService.getAllAccounts(
allAccounts -> {
if (allAccounts.ethDappSelectedAccount != null) {
@@ -13,7 +13,6 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import org.chromium.base.Callbacks;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.BraveWalletService;
@@ -36,25 +35,35 @@ import java.util.Map;
import java.util.stream.Collectors;
public class NetworkModel implements JsonRpcServiceObserver {
/**
* A generic 1-argument callback.
*
* @param <T1> The type of the first argument.
*/
public interface Callback1<T1> {
/** Call the callback. */
void call(T1 arg1);
}
private BraveWalletService mBraveWalletService;
private JsonRpcService mJsonRpcService;
private final Object mLock = new Object();
private Mode mMode = Mode.WALLET_MODE;
private final MediatorLiveData<String> _mChainId;
private final MediatorLiveData<List<NetworkInfo>> _mDefaultCoinCryptoNetworks;
private final MutableLiveData<List<NetworkInfo>> _mCryptoNetworks;
private final MediatorLiveData<String> mMediatorChainId;
private final MediatorLiveData<List<NetworkInfo>> mMediatorDefaultCoinCryptoNetworks;
private final MutableLiveData<List<NetworkInfo>> mMutableCryptoNetworks;
private final CryptoSharedData mSharedData;
private final CryptoSharedActions mCryptoActions;
private final MediatorLiveData<Pair<String, List<NetworkInfo>>> _mPairChainAndNetwork;
private final MediatorLiveData<NetworkInfo> _mNeedToCreateAccountForNetwork;
private final MediatorLiveData<NetworkInfo> _mDefaultNetwork;
private final MediatorLiveData<String[]> _mCustomNetworkIds;
private final MediatorLiveData<List<NetworkInfo>> _mPrimaryNetworks;
private final MediatorLiveData<List<NetworkInfo>> _mSecondaryNetworks;
private final MediatorLiveData<Pair<String, List<NetworkInfo>>> mMediatorPairChainAndNetwork;
private final MediatorLiveData<NetworkInfo> mMediatorNeedToCreateAccountForNetwork;
private final MediatorLiveData<NetworkInfo> mMediatorDefaultNetwork;
private final MediatorLiveData<String[]> mMediatorCustomNetworkIds;
private final MediatorLiveData<List<NetworkInfo>> mMediatorPrimaryNetworks;
private final MediatorLiveData<List<NetworkInfo>> mMediatorSecondaryNetworks;
public final LiveData<String> mChainId;
private final MutableLiveData<NetworkLists> _mNetworkLists;
private final MutableLiveData<NetworkLists> mMutableNetworkLists;
public final LiveData<String[]> mCustomNetworkIds;
public LiveData<NetworkInfo> mNeedToCreateAccountForNetwork;
public final LiveData<Pair<String, List<NetworkInfo>>> mPairChainAndNetwork;
@@ -76,62 +85,63 @@ public class NetworkModel implements JsonRpcServiceObserver {
mSharedData = sharedData;
mCryptoActions = cryptoSharedActions;
_mChainId = new MediatorLiveData<>();
_mChainId.setValue(BraveWalletConstants.MAINNET_CHAIN_ID);
mChainId = _mChainId;
_mDefaultCoinCryptoNetworks = new MediatorLiveData<>();
mDefaultCoinCryptoNetworks = _mDefaultCoinCryptoNetworks;
_mCryptoNetworks = new MutableLiveData<>(Collections.emptyList());
mCryptoNetworks = _mCryptoNetworks;
_mPairChainAndNetwork = new MediatorLiveData<>();
mPairChainAndNetwork = _mPairChainAndNetwork;
_mDefaultNetwork = new MediatorLiveData<>();
mDefaultNetwork = _mDefaultNetwork;
_mNeedToCreateAccountForNetwork = new MediatorLiveData<>();
mNeedToCreateAccountForNetwork = _mNeedToCreateAccountForNetwork;
_mCustomNetworkIds = new MediatorLiveData<>();
_mCustomNetworkIds.postValue(new String[0]);
mCustomNetworkIds = _mCustomNetworkIds;
_mPrimaryNetworks = new MediatorLiveData<>();
mPrimaryNetworks = _mPrimaryNetworks;
_mSecondaryNetworks = new MediatorLiveData<>();
mSecondaryNetworks = _mSecondaryNetworks;
mMediatorChainId = new MediatorLiveData<>();
mMediatorChainId.setValue(BraveWalletConstants.MAINNET_CHAIN_ID);
mChainId = mMediatorChainId;
mMediatorDefaultCoinCryptoNetworks = new MediatorLiveData<>();
mDefaultCoinCryptoNetworks = mMediatorDefaultCoinCryptoNetworks;
mMutableCryptoNetworks = new MutableLiveData<>(Collections.emptyList());
mCryptoNetworks = mMutableCryptoNetworks;
mMediatorPairChainAndNetwork = new MediatorLiveData<>();
mPairChainAndNetwork = mMediatorPairChainAndNetwork;
mMediatorDefaultNetwork = new MediatorLiveData<>();
mDefaultNetwork = mMediatorDefaultNetwork;
mMediatorNeedToCreateAccountForNetwork = new MediatorLiveData<>();
mNeedToCreateAccountForNetwork = mMediatorNeedToCreateAccountForNetwork;
mMediatorCustomNetworkIds = new MediatorLiveData<>();
mMediatorCustomNetworkIds.postValue(new String[0]);
mCustomNetworkIds = mMediatorCustomNetworkIds;
mMediatorPrimaryNetworks = new MediatorLiveData<>();
mPrimaryNetworks = mMediatorPrimaryNetworks;
mMediatorSecondaryNetworks = new MediatorLiveData<>();
mSecondaryNetworks = mMediatorSecondaryNetworks;
jsonRpcService.addObserver(this);
_mNetworkLists = new MutableLiveData<>();
mNetworkLists = _mNetworkLists;
_mPairChainAndNetwork.setValue(Pair.create("", Collections.emptyList()));
_mPairChainAndNetwork.addSource(
_mChainId,
mMutableNetworkLists = new MutableLiveData<>();
mNetworkLists = mMutableNetworkLists;
mMediatorPairChainAndNetwork.setValue(Pair.create("", Collections.emptyList()));
mMediatorPairChainAndNetwork.addSource(
mMediatorChainId,
chainId -> {
_mPairChainAndNetwork.setValue(
Pair.create(chainId, _mDefaultCoinCryptoNetworks.getValue()));
mMediatorPairChainAndNetwork.setValue(
Pair.create(chainId, mMediatorDefaultCoinCryptoNetworks.getValue()));
});
_mPairChainAndNetwork.addSource(
_mDefaultCoinCryptoNetworks,
mMediatorPairChainAndNetwork.addSource(
mMediatorDefaultCoinCryptoNetworks,
networks -> {
_mPairChainAndNetwork.setValue(Pair.create(_mChainId.getValue(), networks));
mMediatorPairChainAndNetwork.setValue(
Pair.create(mMediatorChainId.getValue(), networks));
});
_mDefaultNetwork.addSource(
_mPairChainAndNetwork,
mMediatorDefaultNetwork.addSource(
mMediatorPairChainAndNetwork,
chainIdAndInfosPair -> {
String chainId = chainIdAndInfosPair.first;
List<NetworkInfo> cryptoNetworks = chainIdAndInfosPair.second;
if (chainId == null || cryptoNetworks == null) return;
for (NetworkInfo networkInfo : cryptoNetworks) {
if (networkInfo.chainId.equals(chainId)) {
_mDefaultNetwork.postValue(networkInfo);
mMediatorDefaultNetwork.postValue(networkInfo);
break;
}
}
});
_mChainId.addSource(mSharedData.getCoinTypeLd(), coinType -> updateChainId());
_mDefaultCoinCryptoNetworks.addSource(
mMediatorChainId.addSource(mSharedData.getCoinTypeLd(), coinType -> updateChainId());
mMediatorDefaultCoinCryptoNetworks.addSource(
mSharedData.getCoinTypeLd(),
coinType -> {
getAllNetworks(
mJsonRpcService,
networkInfoSet -> {
_mDefaultCoinCryptoNetworks.postValue(
mMediatorDefaultCoinCryptoNetworks.postValue(
new ArrayList<>(
networkInfoSet.stream()
.filter(n -> n.coin == coinType)
@@ -139,30 +149,38 @@ public class NetworkModel implements JsonRpcServiceObserver {
});
});
_mCustomNetworkIds.addSource(
mMediatorCustomNetworkIds.addSource(
mSharedData.getCoinTypeLd(),
coinType -> {
mJsonRpcService.getCustomNetworks(coinType, _mCustomNetworkIds::postValue);
mJsonRpcService.getCustomNetworks(
coinType, mMediatorCustomNetworkIds::postValue);
});
mMediatorPrimaryNetworks.addSource(
mCryptoNetworks,
networkInfos -> {
List<NetworkInfo> primaryNws = new ArrayList<>();
for (NetworkInfo networkInfo : networkInfos) {
if (WalletConstants.SUPPORTED_TOP_LEVEL_CHAIN_IDS.contains(
networkInfo.chainId)) {
primaryNws.add(networkInfo);
}
}
mMediatorPrimaryNetworks.postValue(primaryNws);
});
mMediatorSecondaryNetworks.addSource(
mCryptoNetworks,
networkInfos -> {
List<NetworkInfo> secondaryNws = new ArrayList<>();
for (NetworkInfo networkInfo : networkInfos) {
if (!WalletConstants.SUPPORTED_TOP_LEVEL_CHAIN_IDS.contains(
networkInfo.chainId)
&& !WalletConstants.KNOWN_TEST_CHAIN_IDS.contains(
networkInfo.chainId)) {
secondaryNws.add(networkInfo);
}
}
mMediatorSecondaryNetworks.postValue(secondaryNws);
});
_mPrimaryNetworks.addSource(mCryptoNetworks, networkInfos -> {
List<NetworkInfo> primaryNws = new ArrayList<>();
for (NetworkInfo networkInfo : networkInfos) {
if (WalletConstants.SUPPORTED_TOP_LEVEL_CHAIN_IDS.contains(networkInfo.chainId)) {
primaryNws.add(networkInfo);
}
}
_mPrimaryNetworks.postValue(primaryNws);
});
_mSecondaryNetworks.addSource(mCryptoNetworks, networkInfos -> {
List<NetworkInfo> secondaryNws = new ArrayList<>();
for (NetworkInfo networkInfo : networkInfos) {
if (!WalletConstants.SUPPORTED_TOP_LEVEL_CHAIN_IDS.contains(networkInfo.chainId)
&& !WalletConstants.KNOWN_TEST_CHAIN_IDS.contains(networkInfo.chainId)) {
secondaryNws.add(networkInfo);
}
}
_mSecondaryNetworks.postValue(secondaryNws);
});
}
private void updateChainId() {
@@ -178,20 +196,24 @@ public class NetworkModel implements JsonRpcServiceObserver {
return;
}
@CoinType.EnumType int coin = coinBoxed;
mJsonRpcService.getNetwork(coin, null, networkInfo -> {
if (networkInfo != null) {
_mChainId.postValue(networkInfo.chainId);
}
});
mJsonRpcService.getNetwork(
coin,
null,
networkInfo -> {
if (networkInfo != null) {
mMediatorChainId.postValue(networkInfo.chainId);
}
});
} else if (mMode == Mode.PANEL_MODE) {
if (mBraveWalletService == null) {
return;
}
mBraveWalletService.getNetworkForSelectedAccountOnActiveOrigin(networkInfo -> {
if (networkInfo != null) {
_mChainId.postValue(networkInfo.chainId);
}
});
mBraveWalletService.getNetworkForSelectedAccountOnActiveOrigin(
networkInfo -> {
if (networkInfo != null) {
mMediatorChainId.postValue(networkInfo.chainId);
}
});
}
}
@@ -202,18 +224,21 @@ public class NetworkModel implements JsonRpcServiceObserver {
void setUpAccountObserver(LiveData<List<AccountInfo>> accounts) {
// getAccounts can be null as it's being set via a setter
// clear the _mNeedToCreateAccountForNetwork state once a account has been created
// clear the mMediatorNeedToCreateAccountForNetwork state once a account has been created
// think we may not need this since it's being cleared with clearCreateAccountState anyway
if (mSharedData.getAccounts() == null) return;
_mNeedToCreateAccountForNetwork.addSource(accounts, accountInfos -> {
if (_mNeedToCreateAccountForNetwork.getValue() == null) return;
for (AccountInfo accountInfo : accountInfos) {
if (accountInfo.accountId.coin == _mNeedToCreateAccountForNetwork.getValue().coin) {
_mNeedToCreateAccountForNetwork.postValue(null);
break;
}
}
});
mMediatorNeedToCreateAccountForNetwork.addSource(
accounts,
accountInfos -> {
if (mMediatorNeedToCreateAccountForNetwork.getValue() == null) return;
for (AccountInfo accountInfo : accountInfos) {
if (accountInfo.accountId.coin
== mMediatorNeedToCreateAccountForNetwork.getValue().coin) {
mMediatorNeedToCreateAccountForNetwork.postValue(null);
break;
}
}
});
}
public void resetServices(
@@ -231,7 +256,7 @@ public class NetworkModel implements JsonRpcServiceObserver {
@SuppressWarnings("NoStreams")
static void getAllNetworks(
JsonRpcService jsonRpcService, Callbacks.Callback1<List<NetworkInfo>> callback) {
JsonRpcService jsonRpcService, Callback1<List<NetworkInfo>> callback) {
if (jsonRpcService == null) {
callback.call(Collections.emptyList());
return;
@@ -278,7 +303,7 @@ public class NetworkModel implements JsonRpcServiceObserver {
getAllNetworks(
mJsonRpcService,
cryptoNetworks -> {
_mCryptoNetworks.postValue(cryptoNetworks);
mMutableCryptoNetworks.postValue(cryptoNetworks);
List<NetworkInfo> primary = new ArrayList<>();
List<NetworkInfo> secondary = new ArrayList<>();
@@ -295,20 +320,24 @@ public class NetworkModel implements JsonRpcServiceObserver {
secondary.add(networkInfo);
}
}
_mNetworkLists.postValue(networkLists);
mMutableNetworkLists.postValue(networkLists);
});
}
}
public void setNetworkWithAccountCheck(NetworkInfo networkToBeSetAsSelected,
boolean setNetworkAsDefault, Callbacks.Callback1<Boolean> callback) {
NetworkInfo selectedNetwork = _mDefaultNetwork.getValue();
public void setNetworkWithAccountCheck(
NetworkInfo networkToBeSetAsSelected,
boolean setNetworkAsDefault,
Callback1<Boolean> callback) {
NetworkInfo selectedNetwork = mMediatorDefaultNetwork.getValue();
if (isSameNetwork(networkToBeSetAsSelected, selectedNetwork)) return;
mBraveWalletService.ensureSelectedAccountForChain(
networkToBeSetAsSelected.coin, networkToBeSetAsSelected.chainId, accountId -> {
networkToBeSetAsSelected.coin,
networkToBeSetAsSelected.chainId,
accountId -> {
if (accountId == null) {
_mNeedToCreateAccountForNetwork.postValue(networkToBeSetAsSelected);
mMediatorNeedToCreateAccountForNetwork.postValue(networkToBeSetAsSelected);
callback.call(false);
return;
}
@@ -322,9 +351,10 @@ public class NetworkModel implements JsonRpcServiceObserver {
}
public void setNetworkForSelectedAccountOnActiveOrigin(
NetworkInfo networkToBeSetAsSelected, Callbacks.Callback1<Boolean> callback) {
NetworkInfo networkToBeSetAsSelected, Callback1<Boolean> callback) {
mBraveWalletService.setNetworkForSelectedAccountOnActiveOrigin(
networkToBeSetAsSelected.chainId, success -> {
networkToBeSetAsSelected.chainId,
success -> {
callback.call(success);
mCryptoActions.updateCoinType();
init();
@@ -332,8 +362,7 @@ public class NetworkModel implements JsonRpcServiceObserver {
}
public void setDefaultNetwork(
@NonNull final NetworkInfo networkInfo,
@NonNull final Callbacks.Callback1<Boolean> callback) {
@NonNull final NetworkInfo networkInfo, @NonNull final Callback1<Boolean> callback) {
mJsonRpcService.setNetwork(
networkInfo.chainId,
networkInfo.coin,
@@ -346,12 +375,12 @@ public class NetworkModel implements JsonRpcServiceObserver {
}
public void clearCreateAccountState() {
_mNeedToCreateAccountForNetwork.postValue(null);
mMediatorNeedToCreateAccountForNetwork.postValue(null);
}
public NetworkInfo getNetwork(String chainId) {
if (TextUtils.isEmpty(chainId)) return null;
List<NetworkInfo> cryptoNws = JavaUtils.safeVal(_mCryptoNetworks.getValue());
List<NetworkInfo> cryptoNws = JavaUtils.safeVal(mMutableCryptoNetworks.getValue());
for (NetworkInfo info : cryptoNws) {
if (info.chainId.equals(chainId)) {
return info;
@@ -38,7 +38,6 @@ import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
import org.chromium.base.Callbacks;
import org.chromium.base.ContextUtils;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.content.WebContentsFactory;
@@ -430,11 +429,21 @@ public class ImageLoader {
return url.endsWith(".gif") || url.endsWith("=gif");
}
/**
* A generic 1-argument callback.
*
* @param <T1> The type of the first argument.
*/
public interface Callback1<T1> {
/** Call the callback. */
void call(T1 arg1);
}
public static void fetchFavIcon(
String originSpecUrl,
boolean useIncognitoNtpIcon,
WeakReference<Context> context,
Callbacks.Callback1<Bitmap> callback) {
Callback1<Bitmap> callback) {
try {
BraveActivity activity = BraveActivity.getBraveActivity();
FaviconHelper.FaviconImageCallback imageCallback =
@@ -10,18 +10,26 @@ import org.jni_zero.JNINamespace;
import org.jni_zero.NativeMethods;
import org.chromium.base.Callback;
import org.chromium.base.Callbacks;
import org.chromium.base.Log;
import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.settings.BraveWalletPreferences;
import org.chromium.content_public.browser.WebContents;
/**
* @noinspection unused
*/
/** Helper class for BraveWalletProviderDelegate implementation. */
@JNINamespace("brave_wallet")
@SuppressWarnings("unused")
public class BraveWalletProviderDelegateImplHelper {
/**
* A generic 1-argument callback.
*
* @param <T1> The type of the first argument.
*/
public interface Callback1<T1> {
/** Call the callback. */
void call(T1 arg1);
}
private static final String TAG = "BraveWalletProvider";
@CalledByNative
@@ -90,7 +98,7 @@ public class BraveWalletProviderDelegateImplHelper {
}
public static void isSolanaConnected(
WebContents webContents, String account, Callbacks.Callback1<Boolean> callback) {
WebContents webContents, String account, Callback1<Boolean> callback) {
Callback<Boolean> callbackWrapper =
result -> {
callback.call(result);
@@ -17,7 +17,6 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.chromium.base.Callbacks;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.crypto_wallet.util.AndroidUtils;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
@@ -28,6 +27,27 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TwoLineItemRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
/**
* A generic 1-argument callback.
*
* @param <T1> The type of the first argument.
*/
public interface Callback1<T1> {
/** Call the callback. */
void call(T1 arg1);
}
/**
* A generic 2-argument callback.
*
* @param <T1> The type of the first argument.
* @param <T2> The type of the second argument.
*/
public interface Callback2<T1, T2> {
/** Call the callback. */
void call(T1 arg1, T2 arg2);
}
private List<TwoLineItem> mValues;
private final ExecutorService mExecutor;
private final Handler mHandler;
@@ -199,12 +219,10 @@ public class TwoLineItemRecyclerViewAdapter extends RecyclerView.Adapter<Recycle
public ImageType imageType;
public String imgData;
public Callbacks.Callback2<TextView, TextView> updateViewCb;
public Callback2<TextView, TextView> updateViewCb;
public TwoLineItemText(
String title,
String subTitle,
Callbacks.Callback2<TextView, TextView> customUiChanges) {
String title, String subTitle, Callback2<TextView, TextView> customUiChanges) {
this(title, subTitle);
this.updateViewCb = customUiChanges;
}
@@ -250,7 +268,7 @@ public class TwoLineItemRecyclerViewAdapter extends RecyclerView.Adapter<Recycle
public static class TwoLineSingleText implements TwoLineItem {
public String mText;
public Callbacks.Callback1<TextView> updateViewCb;
public Callback1<TextView> updateViewCb;
public TwoLineSingleText() {
mText = "";
@@ -5,7 +5,6 @@
package org.chromium.chrome.browser.crypto_wallet.util;
import org.chromium.base.Callbacks;
import org.chromium.base.Log;
import org.chromium.brave_wallet.mojom.AssetPrice;
import org.chromium.brave_wallet.mojom.AssetPriceRequest;
@@ -23,7 +22,7 @@ public class AssetsPricesHelper {
public static void fetchPrices(
AssetRatioService assetRatioService,
BlockchainToken[] assets,
Callbacks.Callback1<List<AssetPrice>> callback) {
AsyncUtils.Callback1<List<AssetPrice>> callback) {
List<AssetPriceRequest> requests = new ArrayList<>();
for (BlockchainToken asset : assets) {
@@ -7,7 +7,6 @@ package org.chromium.chrome.browser.crypto_wallet.util;
import static org.chromium.chrome.browser.crypto_wallet.util.Utils.warnWhenError;
import org.chromium.base.Callbacks;
import org.chromium.brave_wallet.mojom.AssetPrice;
import org.chromium.brave_wallet.mojom.AssetRatioService;
import org.chromium.brave_wallet.mojom.AssetTimePrice;
@@ -28,6 +27,40 @@ import java.util.List;
public class AsyncUtils {
private static final String TAG = "AsyncUtils";
/**
* A generic 1-argument callback.
*
* @param <T1> The type of the first argument.
*/
public interface Callback1<T1> {
/** Call the callback. */
void call(T1 arg1);
}
/**
* A generic 2-argument callback.
*
* @param <T1> The type of the first argument.
* @param <T2> The type of the second argument.
*/
public interface Callback2<T1, T2> {
/** Call the callback. */
void call(T1 arg1, T2 arg2);
}
/**
* A generic 4-argument callback.
*
* @param <T1> The type of the first argument.
* @param <T2> The type of the second argument.
* @param <T3> The type of the third argument.
* @param <T4> The type of the fourth argument.
*/
public interface Callback4<T1, T2, T3, T4> {
/** Call the callback. */
void call(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}
// Helper to track multiple wallet services responses
public static class MultiResponseHandler {
private Runnable mWhenAllCompletedRunnable;
@@ -243,7 +276,7 @@ public class AsyncUtils {
}
public static class FetchPricesResponseContext extends SingleResponseBaseContext
implements Callbacks.Callback1<List<AssetPrice>> {
implements Callback1<List<AssetPrice>> {
public List<AssetPrice> assetPrices;
public FetchPricesResponseContext(Runnable responseCompleteCallback) {
@@ -258,7 +291,7 @@ public class AsyncUtils {
}
public static class GetNativeAssetsBalancesResponseContext extends SingleResponseBaseContext
implements Callbacks.Callback2<Integer, HashMap<String, Double>> {
implements Callback2<Integer, HashMap<String, Double>> {
public int coinType;
public HashMap<String, Double> nativeAssetsBalances;
@@ -275,7 +308,7 @@ public class AsyncUtils {
}
public static class GetBlockchainTokensBalancesResponseContext extends SingleResponseBaseContext
implements Callbacks.Callback2<Integer, HashMap<String, HashMap<String, Double>>> {
implements Callback2<Integer, HashMap<String, HashMap<String, Double>>> {
public HashMap<String, HashMap<String, Double>> blockchainTokensBalances;
public int coinType;
@@ -294,7 +327,7 @@ public class AsyncUtils {
}
public static class GetTxExtraInfoResponseContext extends SingleResponseBaseContext
implements Callbacks.Callback4<
implements Callback4<
List<AssetPrice>,
BlockchainToken[],
HashMap<String, Double>,
@@ -346,7 +379,7 @@ public class AsyncUtils {
}
public static class GetP3ABalancesContext extends SingleResponseBaseContext
implements Callbacks.Callback1<HashMap<Integer, HashSet<String>>> {
implements Callback1<HashMap<Integer, HashSet<String>>> {
public HashMap<Integer, HashSet<String>> activeAddresses;
public GetP3ABalancesContext(Runnable responseCompleteCallback) {
@@ -7,7 +7,6 @@ package org.chromium.chrome.browser.crypto_wallet.util;
import androidx.annotation.NonNull;
import org.chromium.base.Callbacks;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.BlockchainRegistry;
import org.chromium.brave_wallet.mojom.BlockchainToken;
@@ -44,7 +43,7 @@ public class BalanceHelper {
@NonNull final JsonRpcService jsonRpcService,
@NonNull final NetworkInfo selectedNetwork,
@NonNull final AccountInfo[] accounts,
@NonNull final Callbacks.Callback2<Integer, HashMap<String, Double>> callback) {
@NonNull final AsyncUtils.Callback2<Integer, HashMap<String, Double>> callback) {
HashMap<String, Double> nativeAssetsBalances = new HashMap<>();
MultiResponseHandler balancesMultiResponse = new MultiResponseHandler(accounts.length);
@@ -114,7 +113,7 @@ public class BalanceHelper {
NetworkInfo selectedNetwork,
AccountInfo[] accountInfos,
BlockchainToken[] tokens,
Callbacks.Callback2<Integer, HashMap<String, HashMap<String, Double>>> callback) {
AsyncUtils.Callback2<Integer, HashMap<String, HashMap<String, Double>>> callback) {
if (jsonRpcService == null) return;
HashMap<String, HashMap<String, Double>> blockchainTokensBalances =
new HashMap<String, HashMap<String, Double>>();
@@ -222,7 +221,7 @@ public class BalanceHelper {
WeakReference<BraveWalletBaseActivity> activityRef,
List<NetworkInfo> allNetworks,
NetworkInfo selectedNetwork,
Callbacks.Callback1<HashMap<Integer, HashSet<String>>> callback) {
AsyncUtils.Callback1<HashMap<Integer, HashSet<String>>> callback) {
BraveWalletBaseActivity activity = activityRef.get();
if (activity == null || activity.isFinishing()) return;
KeyringService keyringService = activity.getKeyringService();
@@ -234,13 +233,16 @@ public class BalanceHelper {
&& keyringService != null
&& jsonRpcService != null;
if (JavaUtils.anyNull(
braveWalletService, blockchainRegistry, keyringService, jsonRpcService)) return;
braveWalletService, blockchainRegistry, keyringService, jsonRpcService)) {
return;
}
boolean P3AEnabled = BraveLocalState.get().getBoolean(BravePref.P3A_ENABLED);
HashMap<Integer, HashSet<String>> activeAddresses = new HashMap<Integer, HashSet<String>>();
for (int coinType : Utils.P3ACoinTypes)
for (int coinType : Utils.P3ACoinTypes) {
activeAddresses.put(coinType, new HashSet<String>());
}
if (!P3AEnabled) {
callback.call(activeAddresses);
@@ -392,16 +394,18 @@ public class BalanceHelper {
HashMap<Integer, HashSet<String>> activeAddresses) {
for (GetNativeAssetsBalancesResponseContext ctx : nativeAssetsBalancesResponses) {
for (Map.Entry<String, Double> nativeEntry : ctx.nativeAssetsBalances.entrySet()) {
if (nativeEntry.getValue() > 0.0d)
if (nativeEntry.getValue() > 0.0d) {
activeAddresses.get(ctx.coinType).add(nativeEntry.getKey());
}
}
}
for (GetBlockchainTokensBalancesResponseContext ctx : blockchainTokensBalancesResponses) {
for (Map.Entry<String, HashMap<String, Double>> accEntry :
ctx.blockchainTokensBalances.entrySet()) {
for (Map.Entry<String, Double> tokenEntry : accEntry.getValue().entrySet()) {
if (tokenEntry.getValue() > 0.0d)
if (tokenEntry.getValue() > 0.0d) {
activeAddresses.get(ctx.coinType).add(accEntry.getKey());
}
}
}
}
@@ -8,7 +8,6 @@ package org.chromium.chrome.browser.crypto_wallet.util;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.chromium.base.Callbacks;
import org.chromium.brave_wallet.mojom.BlockchainRegistry;
import org.chromium.brave_wallet.mojom.BlockchainToken;
import org.chromium.brave_wallet.mojom.BraveWalletService;
@@ -22,6 +21,16 @@ import java.util.List;
import java.util.stream.Stream;
public class TokenUtils {
/**
* A generic 1-argument callback.
*
* @param <T1> The type of the first argument.
*/
public interface Callback1<T1> {
/** Call the callback. */
void call(T1 arg1);
}
/**
* Type of token used for filtering an array of {@code BlockchainToken}.
*
@@ -125,7 +134,7 @@ public class TokenUtils {
NetworkInfo selectedNetwork,
int coinType,
TokenType tokenType,
Callbacks.Callback1<BlockchainToken[]> callback) {
Callback1<BlockchainToken[]> callback) {
braveWalletService.getUserAssets(
selectedNetwork.chainId,
coinType,
@@ -151,7 +160,7 @@ public class TokenUtils {
@Nullable BlockchainRegistry blockchainRegistry,
@Nullable NetworkInfo selectedNetwork,
TokenType tokenType,
Callbacks.Callback1<BlockchainToken[]> callback) {
Callback1<BlockchainToken[]> callback) {
if (braveWalletService == null || blockchainRegistry == null || selectedNetwork == null) {
return;
}
@@ -181,7 +190,7 @@ public class TokenUtils {
int coinType,
TokenType tokenType,
boolean userAssetsOnly,
Callbacks.Callback1<BlockchainToken[]> callback) {
Callback1<BlockchainToken[]> callback) {
if (JavaUtils.anyNull(braveWalletService, blockchainRegistry)) return;
if (userAssetsOnly) {
getVisibleUserAssetsFiltered(
@@ -41,7 +41,6 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Callbacks;
import org.chromium.base.CommandLine;
import org.chromium.base.Log;
import org.chromium.base.shared_preferences.SharedPreferencesManager;
@@ -1124,7 +1123,7 @@ public class Utils {
AccountInfo[] accountInfos,
BlockchainToken[] filterByTokens,
boolean userAssetsOnly,
Callbacks.Callback4<
AsyncUtils.Callback4<
List<AssetPrice>,
BlockchainToken[],
HashMap<String, Double>,
@@ -1222,7 +1221,7 @@ public class Utils {
* @param callback Callback containing a filtered list of P3A networks.
*/
public static void getP3ANetworks(
List<NetworkInfo> allNetworks, Callbacks.Callback1<List<NetworkInfo>> callback) {
List<NetworkInfo> allNetworks, AsyncUtils.Callback1<List<NetworkInfo>> callback) {
ArrayList<NetworkInfo> relevantNetworks = new ArrayList<NetworkInfo>();
boolean countTestNetworks =
CommandLine.getInstance()
@@ -18,7 +18,6 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.chromium.base.Callback;
import org.chromium.base.Callbacks;
import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.brave_wallet.mojom.JsonRpcService;
import org.chromium.brave_wallet.mojom.NetworkInfo;
@@ -36,6 +35,19 @@ import java.util.Arrays;
public class BraveWalletNetworksPreference extends Preference
implements ConnectionErrorHandler, NetworkPreferenceAdapter.ItemClickListener {
/**
* A generic 4-argument callback.
*
* @param <T1> The type of the first argument.
* @param <T2> The type of the second argument.
* @param <T3> The type of the third argument.
* @param <T4> The type of the fourth argument.
*/
public interface Callback4<T1, T2, T3, T4> {
/** Call the callback. */
void call(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}
private AppCompatButton mAddNetwork;
private RecyclerView mRecyclerView;
@Nullable private BraveWalletAddNetworksFragment.Listener mListener;
@@ -211,8 +223,7 @@ public class BraveWalletNetworksPreference extends Preference
*/
private void getAvailableChainIds(
@CoinType.EnumType final int coinType,
@NonNull
final Callbacks.Callback4<String, NetworkInfo[], String[], String[]> callback) {
@NonNull final Callback4<String, NetworkInfo[], String[], String[]> callback) {
mJsonRpcService.getDefaultChainId(
coinType,
defaultChainId ->