FairEmail/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java

739 lines
33 KiB
Java
Raw Normal View History

2019-01-14 11:11:03 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2019-01-14 11:11:03 +00:00
*/
2021-08-09 17:31:35 +00:00
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD;
2019-01-14 11:11:03 +00:00
import android.content.Context;
import android.content.Intent;
2021-06-26 07:05:25 +00:00
import android.graphics.Paint;
2021-08-14 08:32:58 +00:00
import android.graphics.Typeface;
2020-07-29 12:06:40 +00:00
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
2019-07-06 14:24:03 +00:00
import android.net.Uri;
2019-01-14 11:11:03 +00:00
import android.os.Bundle;
import android.text.Editable;
2019-01-14 11:11:03 +00:00
import android.text.TextUtils;
import android.text.TextWatcher;
2019-01-14 11:11:03 +00:00
import android.text.method.LinkMovementMethod;
2019-03-03 11:16:00 +00:00
import android.view.KeyEvent;
2019-01-14 11:11:03 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2019-03-03 11:16:00 +00:00
import android.view.inputmethod.EditorInfo;
2019-01-14 11:11:03 +00:00
import android.widget.Button;
2022-03-31 07:25:12 +00:00
import android.widget.CheckBox;
2019-01-14 11:11:03 +00:00
import android.widget.EditText;
2019-07-06 14:24:03 +00:00
import android.widget.ScrollView;
2019-01-14 11:11:03 +00:00
import android.widget.TextView;
2022-03-31 07:25:12 +00:00
import android.widget.Toast;
2019-01-14 11:11:03 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.FragmentActivity;
2021-09-28 19:15:46 +00:00
import androidx.lifecycle.Lifecycle;
2019-01-14 11:11:03 +00:00
import com.google.android.material.textfield.TextInputLayout;
import java.net.UnknownHostException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
2021-10-09 06:23:53 +00:00
import java.util.Arrays;
2019-01-14 11:11:03 +00:00
import java.util.Date;
import java.util.List;
import javax.mail.AuthenticationFailedException;
import javax.mail.Folder;
import javax.mail.Store;
2019-01-14 11:11:03 +00:00
2019-01-15 17:41:55 +00:00
public class FragmentQuickSetup extends FragmentBase {
2019-01-14 11:11:03 +00:00
private ViewGroup view;
2019-07-06 14:24:03 +00:00
private ScrollView scroll;
2019-01-14 11:11:03 +00:00
2021-06-26 07:05:25 +00:00
private TextView tvPrivacy;
2019-01-14 11:11:03 +00:00
private EditText etName;
private EditText etEmail;
private TextInputLayout tilPassword;
private TextView tvCharacters;
2019-01-14 11:11:03 +00:00
private Button btnCheck;
2019-07-28 12:11:42 +00:00
private ContentLoadingProgressBar pbCheck;
2021-07-06 12:18:59 +00:00
private TextView tvPatience;
2019-02-18 11:39:43 +00:00
2019-01-14 11:11:03 +00:00
private TextView tvError;
2020-01-31 11:03:23 +00:00
private TextView tvErrorHint;
2021-07-08 17:47:06 +00:00
private TextView tvInstructions;
2019-07-06 14:24:03 +00:00
private Button btnHelp;
2019-09-09 14:44:06 +00:00
private Button btnSupport;
2019-01-14 11:11:03 +00:00
2021-10-10 17:54:16 +00:00
private TextView tvUser;
2019-02-18 11:39:43 +00:00
private TextView tvImap;
private TextView tvSmtp;
private TextView tvImapFingerprint;
private TextView tvImapDnsNames;
2020-05-18 19:41:56 +00:00
private TextView tvSmtpFingerprint;
private TextView tvSmtpDnsNames;
2022-03-31 07:25:12 +00:00
private CheckBox cbUpdate;
2019-02-18 11:39:43 +00:00
private Button btnSave;
2019-07-28 12:11:42 +00:00
private ContentLoadingProgressBar pbSave;
2019-02-18 11:39:43 +00:00
private Group grpSetup;
private Group grpCertificate;
2019-11-15 08:33:43 +00:00
private Group grpError;
2019-02-18 11:39:43 +00:00
2022-03-31 07:25:12 +00:00
private boolean update;
2021-10-10 17:54:16 +00:00
private EmailProvider bestProvider = null;
private Bundle bestArgs = null;
2021-06-26 07:05:25 +00:00
private static final String PRIVACY_URI = "https://www.mozilla.org/privacy/";
2021-10-10 17:54:16 +00:00
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("fair:password", tilPassword.getEditText().getText().toString());
outState.putParcelable("fair:best", bestProvider);
outState.putParcelable("fair:args", bestArgs);
}
2022-03-31 07:25:12 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
2022-03-31 15:33:53 +00:00
update = args.getBoolean("update", true);
2022-03-31 07:25:12 +00:00
}
2019-01-14 11:11:03 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
2021-05-08 11:22:01 +00:00
setSubtitle(R.string.title_setup_other);
2019-02-18 17:00:58 +00:00
setHasOptionsMenu(true);
2019-01-14 11:11:03 +00:00
view = (ViewGroup) inflater.inflate(R.layout.fragment_quick_setup, container, false);
2019-07-06 14:24:03 +00:00
scroll = view.findViewById(R.id.scroll);
2019-01-14 11:11:03 +00:00
// Get controls
2021-06-26 07:05:25 +00:00
tvPrivacy = view.findViewById(R.id.tvPrivacy);
2019-01-14 11:11:03 +00:00
etName = view.findViewById(R.id.etName);
etEmail = view.findViewById(R.id.etEmail);
tilPassword = view.findViewById(R.id.tilPassword);
tvCharacters = view.findViewById(R.id.tvCharacters);
2019-01-14 11:11:03 +00:00
btnCheck = view.findViewById(R.id.btnCheck);
2019-07-28 12:11:42 +00:00
pbCheck = view.findViewById(R.id.pbCheck);
2021-07-06 12:18:59 +00:00
tvPatience = view.findViewById(R.id.tvPatience);
2019-02-18 11:39:43 +00:00
2019-01-14 11:11:03 +00:00
tvError = view.findViewById(R.id.tvError);
2020-01-31 11:03:23 +00:00
tvErrorHint = view.findViewById(R.id.tvErrorHint);
2021-07-08 17:47:06 +00:00
tvInstructions = view.findViewById(R.id.tvInstructions);
2019-07-06 14:24:03 +00:00
btnHelp = view.findViewById(R.id.btnHelp);
2019-09-09 14:44:06 +00:00
btnSupport = view.findViewById(R.id.btnSupport);
2019-01-14 11:11:03 +00:00
2021-10-10 17:54:16 +00:00
tvUser = view.findViewById(R.id.tvUser);
2019-02-18 11:39:43 +00:00
tvImap = view.findViewById(R.id.tvImap);
tvSmtp = view.findViewById(R.id.tvSmtp);
tvImapFingerprint = view.findViewById(R.id.tvImapFingerprint);
tvImapDnsNames = view.findViewById(R.id.tvImapDnsNames);
2020-05-18 19:41:56 +00:00
tvSmtpFingerprint = view.findViewById(R.id.tvSmtpFingerprint);
tvSmtpDnsNames = view.findViewById(R.id.tvSmtpDnsNames);
2022-03-31 07:25:12 +00:00
cbUpdate = view.findViewById(R.id.cbUpdate);
2019-02-18 11:39:43 +00:00
btnSave = view.findViewById(R.id.btnSave);
2019-07-28 12:11:42 +00:00
pbSave = view.findViewById(R.id.pbSave);
2019-02-18 11:39:43 +00:00
grpSetup = view.findViewById(R.id.grpSetup);
grpCertificate = view.findViewById(R.id.grpCertificate);
2019-11-15 08:33:43 +00:00
grpError = view.findViewById(R.id.grpError);
2019-02-18 11:39:43 +00:00
2019-01-14 11:11:03 +00:00
// Wire controls
2021-06-26 07:05:25 +00:00
tvPrivacy.setPaintFlags(tvPrivacy.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
tvPrivacy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.view(v.getContext(), Uri.parse(PRIVACY_URI), false);
}
});
2019-01-14 11:11:03 +00:00
tilPassword.setHintEnabled(false);
2019-03-03 11:16:00 +00:00
tilPassword.getEditText().setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
onSave(true);
return true;
}
return false;
}
});
2019-09-21 19:08:50 +00:00
tilPassword.getEditText().addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Do nothing
}
@Override
public void afterTextChanged(Editable s) {
String password = s.toString();
boolean warning = (Helper.containsWhiteSpace(password) ||
Helper.containsControlChars(password));
tvCharacters.setVisibility(warning &&
tilPassword.getVisibility() == View.VISIBLE
? View.VISIBLE : View.GONE);
}
});
2019-01-14 11:11:03 +00:00
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2019-02-18 11:39:43 +00:00
onSave(true);
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSave(false);
2019-01-17 09:57:01 +00:00
}
});
2019-01-14 11:11:03 +00:00
2019-07-06 14:24:03 +00:00
btnHelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, (Uri) btnHelp.getTag());
Helper.view(getContext(), intent);
}
});
2019-09-09 14:44:06 +00:00
btnSupport.setOnClickListener(new View.OnClickListener() {
@Override
2021-04-06 06:59:35 +00:00
public void onClick(View v) {
Helper.view(v.getContext(), Helper.getSupportUri(v.getContext()), false);
2019-09-09 14:44:06 +00:00
}
});
2019-01-17 09:57:01 +00:00
// Initialize
tvCharacters.setVisibility(View.GONE);
2020-05-18 19:41:56 +00:00
tvImapFingerprint.setText(null);
tvSmtpFingerprint.setText(null);
2019-07-28 12:11:42 +00:00
pbCheck.setVisibility(View.GONE);
2021-07-06 12:18:59 +00:00
tvPatience.setVisibility(View.GONE);
2019-07-28 12:11:42 +00:00
pbSave.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
tvInstructions.setVisibility(View.GONE);
tvInstructions.setMovementMethod(LinkMovementMethod.getInstance());
2021-07-08 17:47:06 +00:00
btnHelp.setVisibility(View.GONE);
2022-03-31 07:25:12 +00:00
cbUpdate.setChecked(update);
cbUpdate.setVisibility(View.GONE);
2021-06-20 20:00:17 +00:00
btnSave.setVisibility(View.GONE);
2019-02-18 11:39:43 +00:00
grpSetup.setVisibility(View.GONE);
grpCertificate.setVisibility(View.GONE);
2019-11-15 08:33:43 +00:00
grpError.setVisibility(View.GONE);
2019-01-14 11:11:03 +00:00
2021-10-10 17:54:16 +00:00
if (savedInstanceState != null) {
tilPassword.getEditText().setText(savedInstanceState.getString("fair:password"));
bestProvider = savedInstanceState.getParcelable("fair:best");
bestArgs = savedInstanceState.getParcelable("fair:args");
showResult(bestProvider, bestArgs);
}
2019-01-17 09:57:01 +00:00
return view;
}
2019-01-14 11:11:03 +00:00
2019-02-18 11:39:43 +00:00
private void onSave(boolean check) {
2019-01-17 09:57:01 +00:00
Bundle args = new Bundle();
2019-10-02 16:29:04 +00:00
args.putString("name", etName.getText().toString().trim());
2019-01-17 09:57:01 +00:00
args.putString("email", etEmail.getText().toString().trim());
args.putString("password", tilPassword.getEditText().getText().toString());
2022-03-31 07:25:12 +00:00
args.putBoolean("update", cbUpdate.isChecked());
2019-02-18 11:39:43 +00:00
args.putBoolean("check", check);
2021-10-10 17:54:16 +00:00
args.putParcelable("best", bestProvider);
2019-01-14 11:11:03 +00:00
2019-02-18 11:39:43 +00:00
new SimpleTask<EmailProvider>() {
2019-01-17 09:57:01 +00:00
@Override
protected void onPreExecute(Bundle args) {
2019-02-18 11:39:43 +00:00
boolean check = args.getBoolean("check");
Helper.setViewsEnabled(view, false);
2019-07-28 12:11:42 +00:00
pbCheck.setVisibility(check ? View.VISIBLE : View.GONE);
2021-07-06 12:18:59 +00:00
tvPatience.setVisibility(check ? View.VISIBLE : View.GONE);
2019-07-28 12:11:42 +00:00
pbSave.setVisibility(check ? View.GONE : View.VISIBLE);
2019-11-15 08:33:43 +00:00
grpError.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
tvInstructions.setVisibility(View.GONE);
2021-07-08 17:47:06 +00:00
btnHelp.setVisibility(View.GONE);
2022-03-31 07:25:12 +00:00
cbUpdate.setVisibility(check ? View.GONE : View.VISIBLE);
2021-06-20 20:00:17 +00:00
btnSave.setVisibility(check ? View.GONE : View.VISIBLE);
2019-02-18 11:39:43 +00:00
grpSetup.setVisibility(check ? View.GONE : View.VISIBLE);
if (check)
grpCertificate.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
}
2019-01-14 11:11:03 +00:00
2019-01-17 09:57:01 +00:00
@Override
protected void onPostExecute(Bundle args) {
2019-02-18 11:39:43 +00:00
Helper.setViewsEnabled(view, true);
2019-07-28 12:11:42 +00:00
pbCheck.setVisibility(View.GONE);
2021-07-06 12:18:59 +00:00
tvPatience.setVisibility(View.GONE);
2019-07-28 12:11:42 +00:00
pbSave.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
}
2019-01-14 11:11:03 +00:00
2019-01-17 09:57:01 +00:00
@Override
2019-02-18 11:39:43 +00:00
protected EmailProvider onExecute(Context context, Bundle args) throws Throwable {
2019-10-02 16:29:04 +00:00
String name = args.getString("name");
String email = args.getString("email");
2019-01-17 09:57:01 +00:00
String password = args.getString("password");
2019-02-18 11:39:43 +00:00
boolean check = args.getBoolean("check");
2021-10-10 17:54:16 +00:00
EmailProvider best = args.getParcelable("best");
2019-01-17 09:57:01 +00:00
if (TextUtils.isEmpty(name))
throw new IllegalArgumentException(context.getString(R.string.title_no_name));
if (TextUtils.isEmpty(email))
throw new IllegalArgumentException(context.getString(R.string.title_no_email));
2020-03-05 13:45:29 +00:00
if (!Helper.EMAIL_ADDRESS.matcher(email).matches())
2019-09-08 16:22:48 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_email_invalid, email));
2019-10-02 12:54:10 +00:00
if (TextUtils.isEmpty(password))
throw new IllegalArgumentException(context.getString(R.string.title_no_password));
2019-01-17 09:57:01 +00:00
2021-08-16 16:45:47 +00:00
int at = email.indexOf('@');
String username = email.substring(0, at);
2020-07-29 12:06:40 +00:00
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ani = (cm == null ? null : cm.getActiveNetworkInfo());
if (ani == null || !ani.isConnected())
throw new IllegalArgumentException(context.getString(R.string.title_no_internet));
2021-08-16 16:45:47 +00:00
Throwable fail = null;
2021-10-10 17:54:16 +00:00
List<EmailProvider> providers;
if (best == null)
providers = EmailProvider.fromEmail(context, email, EmailProvider.Discover.ALL);
else
providers = Arrays.asList(best);
2021-08-16 16:45:47 +00:00
for (EmailProvider provider : providers)
2019-09-18 14:34:07 +00:00
try {
2021-08-16 19:14:25 +00:00
EntityLog.log(context, "Checking" +
" imap=" + provider.imap + " smtp=" + provider.smtp);
2021-08-16 16:45:47 +00:00
2021-08-16 20:37:44 +00:00
if (fail == null)
args.putParcelable("provider", provider);
2021-10-09 06:23:53 +00:00
List<String> users;
if (provider.user == EmailProvider.UserType.LOCAL)
users = Arrays.asList(username, email);
2022-03-20 10:35:16 +00:00
else if (provider.user == EmailProvider.UserType.VALUE) {
String user = provider.username;
if (user.startsWith("*@"))
user = username + user.substring(1);
users = Arrays.asList(user, email, username);
} else
2021-10-09 06:23:53 +00:00
users = Arrays.asList(email, username);
Log.i("User type=" + provider.user +
" users=" + TextUtils.join(", ", users));
2021-08-16 16:45:47 +00:00
List<EntityFolder> folders;
String imap_fingerprint = null;
String smtp_fingerprint = null;
X509Certificate imap_certificate = null;
X509Certificate smtp_certificate = null;
2021-10-09 06:23:53 +00:00
String user = null;
2021-08-16 16:45:47 +00:00
String aprotocol = (provider.imap.starttls ? "imap" : "imaps");
int aencryption = (provider.imap.starttls ? EmailService.ENCRYPTION_STARTTLS : EmailService.ENCRYPTION_SSL);
try (EmailService iservice = new EmailService(
context, aprotocol, null, aencryption, false, EmailService.PURPOSE_CHECK, true)) {
2021-10-09 06:23:53 +00:00
List<Throwable> exceptions = new ArrayList<>();
for (int i = 0; i < users.size(); i++) {
user = users.get(i);
Log.i("Trying with user=" + user);
try {
iservice.connect(
provider.imap.host, provider.imap.port,
AUTH_TYPE_PASSWORD, null,
user, password,
null, null);
break;
} catch (EmailService.UntrustedException ex) {
imap_certificate = ex.getCertificate();
imap_fingerprint = EntityCertificate.getKeyFingerprint(imap_certificate);
iservice.connect(
provider.imap.host, provider.imap.port,
AUTH_TYPE_PASSWORD, null,
user, password,
null, imap_fingerprint);
break;
} catch (Throwable ex) {
Log.w(ex);
// Why not AuthenticationFailedException?
// Some providers terminate the connection with an invalid username
exceptions.add(ex);
if (i + 1 == users.size()) {
for (Throwable e : exceptions)
if (e instanceof AuthenticationFailedException)
throw ex;
throw exceptions.get(0);
2021-08-16 16:45:47 +00:00
}
2021-10-09 06:23:53 +00:00
}
}
2021-08-11 12:45:37 +00:00
folders = iservice.getFolders();
2021-08-16 16:45:47 +00:00
if (folders.size() == 1 &&
EntityFolder.INBOX.equals(folders.get(0).type))
try {
Log.i("Creating system folders");
Store istore = iservice.getStore();
istore.getFolder(EntityFolder.DRAFTS).create(Folder.HOLDS_FOLDERS);
istore.getFolder(EntityFolder.SENT).create(Folder.HOLDS_FOLDERS);
istore.getFolder(EntityFolder.ARCHIVE).create(Folder.HOLDS_FOLDERS);
istore.getFolder(EntityFolder.TRASH).create(Folder.HOLDS_FOLDERS);
istore.getFolder(EntityFolder.JUNK).create(Folder.HOLDS_FOLDERS);
folders = iservice.getFolders();
} catch (Throwable ex) {
Log.e(ex);
}
2021-08-11 12:45:37 +00:00
}
2019-01-17 09:57:01 +00:00
2021-08-16 16:45:47 +00:00
Long max_size;
String iprotocol = (provider.smtp.starttls ? "smtp" : "smtps");
int iencryption = (provider.smtp.starttls ? EmailService.ENCRYPTION_STARTTLS : EmailService.ENCRYPTION_SSL);
try (EmailService iservice = new EmailService(
context, iprotocol, null, iencryption, false,
EmailService.PURPOSE_CHECK, true)) {
iservice.setUseIp(provider.useip, null);
try {
iservice.connect(
provider.smtp.host, provider.smtp.port,
AUTH_TYPE_PASSWORD, null,
user, password,
null, null);
} catch (EmailService.UntrustedException ex) {
smtp_certificate = ex.getCertificate();
2021-08-16 19:14:25 +00:00
smtp_fingerprint = EntityCertificate.getKeyFingerprint(smtp_certificate);
iservice.connect(
provider.smtp.host, provider.smtp.port,
AUTH_TYPE_PASSWORD, null,
user, password,
null, smtp_fingerprint);
2021-08-16 16:45:47 +00:00
}
2021-08-16 16:45:47 +00:00
max_size = iservice.getMaxSize();
}
2019-01-17 09:57:01 +00:00
2021-08-16 16:45:47 +00:00
if (check) {
2021-10-11 06:13:29 +00:00
args.putString("user", user);
2021-08-16 16:45:47 +00:00
args.putSerializable("imap_certificate", imap_certificate);
args.putSerializable("smtp_certificate", smtp_certificate);
return provider;
}
2019-02-18 11:39:43 +00:00
2022-03-31 07:25:12 +00:00
EntityAccount update = null;
2021-08-16 16:45:47 +00:00
DB db = DB.getInstance(context);
try {
db.beginTransaction();
2019-09-18 14:34:07 +00:00
2021-08-16 16:45:47 +00:00
EntityAccount primary = db.account().getPrimaryAccount();
2019-01-17 09:57:01 +00:00
2022-03-31 07:25:12 +00:00
if (args.getBoolean("update")) {
List<EntityAccount> accounts =
db.account().getAccounts(user, new int[]{AUTH_TYPE_PASSWORD});
if (accounts != null)
for (EntityAccount existing : accounts)
if (existing.protocol == EntityAccount.TYPE_IMAP)
if (update == null)
update = existing;
else {
update = null;
break;
}
}
2019-01-17 09:57:01 +00:00
2022-03-31 07:25:12 +00:00
if (update == null) {
// Create account
EntityAccount account = new EntityAccount();
account.host = provider.imap.host;
account.encryption = aencryption;
account.port = provider.imap.port;
account.auth_type = AUTH_TYPE_PASSWORD;
account.user = user;
account.password = password;
account.fingerprint = imap_fingerprint;
account.name = provider.name + "/" + username;
account.synchronize = true;
account.primary = (primary == null);
if (provider.keepalive > 0)
account.poll_interval = provider.keepalive;
account.partial_fetch = provider.partial;
account.created = new Date().getTime();
account.last_connected = account.created;
account.id = db.account().insertAccount(account);
args.putLong("account", account.id);
EntityLog.log(context, "Quick added account=" + account.name);
// Create folders
for (EntityFolder folder : folders) {
EntityFolder existing = db.folder().getFolderByName(account.id, folder.name);
if (existing == null) {
folder.account = account.id;
folder.setSpecials(account);
folder.id = db.folder().insertFolder(folder);
EntityLog.log(context, "Quick added folder=" + folder.name + " type=" + folder.type);
if (folder.synchronize)
EntityOperation.sync(context, folder.id, true);
}
2021-08-16 16:45:47 +00:00
}
2022-03-31 07:25:12 +00:00
// Set swipe left/right folder
for (EntityFolder folder : folders)
if (EntityFolder.TRASH.equals(folder.type))
account.swipe_left = folder.id;
else if (EntityFolder.ARCHIVE.equals(folder.type))
account.swipe_right = folder.id;
db.account().updateAccount(account);
// Create identity
EntityIdentity identity = new EntityIdentity();
identity.name = name;
identity.email = email;
identity.account = account.id;
identity.host = provider.smtp.host;
identity.encryption = iencryption;
identity.port = provider.smtp.port;
identity.auth_type = AUTH_TYPE_PASSWORD;
identity.user = user;
identity.password = password;
identity.fingerprint = smtp_fingerprint;
identity.use_ip = provider.useip;
identity.synchronize = true;
identity.primary = true;
identity.max_size = max_size;
identity.id = db.identity().insertIdentity(identity);
EntityLog.log(context, "Quick added identity=" + identity.name + " email=" + identity.email);
} else {
args.putLong("account", update.id);
EntityLog.log(context, "Quick setup update account=" + update.name);
db.account().setAccountSynchronize(update.id, true);
db.account().setAccountPassword(update.id, password, AUTH_TYPE_PASSWORD);
db.account().setAccountFingerprint(update.id, imap_fingerprint);
db.identity().setIdentityPassword(update.id, update.user, password, update.auth_type, AUTH_TYPE_PASSWORD);
db.identity().setIdentityFingerprint(update.id, smtp_fingerprint);
}
2021-08-16 16:45:47 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
2020-03-09 14:57:58 +00:00
}
2021-08-16 16:45:47 +00:00
2022-03-31 07:25:12 +00:00
if (update == null)
ServiceSynchronize.eval(context, "quick setup");
else {
args.putBoolean("updated", true);
ServiceSynchronize.reload(context, update.id, true, "quick setup");
}
2021-10-10 17:54:16 +00:00
return provider;
2021-08-16 16:45:47 +00:00
} catch (Throwable ex) {
Log.w(ex);
if (fail == null)
fail = ex;
2019-01-14 11:11:03 +00:00
}
2019-01-17 09:57:01 +00:00
2021-08-16 16:45:47 +00:00
if (fail != null)
throw fail;
2019-01-17 09:57:01 +00:00
return null;
2019-01-14 11:11:03 +00:00
}
2019-01-17 09:57:01 +00:00
@Override
2019-02-18 11:39:43 +00:00
protected void onExecuted(Bundle args, EmailProvider result) {
setManual(false);
2019-02-18 11:39:43 +00:00
boolean check = args.getBoolean("check");
if (check) {
2021-10-10 17:54:16 +00:00
bestProvider = result;
bestArgs = args;
showResult(bestProvider, bestArgs);
} else {
2022-03-31 07:25:12 +00:00
boolean updated = args.getBoolean("updated");
if (updated) {
finish();
ToastEx.makeText(getContext(), R.string.title_setup_oauth_updated, Toast.LENGTH_LONG).show();
} else {
FragmentDialogAccount fragment = new FragmentDialogAccount();
fragment.setArguments(args);
fragment.setTargetFragment(FragmentQuickSetup.this, ActivitySetup.REQUEST_DONE);
fragment.show(getParentFragmentManager(), "quick:review");
}
}
2019-01-17 09:57:01 +00:00
}
2019-01-14 11:11:03 +00:00
2019-01-17 09:57:01 +00:00
@Override
2019-07-06 14:24:03 +00:00
protected void onException(final Bundle args, Throwable ex) {
2019-09-21 19:53:54 +00:00
Log.e(ex);
setManual(true);
2021-06-25 18:49:55 +00:00
EmailProvider provider = args.getParcelable("provider");
2019-10-02 12:54:10 +00:00
2021-08-09 17:31:35 +00:00
etName.clearFocus();
etEmail.clearFocus();
Helper.hideKeyboard(view);
2020-02-02 18:05:34 +00:00
if (ex instanceof AuthenticationFailedException) {
String message = getString(R.string.title_setup_no_auth_hint);
2021-06-20 20:00:17 +00:00
if (provider != null && provider.appPassword)
message += "\n\n" + getString(R.string.title_setup_app_password_hint);
2020-02-02 18:05:34 +00:00
tvErrorHint.setText(message);
} else
2020-01-31 11:03:23 +00:00
tvErrorHint.setText(R.string.title_setup_no_settings_hint);
2019-10-02 12:54:10 +00:00
if (ex instanceof IllegalArgumentException || ex instanceof UnknownHostException) {
2019-09-13 16:52:09 +00:00
tvError.setText(ex.getMessage());
2019-11-15 08:33:43 +00:00
grpError.setVisibility(View.VISIBLE);
2019-10-02 12:54:10 +00:00
2020-08-23 15:34:14 +00:00
getMainHandler().post(new Runnable() {
2019-10-02 12:54:10 +00:00
@Override
public void run() {
2021-09-28 19:15:46 +00:00
if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
return;
scroll.smoothScrollTo(0, btnSupport.getBottom());
2019-10-02 12:54:10 +00:00
}
});
} else {
2019-12-06 07:50:46 +00:00
tvError.setText(Log.formatThrowable(ex, false));
2019-11-15 08:33:43 +00:00
grpError.setVisibility(View.VISIBLE);
2019-07-06 14:24:03 +00:00
2021-06-20 20:00:17 +00:00
if (provider != null && provider.link != null) {
Uri uri = Uri.parse(provider.link);
2019-10-02 12:54:10 +00:00
btnHelp.setTag(uri);
btnHelp.setVisibility(View.VISIBLE);
}
2019-09-09 14:44:06 +00:00
2021-06-20 20:00:17 +00:00
if (provider != null && provider.documentation != null) {
tvInstructions.setText(HtmlHelper.fromHtml(provider.documentation.toString(), getContext()));
2019-10-02 12:54:10 +00:00
tvInstructions.setVisibility(View.VISIBLE);
2019-07-06 14:24:03 +00:00
}
2021-06-20 20:00:17 +00:00
if (provider != null &&
provider.imap != null && provider.smtp != null) {
2021-10-10 17:54:16 +00:00
tvUser.setText("-");
2021-06-20 20:00:17 +00:00
tvImap.setText(provider.imap.toString());
tvSmtp.setText(provider.smtp.toString());
grpSetup.setVisibility(View.VISIBLE);
grpCertificate.setVisibility(View.GONE);
2021-06-20 20:00:17 +00:00
}
2020-08-23 15:34:14 +00:00
getMainHandler().post(new Runnable() {
2019-10-02 12:54:10 +00:00
@Override
public void run() {
2021-09-28 19:15:46 +00:00
if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
return;
2021-07-08 17:47:06 +00:00
scroll.smoothScrollTo(0, btnSupport.getBottom());
2019-10-02 12:54:10 +00:00
}
});
}
2019-01-17 09:57:01 +00:00
}
private void setManual(boolean manual) {
FragmentActivity activity = getActivity();
if (activity == null)
return;
Intent intent = activity.getIntent();
if (intent == null)
return;
intent.putExtra("manual", manual);
}
2019-09-11 12:03:59 +00:00
}.execute(this, args, "setup:quick");
2019-01-14 11:11:03 +00:00
}
2021-10-10 17:54:16 +00:00
private void showResult(EmailProvider provider, Bundle args) {
2021-10-12 06:52:44 +00:00
X509Certificate imap_certificate = (args == null ? null
: (X509Certificate) args.getSerializable("imap_certificate"));
X509Certificate smtp_certificate = (args == null ? null
: (X509Certificate) args.getSerializable("smtp_certificate"));
2021-10-10 17:54:16 +00:00
List<String> imapNames = new ArrayList<>();
if (imap_certificate != null)
try {
imapNames = EntityCertificate.getDnsNames(imap_certificate);
} catch (Throwable ignored) {
}
boolean imapMatches = (provider != null &&
EntityCertificate.matches(provider.imap.host, imapNames));
List<String> smtpNames = new ArrayList<>();
if (smtp_certificate != null)
try {
smtpNames = EntityCertificate.getDnsNames(smtp_certificate);
} catch (Throwable ignored) {
}
boolean smtpMatches = (provider != null &&
EntityCertificate.matches(provider.imap.host, smtpNames));
2021-10-12 06:52:44 +00:00
tvUser.setText((args == null ? null : args.getString("user")));
2021-10-10 17:54:16 +00:00
tvImap.setText(provider == null ? null : provider.imap.toString());
tvSmtp.setText(provider == null ? null : provider.smtp.toString());
grpSetup.setVisibility(provider == null ? View.GONE : View.VISIBLE);
tvImapFingerprint.setText(EntityCertificate.getKeyFingerprint(imap_certificate));
tvImapDnsNames.setText(TextUtils.join(", ", imapNames));
tvImapDnsNames.setTypeface(imapMatches ? Typeface.DEFAULT : Typeface.DEFAULT_BOLD);
tvSmtpFingerprint.setText(EntityCertificate.getKeyFingerprint(smtp_certificate));
tvSmtpDnsNames.setText(TextUtils.join(", ", smtpNames));
tvSmtpDnsNames.setTypeface(smtpMatches ? Typeface.DEFAULT : Typeface.DEFAULT_BOLD);
grpCertificate.setVisibility(
imap_certificate == null && smtp_certificate == null
? View.GONE : View.VISIBLE);
2022-03-31 07:25:12 +00:00
cbUpdate.setVisibility(provider == null ? View.GONE : View.VISIBLE);
2021-10-10 17:54:16 +00:00
btnSave.setVisibility(provider == null ? View.GONE : View.VISIBLE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
2019-07-15 12:11:55 +00:00
try {
switch (requestCode) {
2019-09-21 19:53:54 +00:00
case ActivitySetup.REQUEST_DONE:
2019-07-15 12:11:55 +00:00
finish();
break;
}
} catch (Throwable ex) {
Log.e(ex);
}
}
2019-01-14 11:11:03 +00:00
}