Introduced simple task

Loaders are not suitable for one shot tasks which execution needs to be guaranteed
pull/50/head
M66B 7 years ago
parent 6d525fb77b
commit 096919e307

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="EntryPointsManager">
<list size="1">
<item index="0" class="java.lang.String" itemvalue="androidx.lifecycle.OnLifecycleEvent" />
</list>
</component>
<component name="NullableNotNullManager"> <component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" /> <option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" /> <option name="myDefaultNotNull" value="android.support.annotation.NonNull" />

@ -26,10 +26,6 @@ import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
public class ActivityCompose extends ActivityBase implements FragmentManager.OnBackStackChangedListener { public class ActivityCompose extends ActivityBase implements FragmentManager.OnBackStackChangedListener {
static final int LOADER_COMPOSE_GET = 1;
static final int LOADER_COMPOSE_PUT = 2;
static final int LOADER_COMPOSE_ATTACHMENT = 3;
static final int REQUEST_CONTACT_TO = 1; static final int REQUEST_CONTACT_TO = 1;
static final int REQUEST_CONTACT_CC = 2; static final int REQUEST_CONTACT_CC = 2;
static final int REQUEST_CONTACT_BCC = 3; static final int REQUEST_CONTACT_BCC = 3;

@ -31,10 +31,6 @@ import androidx.fragment.app.FragmentTransaction;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
public class ActivitySetup extends ActivityBase implements FragmentManager.OnBackStackChangedListener { public class ActivitySetup extends ActivityBase implements FragmentManager.OnBackStackChangedListener {
static final int LOADER_CREATE_OUTBOX = 1;
static final int LOADER_DELETE_ACCOUNT = 2;
static final int LOADER_DELETE_IDENTITY = 3;
static final String ACTION_EDIT_ACCOUNT = BuildConfig.APPLICATION_ID + ".EDIT_ACCOUNT"; static final String ACTION_EDIT_ACCOUNT = BuildConfig.APPLICATION_ID + ".EDIT_ACCOUNT";
static final String ACTION_EDIT_IDENTITY = BuildConfig.APPLICATION_ID + ".EDIT_IDENTITY"; static final String ACTION_EDIT_IDENTITY = BuildConfig.APPLICATION_ID + ".EDIT_IDENTITY";

@ -66,22 +66,6 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
private ListView drawerList; private ListView drawerList;
private ActionBarDrawerToggle drawerToggle; private ActionBarDrawerToggle drawerToggle;
static final int LOADER_EXCEPTION = 1;
static final int LOADER_ACCOUNT_CHECK = 2;
static final int LOADER_ACCOUNT_PUT = 3;
static final int LOADER_IDENTITY_PUT = 4;
static final int LOADER_FOLDER_PUT = 5;
static final int LOADER_MESSAGE_ACCOUNT = 6;
static final int LOADER_MESSAGE_VIEW = 7;
static final int LOADER_MESSAGE_SEEN = 8;
static final int LOADER_MESSAGE_EDIT = 9;
static final int LOADER_MESSAGE_SPAM = 10;
static final int LOADER_MESSAGE_TRASH = 11;
static final int LOADER_MESSAGE_MOVE = 12;
static final int LOADER_MESSAGE_ARCHIVE = 13;
static final int LOADER_SEEN_UNTIL = 14;
static final int LOADER_DEBUG_INFO = 15;
static final int REQUEST_VIEW = 1; static final int REQUEST_VIEW = 1;
static final int REQUEST_UNSEEN = 2; static final int REQUEST_UNSEEN = 2;
@ -179,12 +163,12 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
fragmentTransaction.commit(); fragmentTransaction.commit();
} }
new SimpleLoader<Long>() { new SimpleTask<Long>() {
@Override @Override
public Long onLoad(Bundle args) throws Throwable { protected Long onLoad(Context context, Bundle args) throws Throwable {
File file = new File(getContext().getCacheDir(), "crash.log"); File file = new File(context.getCacheDir(), "crash.log");
if (file.exists()) { if (file.exists()) {
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
EntityFolder drafts = db.folder().getPrimaryDrafts(); EntityFolder drafts = db.folder().getPrimaryDrafts();
if (drafts != null) { if (drafts != null) {
Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode"); Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode");
@ -221,7 +205,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
draft.account = drafts.account; draft.account = drafts.account;
draft.folder = drafts.id; draft.folder = drafts.id;
draft.to = new Address[]{to}; draft.to = new Address[]{to};
draft.subject = getString(R.string.app_name) + " crash log"; draft.subject = context.getString(R.string.app_name) + " crash log";
draft.body = "<pre>" + sb.toString().replaceAll("\\r?\\n", "<br />") + "</pre>"; draft.body = "<pre>" + sb.toString().replaceAll("\\r?\\n", "<br />") + "</pre>";
draft.received = new Date().getTime(); draft.received = new Date().getTime();
draft.seen = false; draft.seen = false;
@ -239,7 +223,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
} }
@Override @Override
public void onLoaded(Bundle args, Long id) { protected void onLoaded(Bundle args, Long id) {
if (id != null) if (id != null)
startActivity( startActivity(
new Intent(ActivityView.this, ActivityCompose.class) new Intent(ActivityView.this, ActivityCompose.class)
@ -247,7 +231,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
.putExtra("id", id)); .putExtra("id", id));
} }
}.load(this, LOADER_EXCEPTION, new Bundle()); }.load(this, new Bundle());
checkIntent(getIntent()); checkIntent(getIntent());
} }
@ -339,11 +323,11 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("time", new Date().getTime()); args.putLong("time", new Date().getTime());
new SimpleLoader<Void>() { new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) { protected Void onLoad(Context context, Bundle args) {
long time = args.getLong("time"); long time = args.getLong("time");
DaoAccount dao = DB.getInstance(getContext()).account(); DaoAccount dao = DB.getInstance(context).account();
for (EntityAccount account : dao.getAccounts(true)) { for (EntityAccount account : dao.getAccounts(true)) {
account.seen_until = time; account.seen_until = time;
dao.updateAccount(account); dao.updateAccount(account);
@ -352,10 +336,10 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Toast.makeText(ActivityView.this, ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(ActivityView.this, ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, LOADER_SEEN_UNTIL, args); }.load(this, args);
} }
} }
@ -461,11 +445,11 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
} else if (ACTION_VIEW_MESSAGE.equals(intent.getAction())) { } else if (ACTION_VIEW_MESSAGE.equals(intent.getAction())) {
new SimpleLoader<Void>() { new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) { protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
DB db = DB.getInstance(ActivityView.this); DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id); EntityMessage message = db.message().getMessage(id);
EntityFolder folder = db.folder().getFolder(message.folder); EntityFolder folder = db.folder().getFolder(message.folder);
if (!EntityFolder.OUTBOX.equals(folder.type) && if (!EntityFolder.OUTBOX.equals(folder.type) &&
@ -485,7 +469,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
db.endTransaction(); db.endTransaction();
} }
EntityOperation.process(ActivityView.this); EntityOperation.process(context);
} }
} }
@ -493,7 +477,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
} }
@Override @Override
public void onLoaded(Bundle args, Void result) { protected void onLoaded(Bundle args, Void result) {
FragmentMessage fragment = new FragmentMessage(); FragmentMessage fragment = new FragmentMessage();
fragment.setArguments(args); fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
@ -502,10 +486,10 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Toast.makeText(ActivityView.this, ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(ActivityView.this, ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(ActivityView.this, LOADER_MESSAGE_VIEW, intent.getExtras()); }.load(ActivityView.this, intent.getExtras());
} else if (ACTION_EDIT_FOLDER.equals(intent.getAction())) { } else if (ACTION_EDIT_FOLDER.equals(intent.getAction())) {
FragmentFolder fragment = new FragmentFolder(); FragmentFolder fragment = new FragmentFolder();

@ -92,6 +92,9 @@ public interface DaoMessage {
" WHERE message.id = :id") " WHERE message.id = :id")
LiveData<TupleMessageEx> liveMessage(long id); LiveData<TupleMessageEx> liveMessage(long id);
@Query("SELECT * FROM message WHERE msgid = :msgid")
LiveData<EntityMessage> liveMessageByMsgId(String msgid);
@Query("SELECT uid FROM message WHERE folder = :folder AND received >= :received AND NOT uid IS NULL") @Query("SELECT uid FROM message WHERE folder = :folder AND received >= :received AND NOT uid IS NULL")
List<Long> getUids(long folder, long received); List<Long> getUids(long folder, long received);

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B) Copyright 2018 by Marcel Bokhorst (M66B)
*/ */
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -54,17 +55,17 @@ public class FragmentAbout extends FragmentEx {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
btnDebugInfo.setEnabled(false); btnDebugInfo.setEnabled(false);
new SimpleLoader<Long>() { new SimpleTask<Long>() {
@Override @Override
public Long onLoad(Bundle args) throws UnsupportedEncodingException { protected Long onLoad(Context context, Bundle args) throws UnsupportedEncodingException {
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
EntityFolder drafts = db.folder().getPrimaryDrafts(); EntityFolder drafts = db.folder().getPrimaryDrafts();
if (drafts == null) if (drafts == null)
throw new IllegalArgumentException(getString(R.string.title_no_drafts)); throw new IllegalArgumentException(context.getString(R.string.title_no_drafts));
StringBuilder info = Helper.getDebugInfo(); StringBuilder info = Helper.getDebugInfo();
info.insert(0, getString(R.string.title_debug_info_remark) + "\n\n\n\n"); info.insert(0, context.getString(R.string.title_debug_info_remark) + "\n\n\n\n");
Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode"); Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode");
@ -84,7 +85,7 @@ public class FragmentAbout extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Long id) { protected void onLoaded(Bundle args, Long id) {
btnDebugInfo.setEnabled(true); btnDebugInfo.setEnabled(true);
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "edit") .putExtra("action", "edit")
@ -92,11 +93,11 @@ public class FragmentAbout extends FragmentEx {
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
btnDebugInfo.setEnabled(true); btnDebugInfo.setEnabled(true);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentAbout.this, ActivityView.LOADER_DEBUG_INFO, new Bundle()); }.load(FragmentAbout.this, new Bundle());
} }
}); });

