Skip to content

Commit

Permalink
Handle if request comes from same hostname. If not, it may be an intent.
Browse files Browse the repository at this point in the history
This may solve the "External links constantly opening xtools-at#15" issue
  • Loading branch information
camoralb committed Jul 8, 2020
1 parent f1bddca commit fc10c9f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/src/main/java/at/xtools/pwawrapper/webview/WebViewHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ public void onReceivedError(WebView view, WebResourceRequest request, WebResourc
handleLoadError(error.getErrorCode());
}
}
}
//Handle if request comes from same hostname. If not, it may be an intent
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

if((String.valueOf(request.getUrl())).contains(Constants.WEBAPP_HOST)) {
view.loadUrl(String.valueOf(request.getUrl()));
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
view.getContext().startActivity(intent);
}

return true;
}
});
}
Expand Down

2 comments on commit fc10c9f

@lebiben
Copy link

@lebiben lebiben commented on fc10c9f Oct 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better code is to use instead this if condition:

if(Objects.requireNonNull(Uri.parse(String.valueOf(request.getUrl())).getHost()).endsWith(Constants.WEBAPP_HOST))

@lebiben
Copy link

@lebiben lebiben commented on fc10c9f Oct 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UPDATE: Correct code for shouldOverrideUrlLoading method:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(Objects.requireNonNull(Uri.parse(url).getHost()).endsWith(Constants.WEBAPP_HOST)) {
        return false;
    }

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    view.getContext().startActivity(intent);
    return true;
}

Please sign in to comment.