Merge pull request #13842 from brave/fix-empty-account

fix(wallet):dapps panel auto close
This commit is contained in:
Pavneet Singh
2022-06-17 21:44:25 +05:30
committed by GitHub
3 changed files with 80 additions and 40 deletions
@@ -5,27 +5,36 @@
package org.chromium.chrome.browser.app.domain;
import androidx.annotation.UiThread;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.KeyringInfo;
import org.chromium.brave_wallet.mojom.KeyringService;
import org.chromium.brave_wallet.mojom.KeyringServiceObserver;
import org.chromium.chrome.browser.crypto_wallet.util.AccountsPermissionsHelper;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.mojo.system.MojoException;
import java.util.HashSet;
public class KeyringModel implements KeyringServiceObserver {
private KeyringService mKeyringService;
private BraveWalletService mBraveWalletService;
private MutableLiveData<KeyringInfo> _mKeyringInfoLiveData;
public LiveData<KeyringInfo> mKeyringInfoLiveData;
private final MutableLiveData<AccountInfo> _mSelectedAccount;
public LiveData<AccountInfo> mSelectedAccount;
private CryptoSharedData mSharedData;
// Todo: create method to interact with keyring
private AccountsPermissionsHelper mAccountsPermissionsHelper;
public KeyringModel(KeyringService mKeyringService, CryptoSharedData sharedData) {
this.mKeyringService = mKeyringService;
public KeyringModel(KeyringService keyringService, CryptoSharedData sharedData,
BraveWalletService braveWalletService) {
mKeyringService = keyringService;
mBraveWalletService = braveWalletService;
mSharedData = sharedData;
_mKeyringInfoLiveData = new MutableLiveData<>(null);
mKeyringInfoLiveData = _mKeyringInfoLiveData;
@@ -40,22 +49,62 @@ public class KeyringModel implements KeyringServiceObserver {
private void update() {
mKeyringService.getKeyringInfo(BraveWalletConstants.DEFAULT_KEYRING_ID, keyringInfo -> {
_mKeyringInfoLiveData.postValue(keyringInfo);
Integer coinType = mSharedData.getCoinType();
mKeyringService.getSelectedAccount(coinType, accountAddress -> {
if (keyringInfo.accountInfos.length > 0) {
AccountInfo selectedAccount = keyringInfo.accountInfos[0];
mKeyringService.getSelectedAccount(mSharedData.getCoinType(), accountAddress -> {
if (accountAddress != null && !accountAddress.isEmpty()) {
AccountInfo selectedAccountInfo = null;
for (AccountInfo accountInfo : keyringInfo.accountInfos) {
if (accountInfo.address.equals(accountAddress)) {
selectedAccount = accountInfo;
selectedAccountInfo = accountInfo;
break;
}
}
_mSelectedAccount.postValue(selectedAccount);
_mSelectedAccount.postValue(selectedAccountInfo);
} else if (keyringInfo.accountInfos.length > 0) {
_mSelectedAccount.postValue(keyringInfo.accountInfos[0]);
}
});
});
}
private void updateSelectedAccountPerOriginOrFirst(KeyringInfo keyringInfo) {
mAccountsPermissionsHelper = new AccountsPermissionsHelper(
mBraveWalletService, keyringInfo.accountInfos, Utils.getCurrentMojomOrigin());
mAccountsPermissionsHelper.checkAccounts(() -> {
String selectedAccountAddress = null;
HashSet<AccountInfo> permissionAccounts =
mAccountsPermissionsHelper.getAccountsWithPermissions();
if (!permissionAccounts.isEmpty()) {
selectedAccountAddress = permissionAccounts.iterator().next().address;
} else if (keyringInfo.accountInfos.length > 0) {
selectedAccountAddress = keyringInfo.accountInfos[0].address;
}
if (selectedAccountAddress != null) {
setSelectedAccount(selectedAccountAddress, mSharedData.getCoinType());
}
});
}
/**
* Enforce to fetch and use the first permitted account if there is no selected account in
* Keyring service
*
* @return mSelectedAccount live data to get the selected account
*/
@UiThread
public LiveData<AccountInfo> getSelectedAccountOrAccountPerOrigin() {
_mSelectedAccount.setValue(null);
mKeyringService.getSelectedAccount(mSharedData.getCoinType(), accountAddress -> {
if (accountAddress == null) {
mKeyringService.getKeyringInfo(BraveWalletConstants.DEFAULT_KEYRING_ID,
keyringInfo -> { updateSelectedAccountPerOriginOrFirst(keyringInfo); });
} else {
update();
}
});
return mSelectedAccount;
}
public void setSelectedAccount(String accountAddress, int coin) {
mKeyringService.setSelectedAccount(accountAddress, coin, isAccountSelected -> {});
}
@@ -64,11 +113,13 @@ public class KeyringModel implements KeyringServiceObserver {
return _mKeyringInfoLiveData.getValue();
}
public void resetService(KeyringService keyringService) {
if (mKeyringService == keyringService) {
return;
public void resetService(KeyringService keyringService, BraveWalletService braveWalletService) {
if (mKeyringService != keyringService) {
mKeyringService = keyringService;
}
if (mBraveWalletService != braveWalletService) {
mBraveWalletService = braveWalletService;
}
this.mKeyringService = keyringService;
init();
}
@@ -45,7 +45,8 @@ public class WalletModel {
mJsonRpcService, mEthTxManagerProxy, mSolanaTxManagerProxy, mBraveWalletService,
mAssetRatioService);
mDappsModel = new DappsModel(mJsonRpcService, mBraveWalletService, mCryptoModel.getPendingTxHelper());
mKeyringModel = new KeyringModel(keyringService, mCryptoModel.getSharedData());
mKeyringModel =
new KeyringModel(keyringService, mCryptoModel.getSharedData(), braveWalletService);
init();
}
@@ -65,7 +66,7 @@ public class WalletModel {
mJsonRpcService, mEthTxManagerProxy, mSolanaTxManagerProxy, mBraveWalletService,
mAssetRatioService);
mDappsModel.resetServices(mJsonRpcService, mBraveWalletService, mCryptoModel.getPendingTxHelper());
mKeyringModel.resetService(mKeyringService);
mKeyringModel.resetService(mKeyringService, braveWalletService);
init();
}
@@ -23,15 +23,11 @@ import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.brave_wallet.mojom.JsonRpcService;
import org.chromium.brave_wallet.mojom.KeyringService;
import org.chromium.brave_wallet.mojom.NetworkInfo;
import org.chromium.brave_wallet.mojom.SignMessageRequest;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.crypto_wallet.adapters.SignMessagePagerAdapter;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.url.GURL;
@@ -127,27 +123,19 @@ public class SignMessageFragment extends BaseDAppsBottomSheetDialogFragment {
}
private void updateAccount() {
getKeyringService().getSelectedAccount(CoinType.ETH, address -> {
if (address == null) {
getActivity().finish();
return;
}
getKeyringService().getKeyringInfo(
BraveWalletConstants.DEFAULT_KEYRING_ID, keyringInfo -> {
if (keyringInfo == null) {
return;
}
for (AccountInfo accountInfo : keyringInfo.accountInfos) {
if (address.equals(accountInfo.address)) {
Utils.setBlockiesBitmapResource(
mExecutor, mHandler, mAccountImage, address, true);
String accountText = accountInfo.name + "\n" + address;
mAccountName.setText(accountText);
break;
}
}
BraveActivity activity = BraveActivity.getBraveActivity();
if (activity != null) {
activity.getWalletModel()
.getKeyringModel()
.getSelectedAccountOrAccountPerOrigin()
.observe(getViewLifecycleOwner(), accountInfo -> {
if (accountInfo == null) return;
Utils.setBlockiesBitmapResource(
mExecutor, mHandler, mAccountImage, accountInfo.address, true);
String accountText = accountInfo.name + "\n" + accountInfo.address;
mAccountName.setText(accountText);
});
});
}
}
private void updateNetwork() {