Skip to content

Commit

Permalink
Fixes an intermittent crash on Android on a wallet creation (uplift t…
Browse files Browse the repository at this point in the history
…o 1.48.x) (#16741)
  • Loading branch information
brave-builds authored Jan 20, 2023
1 parent aa7d88f commit b57fe2a
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 12 deletions.
12 changes: 10 additions & 2 deletions android/java/org/chromium/chrome/browser/app/BraveActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public boolean onMenuOrKeyboardAction(int id, boolean fromMenu) {
} else if (id == R.id.brave_rewards_id) {
openNewOrSelectExistingTab(BRAVE_REWARDS_SETTINGS_URL);
} else if (id == R.id.brave_wallet_id) {
openBraveWallet(false);
openBraveWallet(false, false, false);
} else if (id == R.id.brave_news_id) {
openBraveNewsSettings();
} else if (id == R.id.request_brave_vpn_id || id == R.id.request_brave_vpn_check_id) {
Expand Down Expand Up @@ -744,6 +744,12 @@ public void initializeState() {
setComesFromNewTab(false);
setNewsItemsFeedCards(null);
BraveSearchEngineUtils.initializeBraveSearchEngineStates(getTabModelSelector());
Intent intent = getIntent();
if (intent != null && intent.getBooleanExtra(Utils.RESTART_WALLET_ACTIVITY, false)) {
openBraveWallet(false,
intent.getBooleanExtra(Utils.RESTART_WALLET_ACTIVITY_SETUP, false),
intent.getBooleanExtra(Utils.RESTART_WALLET_ACTIVITY_RESTORE, false));
}
}

public int getLastTabId() {
Expand Down Expand Up @@ -1264,9 +1270,11 @@ public void openBraveConnectedSitesSettings() {
settingsLauncher.launchSettingsActivity(this, BraveWalletEthereumConnectedSites.class);
}

public void openBraveWallet(boolean fromDapp) {
public void openBraveWallet(boolean fromDapp, boolean setupAction, boolean restoreAction) {
Intent braveWalletIntent = new Intent(this, BraveWalletActivity.class);
braveWalletIntent.putExtra(Utils.IS_FROM_DAPPS, fromDapp);
braveWalletIntent.putExtra(Utils.RESTART_WALLET_ACTIVITY_SETUP, setupAction);
braveWalletIntent.putExtra(Utils.RESTART_WALLET_ACTIVITY_RESTORE, restoreAction);
braveWalletIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(braveWalletIntent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ private void update(int coinType) {
}

void update() {
mBraveWalletService.getSelectedCoin(coinType -> { update(coinType); });
if (mBraveWalletService != null) {
mBraveWalletService.getSelectedCoin(coinType -> { update(coinType); });
}
}

private void updateSelectedAccountPerOriginOrFirst(KeyringInfo keyringInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,17 @@ public void updateCoinType() {
*/
@Override
public void updateCoinAccountNetworkInfo(@CoinType.EnumType int coin) {
// update coin
if (mBraveWalletService == null) {
return;
}
// Update coin
mBraveWalletService.setSelectedCoin(coin);
mCryptoModel.updateCoinType(coin, isCoinUpdated -> {
// update account per selected coin
// Update account per selected coin
mKeyringModel.update();
// update network per selected coin
// Update network per selected coin
mCryptoModel.getNetworkModel().init();
// update transactions
// Update transactions
mCryptoModel.refreshTransactions();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class BraveWalletActivity extends BraveWalletBaseActivity implements OnNe
private boolean mShowBiometricPrompt;
private boolean mIsFromDapps;
private WalletModel mWalletModel;
private boolean mRestartSetupAction;
private boolean mRestartRestoreAction;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand Down Expand Up @@ -107,6 +109,10 @@ protected void triggerLayoutInflation() {
mIsFromDapps = false;
if (getIntent() != null) {
mIsFromDapps = getIntent().getBooleanExtra(Utils.IS_FROM_DAPPS, false);
mRestartSetupAction =
getIntent().getBooleanExtra(Utils.RESTART_WALLET_ACTIVITY_SETUP, false);
mRestartRestoreAction =
getIntent().getBooleanExtra(Utils.RESTART_WALLET_ACTIVITY_RESTORE, false);
}
mShowBiometricPrompt = true;
mToolbar = findViewById(R.id.toolbar);
Expand Down Expand Up @@ -242,7 +248,8 @@ private void setNavigationFragments(int type) {
mSwapButton.setVisibility(View.GONE);
mCryptoOnboardingLayout.setVisibility(View.VISIBLE);
if (type == ONBOARDING_FIRST_PAGE_ACTION) {
SetupWalletFragment setupWalletFragment = new SetupWalletFragment();
SetupWalletFragment setupWalletFragment =
new SetupWalletFragment(mRestartSetupAction, mRestartRestoreAction);
setupWalletFragment.setOnNextPageListener(this);
navigationItems.add(new NavigationItem(
getResources().getString(R.string.setup_crypto), setupWalletFragment));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
private void setUpAccountList(View view) {
RecyclerView rvAccounts = view.findViewById(R.id.rv_accounts);
walletCoinAdapter = new WalletCoinAdapter(WalletCoinAdapter.AdapterType.ACCOUNTS_LIST);
if (mWalletModel == null) {
return;
}
mWalletModel.getKeyringModel().mAccountInfos.observe(
getViewLifecycleOwner(), accountInfos -> {
List<WalletListItemModel> walletListItemModelList = new ArrayList<>();
Expand All @@ -121,6 +124,9 @@ private void setUpAccountList(View view) {
}

private void setUpSecondaryAccountList(View view) {
if (mWalletModel == null) {
return;
}
RecyclerView rvSecondaryAccounts = view.findViewById(R.id.rv_secondary_accounts);
WalletCoinAdapter walletCoinAdapter =
new WalletCoinAdapter(WalletCoinAdapter.AdapterType.ACCOUNTS_LIST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.chromium.chrome.browser.crypto_wallet.fragments.onboarding_fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -15,9 +16,22 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.chromium.base.task.PostTask;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.content_public.browser.UiThreadTaskTraits;

public class SetupWalletFragment extends CryptoOnboardingFragment {
private boolean mRestartSetupAction;
private boolean mRestartRestoreAction;

public SetupWalletFragment(boolean restartSetupAction, boolean restartRestoreAction) {
mRestartSetupAction = restartSetupAction;
mRestartRestoreAction = restartRestoreAction;
}

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Expand All @@ -28,9 +42,40 @@ public View onCreateView(
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button setupCryptoButton = view.findViewById(R.id.btn_setup_crypto);
setupCryptoButton.setOnClickListener(v -> onNextPage.gotoOnboardingPage());
setupCryptoButton.setOnClickListener(v -> {
checkOnBraveActivity(true, false);
onNextPage.gotoOnboardingPage();
});

TextView restoreButton = view.findViewById(R.id.btn_restore);
restoreButton.setOnClickListener(v -> onNextPage.gotoRestorePage(true));
restoreButton.setOnClickListener(v -> {
checkOnBraveActivity(false, true);
onNextPage.gotoRestorePage(true);
});
PostTask.postTask(UiThreadTaskTraits.DEFAULT, () -> {
if (mRestartSetupAction) {
setupCryptoButton.performClick();
} else if (mRestartRestoreAction) {
restoreButton.performClick();
}
mRestartSetupAction = false;
mRestartRestoreAction = false;
});
}

// We need to remove that check and restart once
// https://github.com/brave/brave-browser/issues/27887
// is done.
private void checkOnBraveActivity(boolean setupAction, boolean restoreAction) {
BraveActivity activity = BraveActivity.getBraveActivity();
if (activity == null) {
Intent intent = new Intent(getActivity(), ChromeTabbedActivity.class);
intent.putExtra(Utils.RESTART_WALLET_ACTIVITY, true);
intent.putExtra(Utils.RESTART_WALLET_ACTIVITY_SETUP, setupAction);
intent.putExtra(Utils.RESTART_WALLET_ACTIVITY_RESTORE, restoreAction);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
getActivity().finish();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ private void setUpViews() {
dismiss();
BraveActivity activity = BraveActivity.getBraveActivity();
if (activity != null) {
activity.openBraveWallet(false);
activity.openBraveWallet(false, false, false);
}
});
mOptionsImage = mPopupView.findViewById(R.id.iv_dapp_panel_menu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
private void openWallet() {
BraveActivity activity = BraveActivity.getBraveActivity();
assert activity != null;
activity.openBraveWallet(true);
activity.openBraveWallet(true, false, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ public class Utils {
public static final String ASSET_DECIMALS = "assetDecimals";
public static final String CHAIN_ID = "chainId";
public static final String IS_FROM_DAPPS = "isFromDapps";
public static final String RESTART_WALLET_ACTIVITY = "restartWalletActivity";
public static final String RESTART_WALLET_ACTIVITY_SETUP = "restartWalletActivitySetup";
public static final String RESTART_WALLET_ACTIVITY_RESTORE = "restartWalletActivityRestore";
public static final String ETHEREUM_CONTRACT_FOR_SWAP =
"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
public static final BigInteger MAX_UINT256 =
Expand Down

0 comments on commit b57fe2a

Please sign in to comment.