Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Ensure initial TransferState is reported on setting TransferListener #2930

Merged
merged 2 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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