Refactoring

This commit is contained in:
M66B
2019-01-17 09:57:01 +00:00
parent bb1e697c41
commit 3b06c3d027
7 changed files with 2248 additions and 2192 deletions

View File

@@ -135,250 +135,7 @@ public class FragmentQuickSetup extends FragmentBase {
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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);
new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
etName.setEnabled(false);
etEmail.setEnabled(false);
tilPassword.setEnabled(false);
btnAuthorize.setEnabled(false);
btnCheck.setEnabled(false);
tvError.setVisibility(View.GONE);
tvInstructions.setVisibility(View.GONE);
}
@Override
protected void onPostExecute(Bundle args) {
etName.setEnabled(true);
etEmail.setEnabled(true);
tilPassword.setEnabled(true);
btnAuthorize.setEnabled(true);
btnCheck.setEnabled(true);
}
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
String name = args.getString("name");
String email = args.getString("email");
String password = args.getString("password");
int auth_type = args.getInt("auth_type");
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]);
Character separator;
long now = new Date().getTime();
List<EntityFolder> folders = new ArrayList<>();
EntityFolder inbox = new EntityFolder();
inbox.name = "INBOX";
inbox.type = EntityFolder.INBOX;
inbox.level = 0;
inbox.synchronize = true;
inbox.unified = true;
inbox.notify = true;
inbox.sync_days = EntityFolder.DEFAULT_SYNC;
inbox.keep_days = EntityFolder.DEFAULT_KEEP;
folders.add(inbox);
{
Properties props = MessageHelper.getSessionProperties(auth_type, null, false);
Session isession = Session.getInstance(props, null);
isession.setDebug(true);
IMAPStore istore = null;
try {
istore = (IMAPStore) isession.getStore(provider.imap_starttls ? "imap" : "imaps");
istore.connect(provider.imap_host, provider.imap_port, user, password);
separator = istore.getDefaultFolder().getSeparator();
boolean drafts = false;
for (Folder ifolder : istore.getDefaultFolder().list("*")) {
String type = null;
boolean selectable = true;
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
Log.i(ifolder.getFullName() + " attrs=" + TextUtils.join(" ", attrs));
for (String attr : attrs) {
if ("\\Noselect".equals(attr) || "\\NonExistent".equals(attr))
selectable = false;
if (attr.startsWith("\\")) {
int index = EntityFolder.SYSTEM_FOLDER_ATTR.indexOf(attr.substring(1));
if (index >= 0) {
type = EntityFolder.SYSTEM_FOLDER_TYPE.get(index);
break;
}
}
}
if (selectable && type != null) {
int sync = EntityFolder.SYSTEM_FOLDER_SYNC.indexOf(type);
EntityFolder folder = new EntityFolder();
folder.name = ifolder.getFullName();
folder.type = type;
folder.level = EntityFolder.getLevel(separator, folder.name);
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);
if (EntityFolder.DRAFTS.equals(type))
drafts = true;
}
}
if (!drafts)
throw new IllegalArgumentException(
context.getString(R.string.title_setup_no_settings, dparts[1]));
} finally {
if (istore != null)
istore.close();
}
}
{
Properties props = MessageHelper.getSessionProperties(auth_type, null, false);
Session isession = Session.getInstance(props, null);
isession.setDebug(true);
Transport itransport = isession.getTransport(provider.smtp_starttls ? "smtp" : "smtps");
try {
itransport.connect(provider.smtp_host, provider.smtp_port, user, password);
} finally {
itransport.close();
}
}
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;
account.password = password;
account.name = provider.name;
account.color = null;
account.synchronize = true;
account.primary = (primary == null);
account.notify = false;
account.browse = true;
account.poll_interval = 19;
account.prefix = provider.prefix;
account.created = now;
account.error = null;
account.last_connected = now;
account.id = db.account().insertAccount(account);
// Create folders
for (EntityFolder folder : folders) {
folder.account = account.id;
folder.id = db.folder().insertFolder(folder);
}
// 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;
identity.password = password;
identity.synchronize = true;
identity.primary = true;
identity.replyto = null;
identity.bcc = null;
identity.delivery_receipt = false;
identity.read_receipt = false;
identity.store_sent = false;
identity.sent_folder = null;
identity.error = null;
identity.id = db.identity().insertIdentity(identity);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.reload(context, "quick setup");
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
etName.setText(null);
etEmail.setText(null);
tilPassword.getEditText().setText(null);
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();
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (args.containsKey("documentation")) {
tvInstructions.setText(Html.fromHtml(args.getString("documentation")));
tvInstructions.setVisibility(View.VISIBLE);
}
if (ex instanceof IllegalArgumentException)
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");
onCheck();
}
});
@@ -390,6 +147,253 @@ public class FragmentQuickSetup extends FragmentBase {
return view;
}
private void onCheck() {
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);
new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
etName.setEnabled(false);
etEmail.setEnabled(false);
tilPassword.setEnabled(false);
btnAuthorize.setEnabled(false);
btnCheck.setEnabled(false);
tvError.setVisibility(View.GONE);
tvInstructions.setVisibility(View.GONE);
}
@Override
protected void onPostExecute(Bundle args) {
etName.setEnabled(true);
etEmail.setEnabled(true);
tilPassword.setEnabled(true);
btnAuthorize.setEnabled(true);
btnCheck.setEnabled(true);
}
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
String name = args.getString("name");
String email = args.getString("email");
String password = args.getString("password");
int auth_type = args.getInt("auth_type");
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]);
Character separator;
long now = new Date().getTime();
List<EntityFolder> folders = new ArrayList<>();
EntityFolder inbox = new EntityFolder();
inbox.name = "INBOX";
inbox.type = EntityFolder.INBOX;
inbox.level = 0;
inbox.synchronize = true;
inbox.unified = true;
inbox.notify = true;
inbox.sync_days = EntityFolder.DEFAULT_SYNC;
inbox.keep_days = EntityFolder.DEFAULT_KEEP;
folders.add(inbox);
{
Properties props = MessageHelper.getSessionProperties(auth_type, null, false);
Session isession = Session.getInstance(props, null);
isession.setDebug(true);
IMAPStore istore = null;
try {
istore = (IMAPStore) isession.getStore(provider.imap_starttls ? "imap" : "imaps");
istore.connect(provider.imap_host, provider.imap_port, user, password);
separator = istore.getDefaultFolder().getSeparator();
boolean drafts = false;
for (Folder ifolder : istore.getDefaultFolder().list("*")) {
String type = null;
boolean selectable = true;
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
Log.i(ifolder.getFullName() + " attrs=" + TextUtils.join(" ", attrs));
for (String attr : attrs) {
if ("\\Noselect".equals(attr) || "\\NonExistent".equals(attr))
selectable = false;
if (attr.startsWith("\\")) {
int index = EntityFolder.SYSTEM_FOLDER_ATTR.indexOf(attr.substring(1));
if (index >= 0) {
type = EntityFolder.SYSTEM_FOLDER_TYPE.get(index);
break;
}
}
}
if (selectable && type != null) {
int sync = EntityFolder.SYSTEM_FOLDER_SYNC.indexOf(type);
EntityFolder folder = new EntityFolder();
folder.name = ifolder.getFullName();
folder.type = type;
folder.level = EntityFolder.getLevel(separator, folder.name);
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);
if (EntityFolder.DRAFTS.equals(type))
drafts = true;
}
}
if (!drafts)
throw new IllegalArgumentException(
context.getString(R.string.title_setup_no_settings, dparts[1]));
} finally {
if (istore != null)
istore.close();
}
}
{
Properties props = MessageHelper.getSessionProperties(auth_type, null, false);
Session isession = Session.getInstance(props, null);
isession.setDebug(true);
Transport itransport = isession.getTransport(provider.smtp_starttls ? "smtp" : "smtps");
try {
itransport.connect(provider.smtp_host, provider.smtp_port, user, password);
} finally {
itransport.close();
}
}
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;
account.password = password;
account.name = provider.name;
account.color = null;
account.synchronize = true;
account.primary = (primary == null);
account.notify = false;
account.browse = true;
account.poll_interval = 19;
account.prefix = provider.prefix;
account.created = now;
account.error = null;
account.last_connected = now;
account.id = db.account().insertAccount(account);
// Create folders
for (EntityFolder folder : folders) {
folder.account = account.id;
folder.id = db.folder().insertFolder(folder);
}
// 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;
identity.password = password;
identity.synchronize = true;
identity.primary = true;
identity.replyto = null;
identity.bcc = null;
identity.delivery_receipt = false;
identity.read_receipt = false;
identity.store_sent = false;
identity.sent_folder = null;
identity.error = null;
identity.id = db.identity().insertIdentity(identity);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.reload(context, "quick setup");
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
etName.setText(null);
etEmail.setText(null);
tilPassword.getEditText().setText(null);
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();
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (args.containsKey("documentation")) {
tvInstructions.setText(Html.fromHtml(args.getString("documentation")));
tvInstructions.setVisibility(View.VISIBLE);
}
if (ex instanceof IllegalArgumentException)
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");
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == ActivitySetup.REQUEST_CHOOSE_ACCOUNT)