From 242b1a7f0f98e40cbc779393f9f90bf6921e7f69 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 7 Sep 2020 18:54:19 +0200 Subject: [PATCH] comment #35 - simplify code for reusing it --- .../activities/TransformActivity.java | 359 +----------------- .../fedilab/nitterizeme/helpers/Utils.java | 118 +++++- 2 files changed, 137 insertions(+), 340 deletions(-) diff --git a/app/src/main/java/app/fedilab/nitterizeme/activities/TransformActivity.java b/app/src/main/java/app/fedilab/nitterizeme/activities/TransformActivity.java index 87d3711..df1664e 100644 --- a/app/src/main/java/app/fedilab/nitterizeme/activities/TransformActivity.java +++ b/app/src/main/java/app/fedilab/nitterizeme/activities/TransformActivity.java @@ -15,19 +15,14 @@ package app.fedilab.nitterizeme.activities; * see . */ import android.app.Activity; -import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.util.Patterns; -import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; -import java.net.URLDecoder; -import java.net.URLEncoder; import java.util.Arrays; import java.util.Objects; import java.util.regex.Matcher; @@ -36,30 +31,15 @@ import java.util.regex.Pattern; import app.fedilab.nitterizeme.BuildConfig; import app.fedilab.nitterizeme.helpers.Utils; -import static app.fedilab.nitterizeme.activities.CheckAppActivity.bibliogram_instances; -import static app.fedilab.nitterizeme.activities.CheckAppActivity.instagram_domains; -import static app.fedilab.nitterizeme.activities.CheckAppActivity.invidious_instances; -import static app.fedilab.nitterizeme.activities.CheckAppActivity.nitter_instances; -import static app.fedilab.nitterizeme.activities.CheckAppActivity.outlook_safe_domain; import static app.fedilab.nitterizeme.activities.CheckAppActivity.shortener_domains; -import static app.fedilab.nitterizeme.activities.CheckAppActivity.twitter_domains; -import static app.fedilab.nitterizeme.activities.CheckAppActivity.youtube_domains; -import static app.fedilab.nitterizeme.activities.MainActivity.SET_BIBLIOGRAM_ENABLED; -import static app.fedilab.nitterizeme.activities.MainActivity.SET_INVIDIOUS_ENABLED; -import static app.fedilab.nitterizeme.activities.MainActivity.SET_NITTER_ENABLED; import static app.fedilab.nitterizeme.helpers.Utils.KILL_ACTIVITY; -import static app.fedilab.nitterizeme.helpers.Utils.ampExtract; -import static app.fedilab.nitterizeme.helpers.Utils.bibliogramAccountPattern; -import static app.fedilab.nitterizeme.helpers.Utils.bibliogramPostPattern; import static app.fedilab.nitterizeme.helpers.Utils.forwardToBrowser; +import static app.fedilab.nitterizeme.helpers.Utils.isRouted; import static app.fedilab.nitterizeme.helpers.Utils.manageShortened; import static app.fedilab.nitterizeme.helpers.Utils.manageShortenedShare; -import static app.fedilab.nitterizeme.helpers.Utils.maps; -import static app.fedilab.nitterizeme.helpers.Utils.nitterPattern; -import static app.fedilab.nitterizeme.helpers.Utils.outlookRedirect; import static app.fedilab.nitterizeme.helpers.Utils.remove_tracking_param; +import static app.fedilab.nitterizeme.helpers.Utils.routerEnabledForHost; import static app.fedilab.nitterizeme.helpers.Utils.transformUrl; -import static app.fedilab.nitterizeme.helpers.Utils.youtubePattern; public class TransformActivity extends Activity { @@ -68,7 +48,6 @@ public class TransformActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - SharedPreferences sharedpreferences = getSharedPreferences(MainActivity.APP_PREFS, Context.MODE_PRIVATE); Intent intent = getIntent(); if (intent != null && intent.getStringExtra("nitterizeme") != null) { finish(); @@ -93,11 +72,9 @@ public class TransformActivity extends Activity { //Shortened URLs if (Arrays.asList(shortener_domains).contains(host)) { manageShortened(TransformActivity.this, url); - } - //Twitter URLs - else if (Arrays.asList(twitter_domains).contains(host)) { - boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true); - if (nitter_enabled) { + } else if (isRouted(url)) { + boolean routeEnabled = routerEnabledForHost(TransformActivity.this, url); + if (routeEnabled) { Intent delegate = new Intent(Intent.ACTION_VIEW); String transformedURL = transformUrl(TransformActivity.this, url); if (transformedURL != null) { @@ -117,164 +94,6 @@ public class TransformActivity extends Activity { } else { forwardToBrowser(TransformActivity.this, intent); } - } //Instagram URLs - else if (Arrays.asList(instagram_domains).contains(host)) { - boolean bibliogram_enabled = sharedpreferences.getBoolean(SET_BIBLIOGRAM_ENABLED, true); - if (bibliogram_enabled) { - Intent delegate = new Intent(Intent.ACTION_VIEW); - String transformedURL = transformUrl(TransformActivity.this, url); - if (transformedURL != null) { - delegate.setData(Uri.parse(transformUrl(TransformActivity.this, url))); - delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - if (BuildConfig.fullLinks) { - forwardToBrowser(TransformActivity.this, delegate); - } else { - if (delegate.resolveActivity(getPackageManager()) != null) { - startActivity(delegate); - finish(); - } - } - } else { - forwardToBrowser(TransformActivity.this, intent); - } - } else { - forwardToBrowser(TransformActivity.this, intent); - } - } - //Maps URLs (containing /maps/place like Google Maps links) - else if (url.contains("/maps/place")) { - boolean osm_enabled = sharedpreferences.getBoolean(MainActivity.SET_OSM_ENABLED, true); - if (osm_enabled) { - Intent delegate = new Intent(Intent.ACTION_VIEW); - String transformedURL = transformUrl(TransformActivity.this, url); - if (transformedURL != null) { - delegate.setData(Uri.parse(transformUrl(TransformActivity.this, url))); - delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - if (BuildConfig.fullLinks) { - forwardToBrowser(TransformActivity.this, delegate); - } else { - if (delegate.resolveActivity(getPackageManager()) != null) { - startActivity(delegate); - finish(); - } - } - } else { - forwardToBrowser(TransformActivity.this, intent); - } - } else { - forwardToBrowser(TransformActivity.this, intent); - } - } - //AMP URLs (containing /amp/s/ like Google AMP links) - else if (url.contains("/amp/s/")) { - Intent delegate = new Intent(Intent.ACTION_VIEW); - Matcher matcher = ampExtract.matcher(url); - String transformedURL = null; - while (matcher.find()) { - transformedURL = "https://" + matcher.group(1); - } - if (transformedURL != null) { - delegate.setData(Uri.parse(transformedURL)); - delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - if (BuildConfig.fullLinks) { - forwardToBrowser(TransformActivity.this, delegate); - } else { - if (delegate.resolveActivity(getPackageManager()) != null) { - startActivity(delegate); - finish(); - } - } - } else { - forwardToBrowser(TransformActivity.this, intent); - } - } - //YouTube URLs - else if (Arrays.asList(youtube_domains).contains(host)) { //Youtube URL - boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true); - if (invidious_enabled) { - Intent delegate = new Intent(Intent.ACTION_VIEW); - String transformedURL = transformUrl(TransformActivity.this, url); - if (transformedURL != null) { - delegate.setData(Uri.parse(transformUrl(TransformActivity.this, url))); - delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - if (BuildConfig.fullLinks) { - forwardToBrowser(TransformActivity.this, delegate); - } else { - if (delegate.resolveActivity(getPackageManager()) != null) { - startActivity(delegate); - finish(); - } - } - } else { - forwardToBrowser(TransformActivity.this, intent); - } - } else { - forwardToBrowser(TransformActivity.this, intent); - } - } - //Transform an Invidious URL from an instance to another one selected by the end user. - else if (Arrays.asList(invidious_instances).contains(host)) { - boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true); - if (invidious_enabled) { - String invidiousHost = sharedpreferences.getString(MainActivity.SET_INVIDIOUS_HOST, MainActivity.DEFAULT_INVIDIOUS_HOST).toLowerCase(); - String transformedURL = url; - if (host != null && host.compareTo(invidiousHost) != 0) { - if (!invidiousHost.startsWith("http")) { - transformedURL = url.replace(host, invidiousHost); - } else { - transformedURL = url.replace("https://" + host, invidiousHost).replace("http://" + host, invidiousHost); - } - } - transformedURL = Utils.replaceInvidiousParams(TransformActivity.this, transformedURL); - intent.setData(Uri.parse(transformedURL)); - } - forwardToBrowser(TransformActivity.this, intent); - } - //Transform a Nitter URL from an instance to another one selected by the end user. - else if (Arrays.asList(nitter_instances).contains(host)) { - boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true); - if (nitter_enabled) { - String nitterHost = sharedpreferences.getString(MainActivity.SET_NITTER_HOST, MainActivity.DEFAULT_NITTER_HOST).toLowerCase(); - String transformedURL = url; - if (host != null && host.compareTo(nitterHost) != 0) { - if (!nitterHost.startsWith("http")) { - transformedURL = url.replace(host, nitterHost); - } else { - transformedURL = url.replace("https://" + host, nitterHost).replace("http://" + host, nitterHost); - } - } - intent.setData(Uri.parse(transformedURL)); - } - forwardToBrowser(TransformActivity.this, intent); - } - //Transform a Bibliogram URL from an instance to another one selected by the end user. - else if (Arrays.asList(bibliogram_instances).contains(host)) { - boolean bibliogram_enabled = sharedpreferences.getBoolean(SET_BIBLIOGRAM_ENABLED, true); - if (bibliogram_enabled) { - String bibliogramHost = sharedpreferences.getString(MainActivity.SET_BIBLIOGRAM_HOST, MainActivity.DEFAULT_BIBLIOGRAM_HOST).toLowerCase(); - String transformedURL = url; - if (host != null && host.compareTo(bibliogramHost) != 0) { - if (!bibliogramHost.startsWith("http")) { - transformedURL = url.replace(host, bibliogramHost); - } else { - transformedURL = url.replace("https://" + host, bibliogramHost).replace("http://" + host, bibliogramHost); - } - } - intent.setData(Uri.parse(transformedURL)); - - } - forwardToBrowser(TransformActivity.this, intent); - } else if (host != null && host.contains(outlook_safe_domain)) { - Matcher matcher = outlookRedirect.matcher(url); - if (matcher.find()) { - url = matcher.group(3); - try { - url = URLDecoder.decode(url, "UTF-8"); - } catch (UnsupportedEncodingException ignored) { - } - } - intent.setData(Uri.parse(url)); - forwardToBrowser(TransformActivity.this, intent); } else { String newUrl = remove_tracking_param(url); try { @@ -311,7 +130,6 @@ public class TransformActivity extends Activity { * @param extraText String the new extra text */ private void share(String extraText) { - SharedPreferences sharedpreferences = getSharedPreferences(MainActivity.APP_PREFS, Context.MODE_PRIVATE); String url = null; if (extraText != null) { @@ -328,15 +146,7 @@ public class TransformActivity extends Activity { } } } - URL url_; - String host = null; - try { - url_ = new URL(url); - host = url_.getHost(); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - String newUrl = null; + String newUrl; if (url == null) { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); @@ -346,154 +156,27 @@ public class TransformActivity extends Activity { return; } url = Utils.remove_tracking_param(url); - Uri url_r = Uri.parse(url); - String scheme = url_r.getScheme(); - if (scheme == null) { - scheme = "https://"; - } else { - scheme += "://"; + URL url_; + String host = null; + try { + url_ = new URL(url); + host = url_.getHost(); + } catch (MalformedURLException e) { + e.printStackTrace(); } - if (Arrays.asList(twitter_domains).contains(host)) { - boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true); - if (nitter_enabled) { - - String nitterHost = sharedpreferences.getString(MainActivity.SET_NITTER_HOST, MainActivity.DEFAULT_NITTER_HOST).toLowerCase(); - if (nitterHost.startsWith("http")) { - scheme = ""; - } - assert host != null; - if (host.compareTo("pbs.twimg.com") == 0 || host.compareTo("pic.twitter.com") == 0) { - try { - newUrl = scheme + nitterHost + "/pic/" + URLEncoder.encode(url, "utf-8"); - } catch (UnsupportedEncodingException e) { - newUrl = scheme + nitterHost + "/pic/" + url; - } - } else if (url.contains("/search?")) { - newUrl = url.replace(host, nitterHost); - } else { - Matcher matcher = nitterPattern.matcher(url); - while (matcher.find()) { - final String nitter_directory = matcher.group(2); - newUrl = scheme + nitterHost + nitter_directory; - } - } + if (Arrays.asList(shortener_domains).contains(host)) { + Uri url_r = Uri.parse(url); + String scheme = url_r.getScheme(); + if (scheme == null) { + scheme = "https://"; + } else { + scheme += "://"; } - } else if (Arrays.asList(instagram_domains).contains(host)) { - boolean bibliogram_enabled = sharedpreferences.getBoolean(SET_BIBLIOGRAM_ENABLED, true); - if (bibliogram_enabled) { - String bibliogramHost = sharedpreferences.getString(MainActivity.SET_BIBLIOGRAM_HOST, MainActivity.DEFAULT_BIBLIOGRAM_HOST).toLowerCase(); - if (bibliogramHost.startsWith("http")) { - scheme = ""; - } - Matcher matcher = bibliogramPostPattern.matcher(url); - while (matcher.find()) { - final String bibliogram_directory = matcher.group(2); - - newUrl = scheme + bibliogramHost + bibliogram_directory; - } - matcher = bibliogramAccountPattern.matcher(url); - while (matcher.find()) { - final String bibliogram_directory = matcher.group(2); - if (bibliogram_directory != null && bibliogram_directory.compareTo("privacy") != 0 && !bibliogram_directory.startsWith("/tv/") && !bibliogram_directory.startsWith("/reel/") && !bibliogram_directory.startsWith("/igtv/")) { - newUrl = scheme + bibliogramHost + "/u" + bibliogram_directory; - } else { - newUrl = scheme + bibliogramHost + bibliogram_directory; - } - } - } - } else if (url.contains("/maps/place/")) { - boolean osm_enabled = sharedpreferences.getBoolean(MainActivity.SET_OSM_ENABLED, true); - if (osm_enabled) { - Matcher matcher = maps.matcher(url); - while (matcher.find()) { - final String localization = matcher.group(2); - assert localization != null; - String[] data = localization.split(","); - if (data.length > 2) { - String zoom; - String[] details = data[2].split("\\."); - if (details.length > 0) { - zoom = details[0]; - } else { - zoom = data[2]; - } - String osmHost = sharedpreferences.getString(MainActivity.SET_OSM_HOST, MainActivity.DEFAULT_OSM_HOST).toLowerCase(); - newUrl = scheme + osmHost + "/#map=" + zoom + "/" + data[0] + "/" + data[1]; - } - } - } - } else if (url.contains("/amp/s/")) { - Matcher matcher = ampExtract.matcher(url); - while (matcher.find()) { - newUrl = scheme + matcher.group(1); - } - } else if (Arrays.asList(youtube_domains).contains(host)) { //Youtube URL - boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true); - if (invidious_enabled) { - String invidiousHost = sharedpreferences.getString(MainActivity.SET_INVIDIOUS_HOST, MainActivity.DEFAULT_INVIDIOUS_HOST).toLowerCase(); - if (invidiousHost.startsWith("http")) { - scheme = ""; - } - Matcher matcher = youtubePattern.matcher(url); - while (matcher.find()) { - final String youtubeId = matcher.group(3); - if (Objects.requireNonNull(matcher.group(2)).compareTo("youtu.be") == 0) { - newUrl = scheme + invidiousHost + "/watch?v=" + youtubeId; - } else { - newUrl = scheme + invidiousHost + "/" + youtubeId; - } - } - } - } else if (Arrays.asList(shortener_domains).contains(host)) { manageShortenedShare(TransformActivity.this, url, extraText, scheme); return; - } else if (Arrays.asList(invidious_instances).contains(host)) { - boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true); - newUrl = url; - if (invidious_enabled) { - String invidiousHost = sharedpreferences.getString(MainActivity.SET_INVIDIOUS_HOST, MainActivity.DEFAULT_INVIDIOUS_HOST).toLowerCase(); - if (host != null && host.compareTo(invidiousHost) != 0) { - if (!invidiousHost.startsWith("http")) { - newUrl = url.replace(host, invidiousHost); - } else { - newUrl = url.replace("https://" + host, invidiousHost).replace("http://" + host, invidiousHost); - } - } - - newUrl = Utils.replaceInvidiousParams(TransformActivity.this, newUrl); - } - } - //Transform a Nitter URL from an instance to another one selected by the end user. - else if (Arrays.asList(nitter_instances).contains(host)) { - newUrl = url; - boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true); - if (nitter_enabled) { - String nitterHost = sharedpreferences.getString(MainActivity.SET_NITTER_HOST, MainActivity.DEFAULT_NITTER_HOST).toLowerCase(); - if (host != null && host.compareTo(nitterHost) != 0) { - if (!nitterHost.startsWith("http")) { - newUrl = url.replace(host, nitterHost); - } else { - newUrl = url.replace("https://" + host, nitterHost).replace("http://" + host, nitterHost); - } - } - } - } - //Transform a Bibliogram URL from an instance to another one selected by the end user. - else if (Arrays.asList(bibliogram_instances).contains(host)) { - newUrl = url; - boolean bibliogram_enabled = sharedpreferences.getBoolean(SET_BIBLIOGRAM_ENABLED, true); - if (bibliogram_enabled) { - String bibliogramHost = sharedpreferences.getString(MainActivity.SET_BIBLIOGRAM_HOST, MainActivity.DEFAULT_BIBLIOGRAM_HOST).toLowerCase(); - if (host != null && host.compareTo(bibliogramHost) != 0) { - if (!bibliogramHost.startsWith("http")) { - newUrl = url.replace(host, bibliogramHost); - } else { - newUrl = url.replace("https://" + host, bibliogramHost).replace("http://" + host, bibliogramHost); - } - } - } } + newUrl = transformUrl(TransformActivity.this, url); if (newUrl != null) { extraText = extraText.replaceAll(Pattern.quote(url), Matcher.quoteReplacement(newUrl)); } diff --git a/app/src/main/java/app/fedilab/nitterizeme/helpers/Utils.java b/app/src/main/java/app/fedilab/nitterizeme/helpers/Utils.java index db5186d..72eb654 100644 --- a/app/src/main/java/app/fedilab/nitterizeme/helpers/Utils.java +++ b/app/src/main/java/app/fedilab/nitterizeme/helpers/Utils.java @@ -47,6 +47,7 @@ import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLDecoder; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -70,8 +71,11 @@ import app.fedilab.nitterizeme.activities.MainActivity; import app.fedilab.nitterizeme.activities.WebviewPlayerActivity; import static android.content.Context.DOWNLOAD_SERVICE; +import static app.fedilab.nitterizeme.activities.CheckAppActivity.bibliogram_instances; import static app.fedilab.nitterizeme.activities.CheckAppActivity.instagram_domains; import static app.fedilab.nitterizeme.activities.CheckAppActivity.invidious_instances; +import static app.fedilab.nitterizeme.activities.CheckAppActivity.nitter_instances; +import static app.fedilab.nitterizeme.activities.CheckAppActivity.outlook_safe_domain; import static app.fedilab.nitterizeme.activities.CheckAppActivity.shortener_domains; import static app.fedilab.nitterizeme.activities.CheckAppActivity.twitter_domains; import static app.fedilab.nitterizeme.activities.CheckAppActivity.youtube_domains; @@ -333,6 +337,13 @@ public class Utils { } else { return url; } + } else if (url.contains("/amp/s/")) { + Matcher matcher = ampExtract.matcher(url); + String transformedURL = url; + while (matcher.find()) { + transformedURL = "https://" + matcher.group(1); + } + return transformedURL; } else if (Arrays.asList(youtube_domains).contains(host)) { //Youtube URL boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true); if (invidious_enabled) { @@ -360,6 +371,70 @@ public class Utils { } else { return url; } + } else if (Arrays.asList(invidious_instances).contains(host)) { + boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true); + newUrl = url; + if (invidious_enabled) { + String invidiousHost = sharedpreferences.getString(MainActivity.SET_INVIDIOUS_HOST, MainActivity.DEFAULT_INVIDIOUS_HOST); + assert invidiousHost != null; + invidiousHost = invidiousHost.toLowerCase(); + if (host != null && host.compareTo(invidiousHost) != 0) { + if (!invidiousHost.startsWith("http")) { + newUrl = url.replace(host, invidiousHost); + } else { + newUrl = url.replace("https://" + host, invidiousHost).replace("http://" + host, invidiousHost); + } + } + newUrl = Utils.replaceInvidiousParams(context, newUrl); + } + return newUrl; + } + //Transform a Nitter URL from an instance to another one selected by the end user. + else if (Arrays.asList(nitter_instances).contains(host)) { + newUrl = url; + boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true); + if (nitter_enabled) { + String nitterHost = sharedpreferences.getString(MainActivity.SET_NITTER_HOST, MainActivity.DEFAULT_NITTER_HOST); + assert nitterHost != null; + nitterHost = nitterHost.toLowerCase(); + if (host != null && host.compareTo(nitterHost) != 0) { + if (!nitterHost.startsWith("http")) { + newUrl = url.replace(host, nitterHost); + } else { + newUrl = url.replace("https://" + host, nitterHost).replace("http://" + host, nitterHost); + } + } + } + return newUrl; + } + //Transform a Bibliogram URL from an instance to another one selected by the end user. + else if (Arrays.asList(bibliogram_instances).contains(host)) { + newUrl = url; + boolean bibliogram_enabled = sharedpreferences.getBoolean(SET_BIBLIOGRAM_ENABLED, true); + if (bibliogram_enabled) { + String bibliogramHost = sharedpreferences.getString(MainActivity.SET_BIBLIOGRAM_HOST, MainActivity.DEFAULT_BIBLIOGRAM_HOST); + assert bibliogramHost != null; + bibliogramHost = bibliogramHost.toLowerCase(); + if (host != null && host.compareTo(bibliogramHost) != 0) { + if (!bibliogramHost.startsWith("http")) { + newUrl = url.replace(host, bibliogramHost); + } else { + newUrl = url.replace("https://" + host, bibliogramHost).replace("http://" + host, bibliogramHost); + } + } + } + return newUrl; + } else if (host != null && host.contains(outlook_safe_domain)) { + newUrl = url; + Matcher matcher = outlookRedirect.matcher(url); + if (matcher.find()) { + String tmp_url = matcher.group(3); + try { + newUrl = URLDecoder.decode(tmp_url, "UTF-8"); + } catch (UnsupportedEncodingException ignored) { + } + } + return newUrl; } return url; } @@ -648,7 +723,7 @@ public class Utils { * * @return boolean */ - @SuppressWarnings({"unused", "SameParameterValue"}) + @SuppressWarnings({"SameParameterValue"}) private static boolean isAppInstalled(Context context, String packageName) { try { context.getPackageManager().getPackageInfo(packageName, 0); @@ -663,7 +738,6 @@ public class Utils { * * @return PackageInfo */ - @SuppressWarnings("unused") public static PackageInfo getPackageInfo(Context context, String packageName) { PackageInfo packageInfo = null; try { @@ -985,4 +1059,44 @@ public class Utils { } + public static boolean isRouted(String url) { + + URL url_; + String host = null; + try { + url_ = new URL(url); + host = url_.getHost(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + return Arrays.asList(twitter_domains).contains(host) || Arrays.asList(nitter_instances).contains(host) + || Arrays.asList(instagram_domains).contains(host) || Arrays.asList(bibliogram_instances).contains(host) + || url.contains("/maps/place") || url.contains("/amp/s/") || (host != null && host.contains(outlook_safe_domain)) + || Arrays.asList(youtube_domains).contains(host) || Arrays.asList(invidious_instances).contains(host); + } + + public static boolean routerEnabledForHost(Context context, String url) { + + URL url_; + String host = null; + try { + url_ = new URL(url); + host = url_.getHost(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + SharedPreferences sharedpreferences = context.getSharedPreferences(MainActivity.APP_PREFS, Context.MODE_PRIVATE); + if (Arrays.asList(twitter_domains).contains(host) || Arrays.asList(nitter_instances).contains(host)) { + return sharedpreferences.getBoolean(SET_NITTER_ENABLED, true); + } else if (Arrays.asList(instagram_domains).contains(host) || Arrays.asList(bibliogram_instances).contains(host)) { + return sharedpreferences.getBoolean(SET_BIBLIOGRAM_ENABLED, true); + } else if (url.contains("/maps/place")) { + return sharedpreferences.getBoolean(MainActivity.SET_OSM_ENABLED, true); + } else if (Arrays.asList(youtube_domains).contains(host) || Arrays.asList(invidious_instances).contains(host)) { + return sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true); + } else + return url.contains("/amp/s/") || (host != null && host.contains(outlook_safe_domain)); + } + + }