From 15cc220fb064cc2d2032b73d89f263e37ae159fd Mon Sep 17 00:00:00 2001 From: Distopico Vegan Date: Sat, 7 Nov 2020 13:21:58 -0500 Subject: [PATCH] fix: gmail authentication over non-oAuth closes: https://framagit.org/dystopia-project/simple-email/-/issues/39 --- .../org/dystopia/email/FragmentAccount.java | 59 +- .../org/dystopia/email/ViewButtonColor.java | 2 +- app/src/main/res/layout/fragment_account.xml | 1105 +++++++++-------- app/src/main/res/values-en/strings.xml | 2 + app/src/main/res/values/dimens.xml | 2 +- app/src/main/res/values/strings.xml | 4 +- docs/FAQ.md | 17 +- 7 files changed, 624 insertions(+), 567 deletions(-) diff --git a/app/src/main/java/org/dystopia/email/FragmentAccount.java b/app/src/main/java/org/dystopia/email/FragmentAccount.java index 31068f31..1a2a0e56 100644 --- a/app/src/main/java/org/dystopia/email/FragmentAccount.java +++ b/app/src/main/java/org/dystopia/email/FragmentAccount.java @@ -40,8 +40,12 @@ import android.os.Handler; import android.preference.PreferenceManager; import android.text.Editable; import android.text.Html; +import android.text.SpannableString; +import android.text.Spanned; import android.text.TextUtils; import android.text.TextWatcher; +import android.text.method.LinkMovementMethod; +import android.text.util.Linkify; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -60,6 +64,7 @@ import android.widget.Spinner; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import androidx.constraintlayout.widget.Group; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentResultListener; @@ -100,7 +105,7 @@ public class FragmentAccount extends FragmentEx { private TextInputLayout tilPassword; private Button btnAuthorize; - + private TextView tvGmailNote; private Button btnAdvanced; private TextView tvName; @@ -184,6 +189,7 @@ public class FragmentAccount extends FragmentEx { tilPassword = view.findViewById(R.id.tilPassword); btnAuthorize = view.findViewById(R.id.btnAuthorize); + tvGmailNote = view.findViewById(R.id.tvGmailNote); btnAdvanced = view.findViewById(R.id.btnAdvanced); etName = view.findViewById(R.id.etName); @@ -230,7 +236,18 @@ public class FragmentAccount extends FragmentEx { cbInsecure.setVisibility(position == 1 && insecure ? View.VISIBLE : View.GONE); grpAuthorize.setVisibility(position > 0 ? View.VISIBLE : View.GONE); - btnAuthorize.setVisibility(provider.type == null ? View.GONE : View.VISIBLE); + // TODO: since December 31, 2019 Gmail retricted the oAuth + // gmail scopes and it require privative google play services java libraries + // so, only with "insecure" app enabled is supported + // see: https://developers.google.com/terms/api-services-user-data-policy#additional-requirements-for-specific-api-scopes + // + // maybe with webview and a web library? + btnAuthorize.setVisibility(View.GONE); + tvGmailNote.setVisibility(View.GONE); + if (provider.type != null) { + tvGmailNote.setVisibility(View.VISIBLE); + tvGmailNote.setText(Html.fromHtml(getString(R.string.text_gmail_note))); + } btnAdvanced.setVisibility(position > 0 ? View.VISIBLE : View.GONE); if (position == 0) { @@ -354,6 +371,24 @@ public class FragmentAccount extends FragmentEx { } }); + tvGmailNote.setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + Spanned htmlMessage = Html.fromHtml(getString(R.string.message_gmail_note)); + final SpannableString dialogMessage = new SpannableString(htmlMessage); + Linkify.addLinks(dialogMessage, Linkify.WEB_URLS); + + AlertDialog dialog = new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) + .setMessage(dialogMessage) + .setPositiveButton(android.R.string.ok, null) + .show(); + + ((TextView) dialog.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance()); + } + } + ); + btnAdvanced.setOnClickListener( new View.OnClickListener() { @Override @@ -420,9 +455,7 @@ public class FragmentAccount extends FragmentEx { args.putString("port", etPort.getText().toString()); args.putString("user", etUser.getText().toString()); args.putString("password", tilPassword.getEditText().getText().toString()); - args.putInt( - "auth_type", - authorized == null ? Helper.AUTH_TYPE_PASSWORD : provider.getAuthType()); + args.putInt("auth_type", provider.getAuthType()); new SimpleTask() { @Override @@ -457,14 +490,24 @@ public class FragmentAccount extends FragmentEx { Session isession = Session.getInstance(props, null); isession.setDebug(true); IMAPStore istore = null; + try { istore = (IMAPStore) isession.getStore(starttls ? "imap" : "imaps"); + String originalPassword = password; try { + if (auth_type == Helper.AUTH_TYPE_GMAIL) { + password = Helper.refreshToken(context, "com.google", user, password); + } istore.connect(host, Integer.parseInt(port), user, password); } catch (AuthenticationFailedException ex) { + // Try normal imap access with gmail allowed "insecure" app enabled if (auth_type == Helper.AUTH_TYPE_GMAIL) { - password = Helper.refreshToken(context, "com.google", user, password); - istore.connect(host, Integer.parseInt(port), user, password); + auth_type = Helper.AUTH_TYPE_PASSWORD; + args.putInt("auth_type", auth_type); + props = MessageHelper.getSessionProperties(auth_type, insecure); + isession = Session.getInstance(props, null); + istore = (IMAPStore) isession.getStore(starttls ? "imap" : "imaps"); + istore.connect(host, Integer.parseInt(port), user, originalPassword); } else { throw ex; } @@ -1183,7 +1226,7 @@ public class FragmentAccount extends FragmentEx { } } }, - null); + null); break; } } diff --git a/app/src/main/java/org/dystopia/email/ViewButtonColor.java b/app/src/main/java/org/dystopia/email/ViewButtonColor.java index 82f89578..0fc2d8c0 100644 --- a/app/src/main/java/org/dystopia/email/ViewButtonColor.java +++ b/app/src/main/java/org/dystopia/email/ViewButtonColor.java @@ -66,7 +66,7 @@ public class ViewButtonColor extends AppCompatButton { GradientDrawable background = new GradientDrawable(); background.setColor(color); background.setStroke( - ViewHelper.dp2px(getContext(), 1), + ViewHelper.dp2px(getContext(), 3), Helper.resolveColor(getContext(), R.attr.colorSeparator)); background.setCornerRadius(50); setBackground(background); diff --git a/app/src/main/res/layout/fragment_account.xml b/app/src/main/res/layout/fragment_account.xml index 23d8d4e5..d7a086e4 100644 --- a/app/src/main/res/layout/fragment_account.xml +++ b/app/src/main/res/layout/fragment_account.xml @@ -1,550 +1,565 @@ - - + + + + + + + + + + + + + + - - - - - - - - - - - - - -