Skip to content

Commit

Permalink
(Wallet) Implement WebUI for main portfolio screen (#20635)
Browse files Browse the repository at this point in the history
Implements WebUI to show Android Wallet except for these parts that will remain native:

- Onboarding
- Authentication
- Transaction confirmations
  • Loading branch information
simoarpe authored Dec 7, 2023
1 parent bdd8dd6 commit fbc2b67
Show file tree
Hide file tree
Showing 21 changed files with 195 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ public abstract class BraveActivity extends ChromeActivity
BraveNewsConnectionErrorHandler.BraveNewsConnectionErrorHandlerDelegate,
MiscAndroidMetricsConnectionErrorHandler
.MiscAndroidMetricsConnectionErrorHandlerDelegate {
public static final String BRAVE_WALLET_HOST = "wallet";
public static final String BRAVE_WALLET_URL = "brave://wallet/crypto/portfolio/assets";
public static final String BRAVE_BUY_URL = "brave://wallet/crypto/fund-wallet";
public static final String BRAVE_SEND_URL = "brave://wallet/send";
public static final String BRAVE_SWAP_URL = "brave://wallet/swap";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

package org.chromium.chrome.browser.crypto_wallet.activities;

import static org.chromium.chrome.browser.crypto_wallet.adapters.CryptoFragmentPageAdapter.ACCOUNTS_FRAGMENT_POSITION;
import static org.chromium.chrome.browser.crypto_wallet.adapters.CryptoFragmentPageAdapter.MARKET_FRAGMENT_POSITION;
import static org.chromium.chrome.browser.crypto_wallet.adapters.CryptoFragmentPageAdapter.PORTFOLIO_FRAGMENT_POSITION;
import static org.chromium.chrome.browser.crypto_wallet.adapters.CryptoFragmentPageAdapter.TRANSACTIONS_ACTIVITY_FRAGMENT_POSITION;
import static org.chromium.chrome.browser.crypto_wallet.util.Utils.ONBOARDING_ACTION;
import static org.chromium.chrome.browser.crypto_wallet.util.Utils.ONBOARDING_FIRST_PAGE_ACTION;
import static org.chromium.chrome.browser.crypto_wallet.util.Utils.RESTORE_WALLET_ACTION;
Expand Down Expand Up @@ -349,31 +345,7 @@ private void showMainLayout() {
addRemoveSecureFlag(false);

mCryptoOnboardingLayout.setVisibility(View.GONE);
mCryptoLayout.setVisibility(View.VISIBLE);

mBottomNavigationView.setOnItemSelectedListener(menuItem -> {
final int menuItemId = menuItem.getItemId();
if (menuItemId == R.id.action_wallet_portfolio) {
mViewPager.setCurrentItem(PORTFOLIO_FRAGMENT_POSITION, true);
} else if (menuItemId == R.id.action_wallet_activity) {
mViewPager.setCurrentItem(TRANSACTIONS_ACTIVITY_FRAGMENT_POSITION, true);
} else if (menuItemId == R.id.action_wallet_accounts) {
mViewPager.setCurrentItem(ACCOUNTS_FRAGMENT_POSITION, true);
} else if (menuItemId == R.id.action_wallet_explore) {
mViewPager.setCurrentItem(MARKET_FRAGMENT_POSITION, true);
}
return true;
});

if (mKeyringService != null) {
mKeyringService.isWalletBackedUp(backed_up -> {
if (!backed_up) {
showWalletBackupBanner();
} else {
findViewById(R.id.wallet_backup_banner).setVisibility(View.GONE);
}
});
}
WalletUtils.openWebWallet();
}

private void addBackupWalletSequence(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

package org.chromium.chrome.browser.crypto_wallet.controller;

import static org.chromium.chrome.browser.app.BraveActivity.BRAVE_WALLET_HOST;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
Expand All @@ -32,15 +35,17 @@
import org.chromium.chrome.browser.fullscreen.FullscreenManager;
import org.chromium.chrome.browser.toolbar.bottom.BottomToolbarConfiguration;
import org.chromium.chrome.browser.util.ConfigurationUtils;
import org.chromium.content_public.browser.WebContents;
import org.chromium.mojo.bindings.ConnectionErrorHandler;
import org.chromium.mojo.system.MojoException;
import org.chromium.url.GURL;

public class DAppsWalletController
implements ConnectionErrorHandler, BraveWalletPanel.BraveWalletPanelServices {
private static final String TAG = DAppsWalletController.class.getSimpleName();
private FullscreenManager mFullscreenManager;
private Context mContext;
private View mAnchorViewHost;
private final Context mContext;
private final View mAnchorViewHost;
private AssetRatioService mAssetRatioService;
private KeyringService mKeyringService;
private BraveWalletService mBraveWalletService;
Expand All @@ -50,39 +55,71 @@ public class DAppsWalletController
private BraveWalletPanel mBraveWalletPanel;
private DialogInterface.OnDismissListener mOnDismissListener;
private final AppCompatActivity mActivity;
@Nullable private final GURL mVisibleUrl;

private DialogInterface.OnDismissListener mDialogOrPanelDismissListener = dialog -> {
if (mOnDismissListener != null) {
mOnDismissListener.onDismiss(dialog);
}
cleanUp();
};
@NonNull private final DefaultLifecycleObserver mDefaultLifecycleObserver;

@NonNull private final DialogInterface.OnDismissListener mDialogOrPanelDismissListener;

public DAppsWalletController(Context mContext, View mAnchorViewHost) {
this.mContext = mContext;
this.mAnchorViewHost = mAnchorViewHost;
this.mActivity = BraveActivity.getChromeTabbedActivity();
WebContents webContents = null;
mDefaultLifecycleObserver =
new DefaultLifecycleObserver() {
@Override
public void onResume(@NonNull LifecycleOwner owner) {
if (mBraveWalletPanel != null) {
mBraveWalletPanel.resume();
}
}

@Override
public void onPause(@NonNull LifecycleOwner owner) {
if (mBraveWalletPanel != null) {
mBraveWalletPanel.pause();
}
}
};
mDialogOrPanelDismissListener =
dialog -> {
if (mOnDismissListener != null) {
mOnDismissListener.onDismiss(dialog);
}
DAppsWalletController.this.cleanUp();
};
try {
BraveActivity activity = BraveActivity.getBraveActivity();
webContents = activity.getCurrentWebContents();

ObservableSupplier<BrowserControlsManager> managerSupplier =
activity.getBrowserControlsManagerSupplier();
mFullscreenManager = managerSupplier.get().getFullscreenManager();
} catch (BraveActivity.BraveActivityNotFoundException | NullPointerException e) {
Log.e(TAG, "Constructor", e);
}

if (webContents != null) {
mVisibleUrl = webContents.getVisibleUrl();
} else {
mVisibleUrl = null;
}
}

public DAppsWalletController(Context mContext, View mAnchorViewHost,
public DAppsWalletController(
Context context,
View anchorViewHost,
DialogInterface.OnDismissListener onDismissListener) {
this(mContext, mAnchorViewHost);
this(context, anchorViewHost);
this.mOnDismissListener = onDismissListener;
}

public void showWalletPanel() {
InitAssetRatioService();
InitKeyringService();
InitJsonRpcService();
InitBraveWalletService();
initAssetRatioService();
initKeyringService();
initJsonRpcService();
initBraveWalletService();
if (Utils.shouldShowCryptoOnboarding()) {
showOnBoardingOrUnlock();
} else {
Expand All @@ -102,14 +139,17 @@ public void showWalletPanel() {
}

private void createAndShowWalletPanel() {
boolean showExpandButton =
mVisibleUrl != null && !mVisibleUrl.getHost().equals(BRAVE_WALLET_HOST);
mBraveWalletPanel =
new BraveWalletPanel(mAnchorViewHost, mDialogOrPanelDismissListener, this);
new BraveWalletPanel(
mAnchorViewHost, mDialogOrPanelDismissListener, this, showExpandButton);
mBraveWalletPanel.showLikePopDownMenu();
setupLifeCycleUpdater();
}

private void setupLifeCycleUpdater() {
mActivity.getLifecycle().addObserver(defaultLifecycleObserver);
mActivity.getLifecycle().addObserver(mDefaultLifecycleObserver);
}

private void showOnBoardingOrUnlock() {
Expand Down Expand Up @@ -165,10 +205,10 @@ public void onConnectionError(MojoException e) {
mAssetRatioService.close();
mAssetRatioService = null;
}
InitAssetRatioService();
InitKeyringService();
InitJsonRpcService();
InitBraveWalletService();
initAssetRatioService();
initKeyringService();
initJsonRpcService();
initBraveWalletService();
updateState();
}

Expand Down Expand Up @@ -198,28 +238,28 @@ private void updateState() {
mHasStateInitialise = true;
}

private void InitKeyringService() {
private void initKeyringService() {
if (mKeyringService != null) {
return;
}
mKeyringService = KeyringServiceFactory.getInstance().getKeyringService(this);
}

private void InitJsonRpcService() {
private void initJsonRpcService() {
if (mJsonRpcService != null) {
return;
}
mJsonRpcService = JsonRpcServiceFactory.getInstance().getJsonRpcService(this);
}

private void InitBraveWalletService() {
private void initBraveWalletService() {
if (mBraveWalletService != null) {
return;
}
mBraveWalletService = BraveWalletServiceFactory.getInstance().getBraveWalletService(this);
}

private void InitAssetRatioService() {
private void initAssetRatioService() {
if (mAssetRatioService != null) {
return;
}
Expand All @@ -244,27 +284,10 @@ private void cleanUp() {
mAssetRatioService = null;
}
if (mActivity != null) {
mActivity.getLifecycle().removeObserver(defaultLifecycleObserver);
mActivity.getLifecycle().removeObserver(mDefaultLifecycleObserver);
}
}

private final DefaultLifecycleObserver defaultLifecycleObserver =
new DefaultLifecycleObserver() {
@Override
public void onResume(@NonNull LifecycleOwner owner) {
if (mBraveWalletPanel != null) {
mBraveWalletPanel.resume();
}
}

@Override
public void onPause(@NonNull LifecycleOwner owner) {
if (mBraveWalletPanel != null) {
mBraveWalletPanel.pause();
}
}
};

private boolean shouldShowNotificationAtTop(Context context) {
return ConfigurationUtils.isTablet(context)
|| !BottomToolbarConfiguration.isBottomToolbarEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public class BraveWalletPanel implements DialogInterface {
private final Context mContext;
private final Observer<AllAccountsInfo> mAllAccountsInfoObserver;

private final boolean mShowExpandButton;

private final Observer<NetworkInfo> mDefaultNetworkObserver;

public interface BraveWalletPanelServices {
Expand All @@ -109,9 +111,11 @@ public interface BraveWalletPanelServices {
JsonRpcService getJsonRpcService();
}

public BraveWalletPanel(@NonNull final View anchorViewHost,
public BraveWalletPanel(
@NonNull final View anchorViewHost,
@NonNull final OnDismissListener onDismissListener,
@NonNull final BraveWalletPanelServices braveWalletPanelServices) {
@NonNull final BraveWalletPanelServices braveWalletPanelServices,
final boolean showExpandButton) {
try {
mWalletModel = BraveActivity.getBraveActivity().getWalletModel();
// Update network model to use network per origin
Expand All @@ -120,6 +124,7 @@ public BraveWalletPanel(@NonNull final View anchorViewHost,
Log.e(TAG, "BraveWalletPanel Constructor", e);
}

mShowExpandButton = showExpandButton;
mAccountsWithPermissions = new HashSet<>();
mExecutor = Executors.newSingleThreadExecutor();
mHandler = new Handler(Looper.getMainLooper());
Expand Down Expand Up @@ -423,15 +428,19 @@ private void setUpViews() {
mPopupWindow.setWidth((int) (isTablet ? (deviceWidth * 0.6) : (deviceWidth * 0.95)));

mExpandWalletImage = mPopupView.findViewById(R.id.iv_dapp_panel_expand);
mExpandWalletImage.setOnClickListener(v -> {
dismiss();
try {
BraveActivity activity = BraveActivity.getBraveActivity();
activity.openBraveWallet(false, false, false);
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "setUpViews ExpandWalletImage click " + e);
}
});
if (mShowExpandButton) {
mExpandWalletImage.setVisibility(View.VISIBLE);
mExpandWalletImage.setOnClickListener(
v -> {
dismiss();
try {
BraveActivity activity = BraveActivity.getBraveActivity();
activity.openBraveWallet(false, false, false);
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "ExpandWalletImage", e);
}
});
}
mOptionsImage = mPopupView.findViewById(R.id.iv_dapp_panel_menu);
mOptionsImage.setOnClickListener(v -> { showPopupMenu(); });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.chromium.base.Log;
import org.chromium.brave_wallet.mojom.AccountId;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.BlockchainToken;
Expand All @@ -19,6 +20,7 @@
import org.chromium.brave_wallet.mojom.SolanaTxData;
import org.chromium.brave_wallet.mojom.TxDataUnion;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.util.TabUtils;
import org.chromium.mojo_base.mojom.TimeDelta;

Expand All @@ -37,6 +39,7 @@ public class WalletUtils {
private static final String ACCOUNT_INFO = "accountInfo";
private static final String BLOCKCHAIN_TOKEN = "blockchainToken";
private static final String NETWORK_INFO = "networkInfo";
private static final String TAG = "WalletUtils";

private static String getNewAccountPrefixForCoin(@CoinType.EnumType int coinType) {
switch (coinType) {
Expand Down Expand Up @@ -154,6 +157,16 @@ public static NetworkInfo getNetworkInfoFromIntent(@NonNull final Intent intent)
return NetworkInfo.deserialize(ByteBuffer.wrap(bytes));
}

public static void openWebWallet() {
try {
BraveActivity activity = BraveActivity.getBraveActivity();
activity.openNewOrSelectExistingTab(BraveActivity.BRAVE_WALLET_URL, true);
TabUtils.bringChromeTabbedActivityToTheTop(activity);
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "Error while opening wallet tab.", e);
}
}

public static void openWalletHelpCenter(Context context) {
if (context == null) return;
TabUtils.openUrlInCustomTab(context, WalletConstants.WALLET_HELP_CENTER);
Expand Down
1 change: 1 addition & 0 deletions android/java/res/layout/brave_wallet_panel_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
android:contentDescription="@string/accessibility_wallet_dapp_panel_expand"
android:foreground="?android:attr/selectableItemBackground"
android:paddingVertical="10dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand Down
Loading

0 comments on commit fbc2b67

Please sign in to comment.