mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-20 14:49:53 +01:00
[android] fix IllegalStateException when creating attachments
Injecting the non-singleton AttachmentCreator keeps an instance around that gets re-used with a different ViewModel. When backing out without sending or cancelling the attachments, we don't reset the state which leads us into an illegal state.
This commit is contained in:
@@ -76,8 +76,12 @@ class AttachmentCreatorImpl implements AttachmentCreator {
|
|||||||
@UiThread
|
@UiThread
|
||||||
public LiveData<AttachmentResult> storeAttachments(
|
public LiveData<AttachmentResult> storeAttachments(
|
||||||
LiveData<GroupId> groupId, Collection<Uri> newUris) {
|
LiveData<GroupId> groupId, Collection<Uri> newUris) {
|
||||||
if (task != null || result != null || !uris.isEmpty())
|
if (task != null || result != null || !uris.isEmpty()) {
|
||||||
|
if (task != null) LOG.warning("Task already exists!");
|
||||||
|
if (result != null) LOG.warning("Result already exists!");
|
||||||
|
if (!uris.isEmpty()) LOG.warning("Uris available: " + uris);
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
MutableLiveData<AttachmentResult> result = new MutableLiveData<>();
|
MutableLiveData<AttachmentResult> result = new MutableLiveData<>();
|
||||||
this.result = result;
|
this.result = result;
|
||||||
uris.addAll(newUris);
|
uris.addAll(newUris);
|
||||||
@@ -96,8 +100,12 @@ class AttachmentCreatorImpl implements AttachmentCreator {
|
|||||||
@UiThread
|
@UiThread
|
||||||
public LiveData<AttachmentResult> getLiveAttachments() {
|
public LiveData<AttachmentResult> getLiveAttachments() {
|
||||||
MutableLiveData<AttachmentResult> result = this.result;
|
MutableLiveData<AttachmentResult> result = this.result;
|
||||||
if (task == null || result == null || uris.isEmpty())
|
if (task == null || result == null || uris.isEmpty()) {
|
||||||
|
if (task == null) LOG.warning("No Task!");
|
||||||
|
if (result == null) LOG.warning("No Result!");
|
||||||
|
if (uris.isEmpty()) LOG.warning("Uris empty!");
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
// A task is already running. It will update the result LiveData.
|
// A task is already running. It will update the result LiveData.
|
||||||
// So nothing more to do here.
|
// So nothing more to do here.
|
||||||
return result;
|
return result;
|
||||||
@@ -174,8 +182,7 @@ class AttachmentCreatorImpl implements AttachmentCreator {
|
|||||||
@Override
|
@Override
|
||||||
@UiThread
|
@UiThread
|
||||||
public void cancel() {
|
public void cancel() {
|
||||||
if (task == null) throw new AssertionError();
|
if (task != null) task.cancel();
|
||||||
task.cancel();
|
|
||||||
deleteUnsentAttachments();
|
deleteUnsentAttachments();
|
||||||
resetState();
|
resetState();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ public class ConversationViewModel extends AndroidViewModel
|
|||||||
@Override
|
@Override
|
||||||
protected void onCleared() {
|
protected void onCleared() {
|
||||||
super.onCleared();
|
super.onCleared();
|
||||||
attachmentCreator.deleteUnsentAttachments();
|
attachmentCreator.cancel(); // also deletes unsent attachments
|
||||||
eventBus.removeListener(this);
|
eventBus.removeListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,6 +274,7 @@ public class ConversationViewModel extends AndroidViewModel
|
|||||||
settingsManager.mergeSettings(settings, SETTINGS_NAMESPACE);
|
settingsManager.mergeSettings(settings, SETTINGS_NAMESPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
private void createMessage(GroupId groupId, @Nullable String text,
|
private void createMessage(GroupId groupId, @Nullable String text,
|
||||||
List<AttachmentHeader> headers, long timestamp,
|
List<AttachmentHeader> headers, long timestamp,
|
||||||
boolean hasImageSupport) {
|
boolean hasImageSupport) {
|
||||||
@@ -292,6 +293,7 @@ public class ConversationViewModel extends AndroidViewModel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
private void storeMessage(PrivateMessage m) {
|
private void storeMessage(PrivateMessage m) {
|
||||||
attachmentCreator.onAttachmentsSent(m.getMessage().getId());
|
attachmentCreator.onAttachmentsSent(m.getMessage().getId());
|
||||||
dbExecutor.execute(() -> {
|
dbExecutor.execute(() -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user