Adds a custom network to networks drop down, hides buy/swap for custom networks on Android

This commit is contained in:
Serg
2022-01-11 16:28:14 -05:00
parent 5efd66567b
commit adb5c843f2
10 changed files with 264 additions and 142 deletions
@@ -155,6 +155,13 @@ public class AssetDetailActivity extends AsyncInitializationActivity
BuySendSwapActivity.ActivityType.SWAP, mAssetSymbol);
}
});
if (!Utils.isCustomNetwork(mChainId)) {
btnBuy.setVisibility(View.VISIBLE);
btnSwap.setVisibility(View.VISIBLE);
} else {
btnBuy.setVisibility(View.GONE);
btnSwap.setVisibility(View.GONE);
}
RadioGroup radioGroup = findViewById(R.id.asset_duration_radio_group);
checkedTimeframeType = radioGroup.getCheckedRadioButtonId();
@@ -139,10 +139,14 @@ public class BraveWalletActivity extends AsyncInitializationActivity
}
mSwapButton.setOnClickListener(v -> {
SwapBottomSheetDialogFragment swapBottomSheetDialogFragment =
SwapBottomSheetDialogFragment.newInstance();
swapBottomSheetDialogFragment.show(
getSupportFragmentManager(), SwapBottomSheetDialogFragment.TAG_FRAGMENT);
assert mJsonRpcService != null;
mJsonRpcService.getChainId(chainId -> {
SwapBottomSheetDialogFragment swapBottomSheetDialogFragment =
SwapBottomSheetDialogFragment.newInstance();
swapBottomSheetDialogFragment.setChainId(chainId);
swapBottomSheetDialogFragment.show(
getSupportFragmentManager(), SwapBottomSheetDialogFragment.TAG_FRAGMENT);
});
});
mCryptoLayout = findViewById(R.id.crypto_layout);
@@ -238,8 +238,6 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
fromValueText.setHint("0");
TextView fromBalanceText = findViewById(R.id.from_balance_text);
TextView fromAssetText = findViewById(R.id.from_asset_text);
TextView toBalanceText = findViewById(R.id.to_balance_text);
TextView toAssetText = findViewById(R.id.to_asset_text);
@@ -306,28 +304,31 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.network_spinner) {
String item = parent.getItemAtPosition(position).toString();
final String chainId = Utils.getNetworkConst(this, item);
mJsonRpcService.getAllNetworks(chains -> {
EthereumChain[] customNetworks = Utils.getCustomNetworks(chains);
final String chainId = Utils.getNetworkConst(this, item, customNetworks);
if (mActivityType == ActivityType.BUY) {
adjustTestFaucetControls(getPerNetworkUiInfo(chainId));
}
if (mActivityType == ActivityType.BUY) {
adjustTestFaucetControls(getPerNetworkUiInfo(chainId));
}
if (mJsonRpcService != null) {
mJsonRpcService.setNetwork(chainId, (success) -> {
if (!success) {
Log.e(TAG, "Could not set network");
}
mCurrentChainId = chainId;
});
}
updateBalance(mCustomAccountAdapter.getTitleAtPosition(
mAccountSpinner.getSelectedItemPosition()),
true);
if (mActivityType == ActivityType.SWAP) {
if (mJsonRpcService != null) {
mJsonRpcService.setNetwork(chainId, (success) -> {
if (!success) {
Log.e(TAG, "Could not set network");
}
mCurrentChainId = chainId;
});
}
updateBalance(mCustomAccountAdapter.getTitleAtPosition(
mAccountSpinner.getSelectedItemPosition()),
false);
}
true);
if (mActivityType == ActivityType.SWAP) {
updateBalance(mCustomAccountAdapter.getTitleAtPosition(
mAccountSpinner.getSelectedItemPosition()),
false);
}
});
} else if (parent.getId() == R.id.accounts_spinner) {
updateBalance(mCustomAccountAdapter.getTitleAtPosition(position), true);
if (mActivityType == ActivityType.SWAP) {
@@ -673,8 +674,8 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
}
}
private int getIndexOf(Spinner spinner, String chainId) {
String strNetwork = Utils.getNetworkText(this, chainId).toString();
private int getIndexOf(Spinner spinner, String chainId, EthereumChain[] customNetworks) {
String strNetwork = Utils.getNetworkText(this, chainId, customNetworks).toString();
for (int i = 0; i < spinner.getCount(); i++) {
if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(strNetwork)) {
return i;
@@ -1633,11 +1634,28 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
mCurrentChainId = chainId;
Spinner spinner = findViewById(R.id.network_spinner);
spinner.setOnItemSelectedListener(this);
// Creating adapter for spinner
NetworkSpinnerAdapter dataAdapter = new NetworkSpinnerAdapter(
this, Utils.getNetworksList(this), Utils.getNetworksAbbrevList(this));
spinner.setAdapter(dataAdapter);
spinner.setSelection(getIndexOf(spinner, chainId));
mJsonRpcService.getAllNetworks(chains -> {
EthereumChain[] customNetworks = Utils.getCustomNetworks(chains);
// Creating adapter for spinner
NetworkSpinnerAdapter dataAdapter = new NetworkSpinnerAdapter(this,
Utils.getNetworksList(this, customNetworks),
Utils.getNetworksAbbrevList(this, customNetworks));
spinner.setAdapter(dataAdapter);
spinner.setSelection(getIndexOf(spinner, chainId, customNetworks));
for (EthereumChain chain : chains) {
if (chainId.equals(chain.chainId)) {
TextView fromAssetText = findViewById(R.id.from_asset_text);
if (Utils.isCustomNetwork(chainId)) {
Utils.setBlockiesBitmapCustomAsset(mExecutor, mHandler, null, "",
chain.symbol, getResources().getDisplayMetrics().density,
fromAssetText, this, true, (float) 0.5);
}
fromAssetText.setText(chain.symbol);
break;
}
}
});
});
}
if (mKeyringService != null) {
@@ -35,6 +35,7 @@ import org.chromium.brave_wallet.mojom.BlockchainToken;
import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.EthTxService;
import org.chromium.brave_wallet.mojom.EthereumChain;
import org.chromium.brave_wallet.mojom.JsonRpcService;
import org.chromium.brave_wallet.mojom.TransactionInfo;
import org.chromium.brave_wallet.mojom.TransactionType;
@@ -176,8 +177,12 @@ public class ApproveTxBottomSheetDialogFragment extends BottomSheetDialogFragmen
JsonRpcService jsonRpcService = getJsonRpcService();
assert jsonRpcService != null;
jsonRpcService.getChainId(chainId -> {
TextView networkName = view.findViewById(R.id.network_name);
networkName.setText(Utils.getNetworkText(getActivity(), chainId).toString());
jsonRpcService.getAllNetworks(chains -> {
EthereumChain[] customNetworks = Utils.getCustomNetworks(chains);
TextView networkName = view.findViewById(R.id.network_name);
networkName.setText(
Utils.getNetworkText(getActivity(), chainId, customNetworks).toString());
});
TextView txType = view.findViewById(R.id.tx_type);
txType.setText(getResources().getString(R.string.send));
if (mTxInfo.txType == TransactionType.ERC20_TRANSFER
@@ -351,17 +351,19 @@ public class EditVisibleAssetsBottomSheetDialogFragment extends BottomSheetDialo
walletCoinAdapter = new WalletCoinAdapter(mType);
List<WalletListItemModel> walletListItemModelList = new ArrayList<>();
String tokensPath = BlockchainRegistryFactory.getInstance().getTokensIconsLocation();
// Add ETH as a first item always
BlockchainToken eth = Utils.createEthereumBlockchainToken();
WalletListItemModel itemModelEth =
new WalletListItemModel(R.drawable.ic_eth, eth.name, eth.symbol, "", "");
itemModelEth.setIsUserSelected(
selectedTokensSymbols.contains(eth.symbol.toUpperCase(Locale.getDefault())));
itemModelEth.setIconPath("file://" + tokensPath + "/" + eth.logo);
itemModelEth.setBlockchainToken(eth);
walletListItemModelList.add(itemModelEth);
if (!Utils.isCustomNetwork(mChainId)) {
// Add ETH as a first item always
BlockchainToken eth = Utils.createEthereumBlockchainToken();
WalletListItemModel itemModelEth =
new WalletListItemModel(R.drawable.ic_eth, eth.name, eth.symbol, "", "");
itemModelEth.setIsUserSelected(
selectedTokensSymbols.contains(eth.symbol.toUpperCase(Locale.getDefault())));
itemModelEth.setIconPath("file://" + tokensPath + "/" + eth.logo);
itemModelEth.setBlockchainToken(eth);
walletListItemModelList.add(itemModelEth);
}
for (int i = 0; i < tokens.length; i++) {
if (tokens[i].symbol.equals("ETH")) {
if (tokens[i].symbol.equals("ETH") && !Utils.isCustomNetwork(mChainId)) {
// We have added ETH already
continue;
}
@@ -36,6 +36,7 @@ import org.chromium.brave_wallet.mojom.AssetRatioService;
import org.chromium.brave_wallet.mojom.BlockchainToken;
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.EthTxService;
import org.chromium.brave_wallet.mojom.EthereumChain;
import org.chromium.brave_wallet.mojom.JsonRpcService;
import org.chromium.brave_wallet.mojom.KeyringService;
import org.chromium.brave_wallet.mojom.TransactionInfo;
@@ -140,26 +141,34 @@ public class PortfolioFragment extends Fragment
});
// Creating adapter for spinner
NetworkSpinnerAdapter dataAdapter = new NetworkSpinnerAdapter(getActivity(),
Utils.getNetworksList(getActivity()), Utils.getNetworksAbbrevList(getActivity()));
mSpinner.setAdapter(dataAdapter);
updateNetwork();
JsonRpcService jsonRpcService = getJsonRpcService();
assert jsonRpcService != null;
jsonRpcService.getAllNetworks(chains -> {
EthereumChain[] customNetworks = Utils.getCustomNetworks(chains);
NetworkSpinnerAdapter dataAdapter = new NetworkSpinnerAdapter(getActivity(),
Utils.getNetworksList(getActivity(), customNetworks),
Utils.getNetworksAbbrevList(getActivity(), customNetworks));
mSpinner.setAdapter(dataAdapter);
updateNetwork();
});
return view;
}
private void updateNetwork() {
JsonRpcService jsonRpcService = getJsonRpcService();
if (jsonRpcService != null) {
jsonRpcService.getChainId(chain_id -> {
assert jsonRpcService != null;
jsonRpcService.getChainId(chain_id -> {
jsonRpcService.getAllNetworks(chains -> {
EthereumChain[] customNetworks = Utils.getCustomNetworks(chains);
String chainName = mSpinner.getSelectedItem().toString();
String chainId = Utils.getNetworkConst(getActivity(), chainName);
String chainId = Utils.getNetworkConst(getActivity(), chainName, customNetworks);
if (chainId.equals(chain_id)) {
return;
}
mSpinner.setSelection(getIndexOf(mSpinner, chain_id));
mSpinner.setSelection(getIndexOf(mSpinner, chain_id, customNetworks));
});
}
});
}
@Override
@@ -168,8 +177,9 @@ public class PortfolioFragment extends Fragment
updateNetwork();
}
private int getIndexOf(Spinner spinner, String chain_id) {
String strNetwork = Utils.getNetworkText(getActivity(), chain_id).toString();
private int getIndexOf(Spinner spinner, String chain_id, EthereumChain[] customNetworks) {
String strNetwork =
Utils.getNetworkText(getActivity(), chain_id, customNetworks).toString();
for (int i = 0; i < spinner.getCount(); i++) {
if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(strNetwork)) {
return i;
@@ -184,12 +194,16 @@ public class PortfolioFragment extends Fragment
String item = parent.getItemAtPosition(position).toString();
JsonRpcService jsonRpcService = getJsonRpcService();
if (jsonRpcService != null) {
jsonRpcService.setNetwork(Utils.getNetworkConst(getActivity(), item), (success) -> {
if (!success) {
Log.e(TAG, "Could not set network");
return;
}
updatePortfolioGetPendingTx(true);
jsonRpcService.getAllNetworks(chains -> {
EthereumChain[] customNetworks = Utils.getCustomNetworks(chains);
jsonRpcService.setNetwork(
Utils.getNetworkConst(getActivity(), item, customNetworks), (success) -> {
if (!success) {
Log.e(TAG, "Could not set network");
return;
}
updatePortfolioGetPendingTx(true);
});
});
}
}
@@ -204,26 +218,31 @@ public class PortfolioFragment extends Fragment
TextView editVisibleAssets = view.findViewById(R.id.edit_visible_assets);
editVisibleAssets.setOnClickListener(v -> {
String chainName = mSpinner.getSelectedItem().toString();
String chainId = Utils.getNetworkConst(getActivity(), chainName);
JsonRpcService jsonRpcService = getJsonRpcService();
assert jsonRpcService != null;
jsonRpcService.getAllNetworks(chains -> {
String chainName = mSpinner.getSelectedItem().toString();
EthereumChain[] customNetworks = Utils.getCustomNetworks(chains);
String chainId = Utils.getNetworkConst(getActivity(), chainName, customNetworks);
EditVisibleAssetsBottomSheetDialogFragment bottomSheetDialogFragment =
EditVisibleAssetsBottomSheetDialogFragment.newInstance(
WalletCoinAdapter.AdapterType.EDIT_VISIBLE_ASSETS_LIST);
EditVisibleAssetsBottomSheetDialogFragment bottomSheetDialogFragment =
EditVisibleAssetsBottomSheetDialogFragment.newInstance(
WalletCoinAdapter.AdapterType.EDIT_VISIBLE_ASSETS_LIST);
bottomSheetDialogFragment.setChainId(chainId);
bottomSheetDialogFragment.setDismissListener(
new EditVisibleAssetsBottomSheetDialogFragment.DismissListener() {
@Override
public void onDismiss(Boolean isAssetsListChanged) {
if (isAssetsListChanged != null && isAssetsListChanged) {
updatePortfolioGetPendingTx(false);
bottomSheetDialogFragment.setChainId(chainId);
bottomSheetDialogFragment.setDismissListener(
new EditVisibleAssetsBottomSheetDialogFragment.DismissListener() {
@Override
public void onDismiss(Boolean isAssetsListChanged) {
if (isAssetsListChanged != null && isAssetsListChanged) {
updatePortfolioGetPendingTx(false);
}
}
}
});
});
bottomSheetDialogFragment.show(
getFragmentManager(), EditVisibleAssetsBottomSheetDialogFragment.TAG_FRAGMENT);
bottomSheetDialogFragment.show(getFragmentManager(),
EditVisibleAssetsBottomSheetDialogFragment.TAG_FRAGMENT);
});
});
RadioGroup radioGroup = view.findViewById(R.id.portfolio_duration_radio_group);
@@ -280,10 +299,15 @@ public class PortfolioFragment extends Fragment
@Override
public void onAssetClick(BlockchainToken asset) {
String chainName = mSpinner.getSelectedItem().toString();
String chainId = Utils.getNetworkConst(getActivity(), chainName);
Utils.openAssetDetailsActivity(getActivity(), chainId, asset.symbol, asset.name,
asset.contractAddress, asset.logo, asset.decimals);
JsonRpcService jsonRpcService = getJsonRpcService();
assert jsonRpcService != null;
jsonRpcService.getAllNetworks(chains -> {
EthereumChain[] customNetworks = Utils.getCustomNetworks(chains);
String chainName = mSpinner.getSelectedItem().toString();
String chainId = Utils.getNetworkConst(getActivity(), chainName, customNetworks);
Utils.openAssetDetailsActivity(getActivity(), chainId, asset.symbol, asset.name,
asset.contractAddress, asset.logo, asset.decimals);
});
}
private AssetRatioService getAssetRatioService() {
@@ -372,37 +396,41 @@ public class PortfolioFragment extends Fragment
KeyringService keyringService = getKeyringService();
assert keyringService != null;
keyringService.getDefaultKeyringInfo(keyringInfo -> {
AccountInfo[] accountInfos = new AccountInfo[] {};
if (keyringInfo != null) {
accountInfos = keyringInfo.accountInfos;
}
JsonRpcService jsonRpcService = getJsonRpcService();
assert jsonRpcService != null;
jsonRpcService.getAllNetworks(chains -> {
AccountInfo[] accountInfos = new AccountInfo[] {};
if (keyringInfo != null) {
accountInfos = keyringInfo.accountInfos;
}
if (mPortfolioHelper == null) {
mPortfolioHelper = new PortfolioHelper(getBraveWalletService(),
getAssetRatioService(), getJsonRpcService(), accountInfos);
}
if (mPortfolioHelper == null) {
mPortfolioHelper = new PortfolioHelper(getBraveWalletService(),
getAssetRatioService(), getJsonRpcService(), accountInfos);
}
EthereumChain[] customNetworks = Utils.getCustomNetworks(chains);
String chainName = mSpinner.getSelectedItem().toString();
String chainId = Utils.getNetworkConst(getActivity(), chainName, customNetworks);
mPortfolioHelper.setChainId(chainId);
mPortfolioHelper.calculateBalances(() -> {
final String fiatSumString = String.format(
Locale.getDefault(), "$%,.2f", mPortfolioHelper.getTotalFiatSum());
PostTask.runOrPostTask(UiThreadTaskTraits.DEFAULT, () -> {
mFiatSumString = fiatSumString;
mBalance.setText(mFiatSumString);
mBalance.invalidate();
String chainName = mSpinner.getSelectedItem().toString();
String chainId = Utils.getNetworkConst(getActivity(), chainName);
mPortfolioHelper.setChainId(chainId);
mPortfolioHelper.calculateBalances(() -> {
final String fiatSumString = String.format(
Locale.getDefault(), "$%,.2f", mPortfolioHelper.getTotalFiatSum());
PostTask.runOrPostTask(UiThreadTaskTraits.DEFAULT, () -> {
mFiatSumString = fiatSumString;
mBalance.setText(mFiatSumString);
mBalance.invalidate();
setUpCoinList(mPortfolioHelper.getUserAssets(),
mPortfolioHelper.getPerTokenCryptoSum(),
mPortfolioHelper.getPerTokenFiatSum());
setUpCoinList(mPortfolioHelper.getUserAssets(),
mPortfolioHelper.getPerTokenCryptoSum(),
mPortfolioHelper.getPerTokenFiatSum());
updatePortfolioGraph();
updatePortfolioGraph();
});
});
if (getPendingTx) {
getPendingTx(accountInfos);
}
});
if (getPendingTx) {
getPendingTx(accountInfos);
}
});
}
@@ -30,11 +30,16 @@ public class SwapBottomSheetDialogFragment
LinearLayout mBuyLayout;
LinearLayout mSendLayout;
LinearLayout mSwapLayout;
private String mChainId;
public static SwapBottomSheetDialogFragment newInstance() {
return new SwapBottomSheetDialogFragment();
}
public void setChainId(String chainId) {
mChainId = chainId;
}
@Override
public void show(FragmentManager manager, String tag) {
try {
@@ -66,6 +71,14 @@ public class SwapBottomSheetDialogFragment
mSendLayout.setOnClickListener(this);
mSwapLayout = view.findViewById(R.id.swap_layout);
mSwapLayout.setOnClickListener(this);
assert mChainId != null;
if (!Utils.isCustomNetwork(mChainId)) {
mBuyLayout.setVisibility(View.VISIBLE);
mSwapLayout.setVisibility(View.VISIBLE);
} else {
mBuyLayout.setVisibility(View.GONE);
mSwapLayout.setVisibility(View.GONE);
}
dialog.setContentView(view);
ViewParent parent = view.getParent();
@@ -220,7 +220,45 @@ public class Utils {
activity.startActivity(assetDetailIntent);
}
public static String[] getNetworksList(Activity activity) {
public static EthereumChain[] getCustomNetworks(EthereumChain[] chains) {
List<EthereumChain> al = new ArrayList<EthereumChain>();
for (EthereumChain chain : chains) {
switch (chain.chainId) {
case BraveWalletConstants.RINKEBY_CHAIN_ID:
case BraveWalletConstants.ROPSTEN_CHAIN_ID:
case BraveWalletConstants.GOERLI_CHAIN_ID:
case BraveWalletConstants.KOVAN_CHAIN_ID:
case BraveWalletConstants.LOCALHOST_CHAIN_ID:
case BraveWalletConstants.MAINNET_CHAIN_ID:
continue;
default:
al.add(chain);
}
}
EthereumChain[] arr = new EthereumChain[al.size()];
arr = al.toArray(arr);
return arr;
}
public static boolean isCustomNetwork(String chainId) {
switch (chainId) {
case BraveWalletConstants.RINKEBY_CHAIN_ID:
case BraveWalletConstants.ROPSTEN_CHAIN_ID:
case BraveWalletConstants.GOERLI_CHAIN_ID:
case BraveWalletConstants.KOVAN_CHAIN_ID:
case BraveWalletConstants.LOCALHOST_CHAIN_ID:
case BraveWalletConstants.MAINNET_CHAIN_ID:
return false;
default:
break;
}
return true;
}
public static String[] getNetworksList(Activity activity, EthereumChain[] customNetworks) {
List<String> categories = new ArrayList<String>();
categories.add(activity.getText(R.string.mainnet).toString());
categories.add(activity.getText(R.string.rinkeby).toString());
@@ -231,11 +269,15 @@ public class Utils {
if (0 != (activity.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) {
categories.add(activity.getText(R.string.localhost).toString());
}
for (EthereumChain chain : customNetworks) {
categories.add(chain.chainName);
}
return categories.toArray(new String[0]);
}
public static String[] getNetworksAbbrevList(Activity activity) {
public static String[] getNetworksAbbrevList(
Activity activity, EthereumChain[] customNetworks) {
List<String> categories = new ArrayList<String>();
categories.add(activity.getText(R.string.mainnet_short).toString());
categories.add(activity.getText(R.string.rinkeby_short).toString());
@@ -246,11 +288,15 @@ public class Utils {
if (0 != (activity.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) {
categories.add(activity.getText(R.string.localhost).toString());
}
for (EthereumChain chain : customNetworks) {
categories.add(chain.chainName);
}
return categories.toArray(new String[0]);
}
public static CharSequence getNetworkText(Activity activity, String chain_id) {
public static CharSequence getNetworkText(
Activity activity, String chain_id, EthereumChain[] customNetworks) {
CharSequence strNetwork = activity.getText(R.string.mainnet);
switch (chain_id) {
case BraveWalletConstants.RINKEBY_CHAIN_ID:
@@ -269,8 +315,15 @@ public class Utils {
strNetwork = activity.getText(R.string.localhost);
break;
case BraveWalletConstants.MAINNET_CHAIN_ID:
default:
strNetwork = activity.getText(R.string.mainnet);
break;
default:
for (EthereumChain chain : customNetworks) {
if (chain_id.equals(chain.chainId)) {
strNetwork = chain.chainName;
break;
}
}
}
return strNetwork;
@@ -318,18 +371,25 @@ public class Utils {
}
}
public static String getNetworkConst(Activity activity, String network) {
public static String getNetworkConst(
Activity activity, String network, EthereumChain[] customNetworks) {
String networkConst = BraveWalletConstants.MAINNET_CHAIN_ID;
if (network.equals(activity.getText(R.string.rinkeby).toString())) {
networkConst = BraveWalletConstants.RINKEBY_CHAIN_ID;
return BraveWalletConstants.RINKEBY_CHAIN_ID;
} else if (network.equals(activity.getText(R.string.ropsten).toString())) {
networkConst = BraveWalletConstants.ROPSTEN_CHAIN_ID;
return BraveWalletConstants.ROPSTEN_CHAIN_ID;
} else if (network.equals(activity.getText(R.string.goerli).toString())) {
networkConst = BraveWalletConstants.GOERLI_CHAIN_ID;
return BraveWalletConstants.GOERLI_CHAIN_ID;
} else if (network.equals(activity.getText(R.string.kovan).toString())) {
networkConst = BraveWalletConstants.KOVAN_CHAIN_ID;
return BraveWalletConstants.KOVAN_CHAIN_ID;
} else if (network.equals(activity.getText(R.string.localhost).toString())) {
networkConst = BraveWalletConstants.LOCALHOST_CHAIN_ID;
return BraveWalletConstants.LOCALHOST_CHAIN_ID;
}
for (EthereumChain chain : customNetworks) {
if (network.equals(chain.chainName)) {
return chain.chainId;
}
}
return networkConst;
@@ -19,6 +19,7 @@ import org.chromium.brave_wallet.mojom.EthereumChain;
import org.chromium.brave_wallet.mojom.JsonRpcService;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.crypto_wallet.JsonRpcServiceFactory;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.components.browser_ui.widget.TintedDrawable;
import org.chromium.mojo.bindings.ConnectionErrorHandler;
import org.chromium.mojo.system.MojoException;
@@ -62,9 +63,7 @@ public class BraveWalletNetworksPreference extends Preference
updateNetworksList();
}
@Override
protected void onPrepareForRemoval() {
super.onPrepareForRemoval();
public void destroy() {
mJsonRpcService.close();
}
@@ -123,33 +122,11 @@ public class BraveWalletNetworksPreference extends Preference
assert mJsonRpcService != null;
mJsonRpcService.getChainId(chainId -> {
mJsonRpcService.getAllNetworks(chains -> {
mAdapter.setDisplayedNetworks(chainId, getCustomNetworks(chains));
mAdapter.setDisplayedNetworks(chainId, Utils.getCustomNetworks(chains));
if (mRecyclerView.getAdapter() != mAdapter) {
mRecyclerView.setAdapter(mAdapter);
}
});
});
}
private EthereumChain[] getCustomNetworks(EthereumChain[] chains) {
List<EthereumChain> al = new ArrayList<EthereumChain>();
for (EthereumChain chain : chains) {
switch (chain.chainId) {
case BraveWalletConstants.RINKEBY_CHAIN_ID:
case BraveWalletConstants.ROPSTEN_CHAIN_ID:
case BraveWalletConstants.GOERLI_CHAIN_ID:
case BraveWalletConstants.KOVAN_CHAIN_ID:
case BraveWalletConstants.LOCALHOST_CHAIN_ID:
case BraveWalletConstants.MAINNET_CHAIN_ID:
continue;
default:
al.add(chain);
}
}
EthereumChain[] arr = new EthereumChain[al.size()];
arr = al.toArray(arr);
return arr;
}
}
@@ -35,6 +35,14 @@ public class BraveWalletNetworksPreferenceFragment extends BravePreferenceFragme
mNetworksAddPref.registerActivityLauncher(this);
}
@Override
public void onDestroy() {
super.onDestroy();
BraveWalletNetworksPreference mNetworksAddPref =
(BraveWalletNetworksPreference) findPreference(PREF_BRAVE_WALLET_NETWORKS_ADD);
mNetworksAddPref.destroy();
}
@Override
public void setSettingsLauncher(SettingsLauncher settingsLauncher) {
mSettingsLauncher = settingsLauncher;