Skip to content

Commit

Permalink
refactor ListenableFutureTask and make saves async
Browse files Browse the repository at this point in the history
// FREEBIE
  • Loading branch information
mcginty committed Oct 28, 2014
1 parent 53da1f8 commit ff2ac8a
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package org.whispersystems.textsecure.util;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ListenableFutureTask<V> extends FutureTask<V> {

// private WeakReference<FutureTaskListener<V>> listener;
private FutureTaskListener<V> listener;
private final List<FutureTaskListener<V>> listeners;

public ListenableFutureTask(Callable<V> callable, FutureTaskListener<V> listener) {
public ListenableFutureTask(Callable<V> callable) {
super(callable);
this.listener = listener;
// if (listener == null) {
// this.listener = null;
// } else {
// this.listener = new WeakReference<FutureTaskListener<V>>(listener);
// }
this.listeners = new LinkedList<>();
}

public synchronized void setListener(FutureTaskListener<V> listener) {
// if (listener != null) this.listener = new WeakReference<FutureTaskListener<V>>(listener);
// else this.listener = null;
this.listener = listener;

public synchronized void addListener(FutureTaskListener<V> listener) {
if (this.isDone()) {
callback();
callback(listener);
return;
}
listeners.add(listener);
}

public synchronized boolean removeListener(FutureTaskListener<V> listener) {
return listeners.remove(listener);
}

@Override
Expand All @@ -35,17 +33,19 @@ protected synchronized void done() {
}

private void callback() {
if (this.listener != null) {
FutureTaskListener<V> nestedListener = this.listener;
// FutureTaskListener<V> nestedListener = this.listener.get();
if (nestedListener != null) {
try {
nestedListener.onSuccess(get());
} catch (ExecutionException ee) {
nestedListener.onFailure(ee);
} catch (InterruptedException e) {
throw new AssertionError(e);
}
for (FutureTaskListener<V> listener : listeners) {
callback(listener);
}
}

private void callback(FutureTaskListener<V> listener) {
if (listener != null) {
try {
listener.onSuccess(get());
} catch (ExecutionException ee) {
listener.onFailure(ee);
} catch (InterruptedException e) {
throw new AssertionError(e);
}
}
}
Expand Down
23 changes: 15 additions & 8 deletions src/org/thoughtcrime/securesms/ConversationFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.thoughtcrime.securesms.util.SaveAttachmentTask;
import org.thoughtcrime.securesms.util.SaveAttachmentTask.Attachment;
import org.whispersystems.textsecure.crypto.MasterSecret;
import org.whispersystems.textsecure.util.FutureTaskListener;

import java.sql.Date;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -242,14 +243,20 @@ private void handleSaveAttachment(final MediaMmsMessageRecord message) {
SaveAttachmentTask.showWarningDialog(getActivity(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

final Slide slide = message.getMediaSlideSync();
if (slide == null) {
Log.w(TAG, "No slide with attachable media found, failing nicely.");
Toast.makeText(getActivity(), R.string.ConversationFragment_error_while_saving_attachment_to_sd_card, Toast.LENGTH_LONG).show();
return;
}
SaveAttachmentTask saveTask = new SaveAttachmentTask(getActivity(), masterSecret);
saveTask.execute(new Attachment(slide.getUri(), slide.getContentType(), message.getDateReceived()));
message.fetchMediaSlide(new FutureTaskListener<Slide>() {
@Override
public void onSuccess(Slide slide) {
SaveAttachmentTask saveTask = new SaveAttachmentTask(getActivity(), masterSecret);
saveTask.execute(new Attachment(slide.getUri(), slide.getContentType(), message.getDateReceived()));
}

@Override
public void onFailure(Throwable error) {
Log.w(TAG, "No slide with attachable media found, failing nicely.");
Log.w(TAG, error);
Toast.makeText(getActivity(), R.string.ConversationFragment_error_while_saving_attachment_to_sd_card, Toast.LENGTH_LONG).show();
}
});
}
});
}
Expand Down
10 changes: 6 additions & 4 deletions src/org/thoughtcrime/securesms/ConversationItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public class ConversationItem extends LinearLayout {
private Button mmsDownloadButton;
private TextView mmsDownloadingLabel;
private ListenableFutureTask<SlideDeck> slideDeck;
private FutureTaskListener<SlideDeck> slideDeckListener;
private TypedArray backgroundDrawables;

private final FailedIconClickListener failedIconClickListener = new FailedIconClickListener();
Expand Down Expand Up @@ -181,8 +182,8 @@ public void set(MasterSecret masterSecret, MessageRecord messageRecord,
}

public void unbind() {
if (slideDeck != null)
slideDeck.setListener(null);
if (slideDeck != null && slideDeckListener != null)
slideDeck.removeListener(slideDeckListener);
}

public MessageRecord getMessageRecord() {
Expand Down Expand Up @@ -345,7 +346,7 @@ private void setMediaMmsAttributes(MediaMmsMessageRecord messageRecord) {
}

slideDeck = messageRecord.getSlideDeckFuture();
slideDeck.setListener(new FutureTaskListener<SlideDeck>() {
slideDeckListener = new FutureTaskListener<SlideDeck>() {
@Override
public void onSuccess(final SlideDeck result) {
if (result == null)
Expand Down Expand Up @@ -376,7 +377,8 @@ public boolean onLongClick(View v) {

@Override
public void onFailure(Throwable error) {}
});
};
slideDeck.addListener(slideDeckListener);
}

/// Helper Methods
Expand Down
4 changes: 2 additions & 2 deletions src/org/thoughtcrime/securesms/database/MmsDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ public SlideDeck call() throws Exception {
}
};

future = new ListenableFutureTask<SlideDeck>(task, null);
future = new ListenableFutureTask<SlideDeck>(task);
slideResolver.execute(future);

return future;
Expand All @@ -1084,7 +1084,7 @@ public SlideDeck call() throws Exception {
}
};

ListenableFutureTask<SlideDeck> future = new ListenableFutureTask<SlideDeck>(task, null);
ListenableFutureTask<SlideDeck> future = new ListenableFutureTask<SlideDeck>(task);
future.run();

return future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@

import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.MmsDatabase;
import org.thoughtcrime.securesms.mms.MediaNotFoundException;
import org.thoughtcrime.securesms.mms.Slide;
import org.thoughtcrime.securesms.mms.SlideDeck;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.whispersystems.textsecure.push.exceptions.NotFoundException;
import org.whispersystems.textsecure.util.FutureTaskListener;
import org.whispersystems.textsecure.util.ListenableFutureTask;

import java.util.List;
Expand Down Expand Up @@ -82,21 +85,26 @@ public boolean containsMediaSlide() {
return deck != null && deck.containsMediaSlide();
}

public Slide getMediaSlideSync() {
SlideDeck deck = getSlideDeckSync();
if (deck == null) {
return null;
}
List<Slide> slides = deck.getSlides();

for (Slide slide : slides) {
if (slide.hasImage() || slide.hasVideo() || slide.hasAudio()) {
return slide;
public void fetchMediaSlide(final FutureTaskListener<Slide> listener) {
slideDeckFutureTask.addListener(new FutureTaskListener<SlideDeck>() {
@Override
public void onSuccess(SlideDeck deck) {
for (Slide slide : deck.getSlides()) {
if (slide.hasImage() || slide.hasVideo() || slide.hasAudio()) {
listener.onSuccess(slide);
return;
}
}
listener.onFailure(new MediaNotFoundException("no media slide found"));
}
}
return null;
}

@Override
public void onFailure(Throwable error) {
listener.onFailure(error);
}
});
}

public int getPartCount() {
return partCount;
Expand Down
36 changes: 36 additions & 0 deletions src/org/thoughtcrime/securesms/mms/MediaNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (C) 2014 Open Whisper Systems
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms.mms;

public class MediaNotFoundException extends Exception {

public MediaNotFoundException() {
}

public MediaNotFoundException(String detailMessage) {
super(detailMessage);
}

public MediaNotFoundException(Throwable throwable) {
super(throwable);
}

public MediaNotFoundException(String detailMessage, Throwable throwable) {
super(detailMessage, throwable);
}

}
2 changes: 1 addition & 1 deletion src/org/thoughtcrime/securesms/recipients/Recipient.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Recipient[] newArray(int size) {
this.contactPhoto = contactPhoto;
this.recipientId = recipientId;

future.setListener(new FutureTaskListener<RecipientDetails>() {
future.addListener(new FutureTaskListener<RecipientDetails>() {
@Override
public void onSuccess(RecipientDetails result) {
if (result != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public RecipientDetails call() throws Exception {
}
};

ListenableFutureTask<RecipientDetails> future = new ListenableFutureTask<RecipientDetails>(task, null);
ListenableFutureTask<RecipientDetails> future = new ListenableFutureTask<RecipientDetails>(task);

asyncRecipientResolver.submit(future);

Expand Down

0 comments on commit ff2ac8a

Please sign in to comment.