From 0f294e4f4b2b2613b3d0996587c570dc7647ebed Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 9 Mar 2019 15:46:49 +0000 Subject: [PATCH] Show organization for host --- .../eu/faircode/email/AdapterMessage.java | 42 ++++++++++++++++++- .../eu/faircode/email/FragmentOptions.java | 2 +- .../main/java/eu/faircode/email/Helper.java | 18 ++++++++ app/src/main/res/layout/dialog_link.xml | 12 +++++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 07a3c8c5f1..05e83d8a1e 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -1704,18 +1704,58 @@ public class AdapterMessage extends RecyclerView.Adapter() { + @Override + protected void onPreExecute(Bundle args) { + cbOrganization.setText("…"); + } + + @Override + protected String onExecute(Context context, Bundle args) throws Throwable { + Uri uri = args.getParcelable("uri"); + String host = uri.getHost(); + return (TextUtils.isEmpty(host) ? null : Helper.getOrganization(host)); + } + + @Override + protected void onExecuted(Bundle args, String organization) { + cbOrganization.setText(organization == null ? "?" : organization); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + cbOrganization.setText(ex.getMessage()); + } + }.execute(context, owner, args, "link:domain"); + } else + cbOrganization.setText(R.string.title_show_organization); + } + }); + etLink.setText(uri.toString()); + cbOrganization.setChecked(prefs.getBoolean("show_organization", true)); tvInsecure.setVisibility("http".equals(uri.getScheme()) ? View.VISIBLE : View.GONE); new DialogBuilderLifecycle(context, owner) diff --git a/app/src/main/java/eu/faircode/email/FragmentOptions.java b/app/src/main/java/eu/faircode/email/FragmentOptions.java index 0e9b67bf53..e98ca6c047 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptions.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptions.java @@ -123,7 +123,7 @@ public class FragmentOptions extends FragmentBase implements SharedPreferences.O "notify_preview", "search_local", "light", "sound", "updates", "debug", "first", "why", "last_update_check", "app_support", "message_swipe", "message_select", "folder_actions", "folder_sync", - "edit_ref_confirmed", "show_html_confirmed", "show_images_confirmed", "print_html_confirmed" + "edit_ref_confirmed", "show_html_confirmed", "show_images_confirmed", "print_html_confirmed", "show_organization" }; @Override diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index 2226c76233..ec62a107af 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -73,6 +73,8 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.net.InetAddress; +import java.net.URL; import java.net.UnknownHostException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -93,6 +95,7 @@ import javax.mail.MessageRemovedException; import javax.mail.MessagingException; import javax.mail.Store; import javax.mail.internet.InternetAddress; +import javax.net.ssl.HttpsURLConnection; import androidx.annotation.NonNull; import androidx.browser.customtabs.CustomTabsIntent; @@ -1000,4 +1003,19 @@ public class Helper { static String sanitizeFilename(String name) { return (name == null ? null : name.replaceAll("[^a-zA-Z0-9\\.\\-]", "_")); } + + static String getOrganization(String host) throws IOException { + InetAddress address = InetAddress.getByName(host); + URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org"); + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setReadTimeout(15 * 1000); + connection.connect(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + String organization = reader.readLine(); + if ("undefined".equals(organization)) + organization = null; + return organization; + } + } } diff --git a/app/src/main/res/layout/dialog_link.xml b/app/src/main/res/layout/dialog_link.xml index d8b28a97e4..c84b360433 100644 --- a/app/src/main/res/layout/dialog_link.xml +++ b/app/src/main/res/layout/dialog_link.xml @@ -12,6 +12,7 @@ android:text="@string/title_open_link" android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textColor="?android:attr/textColorPrimary" + android:textStyle="bold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> @@ -26,6 +27,15 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tvOpenLink" /> + + + app:layout_constraintTop_toBottomOf="@id/cbOrganization" /> \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e6303ee771..f081d824e9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -523,6 +523,7 @@ Downloading messages can take some time, depending on the speed of the provider, internet connection and device and on the number of messages. While downloading messages the app might respond slower. Open link + Show organization This link is insecure Select app There is an update to version %1$s available