From 0e2b1ef8654a3f2c98aa11689efd557d1eb35a37 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 3 Mar 2019 16:07:09 +0000 Subject: [PATCH] Warn for insecure links --- .../eu/faircode/email/AdapterMessage.java | 4 + .../eu/faircode/email/FragmentCompose.java | 142 +++++++++++------- app/src/main/res/layout/dialog_link.xml | 13 ++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 104 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index f00692ab55..3e8452e2b7 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -1601,7 +1601,11 @@ public class AdapterMessage extends RecyclerView.Adapter end) { - int tmp = start; - start = end; - end = tmp; + int s = etBody.getSelectionStart(); + int e = etBody.getSelectionEnd(); + + if (s < 0) + s = 0; + if (e < 0) + e = 0; + + if (s > e) { + int tmp = s; + s = e; + e = tmp; } - if (start == end) - Snackbar.make(view, R.string.title_no_selection, Snackbar.LENGTH_LONG).show(); - else { - final SpannableString s = new SpannableString(etBody.getText()); - switch (id) { - case R.id.menu_bold: - s.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - break; + final int start = s; + final int end = e; - case R.id.menu_italic: - s.setSpan(new StyleSpan(Typeface.ITALIC), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - break; + final SpannableString ss = new SpannableString(etBody.getText()); - case R.id.menu_clear: - for (Object span : s.getSpans(start, end, Object.class)) - if (!(span instanceof ImageSpan)) - s.removeSpan(span); - break; + switch (id) { + case R.id.menu_bold: + if (start == end) + Snackbar.make(view, R.string.title_no_selection, Snackbar.LENGTH_LONG).show(); + else + ss.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + break; - case R.id.menu_link: - Uri uri = null; + case R.id.menu_italic: + if (start == end) + Snackbar.make(view, R.string.title_no_selection, Snackbar.LENGTH_LONG).show(); + else + ss.setSpan(new StyleSpan(Typeface.ITALIC), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + break; - ClipboardManager cbm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); - if (cbm.hasPrimaryClip()) { - String link = cbm.getPrimaryClip().getItemAt(0).coerceToText(getContext()).toString(); - uri = Uri.parse(link); - if (uri.getScheme() == null) - uri = null; + case R.id.menu_clear: + for (Object span : ss.getSpans(0, ss.length(), Object.class)) + if (!(span instanceof ImageSpan)) + ss.removeSpan(span); + break; + + case R.id.menu_link: + Uri uri = null; + + ClipboardManager cbm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); + if (cbm.hasPrimaryClip()) { + String link = cbm.getPrimaryClip().getItemAt(0).coerceToText(getContext()).toString(); + uri = Uri.parse(link); + if (uri.getScheme() == null) + uri = null; + } + + View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_link, null); + final EditText etLink = view.findViewById(R.id.etLink); + final TextView tvInsecure = view.findViewById(R.id.tvInsecure); + + etLink.setText(uri == null ? "https://" : uri.toString()); + tvInsecure.setVisibility(View.GONE); + + etLink.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { } - View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_link, null); - final int fStart = start; - final int fEnd = end; - final EditText etLink = view.findViewById(R.id.etLink); - etLink.setText(uri == null ? "https://" : uri.toString()); - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setView(view) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - s.setSpan(new URLSpan(etLink.getText().toString()), fStart, fEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - etBody.setText(s); - etBody.setSelection(fEnd); - } - }) - .show(); - new Handler().post(new Runnable() { - @Override - public void run() { - etLink.requestFocus(); - } - }); + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + tvInsecure.setVisibility("http".equals(Uri.parse(s.toString()).getScheme()) ? View.VISIBLE : View.GONE); + } - return; - } + @Override + public void afterTextChanged(Editable s) { + } + }); - etBody.setText(s); - etBody.setSelection(end); + new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) + .setView(view) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ss.setSpan(new URLSpan(etLink.getText().toString()), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + etBody.setText(ss); + etBody.setSelection(end); + } + }) + .show(); + new Handler().post(new Runnable() { + @Override + public void run() { + etLink.requestFocus(); + } + }); + + return; } + + etBody.setText(ss); + etBody.setSelection(end); } private void onMenuSendAfter() { diff --git a/app/src/main/res/layout/dialog_link.xml b/app/src/main/res/layout/dialog_link.xml index efdf37fd45..5e80347ce1 100644 --- a/app/src/main/res/layout/dialog_link.xml +++ b/app/src/main/res/layout/dialog_link.xml @@ -25,4 +25,17 @@ android:textAppearance="@style/TextAppearance.AppCompat.Small" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tvOpenLink" /> + + + \ 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 cc99ffd996..5f3677749b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -517,6 +517,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 + This link is insecure Select app There is an update to version %1$s available Do you have a question or problem?