Files
FairEmail/app/src/main/java/eu/faircode/email/FragmentIdentity.java

935 lines
39 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
import android.content.Context;
2018-08-07 19:35:21 +00:00
import android.content.DialogInterface;
2018-11-10 16:47:11 +00:00
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
2018-09-22 07:55:32 +00:00
import android.os.Handler;
2018-12-26 10:54:48 +00:00
import android.text.Editable;
2019-01-05 17:03:19 +00:00
import android.text.Spanned;
2018-08-02 13:33:06 +00:00
import android.text.TextUtils;
2018-12-26 10:54:48 +00:00
import android.text.TextWatcher;
import android.util.Patterns;
2018-08-02 13:33:06 +00:00
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2018-08-02 13:33:06 +00:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
2018-11-10 16:47:11 +00:00
import android.widget.ImageView;
2018-09-22 07:55:32 +00:00
import android.widget.ScrollView;
2018-08-02 13:33:06 +00:00
import android.widget.Spinner;
2018-12-30 12:35:34 +00:00
import android.widget.TextView;
2018-08-02 13:33:06 +00:00
2018-11-10 16:47:11 +00:00
import com.android.colorpicker.ColorPickerDialog;
import com.android.colorpicker.ColorPickerSwatch;
2018-09-18 14:22:10 +00:00
import com.google.android.material.snackbar.Snackbar;
2018-08-08 06:55:47 +00:00
import com.google.android.material.textfield.TextInputLayout;
2018-12-30 09:55:14 +00:00
import java.net.UnknownHostException;
2018-08-13 18:31:42 +00:00
import java.util.ArrayList;
2018-08-02 13:33:06 +00:00
import java.util.List;
import java.util.Properties;
2018-09-21 13:09:22 +00:00
import javax.mail.AuthenticationFailedException;
2018-08-02 13:33:06 +00:00
import javax.mail.Session;
import javax.mail.Transport;
2018-08-08 06:55:47 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2018-08-24 16:41:22 +00:00
import androidx.constraintlayout.widget.Group;
2018-11-10 16:47:11 +00:00
import androidx.fragment.app.FragmentTransaction;
2018-08-08 06:55:47 +00:00
2019-01-15 17:41:55 +00:00
public class FragmentIdentity extends FragmentBase {
private ViewGroup view;
2018-12-26 10:54:48 +00:00
2018-08-02 13:33:06 +00:00
private EditText etName;
private EditText etEmail;
2018-12-26 10:54:48 +00:00
2018-09-05 20:04:23 +00:00
private Spinner spAccount;
2018-12-26 10:54:48 +00:00
private EditText etDisplay;
private Button btnColor;
private View vwColor;
private ImageView ibColorDefault;
private EditText etSignature;
2019-01-05 17:03:19 +00:00
private Button btnHtml;
2018-12-26 10:54:48 +00:00
2018-09-05 20:04:23 +00:00
private Button btnAdvanced;
2018-08-15 09:50:39 +00:00
private Spinner spProvider;
2018-09-18 14:22:10 +00:00
private EditText etDomain;
private Button btnAutoConfig;
2018-08-02 13:33:06 +00:00
private EditText etHost;
private CheckBox cbStartTls;
2018-10-23 07:52:20 +00:00
private CheckBox cbInsecure;
2018-08-02 13:33:06 +00:00
private EditText etPort;
private EditText etUser;
private TextInputLayout tilPassword;
2019-01-10 18:24:20 +00:00
private EditText etRealm;
2018-11-14 14:51:50 +01:00
2018-08-02 13:33:06 +00:00
private CheckBox cbSynchronize;
2018-08-06 10:02:47 +00:00
private CheckBox cbPrimary;
2018-12-26 10:54:48 +00:00
private EditText etReplyTo;
private EditText etBcc;
private CheckBox cbPlainOnly;
2018-12-26 10:54:48 +00:00
private CheckBox cbDeliveryReceipt;
private CheckBox cbReadReceipt;
2018-11-14 14:51:50 +01:00
2019-02-17 16:35:01 +00:00
private CheckBox cbStoreSent;
2018-08-06 10:27:21 +00:00
private Button btnSave;
2018-12-27 12:31:02 +00:00
private ContentLoadingProgressBar pbSave;
2018-12-30 12:35:34 +00:00
private TextView tvError;
2018-12-27 12:31:02 +00:00
private ContentLoadingProgressBar pbWait;
2018-12-26 10:54:48 +00:00
2018-12-27 07:48:59 +00:00
private Group grpAuthorize;
2018-09-05 20:04:23 +00:00
private Group grpAdvanced;
2018-08-02 13:33:06 +00:00
2018-08-27 14:31:45 +00:00
private long id = -1;
private boolean saving = false;
2019-01-26 11:18:59 +00:00
private int auth_type = Helper.AUTH_TYPE_PASSWORD;
2018-11-10 16:47:11 +00:00
private int color = Color.TRANSPARENT;
2018-08-27 14:31:45 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
Bundle args = getArguments();
2018-12-04 11:40:15 +01:00
id = args.getLong("id", -1);
2018-08-27 14:31:45 +00:00
}
2018-08-02 13:33:06 +00:00
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_edit_identity);
setHasOptionsMenu(true);
view = (ViewGroup) inflater.inflate(R.layout.fragment_identity, container, false);
2018-08-02 13:33:06 +00:00
// Get controls
etName = view.findViewById(R.id.etName);
etEmail = view.findViewById(R.id.etEmail);
2018-09-05 20:04:23 +00:00
spAccount = view.findViewById(R.id.spAccount);
etDisplay = view.findViewById(R.id.etDisplay);
2018-12-26 10:54:48 +00:00
btnColor = view.findViewById(R.id.btnColor);
vwColor = view.findViewById(R.id.vwColor);
ibColorDefault = view.findViewById(R.id.ibColorDefault);
etSignature = view.findViewById(R.id.etSignature);
2019-01-05 17:03:19 +00:00
btnHtml = view.findViewById(R.id.btnHtml);
2018-09-18 14:22:10 +00:00
2018-12-26 10:54:48 +00:00
btnAdvanced = view.findViewById(R.id.btnAdvanced);
2018-08-15 09:50:39 +00:00
spProvider = view.findViewById(R.id.spProvider);
2018-12-30 12:35:34 +00:00
2018-09-18 14:22:10 +00:00
etDomain = view.findViewById(R.id.etDomain);
btnAutoConfig = view.findViewById(R.id.btnAutoConfig);
2018-12-30 12:35:34 +00:00
2018-08-02 13:33:06 +00:00
etHost = view.findViewById(R.id.etHost);
cbStartTls = view.findViewById(R.id.cbStartTls);
2018-10-23 07:52:20 +00:00
cbInsecure = view.findViewById(R.id.cbInsecure);
2018-08-02 13:33:06 +00:00
etPort = view.findViewById(R.id.etPort);
etUser = view.findViewById(R.id.etUser);
tilPassword = view.findViewById(R.id.tilPassword);
2019-01-10 18:24:20 +00:00
etRealm = view.findViewById(R.id.etRealm);
2018-09-18 14:22:10 +00:00
2018-08-02 13:33:06 +00:00
cbSynchronize = view.findViewById(R.id.cbSynchronize);
2018-08-06 10:02:47 +00:00
cbPrimary = view.findViewById(R.id.cbPrimary);
2018-12-26 10:54:48 +00:00
etReplyTo = view.findViewById(R.id.etReplyTo);
etBcc = view.findViewById(R.id.etBcc);
cbPlainOnly = view.findViewById(R.id.cbPlainOnly);
2018-12-26 10:54:48 +00:00
cbDeliveryReceipt = view.findViewById(R.id.cbDeliveryReceipt);
cbReadReceipt = view.findViewById(R.id.cbReadReceipt);
2018-09-18 14:22:10 +00:00
2019-02-17 16:35:01 +00:00
cbStoreSent = view.findViewById(R.id.cbStoreSent);
2018-08-06 10:27:21 +00:00
btnSave = view.findViewById(R.id.btnSave);
pbSave = view.findViewById(R.id.pbSave);
2018-12-30 12:35:34 +00:00
tvError = view.findViewById(R.id.tvError);
pbWait = view.findViewById(R.id.pbWait);
2018-12-26 10:54:48 +00:00
2018-12-27 07:48:59 +00:00
grpAuthorize = view.findViewById(R.id.grpAuthorize);
2018-09-05 20:04:23 +00:00
grpAdvanced = view.findViewById(R.id.grpAdvanced);
2018-08-02 13:33:06 +00:00
// Wire controls
spAccount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
2018-12-27 07:48:59 +00:00
grpAuthorize.setVisibility(position > 0 ? View.VISIBLE : View.GONE);
2018-09-06 07:29:30 +00:00
if (position == 0)
grpAdvanced.setVisibility(View.GONE);
2018-09-05 20:04:23 +00:00
tilPassword.setPasswordVisibilityToggleEnabled(position == 0);
2018-08-15 09:50:39 +00:00
Integer tag = (Integer) adapterView.getTag();
if (tag != null && tag.equals(position))
return;
adapterView.setTag(position);
EntityAccount account = (EntityAccount) adapterView.getAdapter().getItem(position);
2019-01-26 11:18:59 +00:00
auth_type = account.auth_type;
2018-08-14 07:56:17 +00:00
// Select associated provider
2018-09-06 07:29:30 +00:00
if (position == 0)
spProvider.setSelection(0);
else {
boolean found = false;
for (int pos = 1; pos < spProvider.getAdapter().getCount(); pos++) {
2019-01-13 15:23:04 +00:00
EmailProvider provider = (EmailProvider) spProvider.getItemAtPosition(pos);
2018-09-06 07:29:30 +00:00
if (provider.imap_host.equals(account.host) &&
provider.imap_port == account.port) {
found = true;
spProvider.setSelection(pos);
// This is needed because the spinner might be invisible
etHost.setText(provider.smtp_host);
etPort.setText(Integer.toString(provider.smtp_port));
2018-11-09 14:48:21 +00:00
cbStartTls.setChecked(provider.smtp_starttls);
2018-09-06 07:29:30 +00:00
break;
}
2018-08-14 07:56:17 +00:00
}
2018-09-06 07:29:30 +00:00
if (!found)
grpAdvanced.setVisibility(View.VISIBLE);
2018-08-15 09:50:39 +00:00
}
2018-08-14 07:56:17 +00:00
2019-01-10 18:24:20 +00:00
// Copy account credentials
2018-09-05 20:04:23 +00:00
etEmail.setText(account.user);
2019-01-26 11:18:59 +00:00
etUser.setTag(auth_type == Helper.AUTH_TYPE_PASSWORD ? null : account.user);
etUser.setText(account.user);
tilPassword.getEditText().setText(account.password);
2019-01-10 18:24:20 +00:00
etRealm.setText(account.realm);
2019-01-26 11:18:59 +00:00
tilPassword.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
etRealm.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
2019-01-26 11:18:59 +00:00
etUser.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String user = etUser.getText().toString();
if (auth_type != Helper.AUTH_TYPE_PASSWORD && !user.equals(etUser.getTag())) {
auth_type = Helper.AUTH_TYPE_PASSWORD;
tilPassword.getEditText().setText(null);
tilPassword.setEnabled(true);
etRealm.setEnabled(true);
}
}
2019-01-26 11:18:59 +00:00
@Override
public void afterTextChanged(Editable s) {
}
});
2018-12-26 10:54:48 +00:00
vwColor.setBackgroundColor(color);
btnColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Helper.isPro(getContext())) {
int[] colors = getContext().getResources().getIntArray(R.array.colorPicker);
ColorPickerDialog colorPickerDialog = new ColorPickerDialog();
colorPickerDialog.initialize(R.string.title_account_color, colors, color, 4, colors.length);
colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() {
@Override
public void onColorSelected(int color) {
setColor(color);
}
});
colorPickerDialog.show(getFragmentManager(), "colorpicker");
} else {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
2019-02-14 19:42:17 +00:00
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
2018-12-26 10:54:48 +00:00
fragmentTransaction.commit();
}
}
});
ibColorDefault.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setColor(Color.TRANSPARENT);
}
});
2019-01-05 17:03:19 +00:00
btnHtml.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_html, null);
final EditText etHtml = dview.findViewById(R.id.etHtml);
2019-02-10 12:01:21 +00:00
etHtml.setText(HtmlHelper.toHtml(etSignature.getText()));
2019-01-05 17:03:19 +00:00
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
2019-02-10 12:01:21 +00:00
Spanned html = HtmlHelper.fromHtml(etHtml.getText().toString());
2019-01-05 17:03:19 +00:00
etSignature.setText(html);
}
})
.show();
}
});
2018-12-26 10:54:48 +00:00
btnAdvanced.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int visibility = (grpAdvanced.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
grpAdvanced.setVisibility(visibility);
if (visibility == View.VISIBLE)
new Handler().post(new Runnable() {
@Override
public void run() {
2018-12-27 17:04:10 +00:00
((ScrollView) view).smoothScrollTo(0, btnAdvanced.getTop());
2018-12-26 10:54:48 +00:00
}
});
}
});
2018-08-15 09:50:39 +00:00
spProvider.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2018-08-02 13:33:06 +00:00
@Override
2018-08-15 09:50:39 +00:00
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
Integer tag = (Integer) adapterView.getTag();
if (tag != null && tag.equals(position))
return;
adapterView.setTag(position);
2019-01-13 15:23:04 +00:00
EmailProvider provider = (EmailProvider) adapterView.getSelectedItem();
2018-08-24 16:41:22 +00:00
// Set associated host/port/starttls
etHost.setText(provider.smtp_host);
etPort.setText(position == 0 ? null : Integer.toString(provider.smtp_port));
2018-11-09 14:48:21 +00:00
cbStartTls.setChecked(provider.smtp_starttls);
2018-08-02 13:33:06 +00:00
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
2018-12-26 10:54:48 +00:00
etDomain.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence text, int start, int before, int count) {
btnAutoConfig.setEnabled(text.length() > 0);
}
@Override
public void afterTextChanged(Editable s) {
}
});
2018-09-18 14:22:10 +00:00
btnAutoConfig.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2019-01-17 09:57:01 +00:00
onAutoConfig();
2018-09-18 14:22:10 +00:00
}
});
2018-08-08 10:22:12 +00:00
cbStartTls.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
etPort.setHint(checked ? "587" : "465");
}
});
2018-08-02 13:33:06 +00:00
2018-08-06 10:02:47 +00:00
cbSynchronize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
cbPrimary.setEnabled(checked);
}
});
2018-08-06 10:27:21 +00:00
btnSave.setOnClickListener(new View.OnClickListener() {
2018-08-02 13:33:06 +00:00
@Override
public void onClick(View v) {
2019-01-17 09:57:01 +00:00
onSave();
2018-08-02 13:33:06 +00:00
}
});
// Initialize
Helper.setViewsEnabled(view, false);
2018-12-30 12:35:34 +00:00
btnAutoConfig.setEnabled(false);
2018-10-23 07:52:20 +00:00
cbInsecure.setVisibility(View.GONE);
tilPassword.setPasswordVisibilityToggleEnabled(id < 0);
2018-09-05 20:04:23 +00:00
btnSave.setVisibility(View.GONE);
btnAdvanced.setVisibility(View.GONE);
pbSave.setVisibility(View.GONE);
2018-12-30 12:35:34 +00:00
tvError.setVisibility(View.GONE);
2018-12-27 07:48:59 +00:00
grpAuthorize.setVisibility(View.GONE);
grpAdvanced.setVisibility(View.GONE);
return view;
}
2019-01-17 09:57:01 +00:00
private void onAutoConfig() {
etDomain.setEnabled(false);
btnAutoConfig.setEnabled(false);
Bundle args = new Bundle();
args.putString("domain", etDomain.getText().toString());
new SimpleTask<EmailProvider>() {
@Override
protected void onPreExecute(Bundle args) {
etDomain.setEnabled(false);
btnAutoConfig.setEnabled(false);
}
@Override
protected void onPostExecute(Bundle args) {
etDomain.setEnabled(true);
btnAutoConfig.setEnabled(true);
}
@Override
protected EmailProvider onExecute(Context context, Bundle args) throws Throwable {
String domain = args.getString("domain");
return EmailProvider.fromDomain(context, domain);
}
@Override
protected void onExecuted(Bundle args, EmailProvider provider) {
etHost.setText(provider.smtp_host);
etPort.setText(Integer.toString(provider.smtp_port));
cbStartTls.setChecked(provider.smtp_starttls);
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException || ex instanceof UnknownHostException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
}
}.execute(FragmentIdentity.this, args, "identity:config");
}
private void onSave() {
EntityAccount account = (EntityAccount) spAccount.getSelectedItem();
String name = etName.getText().toString();
if (TextUtils.isEmpty(name)) {
CharSequence hint = etName.getHint();
if (!TextUtils.isEmpty(hint))
name = hint.toString();
}
Bundle args = new Bundle();
args.putLong("id", id);
args.putString("name", name);
args.putString("email", etEmail.getText().toString().trim());
args.putString("display", etDisplay.getText().toString());
args.putString("replyto", etReplyTo.getText().toString().trim());
args.putString("bcc", etBcc.getText().toString().trim());
args.putBoolean("plain_only", cbPlainOnly.isChecked());
2019-01-17 09:57:01 +00:00
args.putBoolean("delivery_receipt", cbDeliveryReceipt.isChecked());
args.putBoolean("read_receipt", cbReadReceipt.isChecked());
2019-02-17 16:35:01 +00:00
args.putBoolean("store_sent", cbStoreSent.isChecked());
2019-01-17 09:57:01 +00:00
args.putLong("account", account == null ? -1 : account.id);
2019-01-26 11:18:59 +00:00
args.putInt("auth_type", auth_type);
2019-01-17 09:57:01 +00:00
args.putString("host", etHost.getText().toString());
args.putBoolean("starttls", cbStartTls.isChecked());
args.putBoolean("insecure", cbInsecure.isChecked());
args.putString("port", etPort.getText().toString());
args.putString("user", etUser.getText().toString());
args.putString("password", tilPassword.getEditText().getText().toString());
args.putString("realm", etRealm.getText().toString());
args.putInt("color", color);
2019-02-10 12:01:21 +00:00
args.putString("signature", HtmlHelper.toHtml(etSignature.getText()));
2019-01-17 09:57:01 +00:00
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("primary", cbPrimary.isChecked());
new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
saving = true;
getActivity().invalidateOptionsMenu();
2019-01-17 09:57:01 +00:00
Helper.setViewsEnabled(view, false);
pbSave.setVisibility(View.VISIBLE);
tvError.setVisibility(View.GONE);
}
@Override
protected void onPostExecute(Bundle args) {
saving = false;
getActivity().invalidateOptionsMenu();
2019-01-17 09:57:01 +00:00
Helper.setViewsEnabled(view, true);
pbSave.setVisibility(View.GONE);
}
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
String name = args.getString("name");
String email = args.getString("email");
long account = args.getLong("account");
String display = args.getString("display");
Integer color = args.getInt("color");
String signature = args.getString("signature");
int auth_type = args.getInt("auth_type");
String host = args.getString("host");
boolean starttls = args.getBoolean("starttls");
boolean insecure = args.getBoolean("insecure");
String port = args.getString("port");
String user = args.getString("user");
String password = args.getString("password");
String realm = args.getString("realm");
boolean synchronize = args.getBoolean("synchronize");
boolean primary = args.getBoolean("primary");
String replyto = args.getString("replyto");
String bcc = args.getString("bcc");
boolean plain_only = args.getBoolean("plain_only");
2019-01-17 09:57:01 +00:00
boolean delivery_receipt = args.getBoolean("delivery_receipt");
boolean read_receipt = args.getBoolean("read_receipt");
2019-02-17 16:35:01 +00:00
boolean store_sent = args.getBoolean("store_sent");
2019-01-19 18:13:48 +00:00
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));
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches())
throw new IllegalArgumentException(context.getString(R.string.title_email_invalid));
if (TextUtils.isEmpty(host))
throw new IllegalArgumentException(context.getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
port = (starttls ? "587" : "465");
if (TextUtils.isEmpty(user))
throw new IllegalArgumentException(context.getString(R.string.title_no_user));
if (synchronize && TextUtils.isEmpty(password) && !insecure)
throw new IllegalArgumentException(context.getString(R.string.title_no_password));
email = email.toLowerCase();
if (TextUtils.isEmpty(display))
display = null;
if (TextUtils.isEmpty(realm))
realm = null;
if (TextUtils.isEmpty(replyto))
replyto = null;
else
replyto = replyto.toLowerCase();
if (TextUtils.isEmpty(bcc))
bcc = null;
else
bcc = bcc.toLowerCase();
if (Color.TRANSPARENT == color)
color = null;
DB db = DB.getInstance(context);
EntityIdentity identity = db.identity().getIdentity(id);
String identityRealm = (identity == null ? null : identity.realm);
boolean check = (synchronize && (identity == null ||
2019-02-02 17:59:40 +00:00
auth_type != identity.auth_type ||
2019-01-17 09:57:01 +00:00
!host.equals(identity.host) || Integer.parseInt(port) != identity.port ||
!user.equals(identity.user) || !password.equals(identity.password) ||
2019-01-26 11:18:59 +00:00
(realm == null ? identityRealm != null : !realm.equals(identityRealm))));
2019-01-17 09:57:01 +00:00
boolean reload = (identity == null || identity.synchronize != synchronize || check);
Long last_connected = null;
if (identity != null && synchronize == identity.synchronize)
last_connected = identity.last_connected;
2019-01-17 09:57:01 +00:00
// Check SMTP server
if (check) {
2019-02-09 21:03:53 +00:00
String protocol = (starttls ? "smtp" : "smtps");
2019-01-17 09:57:01 +00:00
Properties props = MessageHelper.getSessionProperties(auth_type, realm, insecure);
Session isession = Session.getInstance(props, null);
isession.setDebug(true);
2019-02-09 21:03:53 +00:00
Transport itransport = isession.getTransport(protocol);
2019-01-17 09:57:01 +00:00
try {
try {
itransport.connect(host, Integer.parseInt(port), user, password);
} catch (AuthenticationFailedException ex) {
if (auth_type == Helper.AUTH_TYPE_GMAIL) {
password = Helper.refreshToken(context, "com.google", user, password);
itransport.connect(host, Integer.parseInt(port), user, password);
} else
throw ex;
}
} finally {
itransport.close();
}
}
try {
db.beginTransaction();
boolean update = (identity != null);
if (identity == null)
identity = new EntityIdentity();
identity.name = name;
identity.email = email;
identity.account = account;
identity.display = display;
identity.color = color;
identity.signature = signature;
identity.auth_type = auth_type;
identity.host = host;
identity.starttls = starttls;
identity.insecure = insecure;
identity.port = Integer.parseInt(port);
identity.user = user;
identity.password = password;
identity.realm = realm;
identity.synchronize = synchronize;
identity.primary = (identity.synchronize && primary);
identity.replyto = replyto;
identity.bcc = bcc;
identity.plain_only = plain_only;
2019-01-17 09:57:01 +00:00
identity.delivery_receipt = delivery_receipt;
identity.read_receipt = read_receipt;
2019-02-17 16:35:01 +00:00
identity.store_sent = store_sent;
identity.sent_folder = null;
2019-01-17 09:57:01 +00:00
identity.error = null;
identity.last_connected = last_connected;
2019-01-17 09:57:01 +00:00
if (identity.primary)
db.identity().resetPrimary(account);
if (update)
db.identity().updateIdentity(identity);
else
identity.id = db.identity().insertIdentity(identity);
2019-02-02 07:28:14 +00:00
EntityLog.log(context, (update ? "Updated" : "Added") +
" identity=" + identity.name + " email=" + identity.email);
2019-01-17 09:57:01 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (reload)
ServiceSynchronize.reload(context, "save identity");
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else {
tvError.setText(Helper.formatThrowable(ex));
tvError.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() {
@Override
public void run() {
((ScrollView) view).smoothScrollTo(0, tvError.getBottom());
}
});
}
}
}.execute(FragmentIdentity.this, args, "identity:save");
}
@Override
2018-08-15 09:50:39 +00:00
public void onSaveInstanceState(Bundle outState) {
2018-08-21 18:00:06 +00:00
super.onSaveInstanceState(outState);
2018-08-15 09:50:39 +00:00
outState.putInt("account", spAccount.getSelectedItemPosition());
outState.putInt("provider", spProvider.getSelectedItemPosition());
2019-01-26 11:18:59 +00:00
outState.putInt("auth_type", auth_type);
2018-08-15 09:50:39 +00:00
outState.putString("password", tilPassword.getEditText().getText().toString());
2018-09-05 20:04:23 +00:00
outState.putInt("advanced", grpAdvanced.getVisibility());
2018-11-10 16:47:11 +00:00
outState.putInt("color", color);
2018-08-15 09:50:39 +00:00
}
@Override
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
2018-11-14 17:21:53 +01:00
Bundle args = new Bundle();
args.putLong("id", id);
2018-11-14 17:21:53 +01:00
new SimpleTask<EntityIdentity>() {
2018-08-02 13:33:06 +00:00
@Override
2018-12-31 07:03:48 +00:00
protected EntityIdentity onExecute(Context context, Bundle args) {
2018-11-14 17:21:53 +01:00
long id = args.getLong("id");
return DB.getInstance(context).identity().getIdentity(id);
}
2018-08-16 11:26:53 +00:00
2018-11-14 17:21:53 +01:00
@Override
2018-12-31 07:03:48 +00:00
protected void onExecuted(Bundle args, final EntityIdentity identity) {
2018-09-05 20:39:47 +00:00
if (savedInstanceState == null) {
2019-01-26 11:18:59 +00:00
auth_type = (identity == null ? Helper.AUTH_TYPE_PASSWORD : identity.auth_type);
2018-08-16 11:26:53 +00:00
etName.setText(identity == null ? null : identity.name);
etEmail.setText(identity == null ? null : identity.email);
2018-12-26 10:54:48 +00:00
etDisplay.setText(identity == null ? null : identity.display);
2019-01-09 07:30:46 +00:00
etSignature.setText(identity == null ||
2019-02-10 12:01:21 +00:00
TextUtils.isEmpty(identity.signature) ? null : HtmlHelper.fromHtml(identity.signature));
2018-12-26 10:54:48 +00:00
2018-08-16 11:26:53 +00:00
etHost.setText(identity == null ? null : identity.host);
cbStartTls.setChecked(identity == null ? false : identity.starttls);
2018-11-29 18:08:57 +01:00
cbInsecure.setChecked(identity == null ? false : identity.insecure);
2018-08-16 11:26:53 +00:00
etPort.setText(identity == null ? null : Long.toString(identity.port));
2019-01-26 11:18:59 +00:00
etUser.setTag(identity == null || auth_type == Helper.AUTH_TYPE_PASSWORD ? null : identity.user);
2018-08-16 11:26:53 +00:00
etUser.setText(identity == null ? null : identity.user);
tilPassword.getEditText().setText(identity == null ? null : identity.password);
2019-01-10 18:24:20 +00:00
etRealm.setText(identity == null ? null : identity.realm);
2018-08-16 11:26:53 +00:00
cbSynchronize.setChecked(identity == null ? true : identity.synchronize);
cbPrimary.setChecked(identity == null ? true : identity.primary);
2018-12-26 10:54:48 +00:00
etReplyTo.setText(identity == null ? null : identity.replyto);
etBcc.setText(identity == null ? null : identity.bcc);
cbPlainOnly.setChecked(identity == null ? false : identity.plain_only);
2018-12-26 10:54:48 +00:00
cbDeliveryReceipt.setChecked(identity == null ? false : identity.delivery_receipt);
cbReadReceipt.setChecked(identity == null ? false : identity.read_receipt);
2019-02-17 16:35:01 +00:00
cbStoreSent.setChecked(identity == null ? false : identity.store_sent);
2018-12-26 10:54:48 +00:00
2018-11-10 16:47:11 +00:00
color = (identity == null || identity.color == null ? Color.TRANSPARENT : identity.color);
2018-08-16 11:26:53 +00:00
etName.requestFocus();
if (identity == null)
new SimpleTask<Integer>() {
@Override
2018-12-31 07:03:48 +00:00
protected Integer onExecute(Context context, Bundle args) {
return DB.getInstance(context).identity().getSynchronizingIdentityCount();
}
@Override
2018-12-31 07:03:48 +00:00
protected void onExecuted(Bundle args, Integer count) {
cbPrimary.setChecked(count == 0);
}
2018-12-01 10:47:08 +01:00
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
2018-12-01 10:47:08 +01:00
}
2019-01-12 13:15:15 +00:00
}.execute(FragmentIdentity.this, new Bundle(), "identity:count");
2018-09-05 20:04:23 +00:00
} else {
2019-01-26 11:18:59 +00:00
auth_type = savedInstanceState.getInt("auth_type");
2018-08-15 09:50:39 +00:00
tilPassword.getEditText().setText(savedInstanceState.getString("password"));
2018-09-05 20:04:23 +00:00
grpAdvanced.setVisibility(savedInstanceState.getInt("advanced"));
2018-11-10 16:47:11 +00:00
color = savedInstanceState.getInt("color");
2018-09-05 20:04:23 +00:00
}
2018-08-15 09:50:39 +00:00
Helper.setViewsEnabled(view, true);
2019-01-26 11:18:59 +00:00
tilPassword.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
etRealm.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
2018-11-10 16:47:11 +00:00
setColor(color);
2018-11-14 14:51:50 +01:00
2018-08-15 09:50:39 +00:00
cbPrimary.setEnabled(cbSynchronize.isChecked());
pbWait.setVisibility(View.GONE);
2018-08-08 10:22:12 +00:00
2018-11-14 17:21:53 +01:00
new SimpleTask<List<EntityAccount>>() {
2018-08-08 10:22:12 +00:00
@Override
2018-12-31 07:03:48 +00:00
protected List<EntityAccount> onExecute(Context context, Bundle args) {
2018-11-14 17:21:53 +01:00
return DB.getInstance(context).account().getAccounts();
}
2018-09-05 20:39:47 +00:00
2018-11-14 17:21:53 +01:00
@Override
2018-12-31 07:03:48 +00:00
protected void onExecuted(Bundle args, List<EntityAccount> accounts) {
2018-08-12 15:31:43 +00:00
if (accounts == null)
2018-08-13 18:31:42 +00:00
accounts = new ArrayList<>();
2018-08-12 15:31:43 +00:00
2018-08-08 10:22:12 +00:00
EntityAccount unselected = new EntityAccount();
unselected.id = -1L;
2019-01-26 11:18:59 +00:00
unselected.auth_type = Helper.AUTH_TYPE_PASSWORD;
2018-08-27 14:31:45 +00:00
unselected.name = getString(R.string.title_select);
2018-08-08 10:22:12 +00:00
unselected.primary = false;
accounts.add(0, unselected);
2018-11-11 14:09:46 +00:00
ArrayAdapter<EntityAccount> aaAccount =
new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, accounts);
aaAccount.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spAccount.setAdapter(aaAccount);
2018-08-15 09:50:39 +00:00
// Get providers
2019-01-13 15:23:04 +00:00
List<EmailProvider> providers = EmailProvider.loadProfiles(getContext());
providers.add(0, new EmailProvider(getString(R.string.title_custom)));
2018-08-15 09:50:39 +00:00
2019-01-13 15:23:04 +00:00
ArrayAdapter<EmailProvider> aaProfile =
2018-11-11 14:09:46 +00:00
new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, providers);
aaProfile.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spProvider.setAdapter(aaProfile);
2018-08-15 09:50:39 +00:00
if (savedInstanceState == null) {
2018-08-27 15:08:23 +00:00
spProvider.setTag(0);
spProvider.setSelection(0);
if (identity != null)
for (int pos = 1; pos < providers.size(); pos++)
if (providers.get(pos).smtp_host.equals(identity.host)) {
spProvider.setTag(pos);
spProvider.setSelection(pos);
break;
}
spAccount.setTag(0);
spAccount.setSelection(0);
2018-11-13 19:32:50 +01:00
for (int pos = 0; pos < accounts.size(); pos++) {
EntityAccount account = accounts.get(pos);
if (account.id.equals((identity == null ? -1 : identity.account))) {
2018-08-27 15:08:23 +00:00
spAccount.setTag(pos);
2018-08-15 09:50:39 +00:00
spAccount.setSelection(pos);
2018-08-27 15:08:23 +00:00
// OAuth token could be updated
if (pos > 0 && accounts.get(pos).auth_type != Helper.AUTH_TYPE_PASSWORD)
2018-08-27 15:08:23 +00:00
tilPassword.getEditText().setText(accounts.get(pos).password);
2018-08-15 09:50:39 +00:00
break;
}
2018-11-13 19:32:50 +01:00
}
2018-08-15 09:50:39 +00:00
} else {
int provider = savedInstanceState.getInt("provider");
spProvider.setTag(provider);
spProvider.setSelection(provider);
int account = savedInstanceState.getInt("account");
spAccount.setTag(account);
spAccount.setSelection(account);
}
2018-08-08 10:22:12 +00:00
}
2018-12-01 10:47:08 +01:00
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
2018-12-01 10:47:08 +01:00
}
2019-01-12 13:15:15 +00:00
}.execute(FragmentIdentity.this, args, "identity:accounts:get");
2018-08-02 13:33:06 +00:00
}
2018-12-01 10:47:08 +01:00
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
2018-12-01 10:47:08 +01:00
}
2019-01-12 13:15:15 +00:00
}.execute(this, args, "identity:get");
2018-08-02 13:33:06 +00:00
}
2018-11-10 16:47:11 +00:00
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_identity, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.menu_delete).setVisible(id > 0 && !saving);
super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete:
onMenuDelete();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void onMenuDelete() {
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_identity_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override
protected void onPostExecute(Bundle args) {
Helper.setViewsEnabled(view, false);
pbWait.setVisibility(View.VISIBLE);
}
@Override
2018-12-31 07:03:48 +00:00
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.identity().setIdentityTbd(id);
ServiceSynchronize.reload(context, "delete identity");
return null;
}
@Override
2018-12-31 07:03:48 +00:00
protected void onExecuted(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
}
2019-01-12 13:15:15 +00:00
}.execute(FragmentIdentity.this, args, "identity:delete");
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
}
2018-11-10 16:47:11 +00:00
private void setColor(int color) {
FragmentIdentity.this.color = color;
GradientDrawable border = new GradientDrawable();
border.setColor(color);
border.setStroke(1, Helper.resolveColor(getContext(), R.attr.colorSeparator));
vwColor.setBackground(border);
}
2018-08-02 13:33:06 +00:00
}