From aeb32addf381f10cefe5fa35dc2ff1f00050963e Mon Sep 17 00:00:00 2001 From: Dmitriy Bogdanov Date: Fri, 30 Jun 2017 13:42:36 +0400 Subject: [PATCH] Improve URL handling --- .../apps/Poche/ui/ReadArticleActivity.java | 39 +++++++++++++------ app/src/main/res/values/strings.xml | 2 + 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/fr/gaulupeau/apps/Poche/ui/ReadArticleActivity.java b/app/src/main/java/fr/gaulupeau/apps/Poche/ui/ReadArticleActivity.java index c035cc844..f4184ce4b 100644 --- a/app/src/main/java/fr/gaulupeau/apps/Poche/ui/ReadArticleActivity.java +++ b/app/src/main/java/fr/gaulupeau/apps/Poche/ui/ReadArticleActivity.java @@ -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; @@ -453,10 +454,9 @@ public void onPageFinished(WebView view, String url) { 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 @@ -718,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); @@ -741,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); @@ -755,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()); @@ -833,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() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index edd0020e1..e9377f02d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -151,6 +151,8 @@ Tag: %s + Couldn\'t open this URL + Run connection wizard The connection wizard will help you to set up basic connection settings