@ -61,9 +61,6 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
public class FragmentAccount extends FragmentEx { public class FragmentAccount extends FragmentEx {
private List<Provider> providers; private List<Provider> providers;
@ -176,8 +173,159 @@ public class FragmentAccount extends FragmentEx {
args.putBoolean("synchronize", cbSynchronize.isChecked()); args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("primary", cbPrimary.isChecked()); args.putBoolean("primary", cbPrimary.isChecked());
LoaderManager.getInstance(FragmentAccount.this) new SimpleTask<List<EntityFolder>>() {
.restartLoader(ActivityView.LOADER_ACCOUNT_CHECK, args, checkLoaderCallbacks).forceLoad(); @Override
protected List<EntityFolder> onLoad(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
String host = args.getString("host");
String port = args.getString("port");
String user = args.getString("user");
String password = args.getString("password");
if (TextUtils.isEmpty(host))
throw new Throwable(getContext().getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
throw new Throwable(getContext().getString(R.string.title_no_port));
if (TextUtils.isEmpty(user))
throw new Throwable(getContext().getString(R.string.title_no_user));
if (TextUtils.isEmpty(password))
throw new Throwable(getContext().getString(R.string.title_no_password));
// Check IMAP server / get folders
DB db = DB.getInstance(getContext());
List<EntityFolder> folders = new ArrayList<>();
Session isession = Session.getInstance(MessageHelper.getSessionProperties(), null);
IMAPStore istore = null;
try {
istore = (IMAPStore) isession.getStore("imaps");
istore.connect(host, Integer.parseInt(port), user, password);
if (!istore.hasCapability("IDLE"))
throw new MessagingException(getContext().getString(R.string.title_no_idle));
for (Folder ifolder : istore.getDefaultFolder().list("*")) {
String type = null;
// First check folder attributes
boolean selectable = true;
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
for (String attr : attrs) {
if ("\\Noselect".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) {
// Next check folder full name
if (type == null) {
String fullname = ifolder.getFullName();
for (String attr : EntityFolder.SYSTEM_FOLDER_ATTR)
if (attr.equals(fullname)) {
int index = EntityFolder.SYSTEM_FOLDER_ATTR.indexOf(attr);
type = EntityFolder.SYSTEM_FOLDER_TYPE.get(index);
break;
}
}
// Create entry
EntityFolder folder = db.folder().getFolderByName(id, ifolder.getFullName());
if (folder == null) {
folder = new EntityFolder();
folder.name = ifolder.getFullName();
folder.type = (type == null ? EntityFolder.USER : type);
folder.synchronize = (type != null && EntityFolder.SYSTEM_FOLDER_SYNC.contains(type));
folder.after = (type == null ? EntityFolder.DEFAULT_USER_SYNC : EntityFolder.DEFAULT_SYSTEM_SYNC);
}
folders.add(folder);
Log.i(Helper.TAG, folder.name + " id=" + folder.id +
" type=" + folder.type + " attr=" + TextUtils.join(",", attrs));
}
}
} finally {
if (istore != null)
istore.close();
}
return folders;
}
@Override
protected void onLoaded(Bundle args, List<EntityFolder> folders) {
Helper.setViewsEnabled(view, true);
btnCheck.setEnabled(true);
pbCheck.setVisibility(View.GONE);
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(folders, new Comparator<EntityFolder>() {
@Override
public int compare(EntityFolder f1, EntityFolder f2) {
int s = Integer.compare(
EntityFolder.FOLDER_SORT_ORDER.indexOf(f1.type),
EntityFolder.FOLDER_SORT_ORDER.indexOf(f2.type));
if (s != 0)
return s;
int c = -f1.synchronize.compareTo(f2.synchronize);
if (c != 0)
return c;
return collator.compare(
f1.name == null ? "" : f1.name,
f2.name == null ? "" : f2.name);
}
});
EntityFolder none = new EntityFolder();
none.name = "";
folders.add(0, none);
ArrayAdapter<EntityFolder> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, folders);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spDrafts.setAdapter(adapter);
spSent.setAdapter(adapter);
spAll.setAdapter(adapter);
spTrash.setAdapter(adapter);
spJunk.setAdapter(adapter);
for (int pos = 0; pos < folders.size(); pos++) {
if (EntityFolder.DRAFTS.equals(folders.get(pos).type))
spDrafts.setSelection(pos);
else if (EntityFolder.SENT.equals(folders.get(pos).type))
spSent.setSelection(pos);
else if (EntityFolder.ARCHIVE.equals(folders.get(pos).type))
spAll.setSelection(pos);
else if (EntityFolder.TRASH.equals(folders.get(pos).type))
spTrash.setSelection(pos);
else if (EntityFolder.JUNK.equals(folders.get(pos).type))
spJunk.setSelection(pos);
}
grpFolders.setVisibility(View.VISIBLE);
btnSave.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() {
@Override
public void run() {
((ScrollView) view).smoothScrollTo(0, btnSave.getBottom());
}
});
}
@Override
protected void onException(Bundle args, Throwable ex) {
grpFolders.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
Toast.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}.load(FragmentAccount.this, args);
} }
}); });
@ -221,8 +369,152 @@ public class FragmentAccount extends FragmentEx {
args.putSerializable("trash", trash); args.putSerializable("trash", trash);
args.putSerializable("junk", junk); args.putSerializable("junk", junk);
LoaderManager.getInstance(FragmentAccount.this) new SimpleTask<Void>() {
.restartLoader(ActivityView.LOADER_ACCOUNT_PUT, args, putLoaderCallbacks).forceLoad(); @Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
try {
ServiceSynchronize.stop(getContext(), "folder");
String name = args.getString("name");
String host = args.getString("host");
String port = args.getString("port");
String user = args.getString("user");
String password = args.getString("password");
boolean synchronize = args.getBoolean("synchronize");
EntityFolder drafts = (EntityFolder) args.getSerializable("drafts");
EntityFolder sent = (EntityFolder) args.getSerializable("sent");
EntityFolder all = (EntityFolder) args.getSerializable("all");
EntityFolder trash = (EntityFolder) args.getSerializable("trash");
EntityFolder junk = (EntityFolder) args.getSerializable("junk");
if (TextUtils.isEmpty(host))
throw new Throwable(getContext().getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
throw new Throwable(getContext().getString(R.string.title_no_port));
if (TextUtils.isEmpty(user))
throw new Throwable(getContext().getString(R.string.title_no_user));
if (TextUtils.isEmpty(password))
throw new Throwable(getContext().getString(R.string.title_no_password));
if (drafts == null)
throw new Throwable(getContext().getString(R.string.title_no_drafts));
// Check IMAP server
Session isession = Session.getInstance(MessageHelper.getSessionProperties(), null);
IMAPStore istore = null;
try {
istore = (IMAPStore) isession.getStore("imaps");
istore.connect(host, Integer.parseInt(port), user, password);
if (!istore.hasCapability("IDLE"))
throw new MessagingException(getContext().getString(R.string.title_no_idle));
} finally {
if (istore != null)
istore.close();
}
if (TextUtils.isEmpty(name))
name = host + "/" + user;
DB db = DB.getInstance(getContext());
EntityAccount account = db.account().getAccount(args.getLong("id"));
boolean update = (account != null);
if (account == null)
account = new EntityAccount();
account.name = name;
account.host = host;
account.port = Integer.parseInt(port);
account.user = user;
account.password = password;
account.synchronize = synchronize;
account.primary = (account.synchronize && args.getBoolean("primary"));
// On disabling synchronization mark message seen until now
if (!account.synchronize && account.synchronize != synchronize)
account.seen_until = new Date().getTime();
try {
db.beginTransaction();
if (account.primary)
db.account().resetPrimary();
if (update)
db.account().updateAccount(account);
else
account.id = db.account().insertAccount(account);
List<EntityFolder> folders = new ArrayList<>();
EntityFolder inbox = new EntityFolder();
inbox.name = "INBOX";
inbox.type = EntityFolder.INBOX;
inbox.synchronize = true;
inbox.after = EntityFolder.DEFAULT_INBOX_SYNC;
folders.add(inbox);
drafts.type = EntityFolder.DRAFTS;
folders.add(drafts);
if (sent != null) {
sent.type = EntityFolder.SENT;
folders.add(sent);
}
if (all != null) {
all.type = EntityFolder.ARCHIVE;
folders.add(all);
}
if (trash != null) {
trash.type = EntityFolder.TRASH;
folders.add(trash);
}
if (junk != null) {
junk.type = EntityFolder.JUNK;
folders.add(junk);
}
int count = db.folder().deleteSystemFolders(account.id);
Log.w(Helper.TAG, "Deleted system folders count=" + count);
for (EntityFolder folder : folders) {
EntityFolder existing = db.folder().getFolderByName(account.id, folder.name);
if (existing == null) {
folder.account = account.id;
Log.i(Helper.TAG, "Creating folder=" + folder.name + " (" + folder.type + ")");
folder.id = db.folder().insertFolder(folder);
} else {
existing.type = folder.type;
db.folder().updateFolder(existing);
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
} finally {
ServiceSynchronize.restart(getContext(), "account");
}
}
@Override
protected void onLoaded(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.setViewsEnabled(view, true);
btnCheck.setEnabled(true);
btnSave.setEnabled(true);
pbSave.setVisibility(View.GONE);
Toast.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}.load(FragmentAccount.this, args);
} }
}); });
@ -235,24 +527,28 @@ public class FragmentAccount extends FragmentEx {
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
// TODO: spinner
getFragmentManager().popBackStack(); getFragmentManager().popBackStack();
// TODO: spinner Bundle args = new Bundle();
new SimpleLoader<Void>() { args.putLong("id", id);
new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) { protected Void onLoad(Context context, Bundle args) {
// To prevent foreign key constraints from triggering // To prevent foreign key constraints from triggering
ServiceSynchronize.stop(getContext(), "delete account"); long id = args.getLong("id");
DB.getInstance(getContext()).account().deleteAccount(id); ServiceSynchronize.stop(context, "delete account");
ServiceSynchronize.start(getContext()); DB.getInstance(context).account().deleteAccount(id);
ServiceSynchronize.start(context);
return null; return null;
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentAccount.this, ActivitySetup.LOADER_DELETE_ACCOUNT, new Bundle()); }.load(FragmentAccount.this, args);
} }
}) })
.setNegativeButton(android.R.string.cancel, null).show(); .setNegativeButton(android.R.string.cancel, null).show();
@ -302,381 +598,4 @@ public class FragmentAccount extends FragmentEx {
} }
}); });
} }
private static class CheckData {
Throwable ex;
List<EntityFolder> folders;
}
private static class CheckLoader extends AsyncTaskLoader<CheckData> {
private Bundle args;
CheckLoader(Context context) {
super(context);
}
void setArgs(Bundle args) {
this.args = args;
}
protected void onStartLoading() {
forceLoad();
}
@Override
public CheckData loadInBackground() {
CheckData result = new CheckData();
try {
long id = args.getLong("id");
String host = args.getString("host");
String port = args.getString("port");
String user = args.getString("user");
String password = args.getString("password");
if (TextUtils.isEmpty(host))
throw new Throwable(getContext().getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
throw new Throwable(getContext().getString(R.string.title_no_port));
if (TextUtils.isEmpty(user))
throw new Throwable(getContext().getString(R.string.title_no_user));
if (TextUtils.isEmpty(password))
throw new Throwable(getContext().getString(R.string.title_no_password));
// Check IMAP server / get folders
DB db = DB.getInstance(getContext());
List<EntityFolder> folders = new ArrayList<>();
Session isession = Session.getInstance(MessageHelper.getSessionProperties(), null);
IMAPStore istore = null;
try {
istore = (IMAPStore) isession.getStore("imaps");
istore.connect(host, Integer.parseInt(port), user, password);
if (!istore.hasCapability("IDLE"))
throw new MessagingException(getContext().getString(R.string.title_no_idle));
for (Folder ifolder : istore.getDefaultFolder().list("*")) {
String type = null;
// First check folder attributes
boolean selectable = true;
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
for (String attr : attrs) {
if ("\\Noselect".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) {
// Next check folder full name
if (type == null) {
String fullname = ifolder.getFullName();
for (String attr : EntityFolder.SYSTEM_FOLDER_ATTR)
if (attr.equals(fullname)) {
int index = EntityFolder.SYSTEM_FOLDER_ATTR.indexOf(attr);
type = EntityFolder.SYSTEM_FOLDER_TYPE.get(index);
break;
}
}
// Create entry
EntityFolder folder = db.folder().getFolderByName(id, ifolder.getFullName());
if (folder == null) {
folder = new EntityFolder();
folder.name = ifolder.getFullName();
folder.type = (type == null ? EntityFolder.USER : type);
folder.synchronize = (type != null && EntityFolder.SYSTEM_FOLDER_SYNC.contains(type));
folder.after = (type == null ? EntityFolder.DEFAULT_USER_SYNC : EntityFolder.DEFAULT_SYSTEM_SYNC);
}
folders.add(folder);
Log.i(Helper.TAG, folder.name + " id=" + folder.id +
" type=" + folder.type + " attr=" + TextUtils.join(",", attrs));
}
}
} finally {
if (istore != null)
istore.close();
}
result.folders = folders;
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
result.ex = ex;
}
return result;
}
}
private LoaderManager.LoaderCallbacks checkLoaderCallbacks = new LoaderManager.LoaderCallbacks<CheckData>() {
@NonNull
@Override
public Loader<CheckData> onCreateLoader(int id, Bundle args) {
CheckLoader loader = new CheckLoader(getContext());
loader.setArgs(args);
return loader;
}
@Override
public void onLoadFinished(@NonNull Loader<CheckData> loader, CheckData data) {
LoaderManager.getInstance(FragmentAccount.this).destroyLoader(loader.getId());
Helper.setViewsEnabled(view, true);
btnCheck.setEnabled(true);
pbCheck.setVisibility(View.GONE);
if (data.ex == null) {
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(data.folders, new Comparator<EntityFolder>() {
@Override
public int compare(EntityFolder f1, EntityFolder f2) {
int s = Integer.compare(
EntityFolder.FOLDER_SORT_ORDER.indexOf(f1.type),
EntityFolder.FOLDER_SORT_ORDER.indexOf(f2.type));
if (s != 0)
return s;
int c = -f1.synchronize.compareTo(f2.synchronize);
if (c != 0)
return c;
return collator.compare(
f1.name == null ? "" : f1.name,
f2.name == null ? "" : f2.name);
}
});
EntityFolder none = new EntityFolder();
none.name = "";
data.folders.add(0, none);
ArrayAdapter<EntityFolder> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, data.folders);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spDrafts.setAdapter(adapter);
spSent.setAdapter(adapter);
spAll.setAdapter(adapter);
spTrash.setAdapter(adapter);
spJunk.setAdapter(adapter);
for (int pos = 0; pos < data.folders.size(); pos++) {
if (EntityFolder.DRAFTS.equals(data.folders.get(pos).type))
spDrafts.setSelection(pos);
else if (EntityFolder.SENT.equals(data.folders.get(pos).type))
spSent.setSelection(pos);
else if (EntityFolder.ARCHIVE.equals(data.folders.get(pos).type))
spAll.setSelection(pos);
else if (EntityFolder.TRASH.equals(data.folders.get(pos).type))
spTrash.setSelection(pos);
else if (EntityFolder.JUNK.equals(data.folders.get(pos).type))
spJunk.setSelection(pos);
}
grpFolders.setVisibility(View.VISIBLE);
btnSave.setVisibility(View.VISIBLE);
new Handler().post(new Runnable() {
@Override
public void run() {
((ScrollView) view).smoothScrollTo(0, btnSave.getBottom());
}
});
} else {
Log.w(Helper.TAG, data.ex + "\n" + Log.getStackTraceString(data.ex));
Toast.makeText(getContext(), Helper.formatThrowable(data.ex), Toast.LENGTH_LONG).show();
grpFolders.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
}
}
@Override
public void onLoaderReset(@NonNull Loader<CheckData> loader) {
}
};
private static class PutLoader extends AsyncTaskLoader<Throwable> {
private Bundle args;
PutLoader(Context context) {
super(context);
}
void setArgs(Bundle args) {
this.args = args;
}
protected void onStartLoading() {
forceLoad();
}
@Override
public Throwable loadInBackground() {
try {
ServiceSynchronize.stop(getContext(), "folder");
String name = args.getString("name");
String host = args.getString("host");
String port = args.getString("port");
String user = args.getString("user");
String password = args.getString("password");
boolean synchronize = args.getBoolean("synchronize");
EntityFolder drafts = (EntityFolder) args.getSerializable("drafts");
EntityFolder sent = (EntityFolder) args.getSerializable("sent");
EntityFolder all = (EntityFolder) args.getSerializable("all");
EntityFolder trash = (EntityFolder) args.getSerializable("trash");
EntityFolder junk = (EntityFolder) args.getSerializable("junk");
if (TextUtils.isEmpty(host))
throw new Throwable(getContext().getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
throw new Throwable(getContext().getString(R.string.title_no_port));
if (TextUtils.isEmpty(user))
throw new Throwable(getContext().getString(R.string.title_no_user));
if (TextUtils.isEmpty(password))
throw new Throwable(getContext().getString(R.string.title_no_password));
if (drafts == null)
throw new Throwable(getContext().getString(R.string.title_no_drafts));
// Check IMAP server
Session isession = Session.getInstance(MessageHelper.getSessionProperties(), null);
IMAPStore istore = null;
try {
istore = (IMAPStore) isession.getStore("imaps");
istore.connect(host, Integer.parseInt(port), user, password);
if (!istore.hasCapability("IDLE"))
throw new MessagingException(getContext().getString(R.string.title_no_idle));
} finally {
if (istore != null)
istore.close();
}
if (TextUtils.isEmpty(name))
name = host + "/" + user;
DB db = DB.getInstance(getContext());
EntityAccount account = db.account().getAccount(args.getLong("id"));
boolean update = (account != null);
if (account == null)
account = new EntityAccount();
account.name = name;
account.host = host;
account.port = Integer.parseInt(port);
account.user = user;
account.password = password;
account.synchronize = synchronize;
account.primary = (account.synchronize && args.getBoolean("primary"));
// On disabling synchronization mark message seen until now
if (!account.synchronize && account.synchronize != synchronize)
account.seen_until = new Date().getTime();
try {
db.beginTransaction();
if (account.primary)
db.account().resetPrimary();
if (update)
db.account().updateAccount(account);
else
account.id = db.account().insertAccount(account);
List<EntityFolder> folders = new ArrayList<>();
EntityFolder inbox = new EntityFolder();
inbox.name = "INBOX";
inbox.type = EntityFolder.INBOX;
inbox.synchronize = true;
inbox.after = EntityFolder.DEFAULT_INBOX_SYNC;
folders.add(inbox);
if (drafts != null) {
drafts.type = EntityFolder.DRAFTS;
folders.add(drafts);
}
if (sent != null) {
sent.type = EntityFolder.SENT;
folders.add(sent);
}
if (all != null) {
all.type = EntityFolder.ARCHIVE;
folders.add(all);
}
if (trash != null) {
trash.type = EntityFolder.TRASH;
folders.add(trash);
}
if (junk != null) {
junk.type = EntityFolder.JUNK;
folders.add(junk);
}
int count = db.folder().deleteSystemFolders(account.id);
Log.w(Helper.TAG, "Deleted system folders count=" + count);
for (EntityFolder folder : folders) {
EntityFolder existing = db.folder().getFolderByName(account.id, folder.name);
if (existing == null) {
folder.account = account.id;
Log.i(Helper.TAG, "Creating folder=" + folder.name + " (" + folder.type + ")");
folder.id = db.folder().insertFolder(folder);
} else {
existing.type = folder.type;
db.folder().updateFolder(existing);
}
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
return ex;
} finally {
ServiceSynchronize.restart(getContext(), "account");
}
}
}
private LoaderManager.LoaderCallbacks putLoaderCallbacks = new LoaderManager.LoaderCallbacks<Throwable>() {
@NonNull
@Override
public Loader<Throwable> onCreateLoader(int id, Bundle args) {
PutLoader loader = new PutLoader(getContext());
loader.setArgs(args);
return loader;
}
@Override
public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) {
LoaderManager.getInstance(FragmentAccount.this).destroyLoader(loader.getId());
Helper.setViewsEnabled(view, true);
btnCheck.setEnabled(true);
btnSave.setEnabled(true);
pbSave.setVisibility(View.GONE);
if (ex == null)
getFragmentManager().popBackStack();
else {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
Toast.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}
@Override
public void onLoaderReset(@NonNull Loader<Throwable> loader) {
}
};
} }

@ -20,6 +20,7 @@ package eu.faircode.email;
*/ */
import android.Manifest; import android.Manifest;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
@ -96,43 +97,13 @@ public class FragmentCompose extends FragmentEx {
private AdapterAttachment adapter; private AdapterAttachment adapter;
private boolean attaching = false; private EntityMessage draft = null;
private String action = null;
private long id = -1; // draft id
private long account = -1;
private long reference = -1;
private static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes private static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes
@Override
public void onSaveInstanceState(Bundle outState) {
Log.i(Helper.TAG, "Saving state");
outState.putString("action", action);
outState.putLong("id", id);
outState.putLong("account", account);
outState.putLong("reference", reference);
super.onSaveInstanceState(outState);
}
@Override @Override
@Nullable @Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Get arguments
if (savedInstanceState == null) {
if (action == null) {
action = getArguments().getString("action");
id = getArguments().getLong("id", -1);
account = getArguments().getLong("account", -1);
reference = getArguments().getLong("reference", -1);
}
} else {
Log.i(Helper.TAG, "Restoring state");
action = savedInstanceState.getString("action");
id = savedInstanceState.getLong("id", -1);
account = savedInstanceState.getLong("account", -1);
reference = savedInstanceState.getLong("reference", -1);
}
setSubtitle(R.string.title_compose); setSubtitle(R.string.title_compose);
view = (ViewGroup) inflater.inflate(R.layout.fragment_compose, container, false); view = (ViewGroup) inflater.inflate(R.layout.fragment_compose, container, false);
@ -278,49 +249,19 @@ public class FragmentCompose extends FragmentEx {
public void onActivityCreated(@Nullable Bundle savedInstanceState) { public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
DB.getInstance(getContext()).identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() { if (draft == null) {
@Override Bundle args = new Bundle();
public void onChanged(@Nullable final List<EntityIdentity> identities) { args.putString("action", getArguments().getString("action"));
Log.i(Helper.TAG, "Set identities=" + identities.size()); args.putLong("id", getArguments().getLong("id", -1));
args.putLong("account", getArguments().getLong("account", -1));
// Sort identities args.putLong("reference", getArguments().getLong("reference", -1));
Collections.sort(identities, new Comparator<EntityIdentity>() { draftLoader.load(FragmentCompose.this, args);
@Override }
public int compare(EntityIdentity i1, EntityIdentity i2) {
return i1.name.compareTo(i2.name);
}
});
// Show identities
ArrayAdapter<EntityIdentity> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, identities);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spFrom.setAdapter(adapter);
// Select primary identity
for (int pos = 0; pos < identities.size(); pos++)
if (identities.get(pos).primary) {
spFrom.setSelection(pos);
break;
}
spFrom.setVisibility(View.VISIBLE);
ivIdentityAdd.setVisibility(View.VISIBLE);
// Get draft, might select another identity
Bundle args = new Bundle();
args.putString("action", action);
args.putLong("id", id);
args.putLong("account", account);
args.putLong("reference", reference);
getLoader.load(FragmentCompose.this, ActivityCompose.LOADER_COMPOSE_GET, args);
}
});
} }
@Override @Override
public void onPause() { public void onPause() {
if (!attaching) onAction(R.id.action_save);
onAction(R.id.action_save);
super.onPause(); super.onPause();
} }
@ -333,7 +274,8 @@ public class FragmentCompose extends FragmentEx {
@Override @Override
public void onPrepareOptionsMenu(Menu menu) { public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu); super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_attachment).setEnabled(id >= 0); menu.findItem(R.id.menu_attachment).setVisible(draft != null);
menu.findItem(R.id.menu_addresses).setVisible(draft != null);
} }
@Override @Override
@ -351,7 +293,6 @@ public class FragmentCompose extends FragmentEx {
} }
private void onMenuAttachment() { private void onMenuAttachment() {
attaching = true;
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE); intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*"); intent.setType("*/*");
@ -420,23 +361,23 @@ public class FragmentCompose extends FragmentEx {
private void handleAddAttachment(Intent data) { private void handleAddAttachment(Intent data) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putString("msgid", draft.msgid);
args.putParcelable("uri", data.getData()); args.putParcelable("uri", data.getData());
new SimpleLoader<Void>() { new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) throws IOException { protected Void onLoad(Context context, Bundle args) throws IOException {
Cursor cursor = null; Cursor cursor = null;
try { try {
Uri uri = args.getParcelable("uri"); Uri uri = args.getParcelable("uri");
cursor = getContext().getContentResolver().query(uri, null, null, null, null, null); cursor = context.getContentResolver().query(uri, null, null, null, null, null);
if (cursor == null || !cursor.moveToFirst()) if (cursor == null || !cursor.moveToFirst())
return null; return null;
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
long id = args.getLong("id"); String msgid = args.getString("msgid");
EntityMessage draft = db.message().getMessage(id); EntityMessage draft = db.message().getMessageByMsgId(msgid);
EntityAttachment attachment = new EntityAttachment(); EntityAttachment attachment = new EntityAttachment();
attachment.message = draft.id; attachment.message = draft.id;
@ -458,7 +399,7 @@ public class FragmentCompose extends FragmentEx {
InputStream is = null; InputStream is = null;
try { try {
is = getContext().getContentResolver().openInputStream(uri); is = context.getContentResolver().openInputStream(uri);
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
int len; int len;
@ -490,24 +431,24 @@ public class FragmentCompose extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Void data) { protected void onLoaded(Bundle args, Void data) {
attaching = false;
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, ActivityCompose.LOADER_COMPOSE_ATTACHMENT, args); }.load(this, args);
} }
private void onAction(int action) { private void onAction(int action) {
Helper.setViewsEnabled(view, false);
bottom_navigation.getMenu().setGroupEnabled(0, false); bottom_navigation.getMenu().setGroupEnabled(0, false);
EntityIdentity identity = (EntityIdentity) spFrom.getSelectedItem(); EntityIdentity identity = (EntityIdentity) spFrom.getSelectedItem();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putString("msgid", draft.msgid);
args.putInt("action", action); args.putInt("action", action);
args.putLong("identity", identity == null ? -1 : identity.id); args.putLong("identity", identity == null ? -1 : identity.id);
args.putString("to", etTo.getText().toString()); args.putString("to", etTo.getText().toString());
@ -516,19 +457,21 @@ public class FragmentCompose extends FragmentEx {
args.putString("subject", etSubject.getText().toString()); args.putString("subject", etSubject.getText().toString());
args.putString("body", etBody.getText().toString()); args.putString("body", etBody.getText().toString());
putLoader.load(this, ActivityCompose.LOADER_COMPOSE_PUT, args); Log.i(Helper.TAG, "Run load id=" + draft.id + " msgid=" + draft.msgid);
actionLoader.load(this, args);
} }
private SimpleLoader<EntityMessage> getLoader = new SimpleLoader<EntityMessage>() { private SimpleTask<EntityMessage> draftLoader = new SimpleTask<EntityMessage>() {
@Override @Override
public EntityMessage onLoad(Bundle args) { protected EntityMessage onLoad(Context context, Bundle args) {
String action = args.getString("action"); String action = args.getString("action");
long id = args.getLong("id", -1); long id = args.getLong("id", -1);
long account = args.getLong("account", -1); long account = args.getLong("account", -1);
long reference = args.getLong("reference", -1); long reference = args.getLong("reference", -1);
Log.i(Helper.TAG, "Get load action=" + action + " id=" + id + " account=" + account + " reference=" + reference);
DB db = DB.getInstance(getContext()); Log.i(Helper.TAG, "Load draft action=" + action + " id=" + id + " account=" + account + " reference=" + reference);
DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessage(id); EntityMessage draft = db.message().getMessage(id);
if (draft == null) { if (draft == null) {
@ -550,6 +493,7 @@ public class FragmentCompose extends FragmentEx {
draft = new EntityMessage(); draft = new EntityMessage();
draft.account = account; draft.account = account;
draft.folder = drafts.id; draft.folder = drafts.id;
draft.msgid = draft.generateMessageId();
if (ref != null) { if (ref != null) {
draft.thread = ref.thread; draft.thread = ref.thread;
@ -577,17 +521,17 @@ public class FragmentCompose extends FragmentEx {
} }
if ("reply".equals(action) || "reply_all".equals(action)) { if ("reply".equals(action) || "reply_all".equals(action)) {
draft.subject = getContext().getString(R.string.title_subject_reply, ref.subject); draft.subject = context.getString(R.string.title_subject_reply, ref.subject);
draft.body = String.format("<br><br>%s %s:<br><br>%s", draft.body = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()), Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", draft.to)), Html.escapeHtml(TextUtils.join(", ", draft.to)),
HtmlHelper.sanitize(getContext(), ref.body, true)); HtmlHelper.sanitize(context, ref.body, true));
} else if ("forward".equals(action)) { } else if ("forward".equals(action)) {
draft.subject = getContext().getString(R.string.title_subject_forward, ref.subject); draft.subject = context.getString(R.string.title_subject_forward, ref.subject);
draft.body = String.format("<br><br>%s %s:<br><br>%s", draft.body = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()), Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", ref.from)), Html.escapeHtml(TextUtils.join(", ", ref.from)),
HtmlHelper.sanitize(getContext(), ref.body, true)); HtmlHelper.sanitize(context, ref.body, true));
} }
} }
@ -611,78 +555,158 @@ public class FragmentCompose extends FragmentEx {
db.endTransaction(); db.endTransaction();
} }
EntityOperation.process(getContext()); EntityOperation.process(context);
} }
return draft; return draft;
} }
@Override @Override
public void onLoaded(Bundle args, EntityMessage draft) { protected void onLoaded(Bundle args, EntityMessage draft) {
id = draft.id; FragmentCompose.this.draft = draft;
if ("new".equals(args.getString("action"))) Log.i(Helper.TAG, "Loaded draft id=" + draft.id + " msgid=" + draft.msgid);
action = "edit";
DB db = DB.getInstance(getContext());
Log.i(Helper.TAG, "Get loaded action=" + action + " id=" + id);
db.message().liveMessageByMsgId(draft.msgid).observe(getViewLifecycleOwner(), new Observer<EntityMessage>() {
getActivity().invalidateOptionsMenu(); boolean observed = false;
pbWait.setVisibility(View.GONE);
grpAddresses.setVisibility("reply_all".equals(action) ? View.VISIBLE : View.GONE); @Override
grpReady.setVisibility(View.VISIBLE); public void onChanged(final EntityMessage draft) {
// Message was deleted
ArrayAdapter aa = (ArrayAdapter) spFrom.getAdapter(); if (draft == null) {
if (aa != null) { getFragmentManager().popBackStack();
for (int pos = 0; pos < aa.getCount(); pos++) { return;
EntityIdentity identity = (EntityIdentity) aa.getItem(pos);
if (draft.identity == null
? draft.from != null && draft.from.length > 0 && ((InternetAddress) draft.from[0]).getAddress().equals(identity.email)
: draft.identity.equals(identity.id)) {
spFrom.setSelection(pos);
break;
} }
}
}
etTo.setText(draft.to == null ? null : TextUtils.join(", ", draft.to)); // New working copy
etCc.setText(draft.cc == null ? null : TextUtils.join(", ", draft.cc)); FragmentCompose.this.draft = draft;
etBcc.setText(draft.bcc == null ? null : TextUtils.join(", ", draft.bcc));
etSubject.setText(draft.subject); // Set controls only once
if (observed)
return;
observed = true;
String action = getArguments().getString("action");
getActivity().invalidateOptionsMenu();
pbWait.setVisibility(View.GONE);
grpAddresses.setVisibility("reply_all".equals(action) ? View.VISIBLE : View.GONE);
grpReady.setVisibility(View.VISIBLE);
ArrayAdapter aa = (ArrayAdapter) spFrom.getAdapter();
if (aa != null) {
for (int pos = 0; pos < aa.getCount(); pos++) {
EntityIdentity identity = (EntityIdentity) aa.getItem(pos);
if (draft.identity == null
? draft.from != null && draft.from.length > 0 && ((InternetAddress) draft.from[0]).getAddress().equals(identity.email)
: draft.identity.equals(identity.id)) {
spFrom.setSelection(pos);
break;
}
}
}
DB db = DB.getInstance(getContext()); etTo.setText(draft.to == null ? null : TextUtils.join(", ", draft.to));
db.attachment().liveAttachments(draft.id).removeObservers(getViewLifecycleOwner()); etCc.setText(draft.cc == null ? null : TextUtils.join(", ", draft.cc));
db.attachment().liveAttachments(draft.id).observe(getViewLifecycleOwner(), etBcc.setText(draft.bcc == null ? null : TextUtils.join(", ", draft.bcc));
new Observer<List<TupleAttachment>>() { etSubject.setText(draft.subject);
etBody.setText(TextUtils.isEmpty(draft.body) ? null : Html.fromHtml(draft.body));
if ("edit".equals(action))
etTo.requestFocus();
else if ("reply".equals(action) || "reply_all".equals(action))
etBody.requestFocus();
else if ("forward".equals(action))
etTo.requestFocus();
bottom_navigation.getMenu().setGroupEnabled(0, true);
final DB db = DB.getInstance(getContext());
db.identity().liveIdentities(true).removeObservers(getViewLifecycleOwner());
db.identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
@Override @Override
public void onChanged(@Nullable List<TupleAttachment> attachments) { public void onChanged(@Nullable final List<EntityIdentity> identities) {
adapter.set(attachments); Log.i(Helper.TAG, "Set identities=" + identities.size());
grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
} // Sort identities
}); Collections.sort(identities, new Comparator<EntityIdentity>() {
@Override
public int compare(EntityIdentity i1, EntityIdentity i2) {
return i1.name.compareTo(i2.name);
}
});
// Show identities
ArrayAdapter<EntityIdentity> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, identities);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spFrom.setAdapter(adapter);
boolean found = false;
// Select earlier selected identity
if (draft.identity != null)
for (int pos = 0; pos < identities.size(); pos++) {
if (identities.get(pos).id.equals(draft.identity)) {
spFrom.setSelection(pos);
found = true;
break;
}
}
// Select identity matching from address
if (!found && draft.from != null && draft.from.length > 0) {
String from = ((InternetAddress) draft.from[0]).getAddress();
for (int pos = 0; pos < identities.size(); pos++) {
if (identities.get(pos).email.equals(from)) {
spFrom.setSelection(pos);
found = true;
break;
}
}
}
// Select primary identity
if (!found)
for (int pos = 0; pos < identities.size(); pos++)
if (identities.get(pos).primary) {
spFrom.setSelection(pos);
break;
}
spFrom.setVisibility(View.VISIBLE);
ivIdentityAdd.setVisibility(View.VISIBLE);
etBody.setText(TextUtils.isEmpty(draft.body) ? null : Html.fromHtml(draft.body)); }
});
if ("edit".equals(action)) db.attachment().liveAttachments(draft.id).removeObservers(getViewLifecycleOwner());
etTo.requestFocus(); db.attachment().liveAttachments(draft.id).observe(getViewLifecycleOwner(),
else if ("reply".equals(action) || "reply_all".equals(action)) new Observer<List<TupleAttachment>>() {
etBody.requestFocus(); @Override
else if ("forward".equals(action)) public void onChanged(@Nullable List<TupleAttachment> attachments) {
etTo.requestFocus(); adapter.set(attachments);
grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
}
});
bottom_navigation.getMenu().setGroupEnabled(0, true); }
});
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}; };
private SimpleLoader<EntityMessage> putLoader = new SimpleLoader<EntityMessage>() { private SimpleTask<EntityMessage> actionLoader = new SimpleTask<EntityMessage>() {
@Override @Override
public EntityMessage onLoad(Bundle args) throws Throwable { protected EntityMessage onLoad(Context context, Bundle args) throws Throwable {
// Get data // Get data
long id = args.getLong("id"); String id = args.getString("msgid");
int action = args.getInt("action"); int action = args.getInt("action");
long iid = args.getLong("identity"); long iid = args.getLong("identity");
String to = args.getString("to"); String to = args.getString("to");
@ -690,11 +714,10 @@ public class FragmentCompose extends FragmentEx {
String bcc = args.getString("bcc"); String bcc = args.getString("bcc");
String subject = args.getString("subject"); String subject = args.getString("subject");
String body = args.getString("body"); String body = args.getString("body");
Log.i(Helper.TAG, "Put load action=" + action + " id=" + id);
// Get draft & selected identity // Get draft & selected identity
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessage(id); EntityMessage draft = db.message().getMessageByMsgId(id);
EntityIdentity identity = db.identity().getIdentity(iid); EntityIdentity identity = db.identity().getIdentity(iid);
// Draft deleted by server // Draft deleted by server
@ -702,6 +725,8 @@ public class FragmentCompose extends FragmentEx {
if (draft == null) if (draft == null)
throw new MessageRemovedException(); throw new MessageRemovedException();
Log.i(Helper.TAG, "Load action msgid=" + draft.msgid + " action=" + action);
// Convert data // Convert data
Address afrom[] = (identity == null ? null : new Address[]{new InternetAddress(identity.email, identity.name)}); Address afrom[] = (identity == null ? null : new Address[]{new InternetAddress(identity.email, identity.name)});
Address ato[] = (TextUtils.isEmpty(to) ? null : InternetAddress.parse(to)); Address ato[] = (TextUtils.isEmpty(to) ? null : InternetAddress.parse(to));
@ -732,6 +757,7 @@ public class FragmentCompose extends FragmentEx {
EntityOperation.queue(db, draft, EntityOperation.MOVE, trash.id); EntityOperation.queue(db, draft, EntityOperation.MOVE, trash.id);
} else if (action == R.id.action_save) { } else if (action == R.id.action_save) {
// Save message ID
String msgid = draft.msgid; String msgid = draft.msgid;
// Save attachments // Save attachments
@ -748,7 +774,7 @@ public class FragmentCompose extends FragmentEx {
// Create new draft // Create new draft
draft.id = null; draft.id = null;
draft.uid = null; // unique index folder/uid draft.uid = null;
draft.msgid = msgid; draft.msgid = msgid;
draft.ui_hide = false; draft.ui_hide = false;
draft.id = db.message().insertMessage(draft); draft.id = db.message().insertMessage(draft);
@ -764,20 +790,22 @@ public class FragmentCompose extends FragmentEx {
} else if (action == R.id.action_send) { } else if (action == R.id.action_send) {
// Check data // Check data
if (draft.identity == null) if (draft.identity == null)
throw new IllegalArgumentException(getContext().getString(R.string.title_from_missing)); throw new IllegalArgumentException(context.getString(R.string.title_from_missing));
if (draft.to == null && draft.cc == null && draft.bcc == null) if (draft.to == null && draft.cc == null && draft.bcc == null)
throw new IllegalArgumentException(getContext().getString(R.string.title_to_missing)); throw new IllegalArgumentException(context.getString(R.string.title_to_missing));
if (db.attachment().getAttachmentCountWithoutContent(draft.id) > 0) if (db.attachment().getAttachmentCountWithoutContent(draft.id) > 0)
throw new IllegalArgumentException(getContext().getString(R.string.title_attachments_missing)); throw new IllegalArgumentException(context.getString(R.string.title_attachments_missing));
// Save message ID
String msgid = draft.msgid;
// Save attachments
List<EntityAttachment> attachments = db.attachment().getAttachments(draft.id); List<EntityAttachment> attachments = db.attachment().getAttachments(draft.id);
for (EntityAttachment attachment : attachments) for (EntityAttachment attachment : attachments)
attachment.content = db.attachment().getContent(attachment.id); attachment.content = db.attachment().getContent(attachment.id);
String msgid = draft.msgid;
// Delete draft (cannot move to outbox) // Delete draft (cannot move to outbox)
draft.msgid = null; draft.msgid = null;
draft.ui_hide = true; draft.ui_hide = true;
@ -792,6 +820,7 @@ public class FragmentCompose extends FragmentEx {
draft.ui_hide = false; draft.ui_hide = false;
draft.id = db.message().insertMessage(draft); draft.id = db.message().insertMessage(draft);
// Restore attachments
for (EntityAttachment attachment : attachments) { for (EntityAttachment attachment : attachments) {
attachment.message = draft.id; attachment.message = draft.id;
db.attachment().insertAttachment(attachment); db.attachment().insertAttachment(attachment);
@ -805,17 +834,19 @@ public class FragmentCompose extends FragmentEx {
db.endTransaction(); db.endTransaction();
} }
EntityOperation.process(getContext()); EntityOperation.process(context);
return draft; return draft;
} }
@Override @Override
public void onLoaded(Bundle args, EntityMessage draft) { protected void onLoaded(Bundle args, EntityMessage draft) {
id = draft.id; FragmentCompose.this.draft = draft;
int action = args.getInt("action"); int action = args.getInt("action");
Log.i(Helper.TAG, "Get loaded action=" + action + " id=" + id); Log.i(Helper.TAG, "Loaded action id=" + draft.id + " msgid=" + draft.msgid + " action=" + action);
Helper.setViewsEnabled(view, true);
bottom_navigation.getMenu().setGroupEnabled(0, true); bottom_navigation.getMenu().setGroupEnabled(0, true);
if (action == R.id.action_trash) { if (action == R.id.action_trash) {
@ -830,7 +861,7 @@ public class FragmentCompose extends FragmentEx {
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
bottom_navigation.getMenu().setGroupEnabled(0, true); bottom_navigation.getMenu().setGroupEnabled(0, true);
if (ex instanceof IllegalArgumentException) if (ex instanceof IllegalArgumentException)

@ -22,7 +22,6 @@ package eu.faircode.email;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -35,9 +34,6 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
public class FragmentFolder extends FragmentEx { public class FragmentFolder extends FragmentEx {
private ViewGroup view; private ViewGroup view;
@ -77,8 +73,47 @@ public class FragmentFolder extends FragmentEx {
args.putBoolean("synchronize", cbSynchronize.isChecked()); args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putString("after", etAfter.getText().toString()); args.putString("after", etAfter.getText().toString());
LoaderManager.getInstance(FragmentFolder.this) new SimpleTask<Void>() {
.restartLoader(ActivityView.LOADER_FOLDER_PUT, args, putLoaderCallbacks).forceLoad(); @Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
try {
ServiceSynchronize.stop(getContext(), "folder");
long id = args.getLong("id");
boolean synchronize = args.getBoolean("synchronize");
String after = args.getString("after");
int days = (TextUtils.isEmpty(after) ? 7 : Integer.parseInt(after));
DB db = DB.getInstance(getContext());
DaoFolder dao = db.folder();
EntityFolder folder = dao.getFolder(id);
folder.synchronize = synchronize;
folder.after = days;
dao.updateFolder(folder);
if (!folder.synchronize)
db.message().deleteMessages(folder.id);
return null;
} finally {
ServiceSynchronize.restart(getContext(), "folder");
}
}
@Override
protected void onLoaded(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
pbSave.setVisibility(View.GONE);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentFolder.this, args);
} }
}); });
@ -114,79 +149,4 @@ public class FragmentFolder extends FragmentEx {
} }
}); });
} }
private static class PutLoader extends AsyncTaskLoader<Throwable> {
private Bundle args;
PutLoader(Context context) {
super(context);
}
void setArgs(Bundle args) {
this.args = args;
}
protected void onStartLoading() {
forceLoad();
}
@Override
public Throwable loadInBackground() {
try {
ServiceSynchronize.stop(getContext(), "folder");
long id = args.getLong("id");
boolean synchronize = args.getBoolean("synchronize");
String after = args.getString("after");
int days = (TextUtils.isEmpty(after) ? 7 : Integer.parseInt(after));
DB db = DB.getInstance(getContext());
DaoFolder dao = db.folder();
EntityFolder folder = dao.getFolder(id);
folder.synchronize = synchronize;
folder.after = days;
dao.updateFolder(folder);
if (!folder.synchronize)
db.message().deleteMessages(folder.id);
return null;
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
return ex;
} finally {
ServiceSynchronize.restart(getContext(), "folder");
}
}
}
private LoaderManager.LoaderCallbacks putLoaderCallbacks = new LoaderManager.LoaderCallbacks<Throwable>() {
@NonNull
@Override
public Loader<Throwable> onCreateLoader(int id, Bundle args) {
PutLoader loader = new PutLoader(getContext());
loader.setArgs(args);
return loader;
}
@Override
public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) {
LoaderManager.getInstance(FragmentFolder.this).destroyLoader(loader.getId());
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
pbSave.setVisibility(View.GONE);
if (ex == null)
getFragmentManager().popBackStack();
else {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onLoaderReset(@NonNull Loader<Throwable> loader) {
}
};
} }

@ -25,7 +25,6 @@ import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -53,9 +52,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
public class FragmentIdentity extends FragmentEx { public class FragmentIdentity extends FragmentEx {
private List<Provider> providers; private List<Provider> providers;
@ -200,8 +196,107 @@ public class FragmentIdentity extends FragmentEx {
args.putBoolean("synchronize", cbSynchronize.isChecked()); args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("primary", cbPrimary.isChecked()); args.putBoolean("primary", cbPrimary.isChecked());
LoaderManager.getInstance(FragmentIdentity.this) new SimpleTask<Void>() {
.restartLoader(ActivityView.LOADER_IDENTITY_PUT, args, putLoaderCallbacks).forceLoad(); @Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
try {
ServiceSynchronize.stop(getContext(), "account");
long id = args.getLong("id");
String name = args.getString("name");
String email = args.getString("email");
String replyto = args.getString("replyto");
long account = args.getLong("account");
String host = args.getString("host");
boolean starttls = args.getBoolean("starttls");
String port = args.getString("port");
String user = args.getString("user");
String password = args.getString("password");
if (TextUtils.isEmpty(name))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_name));
if (TextUtils.isEmpty(email))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_email));
if (account < 0)
throw new IllegalArgumentException(getContext().getString(R.string.title_no_account));
if (TextUtils.isEmpty(host))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_port));
if (TextUtils.isEmpty(user))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_user));
if (TextUtils.isEmpty(password))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_password));
if (TextUtils.isEmpty(replyto))
replyto = null;
DB db = DB.getInstance(getContext());
EntityIdentity identity = db.identity().getIdentity(id);
boolean update = (identity != null);
if (identity == null)
identity = new EntityIdentity();
identity.name = name;
identity.email = email;
identity.replyto = replyto;
identity.account = account;
identity.host = Objects.requireNonNull(host);
identity.port = Integer.parseInt(port);
identity.starttls = starttls;
identity.user = user;
identity.password = password;
identity.synchronize = args.getBoolean("synchronize");
identity.primary = (identity.synchronize && args.getBoolean("primary"));
// Check SMTP server
if (identity.synchronize) {
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
Transport itransport = isession.getTransport(identity.starttls ? "smtp" : "smtps");
try {
itransport.connect(identity.host, identity.port, identity.user, identity.password);
} finally {
itransport.close();
}
}
try {
db.beginTransaction();
if (identity.primary)
db.identity().resetPrimary();
if (update)
db.identity().updateIdentity(identity);
else
identity.id = db.identity().insertIdentity(identity);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
} finally {
ServiceSynchronize.restart(getContext(), "account");
}
}
@Override
protected void onLoaded(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
pbSave.setVisibility(View.GONE);
Toast.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}.load(FragmentIdentity.this, args);
} }
}); });
@ -214,20 +309,25 @@ public class FragmentIdentity extends FragmentEx {
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
getFragmentManager().popBackStack();
// TODO: spinner // TODO: spinner
new SimpleLoader<Void>() { getFragmentManager().popBackStack();
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) throws Throwable { protected Void onLoad(Context context, Bundle args) {
DB.getInstance(getContext()).identity().deleteIdentity(id); long id = args.getLong("id");
DB.getInstance(context).identity().deleteIdentity(id);
return null; return null;
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentIdentity.this, ActivitySetup.LOADER_DELETE_IDENTITY, new Bundle()); }.load(FragmentIdentity.this, args);
} }
}) })
.setNegativeButton(android.R.string.cancel, null).show(); .setNegativeButton(android.R.string.cancel, null).show();
@ -301,138 +401,4 @@ public class FragmentIdentity extends FragmentEx {
} }
}); });
} }
private static class PutLoader extends AsyncTaskLoader<Throwable> {
private Bundle args;
PutLoader(Context context) {
super(context);
}
void setArgs(Bundle args) {
this.args = args;
}
protected void onStartLoading() {
forceLoad();
}
@Override
public Throwable loadInBackground() {
try {
ServiceSynchronize.stop(getContext(), "account");
long id = args.getLong("id");
String name = args.getString("name");
String email = args.getString("email");
String replyto = args.getString("replyto");
long account = args.getLong("account");
String host = args.getString("host");
boolean starttls = args.getBoolean("starttls");
String port = args.getString("port");
String user = args.getString("user");
String password = args.getString("password");
if (TextUtils.isEmpty(name))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_name));
if (TextUtils.isEmpty(email))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_email));
if (account < 0)
throw new IllegalArgumentException(getContext().getString(R.string.title_no_account));
if (TextUtils.isEmpty(host))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_host));
if (TextUtils.isEmpty(port))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_port));
if (TextUtils.isEmpty(user))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_user));
if (TextUtils.isEmpty(password))
throw new IllegalArgumentException(getContext().getString(R.string.title_no_password));
if (TextUtils.isEmpty(replyto))
replyto = null;
DB db = DB.getInstance(getContext());
EntityIdentity identity = db.identity().getIdentity(id);
boolean update = (identity != null);
if (identity == null)
identity = new EntityIdentity();
identity.name = name;
identity.email = email;
identity.replyto = replyto;
identity.account = account;
identity.host = Objects.requireNonNull(host);
identity.port = Integer.parseInt(port);
identity.starttls = starttls;
identity.user = user;
identity.password = password;
identity.synchronize = args.getBoolean("synchronize");
identity.primary = (identity.synchronize && args.getBoolean("primary"));
// Check SMTP server
if (identity.synchronize) {
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
Transport itransport = isession.getTransport(identity.starttls ? "smtp" : "smtps");
try {
itransport.connect(identity.host, identity.port, identity.user, identity.password);
} finally {
itransport.close();
}
}
try {
db.beginTransaction();
if (identity.primary)
db.identity().resetPrimary();
if (update)
db.identity().updateIdentity(identity);
else
identity.id = db.identity().insertIdentity(identity);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
return ex;
} finally {
ServiceSynchronize.restart(getContext(), "account");
}
}
}
private LoaderManager.LoaderCallbacks putLoaderCallbacks = new LoaderManager.LoaderCallbacks<Throwable>() {
@NonNull
@Override
public Loader<Throwable> onCreateLoader(int id, Bundle args) {
PutLoader loader = new PutLoader(getContext());
loader.setArgs(args);
return loader;
}
@Override
public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) {
LoaderManager.getInstance(FragmentIdentity.this).destroyLoader(loader.getId());
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
pbSave.setVisibility(View.GONE);
if (ex == null)
getFragmentManager().popBackStack();
else {
Log.w(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
Toast.makeText(getContext(), Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}
@Override
public void onLoaderReset(@NonNull Loader<Throwable> loader) {
}
};
} }

@ -62,9 +62,6 @@ import androidx.browser.customtabs.CustomTabsIntent;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -402,11 +399,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader<Void>() { new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) { protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
try { try {
db.beginTransaction(); db.beginTransaction();
@ -422,13 +419,13 @@ public class FragmentMessage extends FragmentEx {
db.endTransaction(); db.endTransaction();
} }
EntityOperation.process(getContext()); EntityOperation.process(context);
return null; return null;
} }
@Override @Override
public void onLoaded(Bundle args, Void data) { protected void onLoaded(Bundle args, Void data) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
} }
@ -439,7 +436,7 @@ public class FragmentMessage extends FragmentEx {
item.setIcon(icon); item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, ActivityView.LOADER_MESSAGE_SEEN, args); }.load(this, args);
} }
private void onActionEdit(long id) { private void onActionEdit(long id) {
@ -452,11 +449,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader<Long>() { new SimpleTask<Long>() {
@Override @Override
public Long onLoad(Bundle args) { protected Long onLoad(Context context, Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessage(id); EntityMessage draft = db.message().getMessage(id);
EntityFolder drafts = db.folder().getFolderByType(draft.account, EntityFolder.DRAFTS); EntityFolder drafts = db.folder().getFolderByType(draft.account, EntityFolder.DRAFTS);
draft.id = null; draft.id = null;
@ -467,7 +464,7 @@ public class FragmentMessage extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Long id) { protected void onLoaded(Bundle args, Long id) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
getContext().startActivity( getContext().startActivity(
@ -482,7 +479,7 @@ public class FragmentMessage extends FragmentEx {
item.setIcon(icon); item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, ActivityView.LOADER_MESSAGE_EDIT, args); }.load(this, args);
} }
private void onActionForward(long id) { private void onActionForward(long id) {
@ -513,11 +510,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader<Void>() { new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) { protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
try { try {
db.beginTransaction(); db.beginTransaction();
@ -533,24 +530,24 @@ public class FragmentMessage extends FragmentEx {
db.endTransaction(); db.endTransaction();
} }
EntityOperation.process(getContext()); EntityOperation.process(context);
return null; return null;
} }
@Override @Override
public void onLoaded(Bundle args, Void result) { protected void onLoaded(Bundle args, Void result) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_SPAM, args); }.load(FragmentMessage.this, args);
} }
}) })
.setNegativeButton(android.R.string.cancel, null).show(); .setNegativeButton(android.R.string.cancel, null).show();
@ -575,11 +572,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader<Void>() { new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) { protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
try { try {
db.beginTransaction(); db.beginTransaction();
@ -593,24 +590,24 @@ public class FragmentMessage extends FragmentEx {
db.endTransaction(); db.endTransaction();
} }
EntityOperation.process(getContext()); EntityOperation.process(context);
return null; return null;
} }
@Override @Override
public void onLoaded(Bundle args, Void result) { protected void onLoaded(Bundle args, Void result) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_TRASH, args); }.load(FragmentMessage.this, args);
} }
}) })
.setNegativeButton(android.R.string.cancel, null).show(); .setNegativeButton(android.R.string.cancel, null).show();
@ -624,11 +621,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader<Void>() { new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) { protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
try { try {
db.beginTransaction(); db.beginTransaction();
@ -644,32 +641,128 @@ public class FragmentMessage extends FragmentEx {
db.endTransaction(); db.endTransaction();
} }
EntityOperation.process(getContext()); EntityOperation.process(context);
return null; return null;
} }
@Override @Override
public void onLoaded(Bundle args, Void result) { protected void onLoaded(Bundle args, Void result) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_TRASH, args); }.load(FragmentMessage.this, args);
} }
} }
private void onActionMove(long id) { private void onActionMove(long id) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
LoaderManager.getInstance(this)
.restartLoader(ActivityView.LOADER_MESSAGE_MOVE, args, moveLoaderCallbacks).forceLoad(); new SimpleTask<List<EntityFolder>>() {
@Override
protected List<EntityFolder> onLoad(Context context, Bundle args) {
DB db = DB.getInstance(getContext());
EntityMessage message = db.message().getMessage(args.getLong("id"));
List<EntityFolder> folders = db.folder().getUserFolders(message.account);
for (int i = 0; i < folders.size(); i++)
if (folders.get(i).id.equals(message.folder)) {
folders.remove(i);
break;
}
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(folders, new Comparator<EntityFolder>() {
@Override
public int compare(EntityFolder f1, EntityFolder f2) {
return collator.compare(f1.name, f2.name);
}
});
EntityFolder inbox = db.folder().getFolderByType(message.account, EntityFolder.INBOX);
if (!message.folder.equals(inbox.id))
folders.add(0, inbox);
return folders;
}
@Override
protected void onLoaded(final Bundle args, List<EntityFolder> folders) {
View anchor = bottom_navigation.findViewById(R.id.action_move);
PopupMenu popupMenu = new PopupMenu(getContext(), anchor);
int order = 0;
for (EntityFolder folder : folders)
popupMenu.getMenu().add(Menu.NONE, folder.id.intValue(), order++,
Helper.localizeFolderName(getContext(), folder.name));
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(final MenuItem target) {
final MenuItem item = bottom_navigation.getMenu().findItem(R.id.action_move);
item.setEnabled(false);
final Drawable icon = item.getIcon();
item.setIcon(Helper.toDimmed(icon));
args.putLong("target", target.getItemId());
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
long target = args.getLong("target");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
message.ui_hide = true;
db.message().updateMessage(message);
EntityOperation.queue(db, message, EntityOperation.MOVE, target);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityOperation.process(context);
return null;
}
@Override
protected void onLoaded(Bundle args, Void result) {
item.setEnabled(true);
item.setIcon(icon);
}
@Override
protected void onException(Bundle args, Throwable ex) {
item.setEnabled(true);
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentMessage.this, args);
return true;
}
});
popupMenu.show();
}
}.load(FragmentMessage.this, args);
} }
private void onActionArchive(long id) { private void onActionArchive(long id) {
@ -682,11 +775,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
new SimpleLoader<Void>() { new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) { protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id"); long id = args.getLong("id");
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
try { try {
db.beginTransaction(); db.beginTransaction();
@ -702,24 +795,24 @@ public class FragmentMessage extends FragmentEx {
db.endTransaction(); db.endTransaction();
} }
EntityOperation.process(getContext()); EntityOperation.process(context);
return null; return null;
} }
@Override @Override
public void onLoaded(Bundle args, Void result) { protected void onLoaded(Bundle args, Void result) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
item.setEnabled(true); item.setEnabled(true);
item.setIcon(icon); item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_ARCHIVE, args); }.load(FragmentMessage.this, args);
} }
private void onActionReply(long id) { private void onActionReply(long id) {
@ -727,132 +820,4 @@ public class FragmentMessage extends FragmentEx {
.putExtra("action", "reply") .putExtra("action", "reply")
.putExtra("reference", id)); .putExtra("reference", id));
} }
private static class MoveLoader extends AsyncTaskLoader<List<EntityFolder>> {
private Bundle args;
MoveLoader(Context context) {
super(context);
}
void setArgs(Bundle args) {
this.args = args;
}
protected void onStartLoading() {
forceLoad();
}
@Override
public List<EntityFolder> loadInBackground() {
DB db = DB.getInstance(getContext());
EntityMessage message = db.message().getMessage(args.getLong("id"));
List<EntityFolder> folders = db.folder().getUserFolders(message.account);
for (int i = 0; i < folders.size(); i++)
if (folders.get(i).id.equals(message.folder)) {
folders.remove(i);
break;
}
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(folders, new Comparator<EntityFolder>() {
@Override
public int compare(EntityFolder f1, EntityFolder f2) {
return collator.compare(f1.name, f2.name);
}
});
EntityFolder inbox = db.folder().getFolderByType(message.account, EntityFolder.INBOX);
if (!message.folder.equals(inbox.id))
folders.add(0, inbox);
return folders;
}
}
private LoaderManager.LoaderCallbacks moveLoaderCallbacks = new LoaderManager.LoaderCallbacks<List<EntityFolder>>() {
@NonNull
@Override
public Loader<List<EntityFolder>> onCreateLoader(int id, Bundle args) {
MoveLoader loader = new MoveLoader(getContext());
loader.setArgs(args);
return loader;
}
@Override
public void onLoadFinished(@NonNull final Loader<List<EntityFolder>> loader, List<EntityFolder> folders) {
LoaderManager.getInstance(FragmentMessage.this).destroyLoader(loader.getId());
View anchor = bottom_navigation.findViewById(R.id.action_move);
PopupMenu popupMenu = new PopupMenu(getContext(), anchor);
int order = 0;
for (EntityFolder folder : folders)
popupMenu.getMenu().add(Menu.NONE, folder.id.intValue(), order++,
Helper.localizeFolderName(getContext(), folder.name));
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(final MenuItem target) {
final MenuItem item = bottom_navigation.getMenu().findItem(R.id.action_move);
item.setEnabled(false);
final Drawable icon = item.getIcon();
item.setIcon(Helper.toDimmed(icon));
Bundle args = ((MoveLoader) loader).args;
args.putLong("target", target.getItemId());
new SimpleLoader<Void>() {
@Override
public Void onLoad(Bundle args) {
long id = args.getLong("id");
long target = args.getLong("target");
DB db = DB.getInstance(getContext());
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
message.ui_hide = true;
db.message().updateMessage(message);
EntityOperation.queue(db, message, EntityOperation.MOVE, target);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
EntityOperation.process(getContext());
return null;
}
@Override
public void onLoaded(Bundle args, Void result) {
item.setEnabled(true);
item.setIcon(icon);
}
@Override
public void onException(Bundle args, Throwable ex) {
item.setEnabled(true);
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentMessage.this, ActivityView.LOADER_MESSAGE_MOVE, args);
return true;
}
});
popupMenu.show();
}
@Override
public void onLoaderReset(@NonNull Loader<List<EntityFolder>> loader) {
}
};
} }

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B) Copyright 2018 by Marcel Bokhorst (M66B)
*/ */
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -150,13 +151,13 @@ public class FragmentMessages extends FragmentEx {
} }
}); });
new SimpleLoader<Long>() { new SimpleTask<Long>() {
@Override @Override
public Long onLoad(Bundle args) { protected Long onLoad(Context context, Bundle args) {
long folder = (args == null ? -1 : args.getLong("folder", -1)); long folder = (args == null ? -1 : args.getLong("folder", -1));
long thread = (args == null ? -1 : args.getLong("thread", -1)); // message ID long thread = (args == null ? -1 : args.getLong("thread", -1)); // message ID
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
Long account; Long account;
if (thread < 0) if (thread < 0)
@ -174,15 +175,15 @@ public class FragmentMessages extends FragmentEx {
} }
@Override @Override
public void onLoaded(Bundle args, Long account) { protected void onLoaded(Bundle args, Long account) {
fab.setTag(account); fab.setTag(account);
fab.setVisibility(View.VISIBLE); fab.setVisibility(View.VISIBLE);
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, ActivityView.LOADER_MESSAGE_ACCOUNT, getArguments()); }.load(this, getArguments());
} }
} }

