Skip to content

Commit

Permalink
Merge pull request #593 from wallabag/better_url_handling
Browse files Browse the repository at this point in the history
Better URL handling
  • Loading branch information
Strubbl authored Jun 30, 2017
2 parents f13149d + aeb32ad commit da43b3e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
Expand Down Expand Up @@ -448,14 +449,14 @@ public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}

@SuppressWarnings("deprecation") // can't use newer method until API 21
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) { // TODO: check
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
// If we try to open current URL, do not propose to save it, directly open browser
if(url.equals(articleUrl)) {
Intent launchBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(launchBrowserIntent);
openURL(url);
} else {
openUrl(url);
handleUrlClicked(url);
}

return true; // always override URL loading
Expand Down Expand Up @@ -717,8 +718,9 @@ private void loadingFinished() {
}
}

private void openUrl(final String url) {
if(url == null) return;
private void handleUrlClicked(final String url) {
Log.d(TAG, "handleUrlClicked() url: " + url);
if(TextUtils.isEmpty(url)) return;

// TODO: fancy dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Expand All @@ -740,9 +742,7 @@ private void openUrl(final String url) {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent launchBrowserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(launchBrowserIntent);
openURL(url);
break;
case 1:
ServiceHelper.addLink(ReadArticleActivity.this, url);
Expand All @@ -754,6 +754,26 @@ public void onClick(DialogInterface dialog, int which) {
builder.show();
}

private void openURL(String url) {
Log.d(TAG, "openURL() url: " + url);
if(TextUtils.isEmpty(url)) return;

Uri uri = Uri.parse(url);
if(uri.getScheme() == null) {
Log.i(TAG, "openURL() scheme is null, appending default scheme");
uri = Uri.parse("http://" + url);
}
Log.d(TAG, "openURL() uri: " + uri);

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(intent);
} catch(ActivityNotFoundException e) {
Log.w(TAG, "openURL() failed to open URL", e);
Toast.makeText(this, R.string.message_couldNotOpenUrl, Toast.LENGTH_SHORT).show();
}
}

private void markAsReadAndClose() {
OperationsHelper.archiveArticle(this, article.getArticleId(), !article.getArchive());

Expand Down Expand Up @@ -832,9 +852,7 @@ private void manageTags() {
}

private void openOriginal() {
Intent launchBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(articleUrl));

startActivity(launchBrowserIntent);
openURL(articleUrl);
}

private void copyOriginalURL() {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@

<string name="title_main_tag">Tag: %s</string>

<string name="message_couldNotOpenUrl">Couldn\'t open this URL</string>

<string name="pref_name_runConnectionWizard">Run connection wizard</string>
<string name="pref_desc_runConnectionWizard">The connection wizard will help you to set up basic connection settings</string>

Expand Down

0 comments on commit da43b3e

Please sign in to comment.