avoid ANR in ConversationActivity.processComposeControls

This commit is contained in:
adbenitez 2025-03-10 22:17:47 +01:00
parent faa94acbcb
commit cca9dfb04a

View file

@ -1015,9 +1015,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
final SettableFuture<Integer> future = new SettableFuture<>();
DcMsg msg = null;
Optional<QuoteModel> quote = inputPanel.getQuote();
Integer recompress = 0;
boolean editing = isEditing;
// for a quick ui feedback, we clear the related controls immediately on sending messages.
@ -1027,122 +1025,113 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
inputPanel.clearQuote();
}
if (editing) {
int msgId = quote.get().getQuotedMsg().getId();
Util.runOnAnyBackgroundThread(() -> {
Util.runOnAnyBackgroundThread(() -> {
DcMsg msg = null;
int recompress = 0;
if (editing) {
int msgId = quote.get().getQuotedMsg().getId();
if (action == ACTION_SEND_OUT) {
dcContext.sendEditRequest(msgId, body);
} else {
dcContext.setDraft(chatId, null);
}
});
return future;
}
if(slideDeck!=null) {
if (action==ACTION_SEND_OUT) {
attachmentManager.clear(glideRequests, false);
future.set(chatId);
return;
}
try {
if (slideDeck.getWebxdctDraftId() != 0) {
msg = dcContext.getDraft(chatId);
} else {
List<Attachment> attachments = slideDeck.asAttachments();
for (Attachment attachment : attachments) {
String contentType = attachment.getContentType();
if (MediaUtil.isImageType(contentType) && slideDeck.getDocumentSlide() == null) {
msg = new DcMsg(dcContext,
MediaUtil.isGif(contentType) ? DcMsg.DC_MSG_GIF : DcMsg.DC_MSG_IMAGE);
msg.setDimension(attachment.getWidth(), attachment.getHeight());
} else if (MediaUtil.isAudioType(contentType)) {
msg = new DcMsg(dcContext,
attachment.isVoiceNote() ? DcMsg.DC_MSG_VOICE : DcMsg.DC_MSG_AUDIO);
} else if (MediaUtil.isVideoType(contentType) && slideDeck.getDocumentSlide() == null) {
msg = new DcMsg(dcContext, DcMsg.DC_MSG_VIDEO);
recompress = DcMsg.DC_MSG_VIDEO;
} else {
msg = new DcMsg(dcContext, DcMsg.DC_MSG_FILE);
if(slideDeck!=null) {
if (action==ACTION_SEND_OUT) {
Util.runOnMain(() -> attachmentManager.clear(glideRequests, false));
}
try {
if (slideDeck.getWebxdctDraftId() != 0) {
msg = dcContext.getDraft(chatId);
} else {
List<Attachment> attachments = slideDeck.asAttachments();
for (Attachment attachment : attachments) {
String contentType = attachment.getContentType();
if (MediaUtil.isImageType(contentType) && slideDeck.getDocumentSlide() == null) {
msg = new DcMsg(dcContext,
MediaUtil.isGif(contentType) ? DcMsg.DC_MSG_GIF : DcMsg.DC_MSG_IMAGE);
msg.setDimension(attachment.getWidth(), attachment.getHeight());
} else if (MediaUtil.isAudioType(contentType)) {
msg = new DcMsg(dcContext,
attachment.isVoiceNote() ? DcMsg.DC_MSG_VOICE : DcMsg.DC_MSG_AUDIO);
} else if (MediaUtil.isVideoType(contentType) && slideDeck.getDocumentSlide() == null) {
msg = new DcMsg(dcContext, DcMsg.DC_MSG_VIDEO);
recompress = DcMsg.DC_MSG_VIDEO;
} else {
msg = new DcMsg(dcContext, DcMsg.DC_MSG_FILE);
}
String path = attachment.getRealPath(this);
msg.setFileAndDeduplicate(path, attachment.getFileName(), null);
}
String path = attachment.getRealPath(this);
msg.setFileAndDeduplicate(path, attachment.getFileName(), null);
}
if (msg != null) {
msg.setText(body);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
else if (!body.isEmpty()){
msg = new DcMsg(dcContext, DcMsg.DC_MSG_TEXT);
msg.setText(body);
}
catch(Exception e) {
e.printStackTrace();
if (quote.isPresent()) {
if (msg == null) msg = new DcMsg(dcContext, DcMsg.DC_MSG_TEXT);
msg.setQuote(quote.get().getQuotedMsg());
}
}
else if (!body.isEmpty()){
msg = new DcMsg(dcContext, DcMsg.DC_MSG_TEXT);
msg.setText(body);
}
if (quote.isPresent()) {
if (msg == null) msg = new DcMsg(dcContext, DcMsg.DC_MSG_TEXT);
msg.setQuote(quote.get().getQuotedMsg());
}
if (action==ACTION_SEND_OUT) {
// msg may still be null to clear drafts
new AsyncTask<Object, Void, Void>() {
@Override
protected Void doInBackground(Object... param) {
DcMsg msg = (DcMsg)param[0];
Integer recompress = (Integer)param[1];
if (action==ACTION_SEND_OUT) {
// for WEBXDC, drafts are just sent out as is.
// for preparations and other cases, cleanup draft soon.
if (msg == null || msg.getType() != DcMsg.DC_MSG_WEBXDC) {
dcContext.setDraft(dcChat.getId(), null);
}
// for WEBXDC, drafts are just sent out as is.
// for preparations and other cases, cleanup draft soon.
if (msg == null || msg.getType() != DcMsg.DC_MSG_WEBXDC) {
dcContext.setDraft(dcChat.getId(), null);
}
if(msg!=null)
{
boolean doSend = true;
if (recompress==DcMsg.DC_MSG_VIDEO) {
Util.runOnMain(() -> {
progressDialog = ProgressDialog.show(
ConversationActivity.this,
"",
getString(R.string.one_moment),
true,
false
);
});
doSend = VideoRecoder.prepareVideo(ConversationActivity.this, dcChat.getId(), msg);
Util.runOnMain(() -> {
try {
progressDialog.dismiss();
} catch (final IllegalArgumentException e) {
// The activity is finishing/destroyed, do nothing.
}
});
}
if (doSend) {
if (dcContext.sendMsg(dcChat.getId(), msg) == 0) {
Util.runOnMain(()-> Toast.makeText(ConversationActivity.this, dcContext.getLastError(), Toast.LENGTH_LONG).show());
return null;
if(msg!=null) {
boolean doSend = true;
if (recompress==DcMsg.DC_MSG_VIDEO) {
Util.runOnMain(() -> {
progressDialog = ProgressDialog.show(
ConversationActivity.this,
"",
getString(R.string.one_moment),
true,
false
);
});
doSend = VideoRecoder.prepareVideo(ConversationActivity.this, dcChat.getId(), msg);
Util.runOnMain(() -> {
try {
progressDialog.dismiss();
} catch (final IllegalArgumentException e) {
// The activity is finishing/destroyed, do nothing.
}
}
Util.runOnMain(()-> sendComplete(dcChat.getId()));
});
}
}
else {
dcContext.setDraft(dcChat.getId(), msg);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
future.set(chatId);
if (doSend) {
if (dcContext.sendMsg(dcChat.getId(), msg) == 0) {
Util.runOnMain(()-> Toast.makeText(ConversationActivity.this, dcContext.getLastError(), Toast.LENGTH_LONG).show());
future.set(chatId);
return;
}
}
Util.runOnMain(() -> sendComplete(dcChat.getId()));
}
} else {
dcContext.setDraft(dcChat.getId(), msg);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, msg, recompress);
future.set(chatId);
});
return future;
}
@ -1361,8 +1350,12 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
Toast.LENGTH_SHORT).show();
}
else {
processComposeControls(ACTION_SEND_OUT);
DcHelper.getNotificationCenter(ConversationActivity.this).maybePlaySendSound(dcChat);
processComposeControls(ACTION_SEND_OUT).addListener(new AssertedSuccessListener<Integer>() {
@Override
public void onSuccess(Integer chatId) {
DcHelper.getNotificationCenter(ConversationActivity.this).maybePlaySendSound(dcChat);
}
});
}
}