@ -20,6 +20,7 @@ package eu.faircode.email;
*/ */
import android.Manifest; import android.Manifest;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
@ -152,10 +153,10 @@ public class FragmentSetup extends FragmentEx {
onRequestPermissionsResult(0, permissions, grantResults); onRequestPermissionsResult(0, permissions, grantResults);
// Create outbox // Create outbox
new SimpleLoader<Void>() { new SimpleTask<Void>() {
@Override @Override
public Void onLoad(Bundle args) throws Throwable { protected Void onLoad(Context context, Bundle args) throws Throwable {
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(context);
EntityFolder outbox = db.folder().getOutbox(); EntityFolder outbox = db.folder().getOutbox();
if (outbox == null) { if (outbox == null) {
outbox = new EntityFolder(); outbox = new EntityFolder();
@ -169,10 +170,10 @@ public class FragmentSetup extends FragmentEx {
} }
@Override @Override
public void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
} }
}.load(this, ActivitySetup.LOADER_CREATE_OUTBOX, new Bundle()); }.load(this, new Bundle());
return view; return view;
} }

@ -1,134 +0,0 @@
package eu.faircode.email;
/*
This file is part of Safe email.
Safe email 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.
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
//
// This simple loader is simple to use, but it is also simple to cause bugs that can easily lead to crashes
// Make sure to not access any member in any outer scope from onLoad
//
public abstract class SimpleLoader<T> {
private Context context;
private LoaderManager manager;
public void load(AppCompatActivity activity, int id, Bundle args) {
this.context = activity;
this.manager = LoaderManager.getInstance(activity);
manager.restartLoader(id, args, callbacks).forceLoad();
}
public void load(Fragment fragment, int id, Bundle args) {
this.context = fragment.getContext();
this.manager = LoaderManager.getInstance(fragment);
manager.restartLoader(id, args, callbacks).forceLoad();
}
public T onLoad(Bundle args) throws Throwable {
// Be careful not to access members in outer scopes
return null;
}
public void onLoaded(Bundle args, T data) {
}
public void onException(Bundle args, Throwable ex) {
}
protected Context getContext() {
return context;
}
private static class CommonLoader extends AsyncTaskLoader<Result> {
boolean loading = false;
Bundle args;
SimpleLoader loader;
CommonLoader(Context context) {
super(context);
}
void setArgs(Bundle args, SimpleLoader x) {
this.args = args;
this.loader = x;
}
protected void onStartLoading() {
if (!loading)
forceLoad();
}
@Override
public Result loadInBackground() {
loading = true;
Result result = new Result();
try {
result.data = loader.onLoad(args);
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
result.ex = ex;
}
return result;
}
}
private LoaderManager.LoaderCallbacks callbacks = new LoaderManager.LoaderCallbacks<Result>() {
@Override
public Loader<Result> onCreateLoader(int id, Bundle args) {
CommonLoader loader = new CommonLoader(context);
loader.setArgs(args, SimpleLoader.this);
return loader;
}
@Override
public void onLoadFinished(Loader<Result> loader, Result data) {
manager.destroyLoader(loader.getId());
CommonLoader common = (CommonLoader) loader;
if (data.ex == null)
onLoaded(common.args, (T) data.data);
else
onException(common.args, data.ex);
common.args = null;
common.loader = null;
manager = null;
}
@Override
public void onLoaderReset(Loader<Result> loader) {
}
};
private static class Result {
Throwable ex;
Object data;
}
}

@ -0,0 +1,98 @@
package eu.faircode.email;
/*
This file is part of Safe email.
Safe email 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.
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
//
// This simple task is simple to use, but it is also simple to cause bugs that can easily lead to crashes
// Make sure to not access any member in any outer scope from onLoad
//
public abstract class SimpleTask<T> implements LifecycleObserver {
private boolean alive = true;
public void load(AppCompatActivity activity, Bundle args) {
run(activity, activity, args);
}
public void load(Fragment fragment, Bundle args) {
run(fragment.getContext(), fragment.getViewLifecycleOwner(), args);
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
alive = false;
}
private void run(final Context context, LifecycleOwner owner, final Bundle args) {
owner.getLifecycle().addObserver(this);
new AsyncTask<Object, Object, Result>() {
@Override
protected Result doInBackground(Object... params) {
Result result = new Result();
try {
result.data = onLoad(context, args);
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
result.ex = ex;
}
return result;
}
@Override
protected void onPostExecute(Result result) {
if (alive) {
if (result.ex == null)
onLoaded(args, (T) result.data);
else
onException(args, result.ex);
}
}
}.execute(args);
}
protected T onLoad(Context context, Bundle args) throws Throwable {
// Be careful not to access members in outer scopes
return null;
}
protected void onLoaded(Bundle args, T data) {
}
protected void onException(Bundle args, Throwable ex) {
}
private static class Result {
Throwable ex;
Object data;
}
}
Loading…
Cancel
Save