2019-01-14 11:11:03 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
|
|
|
import android.Manifest;
|
|
|
|
import android.accounts.Account;
|
|
|
|
import android.accounts.AccountManager;
|
|
|
|
import android.accounts.AccountManagerCallback;
|
|
|
|
import android.accounts.AccountManagerFuture;
|
|
|
|
import android.accounts.AuthenticatorException;
|
|
|
|
import android.accounts.OperationCanceledException;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
2019-06-18 15:40:57 +00:00
|
|
|
import android.net.Uri;
|
2019-01-14 11:11:03 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.text.method.LinkMovementMethod;
|
|
|
|
import android.util.Patterns;
|
2019-03-03 11:16:00 +00:00
|
|
|
import android.view.KeyEvent;
|
2019-01-14 11:11:03 +00:00
|
|
|
import android.view.LayoutInflater;
|
2019-02-18 17:00:58 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2019-01-14 11:11:03 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2019-03-03 11:16:00 +00:00
|
|
|
import android.view.inputmethod.EditorInfo;
|
2019-01-14 11:11:03 +00:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.constraintlayout.widget.Group;
|
2019-05-10 16:33:23 +00:00
|
|
|
import androidx.lifecycle.Lifecycle;
|
2019-04-17 18:21:44 +00:00
|
|
|
|
2019-01-14 11:11:03 +00:00
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
|
|
|
import com.google.android.material.textfield.TextInputLayout;
|
|
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
|
|
import com.sun.mail.imap.IMAPStore;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2019-05-12 08:20:19 +00:00
|
|
|
import java.net.UnknownHostException;
|
2019-01-14 11:11:03 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import javax.mail.Folder;
|
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.Transport;
|
|
|
|
|
|
|
|
import static android.accounts.AccountManager.newChooseAccountIntent;
|
|
|
|
import static android.app.Activity.RESULT_OK;
|
2019-06-15 11:26:22 +00:00
|
|
|
import static com.google.android.material.textfield.TextInputLayout.END_ICON_NONE;
|
|
|
|
import static com.google.android.material.textfield.TextInputLayout.END_ICON_PASSWORD_TOGGLE;
|
2019-01-14 11:11:03 +00:00
|
|
|
|
2019-01-15 17:41:55 +00:00
|
|
|
public class FragmentQuickSetup extends FragmentBase {
|
2019-01-14 11:11:03 +00:00
|
|
|
private ViewGroup view;
|
|
|
|
|
|
|
|
private EditText etName;
|
|
|
|
private EditText etEmail;
|
|
|
|
private Button btnAuthorize;
|
|
|
|
private TextInputLayout tilPassword;
|
|
|
|
private Button btnCheck;
|
2019-02-18 11:39:43 +00:00
|
|
|
|
2019-01-14 11:11:03 +00:00
|
|
|
private TextView tvError;
|
|
|
|
private TextView tvInstructions;
|
|
|
|
|
2019-02-18 11:39:43 +00:00
|
|
|
private TextView tvImap;
|
|
|
|
private TextView tvSmtp;
|
|
|
|
private Button btnSave;
|
|
|
|
private Group grpSetup;
|
|
|
|
|
2019-05-12 16:41:51 +00:00
|
|
|
private int auth_type = ConnectionHelper.AUTH_TYPE_PASSWORD;
|
2019-01-14 11:11:03 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
@Nullable
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2019-05-21 12:15:43 +00:00
|
|
|
setSubtitle(R.string.title_setup_quick);
|
2019-02-18 17:00:58 +00:00
|
|
|
setHasOptionsMenu(true);
|
2019-01-14 11:11:03 +00:00
|
|
|
|
|
|
|
view = (ViewGroup) inflater.inflate(R.layout.fragment_quick_setup, container, false);
|
|
|
|
|
|
|
|
// Get controls
|
|
|
|
etName = view.findViewById(R.id.etName);
|
|
|
|
btnAuthorize = view.findViewById(R.id.btnAuthorize);
|
|
|
|
etEmail = view.findViewById(R.id.etEmail);
|
|
|
|
tilPassword = view.findViewById(R.id.tilPassword);
|
|
|
|
btnCheck = view.findViewById(R.id.btnCheck);
|
2019-02-18 11:39:43 +00:00
|
|
|
|
2019-01-14 11:11:03 +00:00
|
|
|
tvError = view.findViewById(R.id.tvError);
|
|
|
|
tvInstructions = view.findViewById(R.id.tvInstructions);
|
|
|
|
|
2019-02-18 11:39:43 +00:00
|
|
|
tvImap = view.findViewById(R.id.tvImap);
|
|
|
|
tvSmtp = view.findViewById(R.id.tvSmtp);
|
|
|
|
btnSave = view.findViewById(R.id.btnSave);
|
|
|
|
grpSetup = view.findViewById(R.id.grpSetup);
|
|
|
|
|
2019-01-14 11:11:03 +00:00
|
|
|
// Wire controls
|
|
|
|
|
|
|
|
btnAuthorize.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2019-06-18 15:40:57 +00:00
|
|
|
if (!Helper.hasValidFingerprint(getContext())) {
|
|
|
|
Snackbar snackbar = Snackbar.make(view, R.string.title_no_xoauth2, Snackbar.LENGTH_LONG);
|
|
|
|
final Intent intent = new Intent(Intent.ACTION_VIEW);
|
2019-06-18 15:42:04 +00:00
|
|
|
intent.setData(Uri.parse(Helper.FAQ_URI + "#user-content-faq109"));
|
2019-06-18 15:40:57 +00:00
|
|
|
if (intent.resolveActivity(getContext().getPackageManager()) != null)
|
|
|
|
snackbar.setAction(R.string.title_info, new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
snackbar.show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-14 11:11:03 +00:00
|
|
|
String permission = Manifest.permission.GET_ACCOUNTS;
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O &&
|
2019-02-07 09:02:40 +00:00
|
|
|
!Helper.hasPermission(getContext(), permission)) {
|
2019-01-14 11:11:03 +00:00
|
|
|
Log.i("Requesting " + permission);
|
|
|
|
requestPermissions(new String[]{permission}, ActivitySetup.REQUEST_CHOOSE_ACCOUNT);
|
|
|
|
} else
|
|
|
|
selectAccount();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-18 11:39:43 +00:00
|
|
|
etEmail.addTextChangedListener(new TextWatcher() {
|
2019-01-14 11:11:03 +00:00
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
2019-05-12 16:41:51 +00:00
|
|
|
if (auth_type != ConnectionHelper.AUTH_TYPE_PASSWORD) {
|
|
|
|
auth_type = ConnectionHelper.AUTH_TYPE_PASSWORD;
|
2019-02-18 11:39:43 +00:00
|
|
|
tilPassword.getEditText().setText(null);
|
|
|
|
tilPassword.setEnabled(true);
|
2019-06-15 11:26:22 +00:00
|
|
|
tilPassword.setEndIconMode(END_ICON_PASSWORD_TOGGLE);
|
2019-02-18 11:39:43 +00:00
|
|
|
}
|
2019-01-14 11:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
}
|
2019-02-18 11:39:43 +00:00
|
|
|
});
|
2019-01-14 11:11:03 +00:00
|
|
|
|
|
|
|
tilPassword.setHintEnabled(false);
|
|
|
|
|
2019-03-03 11:16:00 +00:00
|
|
|
tilPassword.getEditText().setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
|
|
if (actionId == EditorInfo.IME_ACTION_GO) {
|
|
|
|
onSave(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2019-01-14 11:11:03 +00:00
|
|
|
btnCheck.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2019-02-18 11:39:43 +00:00
|
|
|
onSave(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
btnSave.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
onSave(false);
|
2019-01-17 09:57:01 +00:00
|
|
|
}
|
|
|
|
});
|
2019-01-14 11:11:03 +00:00
|
|
|
|
2019-01-17 09:57:01 +00:00
|
|
|
// Initialize
|
|
|
|
tvError.setVisibility(View.GONE);
|
|
|
|
tvInstructions.setVisibility(View.GONE);
|
|
|
|
tvInstructions.setMovementMethod(LinkMovementMethod.getInstance());
|
2019-02-18 11:39:43 +00:00
|
|
|
grpSetup.setVisibility(View.GONE);
|
2019-01-14 11:11:03 +00:00
|
|
|
|
2019-01-17 09:57:01 +00:00
|
|
|
return view;
|
|
|
|
}
|
2019-01-14 11:11:03 +00:00
|
|
|
|
2019-02-18 17:00:58 +00:00
|
|
|
@Override
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
inflater.inflate(R.menu.menu_quick_setup, menu);
|
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPrepareOptionsMenu(Menu menu) {
|
|
|
|
PackageManager pm = getContext().getPackageManager();
|
|
|
|
menu.findItem(R.id.menu_help).setVisible(Helper.getIntentSetupHelp().resolveActivity(pm) != null);
|
|
|
|
super.onPrepareOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.menu_help:
|
|
|
|
onMenuHelp();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onMenuHelp() {
|
|
|
|
startActivity(Helper.getIntentSetupHelp());
|
|
|
|
}
|
|
|
|
|
2019-02-18 11:39:43 +00:00
|
|
|
private void onSave(boolean check) {
|
2019-01-17 09:57:01 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putString("name", etName.getText().toString());
|
|
|
|
args.putString("email", etEmail.getText().toString().trim());
|
|
|
|
args.putString("password", tilPassword.getEditText().getText().toString());
|
|
|
|
args.putInt("auth_type", auth_type);
|
2019-02-18 11:39:43 +00:00
|
|
|
args.putBoolean("check", check);
|
2019-01-14 11:11:03 +00:00
|
|
|
|
2019-02-18 11:39:43 +00:00
|
|
|
new SimpleTask<EmailProvider>() {
|
2019-01-17 09:57:01 +00:00
|
|
|
@Override
|
|
|
|
protected void onPreExecute(Bundle args) {
|
2019-02-18 11:39:43 +00:00
|
|
|
boolean check = args.getBoolean("check");
|
|
|
|
|
|
|
|
Helper.setViewsEnabled(view, false);
|
2019-01-17 09:57:01 +00:00
|
|
|
tvError.setVisibility(View.GONE);
|
|
|
|
tvInstructions.setVisibility(View.GONE);
|
2019-02-18 11:39:43 +00:00
|
|
|
grpSetup.setVisibility(check ? View.GONE : View.VISIBLE);
|
2019-01-17 09:57:01 +00:00
|
|
|
}
|
2019-01-14 11:11:03 +00:00
|
|
|
|
2019-01-17 09:57:01 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bundle args) {
|
2019-02-18 11:39:43 +00:00
|
|
|
Helper.setViewsEnabled(view, true);
|
2019-01-17 09:57:01 +00:00
|
|
|
}
|
2019-01-14 11:11:03 +00:00
|
|
|
|
2019-01-17 09:57:01 +00:00
|
|
|
@Override
|
2019-02-18 11:39:43 +00:00
|
|
|
protected EmailProvider onExecute(Context context, Bundle args) throws Throwable {
|
2019-01-17 09:57:01 +00:00
|
|
|
String name = args.getString("name");
|
|
|
|
String email = args.getString("email");
|
|
|
|
String password = args.getString("password");
|
|
|
|
int auth_type = args.getInt("auth_type");
|
2019-02-18 11:39:43 +00:00
|
|
|
boolean check = args.getBoolean("check");
|
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));
|
|
|
|
|
|
|
|
String[] dparts = email.split("@");
|
|
|
|
EmailProvider provider = EmailProvider.fromDomain(context, dparts[1]);
|
|
|
|
|
|
|
|
if (provider.documentation != null)
|
|
|
|
args.putString("documentation", provider.documentation.toString());
|
|
|
|
|
|
|
|
String user = (provider.user == EmailProvider.UserType.EMAIL ? email : dparts[0]);
|
|
|
|
|
|
|
|
List<EntityFolder> folders = new ArrayList<>();
|
2019-02-18 11:39:43 +00:00
|
|
|
long now = new Date().getTime();
|
2019-01-17 09:57:01 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
Properties props = MessageHelper.getSessionProperties(auth_type, null, false);
|
|
|
|
Session isession = Session.getInstance(props, null);
|
|
|
|
isession.setDebug(true);
|
2019-02-22 15:59:23 +00:00
|
|
|
try (IMAPStore istore = (IMAPStore) isession.getStore(provider.imap_starttls ? "imap" : "imaps")) {
|
2019-01-17 09:57:01 +00:00
|
|
|
istore.connect(provider.imap_host, provider.imap_port, user, password);
|
|
|
|
|
2019-02-02 07:28:14 +00:00
|
|
|
boolean inbox = false;
|
2019-01-17 09:57:01 +00:00
|
|
|
boolean drafts = false;
|
|
|
|
for (Folder ifolder : istore.getDefaultFolder().list("*")) {
|
2019-02-01 21:06:14 +00:00
|
|
|
String fullName = ifolder.getFullName();
|
2019-01-17 09:57:01 +00:00
|
|
|
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
|
2019-02-01 21:06:14 +00:00
|
|
|
String type = EntityFolder.getType(attrs, fullName);
|
|
|
|
|
|
|
|
Log.i(fullName + " attrs=" + TextUtils.join(" ", attrs) + " type=" + type);
|
2019-01-14 11:11:03 +00:00
|
|
|
|
2019-02-02 07:28:14 +00:00
|
|
|
if (type != null && !EntityFolder.USER.equals(type)) {
|
2019-01-17 09:57:01 +00:00
|
|
|
int sync = EntityFolder.SYSTEM_FOLDER_SYNC.indexOf(type);
|
|
|
|
EntityFolder folder = new EntityFolder();
|
2019-02-01 21:06:14 +00:00
|
|
|
folder.name = fullName;
|
2019-01-17 09:57:01 +00:00
|
|
|
folder.type = type;
|
|
|
|
folder.synchronize = (sync >= 0);
|
|
|
|
folder.download = (sync < 0 ? true : EntityFolder.SYSTEM_FOLDER_DOWNLOAD.get(sync));
|
|
|
|
folder.sync_days = EntityFolder.DEFAULT_SYNC;
|
|
|
|
folder.keep_days = EntityFolder.DEFAULT_KEEP;
|
|
|
|
folders.add(folder);
|
|
|
|
|
2019-02-09 09:57:54 +00:00
|
|
|
if (EntityFolder.INBOX.equals(type)) {
|
|
|
|
folder.unified = true;
|
2019-03-06 14:54:32 +00:00
|
|
|
folder.notify = true;
|
2019-02-02 07:28:14 +00:00
|
|
|
inbox = true;
|
2019-02-09 09:57:54 +00:00
|
|
|
}
|
2019-01-17 09:57:01 +00:00
|
|
|
if (EntityFolder.DRAFTS.equals(type))
|
|
|
|
drafts = true;
|
|
|
|
}
|
2019-01-14 11:11:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-02 07:28:14 +00:00
|
|
|
if (!inbox || !drafts)
|
2019-01-17 09:57:01 +00:00
|
|
|
throw new IllegalArgumentException(
|
|
|
|
context.getString(R.string.title_setup_no_settings, dparts[1]));
|
2019-01-14 11:11:03 +00:00
|
|
|
}
|
2019-01-17 09:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Properties props = MessageHelper.getSessionProperties(auth_type, null, false);
|
|
|
|
Session isession = Session.getInstance(props, null);
|
|
|
|
isession.setDebug(true);
|
2019-02-22 15:59:23 +00:00
|
|
|
try (Transport itransport = isession.getTransport(provider.smtp_starttls ? "smtp" : "smtps")) {
|
2019-01-17 09:57:01 +00:00
|
|
|
itransport.connect(provider.smtp_host, provider.smtp_port, user, password);
|
2019-01-14 11:11:03 +00:00
|
|
|
}
|
2019-01-17 09:57:01 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 11:39:43 +00:00
|
|
|
if (check)
|
|
|
|
return provider;
|
|
|
|
|
2019-01-17 09:57:01 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
EntityAccount primary = db.account().getPrimaryAccount();
|
|
|
|
|
|
|
|
// Create account
|
|
|
|
EntityAccount account = new EntityAccount();
|
|
|
|
|
|
|
|
account.auth_type = auth_type;
|
|
|
|
account.host = provider.imap_host;
|
|
|
|
account.starttls = provider.imap_starttls;
|
|
|
|
account.insecure = false;
|
|
|
|
account.port = provider.imap_port;
|
|
|
|
account.user = user;
|
2019-03-11 14:19:33 +00:00
|
|
|
account.password = password;
|
2019-01-17 09:57:01 +00:00
|
|
|
|
|
|
|
account.name = provider.name;
|
|
|
|
account.color = null;
|
|
|
|
|
|
|
|
account.synchronize = true;
|
|
|
|
account.primary = (primary == null);
|
|
|
|
account.notify = false;
|
|
|
|
account.browse = true;
|
2019-03-14 10:47:19 +00:00
|
|
|
account.poll_interval = EntityAccount.DEFAULT_KEEP_ALIVE_INTERVAL;
|
2019-01-17 09:57:01 +00:00
|
|
|
|
|
|
|
account.created = now;
|
|
|
|
account.error = null;
|
|
|
|
account.last_connected = now;
|
|
|
|
|
|
|
|
account.id = db.account().insertAccount(account);
|
2019-02-02 07:28:14 +00:00
|
|
|
EntityLog.log(context, "Quick added account=" + account.name);
|
2019-01-17 09:57:01 +00:00
|
|
|
|
|
|
|
// Create folders
|
|
|
|
for (EntityFolder folder : folders) {
|
|
|
|
folder.account = account.id;
|
|
|
|
folder.id = db.folder().insertFolder(folder);
|
2019-02-02 07:28:14 +00:00
|
|
|
EntityLog.log(context, "Quick added folder=" + folder.name + " type=" + folder.type);
|
2019-01-14 11:11:03 +00:00
|
|
|
}
|
2019-01-17 09:57:01 +00:00
|
|
|
|
2019-01-20 18:48:00 +00:00
|
|
|
// Set swipe left/right folder
|
|
|
|
for (EntityFolder folder : folders)
|
|
|
|
if (EntityFolder.TRASH.equals(folder.type))
|
|
|
|
account.swipe_left = folder.id;
|
|
|
|
else if (EntityFolder.ARCHIVE.equals(folder.type))
|
|
|
|
account.swipe_right = folder.id;
|
|
|
|
|
|
|
|
if (account.swipe_right == null && account.swipe_left != null)
|
|
|
|
account.swipe_right = account.swipe_left;
|
|
|
|
|
|
|
|
db.account().updateAccount(account);
|
|
|
|
|
2019-01-17 09:57:01 +00:00
|
|
|
// Create identity
|
|
|
|
EntityIdentity identity = new EntityIdentity();
|
|
|
|
identity.name = name;
|
|
|
|
identity.email = email;
|
|
|
|
identity.account = account.id;
|
|
|
|
|
|
|
|
identity.display = null;
|
|
|
|
identity.color = null;
|
|
|
|
identity.signature = null;
|
|
|
|
|
|
|
|
identity.auth_type = auth_type;
|
|
|
|
identity.host = provider.smtp_host;
|
|
|
|
identity.starttls = provider.smtp_starttls;
|
|
|
|
identity.insecure = false;
|
|
|
|
identity.port = provider.smtp_port;
|
|
|
|
identity.user = user;
|
2019-03-11 14:19:33 +00:00
|
|
|
identity.password = password;
|
2019-01-17 09:57:01 +00:00
|
|
|
identity.synchronize = true;
|
|
|
|
identity.primary = true;
|
|
|
|
|
|
|
|
identity.replyto = null;
|
|
|
|
identity.bcc = null;
|
|
|
|
identity.delivery_receipt = false;
|
|
|
|
identity.read_receipt = false;
|
|
|
|
identity.error = null;
|
|
|
|
|
|
|
|
identity.id = db.identity().insertIdentity(identity);
|
2019-02-02 07:28:14 +00:00
|
|
|
EntityLog.log(context, "Quick added identity=" + identity.name + " email=" + identity.email);
|
2019-01-17 09:57:01 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceSynchronize.reload(context, "quick setup");
|
|
|
|
|
|
|
|
return null;
|
2019-01-14 11:11:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 09:57:01 +00:00
|
|
|
@Override
|
2019-02-18 11:39:43 +00:00
|
|
|
protected void onExecuted(Bundle args, EmailProvider result) {
|
|
|
|
boolean check = args.getBoolean("check");
|
|
|
|
if (check) {
|
2019-02-18 12:57:56 +00:00
|
|
|
tvImap.setText(result == null ? null
|
|
|
|
: result.imap_host + ":" + result.imap_port + (result.imap_starttls ? " starttls" : " ssl"));
|
|
|
|
tvSmtp.setText(result == null ? null
|
|
|
|
: result.smtp_host + ":" + result.smtp_port + (result.smtp_starttls ? " starttls" : " ssl"));
|
2019-02-18 11:39:43 +00:00
|
|
|
grpSetup.setVisibility(result == null ? View.GONE : View.VISIBLE);
|
|
|
|
} else
|
|
|
|
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
|
|
|
|
.setMessage(R.string.title_setup_quick_success)
|
|
|
|
.setPositiveButton(android.R.string.ok, null)
|
|
|
|
.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
|
|
|
@Override
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.create()
|
|
|
|
.show();
|
2019-01-17 09:57:01 +00:00
|
|
|
}
|
2019-01-14 11:11:03 +00:00
|
|
|
|
2019-01-17 09:57:01 +00:00
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
|
|
|
if (args.containsKey("documentation")) {
|
2019-02-10 12:01:21 +00:00
|
|
|
tvInstructions.setText(HtmlHelper.fromHtml(args.getString("documentation")));
|
2019-01-17 09:57:01 +00:00
|
|
|
tvInstructions.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
2019-05-12 08:20:19 +00:00
|
|
|
if (ex instanceof IllegalArgumentException || ex instanceof UnknownHostException)
|
2019-01-17 09:57:01 +00:00
|
|
|
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
|
|
|
|
else {
|
|
|
|
tvError.setText(Helper.formatThrowable(ex));
|
|
|
|
tvError.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.execute(FragmentQuickSetup.this, args, "setup:quick");
|
2019-01-14 11:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
|
if (requestCode == ActivitySetup.REQUEST_CHOOSE_ACCOUNT)
|
|
|
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
|
|
|
|
selectAccount();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
if (requestCode == ActivitySetup.REQUEST_CHOOSE_ACCOUNT)
|
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
accountSelected(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void selectAccount() {
|
|
|
|
Log.i("Select account");
|
|
|
|
startActivityForResult(
|
|
|
|
Helper.getChooser(getContext(), newChooseAccountIntent(
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
new String[]{"com.google"},
|
|
|
|
false,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null)),
|
|
|
|
ActivitySetup.REQUEST_CHOOSE_ACCOUNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void accountSelected(Intent data) {
|
|
|
|
Log.i("Selected account");
|
|
|
|
String name = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
|
|
|
|
String type = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
|
|
|
|
|
|
|
|
AccountManager am = AccountManager.get(getContext());
|
|
|
|
Account[] accounts = am.getAccountsByType(type);
|
|
|
|
Log.i("Accounts=" + accounts.length);
|
|
|
|
for (final Account account : accounts)
|
|
|
|
if (name.equals(account.name)) {
|
|
|
|
etEmail.setEnabled(false);
|
|
|
|
tilPassword.setEnabled(false);
|
|
|
|
btnAuthorize.setEnabled(false);
|
|
|
|
btnCheck.setEnabled(false);
|
|
|
|
final Snackbar snackbar = Snackbar.make(view, R.string.title_authorizing, Snackbar.LENGTH_SHORT);
|
|
|
|
snackbar.show();
|
|
|
|
|
|
|
|
am.getAuthToken(
|
|
|
|
account,
|
2019-05-12 16:41:51 +00:00
|
|
|
ConnectionHelper.getAuthTokenType(type),
|
2019-01-14 11:11:03 +00:00
|
|
|
new Bundle(),
|
|
|
|
getActivity(),
|
|
|
|
new AccountManagerCallback<Bundle>() {
|
|
|
|
@Override
|
|
|
|
public void run(AccountManagerFuture<Bundle> future) {
|
|
|
|
try {
|
|
|
|
Bundle bundle = future.getResult();
|
|
|
|
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
|
|
|
|
Log.i("Got token");
|
|
|
|
|
|
|
|
etEmail.setText(account.name);
|
|
|
|
tilPassword.getEditText().setText(token);
|
2019-05-12 16:41:51 +00:00
|
|
|
auth_type = ConnectionHelper.AUTH_TYPE_GMAIL;
|
2019-01-14 11:11:03 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
if (ex instanceof OperationCanceledException ||
|
|
|
|
ex instanceof AuthenticatorException ||
|
2019-05-10 16:33:23 +00:00
|
|
|
ex instanceof IOException) {
|
|
|
|
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
|
|
|
|
Snackbar.make(view, Helper.formatThrowable(ex), Snackbar.LENGTH_LONG).show();
|
|
|
|
} else
|
2019-01-14 11:11:03 +00:00
|
|
|
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
|
|
|
|
} finally {
|
|
|
|
etEmail.setEnabled(true);
|
2019-05-12 16:41:51 +00:00
|
|
|
tilPassword.setEnabled(auth_type == ConnectionHelper.AUTH_TYPE_PASSWORD);
|
2019-06-15 11:26:22 +00:00
|
|
|
tilPassword.setEndIconMode(auth_type == ConnectionHelper.AUTH_TYPE_PASSWORD
|
|
|
|
? END_ICON_PASSWORD_TOGGLE : END_ICON_NONE);
|
2019-01-14 11:11:03 +00:00
|
|
|
btnAuthorize.setEnabled(true);
|
|
|
|
btnCheck.setEnabled(true);
|
|
|
|
new Handler().postDelayed(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
snackbar.dismiss();
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
null);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|