mirror of https://github.com/M66B/FairEmail.git
Refactoring
This commit is contained in:
parent
ac9e9efcbd
commit
9a1ee17165
|
@ -211,85 +211,26 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB
|
||||||
Intent.ACTION_SEND_MULTIPLE.equals(action));
|
Intent.ACTION_SEND_MULTIPLE.equals(action));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void undoSend(long id, final Context context, final LifecycleOwner owner, final FragmentManager manager) {
|
static void undoSend(final long id, final Context context, final LifecycleOwner owner, final FragmentManager manager) {
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
args.putLong("id", id);
|
args.putLong("id", id);
|
||||||
|
|
||||||
new SimpleTask<EntityMessage>() {
|
new SimpleTask<Long>() {
|
||||||
@Override
|
@Override
|
||||||
protected EntityMessage onExecute(Context context, Bundle args) {
|
protected Long onExecute(Context context, Bundle args) {
|
||||||
long id = args.getLong("id");
|
long id = args.getLong("id");
|
||||||
|
return undoSend(id, context);
|
||||||
DB db = DB.getInstance(context);
|
|
||||||
|
|
||||||
EntityOperation operation = db.operation().getOperation(id, EntityOperation.SEND);
|
|
||||||
if (operation != null)
|
|
||||||
if ("executing".equals(operation.state)) {
|
|
||||||
// Trigger update
|
|
||||||
db.message().setMessageUiBusy(id, new Date().getTime());
|
|
||||||
return null;
|
|
||||||
} else
|
|
||||||
db.operation().deleteOperation(operation.id);
|
|
||||||
|
|
||||||
EntityMessage message;
|
|
||||||
|
|
||||||
try {
|
|
||||||
db.beginTransaction();
|
|
||||||
|
|
||||||
message = db.message().getMessage(id);
|
|
||||||
if (message == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
db.folder().setFolderError(message.folder, null);
|
|
||||||
if (message.identity != null)
|
|
||||||
db.identity().setIdentityError(message.identity, null);
|
|
||||||
|
|
||||||
File source = message.getFile(context);
|
|
||||||
|
|
||||||
// Insert into drafts
|
|
||||||
EntityFolder drafts = db.folder().getFolderByType(message.account, EntityFolder.DRAFTS);
|
|
||||||
if (drafts == null)
|
|
||||||
throw new IllegalArgumentException(context.getString(R.string.title_no_drafts));
|
|
||||||
|
|
||||||
message.id = null;
|
|
||||||
message.folder = drafts.id;
|
|
||||||
message.fts = false;
|
|
||||||
message.ui_snoozed = null;
|
|
||||||
message.error = null;
|
|
||||||
message.id = db.message().insertMessage(message);
|
|
||||||
|
|
||||||
File target = message.getFile(context);
|
|
||||||
source.renameTo(target);
|
|
||||||
|
|
||||||
List<EntityAttachment> attachments = db.attachment().getAttachments(id);
|
|
||||||
for (EntityAttachment attachment : attachments)
|
|
||||||
db.attachment().setMessage(attachment.id, message.id);
|
|
||||||
|
|
||||||
EntityOperation.queue(context, message, EntityOperation.ADD);
|
|
||||||
|
|
||||||
// Delete from outbox
|
|
||||||
db.message().deleteMessage(id); // will delete operation too
|
|
||||||
|
|
||||||
db.setTransactionSuccessful();
|
|
||||||
} finally {
|
|
||||||
db.endTransaction();
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceSynchronize.eval(context, "outbox/drafts");
|
|
||||||
|
|
||||||
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
nm.cancel("send:" + id, 1);
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onExecuted(Bundle args, EntityMessage draft) {
|
protected void onExecuted(Bundle args, Long id) {
|
||||||
if (draft != null)
|
if (id == null)
|
||||||
context.startActivity(
|
return;
|
||||||
new Intent(context, ActivityCompose.class)
|
|
||||||
.putExtra("action", "edit")
|
context.startActivity(
|
||||||
.putExtra("id", draft.id));
|
new Intent(context, ActivityCompose.class)
|
||||||
|
.putExtra("action", "edit")
|
||||||
|
.putExtra("id", id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -298,4 +239,68 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB
|
||||||
}
|
}
|
||||||
}.execute(context, owner, args, "undo:sent");
|
}.execute(context, owner, args, "undo:sent");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Long undoSend(long id, Context context) {
|
||||||
|
DB db = DB.getInstance(context);
|
||||||
|
|
||||||
|
EntityOperation operation = db.operation().getOperation(id, EntityOperation.SEND);
|
||||||
|
if (operation != null)
|
||||||
|
if ("executing".equals(operation.state)) {
|
||||||
|
// Trigger update
|
||||||
|
db.message().setMessageUiBusy(id, new Date().getTime());
|
||||||
|
return null;
|
||||||
|
} else
|
||||||
|
db.operation().deleteOperation(operation.id);
|
||||||
|
|
||||||
|
EntityMessage message;
|
||||||
|
|
||||||
|
try {
|
||||||
|
db.beginTransaction();
|
||||||
|
|
||||||
|
message = db.message().getMessage(id);
|
||||||
|
if (message == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
db.folder().setFolderError(message.folder, null);
|
||||||
|
if (message.identity != null)
|
||||||
|
db.identity().setIdentityError(message.identity, null);
|
||||||
|
|
||||||
|
File source = message.getFile(context);
|
||||||
|
|
||||||
|
// Insert into drafts
|
||||||
|
EntityFolder drafts = db.folder().getFolderByType(message.account, EntityFolder.DRAFTS);
|
||||||
|
if (drafts == null)
|
||||||
|
throw new IllegalArgumentException(context.getString(R.string.title_no_drafts));
|
||||||
|
|
||||||
|
message.id = null;
|
||||||
|
message.folder = drafts.id;
|
||||||
|
message.fts = false;
|
||||||
|
message.ui_snoozed = null;
|
||||||
|
message.error = null;
|
||||||
|
message.id = db.message().insertMessage(message);
|
||||||
|
|
||||||
|
File target = message.getFile(context);
|
||||||
|
source.renameTo(target);
|
||||||
|
|
||||||
|
List<EntityAttachment> attachments = db.attachment().getAttachments(id);
|
||||||
|
for (EntityAttachment attachment : attachments)
|
||||||
|
db.attachment().setMessage(attachment.id, message.id);
|
||||||
|
|
||||||
|
EntityOperation.queue(context, message, EntityOperation.ADD);
|
||||||
|
|
||||||
|
// Delete from outbox
|
||||||
|
db.message().deleteMessage(id); // will delete operation too
|
||||||
|
|
||||||
|
db.setTransactionSuccessful();
|
||||||
|
} finally {
|
||||||
|
db.endTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
ServiceSynchronize.eval(context, "outbox/drafts");
|
||||||
|
|
||||||
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
nm.cancel("send:" + id, 1);
|
||||||
|
|
||||||
|
return message.id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -746,29 +746,21 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undo(String title, Runnable move, Runnable show) {
|
public void undo(String title, final Bundle args, final SimpleTask<Void> move, final SimpleTask<Void> show) {
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
int undo_timeout = prefs.getInt("undo_timeout", 5000);
|
int undo_timeout = prefs.getInt("undo_timeout", 5000);
|
||||||
|
|
||||||
if (undo_timeout == 0)
|
if (undo_timeout == 0)
|
||||||
try {
|
move.execute(this, args, "undo:move");
|
||||||
move.run();
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
Log.e(ex);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
undo(undo_timeout, title, move, show);
|
undo(undo_timeout, title, args, move, show);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undo(long undo_timeout, String title, final Runnable move, final Runnable show) {
|
public void undo(long undo_timeout, String title, final Bundle args, final SimpleTask move, final SimpleTask show) {
|
||||||
if (drawerLayout == null || drawerLayout.getChildCount() == 0) {
|
if (drawerLayout == null || drawerLayout.getChildCount() == 0) {
|
||||||
Log.e("Undo: drawer missing");
|
Log.e("Undo: drawer missing");
|
||||||
if (show != null)
|
if (show != null)
|
||||||
try {
|
show.execute(this, args, "undo:show");
|
||||||
show.run();
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
Log.e(ex);
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -785,11 +777,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
|
||||||
Log.i("Undo timeout");
|
Log.i("Undo timeout");
|
||||||
snackbar.dismiss();
|
snackbar.dismiss();
|
||||||
if (move != null)
|
if (move != null)
|
||||||
try {
|
move.execute(ActivityView.this, args, "undo:move");
|
||||||
move.run();
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
Log.e(ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -800,11 +788,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
|
||||||
getMainHandler().removeCallbacks(timeout);
|
getMainHandler().removeCallbacks(timeout);
|
||||||
snackbar.dismiss();
|
snackbar.dismiss();
|
||||||
if (show != null)
|
if (show != null)
|
||||||
try {
|
show.execute(ActivityView.this, args, "undo:show");
|
||||||
show.run();
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
Log.e(ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1327,6 +1311,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onUndoSend(Intent intent) {
|
private void onUndoSend(Intent intent) {
|
||||||
|
long id = intent.getLongExtra("id", -1);
|
||||||
int delayed = intent.getIntExtra("delayed", 0);
|
int delayed = intent.getIntExtra("delayed", 0);
|
||||||
long scheduled = intent.getLongExtra("scheduled", 0);
|
long scheduled = intent.getLongExtra("scheduled", 0);
|
||||||
long now = new Date().getTime();
|
long now = new Date().getTime();
|
||||||
|
@ -1338,13 +1323,31 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
|
||||||
if (delayed * 1000L < UNDO_SEND_DELAY * 2 || scheduled - now < UNDO_SEND_DELAY * 2)
|
if (delayed * 1000L < UNDO_SEND_DELAY * 2 || scheduled - now < UNDO_SEND_DELAY * 2)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
undo(UNDO_SEND_DELAY, getString(R.string.title_sending), null, new Runnable() {
|
SimpleTask<Long> task = new SimpleTask<Long>() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
protected Long onExecute(Context context, Bundle args) {
|
||||||
long id = intent.getLongExtra("id", -1);
|
long id = args.getLong("id");
|
||||||
ActivityCompose.undoSend(id, ActivityView.this, ActivityView.this, getSupportFragmentManager());
|
return ActivityCompose.undoSend(id, context);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
@Override
|
||||||
|
protected void onExecuted(Bundle args, Long id) {
|
||||||
|
if (id == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
startActivity(
|
||||||
|
new Intent(ActivityView.this, ActivityCompose.class)
|
||||||
|
.putExtra("action", "edit")
|
||||||
|
.putExtra("id", id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onException(Bundle args, Throwable ex) {
|
||||||
|
Log.unexpectedError(getSupportFragmentManager(), ex, !(ex instanceof IllegalArgumentException));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
undo(UNDO_SEND_DELAY, getString(R.string.title_sending), intent.getExtras(), null, task);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BroadcastReceiver receiver = new BroadcastReceiver() {
|
private BroadcastReceiver receiver = new BroadcastReceiver() {
|
||||||
|
|
|
@ -5399,82 +5399,74 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SimpleTask<Void> move = new SimpleTask<Void>() {
|
||||||
|
@Override
|
||||||
|
protected Void onExecute(Context context, Bundle args) {
|
||||||
|
ArrayList<MessageTarget> result = args.getParcelableArrayList("result");
|
||||||
|
|
||||||
|
DB db = DB.getInstance(context);
|
||||||
|
try {
|
||||||
|
db.beginTransaction();
|
||||||
|
|
||||||
|
for (MessageTarget target : result) {
|
||||||
|
EntityMessage message = db.message().getMessage(target.id);
|
||||||
|
if (message == null || !message.ui_hide)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Log.i("Move id=" + target.id + " target=" + target.targetFolder.name);
|
||||||
|
db.message().setMessageUiBusy(target.id, null);
|
||||||
|
db.message().setMessageLastAttempt(target.id, new Date().getTime());
|
||||||
|
EntityOperation.queue(context, message, EntityOperation.MOVE, target.targetFolder.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.setTransactionSuccessful();
|
||||||
|
} finally {
|
||||||
|
db.endTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
ServiceSynchronize.eval(context, "move");
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onException(Bundle args, Throwable ex) {
|
||||||
|
Log.e(ex);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
SimpleTask<Void> show = new SimpleTask<Void>() {
|
||||||
|
@Override
|
||||||
|
protected Void onExecute(Context context, Bundle args) {
|
||||||
|
ArrayList<MessageTarget> result = args.getParcelableArrayList("result");
|
||||||
|
|
||||||
|
DB db = DB.getInstance(context);
|
||||||
|
try {
|
||||||
|
db.beginTransaction();
|
||||||
|
|
||||||
|
for (MessageTarget target : result) {
|
||||||
|
Log.i("Move undo id=" + target.id);
|
||||||
|
db.message().setMessageUiBusy(target.id, null);
|
||||||
|
db.message().setMessageUiHide(target.id, false);
|
||||||
|
db.message().setMessageLastAttempt(target.id, new Date().getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
db.setTransactionSuccessful();
|
||||||
|
} finally {
|
||||||
|
db.endTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onException(Bundle args, Throwable ex) {
|
||||||
|
Log.e(ex);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
String title = getString(R.string.title_move_undo, getDisplay(result, true), result.size());
|
String title = getString(R.string.title_move_undo, getDisplay(result, true), result.size());
|
||||||
((ActivityView) activity).undo(title,
|
((ActivityView) activity).undo(title, args, move, show);
|
||||||
new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
new SimpleTask<Void>() {
|
|
||||||
@Override
|
|
||||||
protected Void onExecute(Context context, Bundle args) {
|
|
||||||
ArrayList<MessageTarget> result = args.getParcelableArrayList("result");
|
|
||||||
|
|
||||||
DB db = DB.getInstance(context);
|
|
||||||
try {
|
|
||||||
db.beginTransaction();
|
|
||||||
|
|
||||||
for (MessageTarget target : result) {
|
|
||||||
EntityMessage message = db.message().getMessage(target.id);
|
|
||||||
if (message == null || !message.ui_hide)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Log.i("Move id=" + target.id + " target=" + target.targetFolder.name);
|
|
||||||
db.message().setMessageUiBusy(target.id, null);
|
|
||||||
db.message().setMessageLastAttempt(target.id, new Date().getTime());
|
|
||||||
EntityOperation.queue(context, message, EntityOperation.MOVE, target.targetFolder.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
db.setTransactionSuccessful();
|
|
||||||
} finally {
|
|
||||||
db.endTransaction();
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceSynchronize.eval(context, "move");
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onException(Bundle args, Throwable ex) {
|
|
||||||
Log.e(ex);
|
|
||||||
}
|
|
||||||
}.execute(FragmentMessages.this, args, "undo:move");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
new SimpleTask<Void>() {
|
|
||||||
@Override
|
|
||||||
protected Void onExecute(Context context, Bundle args) {
|
|
||||||
ArrayList<MessageTarget> result = args.getParcelableArrayList("result");
|
|
||||||
|
|
||||||
DB db = DB.getInstance(context);
|
|
||||||
try {
|
|
||||||
db.beginTransaction();
|
|
||||||
|
|
||||||
for (MessageTarget target : result) {
|
|
||||||
Log.i("Move undo id=" + target.id);
|
|
||||||
db.message().setMessageUiBusy(target.id, null);
|
|
||||||
db.message().setMessageUiHide(target.id, false);
|
|
||||||
db.message().setMessageLastAttempt(target.id, new Date().getTime());
|
|
||||||
}
|
|
||||||
|
|
||||||
db.setTransactionSuccessful();
|
|
||||||
} finally {
|
|
||||||
db.endTransaction();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onException(Bundle args, Throwable ex) {
|
|
||||||
Log.e(ex);
|
|
||||||
}
|
|
||||||
}.execute(FragmentMessages.this, args, "undo:show");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue