Skip to content

Commit

Permalink
Fix: Ensure initial TransferState is reported on setting TransferList…
Browse files Browse the repository at this point in the history
…ener (#2930)

* fix: ensure initial status is reported on setting transfer listener

* accidental comment out
  • Loading branch information
tylerjroach authored Jun 10, 2022
1 parent 2c19aa3 commit 6f222ee
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,22 @@ protected void updateFromDB(Cursor c) {
* @param listener A TransferListener used to receive notification.
*/
public void setTransferListener(TransferListener listener) {
if (listener != null) {
synchronized (this) {
// Remove previous listener.
cleanTransferListener();

// One additional listener is attached so that the basic transfer
// info gets updated along side.
statusListener = new TransferStatusListener();
TransferStatusUpdater.registerListener(id, statusListener);
transferListener = listener;
TransferStatusUpdater.registerListener(id, transferListener);
}
if (statusListener == null) {
statusListener = new TransferStatusListener();
TransferStatusUpdater.registerListener(id, statusListener);
}

if (listener != null) {
transferListener = listener;
transferListener.onStateChanged(id, transferState);
TransferStatusUpdater.registerListener(id, transferListener);
}
}
}

Expand Down Expand Up @@ -251,7 +255,7 @@ public void cleanTransferListener() {
/**
* A listener that can update the {@link TransferObserver}.
*/
private class TransferStatusListener implements TransferListener {
protected class TransferStatusListener implements TransferListener {

@Override
@SuppressWarnings("checkstyle:hiddenfield")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,18 @@ synchronized void updateState(final int id, final TransferState newState) {

// invoke TransferListener callback on main thread
for (final TransferListener l : list) {
mainHandler.post(new Runnable() {
@Override
public void run() {
l.onStateChanged(id, newState);
}
});
// If instance is TransferStatusListener, post immediately.
// Posting to main thread can cause a missed status.
if (l instanceof TransferObserver.TransferStatusListener) {
l.onStateChanged(id, newState);
} else {
mainHandler.post(new Runnable() {
@Override
public void run() {
l.onStateChanged(id, newState);
}
});
}
}

// remove all LISTENERS when the transfer is in a final state so
Expand Down

0 comments on commit 6f222ee

Please sign in to comment.