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

Requeue MSG_DOWNLOAD_UPDATE when received before the service is started. #6828

Closed
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 @@ -484,6 +484,13 @@ public void release() {
}
}

void resendDownloadUpdate(Download download, boolean isRemove) {
Assertions.checkNotNull(download);
DownloadUpdate update =
new DownloadUpdate(download, isRemove, Collections.singletonList(download));
mainHandler.obtainMessage(MSG_DOWNLOAD_UPDATE, update).sendToTarget();
}

private void onRequirementsStateChanged(
RequirementsWatcher requirementsWatcher,
@Requirements.RequirementFlags int notMetRequirements) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.NotificationUtil;
import com.google.android.exoplayer2.util.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -856,6 +858,7 @@ private static final class DownloadManagerHelper implements DownloadManager.List
@Nullable private final Scheduler scheduler;
private final Class<? extends DownloadService> serviceClass;
@Nullable private DownloadService downloadService;
private final List<Runnable> resendMessageCallbacks = new ArrayList<>();

private DownloadManagerHelper(
Context context,
Expand All @@ -877,6 +880,13 @@ private DownloadManagerHelper(
public void attachService(DownloadService downloadService) {
Assertions.checkState(this.downloadService == null);
this.downloadService = downloadService;
if (!resendMessageCallbacks.isEmpty()) {
Log.d(TAG, String.format("%d messages to resend.", resendMessageCallbacks.size()));
for (Runnable notifyCallback : resendMessageCallbacks) {
notifyCallback.run();
}
resendMessageCallbacks.clear();
}
}

public void detachService(DownloadService downloadService, boolean unschedule) {
Expand All @@ -891,13 +901,19 @@ public void detachService(DownloadService downloadService, boolean unschedule) {
public void onDownloadChanged(DownloadManager downloadManager, Download download) {
if (downloadService != null) {
downloadService.notifyDownloadChanged(download);
} else {
Log.d(TAG, "onDownloadChanged with null service. Queuing to resend message later.");
resendMessageCallbacks.add(() -> downloadManager.resendDownloadUpdate(download, false));
}
}

@Override
public void onDownloadRemoved(DownloadManager downloadManager, Download download) {
if (downloadService != null) {
downloadService.notifyDownloadRemoved(download);
} else {
Log.d(TAG, "onDownloadRemoved with null service. Queuing to resend message later.");
resendMessageCallbacks.add(() -> downloadManager.resendDownloadUpdate(download, true));
}
}

Expand Down