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"?>
<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">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />

@ -26,10 +26,6 @@ import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
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_CC = 2;
static final int REQUEST_CONTACT_BCC = 3;

@ -31,10 +31,6 @@ import androidx.fragment.app.FragmentTransaction;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
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_IDENTITY = BuildConfig.APPLICATION_ID + ".EDIT_IDENTITY";

@ -66,22 +66,6 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
private ListView drawerList;
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_UNSEEN = 2;
@ -179,12 +163,12 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
fragmentTransaction.commit();
}
new SimpleLoader<Long>() {
new SimpleTask<Long>() {
@Override
public Long onLoad(Bundle args) throws Throwable {
File file = new File(getContext().getCacheDir(), "crash.log");
protected Long onLoad(Context context, Bundle args) throws Throwable {
File file = new File(context.getCacheDir(), "crash.log");
if (file.exists()) {
DB db = DB.getInstance(getContext());
DB db = DB.getInstance(context);
EntityFolder drafts = db.folder().getPrimaryDrafts();
if (drafts != null) {
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.folder = drafts.id;
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.received = new Date().getTime();
draft.seen = false;
@ -239,7 +223,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
}
@Override
public void onLoaded(Bundle args, Long id) {
protected void onLoaded(Bundle args, Long id) {
if (id != null)
startActivity(
new Intent(ActivityView.this, ActivityCompose.class)
@ -247,7 +231,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
.putExtra("id", id));
}
}.load(this, LOADER_EXCEPTION, new Bundle());
}.load(this, new Bundle());
checkIntent(getIntent());
}
@ -339,11 +323,11 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
Bundle args = new Bundle();
args.putLong("time", new Date().getTime());
new SimpleLoader<Void>() {
new SimpleTask<Void>() {
@Override
public Void onLoad(Bundle args) {
protected Void onLoad(Context context, Bundle args) {
long time = args.getLong("time");
DaoAccount dao = DB.getInstance(getContext()).account();
DaoAccount dao = DB.getInstance(context).account();
for (EntityAccount account : dao.getAccounts(true)) {
account.seen_until = time;
dao.updateAccount(account);
@ -352,10 +336,10 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
}
@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();
}
}.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())) {
new SimpleLoader<Void>() {
new SimpleTask<Void>() {
@Override
public Void onLoad(Bundle args) {
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(ActivityView.this);
DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id);
EntityFolder folder = db.folder().getFolder(message.folder);
if (!EntityFolder.OUTBOX.equals(folder.type) &&
@ -485,7 +469,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
db.endTransaction();
}
EntityOperation.process(ActivityView.this);
EntityOperation.process(context);
}
}
@ -493,7 +477,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
}
@Override
public void onLoaded(Bundle args, Void result) {
protected void onLoaded(Bundle args, Void result) {
FragmentMessage fragment = new FragmentMessage();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
@ -502,10 +486,10 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
}
@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();
}
}.load(ActivityView.this, LOADER_MESSAGE_VIEW, intent.getExtras());
}.load(ActivityView.this, intent.getExtras());
} else if (ACTION_EDIT_FOLDER.equals(intent.getAction())) {
FragmentFolder fragment = new FragmentFolder();

@ -92,6 +92,9 @@ public interface DaoMessage {
" WHERE message.id = :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")
List<Long> getUids(long folder, long received);

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
@ -54,17 +55,17 @@ public class FragmentAbout extends FragmentEx {
@Override
public void onClick(View view) {
btnDebugInfo.setEnabled(false);
new SimpleLoader<Long>() {
new SimpleTask<Long>() {
@Override
public Long onLoad(Bundle args) throws UnsupportedEncodingException {
DB db = DB.getInstance(getContext());
protected Long onLoad(Context context, Bundle args) throws UnsupportedEncodingException {
DB db = DB.getInstance(context);
EntityFolder drafts = db.folder().getPrimaryDrafts();
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();
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");
@ -84,7 +85,7 @@ public class FragmentAbout extends FragmentEx {
}
@Override
public void onLoaded(Bundle args, Long id) {
protected void onLoaded(Bundle args, Long id) {
btnDebugInfo.setEnabled(true);
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "edit")
@ -92,11 +93,11 @@ public class FragmentAbout extends FragmentEx {
}
@Override
public void onException(Bundle args, Throwable ex) {
protected void onException(Bundle args, Throwable ex) {
btnDebugInfo.setEnabled(true);
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.constraintlayout.widget.Group;
import androidx.lifecycle.Observer;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
public class FragmentAccount extends FragmentEx {
private List<Provider> providers;
@ -176,157 +173,9 @@ public class FragmentAccount extends FragmentEx {
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("primary", cbPrimary.isChecked());
LoaderManager.getInstance(FragmentAccount.this)
.restartLoader(ActivityView.LOADER_ACCOUNT_CHECK, args, checkLoaderCallbacks).forceLoad();
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
new SimpleTask<List<EntityFolder>>() {
@Override
public void onClick(View v) {
Helper.setViewsEnabled(view, false);
btnCheck.setEnabled(false);
btnSave.setEnabled(false);
pbSave.setVisibility(View.VISIBLE);
EntityFolder drafts = (EntityFolder) spDrafts.getSelectedItem();
EntityFolder sent = (EntityFolder) spSent.getSelectedItem();
EntityFolder all = (EntityFolder) spAll.getSelectedItem();
EntityFolder trash = (EntityFolder) spTrash.getSelectedItem();
EntityFolder junk = (EntityFolder) spJunk.getSelectedItem();
if (drafts.type == null)
drafts = null;
if (sent.type == null)
sent = null;
if (all.type == null)
all = null;
if (trash.type == null)
trash = null;
if (junk.type == null)
junk = null;
Bundle args = new Bundle();
args.putLong("id", id);
args.putString("name", etName.getText().toString());
args.putString("host", etHost.getText().toString());
args.putString("port", etPort.getText().toString());
args.putString("user", etUser.getText().toString());
args.putString("password", tilPassword.getEditText().getText().toString());
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("primary", cbPrimary.isChecked());
args.putSerializable("drafts", drafts);
args.putSerializable("sent", sent);
args.putSerializable("all", all);
args.putSerializable("trash", trash);
args.putSerializable("junk", junk);
LoaderManager.getInstance(FragmentAccount.this)
.restartLoader(ActivityView.LOADER_ACCOUNT_PUT, args, putLoaderCallbacks).forceLoad();
}
});
ibDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder
.setMessage(R.string.title_account_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getFragmentManager().popBackStack();
// TODO: spinner
new SimpleLoader<Void>() {
@Override
public Void onLoad(Bundle args) {
// To prevent foreign key constraints from triggering
ServiceSynchronize.stop(getContext(), "delete account");
DB.getInstance(getContext()).account().deleteAccount(id);
ServiceSynchronize.start(getContext());
return null;
}
@Override
public void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentAccount.this, ActivitySetup.LOADER_DELETE_ACCOUNT, new Bundle());
}
})
.setNegativeButton(android.R.string.cancel, null).show();
}
});
// Initialize
Helper.setViewsEnabled(view, false);
tilPassword.setPasswordVisibilityToggleEnabled(id < 0);
btnCheck.setEnabled(false);
pbCheck.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
pbSave.setVisibility(View.GONE);
grpFolders.setVisibility(View.GONE);
ibDelete.setVisibility(View.GONE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id", -1));
final DB db = DB.getInstance(getContext());
// Observe
db.account().liveAccount(id).observe(getViewLifecycleOwner(), new Observer<EntityAccount>() {
@Override
public void onChanged(@Nullable EntityAccount account) {
etName.setText(account == null ? null : account.name);
etHost.setText(account == null ? null : account.host);
etPort.setText(account == null ? null : Long.toString(account.port));
etUser.setText(account == null ? null : account.user);
tilPassword.getEditText().setText(account == null ? null : account.password);
cbSynchronize.setChecked(account == null ? true : account.synchronize);
cbPrimary.setChecked(account == null ? true : account.primary);
cbPrimary.setEnabled(account == null ? true : account.synchronize);
ibDelete.setVisibility(account == null ? View.GONE : View.VISIBLE);
Helper.setViewsEnabled(view, true);
btnCheck.setEnabled(true);
pbWait.setVisibility(View.GONE);
}
});
}
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 {
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");
@ -405,38 +254,19 @@ public class FragmentAccount extends FragmentEx {
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;
return folders;
}
@Override
public void onLoadFinished(@NonNull Loader<CheckData> loader, CheckData data) {
LoaderManager.getInstance(FragmentAccount.this).destroyLoader(loader.getId());
protected void onLoaded(Bundle args, List<EntityFolder> folders) {
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>() {
Collections.sort(folders, new Comparator<EntityFolder>() {
@Override
public int compare(EntityFolder f1, EntityFolder f2) {
int s = Integer.compare(
@ -455,9 +285,9 @@ public class FragmentAccount extends FragmentEx {
EntityFolder none = new EntityFolder();
none.name = "";
data.folders.add(0, none);
folders.add(0, none);
ArrayAdapter<EntityFolder> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, data.folders);
ArrayAdapter<EntityFolder> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, folders);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spDrafts.setAdapter(adapter);
@ -466,16 +296,16 @@ public class FragmentAccount extends FragmentEx {
spTrash.setAdapter(adapter);
spJunk.setAdapter(adapter);
for (int pos = 0; pos < data.folders.size(); pos++) {
if (EntityFolder.DRAFTS.equals(data.folders.get(pos).type))
for (int pos = 0; pos < folders.size(); pos++) {
if (EntityFolder.DRAFTS.equals(folders.get(pos).type))
spDrafts.setSelection(pos);
else if (EntityFolder.SENT.equals(data.folders.get(pos).type))
else if (EntityFolder.SENT.equals(folders.get(pos).type))
spSent.setSelection(pos);
else if (EntityFolder.ARCHIVE.equals(data.folders.get(pos).type))
else if (EntityFolder.ARCHIVE.equals(folders.get(pos).type))
spAll.setSelection(pos);
else if (EntityFolder.TRASH.equals(data.folders.get(pos).type))
else if (EntityFolder.TRASH.equals(folders.get(pos).type))
spTrash.setSelection(pos);
else if (EntityFolder.JUNK.equals(data.folders.get(pos).type))
else if (EntityFolder.JUNK.equals(folders.get(pos).type))
spJunk.setSelection(pos);
}
@ -487,36 +317,61 @@ public class FragmentAccount extends FragmentEx {
((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();
}
@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);
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onLoaderReset(@NonNull Loader<CheckData> loader) {
}
};
private static class PutLoader extends AsyncTaskLoader<Throwable> {
private Bundle args;
public void onClick(View v) {
Helper.setViewsEnabled(view, false);
btnCheck.setEnabled(false);
btnSave.setEnabled(false);
pbSave.setVisibility(View.VISIBLE);
PutLoader(Context context) {
super(context);
}
EntityFolder drafts = (EntityFolder) spDrafts.getSelectedItem();
EntityFolder sent = (EntityFolder) spSent.getSelectedItem();
EntityFolder all = (EntityFolder) spAll.getSelectedItem();
EntityFolder trash = (EntityFolder) spTrash.getSelectedItem();
EntityFolder junk = (EntityFolder) spJunk.getSelectedItem();
void setArgs(Bundle args) {
this.args = args;
}
if (drafts.type == null)
drafts = null;
if (sent.type == null)
sent = null;
if (all.type == null)
all = null;
if (trash.type == null)
trash = null;
if (junk.type == null)
junk = null;
protected void onStartLoading() {
forceLoad();
}
Bundle args = new Bundle();
args.putLong("id", id);
args.putString("name", etName.getText().toString());
args.putString("host", etHost.getText().toString());
args.putString("port", etPort.getText().toString());
args.putString("user", etUser.getText().toString());
args.putString("password", tilPassword.getEditText().getText().toString());
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("primary", cbPrimary.isChecked());
args.putSerializable("drafts", drafts);
args.putSerializable("sent", sent);
args.putSerializable("all", all);
args.putSerializable("trash", trash);
args.putSerializable("junk", junk);
new SimpleTask<Void>() {
@Override
public Throwable loadInBackground() {
protected Void onLoad(Context context, Bundle args) throws Throwable {
try {
ServiceSynchronize.stop(getContext(), "folder");
@ -598,10 +453,10 @@ public class FragmentAccount extends FragmentEx {
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);
@ -640,43 +495,107 @@ public class FragmentAccount extends FragmentEx {
}
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;
protected void onLoaded(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) {
LoaderManager.getInstance(FragmentAccount.this).destroyLoader(loader.getId());
protected void onException(Bundle args, Throwable ex) {
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();
}
}.load(FragmentAccount.this, args);
}
});
ibDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder
.setMessage(R.string.title_account_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onLoaderReset(@NonNull Loader<Throwable> loader) {
public void onClick(DialogInterface dialog, int which) {
// TODO: spinner
getFragmentManager().popBackStack();
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
// To prevent foreign key constraints from triggering
long id = args.getLong("id");
ServiceSynchronize.stop(context, "delete account");
DB.getInstance(context).account().deleteAccount(id);
ServiceSynchronize.start(context);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentAccount.this, args);
}
})
.setNegativeButton(android.R.string.cancel, null).show();
}
});
// Initialize
Helper.setViewsEnabled(view, false);
tilPassword.setPasswordVisibilityToggleEnabled(id < 0);
btnCheck.setEnabled(false);
pbCheck.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
pbSave.setVisibility(View.GONE);
grpFolders.setVisibility(View.GONE);
ibDelete.setVisibility(View.GONE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id", -1));
final DB db = DB.getInstance(getContext());
// Observe
db.account().liveAccount(id).observe(getViewLifecycleOwner(), new Observer<EntityAccount>() {
@Override
public void onChanged(@Nullable EntityAccount account) {
etName.setText(account == null ? null : account.name);
etHost.setText(account == null ? null : account.host);
etPort.setText(account == null ? null : Long.toString(account.port));
etUser.setText(account == null ? null : account.user);
tilPassword.getEditText().setText(account == null ? null : account.password);
cbSynchronize.setChecked(account == null ? true : account.synchronize);
cbPrimary.setChecked(account == null ? true : account.primary);
cbPrimary.setEnabled(account == null ? true : account.synchronize);
ibDelete.setVisibility(account == null ? View.GONE : View.VISIBLE);
Helper.setViewsEnabled(view, true);
btnCheck.setEnabled(true);
pbWait.setVisibility(View.GONE);
}
});
}
};
}

@ -20,6 +20,7 @@ package eu.faircode.email;
*/
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
@ -96,43 +97,13 @@ public class FragmentCompose extends FragmentEx {
private AdapterAttachment adapter;
private boolean attaching = false;
private String action = null;
private long id = -1; // draft id
private long account = -1;
private long reference = -1;
private EntityMessage draft = null;
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
@Nullable
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);
view = (ViewGroup) inflater.inflate(R.layout.fragment_compose, container, false);
@ -278,48 +249,18 @@ public class FragmentCompose extends FragmentEx {
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DB.getInstance(getContext()).identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
@Override
public void onChanged(@Nullable final List<EntityIdentity> identities) {
Log.i(Helper.TAG, "Set identities=" + identities.size());
// 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);
// 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
if (draft == null) {
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);
args.putString("action", getArguments().getString("action"));
args.putLong("id", getArguments().getLong("id", -1));
args.putLong("account", getArguments().getLong("account", -1));
args.putLong("reference", getArguments().getLong("reference", -1));
draftLoader.load(FragmentCompose.this, args);
}
});
}
@Override
public void onPause() {
if (!attaching)
onAction(R.id.action_save);
super.onPause();
}
@ -333,7 +274,8 @@ public class FragmentCompose extends FragmentEx {
@Override
public void onPrepareOptionsMenu(Menu 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
@ -351,7 +293,6 @@ public class FragmentCompose extends FragmentEx {
}
private void onMenuAttachment() {
attaching = true;
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
@ -420,23 +361,23 @@ public class FragmentCompose extends FragmentEx {
private void handleAddAttachment(Intent data) {
Bundle args = new Bundle();
args.putLong("id", id);
args.putString("msgid", draft.msgid);
args.putParcelable("uri", data.getData());
new SimpleLoader<Void>() {
new SimpleTask<Void>() {
@Override
public Void onLoad(Bundle args) throws IOException {
protected Void onLoad(Context context, Bundle args) throws IOException {
Cursor cursor = null;
try {
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())
return null;
DB db = DB.getInstance(getContext());
DB db = DB.getInstance(context);
long id = args.getLong("id");
EntityMessage draft = db.message().getMessage(id);
String msgid = args.getString("msgid");
EntityMessage draft = db.message().getMessageByMsgId(msgid);
EntityAttachment attachment = new EntityAttachment();
attachment.message = draft.id;
@ -458,7 +399,7 @@ public class FragmentCompose extends FragmentEx {
InputStream is = null;
try {
is = getContext().getContentResolver().openInputStream(uri);
is = context.getContentResolver().openInputStream(uri);
ByteArrayOutputStream os = new ByteArrayOutputStream();
int len;
@ -490,24 +431,24 @@ public class FragmentCompose extends FragmentEx {
}
@Override
public void onLoaded(Bundle args, Void data) {
attaching = false;
protected void onLoaded(Bundle args, Void data) {
}
@Override
public void onException(Bundle args, Throwable ex) {
protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(this, ActivityCompose.LOADER_COMPOSE_ATTACHMENT, args);
}.load(this, args);
}
private void onAction(int action) {
Helper.setViewsEnabled(view, false);
bottom_navigation.getMenu().setGroupEnabled(0, false);
EntityIdentity identity = (EntityIdentity) spFrom.getSelectedItem();
Bundle args = new Bundle();
args.putLong("id", id);
args.putString("msgid", draft.msgid);
args.putInt("action", action);
args.putLong("identity", identity == null ? -1 : identity.id);
args.putString("to", etTo.getText().toString());
@ -516,19 +457,21 @@ public class FragmentCompose extends FragmentEx {
args.putString("subject", etSubject.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
public EntityMessage onLoad(Bundle args) {
protected EntityMessage onLoad(Context context, Bundle args) {
String action = args.getString("action");
long id = args.getLong("id", -1);
long account = args.getLong("account", -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);
if (draft == null) {
@ -550,6 +493,7 @@ public class FragmentCompose extends FragmentEx {
draft = new EntityMessage();
draft.account = account;
draft.folder = drafts.id;
draft.msgid = draft.generateMessageId();
if (ref != null) {
draft.thread = ref.thread;
@ -577,17 +521,17 @@ public class FragmentCompose extends FragmentEx {
}
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",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", draft.to)),
HtmlHelper.sanitize(getContext(), ref.body, true));
HtmlHelper.sanitize(context, ref.body, true));
} 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",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", ref.from)),
HtmlHelper.sanitize(getContext(), ref.body, true));
HtmlHelper.sanitize(context, ref.body, true));
}
}
@ -611,19 +555,39 @@ public class FragmentCompose extends FragmentEx {
db.endTransaction();
}
EntityOperation.process(getContext());
EntityOperation.process(context);
}
return draft;
}
@Override
public void onLoaded(Bundle args, EntityMessage draft) {
id = draft.id;
if ("new".equals(args.getString("action")))
action = "edit";
protected void onLoaded(Bundle args, EntityMessage draft) {
FragmentCompose.this.draft = draft;
Log.i(Helper.TAG, "Loaded draft id=" + draft.id + " msgid=" + draft.msgid);
DB db = DB.getInstance(getContext());
db.message().liveMessageByMsgId(draft.msgid).observe(getViewLifecycleOwner(), new Observer<EntityMessage>() {
boolean observed = false;
@Override
public void onChanged(final EntityMessage draft) {
// Message was deleted
if (draft == null) {
getFragmentManager().popBackStack();
return;
}
// New working copy
FragmentCompose.this.draft = draft;
Log.i(Helper.TAG, "Get loaded action=" + action + " id=" + id);
// Set controls only once
if (observed)
return;
observed = true;
String action = getArguments().getString("action");
getActivity().invalidateOptionsMenu();
pbWait.setVisibility(View.GONE);
@ -648,18 +612,6 @@ public class FragmentCompose extends FragmentEx {
etBcc.setText(draft.bcc == null ? null : TextUtils.join(", ", draft.bcc));
etSubject.setText(draft.subject);
DB db = DB.getInstance(getContext());
db.attachment().liveAttachments(draft.id).removeObservers(getViewLifecycleOwner());
db.attachment().liveAttachments(draft.id).observe(getViewLifecycleOwner(),
new Observer<List<TupleAttachment>>() {
@Override
public void onChanged(@Nullable List<TupleAttachment> attachments) {
adapter.set(attachments);
grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
}
});
etBody.setText(TextUtils.isEmpty(draft.body) ? null : Html.fromHtml(draft.body));
if ("edit".equals(action))
@ -670,19 +622,91 @@ public class FragmentCompose extends FragmentEx {
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
public void onChanged(@Nullable final List<EntityIdentity> identities) {
Log.i(Helper.TAG, "Set identities=" + identities.size());
// 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);
}
});
db.attachment().liveAttachments(draft.id).removeObservers(getViewLifecycleOwner());
db.attachment().liveAttachments(draft.id).observe(getViewLifecycleOwner(),
new Observer<List<TupleAttachment>>() {
@Override
public void onChanged(@Nullable List<TupleAttachment> attachments) {
adapter.set(attachments);
grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
}
});
}
});
}
@Override
public void onException(Bundle args, Throwable ex) {
protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
};
private SimpleLoader<EntityMessage> putLoader = new SimpleLoader<EntityMessage>() {
private SimpleTask<EntityMessage> actionLoader = new SimpleTask<EntityMessage>() {
@Override
public EntityMessage onLoad(Bundle args) throws Throwable {
protected EntityMessage onLoad(Context context, Bundle args) throws Throwable {
// Get data
long id = args.getLong("id");
String id = args.getString("msgid");
int action = args.getInt("action");
long iid = args.getLong("identity");
String to = args.getString("to");
@ -690,11 +714,10 @@ public class FragmentCompose extends FragmentEx {
String bcc = args.getString("bcc");
String subject = args.getString("subject");
String body = args.getString("body");
Log.i(Helper.TAG, "Put load action=" + action + " id=" + id);
// Get draft & selected identity
DB db = DB.getInstance(getContext());
EntityMessage draft = db.message().getMessage(id);
DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessageByMsgId(id);
EntityIdentity identity = db.identity().getIdentity(iid);
// Draft deleted by server
@ -702,6 +725,8 @@ public class FragmentCompose extends FragmentEx {
if (draft == null)
throw new MessageRemovedException();
Log.i(Helper.TAG, "Load action msgid=" + draft.msgid + " action=" + action);
// Convert data
Address afrom[] = (identity == null ? null : new Address[]{new InternetAddress(identity.email, identity.name)});
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);
} else if (action == R.id.action_save) {
// Save message ID
String msgid = draft.msgid;
// Save attachments
@ -748,7 +774,7 @@ public class FragmentCompose extends FragmentEx {
// Create new draft
draft.id = null;
draft.uid = null; // unique index folder/uid
draft.uid = null;
draft.msgid = msgid;
draft.ui_hide = false;
draft.id = db.message().insertMessage(draft);
@ -764,20 +790,22 @@ public class FragmentCompose extends FragmentEx {
} else if (action == R.id.action_send) {
// Check data
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)
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)
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);
for (EntityAttachment attachment : attachments)
attachment.content = db.attachment().getContent(attachment.id);
String msgid = draft.msgid;
// Delete draft (cannot move to outbox)
draft.msgid = null;
draft.ui_hide = true;
@ -792,6 +820,7 @@ public class FragmentCompose extends FragmentEx {
draft.ui_hide = false;
draft.id = db.message().insertMessage(draft);
// Restore attachments
for (EntityAttachment attachment : attachments) {
attachment.message = draft.id;
db.attachment().insertAttachment(attachment);
@ -805,17 +834,19 @@ public class FragmentCompose extends FragmentEx {
db.endTransaction();
}
EntityOperation.process(getContext());
EntityOperation.process(context);
return draft;
}
@Override
public void onLoaded(Bundle args, EntityMessage draft) {
id = draft.id;
protected void onLoaded(Bundle args, EntityMessage draft) {
FragmentCompose.this.draft = draft;
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);
if (action == R.id.action_trash) {
@ -830,7 +861,7 @@ public class FragmentCompose extends FragmentEx {
}
@Override
public void onException(Bundle args, Throwable ex) {
protected void onException(Bundle args, Throwable ex) {
bottom_navigation.getMenu().setGroupEnabled(0, true);
if (ex instanceof IllegalArgumentException)

@ -22,7 +22,6 @@ package eu.faircode.email;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -35,9 +34,6 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
public class FragmentFolder extends FragmentEx {
private ViewGroup view;
@ -77,61 +73,9 @@ public class FragmentFolder extends FragmentEx {
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putString("after", etAfter.getText().toString());
LoaderManager.getInstance(FragmentFolder.this)
.restartLoader(ActivityView.LOADER_FOLDER_PUT, args, putLoaderCallbacks).forceLoad();
}
});
// Initialize
Helper.setViewsEnabled(view, false);
btnSave.setEnabled(false);
pbSave.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id"));
// Observe
DB.getInstance(getContext()).folder().liveFolder(id).observe(getViewLifecycleOwner(), new Observer<EntityFolder>() {
@Override
public void onChanged(@Nullable EntityFolder folder) {
if (folder != null) {
cbSynchronize.setChecked(folder.synchronize);
etAfter.setText(Integer.toString(folder.after));
}
pbWait.setVisibility(View.GONE);
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
}
});
}
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();
}
new SimpleTask<Void>() {
@Override
public Throwable loadInBackground() {
protected Void onLoad(Context context, Bundle args) throws Throwable {
try {
ServiceSynchronize.stop(getContext(), "folder");
@ -151,42 +95,58 @@ public class FragmentFolder extends FragmentEx {
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;
protected void onLoaded(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) {
LoaderManager.getInstance(FragmentFolder.this).destroyLoader(loader.getId());
protected void onException(Bundle args, Throwable ex) {
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();
}
}.load(FragmentFolder.this, args);
}
});
// Initialize
Helper.setViewsEnabled(view, false);
btnSave.setEnabled(false);
pbSave.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
return view;
}
@Override
public void onLoaderReset(@NonNull Loader<Throwable> loader) {
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id"));
// Observe
DB.getInstance(getContext()).folder().liveFolder(id).observe(getViewLifecycleOwner(), new Observer<EntityFolder>() {
@Override
public void onChanged(@Nullable EntityFolder folder) {
if (folder != null) {
cbSynchronize.setChecked(folder.synchronize);
etAfter.setText(Integer.toString(folder.after));
}
pbWait.setVisibility(View.GONE);
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
}
});
}
};
}

@ -25,7 +25,6 @@ import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -53,9 +52,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.AsyncTaskLoader;
import androidx.loader.content.Loader;
public class FragmentIdentity extends FragmentEx {
private List<Provider> providers;
@ -200,125 +196,9 @@ public class FragmentIdentity extends FragmentEx {
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("primary", cbPrimary.isChecked());
LoaderManager.getInstance(FragmentIdentity.this)
.restartLoader(ActivityView.LOADER_IDENTITY_PUT, args, putLoaderCallbacks).forceLoad();
}
});
ibDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder
.setMessage(R.string.title_identity_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getFragmentManager().popBackStack();
// TODO: spinner
new SimpleLoader<Void>() {
@Override
public Void onLoad(Bundle args) throws Throwable {
DB.getInstance(getContext()).identity().deleteIdentity(id);
return null;
}
@Override
public void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentIdentity.this, ActivitySetup.LOADER_DELETE_IDENTITY, new Bundle());
}
})
.setNegativeButton(android.R.string.cancel, null).show();
}
});
// Initialize
Helper.setViewsEnabled(view, false);
tilPassword.setPasswordVisibilityToggleEnabled(id < 0);
btnSave.setEnabled(false);
pbSave.setVisibility(View.GONE);
ibDelete.setVisibility(View.GONE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id", -1));
final DB db = DB.getInstance(getContext());
// Observe identity
db.identity().liveIdentity(id).observe(getViewLifecycleOwner(), new Observer<EntityIdentity>() {
@Override
public void onChanged(@Nullable final EntityIdentity identity) {
etName.setText(identity == null ? null : identity.name);
etEmail.setText(identity == null ? null : identity.email);
etReplyTo.setText(identity == null ? null : identity.replyto);
etHost.setText(identity == null ? null : identity.host);
cbStartTls.setChecked(identity == null ? false : identity.starttls);
etPort.setText(identity == null ? null : Long.toString(identity.port));
etUser.setText(identity == null ? null : identity.user);
tilPassword.getEditText().setText(identity == null ? null : identity.password);
cbSynchronize.setChecked(identity == null ? true : identity.synchronize);
cbPrimary.setChecked(identity == null ? true : identity.primary);
cbPrimary.setEnabled(identity == null ? true : identity.synchronize);
ibDelete.setVisibility(identity == null ? View.GONE : View.VISIBLE);
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
pbWait.setVisibility(View.GONE);
etName.requestFocus();
db.account().liveAccounts().removeObservers(getViewLifecycleOwner());
db.account().liveAccounts().observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
@Override
public void onChanged(List<EntityAccount> accounts) {
EntityAccount unselected = new EntityAccount();
unselected.id = -1L;
unselected.name = "";
unselected.primary = false;
accounts.add(0, unselected);
ArrayAdapter<EntityAccount> adapterAccount = new ArrayAdapter<>(getContext(), R.layout.spinner_item, accounts);
adapterAccount.setDropDownViewResource(R.layout.spinner_dropdown_item);
spAccount.setAdapter(adapterAccount);
for (int pos = 0; pos < accounts.size(); pos++)
if (accounts.get(pos).id == (identity == null ? -1 : identity.account)) {
spAccount.setSelection(pos);
break;
}
}
});
}
});
}
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();
}
new SimpleTask<Void>() {
@Override
public Throwable loadInBackground() {
protected Void onLoad(Context context, Bundle args) throws Throwable {
try {
ServiceSynchronize.stop(getContext(), "account");
@ -397,42 +277,128 @@ public class FragmentIdentity extends FragmentEx {
}
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;
protected void onLoaded(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) {
LoaderManager.getInstance(FragmentIdentity.this).destroyLoader(loader.getId());
@Override
protected void onException(Bundle args, Throwable ex) {
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();
}
}.load(FragmentIdentity.this, args);
}
});
ibDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder
.setMessage(R.string.title_identity_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO: spinner
getFragmentManager().popBackStack();
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override
public void onLoaderReset(@NonNull Loader<Throwable> loader) {
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB.getInstance(context).identity().deleteIdentity(id);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentIdentity.this, args);
}
})
.setNegativeButton(android.R.string.cancel, null).show();
}
});
// Initialize
Helper.setViewsEnabled(view, false);
tilPassword.setPasswordVisibilityToggleEnabled(id < 0);
btnSave.setEnabled(false);
pbSave.setVisibility(View.GONE);
ibDelete.setVisibility(View.GONE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id", -1));
final DB db = DB.getInstance(getContext());
// Observe identity
db.identity().liveIdentity(id).observe(getViewLifecycleOwner(), new Observer<EntityIdentity>() {
@Override
public void onChanged(@Nullable final EntityIdentity identity) {
etName.setText(identity == null ? null : identity.name);
etEmail.setText(identity == null ? null : identity.email);
etReplyTo.setText(identity == null ? null : identity.replyto);
etHost.setText(identity == null ? null : identity.host);
cbStartTls.setChecked(identity == null ? false : identity.starttls);
etPort.setText(identity == null ? null : Long.toString(identity.port));
etUser.setText(identity == null ? null : identity.user);
tilPassword.getEditText().setText(identity == null ? null : identity.password);
cbSynchronize.setChecked(identity == null ? true : identity.synchronize);
cbPrimary.setChecked(identity == null ? true : identity.primary);
cbPrimary.setEnabled(identity == null ? true : identity.synchronize);
ibDelete.setVisibility(identity == null ? View.GONE : View.VISIBLE);
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
pbWait.setVisibility(View.GONE);
etName.requestFocus();
db.account().liveAccounts().removeObservers(getViewLifecycleOwner());
db.account().liveAccounts().observe(getViewLifecycleOwner(), new Observer<List<EntityAccount>>() {
@Override
public void onChanged(List<EntityAccount> accounts) {
EntityAccount unselected = new EntityAccount();
unselected.id = -1L;
unselected.name = "";
unselected.primary = false;
accounts.add(0, unselected);
ArrayAdapter<EntityAccount> adapterAccount = new ArrayAdapter<>(getContext(), R.layout.spinner_item, accounts);
adapterAccount.setDropDownViewResource(R.layout.spinner_dropdown_item);
spAccount.setAdapter(adapterAccount);
for (int pos = 0; pos < accounts.size(); pos++)
if (accounts.get(pos).id == (identity == null ? -1 : identity.account)) {
spAccount.setSelection(pos);
break;
}
}
});
}
});
}
};
}

@ -62,9 +62,6 @@ import androidx.browser.customtabs.CustomTabsIntent;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.FragmentTransaction;
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.RecyclerView;
@ -402,11 +399,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleLoader<Void>() {
new SimpleTask<Void>() {
@Override
public Void onLoad(Bundle args) {
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(getContext());
DB db = DB.getInstance(context);
try {
db.beginTransaction();
@ -422,13 +419,13 @@ public class FragmentMessage extends FragmentEx {
db.endTransaction();
}
EntityOperation.process(getContext());
EntityOperation.process(context);
return null;
}
@Override
public void onLoaded(Bundle args, Void data) {
protected void onLoaded(Bundle args, Void data) {
item.setEnabled(true);
item.setIcon(icon);
}
@ -439,7 +436,7 @@ public class FragmentMessage extends FragmentEx {
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(this, ActivityView.LOADER_MESSAGE_SEEN, args);
}.load(this, args);
}
private void onActionEdit(long id) {
@ -452,11 +449,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleLoader<Long>() {
new SimpleTask<Long>() {
@Override
public Long onLoad(Bundle args) {
protected Long onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(getContext());
DB db = DB.getInstance(context);
EntityMessage draft = db.message().getMessage(id);
EntityFolder drafts = db.folder().getFolderByType(draft.account, EntityFolder.DRAFTS);
draft.id = null;
@ -467,7 +464,7 @@ public class FragmentMessage extends FragmentEx {
}
@Override
public void onLoaded(Bundle args, Long id) {
protected void onLoaded(Bundle args, Long id) {
item.setEnabled(true);
item.setIcon(icon);
getContext().startActivity(
@ -482,7 +479,7 @@ public class FragmentMessage extends FragmentEx {
item.setIcon(icon);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(this, ActivityView.LOADER_MESSAGE_EDIT, args);
}.load(this, args);
}
private void onActionForward(long id) {
@ -513,11 +510,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleLoader<Void>() {
new SimpleTask<Void>() {
@Override
public Void onLoad(Bundle args) {
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(getContext());
DB db = DB.getInstance(context);
try {
db.beginTransaction();
@ -533,24 +530,24 @@ public class FragmentMessage extends FragmentEx {
db.endTransaction();
}
EntityOperation.process(getContext());
EntityOperation.process(context);
return null;
}
@Override
public void onLoaded(Bundle args, Void result) {
protected void onLoaded(Bundle args, Void result) {
item.setEnabled(true);
item.setIcon(icon);
}
@Override
public void onException(Bundle args, Throwable ex) {
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, ActivityView.LOADER_MESSAGE_SPAM, args);
}.load(FragmentMessage.this, args);
}
})
.setNegativeButton(android.R.string.cancel, null).show();
@ -575,11 +572,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleLoader<Void>() {
new SimpleTask<Void>() {
@Override
public Void onLoad(Bundle args) {
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(getContext());
DB db = DB.getInstance(context);
try {
db.beginTransaction();
@ -593,24 +590,24 @@ public class FragmentMessage extends FragmentEx {
db.endTransaction();
}
EntityOperation.process(getContext());
EntityOperation.process(context);
return null;
}
@Override
public void onLoaded(Bundle args, Void result) {
protected void onLoaded(Bundle args, Void result) {
item.setEnabled(true);
item.setIcon(icon);
}
@Override
public void onException(Bundle args, Throwable ex) {
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, ActivityView.LOADER_MESSAGE_TRASH, args);
}.load(FragmentMessage.this, args);
}
})
.setNegativeButton(android.R.string.cancel, null).show();
@ -624,11 +621,11 @@ public class FragmentMessage extends FragmentEx {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleLoader<Void>() {
new SimpleTask<Void>() {
@Override
public Void onLoad(Bundle args) {
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(getContext());
DB db = DB.getInstance(context);
try {
db.beginTransaction();
@ -644,107 +641,34 @@ public class FragmentMessage extends FragmentEx {
db.endTransaction();
}
EntityOperation.process(getContext());
EntityOperation.process(context);
return null;
}
@Override
public void onLoaded(Bundle args, Void result) {
protected void onLoaded(Bundle args, Void result) {
item.setEnabled(true);
item.setIcon(icon);
}
@Override
public void onException(Bundle args, Throwable ex) {
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, ActivityView.LOADER_MESSAGE_TRASH, args);
}.load(FragmentMessage.this, args);
}
}
private void onActionMove(long id) {
Bundle args = new Bundle();
args.putLong("id", id);
LoaderManager.getInstance(this)
.restartLoader(ActivityView.LOADER_MESSAGE_MOVE, args, moveLoaderCallbacks).forceLoad();
}
private void onActionArchive(long id) {
final MenuItem item = bottom_navigation.getMenu().findItem(R.id.action_archive);
item.setEnabled(false);
final Drawable icon = item.getIcon();
item.setIcon(Helper.toDimmed(icon));
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleLoader<Void>() {
@Override
public Void onLoad(Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(getContext());
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
message.ui_hide = true;
db.message().updateMessage(message);
EntityFolder archive = db.folder().getFolderByType(message.account, EntityFolder.ARCHIVE);
EntityOperation.queue(db, message, EntityOperation.MOVE, archive.id);
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_ARCHIVE, args);
}
private void onActionReply(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "reply")
.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();
}
new SimpleTask<List<EntityFolder>>() {
@Override
public List<EntityFolder> loadInBackground() {
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);
@ -771,23 +695,12 @@ public class FragmentMessage extends FragmentEx {
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());
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++,
@ -802,15 +715,14 @@ public class FragmentMessage extends FragmentEx {
final Drawable icon = item.getIcon();
item.setIcon(Helper.toDimmed(icon));
Bundle args = ((MoveLoader) loader).args;
args.putLong("target", target.getItemId());
new SimpleLoader<Void>() {
new SimpleTask<Void>() {
@Override
public Void onLoad(Bundle args) {
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
long target = args.getLong("target");
DB db = DB.getInstance(getContext());
DB db = DB.getInstance(context);
try {
db.beginTransaction();
@ -825,24 +737,24 @@ public class FragmentMessage extends FragmentEx {
db.endTransaction();
}
EntityOperation.process(getContext());
EntityOperation.process(context);
return null;
}
@Override
public void onLoaded(Bundle args, Void result) {
protected void onLoaded(Bundle args, Void result) {
item.setEnabled(true);
item.setIcon(icon);
}
@Override
public void onException(Bundle args, Throwable ex) {
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, ActivityView.LOADER_MESSAGE_MOVE, args);
}.load(FragmentMessage.this, args);
return true;
}
@ -850,9 +762,62 @@ public class FragmentMessage extends FragmentEx {
popupMenu.show();
}
}.load(FragmentMessage.this, args);
}
private void onActionArchive(long id) {
final MenuItem item = bottom_navigation.getMenu().findItem(R.id.action_archive);
item.setEnabled(false);
final Drawable icon = item.getIcon();
item.setIcon(Helper.toDimmed(icon));
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override
public void onLoaderReset(@NonNull Loader<List<EntityFolder>> loader) {
protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
message.ui_hide = true;
db.message().updateMessage(message);
EntityFolder archive = db.folder().getFolderByType(message.account, EntityFolder.ARCHIVE);
EntityOperation.queue(db, message, EntityOperation.MOVE, archive.id);
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);
}
private void onActionReply(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "reply")
.putExtra("reference", id));
}
};
}

@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
@ -150,13 +151,13 @@ public class FragmentMessages extends FragmentEx {
}
});
new SimpleLoader<Long>() {
new SimpleTask<Long>() {
@Override
public Long onLoad(Bundle args) {
protected Long onLoad(Context context, Bundle args) {
long folder = (args == null ? -1 : args.getLong("folder", -1));
long thread = (args == null ? -1 : args.getLong("thread", -1)); // message ID
DB db = DB.getInstance(getContext());
DB db = DB.getInstance(context);
Long account;
if (thread < 0)
@ -174,15 +175,15 @@ public class FragmentMessages extends FragmentEx {
}
@Override
public void onLoaded(Bundle args, Long account) {
protected void onLoaded(Bundle args, Long account) {
fab.setTag(account);
fab.setVisibility(View.VISIBLE);
}
@Override
public void onException(Bundle args, Throwable ex) {
protected void onException(Bundle args, Throwable ex) {
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.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
@ -152,10 +153,10 @@ public class FragmentSetup extends FragmentEx {
onRequestPermissionsResult(0, permissions, grantResults);
// Create outbox
new SimpleLoader<Void>() {
new SimpleTask<Void>() {
@Override
public Void onLoad(Bundle args) throws Throwable {
DB db = DB.getInstance(getContext());
protected Void onLoad(Context context, Bundle args) throws Throwable {
DB db = DB.getInstance(context);
EntityFolder outbox = db.folder().getOutbox();
if (outbox == null) {
outbox = new EntityFolder();
@ -169,10 +170,10 @@ public class FragmentSetup extends FragmentEx {
}
@Override
public void onException(Bundle args, Throwable ex) {
protected void onException(Bundle args, Throwable ex) {
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(this, ActivitySetup.LOADER_CREATE_OUTBOX, new Bundle());
}.load(this, new Bundle());
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