Delete DraftCreatedEvent and CreateAndPostDraftJob
The listeners of DraftCreatedEvent have been deleted too, as the actions they are taking when receiving back the draft's creation result should not cause any actual changes locally that wouldn't be done anyways by updating the draft
This commit is contained in:
parent
5380a01bc1
commit
4ca172ca58
|
@ -24,7 +24,6 @@ import android.content.Intent;
|
|||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
|
@ -49,7 +48,6 @@ import com.squareup.otto.Subscribe;
|
|||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
@ -61,16 +59,13 @@ import butterknife.BindView;
|
|||
import ch.protonmail.android.R;
|
||||
import ch.protonmail.android.activities.guest.LoginActivity;
|
||||
import ch.protonmail.android.adapters.AttachmentListAdapter;
|
||||
import ch.protonmail.android.api.models.room.messages.Attachment;
|
||||
import ch.protonmail.android.api.models.room.messages.LocalAttachment;
|
||||
import ch.protonmail.android.api.models.room.messages.Message;
|
||||
import ch.protonmail.android.api.models.room.messages.MessagesDatabase;
|
||||
import ch.protonmail.android.api.models.room.messages.MessagesDatabaseFactory;
|
||||
import ch.protonmail.android.attachments.ImportAttachmentsWorker;
|
||||
import ch.protonmail.android.core.Constants;
|
||||
import ch.protonmail.android.core.ProtonMailApplication;
|
||||
import ch.protonmail.android.events.DownloadedAttachmentEvent;
|
||||
import ch.protonmail.android.events.DraftCreatedEvent;
|
||||
import ch.protonmail.android.events.LogoutEvent;
|
||||
import ch.protonmail.android.events.PostImportAttachmentEvent;
|
||||
import ch.protonmail.android.events.PostImportAttachmentFailureEvent;
|
||||
|
@ -83,7 +78,6 @@ import ch.protonmail.android.utils.Logger;
|
|||
import ch.protonmail.android.utils.extensions.TextExtensions;
|
||||
import ch.protonmail.android.utils.ui.dialogs.DialogUtils;
|
||||
import dagger.hilt.android.AndroidEntryPoint;
|
||||
import kotlin.Unit;
|
||||
import kotlin.collections.ArraysKt;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import timber.log.Timber;
|
||||
|
@ -350,31 +344,6 @@ public class AddAttachmentsActivity extends BaseStoragePermissionActivity implem
|
|||
TextExtensions.showToast(this, R.string.problem_selecting_file);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onDraftCreatedEvent(DraftCreatedEvent event) {
|
||||
if (event == null) {
|
||||
return;
|
||||
}
|
||||
if (mDraftId == null || !mDraftId.equals(event.getOldMessageId())) {
|
||||
return;
|
||||
}
|
||||
mDraftCreated = true;
|
||||
invalidateOptionsMenu();
|
||||
mProgressLayout.setVisibility(View.GONE);
|
||||
String newMessageId = event.getMessageId();
|
||||
if (event.getStatus() != Status.NO_NETWORK) {
|
||||
if (!TextUtils.isEmpty(mDraftId) && !TextUtils.isEmpty(newMessageId)) {
|
||||
new DeleteMessageByIdTask(messagesDatabase, mDraftId).execute();
|
||||
}
|
||||
}
|
||||
mDraftId = event.getMessageId();
|
||||
if (event.getStatus() == Status.SUCCESS) {
|
||||
final Message eventMessage = event.getMessage();
|
||||
new UpdateAttachmentsAdapterTask(new WeakReference<>(this), eventMessage,
|
||||
AddAttachmentsActivity.this.messagesDatabase, mAdapter).execute();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
super.onConfigurationChanged(newConfig);
|
||||
|
@ -571,60 +540,6 @@ public class AddAttachmentsActivity extends BaseStoragePermissionActivity implem
|
|||
return embeddedImages;
|
||||
}
|
||||
|
||||
private static class DeleteMessageByIdTask extends AsyncTask<Unit, Unit, Unit> {
|
||||
|
||||
private final MessagesDatabase messagesDatabase;
|
||||
private final String draftId;
|
||||
|
||||
private DeleteMessageByIdTask(MessagesDatabase messagesDatabase, String draftId) {
|
||||
this.messagesDatabase = messagesDatabase;
|
||||
this.draftId = draftId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Unit doInBackground(Unit... units) {
|
||||
messagesDatabase.deleteMessageById(draftId);
|
||||
return Unit.INSTANCE;
|
||||
}
|
||||
}
|
||||
|
||||
private static class UpdateAttachmentsAdapterTask extends AsyncTask<Unit, Unit, List<Attachment>> {
|
||||
private final WeakReference<AddAttachmentsActivity> addAttachmentsActivityWeakReference;
|
||||
private final MessagesDatabase messagesDatabase;
|
||||
private final AttachmentListAdapter adapter;
|
||||
private final Message eventMessage;
|
||||
|
||||
UpdateAttachmentsAdapterTask(
|
||||
WeakReference<AddAttachmentsActivity> addAttachmentsActivityWeakReference, Message eventMessage, MessagesDatabase messagesDatabase,
|
||||
AttachmentListAdapter adapter) {
|
||||
this.addAttachmentsActivityWeakReference = addAttachmentsActivityWeakReference;
|
||||
this.messagesDatabase = messagesDatabase;
|
||||
this.adapter = adapter;
|
||||
this.eventMessage = eventMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Attachment> doInBackground(Unit... units) {
|
||||
return eventMessage.attachments(messagesDatabase);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<Attachment> messageAttachments) {
|
||||
AddAttachmentsActivity addAttachmentsActivity = addAttachmentsActivityWeakReference.get();
|
||||
if (addAttachmentsActivity == null) {
|
||||
return;
|
||||
}
|
||||
int attachmentsCount = messageAttachments.size();
|
||||
if (adapter.getData().size() <= attachmentsCount) {
|
||||
|
||||
ArrayList<LocalAttachment> attachments = new ArrayList<>(LocalAttachment.Companion.createLocalAttachmentList(messageAttachments));
|
||||
int totalEmbeddedImages = addAttachmentsActivity.countEmbeddedImages(attachments);
|
||||
addAttachmentsActivity.updateAttachmentsCount(attachmentsCount, totalEmbeddedImages);
|
||||
adapter.updateData(attachments, totalEmbeddedImages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean openGallery() {
|
||||
if (!isAttachmentsCountAllowed()) {
|
||||
TextExtensions.showToast(this, R.string.max_attachments_reached);
|
||||
|
|
|
@ -1198,13 +1198,11 @@ public class ComposeMessageActivity
|
|||
unit -> {
|
||||
UiUtil.hideKeyboard(this);
|
||||
composeMessageViewModel.finishBuildingMessage(mComposeBodyEditText.getText().toString());
|
||||
ProtonMailApplication.getApplication().resetDraftCreated();
|
||||
return unit;
|
||||
}, true);
|
||||
} else {
|
||||
UiUtil.hideKeyboard(this);
|
||||
composeMessageViewModel.finishBuildingMessage(mComposeBodyEditText.getText().toString());
|
||||
ProtonMailApplication.getApplication().resetDraftCreated();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,7 +87,6 @@ import ch.protonmail.android.api.services.MessagesService;
|
|||
import ch.protonmail.android.events.ApiOfflineEvent;
|
||||
import ch.protonmail.android.events.AuthStatus;
|
||||
import ch.protonmail.android.events.DownloadedAttachmentEvent;
|
||||
import ch.protonmail.android.events.DraftCreatedEvent;
|
||||
import ch.protonmail.android.events.ForceUpgradeEvent;
|
||||
import ch.protonmail.android.events.InvalidAccessTokenEvent;
|
||||
import ch.protonmail.android.events.Login2FAEvent;
|
||||
|
@ -161,7 +160,6 @@ public class ProtonMailApplication extends Application implements androidx.work.
|
|||
private Snackbar apiOfflineSnackBar;
|
||||
@Nullable
|
||||
private StorageLimitEvent mLastStorageLimitEvent;
|
||||
private DraftCreatedEvent mLastDraftCreatedEvent;
|
||||
private WeakReference<Activity> mCurrentActivity;
|
||||
private boolean mUpdateOccurred;
|
||||
private AllCurrencyPlans mAllCurrencyPlans;
|
||||
|
@ -302,20 +300,6 @@ public class ProtonMailApplication extends Application implements androidx.work.
|
|||
return latestEvent;
|
||||
}
|
||||
|
||||
@Produce
|
||||
public DraftCreatedEvent produceDraftCreatedEvent() {
|
||||
return mLastDraftCreatedEvent;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onDraftCreatedEvent(DraftCreatedEvent event) {
|
||||
mLastDraftCreatedEvent = event;
|
||||
}
|
||||
|
||||
public void resetDraftCreated() {
|
||||
mLastDraftCreatedEvent = null;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onOrganizationEvent(OrganizationEvent event) {
|
||||
if (event.getStatus() == Status.SUCCESS) {
|
||||
|
|
|
@ -1,61 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020 Proton Technologies AG
|
||||
*
|
||||
* This file is part of ProtonMail.
|
||||
*
|
||||
* ProtonMail 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.
|
||||
*
|
||||
* ProtonMail 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 ProtonMail. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
package ch.protonmail.android.events;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import ch.protonmail.android.api.models.room.messages.Message;
|
||||
|
||||
/**
|
||||
* Created by sunny on 8/12/15.
|
||||
*/
|
||||
public class DraftCreatedEvent implements Serializable {
|
||||
private String messageId;
|
||||
private String oldMessageId;
|
||||
private Status status;
|
||||
private Message message;
|
||||
|
||||
public DraftCreatedEvent(String messageId, String oldMessageId, Message message){
|
||||
this.messageId = messageId;
|
||||
this.oldMessageId = oldMessageId;
|
||||
this.message = message;
|
||||
this.status = Status.SUCCESS;
|
||||
}
|
||||
|
||||
public DraftCreatedEvent(String messageId, String oldMessageId, Message message, Status status) {
|
||||
this(messageId, oldMessageId, message);
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getMessageId(){
|
||||
return messageId;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public String getOldMessageId() {
|
||||
return oldMessageId;
|
||||
}
|
||||
|
||||
public Message getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
|
@ -1,320 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020 Proton Technologies AG
|
||||
*
|
||||
* This file is part of ProtonMail.
|
||||
*
|
||||
* ProtonMail 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.
|
||||
*
|
||||
* ProtonMail 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 ProtonMail. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
package ch.protonmail.android.jobs;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Base64;
|
||||
import android.webkit.URLUtil;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.birbit.android.jobqueue.Params;
|
||||
import com.birbit.android.jobqueue.RetryConstraint;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import ch.protonmail.android.api.models.IDList;
|
||||
import ch.protonmail.android.api.models.DraftBody;
|
||||
import ch.protonmail.android.api.models.User;
|
||||
import ch.protonmail.android.api.models.address.Address;
|
||||
import ch.protonmail.android.api.models.messages.receive.AttachmentFactory;
|
||||
import ch.protonmail.android.api.models.messages.receive.MessageFactory;
|
||||
import ch.protonmail.android.api.models.messages.receive.MessageResponse;
|
||||
import ch.protonmail.android.api.models.messages.receive.MessageSenderFactory;
|
||||
import ch.protonmail.android.api.models.messages.receive.ServerMessage;
|
||||
import ch.protonmail.android.api.models.messages.receive.ServerMessageSender;
|
||||
import ch.protonmail.android.api.models.room.messages.Attachment;
|
||||
import ch.protonmail.android.api.models.room.messages.Message;
|
||||
import ch.protonmail.android.api.models.room.messages.MessageSender;
|
||||
import ch.protonmail.android.api.models.room.messages.MessagesDatabase;
|
||||
import ch.protonmail.android.api.models.room.messages.MessagesDatabaseFactory;
|
||||
import ch.protonmail.android.api.models.room.pendingActions.PendingActionsDatabase;
|
||||
import ch.protonmail.android.api.models.room.pendingActions.PendingActionsDatabaseFactory;
|
||||
import ch.protonmail.android.api.models.room.pendingActions.PendingSend;
|
||||
import ch.protonmail.android.api.utils.Fields;
|
||||
import ch.protonmail.android.core.Constants;
|
||||
import ch.protonmail.android.crypto.AddressCrypto;
|
||||
import ch.protonmail.android.crypto.Crypto;
|
||||
import ch.protonmail.android.domain.entity.user.AddressKeys;
|
||||
import ch.protonmail.android.events.AttachmentFailedEvent;
|
||||
import ch.protonmail.android.events.DraftCreatedEvent;
|
||||
import ch.protonmail.android.utils.AppUtil;
|
||||
import ch.protonmail.android.utils.Logger;
|
||||
|
||||
public class CreateAndPostDraftJob extends ProtonMailBaseJob {
|
||||
|
||||
private static final String TAG_CREATE_AND_POST_DRAFT_JOB = "CreateAndPostDraftJob";
|
||||
private static final int CREATE_DRAFT_RETRY_LIMIT = 10;
|
||||
|
||||
private Long mDbMessageId;
|
||||
private final String mParentId;
|
||||
private final Constants.MessageActionType mActionType;
|
||||
private final boolean mUploadAttachments;
|
||||
private final List<String> mNewAttachments;
|
||||
private final String mOldSenderAddressID;
|
||||
private final String oldId;
|
||||
private final boolean isTransient;
|
||||
private final String mUsername;
|
||||
|
||||
public CreateAndPostDraftJob(@NonNull Long dbMessageId, String localMessageId, String parentId, Constants.MessageActionType actionType,
|
||||
boolean uploadAttachments, @NonNull List<String> newAttachments, String oldSenderId, boolean isTransient, String username) {
|
||||
super(new Params(Priority.HIGH).requireNetwork().persist().groupBy(Constants.JOB_GROUP_SENDING));
|
||||
mDbMessageId = dbMessageId;
|
||||
oldId = localMessageId;
|
||||
mParentId = parentId;
|
||||
mActionType = actionType;
|
||||
mUploadAttachments = uploadAttachments;
|
||||
mNewAttachments = newAttachments;
|
||||
mOldSenderAddressID = oldSenderId;
|
||||
this.isTransient = isTransient;
|
||||
mUsername = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getRetryLimit() {
|
||||
return CREATE_DRAFT_RETRY_LIMIT;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onProtonCancel(int cancelReason, @Nullable Throwable throwable) {
|
||||
PendingActionsDatabase pendingActionsDatabase = PendingActionsDatabaseFactory.Companion.getInstance(getApplicationContext()).getDatabase();
|
||||
pendingActionsDatabase.deletePendingDraftById(mDbMessageId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRun() throws Throwable {
|
||||
getMessageDetailsRepository().reloadDependenciesForUser(mUsername);
|
||||
// first save draft with -ve messageId so it won't overwrite any message
|
||||
MessagesDatabase messagesDatabase = MessagesDatabaseFactory.Companion.getInstance(getApplicationContext()).getDatabase();
|
||||
MessagesDatabase searchDatabase = MessagesDatabaseFactory.Companion.getSearchDatabase(getApplicationContext()).getDatabase();
|
||||
PendingActionsDatabase pendingActionsDatabase = PendingActionsDatabaseFactory.Companion.getInstance(getApplicationContext()).getDatabase();
|
||||
|
||||
Message message = getMessageDetailsRepository().findMessageByMessageDbId(mDbMessageId);
|
||||
PendingSend pendingForSending = pendingActionsDatabase.findPendingSendByDbId(message.getDbId());
|
||||
|
||||
if (pendingForSending != null) {
|
||||
return; // sending already pressed and in process, so no need to create draft, it will be created from the post send job
|
||||
}
|
||||
|
||||
message.setLocation(Constants.MessageLocationType.DRAFT.getMessageLocationTypeValue());
|
||||
AttachmentFactory attachmentFactory = new AttachmentFactory();
|
||||
MessageSenderFactory messageSenderFactory = new MessageSenderFactory();
|
||||
MessageFactory messageFactory = new MessageFactory(attachmentFactory, messageSenderFactory);
|
||||
|
||||
final ServerMessage serverMessage = messageFactory.createServerMessage(message);
|
||||
final DraftBody newDraft = new DraftBody(serverMessage);
|
||||
Message parentMessage = null;
|
||||
if (mParentId != null) {
|
||||
newDraft.setParentID(mParentId);
|
||||
newDraft.setAction(mActionType.getMessageActionTypeValue());
|
||||
if(!isTransient) {
|
||||
parentMessage = getMessageDetailsRepository().findMessageByIdBlocking(mParentId);
|
||||
} else {
|
||||
parentMessage = getMessageDetailsRepository().findSearchMessageById(mParentId);
|
||||
}
|
||||
}
|
||||
String addressId = message.getAddressID();
|
||||
String encryptedMessage = message.getMessageBody();
|
||||
if (!TextUtils.isEmpty(message.getMessageId())) {
|
||||
Message savedMessage = getMessageDetailsRepository().findMessageByIdBlocking(message.getMessageId());
|
||||
if (savedMessage != null) {
|
||||
encryptedMessage = savedMessage.getMessageBody();
|
||||
}
|
||||
}
|
||||
User user = getUserManager().getUser(mUsername);
|
||||
Address senderAddress = user.getAddressById(addressId);
|
||||
newDraft.getMessage().setSender(new ServerMessageSender(senderAddress.getDisplayName(), senderAddress.getEmail()));
|
||||
AddressCrypto crypto = Crypto.forAddress(getUserManager(), mUsername, message.getAddressID());
|
||||
newDraft.getMessage().setBody(encryptedMessage);
|
||||
List<Attachment> parentAttachmentList = null;
|
||||
if (parentMessage != null) {
|
||||
if(!isTransient) {
|
||||
parentAttachmentList = parentMessage.attachments(messagesDatabase);
|
||||
} else {
|
||||
parentAttachmentList = parentMessage.attachments(searchDatabase);
|
||||
}
|
||||
}
|
||||
if (parentAttachmentList != null) {
|
||||
updateAttachmentKeyPackets(parentAttachmentList, newDraft, mOldSenderAddressID, senderAddress);
|
||||
}
|
||||
if (message.getSenderEmail().contains("+")) { // it's being sent by alias
|
||||
newDraft.getMessage().setSender(new ServerMessageSender(message.getSenderName(), message.getSenderEmail()));
|
||||
}
|
||||
final MessageResponse draftResponse = getApi().createDraftBlocking(newDraft);
|
||||
// on success update draft with messageId
|
||||
|
||||
String newId = draftResponse.getMessageId();
|
||||
Message draftMessage = draftResponse.getMessage();
|
||||
getApi().markMessageAsRead(new IDList(Collections.singletonList(newId)));
|
||||
draftMessage.setDbId(mDbMessageId);
|
||||
draftMessage.setToList(message.getToList());
|
||||
draftMessage.setCcList(message.getCcList());
|
||||
draftMessage.setBccList(message.getBccList());
|
||||
draftMessage.setReplyTos(message.getReplyTos());
|
||||
draftMessage.setSender(message.getSender());
|
||||
draftMessage.setLabelIDs(message.getEventLabelIDs());
|
||||
draftMessage.setParsedHeaders(message.getParsedHeaders());
|
||||
draftMessage.setDownloaded(true);
|
||||
draftMessage.setIsRead(true);
|
||||
draftMessage.setNumAttachments(message.getNumAttachments());
|
||||
draftMessage.setLocalId(oldId);
|
||||
|
||||
for (Attachment atta : draftMessage.getAttachments()) {
|
||||
if (parentAttachmentList != null && !parentAttachmentList.isEmpty()) {
|
||||
for (Attachment parentAtta : parentAttachmentList) {
|
||||
if (parentAtta.getKeyPackets().equals(atta.getKeyPackets())) {
|
||||
atta.setInline(parentAtta.getInline());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
getMessageDetailsRepository().saveMessageInDB(draftMessage);
|
||||
|
||||
pendingForSending = pendingActionsDatabase.findPendingSendByOfflineMessageId(oldId);
|
||||
if (pendingForSending != null) {
|
||||
pendingForSending.setMessageId(newId);
|
||||
pendingActionsDatabase.insertPendingForSend(pendingForSending);
|
||||
}
|
||||
Message offlineDraft = getMessageDetailsRepository().findMessageByIdBlocking(oldId);
|
||||
if (offlineDraft != null) {
|
||||
getMessageDetailsRepository().deleteMessage(offlineDraft);
|
||||
}
|
||||
|
||||
if (message.getNumAttachments() >= 1 && mUploadAttachments && !mNewAttachments.isEmpty()) {
|
||||
List<Attachment> listOfAttachments = new ArrayList<>();
|
||||
for (String attachmentId : mNewAttachments) {
|
||||
listOfAttachments.add(messagesDatabase.findAttachmentById(attachmentId));
|
||||
}
|
||||
getJobManager().addJob(new PostCreateDraftAttachmentsJob(newId, oldId, mUploadAttachments, listOfAttachments, crypto, mUsername));
|
||||
} else {
|
||||
DraftCreatedEvent draftCreatedEvent = new DraftCreatedEvent(message.getMessageId(), oldId, draftMessage);
|
||||
AppUtil.postEventOnUi(draftCreatedEvent);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateAttachmentKeyPackets(List<Attachment> attachmentList, DraftBody draftBody, String oldSenderAddress, Address newSenderAddress) throws Exception {
|
||||
if (!TextUtils.isEmpty(oldSenderAddress)) {
|
||||
AddressCrypto oldCrypto = Crypto.forAddress(getUserManager(), mUsername, oldSenderAddress);
|
||||
AddressKeys newAddressKeys = newSenderAddress.toNewAddress().getKeys();
|
||||
String newPublicKey = oldCrypto.buildArmoredPublicKey(newAddressKeys.getPrimaryKey().getPrivateKey());
|
||||
for (Attachment attachment : attachmentList) {
|
||||
if (mActionType == Constants.MessageActionType.FORWARD ||
|
||||
((mActionType == Constants.MessageActionType.REPLY || mActionType == Constants.MessageActionType.REPLY_ALL) && attachment.getInline())) {
|
||||
String AttachmentID = attachment.getAttachmentId();
|
||||
String keyPackets = attachment.getKeyPackets();
|
||||
byte[] keyPackage = Base64.decode(keyPackets, Base64.DEFAULT);
|
||||
byte[] sessionKey = oldCrypto.decryptKeyPacket(keyPackage);
|
||||
byte[] newKeyPackage = oldCrypto.encryptKeyPacket(sessionKey, newPublicKey);
|
||||
String newKeyPackets = Base64.encodeToString(newKeyPackage, Base64.NO_WRAP);
|
||||
if (!TextUtils.isEmpty(keyPackets)) {
|
||||
draftBody.getAttachmentKeyPackets().put(AttachmentID, newKeyPackets);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (Attachment attachment : attachmentList) {
|
||||
if (mActionType == Constants.MessageActionType.FORWARD ||
|
||||
((mActionType == Constants.MessageActionType.REPLY || mActionType == Constants.MessageActionType.REPLY_ALL) && attachment.getInline())) {
|
||||
String AttachmentID = attachment.getAttachmentId();
|
||||
draftBody.getAttachmentKeyPackets().put(AttachmentID, attachment.getKeyPackets());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated replace with UploadAttachments use case
|
||||
*/
|
||||
private static class PostCreateDraftAttachmentsJob extends ProtonMailBaseJob {
|
||||
private final String mMessageId;
|
||||
private final String mOldMessageId;
|
||||
private final boolean mUploadAttachments;
|
||||
private final List<Attachment> mAttachments;
|
||||
private final AddressCrypto mCrypto;
|
||||
private final String mUsername;
|
||||
|
||||
PostCreateDraftAttachmentsJob(String messageId, String oldMessageId, boolean uploadAttachments, List<Attachment> attachments, AddressCrypto crypto, String username) {
|
||||
super(new Params(Priority.MEDIUM).requireNetwork().persist().groupBy(Constants.JOB_GROUP_MESSAGE));
|
||||
mMessageId = messageId;
|
||||
mOldMessageId = oldMessageId;
|
||||
mUploadAttachments = uploadAttachments;
|
||||
mAttachments = attachments;
|
||||
mCrypto = crypto;
|
||||
mUsername = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRun() throws Throwable {
|
||||
PendingActionsDatabase pendingActionsDatabase = PendingActionsDatabaseFactory.Companion.getInstance(getApplicationContext()).getDatabase();
|
||||
Message message = getMessageDetailsRepository().findMessageByIdBlocking(mMessageId);
|
||||
User user = getUserManager().getUser(mUsername);
|
||||
if (user == null) {
|
||||
pendingActionsDatabase.deletePendingUploadByMessageId(mMessageId, mOldMessageId);
|
||||
return;
|
||||
}
|
||||
if (message != null && mUploadAttachments && (mAttachments != null && mAttachments.size() > 0)) {
|
||||
//upload all attachments
|
||||
List<Attachment> messageAttachments = message.getAttachments();
|
||||
if (messageAttachments != null && mAttachments != null && mAttachments.size() > messageAttachments.size()) {
|
||||
messageAttachments = mAttachments;
|
||||
}
|
||||
for (Attachment attachment : messageAttachments) {
|
||||
try {
|
||||
String filePath = attachment.getFilePath();
|
||||
if (TextUtils.isEmpty(filePath)) {
|
||||
// TODO: inform user that the attachment is not saved properly
|
||||
continue;
|
||||
}
|
||||
final File file = new File(filePath);
|
||||
if (!URLUtil.isDataUrl(filePath) && !file.exists()) {
|
||||
continue;
|
||||
}
|
||||
if (attachment.isUploaded()) {
|
||||
continue;
|
||||
}
|
||||
attachment.uploadAndSave(getMessageDetailsRepository(), getApi(), mCrypto);
|
||||
} catch (Exception e) {
|
||||
Logger.doLogException(TAG_CREATE_AND_POST_DRAFT_JOB, "error while attaching file: " + attachment.getFilePath(), e);
|
||||
AppUtil.postEventOnUi(new AttachmentFailedEvent(message.getMessageId(), message.getSubject(), attachment.getFileName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
message.setNumAttachments(mAttachments.size());
|
||||
PendingSend pendingForSending = pendingActionsDatabase.findPendingSendByDbId(message.getDbId());
|
||||
|
||||
if (pendingForSending == null) {
|
||||
getMessageDetailsRepository().saveMessageInDB(message);
|
||||
}
|
||||
getJobManager().addJob(new FetchMessageDetailJob(message.getMessageId()));
|
||||
pendingActionsDatabase.deletePendingUploadByMessageId(mMessageId, mOldMessageId);
|
||||
DraftCreatedEvent draftCreatedEvent = new DraftCreatedEvent(message.getMessageId(), mOldMessageId, message);
|
||||
AppUtil.postEventOnUi(draftCreatedEvent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
|
||||
return RetryConstraint.createExponentialBackoff(runCount, 500);
|
||||
}
|
||||
}
|
|
@ -28,7 +28,6 @@ import ch.protonmail.android.R
|
|||
import ch.protonmail.android.activities.BaseActivity
|
||||
import ch.protonmail.android.core.Constants
|
||||
import ch.protonmail.android.core.ProtonMailApplication
|
||||
import ch.protonmail.android.events.DraftCreatedEvent
|
||||
import ch.protonmail.android.events.FetchDraftDetailEvent
|
||||
import ch.protonmail.android.events.FetchMessageDetailEvent
|
||||
import ch.protonmail.android.events.LogoutEvent
|
||||
|
@ -52,7 +51,6 @@ const val EXTRA_ATTACHMENT_IMPORT_EVENT = "extra_attachment_import_event"
|
|||
const val EXTRA_TOTAL_COUNT_EVENT = "extra_total_count_event"
|
||||
const val EXTRA_MESSAGE_DETAIL_EVENT = "extra_message_details_event"
|
||||
const val EXTRA_DRAFT_DETAILS_EVENT = "extra_draft_details_event"
|
||||
const val EXTRA_DRAFT_CREATED_EVENT = "extra_draft_created_event"
|
||||
// endregion
|
||||
|
||||
/*
|
||||
|
@ -68,7 +66,6 @@ class ValidatePinActivity : BaseActivity(),
|
|||
private var messageCountsEvent: MessageCountsEvent? = null
|
||||
private var messageDetailEvent: FetchMessageDetailEvent? = null
|
||||
private var draftDetailEvent: FetchDraftDetailEvent? = null
|
||||
private var draftCreatedEvent: DraftCreatedEvent? = null
|
||||
private lateinit var biometricPrompt: BiometricPrompt
|
||||
private lateinit var promptInfo: BiometricPrompt.PromptInfo
|
||||
|
||||
|
@ -125,11 +122,6 @@ class ValidatePinActivity : BaseActivity(),
|
|||
fun onFetchDraftDetailEvent(event: FetchDraftDetailEvent) {
|
||||
draftDetailEvent = event
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
fun onDraftCreatedEvent(event: DraftCreatedEvent) {
|
||||
draftCreatedEvent = event
|
||||
}
|
||||
// endregion
|
||||
|
||||
override fun onPinCreated(pin: String) {
|
||||
|
@ -242,9 +234,6 @@ class ValidatePinActivity : BaseActivity(),
|
|||
if (messageDetailEvent != null) {
|
||||
putExtra(EXTRA_MESSAGE_DETAIL_EVENT, messageDetailEvent)
|
||||
}
|
||||
if (draftCreatedEvent != null) {
|
||||
putExtra(EXTRA_DRAFT_CREATED_EVENT, draftCreatedEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue