Show organization for host

This commit is contained in:
M66B 2019-03-09 15:46:49 +00:00
parent 5aa35797f1
commit 0f294e4f4b
5 changed files with 72 additions and 3 deletions

View File

@ -1704,18 +1704,58 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
}
private void onOpenLink(Uri uri) {
private void onOpenLink(final Uri uri) {
if (BuildConfig.APPLICATION_ID.equals(uri.getHost()) && "/activate/".equals(uri.getPath())) {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_ACTIVATE_PRO)
.putExtra("uri", uri));
} else {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
View view = LayoutInflater.from(context).inflate(R.layout.dialog_link, null);
final EditText etLink = view.findViewById(R.id.etLink);
final CheckBox cbOrganization = view.findViewById(R.id.cbOrganization);
TextView tvInsecure = view.findViewById(R.id.tvInsecure);
cbOrganization.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prefs.edit().putBoolean("show_organization", isChecked).apply();
if (isChecked) {
Bundle args = new Bundle();
args.putParcelable("uri", uri);
new SimpleTask<String>() {
@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)

View File

@ -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

View File

@ -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;
}
}
}

View File

@ -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" />
<CheckBox
android:id="@+id/cbOrganization"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="@string/title_show_organization"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etLink" />
<TextView
android:id="@+id/tvInsecure"
android:layout_width="wrap_content"
@ -36,5 +46,5 @@
android:textColor="?attr/colorWarning"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etLink" />
app:layout_constraintTop_toBottomOf="@id/cbOrganization" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -523,6 +523,7 @@
<string name="title_hint_sync">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.</string>
<string name="title_open_link">Open link</string>
<string name="title_show_organization">Show organization</string>
<string name="title_insecure_link">This link is insecure</string>
<string name="title_select_app">Select app</string>
<string name="title_updated">There is an update to version %1$s available</string>