From 8ce1defef26516a250755fb2ea7e440ec16fa4ad Mon Sep 17 00:00:00 2001 From: stom79 Date: Wed, 17 Jan 2018 07:27:52 +0100 Subject: [PATCH] Fixes issue #249 & #250 - External browser issue --- .../activities/ShowAccountActivity.java | 12 +--- .../mastodon/client/Entities/Status.java | 59 +++++++++---------- .../mastodon/drawers/StatusListAdapter.java | 12 +--- .../gouv/etalab/mastodon/helper/Helper.java | 31 ++++++++-- .../mastodon/jobs/NotificationsSyncJob.java | 2 +- 5 files changed, 58 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java index 0dd6922a8..52390157e 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowAccountActivity.java @@ -333,13 +333,9 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt return true; case R.id.action_open_browser: if( accountUrl != null) { - Intent intent = new Intent(getApplicationContext(), WebviewActivity.class); - Bundle b = new Bundle(); if( !accountUrl.startsWith("http://") && ! accountUrl.startsWith("https://")) accountUrl = "http://" + accountUrl; - b.putString("url", accountUrl); - intent.putExtras(b); - startActivity(intent); + Helper.openBrowser(ShowAccountActivity.this, accountUrl); } return true; case R.id.action_mention: @@ -482,13 +478,9 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt warning_message.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Intent intent = new Intent(getApplicationContext(), WebviewActivity.class); - Bundle b = new Bundle(); if( !accountUrl.startsWith("http://") && ! accountUrl.startsWith("https://")) accountUrl = "http://" + accountUrl; - b.putString("url", accountUrl); - intent.putExtras(b); - startActivity(intent); + Helper.openBrowser(ShowAccountActivity.this, accountUrl); } }); //Timed muted account diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java index 94907d7a6..7e5178e6a 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java @@ -621,39 +621,34 @@ public class Status implements Parcelable{ spannableString.removeSpan(span); List mentions = this.status.getReblog() != null ? this.status.getReblog().getMentions() : this.status.getMentions(); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); - boolean embedded_browser = sharedpreferences.getBoolean(Helper.SET_EMBEDDED_BROWSER, true); - if( embedded_browser){ - Matcher matcher; - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) - matcher = Patterns.WEB_URL.matcher(spannableString); - else - matcher = Helper.urlPattern.matcher(spannableString); - while (matcher.find()){ - int matchStart = matcher.start(1); - int matchEnd = matcher.end(); - final String url = spannableString.toString().substring(matchStart, matchEnd); - spannableString.setSpan(new ClickableSpan() { - @Override - public void onClick(View textView) { - Intent intent = new Intent(context, WebviewActivity.class); - Bundle b = new Bundle(); - String finalUrl = url; - if( !url.startsWith("http://") && ! url.startsWith("https://")) - finalUrl = "http://" + url; - b.putString("url", finalUrl); - intent.putExtras(b); - context.startActivity(intent); - } - @Override - public void updateDrawState(TextPaint ds) { - super.updateDrawState(ds); - } - }, - matchStart, matchEnd, - Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - } + + Matcher matcher; + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) + matcher = Patterns.WEB_URL.matcher(spannableString); + else + matcher = Helper.urlPattern.matcher(spannableString); + while (matcher.find()){ + int matchStart = matcher.start(1); + int matchEnd = matcher.end(); + final String url = spannableString.toString().substring(matchStart, matchEnd); + spannableString.setSpan(new ClickableSpan() { + @Override + public void onClick(View textView) { + String finalUrl = url; + if( !url.startsWith("http://") && ! url.startsWith("https://")) + finalUrl = "http://" + url; + Helper.openBrowser(context, finalUrl); + } + @Override + public void updateDrawState(TextPaint ds) { + super.updateDrawState(ds); + } + }, + matchStart, matchEnd, + Spanned.SPAN_INCLUSIVE_EXCLUSIVE); } + //Deals with mention to make them clickable if( mentions != null && mentions.size() > 0 ) { //Looping through accounts which are mentioned @@ -685,7 +680,7 @@ public class Status implements Parcelable{ } } - Matcher matcher = Helper.hashtagPattern.matcher(spannableString); + matcher = Helper.hashtagPattern.matcher(spannableString); while (matcher.find()){ int matchStart = matcher.start(1); int matchEnd = matcher.end(); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index 10c4aa801..7f454a18c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -965,11 +965,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct holder.status_cardview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Intent intent = new Intent(context, WebviewActivity.class); - Bundle b = new Bundle(); - b.putString("url", status.getCard().getUrl()); - intent.putExtras(b); - context.startActivity(intent); + Helper.openBrowser(context, status.getCard().getUrl()); } }); }else { @@ -991,11 +987,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct } @Override public boolean shouldOverrideUrlLoading (WebView view, String url){ - Intent intent = new Intent(context, WebviewActivity.class); - Bundle b = new Bundle(); - b.putString("url", url); - intent.putExtras(b); - context.startActivity(intent); + Helper.openBrowser(context, url); holder.status_cardview_webview.loadUrl(finalSrc); return true; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 7b35de4de..fafc7a519 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -1286,11 +1286,7 @@ public class Helper { spannableString.setSpan(new ClickableSpan() { @Override public void onClick(View textView) { - Intent intent = new Intent(context, WebviewActivity.class); - Bundle b = new Bundle(); - b.putString("url", url); - intent.putExtras(b); - context.startActivity(intent); + Helper.openBrowser(context, url); } @Override public void updateDrawState(TextPaint ds) { @@ -1786,6 +1782,31 @@ public class Helper { }); } + /** + * Manage URLs to open (built-in or external app) + * @param context Context + * @param url String url to open + */ + public static void openBrowser(Context context, String url){ + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); + boolean embedded_browser = sharedpreferences.getBoolean(Helper.SET_EMBEDDED_BROWSER, true); + if( embedded_browser) { + Intent intent = new Intent(context, WebviewActivity.class); + Bundle b = new Bundle(); + String finalUrl = url; + if (!url.startsWith("http://") && !url.startsWith("https://")) + finalUrl = "http://" + url; + b.putString("url", finalUrl); + intent.putExtras(b); + context.startActivity(intent); + }else { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse(url)); + context.startActivity(intent); + } + } + + public static void installProvider(){ Security.insertProviderAt(Conscrypt.newProvider(),1); } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java index f09d18c9b..0d1049830 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java @@ -263,7 +263,7 @@ public class NotificationsSyncJob extends Job { }) .into(new SimpleTarget() { @Override - public void onResourceReady(Bitmap resource, Transition transition) { + public void onResourceReady(@NonNull Bitmap resource, Transition transition) { notify_user(getContext(), intent, notificationId, resource, finalTitle, message); String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null); if( lastNotif == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(lastNotif)){