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

1815 lines
75 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
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2019-12-25 09:24:31 +00:00
import android.accounts.Account;
import android.accounts.AccountManager;
2018-11-19 17:10:55 +00:00
import android.app.NotificationManager;
2018-08-02 13:33:06 +00:00
import android.content.Context;
2018-08-27 14:31:45 +00:00
import android.content.Intent;
2018-09-12 16:54:48 +00:00
import android.graphics.Color;
2019-07-06 14:24:03 +00:00
import android.net.Uri;
2018-08-27 14:31:45 +00:00
import android.os.Build;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
2018-08-02 13:33:06 +00:00
import android.text.TextUtils;
import android.text.TextWatcher;
2019-06-19 12:51:19 +00:00
import android.text.method.LinkMovementMethod;
2020-02-20 15:46:51 +00:00
import android.view.KeyEvent;
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;
2018-08-06 10:02:47 +00:00
import android.widget.CompoundButton;
2018-08-02 13:33:06 +00:00
import android.widget.EditText;
2019-03-13 07:17:24 +00:00
import android.widget.RadioGroup;
import android.widget.ScrollView;
2018-08-02 13:33:06 +00:00
import android.widget.Spinner;
2018-09-22 07:51:54 +00:00
import android.widget.TextView;
2019-12-25 09:24:31 +00:00
import android.widget.Toast;
2018-08-02 13:33:06 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group;
2019-10-19 18:56:19 +00:00
import androidx.fragment.app.FragmentTransaction;
2019-07-12 12:10:31 +00:00
import androidx.lifecycle.Lifecycle;
2018-09-05 12:37:27 +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-08-02 13:33:06 +00:00
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.protocol.IMAPProtocol;
2018-08-02 13:33:06 +00:00
2018-12-30 09:55:14 +00:00
import java.net.UnknownHostException;
2018-08-02 13:33:06 +00:00
import java.util.ArrayList;
2019-05-06 20:34:18 +00:00
import java.util.Collections;
import java.util.Date;
2018-08-02 13:33:06 +00:00
import java.util.List;
2019-02-26 10:05:21 +00:00
import java.util.Objects;
2018-08-02 13:33:06 +00:00
import javax.mail.Folder;
2019-06-30 14:55:15 +00:00
import static android.app.Activity.RESULT_OK;
2020-02-28 16:18:23 +00:00
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static com.google.android.material.textfield.TextInputLayout.END_ICON_NONE;
import static com.google.android.material.textfield.TextInputLayout.END_ICON_PASSWORD_TOGGLE;
2018-08-27 14:31:45 +00:00
2019-01-15 17:41:55 +00:00
public class FragmentAccount extends FragmentBase {
private ViewGroup view;
2019-04-22 18:43:04 +00:00
private ScrollView scroll;
2018-08-15 09:50:39 +00:00
private Spinner spProvider;
2020-02-28 16:18:23 +00:00
private TextView tvGmailHint;
2018-10-23 07:52:20 +00:00
2018-09-18 14:22:10 +00:00
private EditText etDomain;
private Button btnAutoConfig;
2019-08-17 10:01:42 +00:00
private ContentLoadingProgressBar pbAutoConfig;
2018-10-23 07:52:20 +00:00
private EditText etHost;
2019-03-13 07:17:24 +00:00
private RadioGroup rgEncryption;
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;
private TextView tvCharacters;
2020-02-10 15:35:14 +00:00
private Button btnCertificate;
private TextView tvCertificate;
2019-12-25 09:24:31 +00:00
private Button btnOAuth;
2019-01-10 18:24:20 +00:00
private EditText etRealm;
2018-09-05 18:37:20 +00:00
private EditText etName;
2019-10-01 08:13:22 +00:00
private ViewButtonColor btnColor;
2019-06-28 19:22:20 +00:00
private TextView tvColorPro;
2018-10-07 06:53:09 +00:00
2018-12-26 11:34:05 +00:00
private Button btnAdvanced;
2018-08-02 13:33:06 +00:00
private CheckBox cbSynchronize;
2019-12-08 14:00:15 +00:00
private CheckBox cbOnDemand;
2018-08-06 10:02:47 +00:00
private CheckBox cbPrimary;
2018-12-26 11:34:05 +00:00
private CheckBox cbNotify;
2019-06-28 19:22:20 +00:00
private TextView tvNotifyPro;
private CheckBox cbBrowse;
private CheckBox cbAutoSeen;
2018-09-21 17:19:02 +00:00
private EditText etInterval;
private CheckBox cbPartialFetch;
private CheckBox cbIgnoreSize;
private CheckBox cbUseDate;
2018-10-07 06:53:09 +00:00
private Button btnCheck;
2018-12-27 12:31:02 +00:00
private ContentLoadingProgressBar pbCheck;
2018-09-22 10:29:35 +00:00
private TextView tvIdle;
private TextView tvUtf8;
2018-09-22 10:29:35 +00:00
2018-09-21 17:36:19 +00:00
private ArrayAdapter<EntityFolder> adapter;
2018-08-10 09:45:36 +00:00
private Spinner spDrafts;
private Spinner spSent;
2019-06-16 06:51:27 +00:00
private Spinner spArchive;
2018-08-10 09:45:36 +00:00
private Spinner spTrash;
private Spinner spJunk;
private ArrayAdapter<EntityFolder> adapterSwipe;
2019-01-20 15:22:21 +00:00
private Spinner spLeft;
private Spinner spRight;
2019-10-23 10:51:20 +00:00
private Spinner spMove;
2018-08-10 09:45:36 +00:00
private Button btnSave;
2018-12-27 12:31:02 +00:00
private ContentLoadingProgressBar pbSave;
2019-10-19 18:56:19 +00:00
private CheckBox cbIdentity;
2018-12-30 12:35:34 +00:00
private TextView tvError;
2019-12-16 18:09:49 +00:00
private CheckBox cbTrust;
2019-07-06 14:24:03 +00:00
private Button btnHelp;
2019-09-09 14:44:06 +00:00
private Button btnSupport;
2019-06-19 12:51:19 +00:00
private TextView tvInstructions;
2018-12-30 12:35:34 +00:00
2018-12-27 12:31:02 +00:00
private ContentLoadingProgressBar pbWait;
2018-09-05 18:37:20 +00:00
private Group grpServer;
private Group grpAuthorize;
private Group grpAdvanced;
2018-08-10 09:45:36 +00:00
private Group grpFolders;
2019-11-15 08:33:43 +00:00
private Group grpError;
2018-08-02 13:33:06 +00:00
2018-08-27 14:31:45 +00:00
private long id = -1;
2019-08-13 10:07:30 +00:00
private long copy = -1;
2020-01-29 20:06:45 +00:00
private int auth = EmailService.AUTH_TYPE_PASSWORD;
2019-12-21 15:03:57 +00:00
private String provider = null;
2020-02-10 15:35:14 +00:00
private String certificate = null;
private boolean saving = false;
2018-08-27 14:31:45 +00:00
private static final int REQUEST_COLOR = 1;
private static final int REQUEST_SAVE = 2;
private static final int REQUEST_DELETE = 3;
2019-07-17 12:23:23 +00:00
static final Long SWIPE_ACTION_ASK = -1L;
static final Long SWIPE_ACTION_SEEN = -2L;
2019-10-06 19:01:01 +00:00
static final Long SWIPE_ACTION_SNOOZE = -3L;
2019-10-12 09:09:54 +00:00
static final Long SWIPE_ACTION_HIDE = -4L;
2019-10-23 13:53:25 +00:00
static final Long SWIPE_ACTION_MOVE = -5L;
2019-11-11 09:05:58 +00:00
static final Long SWIPE_ACTION_FLAG = -6L;
2020-03-16 19:58:25 +00:00
static final Long SWIPE_ACTION_DELETE = -7L;
2020-03-17 07:40:51 +00:00
static final Long SWIPE_ACTION_JUNK = -8L;
2019-07-17 12:23:23 +00:00
2018-08-27 14:31:45 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
Bundle args = getArguments();
2019-08-13 10:07:30 +00:00
if (args.getBoolean("copy"))
copy = args.getLong("id", -1);
else
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) {
2019-02-21 20:17:09 +00:00
setSubtitle(R.string.title_edit_account);
2019-03-20 09:47:03 +00:00
setHasOptionsMenu(true);
2019-02-21 20:17:09 +00:00
view = (ViewGroup) inflater.inflate(R.layout.fragment_account, container, false);
2019-04-22 18:43:04 +00:00
scroll = view.findViewById(R.id.scroll);
2018-08-02 13:33:06 +00:00
// Get controls
2018-08-15 09:50:39 +00:00
spProvider = view.findViewById(R.id.spProvider);
2020-02-28 16:18:23 +00:00
tvGmailHint = view.findViewById(R.id.tvGmailHint);
2018-09-18 14:22:10 +00:00
etDomain = view.findViewById(R.id.etDomain);
btnAutoConfig = view.findViewById(R.id.btnAutoConfig);
2019-08-17 10:01:42 +00:00
pbAutoConfig = view.findViewById(R.id.pbAutoConfig);
2018-09-18 14:22:10 +00:00
2018-08-02 13:33:06 +00:00
etHost = view.findViewById(R.id.etHost);
etPort = view.findViewById(R.id.etPort);
2019-03-13 07:17:24 +00:00
rgEncryption = view.findViewById(R.id.rgEncryption);
2018-10-23 07:52:20 +00:00
cbInsecure = view.findViewById(R.id.cbInsecure);
2018-08-02 13:33:06 +00:00
etUser = view.findViewById(R.id.etUser);
tilPassword = view.findViewById(R.id.tilPassword);
tvCharacters = view.findViewById(R.id.tvCharacters);
2020-02-10 15:35:14 +00:00
btnCertificate = view.findViewById(R.id.btnCertificate);
tvCertificate = view.findViewById(R.id.tvCertificate);
2019-12-25 09:24:31 +00:00
btnOAuth = view.findViewById(R.id.btnOAuth);
2019-01-10 18:24:20 +00:00
etRealm = view.findViewById(R.id.etRealm);
2018-09-05 18:37:20 +00:00
etName = view.findViewById(R.id.etName);
btnColor = view.findViewById(R.id.btnColor);
2019-06-28 19:22:20 +00:00
tvColorPro = view.findViewById(R.id.tvColorPro);
2018-09-07 12:34:54 +00:00
2018-12-26 11:34:05 +00:00
btnAdvanced = view.findViewById(R.id.btnAdvanced);
2018-08-02 13:33:06 +00:00
cbSynchronize = view.findViewById(R.id.cbSynchronize);
2019-12-08 14:00:15 +00:00
cbOnDemand = view.findViewById(R.id.cbOnDemand);
2018-08-06 10:02:47 +00:00
cbPrimary = view.findViewById(R.id.cbPrimary);
2018-12-26 11:34:05 +00:00
cbNotify = view.findViewById(R.id.cbNotify);
2019-06-28 19:22:20 +00:00
tvNotifyPro = view.findViewById(R.id.tvNotifyPro);
cbBrowse = view.findViewById(R.id.cbBrowse);
cbAutoSeen = view.findViewById(R.id.cbAutoSeen);
2018-09-21 17:19:02 +00:00
etInterval = view.findViewById(R.id.etInterval);
cbPartialFetch = view.findViewById(R.id.cbPartialFetch);
cbIgnoreSize = view.findViewById(R.id.cbIgnoreSize);
cbUseDate = view.findViewById(R.id.cbUseDate);
2018-09-05 18:37:20 +00:00
2018-08-10 09:45:36 +00:00
btnCheck = view.findViewById(R.id.btnCheck);
2018-08-02 13:33:06 +00:00
pbCheck = view.findViewById(R.id.pbCheck);
2018-09-05 18:37:20 +00:00
2018-09-22 10:29:35 +00:00
tvIdle = view.findViewById(R.id.tvIdle);
tvUtf8 = view.findViewById(R.id.tvUtf8);
2018-09-22 10:29:35 +00:00
2018-08-10 09:45:36 +00:00
spDrafts = view.findViewById(R.id.spDrafts);
spSent = view.findViewById(R.id.spSent);
2019-06-16 06:51:27 +00:00
spArchive = view.findViewById(R.id.spArchive);
2018-08-10 09:45:36 +00:00
spTrash = view.findViewById(R.id.spTrash);
spJunk = view.findViewById(R.id.spJunk);
2019-01-20 15:22:21 +00:00
spLeft = view.findViewById(R.id.spLeft);
spRight = view.findViewById(R.id.spRight);
2019-10-23 10:51:20 +00:00
spMove = view.findViewById(R.id.spMove);
2018-09-05 18:37:20 +00:00
2018-08-10 09:45:36 +00:00
btnSave = view.findViewById(R.id.btnSave);
pbSave = view.findViewById(R.id.pbSave);
2019-10-19 18:56:19 +00:00
cbIdentity = view.findViewById(R.id.cbIdentity);
2019-07-06 14:24:03 +00:00
2018-12-30 12:35:34 +00:00
tvError = view.findViewById(R.id.tvError);
2019-12-16 18:09:49 +00:00
cbTrust = view.findViewById(R.id.cbTrust);
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-06-19 12:51:19 +00:00
tvInstructions = view.findViewById(R.id.tvInstructions);
2018-09-05 18:37:20 +00:00
pbWait = view.findViewById(R.id.pbWait);
2018-09-05 18:37:20 +00:00
grpServer = view.findViewById(R.id.grpServer);
grpAuthorize = view.findViewById(R.id.grpAuthorize);
grpAdvanced = view.findViewById(R.id.grpAdvanced);
2018-08-10 09:45:36 +00:00
grpFolders = view.findViewById(R.id.grpFolders);
2019-11-15 08:33:43 +00:00
grpError = view.findViewById(R.id.grpError);
2018-08-02 13:33:06 +00:00
// Wire controls
2018-08-15 09:50:39 +00:00
spProvider.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2018-08-02 13:33:06 +00:00
@Override
2018-12-04 10:40:15 +00:00
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long itemid) {
2019-01-13 15:23:04 +00:00
EmailProvider provider = (EmailProvider) adapterView.getSelectedItem();
2020-02-28 16:18:23 +00:00
tvGmailHint.setVisibility(
auth == EmailService.AUTH_TYPE_PASSWORD && "gmail".equals(provider.id)
? VISIBLE : GONE);
2019-07-29 16:15:21 +00:00
grpServer.setVisibility(position > 0 ? View.VISIBLE : View.GONE);
2018-09-05 18:37:20 +00:00
grpAuthorize.setVisibility(position > 0 ? View.VISIBLE : View.GONE);
2018-09-05 18:37:20 +00:00
btnAdvanced.setVisibility(position > 0 ? View.VISIBLE : View.GONE);
if (position == 0)
grpAdvanced.setVisibility(View.GONE);
btnCheck.setVisibility(position > 0 ? View.VISIBLE : View.GONE);
2018-09-22 10:29:35 +00:00
tvIdle.setVisibility(View.GONE);
2019-02-21 19:31:28 +00:00
tvUtf8.setVisibility(View.GONE);
2018-09-05 18:37:20 +00:00
Object tag = adapterView.getTag();
if (tag != null && (Integer) tag == position)
return;
adapterView.setTag(position);
2018-08-27 14:31:45 +00:00
2019-08-17 09:50:33 +00:00
etHost.setText(provider.imap.host);
etPort.setText(provider.imap.host == null ? null : Integer.toString(provider.imap.port));
rgEncryption.check(provider.imap.starttls ? R.id.radio_starttls : R.id.radio_ssl);
2018-09-05 18:37:20 +00:00
2019-01-26 11:18:59 +00:00
etUser.setTag(null);
2018-09-05 18:37:20 +00:00
etUser.setText(null);
tilPassword.getEditText().setText(null);
2020-02-10 15:35:14 +00:00
certificate = null;
tvCertificate.setText(R.string.title_optional);
2019-12-25 09:24:31 +00:00
btnOAuth.setEnabled(false);
2019-01-10 18:24:20 +00:00
etRealm.setText(null);
2019-12-16 18:09:49 +00:00
cbTrust.setChecked(false);
2018-08-27 15:08:23 +00:00
2018-09-05 18:37:20 +00:00
etName.setText(position > 1 ? provider.name : null);
etInterval.setText(provider.keepalive > 0 ? Integer.toString(provider.keepalive) : null);
2019-12-05 16:48:56 +00:00
cbPartialFetch.setChecked(provider.partial);
2018-11-16 08:06:10 +00:00
grpFolders.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
2019-10-19 18:56:19 +00:00
cbIdentity.setVisibility(View.GONE);
2018-08-02 13:33:06 +00:00
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
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
}
});
2019-03-13 07:17:24 +00:00
rgEncryption.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
2018-10-23 07:52:20 +00:00
@Override
2019-03-13 07:17:24 +00:00
public void onCheckedChanged(RadioGroup group, int id) {
etPort.setHint(id == R.id.radio_starttls ? "143" : "993");
2018-10-23 07:52:20 +00:00
}
});
cbInsecure.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Object tag = cbInsecure.getTag();
if (tag != null && tag.equals(isChecked))
return;
if (isChecked)
rgEncryption.check(R.id.radio_starttls);
}
});
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) {
if (TextUtils.isEmpty(s))
tilPassword.setEndIconMode(END_ICON_PASSWORD_TOGGLE);
}
@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);
}
});
2020-02-10 15:35:14 +00:00
btnCertificate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.selectKeyAlias(getActivity(), getViewLifecycleOwner(), null, new Helper.IKeyAlias() {
@Override
public void onSelected(String alias) {
certificate = alias;
tvCertificate.setText(alias);
}
@Override
public void onNothingSelected() {
certificate = null;
tvCertificate.setText(getString(R.string.title_optional));
}
});
}
});
2019-12-25 09:24:31 +00:00
btnOAuth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onAuth();
}
});
2018-12-26 11:34:05 +00:00
btnColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2019-09-29 14:31:13 +00:00
Bundle args = new Bundle();
args.putInt("color", btnColor.getColor());
args.putString("title", getString(R.string.title_color));
2019-09-29 17:04:21 +00:00
args.putBoolean("reset", true);
2019-09-29 14:31:13 +00:00
2019-06-30 17:56:23 +00:00
FragmentDialogColor fragment = new FragmentDialogColor();
2019-09-29 14:31:13 +00:00
fragment.setArguments(args);
fragment.setTargetFragment(FragmentAccount.this, REQUEST_COLOR);
2019-10-12 15:16:53 +00:00
fragment.show(getParentFragmentManager(), "account:color");
2018-12-26 11:34:05 +00:00
}
});
2019-06-28 19:22:20 +00:00
Helper.linkPro(tvColorPro);
2018-09-05 18:37:20 +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() {
2019-04-22 18:43:04 +00:00
scroll.smoothScrollTo(0, btnAdvanced.getTop());
2018-09-12 19:19:42 +00:00
}
});
2018-09-12 16:54:48 +00:00
}
});
2018-12-26 11:34:05 +00:00
cbSynchronize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
2018-09-12 16:54:48 +00:00
@Override
2018-12-26 11:34:05 +00:00
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
2019-12-08 14:00:15 +00:00
cbOnDemand.setEnabled(checked);
2018-12-26 11:34:05 +00:00
cbPrimary.setEnabled(checked);
2018-09-12 16:54:48 +00:00
}
});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
2019-05-16 06:13:18 +00:00
Helper.hide(cbNotify);
Helper.hide(view.findViewById(R.id.tvNotifyPro));
}
2018-11-24 09:32:21 +00:00
2019-06-28 19:22:20 +00:00
Helper.linkPro(tvNotifyPro);
2019-12-12 13:52:45 +00:00
etInterval.setHint(Integer.toString(EntityAccount.DEFAULT_KEEP_ALIVE_INTERVAL));
2018-08-10 09:45:36 +00:00
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2019-01-17 09:57:01 +00:00
onCheck();
}
});
2019-01-17 09:57:01 +00:00
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2019-06-16 06:51:27 +00:00
onSave(false);
2019-01-17 09:57:01 +00:00
}
});
2018-12-30 12:35:34 +00:00
2020-01-30 19:52:18 +00:00
addKeyPressedListener(new ActivityBase.IKeyPressedListener() {
@Override
2020-02-20 15:46:51 +00:00
public boolean onKeyPressed(KeyEvent event) {
2020-01-30 19:52:18 +00:00
return false;
}
2019-07-06 14:24:03 +00:00
@Override
public boolean onBackPressed() {
onSave(true);
return true;
}
});
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
public void onClick(View view) {
2020-01-05 08:27:34 +00:00
Helper.view(getContext(), Uri.parse(Helper.SUPPORT_URI), false);
2019-09-09 14:44:06 +00:00
}
});
2019-01-17 09:57:01 +00:00
adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<EntityFolder>());
adapter.setDropDownViewResource(R.layout.spinner_item1_dropdown);
2018-12-30 12:35:34 +00:00
2019-01-17 09:57:01 +00:00
spDrafts.setAdapter(adapter);
spSent.setAdapter(adapter);
2019-06-16 06:51:27 +00:00
spArchive.setAdapter(adapter);
2019-01-17 09:57:01 +00:00
spTrash.setAdapter(adapter);
spJunk.setAdapter(adapter);
2020-06-06 09:18:11 +00:00
adapterSwipe = new ArrayAdapter<EntityFolder>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList<EntityFolder>()) {
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return localize(position, super.getView(position, convertView, parent));
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return localize(position, super.getDropDownView(position, convertView, parent));
}
private View localize(int position, View view) {
EntityFolder folder = getItem(position);
if (folder != null) {
TextView tv = view.findViewById(android.R.id.text1);
tv.setText(EntityFolder.localizeName(view.getContext(), folder.name));
}
return view;
}
};
adapterSwipe.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spLeft.setAdapter(adapterSwipe);
spRight.setAdapter(adapterSwipe);
2019-10-23 10:51:20 +00:00
spMove.setAdapter(adapter);
2019-01-17 09:57:01 +00:00
// Initialize
Helper.setViewsEnabled(view, false);
2020-02-28 16:18:23 +00:00
tvGmailHint.setVisibility(GONE);
2019-01-17 09:57:01 +00:00
btnAutoConfig.setEnabled(false);
2019-08-17 10:01:42 +00:00
pbAutoConfig.setVisibility(View.GONE);
2019-03-13 07:17:24 +00:00
rgEncryption.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
cbInsecure.setVisibility(View.GONE);
tilPassword.setEndIconMode(id < 0 || Helper.isSecure(getContext()) ? END_ICON_PASSWORD_TOGGLE : END_ICON_NONE);
tvCharacters.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
btnAdvanced.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
tvIdle.setVisibility(View.GONE);
2019-02-21 19:31:28 +00:00
tvUtf8.setVisibility(View.GONE);
2018-09-22 10:29:35 +00:00
2019-01-17 09:57:01 +00:00
btnCheck.setVisibility(View.GONE);
pbCheck.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
btnSave.setVisibility(View.GONE);
pbSave.setVisibility(View.GONE);
2019-10-19 18:56:19 +00:00
cbIdentity.setVisibility(View.GONE);
2019-07-06 14:24:03 +00:00
2019-12-16 18:09:49 +00:00
cbTrust.setVisibility(View.GONE);
2019-07-06 14:24:03 +00:00
btnHelp.setVisibility(View.GONE);
2019-09-09 14:44:06 +00:00
btnSupport.setVisibility(View.GONE);
2019-06-19 12:51:19 +00:00
tvInstructions.setVisibility(View.GONE);
tvInstructions.setMovementMethod(LinkMovementMethod.getInstance());
2019-01-17 09:57:01 +00:00
grpServer.setVisibility(View.GONE);
grpAuthorize.setVisibility(View.GONE);
grpAdvanced.setVisibility(View.GONE);
grpFolders.setVisibility(View.GONE);
2019-11-15 08:33:43 +00:00
grpError.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
return view;
}
private void onAutoConfig() {
Bundle args = new Bundle();
2019-10-04 10:34:15 +00:00
args.putString("domain", etDomain.getText().toString().trim());
2019-01-17 09:57:01 +00:00
new SimpleTask<EmailProvider>() {
@Override
protected void onPreExecute(Bundle args) {
etDomain.setEnabled(false);
btnAutoConfig.setEnabled(false);
2019-08-17 10:01:42 +00:00
pbAutoConfig.setVisibility(View.VISIBLE);
2018-08-10 09:45:36 +00:00
}
2018-08-02 13:33:06 +00:00
@Override
2019-01-17 09:57:01 +00:00
protected void onPostExecute(Bundle args) {
etDomain.setEnabled(true);
btnAutoConfig.setEnabled(true);
2019-08-17 10:01:42 +00:00
pbAutoConfig.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
}
2019-01-17 09:57:01 +00:00
@Override
protected EmailProvider onExecute(Context context, Bundle args) throws Throwable {
String domain = args.getString("domain");
2019-08-17 09:26:47 +00:00
return EmailProvider.fromDomain(context, domain, EmailProvider.Discover.IMAP);
2019-01-17 09:57:01 +00:00
}
2018-12-30 12:35:34 +00:00
2019-01-17 09:57:01 +00:00
@Override
protected void onExecuted(Bundle args, EmailProvider provider) {
2019-08-17 09:50:33 +00:00
etHost.setText(provider.imap.host);
etPort.setText(Integer.toString(provider.imap.port));
rgEncryption.check(provider.imap.starttls ? R.id.radio_starttls : R.id.radio_ssl);
2019-01-17 09:57:01 +00:00
}
@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
2019-12-06 07:50:46 +00:00
Log.unexpectedError(getParentFragmentManager(), ex);
2019-01-17 09:57:01 +00:00
}
2019-09-11 12:03:59 +00:00
}.execute(this, args, "account:config");
2019-01-17 09:57:01 +00:00
}
private void onCheck() {
Bundle args = new Bundle();
args.putLong("id", id);
2019-10-04 10:34:15 +00:00
args.putString("host", etHost.getText().toString().trim());
2019-03-13 07:17:24 +00:00
args.putBoolean("starttls", rgEncryption.getCheckedRadioButtonId() == R.id.radio_starttls);
2019-01-17 09:57:01 +00:00
args.putBoolean("insecure", cbInsecure.isChecked());
args.putString("port", etPort.getText().toString());
2019-09-18 14:34:07 +00:00
args.putInt("auth", auth);
2019-12-21 15:03:57 +00:00
args.putString("provider", provider);
2019-12-21 15:11:24 +00:00
args.putString("user", etUser.getText().toString().trim());
2019-01-17 09:57:01 +00:00
args.putString("password", tilPassword.getEditText().getText().toString());
2020-02-10 15:35:14 +00:00
args.putString("certificate", certificate);
2019-01-17 09:57:01 +00:00
args.putString("realm", etRealm.getText().toString());
2019-12-16 18:09:49 +00:00
args.putString("fingerprint", cbTrust.isChecked() ? (String) cbTrust.getTag() : null);
2019-01-17 09:57:01 +00:00
new SimpleTask<CheckResult>() {
@Override
protected void onPreExecute(Bundle args) {
saving = true;
getActivity().invalidateOptionsMenu();
2019-01-17 09:57:01 +00:00
Helper.setViewsEnabled(view, false);
pbCheck.setVisibility(View.VISIBLE);
tvIdle.setVisibility(View.GONE);
tvUtf8.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
grpFolders.setVisibility(View.GONE);
2019-11-15 08:33:43 +00:00
grpError.setVisibility(View.GONE);
2019-07-06 14:24:03 +00:00
btnHelp.setVisibility(View.GONE);
2019-09-09 14:44:06 +00:00
btnSupport.setVisibility(View.GONE);
2019-06-19 12:51:19 +00:00
tvInstructions.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
}
@Override
protected void onPostExecute(Bundle args) {
saving = false;
getActivity().invalidateOptionsMenu();
2019-01-17 09:57:01 +00:00
Helper.setViewsEnabled(view, true);
pbCheck.setVisibility(View.GONE);
}
@Override
protected CheckResult onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
String host = args.getString("host");
boolean starttls = args.getBoolean("starttls");
boolean insecure = args.getBoolean("insecure");
String port = args.getString("port");
2019-09-18 14:34:07 +00:00
int auth = args.getInt("auth");
2019-12-21 15:03:57 +00:00
String provider = args.getString("provider");
2019-01-17 09:57:01 +00:00
String user = args.getString("user");
String password = args.getString("password");
2020-02-10 15:35:14 +00:00
String certificate = args.getString("certificate");
2019-01-17 09:57:01 +00:00
String realm = args.getString("realm");
2019-12-16 18:09:49 +00:00
String fingerprint = args.getString("fingerprint");
2019-01-17 09:57:01 +00:00
2019-07-15 10:41:51 +00:00
if (host.contains(":")) {
Uri h = Uri.parse(host);
host = h.getHost();
}
2019-01-17 09:57:01 +00:00
if (TextUtils.isEmpty(host))
throw new IllegalArgumentException(context.getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
port = (starttls ? "143" : "993");
2019-01-17 09:57:01 +00:00
if (TextUtils.isEmpty(user))
throw new IllegalArgumentException(context.getString(R.string.title_no_user));
2020-02-10 15:35:14 +00:00
if (TextUtils.isEmpty(password) && !insecure && certificate == null)
2019-01-17 09:57:01 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_no_password));
if (TextUtils.isEmpty(realm))
realm = null;
2019-01-20 15:22:21 +00:00
DB db = DB.getInstance(context);
2019-01-17 09:57:01 +00:00
CheckResult result = new CheckResult();
2019-01-20 15:22:21 +00:00
result.account = db.account().getAccount(id);
2019-01-17 09:57:01 +00:00
result.folders = new ArrayList<>();
// Check IMAP server / get folders
2019-07-29 09:17:12 +00:00
String protocol = "imap" + (starttls ? "" : "s");
2020-02-06 12:06:10 +00:00
try (EmailService iservice = new EmailService(
context, protocol, realm, insecure, EmailService.PURPOSE_CHECK, true)) {
iservice.connect(
host, Integer.parseInt(port),
auth, provider,
user, password,
certificate, fingerprint);
2018-12-30 12:35:34 +00:00
2019-09-19 15:41:26 +00:00
result.idle = iservice.hasCapability("IDLE");
2019-01-17 09:57:01 +00:00
2019-02-02 07:28:14 +00:00
boolean inbox = false;
2019-07-28 19:45:29 +00:00
for (Folder ifolder : iservice.getStore().getDefaultFolder().list("*")) {
2019-01-17 09:57:01 +00:00
// Check folder attributes
2019-02-01 21:06:14 +00:00
String fullName = ifolder.getFullName();
2019-02-10 16:33:31 +00:00
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
2019-02-01 21:06:14 +00:00
Log.i(fullName + " attrs=" + TextUtils.join(" ", attrs));
2019-07-07 06:17:27 +00:00
String type = EntityFolder.getType(attrs, fullName, true);
2020-03-31 08:12:03 +00:00
boolean selectable = true;
for (String attr : attrs)
if (attr.equalsIgnoreCase("\\NoSelect"))
selectable = false;
selectable = selectable && ((ifolder.getType() & IMAPFolder.HOLDS_MESSAGES) != 0);
2020-03-31 11:43:58 +00:00
if (type != null && selectable) {
2019-01-17 09:57:01 +00:00
// Create entry
2019-02-01 21:06:14 +00:00
EntityFolder folder = db.folder().getFolderByName(id, fullName);
2019-09-13 13:04:51 +00:00
if (folder == null)
folder = new EntityFolder(fullName, type);
2019-01-17 09:57:01 +00:00
result.folders.add(folder);
2019-09-13 13:04:51 +00:00
if (EntityFolder.INBOX.equals(type)) {
inbox = true;
result.utf8 = (Boolean) ((IMAPFolder) ifolder).doCommand(new IMAPFolder.ProtocolCommand() {
@Override
public Object doCommand(IMAPProtocol protocol) {
return protocol.supportsUtf8();
}
});
2019-09-13 13:04:51 +00:00
}
2019-01-17 09:57:01 +00:00
Log.i(folder.name + " id=" + folder.id +
" type=" + folder.type + " attr=" + TextUtils.join(",", attrs));
}
}
2018-08-22 12:30:27 +00:00
2020-05-07 07:31:32 +00:00
EntityFolder.guessTypes(result.folders, iservice.getStore().getDefaultFolder().getSeparator());
2019-09-13 13:04:51 +00:00
2019-02-02 07:28:14 +00:00
if (!inbox)
2019-02-04 16:10:30 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_no_inbox));
2019-01-17 09:57:01 +00:00
2019-05-06 20:34:18 +00:00
if (result.folders.size() > 0)
2019-06-28 07:46:18 +00:00
Collections.sort(result.folders, result.folders.get(0).getComparator(null));
2019-01-17 09:57:01 +00:00
}
2018-10-07 06:53:09 +00:00
2019-01-17 09:57:01 +00:00
return result;
}
2018-10-07 06:53:09 +00:00
2019-01-17 09:57:01 +00:00
@Override
protected void onExecuted(Bundle args, CheckResult result) {
tvIdle.setVisibility(result.idle ? View.GONE : View.VISIBLE);
tvUtf8.setVisibility(result.utf8 == null || result.utf8 ? View.GONE : View.VISIBLE);
2019-10-17 15:24:04 +00:00
if (!result.idle)
etInterval.setText(Integer.toString(EntityAccount.DEFAULT_POLL_INTERVAL));
2019-02-09 21:03:53 +00:00
setFolders(result.folders, result.account);
2018-08-22 12:30:27 +00:00
2019-12-16 18:09:49 +00:00
if (!cbTrust.isChecked())
cbTrust.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
new Handler().post(new Runnable() {
@Override
public void run() {
2019-10-19 18:56:19 +00:00
scroll.smoothScrollTo(0, cbIdentity.getBottom());
2019-01-17 09:57:01 +00:00
}
});
}
2019-01-17 09:57:01 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
grpFolders.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
2019-10-19 18:56:19 +00:00
cbIdentity.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
2019-06-20 06:39:37 +00:00
else
showError(ex);
2019-01-17 09:57:01 +00:00
}
2019-09-11 12:03:59 +00:00
}.execute(this, args, "account:check");
2019-01-17 09:57:01 +00:00
}
2018-10-07 06:53:09 +00:00
2019-06-16 06:51:27 +00:00
private void onSave(boolean should) {
2019-01-17 09:57:01 +00:00
EntityFolder drafts = (EntityFolder) spDrafts.getSelectedItem();
EntityFolder sent = (EntityFolder) spSent.getSelectedItem();
2019-06-16 06:51:27 +00:00
EntityFolder archive = (EntityFolder) spArchive.getSelectedItem();
2019-01-17 09:57:01 +00:00
EntityFolder trash = (EntityFolder) spTrash.getSelectedItem();
EntityFolder junk = (EntityFolder) spJunk.getSelectedItem();
2019-01-20 15:22:21 +00:00
EntityFolder left = (EntityFolder) spLeft.getSelectedItem();
EntityFolder right = (EntityFolder) spRight.getSelectedItem();
2019-10-23 10:51:20 +00:00
EntityFolder move = (EntityFolder) spMove.getSelectedItem();
2019-01-17 09:57:01 +00:00
2019-06-29 15:30:31 +00:00
if (drafts != null && drafts.id != null && drafts.id == 0L)
2019-01-17 09:57:01 +00:00
drafts = null;
2019-06-29 15:30:31 +00:00
if (sent != null && sent.id != null && sent.id == 0L)
2019-01-17 09:57:01 +00:00
sent = null;
2019-06-29 15:30:31 +00:00
if (archive != null && archive.id != null && archive.id == 0L)
2019-06-16 06:51:27 +00:00
archive = null;
2019-06-29 15:30:31 +00:00
if (trash != null && trash.id != null && trash.id == 0L)
2019-01-17 09:57:01 +00:00
trash = null;
2019-06-29 15:30:31 +00:00
if (junk != null && junk.id != null && junk.id == 0L)
2019-01-17 09:57:01 +00:00
junk = null;
2019-06-29 15:30:31 +00:00
if (left != null && left.id != null && left.id == 0L)
2019-01-20 15:22:21 +00:00
left = null;
2019-06-29 15:30:31 +00:00
if (right != null && right.id != null && right.id == 0L)
2019-01-20 15:22:21 +00:00
right = null;
2019-01-17 09:57:01 +00:00
2019-10-23 10:51:20 +00:00
if (move != null && move.id != null && move.id == 0L)
move = null;
2019-01-17 09:57:01 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
2019-10-04 10:34:15 +00:00
args.putString("host", etHost.getText().toString().trim());
2019-03-13 07:17:24 +00:00
args.putBoolean("starttls", rgEncryption.getCheckedRadioButtonId() == R.id.radio_starttls);
2019-01-17 09:57:01 +00:00
args.putBoolean("insecure", cbInsecure.isChecked());
args.putString("port", etPort.getText().toString());
2019-09-18 14:34:07 +00:00
args.putInt("auth", auth);
2019-12-21 15:03:57 +00:00
args.putString("provider", provider);
2019-12-21 15:11:24 +00:00
args.putString("user", etUser.getText().toString().trim());
2019-01-17 09:57:01 +00:00
args.putString("password", tilPassword.getEditText().getText().toString());
2020-02-10 15:35:14 +00:00
args.putString("certificate", certificate);
2019-01-17 09:57:01 +00:00
args.putString("realm", etRealm.getText().toString());
2019-12-16 18:09:49 +00:00
args.putString("fingerprint", cbTrust.isChecked() ? (String) cbTrust.getTag() : null);
2019-01-17 09:57:01 +00:00
args.putString("name", etName.getText().toString());
2019-09-29 12:45:00 +00:00
args.putInt("color", btnColor.getColor());
2019-01-17 09:57:01 +00:00
args.putBoolean("synchronize", cbSynchronize.isChecked());
2019-12-08 14:00:15 +00:00
args.putBoolean("ondemand", cbOnDemand.isChecked());
2019-01-17 09:57:01 +00:00
args.putBoolean("primary", cbPrimary.isChecked());
args.putBoolean("notify", cbNotify.isChecked());
args.putBoolean("browse", cbBrowse.isChecked());
args.putBoolean("auto_seen", cbAutoSeen.isChecked());
2019-01-17 09:57:01 +00:00
args.putString("interval", etInterval.getText().toString());
args.putBoolean("partial_fetch", cbPartialFetch.isChecked());
args.putBoolean("ignore_size", cbIgnoreSize.isChecked());
args.putBoolean("use_date", cbUseDate.isChecked());
2019-01-17 09:57:01 +00:00
args.putSerializable("drafts", drafts);
args.putSerializable("sent", sent);
2019-06-16 06:51:27 +00:00
args.putSerializable("archive", archive);
2019-01-17 09:57:01 +00:00
args.putSerializable("trash", trash);
args.putSerializable("junk", junk);
2019-01-21 07:17:17 +00:00
args.putSerializable("left", left);
args.putSerializable("right", right);
2019-10-23 10:51:20 +00:00
args.putSerializable("move", move);
2019-01-17 09:57:01 +00:00
2019-06-16 06:51:27 +00:00
args.putBoolean("should", should);
new SimpleTask<Boolean>() {
2019-01-17 09:57:01 +00:00
@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);
2019-11-15 08:33:43 +00:00
grpError.setVisibility(View.GONE);
2019-07-06 14:24:03 +00:00
btnHelp.setVisibility(View.GONE);
2019-09-09 14:44:06 +00:00
btnSupport.setVisibility(View.GONE);
2019-07-06 14:24:03 +00:00
tvInstructions.setVisibility(View.GONE);
2019-01-17 09:57:01 +00:00
}
2018-08-22 12:30:27 +00:00
2019-01-17 09:57:01 +00:00
@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);
}
2018-11-24 09:32:21 +00:00
2019-01-17 09:57:01 +00:00
@Override
2019-06-16 06:51:27 +00:00
protected Boolean onExecute(Context context, Bundle args) throws Throwable {
2019-01-17 09:57:01 +00:00
long id = args.getLong("id");
String host = args.getString("host");
boolean starttls = args.getBoolean("starttls");
boolean insecure = args.getBoolean("insecure");
String port = args.getString("port");
2019-09-18 14:34:07 +00:00
int auth = args.getInt("auth");
2019-12-21 15:03:57 +00:00
String provider = args.getString("provider");
2019-06-21 17:46:46 +00:00
String user = args.getString("user").trim();
2019-01-17 09:57:01 +00:00
String password = args.getString("password");
2020-02-10 15:35:14 +00:00
String certificate = args.getString("certificate");
2019-01-17 09:57:01 +00:00
String realm = args.getString("realm");
2019-12-16 18:09:49 +00:00
String fingerprint = args.getString("fingerprint");
2019-01-17 09:57:01 +00:00
String name = args.getString("name");
Integer color = args.getInt("color");
boolean synchronize = args.getBoolean("synchronize");
2019-12-08 14:00:15 +00:00
boolean ondemand = args.getBoolean("ondemand");
2019-01-17 09:57:01 +00:00
boolean primary = args.getBoolean("primary");
boolean notify = args.getBoolean("notify");
boolean browse = args.getBoolean("browse");
boolean auto_seen = args.getBoolean("auto_seen");
2019-01-17 09:57:01 +00:00
String interval = args.getString("interval");
boolean partial_fetch = args.getBoolean("partial_fetch");
boolean ignore_size = args.getBoolean("ignore_size");
boolean use_date = args.getBoolean("use_date");
2019-01-17 09:57:01 +00:00
EntityFolder drafts = (EntityFolder) args.getSerializable("drafts");
EntityFolder sent = (EntityFolder) args.getSerializable("sent");
2019-06-16 06:51:27 +00:00
EntityFolder archive = (EntityFolder) args.getSerializable("archive");
2019-01-17 09:57:01 +00:00
EntityFolder trash = (EntityFolder) args.getSerializable("trash");
EntityFolder junk = (EntityFolder) args.getSerializable("junk");
2019-01-21 07:17:17 +00:00
EntityFolder left = (EntityFolder) args.getSerializable("left");
EntityFolder right = (EntityFolder) args.getSerializable("right");
2019-10-23 10:51:20 +00:00
EntityFolder move = (EntityFolder) args.getSerializable("move");
2019-01-17 09:57:01 +00:00
2019-08-13 08:27:17 +00:00
boolean pro = ActivityBilling.isPro(context);
2019-06-16 06:51:27 +00:00
boolean should = args.getBoolean("should");
2019-07-15 10:41:51 +00:00
if (host.contains(":")) {
Uri h = Uri.parse(host);
host = h.getHost();
}
2020-01-29 09:06:45 +00:00
if (TextUtils.isEmpty(host) && !should)
throw new IllegalArgumentException(context.getString(R.string.title_no_host));
2019-01-17 09:57:01 +00:00
if (TextUtils.isEmpty(port))
port = (starttls ? "143" : "993");
2020-01-29 09:06:45 +00:00
if (TextUtils.isEmpty(user) && !should)
throw new IllegalArgumentException(context.getString(R.string.title_no_user));
2020-02-10 15:35:14 +00:00
if (synchronize && TextUtils.isEmpty(password) && !insecure && certificate == null && !should)
2020-01-29 09:06:45 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_no_password));
int poll_interval = (TextUtils.isEmpty(interval)
? EntityAccount.DEFAULT_KEEP_ALIVE_INTERVAL : Integer.parseInt(interval));
2019-01-17 09:57:01 +00:00
if (TextUtils.isEmpty(realm))
realm = null;
2019-06-16 06:51:27 +00:00
if (TextUtils.isEmpty(name))
name = user;
2019-06-28 11:08:04 +00:00
if (color == Color.TRANSPARENT || !pro)
2019-01-17 09:57:01 +00:00
color = null;
2019-06-28 11:08:04 +00:00
if (!pro)
notify = false;
2019-01-17 09:57:01 +00:00
long now = new Date().getTime();
DB db = DB.getInstance(context);
EntityAccount account = db.account().getAccount(id);
2019-06-16 06:51:27 +00:00
if (should) {
if (account == null)
return !TextUtils.isEmpty(host) && !TextUtils.isEmpty(user);
if (!Objects.equals(account.host, host))
return true;
if (!Objects.equals(account.starttls, starttls))
return true;
if (!Objects.equals(account.insecure, insecure))
return true;
if (!Objects.equals(account.port, Integer.parseInt(port)))
return true;
2019-09-18 14:34:07 +00:00
if (account.auth_type != auth)
return true;
2019-06-16 06:51:27 +00:00
if (!Objects.equals(account.user, user))
return true;
if (!Objects.equals(account.password, password))
return true;
2020-02-10 15:35:14 +00:00
if (!Objects.equals(account.certificate_alias, certificate))
return true;
2019-06-16 06:51:27 +00:00
if (!Objects.equals(account.realm, realm))
return true;
2019-12-16 18:09:49 +00:00
if (!Objects.equals(account.fingerprint, fingerprint))
return true;
2019-06-16 06:51:27 +00:00
if (!Objects.equals(account.name, name))
return true;
if (!Objects.equals(account.color, color))
return true;
if (!Objects.equals(account.synchronize, synchronize))
return true;
2019-12-08 14:00:15 +00:00
if (!Objects.equals(account.ondemand, ondemand))
return true;
2019-06-16 06:51:27 +00:00
if (!Objects.equals(account.primary, account.synchronize && primary))
return true;
if (!Objects.equals(account.notify, notify))
return true;
if (!Objects.equals(account.browse, browse))
return true;
if (!Objects.equals(account.auto_seen, auto_seen))
return true;
if (!Objects.equals(account.poll_interval, poll_interval))
2019-06-16 06:51:27 +00:00
return true;
if (!Objects.equals(account.partial_fetch, partial_fetch))
return true;
if (!Objects.equals(account.ignore_size, ignore_size))
return true;
if (!Objects.equals(account.use_date, use_date))
return true;
2019-06-16 06:51:27 +00:00
EntityFolder edrafts = db.folder().getFolderByType(account.id, EntityFolder.DRAFTS);
if (!Objects.equals(edrafts == null ? null : edrafts.id, drafts == null ? null : drafts.id))
return true;
EntityFolder esent = db.folder().getFolderByType(account.id, EntityFolder.SENT);
if (!Objects.equals(esent == null ? null : esent.id, sent == null ? null : sent.id))
return true;
EntityFolder earchive = db.folder().getFolderByType(account.id, EntityFolder.ARCHIVE);
if (!Objects.equals(earchive == null ? null : earchive.id, archive == null ? null : archive.id))
return true;
EntityFolder etrash = db.folder().getFolderByType(account.id, EntityFolder.TRASH);
if (!Objects.equals(etrash == null ? null : etrash.id, trash == null ? null : trash.id))
return true;
EntityFolder ejunk = db.folder().getFolderByType(account.id, EntityFolder.JUNK);
if (!Objects.equals(ejunk == null ? null : ejunk.id, junk == null ? null : junk.id))
return true;
if (!Objects.equals(account.swipe_left, left == null ? null : left.id))
2019-06-16 06:51:27 +00:00
return true;
if (!Objects.equals(account.swipe_right, right == null ? null : right.id))
2019-06-16 06:51:27 +00:00
return true;
2019-10-23 10:51:20 +00:00
if (!Objects.equals(account.move_to, move == null ? null : move.id))
return true;
2019-06-16 06:51:27 +00:00
return false;
}
2019-01-17 09:57:01 +00:00
String accountRealm = (account == null ? null : account.realm);
boolean check = (synchronize && (account == null ||
!account.synchronize ||
account.error != null ||
!account.host.equals(host) ||
!account.starttls.equals(starttls) ||
2019-07-06 14:24:03 +00:00
!account.insecure.equals(insecure) ||
!account.port.equals(Integer.parseInt(port)) ||
!account.user.equals(user) ||
!account.password.equals(password) ||
2020-02-10 15:35:14 +00:00
!Objects.equals(account.certificate_alias, certificate) ||
2019-12-16 18:09:49 +00:00
!Objects.equals(realm, accountRealm) ||
!Objects.equals(account.fingerprint, fingerprint)));
Log.i("Account check=" + check);
2019-01-17 09:57:01 +00:00
Long last_connected = null;
if (account != null && synchronize == account.synchronize)
last_connected = account.last_connected;
2019-01-17 09:57:01 +00:00
// Check IMAP server
2019-02-02 07:28:14 +00:00
EntityFolder inbox = null;
2019-01-17 09:57:01 +00:00
if (check) {
2019-07-29 09:17:12 +00:00
String protocol = "imap" + (starttls ? "" : "s");
2020-02-06 12:06:10 +00:00
try (EmailService iservice = new EmailService(
context, protocol, realm, insecure, EmailService.PURPOSE_CHECK, true)) {
iservice.connect(
host, Integer.parseInt(port),
auth, provider,
user, password,
certificate, fingerprint);
2019-02-02 07:28:14 +00:00
2019-07-28 19:45:29 +00:00
for (Folder ifolder : iservice.getStore().getDefaultFolder().list("*")) {
2019-02-02 07:28:14 +00:00
// Check folder attributes
String fullName = ifolder.getFullName();
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
2019-02-02 07:28:14 +00:00
Log.i(fullName + " attrs=" + TextUtils.join(" ", attrs));
2019-07-07 06:17:27 +00:00
String type = EntityFolder.getType(attrs, fullName, true);
2019-02-02 07:28:14 +00:00
if (EntityFolder.INBOX.equals(type)) {
inbox = new EntityFolder();
inbox.name = fullName;
inbox.type = type;
inbox.synchronize = true;
inbox.unified = true;
inbox.notify = true;
inbox.sync_days = EntityFolder.DEFAULT_SYNC;
inbox.keep_days = EntityFolder.DEFAULT_KEEP;
2019-02-02 07:28:14 +00:00
}
}
2019-01-17 09:57:01 +00:00
}
}
2019-01-17 09:57:01 +00:00
try {
db.beginTransaction();
if (account != null && !account.password.equals(password)) {
2020-04-16 15:39:29 +00:00
String domain = DnsHelper.getParentDomain(account.host);
2020-01-08 10:38:45 +00:00
String match = (Objects.equals(account.host, domain) ? account.host : "%." + domain);
int count = db.identity().setIdentityPassword(account.id, account.user, password, match);
Log.i("Updated passwords=" + count + " match=" + match);
}
2019-01-17 09:57:01 +00:00
boolean update = (account != null);
if (account == null)
account = new EntityAccount();
2018-08-22 12:30:27 +00:00
2019-01-17 09:57:01 +00:00
account.host = host;
account.starttls = starttls;
account.insecure = insecure;
account.port = Integer.parseInt(port);
2019-09-18 14:34:07 +00:00
account.auth_type = auth;
2019-12-24 09:35:43 +00:00
account.user = user;
account.password = password;
2020-02-10 15:35:14 +00:00
account.certificate_alias = certificate;
2019-12-21 15:03:57 +00:00
account.provider = provider;
2019-01-17 09:57:01 +00:00
account.realm = realm;
2019-12-16 18:09:49 +00:00
account.fingerprint = fingerprint;
2018-08-22 12:30:27 +00:00
2019-01-17 09:57:01 +00:00
account.name = name;
account.color = color;
2018-11-19 17:10:55 +00:00
2019-01-17 09:57:01 +00:00
account.synchronize = synchronize;
2019-12-08 14:00:15 +00:00
account.ondemand = ondemand;
2019-01-17 09:57:01 +00:00
account.primary = (account.synchronize && primary);
account.notify = notify;
account.browse = browse;
account.auto_seen = auto_seen;
if (account.poll_interval != poll_interval) {
account.keep_alive_ok = false;
account.keep_alive_failed = 0;
2020-04-01 06:57:27 +00:00
account.keep_alive_succeeded = 0;
}
account.poll_interval = Math.max(1, poll_interval);
account.partial_fetch = partial_fetch;
account.ignore_size = ignore_size;
account.use_date = use_date;
2019-01-17 09:57:01 +00:00
if (!update)
account.created = now;
2019-03-27 08:31:52 +00:00
account.warning = null;
2019-01-17 09:57:01 +00:00
account.error = null;
account.last_connected = last_connected;
2018-09-21 17:36:19 +00:00
2019-01-17 09:57:01 +00:00
if (account.primary)
db.account().resetPrimary();
2018-09-21 17:36:19 +00:00
2019-01-17 09:57:01 +00:00
if (update)
db.account().updateAccount(account);
else
account.id = db.account().insertAccount(account);
2019-10-19 18:56:19 +00:00
args.putLong("account", account.id);
2019-02-02 07:28:14 +00:00
EntityLog.log(context, (update ? "Updated" : "Added") + " account=" + account.name);
2019-01-17 09:57:01 +00:00
// Make sure the channel exists on commit
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (account.notify) {
// Add or update notification channel
account.deleteNotificationChannel(context);
account.createNotificationChannel(context);
} else if (!account.synchronize)
account.deleteNotificationChannel(context);
}
2019-01-17 09:57:01 +00:00
List<EntityFolder> folders = new ArrayList<>();
2019-02-02 07:28:14 +00:00
if (inbox != null)
folders.add(inbox);
2019-01-17 09:57:01 +00:00
if (drafts != null) {
drafts.type = EntityFolder.DRAFTS;
2020-04-20 05:28:31 +00:00
drafts.poll = false;
2019-01-17 09:57:01 +00:00
folders.add(drafts);
}
2018-12-30 12:35:34 +00:00
2019-01-17 09:57:01 +00:00
if (sent != null) {
sent.type = EntityFolder.SENT;
2020-04-20 05:28:31 +00:00
sent.poll = false;
2019-01-17 09:57:01 +00:00
folders.add(sent);
}
2019-06-16 06:51:27 +00:00
if (archive != null) {
archive.type = EntityFolder.ARCHIVE;
2020-04-20 05:28:31 +00:00
archive.poll = false;
2019-06-16 06:51:27 +00:00
folders.add(archive);
2019-01-17 09:57:01 +00:00
}
if (trash != null) {
trash.type = EntityFolder.TRASH;
2020-04-20 05:28:31 +00:00
trash.poll = false;
2019-01-17 09:57:01 +00:00
folders.add(trash);
}
if (junk != null) {
junk.type = EntityFolder.JUNK;
2020-04-20 05:28:31 +00:00
junk.poll = false;
2019-01-17 09:57:01 +00:00
folders.add(junk);
}
2018-09-05 18:37:20 +00:00
2019-06-29 15:30:31 +00:00
if (left != null && !(left.id != null && left.id < 0)) {
2019-01-21 07:17:17 +00:00
boolean found = false;
for (EntityFolder folder : folders)
if (left.name.equals(folder.name)) {
found = true;
break;
}
if (!found) {
left.type = EntityFolder.USER;
folders.add(left);
}
}
2019-06-29 15:30:31 +00:00
if (right != null && !(right.id != null && right.id < 0)) {
2019-01-21 07:17:17 +00:00
boolean found = false;
for (EntityFolder folder : folders)
if (right.name.equals(folder.name)) {
found = true;
break;
}
if (!found) {
right.type = EntityFolder.USER;
folders.add(right);
}
}
2019-10-23 10:51:20 +00:00
if (move != null && !(move.id != null && move.id < 0)) {
boolean found = false;
for (EntityFolder folder : folders)
if (move.name.equals(folder.name)) {
found = true;
break;
}
if (!found) {
move.type = EntityFolder.USER;
folders.add(move);
}
}
2019-01-17 09:57:01 +00:00
db.folder().setFoldersUser(account.id);
for (EntityFolder folder : folders) {
EntityFolder existing = db.folder().getFolderByName(account.id, folder.name);
if (existing == null) {
folder.account = account.id;
2019-02-02 07:28:14 +00:00
EntityLog.log(context, "Added folder=" + folder.name + " type=" + folder.type);
2019-01-17 09:57:01 +00:00
folder.id = db.folder().insertFolder(folder);
2020-02-06 12:24:44 +00:00
if (folder.synchronize)
EntityOperation.sync(context, folder.id, false);
2019-01-17 09:57:01 +00:00
} else {
2019-02-02 07:28:14 +00:00
EntityLog.log(context, "Updated folder=" + folder.name + " type=" + folder.type);
2019-01-17 09:57:01 +00:00
db.folder().setFolderType(existing.id, folder.type);
}
}
2018-09-05 18:37:20 +00:00
2019-01-21 07:17:17 +00:00
account.swipe_left = (left == null ? null : left.id);
account.swipe_right = (right == null ? null : right.id);
2019-10-23 10:51:20 +00:00
account.move_to = (move == null ? null : move.id);
2019-01-21 07:17:17 +00:00
db.account().updateAccount(account);
2019-01-17 09:57:01 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2018-09-22 10:29:35 +00:00
2019-12-09 18:44:27 +00:00
ServiceSynchronize.eval(context, "save account");
2018-09-05 18:37:20 +00:00
2019-01-17 09:57:01 +00:00
if (!synchronize) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
2019-06-23 18:56:07 +00:00
nm.cancel("receive:" + account.id, 1);
2019-06-24 06:45:55 +00:00
nm.cancel("alert:" + account.id, 1);
2019-01-17 09:57:01 +00:00
}
2018-09-05 18:37:20 +00:00
2019-06-16 06:51:27 +00:00
return false;
2019-01-17 09:57:01 +00:00
}
2018-09-05 18:37:20 +00:00
2019-01-17 09:57:01 +00:00
@Override
2019-06-16 06:51:27 +00:00
protected void onExecuted(Bundle args, Boolean dirty) {
if (dirty) {
Bundle aargs = new Bundle();
aargs.putString("question", getString(R.string.title_ask_save));
FragmentDialogAsk fragment = new FragmentDialogAsk();
fragment.setArguments(aargs);
fragment.setTargetFragment(FragmentAccount.this, REQUEST_SAVE);
2019-10-12 15:16:53 +00:00
fragment.show(getParentFragmentManager(), "account:save");
2020-05-23 07:04:24 +00:00
} else {
Context context = getContext();
if (context != null)
WidgetUnified.updateData(context); // Update color stripe
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
getParentFragmentManager().popBackStack();
if (cbIdentity.isChecked()) {
Bundle aargs = new Bundle();
aargs.putLong("account", args.getLong("account"));
FragmentIdentity fragment = new FragmentIdentity();
fragment.setArguments(aargs);
FragmentTransaction fragmentTransaction = getParentFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("identity");
fragmentTransaction.commit();
}
2019-10-19 18:56:19 +00:00
}
}
2019-01-17 09:57:01 +00:00
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
2019-06-20 06:39:37 +00:00
else
showError(ex);
2019-01-17 09:57:01 +00:00
}
2019-09-11 12:03:59 +00:00
}.execute(this, args, "account:save");
}
2019-12-25 09:24:31 +00:00
private void onAuth() {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<String>() {
@Override
protected void onPreExecute(Bundle args) {
btnOAuth.setEnabled(false);
}
@Override
protected void onPostExecute(Bundle args) {
btnOAuth.setEnabled(true);
}
@Override
protected String onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
DB db = DB.getInstance(context);
EntityAccount account = db.account().getAccount(id);
if (account == null)
return null;
2020-01-08 07:52:08 +00:00
AccountManager am = AccountManager.get(context);
2019-12-25 09:24:31 +00:00
Account[] accounts = am.getAccountsByType("com.google");
for (Account google : accounts)
if (account.user.equals(google.name))
return am.blockingGetAuthToken(
google,
2020-01-29 20:06:45 +00:00
EmailService.getAuthTokenType("com.google"),
2019-12-25 09:24:31 +00:00
true);
return null;
}
@Override
protected void onExecuted(Bundle args, String token) {
ToastEx.makeText(getContext(), R.string.title_completed, Toast.LENGTH_LONG).show();
tilPassword.getEditText().setText(token);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "account:oauth");
}
2019-06-20 06:39:37 +00:00
private void showError(Throwable ex) {
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-06-20 06:39:37 +00:00
2020-01-29 20:06:45 +00:00
if (ex instanceof EmailService.UntrustedException) {
EmailService.UntrustedException e = (EmailService.UntrustedException) ex;
2019-12-16 18:09:49 +00:00
cbTrust.setTag(e.getFingerprint());
cbTrust.setText(getString(R.string.title_trust, e.getFingerprint()));
cbTrust.setVisibility(View.VISIBLE);
}
2019-07-06 14:24:03 +00:00
final EmailProvider provider = (EmailProvider) spProvider.getSelectedItem();
2019-08-04 07:10:52 +00:00
if (provider != null && provider.link != null) {
Uri uri = Uri.parse(provider.link);
2019-07-06 14:24:03 +00:00
btnHelp.setTag(uri);
btnHelp.setVisibility(View.VISIBLE);
}
2019-06-20 06:39:37 +00:00
2019-09-09 14:44:06 +00:00
btnSupport.setVisibility(View.VISIBLE);
2019-06-20 06:39:37 +00:00
if (provider != null && provider.documentation != null) {
tvInstructions.setText(HtmlHelper.fromHtml(provider.documentation.toString()));
tvInstructions.setVisibility(View.VISIBLE);
2019-07-06 14:24:03 +00:00
}
2019-06-20 06:39:37 +00:00
new Handler().post(new Runnable() {
@Override
public void run() {
2019-07-06 14:24:03 +00:00
if (provider != null && provider.documentation != null)
scroll.smoothScrollTo(0, tvInstructions.getBottom());
else
2019-09-09 14:44:06 +00:00
scroll.smoothScrollTo(0, btnSupport.getBottom());
2019-06-20 06:39:37 +00:00
}
});
}
@Override
2018-08-15 09:50:39 +00:00
public void onSaveInstanceState(Bundle outState) {
2019-03-02 07:35:12 +00:00
outState.putInt("fair:provider", spProvider.getSelectedItemPosition());
outState.putString("fair:password", tilPassword.getEditText().getText().toString());
outState.putInt("fair:advanced", grpAdvanced.getVisibility());
2019-09-18 14:34:07 +00:00
outState.putInt("fair:auth", auth);
2019-12-21 15:03:57 +00:00
outState.putString("fair:authprovider", provider);
2019-04-11 17:30:39 +00:00
super.onSaveInstanceState(outState);
2018-08-15 09:50:39 +00:00
}
@Override
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
2018-11-14 16:21:53 +00:00
Bundle args = new Bundle();
2019-08-13 10:07:30 +00:00
args.putLong("id", copy < 0 ? id : copy);
2018-11-14 16:21:53 +00:00
new SimpleTask<EntityAccount>() {
2018-08-02 13:33:06 +00:00
@Override
2018-12-31 07:03:48 +00:00
protected EntityAccount onExecute(Context context, Bundle args) {
2018-11-14 16:21:53 +00:00
long id = args.getLong("id");
2019-09-19 15:41:26 +00:00
DB db = DB.getInstance(context);
return db.account().getAccount(id);
2018-11-14 16:21:53 +00:00
}
2018-09-05 20:39:47 +00:00
2018-11-14 16:21:53 +00:00
@Override
2019-01-20 15:22:21 +00:00
protected void onExecuted(Bundle args, final EntityAccount account) {
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_select)));
providers.add(1, 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> aaProvider =
2018-11-11 14:09:46 +00:00
new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, providers);
aaProvider.setDropDownViewResource(R.layout.spinner_item1_dropdown);
spProvider.setAdapter(aaProvider);
2018-08-15 09:50:39 +00:00
if (savedInstanceState == null) {
2018-08-27 15:08:23 +00:00
if (account != null) {
2018-09-07 10:45:43 +00:00
boolean found = false;
2018-09-06 07:29:30 +00:00
for (int pos = 2; pos < providers.size(); pos++) {
2019-01-13 15:23:04 +00:00
EmailProvider provider = providers.get(pos);
2019-08-17 09:50:33 +00:00
if (provider.imap.host.equals(account.host) &&
provider.imap.port == account.port &&
provider.imap.starttls == account.starttls) {
2018-09-07 10:45:43 +00:00
found = true;
2018-08-27 15:08:23 +00:00
spProvider.setTag(pos);
spProvider.setSelection(pos);
break;
}
2018-09-06 07:29:30 +00:00
}
2018-09-07 10:45:43 +00:00
if (!found) {
spProvider.setTag(1);
spProvider.setSelection(1);
}
2018-09-05 18:37:20 +00:00
etHost.setText(account.host);
etPort.setText(Long.toString(account.port));
2018-08-27 15:08:23 +00:00
}
2019-03-13 07:17:24 +00:00
rgEncryption.check(account != null && account.starttls ? R.id.radio_starttls : R.id.radio_ssl);
cbInsecure.setTag(account == null ? false : account.insecure);
2018-11-29 17:08:57 +00:00
cbInsecure.setChecked(account == null ? false : account.insecure);
2018-08-16 11:26:53 +00:00
etUser.setText(account == null ? null : account.user);
tilPassword.getEditText().setText(account == null ? null : account.password);
2020-02-10 15:35:14 +00:00
certificate = (account == null ? null : account.certificate_alias);
tvCertificate.setText(certificate == null ? getString(R.string.title_optional) : certificate);
2019-01-10 18:24:20 +00:00
etRealm.setText(account == null ? null : account.realm);
2018-08-27 15:08:23 +00:00
2019-12-16 18:09:49 +00:00
if (account == null || account.fingerprint == null) {
cbTrust.setTag(null);
cbTrust.setChecked(false);
} else {
cbTrust.setTag(account.fingerprint);
cbTrust.setChecked(true);
cbTrust.setText(getString(R.string.title_trust, account.fingerprint));
}
2018-09-05 18:37:20 +00:00
etName.setText(account == null ? null : account.name);
2019-09-29 12:45:00 +00:00
btnColor.setColor(account == null ? null : account.color);
2019-06-28 11:08:04 +00:00
2019-08-13 08:27:17 +00:00
boolean pro = ActivityBilling.isPro(getContext());
2019-06-28 11:08:04 +00:00
cbNotify.setChecked(account != null && account.notify && pro);
cbNotify.setEnabled(pro);
2018-09-05 18:37:20 +00:00
2018-08-16 11:26:53 +00:00
cbSynchronize.setChecked(account == null ? true : account.synchronize);
2019-12-08 14:00:15 +00:00
cbOnDemand.setChecked(account == null ? false : account.ondemand);
cbPrimary.setChecked(account == null ? false : account.primary);
cbBrowse.setChecked(account == null ? true : account.browse);
cbAutoSeen.setChecked(account == null ? true : account.auto_seen);
2018-10-23 15:47:29 +00:00
etInterval.setText(account == null ? "" : Long.toString(account.poll_interval));
cbPartialFetch.setChecked(account == null ? true : account.partial_fetch);
cbIgnoreSize.setChecked(account == null ? false : account.ignore_size);
cbUseDate.setChecked(account == null ? false : account.use_date);
2020-01-29 20:06:45 +00:00
auth = (account == null ? EmailService.AUTH_TYPE_PASSWORD : account.auth_type);
2019-12-21 15:03:57 +00:00
provider = (account == null ? null : account.provider);
2018-09-12 16:54:48 +00:00
new SimpleTask<EntityAccount>() {
@Override
2018-12-31 07:03:48 +00:00
protected EntityAccount onExecute(Context context, Bundle args) {
return DB.getInstance(context).account().getPrimaryAccount();
}
@Override
2018-12-31 07:03:48 +00:00
protected void onExecuted(Bundle args, EntityAccount primary) {
if (primary == null)
cbPrimary.setChecked(true);
}
2018-12-01 09:47:08 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(getParentFragmentManager(), ex);
}
2019-01-12 13:15:15 +00:00
}.execute(FragmentAccount.this, new Bundle(), "account:primary");
2018-08-15 09:50:39 +00:00
} else {
2019-12-21 15:03:57 +00:00
int p = savedInstanceState.getInt("fair:provider");
spProvider.setTag(p);
spProvider.setSelection(p);
2018-08-27 15:08:23 +00:00
2019-03-02 07:35:12 +00:00
tilPassword.getEditText().setText(savedInstanceState.getString("fair:password"));
grpAdvanced.setVisibility(savedInstanceState.getInt("fair:advanced"));
2019-09-18 14:34:07 +00:00
auth = savedInstanceState.getInt("fair:auth");
2019-12-21 15:03:57 +00:00
provider = savedInstanceState.getString("fair:authprovider");
2018-08-15 09:50:39 +00:00
}
Helper.setViewsEnabled(view, true);
2020-01-29 20:06:45 +00:00
if (auth != EmailService.AUTH_TYPE_PASSWORD) {
2019-09-18 14:34:07 +00:00
etUser.setEnabled(false);
tilPassword.setEnabled(false);
2020-02-10 15:35:14 +00:00
btnCertificate.setEnabled(false);
2019-09-18 14:34:07 +00:00
}
2020-01-29 20:06:45 +00:00
if (account == null || account.auth_type != EmailService.AUTH_TYPE_GMAIL)
2019-12-25 09:24:31 +00:00
Helper.hide((btnOAuth));
2019-12-08 14:00:15 +00:00
cbOnDemand.setEnabled(cbSynchronize.isChecked());
cbPrimary.setEnabled(cbSynchronize.isChecked());
2018-08-16 11:26:53 +00:00
// Consider previous check/save/delete as cancelled
pbWait.setVisibility(View.GONE);
2019-10-19 18:56:19 +00:00
if (copy < 0 && account != null) {
args.putLong("account", account.id);
2018-11-14 16:21:53 +00:00
2019-12-16 18:13:54 +00:00
final SimpleTask task = new SimpleTask<List<EntityFolder>>() {
2019-08-13 10:07:30 +00:00
@Override
protected List<EntityFolder> onExecute(Context context, Bundle args) {
long account = args.getLong("account");
2019-01-18 11:15:07 +00:00
2019-08-13 10:07:30 +00:00
DB db = DB.getInstance(context);
List<EntityFolder> folders = db.folder().getFolders(account, false, true);
2019-01-18 11:15:07 +00:00
2019-08-13 10:07:30 +00:00
if (folders != null && folders.size() > 0)
Collections.sort(folders, folders.get(0).getComparator(null));
2019-01-18 11:15:07 +00:00
2019-08-13 10:07:30 +00:00
return folders;
}
2018-11-14 16:21:53 +00:00
2019-08-13 10:07:30 +00:00
@Override
protected void onExecuted(Bundle args, List<EntityFolder> folders) {
if (folders == null)
folders = new ArrayList<>();
setFolders(folders, account);
}
2018-12-01 09:47:08 +00:00
2019-08-13 10:07:30 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(getParentFragmentManager(), ex);
2019-08-13 10:07:30 +00:00
}
2019-12-16 18:13:54 +00:00
};
// Load after provider has been selected
new Handler().post(new Runnable() {
@Override
public void run() {
task.execute(FragmentAccount.this, args, "account:folders");
}
});
2019-08-13 10:07:30 +00:00
}
2018-08-02 13:33:06 +00:00
}
2018-12-01 09:47:08 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(getParentFragmentManager(), ex);
2018-12-01 09:47:08 +00:00
}
2019-01-12 13:15:15 +00:00
}.execute(this, args, "account:get");
2018-08-02 13:33:06 +00:00
}
2018-08-27 14:31:45 +00:00
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_account, 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() {
Bundle aargs = new Bundle();
aargs.putString("question", getString(R.string.title_account_delete));
FragmentDialogAsk fragment = new FragmentDialogAsk();
fragment.setArguments(aargs);
fragment.setTargetFragment(FragmentAccount.this, REQUEST_DELETE);
2019-10-12 15:16:53 +00:00
fragment.show(getParentFragmentManager(), "account:delete");
2018-08-27 14:31:45 +00:00
}
2019-06-30 14:55:15 +00:00
@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) {
case REQUEST_COLOR:
2019-07-15 12:11:55 +00:00
if (resultCode == RESULT_OK && data != null) {
2019-08-13 08:27:17 +00:00
if (ActivityBilling.isPro(getContext())) {
2019-07-15 12:11:55 +00:00
Bundle args = data.getBundleExtra("args");
2019-09-29 12:45:00 +00:00
btnColor.setColor(args.getInt("color"));
2019-07-15 12:11:55 +00:00
} else
2019-08-13 08:27:17 +00:00
startActivity(new Intent(getContext(), ActivityBilling.class));
2019-07-15 12:11:55 +00:00
}
break;
case REQUEST_SAVE:
2019-07-15 12:11:55 +00:00
if (resultCode == RESULT_OK) {
2019-12-17 11:22:29 +00:00
final boolean save = (btnSave.getVisibility() == View.VISIBLE);
2019-07-15 12:11:55 +00:00
new Handler().post(new Runnable() {
@Override
public void run() {
2019-12-17 11:22:29 +00:00
scroll.smoothScrollTo(0, (save ? btnSave : btnCheck).getBottom());
2019-07-15 12:11:55 +00:00
}
});
2019-12-17 11:22:29 +00:00
if (save)
onSave(false);
else
onCheck();
} else if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
2019-10-12 15:16:53 +00:00
getParentFragmentManager().popBackStack();
2019-07-15 12:11:55 +00:00
break;
case REQUEST_DELETE:
2019-07-15 12:11:55 +00:00
if (resultCode == RESULT_OK)
onDelete();
break;
}
} catch (Throwable ex) {
Log.e(ex);
2019-06-30 14:55:15 +00:00
}
}
private void onDelete() {
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
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.account().setAccountTbd(id);
2019-12-09 18:44:27 +00:00
ServiceSynchronize.eval(context, "delete account");
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
2019-10-12 15:16:53 +00:00
getParentFragmentManager().popBackStack();
}
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(getParentFragmentManager(), ex);
}
2019-09-11 12:03:59 +00:00
}.execute(this, args, "account:delete");
}
2019-05-15 08:46:59 +00:00
private void setFolders(List<EntityFolder> _folders, EntityAccount account) {
2019-07-17 12:43:17 +00:00
{
List<EntityFolder> folders = new ArrayList<>();
EntityFolder none = new EntityFolder();
none.id = 0L;
none.name = "-";
folders.add(none);
for (EntityFolder folder : _folders)
if (!EntityFolder.INBOX.equals(folder.type))
folders.add(folder);
adapter.clear();
adapter.addAll(folders);
for (int pos = 0; pos < folders.size(); pos++) {
EntityFolder folder = folders.get(pos);
if (EntityFolder.DRAFTS.equals(folder.type))
spDrafts.setSelection(pos);
else if (EntityFolder.SENT.equals(folder.type))
spSent.setSelection(pos);
else if (EntityFolder.ARCHIVE.equals(folder.type))
spArchive.setSelection(pos);
else if (EntityFolder.TRASH.equals(folder.type))
spTrash.setSelection(pos);
else if (EntityFolder.JUNK.equals(folder.type))
spJunk.setSelection(pos);
2019-10-23 10:51:20 +00:00
if (account != null &&
account.move_to != null && account.move_to.equals(folder.id))
spMove.setSelection(pos);
2019-07-17 12:43:17 +00:00
}
}
2019-07-17 12:43:17 +00:00
{
List<EntityFolder> folders = getFolderActions(getContext());
2019-07-17 12:43:17 +00:00
folders.addAll(_folders);
adapterSwipe.clear();
adapterSwipe.addAll(folders);
Long left = (account == null ? null : account.swipe_left);
Long right = (account == null ? null : account.swipe_right);
2019-07-17 12:43:17 +00:00
String leftDefault = EntityFolder.TRASH;
String rightDefault = EntityFolder.TRASH;
for (EntityFolder folder : folders)
if (EntityFolder.ARCHIVE.equals(folder.type)) {
rightDefault = folder.type;
break;
}
2019-01-20 15:22:21 +00:00
2019-07-17 12:43:17 +00:00
for (int pos = 0; pos < folders.size(); pos++) {
EntityFolder folder = folders.get(pos);
2019-01-20 15:22:21 +00:00
2019-07-17 12:43:17 +00:00
if (left == null ? (account == null && leftDefault.equals(folder.type)) : left.equals(folder.id))
spLeft.setSelection(pos);
if (right == null ? (account == null && rightDefault.equals(folder.type)) : right.equals(folder.id))
spRight.setSelection(pos);
}
}
2019-12-16 18:09:49 +00:00
cbIdentity.setChecked(account == null);
2019-09-30 20:53:50 +00:00
grpFolders.setVisibility(View.VISIBLE);
btnSave.setVisibility(View.VISIBLE);
2019-10-19 18:56:19 +00:00
cbIdentity.setVisibility(View.VISIBLE);
2019-12-16 18:09:49 +00:00
if (cbTrust.isChecked())
cbTrust.setVisibility(View.VISIBLE);
}
2018-09-22 10:29:35 +00:00
static List<EntityFolder> getFolderActions(Context context) {
List<EntityFolder> folders = new ArrayList<>();
EntityFolder none = new EntityFolder();
none.id = 0L;
none.name = "-";
folders.add(none);
EntityFolder ask = new EntityFolder();
ask.id = SWIPE_ACTION_ASK;
ask.name = context.getString(R.string.title_ask_what);
folders.add(ask);
EntityFolder seen = new EntityFolder();
seen.id = SWIPE_ACTION_SEEN;
seen.name = context.getString(R.string.title_seen);
folders.add(seen);
EntityFolder flag = new EntityFolder();
flag.id = SWIPE_ACTION_FLAG;
flag.name = context.getString(R.string.title_flag);
folders.add(flag);
EntityFolder snooze = new EntityFolder();
snooze.id = SWIPE_ACTION_SNOOZE;
snooze.name = context.getString(R.string.title_snooze_now);
folders.add(snooze);
EntityFolder hide = new EntityFolder();
hide.id = SWIPE_ACTION_HIDE;
hide.name = context.getString(R.string.title_hide);
folders.add(hide);
EntityFolder move = new EntityFolder();
move.id = SWIPE_ACTION_MOVE;
move.name = context.getString(R.string.title_move);
folders.add(move);
EntityFolder junk = new EntityFolder();
junk.id = SWIPE_ACTION_JUNK;
junk.name = context.getString(R.string.title_report_spam);
folders.add(junk);
EntityFolder delete = new EntityFolder();
delete.id = SWIPE_ACTION_DELETE;
delete.name = context.getString(R.string.title_delete_permanently);
folders.add(delete);
return folders;
}
2020-06-06 08:58:51 +00:00
private static class CheckResult {
2019-01-20 15:22:21 +00:00
EntityAccount account;
2018-09-22 10:29:35 +00:00
List<EntityFolder> folders;
boolean idle;
Boolean utf8;
2018-09-22 10:29:35 +00:00
}
2018-08-02 13:33:06 +00:00
}