Skip to content

Commit

Permalink
Fix Dappbrowser Intercept (#3369)
Browse files Browse the repository at this point in the history
* - Fix for WalletConnect V2 Foreground Service

* - Fix for Opensea service update

* - fix dappbrowser URL intercept
  • Loading branch information
JamesSmartCell authored Mar 25, 2024
1 parent 5a289e8 commit 8961abf
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 69 deletions.
147 changes: 85 additions & 62 deletions app/src/main/java/com/alphawallet/app/ui/DappBrowserFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebHistoryItem;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
Expand Down Expand Up @@ -72,7 +73,6 @@
import com.alphawallet.app.entity.TransactionReturn;
import com.alphawallet.app.entity.URLLoadInterface;
import com.alphawallet.app.entity.Wallet;
import com.alphawallet.app.entity.WalletConnectActions;
import com.alphawallet.app.entity.WalletType;
import com.alphawallet.app.entity.analytics.ActionSheetSource;
import com.alphawallet.app.entity.analytics.QrScanResultType;
Expand Down Expand Up @@ -218,7 +218,7 @@ public void onActivityResult(Uri uri)
// Need to handle the inverse event where the keyboard is hidden, and we size the page back
// (Remembering to allow for the navigation bar).
private final View.OnApplyWindowInsetsListener resizeListener = (v, insets) -> {
if (v == null || getActivity() == null)
if (getActivity() == null)
{
return insets;
}
Expand Down Expand Up @@ -896,66 +896,11 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathC
web3.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
{
String[] prefixCheck = url.split(":");
if (prefixCheck.length > 1)
{
Intent intent;
switch (prefixCheck[0])
{
case C.DAPP_PREFIX_TELEPHONE:
intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(Intent.createChooser(intent, "Call " + prefixCheck[1]));
return true;
case C.DAPP_PREFIX_MAILTO:
intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse(url));
startActivity(Intent.createChooser(intent, "Email: " + prefixCheck[1]));
return true;
case C.DAPP_PREFIX_ALPHAWALLET:
if (prefixCheck[1].equals(C.DAPP_SUFFIX_RECEIVE))
{
viewModel.showMyAddress(getContext());
return true;
}
break;
case C.DAPP_PREFIX_WALLETCONNECT:
//start walletconnect
if (wallet.type == WalletType.WATCH)
{
showWalletWatch();
}
else
{
walletConnectSession = url;
if (getContext() != null)
viewModel.handleWalletConnect(getContext(), url, activeNetwork);
}
return true;
default:
break;
}
}

if (fromWalletConnectModal(url))
{
String encodedURL = url.split("=")[1];
try
{
String decodedURL = URLDecoder.decode(encodedURL, Charset.defaultCharset().name());
viewModel.handleWalletConnect(getContext(), decodedURL, activeNetwork);
return true;
}
catch (UnsupportedEncodingException e)
{
Timber.d("Decode URL failed: " + e);
}
}

setUrlText(url);
return false;
final Uri uri = request.getUrl();
final String url = uri.toString();
return handlePrefix(url);
}
});

Expand All @@ -978,6 +923,77 @@ public boolean shouldOverrideUrlLoading(WebView view, String url)
}
}

private boolean handlePrefix(String url)
{
String[] prefixCheck = url.split(":");
if (prefixCheck.length > 1)
{
Intent intent;
switch (prefixCheck[0])
{
case C.DAPP_PREFIX_TELEPHONE:
intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(Intent.createChooser(intent, "Call " + prefixCheck[1]));
return true;
case C.DAPP_PREFIX_MAILTO:
intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse(url));
startActivity(Intent.createChooser(intent, "Email: " + prefixCheck[1]));
return true;
case C.DAPP_PREFIX_ALPHAWALLET:
if (prefixCheck[1].equals(C.DAPP_SUFFIX_RECEIVE))
{
viewModel.showMyAddress(getContext());
return true;
}
break;
case C.DAPP_PREFIX_AWALLET:
handleAWCode(url);
return true;
case C.DAPP_PREFIX_WALLETCONNECT:
//start walletconnect
if (wallet.type == WalletType.WATCH)
{
showWalletWatch();
}
else
{
walletConnectSession = url;
if (getContext() != null)
viewModel.handleWalletConnect(getContext(), url, activeNetwork);
}
return true;
default:
break;
}
}

if (fromWalletConnectModal(url))
{
String encodedURL = url.split("=")[1];
try
{
String decodedURL = URLDecoder.decode(encodedURL, Charset.defaultCharset().name());
viewModel.handleWalletConnect(getContext(), decodedURL, activeNetwork);
return true;
}
catch (UnsupportedEncodingException e)
{
Timber.d("Decode URL failed: %s", e.getMessage());
}
}

return false;
}

private void handleAWCode(String awCode)
{
Bundle codeBundle = new Bundle();
codeBundle.putString(C.AWALLET_CODE, awCode);
getParentFragmentManager().setFragmentResult(C.AWALLET_CODE, codeBundle);
}

private boolean fromWalletConnectModal(String url)
{
return url.startsWith("https://" + mainnetMagicLinkDomain + "/wc?uri=");
Expand Down Expand Up @@ -1477,7 +1493,14 @@ private boolean loadUrl(String urlText)
detachFragments();
addToBackStack(DAPP_BROWSER);
cancelSearchSession();
if (checkForMagicLink(urlText)) return true;
if (checkForMagicLink(urlText))
{
return true;
}
else if (handlePrefix(urlText))
{
return true;
}
web3.resetView();
web3.loadUrl(Utils.formatUrl(urlText));
setUrlText(Utils.formatUrl(urlText));
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/alphawallet/app/web3/Web3View.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.net.Uri;
import android.util.AttributeSet;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
Expand All @@ -14,7 +14,6 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.webkit.WebSettingsCompat;
import androidx.webkit.WebViewFeature;

Expand Down Expand Up @@ -141,7 +140,7 @@ public void setWebChromeClient(WebChromeClient client)
}

@Override
public void setWebViewClient(WebViewClient client)
public void setWebViewClient(@NonNull WebViewClient client)
{
super.setWebViewClient(new WrapWebViewClient(webViewClient, client));
}
Expand Down Expand Up @@ -366,11 +365,12 @@ else if (!loadingError && loadInterface != null)
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
final String url = uri.toString();
redirect = true;

return externalClient.shouldOverrideUrlLoading(view, url)
return externalClient.shouldOverrideUrlLoading(view, request)
|| internalClient.shouldOverrideUrlLoading(view, url);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AddressBar extends MaterialToolbar implements ItemClickListener
private ImageView home;

@Nullable
private Disposable disposable;
private Disposable disposable; // awallet://openURL?https%3A%2F%2Fsmart-layer.vercel.app
private boolean focused;

public AddressBar(Context context, AttributeSet attributeSet)
Expand Down

0 comments on commit 8961abf

Please sign in to comment.