Commit 41f76bdb by eguven Committed by Oliver Woodman

Convert DownloadState id to String

For now this id can not be set by client but auto generated using
content cache key and uri.

PiperOrigin-RevId: 225356645
parent 044066f4
...@@ -31,12 +31,15 @@ public class DemoDownloadService extends DownloadService { ...@@ -31,12 +31,15 @@ public class DemoDownloadService extends DownloadService {
private static final int JOB_ID = 1; private static final int JOB_ID = 1;
private static final int FOREGROUND_NOTIFICATION_ID = 1; private static final int FOREGROUND_NOTIFICATION_ID = 1;
private static int nextNotificationId = FOREGROUND_NOTIFICATION_ID + 1;
public DemoDownloadService() { public DemoDownloadService() {
super( super(
FOREGROUND_NOTIFICATION_ID, FOREGROUND_NOTIFICATION_ID,
DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL, DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL,
CHANNEL_ID, CHANNEL_ID,
R.string.exo_download_notification_channel_name); R.string.exo_download_notification_channel_name);
nextNotificationId = FOREGROUND_NOTIFICATION_ID + 1;
} }
@Override @Override
...@@ -82,8 +85,9 @@ public class DemoDownloadService extends DownloadService { ...@@ -82,8 +85,9 @@ public class DemoDownloadService extends DownloadService {
CHANNEL_ID, CHANNEL_ID,
/* contentIntent= */ null, /* contentIntent= */ null,
Util.fromUtf8Bytes(downloadState.action.data)); Util.fromUtf8Bytes(downloadState.action.data));
} else {
return;
} }
int notificationId = FOREGROUND_NOTIFICATION_ID + 1 + downloadState.id; NotificationUtil.setNotification(this, nextNotificationId++, notification);
NotificationUtil.setNotification(this, notificationId, notification);
} }
} }
...@@ -108,6 +108,8 @@ public final class DownloadAction { ...@@ -108,6 +108,8 @@ public final class DownloadAction {
/* data= */ null); /* data= */ null);
} }
/** The unique content id. */
public final String id;
/** The type of the action. */ /** The type of the action. */
public final String type; public final String type;
/** The uri being downloaded or removed. */ /** The uri being downloaded or removed. */
...@@ -140,6 +142,7 @@ public final class DownloadAction { ...@@ -140,6 +142,7 @@ public final class DownloadAction {
List<StreamKey> keys, List<StreamKey> keys,
@Nullable String customCacheKey, @Nullable String customCacheKey,
@Nullable byte[] data) { @Nullable byte[] data) {
this.id = customCacheKey != null ? customCacheKey : uri.toString();
this.type = type; this.type = type;
this.uri = uri; this.uri = uri;
this.isRemoveAction = isRemoveAction; this.isRemoveAction = isRemoveAction;
...@@ -171,9 +174,7 @@ public final class DownloadAction { ...@@ -171,9 +174,7 @@ public final class DownloadAction {
/** Returns whether this is an action for the same media as the {@code other}. */ /** Returns whether this is an action for the same media as the {@code other}. */
public boolean isSameMedia(DownloadAction other) { public boolean isSameMedia(DownloadAction other) {
return customCacheKey == null return id.equals(other.id);
? other.customCacheKey == null && uri.equals(other.uri)
: customCacheKey.equals(other.customCacheKey);
} }
/** Returns keys of tracks to be downloaded. */ /** Returns keys of tracks to be downloaded. */
...@@ -187,7 +188,8 @@ public final class DownloadAction { ...@@ -187,7 +188,8 @@ public final class DownloadAction {
return false; return false;
} }
DownloadAction that = (DownloadAction) o; DownloadAction that = (DownloadAction) o;
return type.equals(that.type) return id.equals(that.id)
&& type.equals(that.type)
&& uri.equals(that.uri) && uri.equals(that.uri)
&& isRemoveAction == that.isRemoveAction && isRemoveAction == that.isRemoveAction
&& keys.equals(that.keys) && keys.equals(that.keys)
...@@ -198,6 +200,7 @@ public final class DownloadAction { ...@@ -198,6 +200,7 @@ public final class DownloadAction {
@Override @Override
public final int hashCode() { public final int hashCode() {
int result = type.hashCode(); int result = type.hashCode();
result = 31 * result + id.hashCode();
result = 31 * result + uri.hashCode(); result = 31 * result + uri.hashCode();
result = 31 * result + (isRemoveAction ? 1 : 0); result = 31 * result + (isRemoveAction ? 1 : 0);
result = 31 * result + keys.hashCode(); result = 31 * result + keys.hashCode();
......
...@@ -94,7 +94,6 @@ public final class DownloadManager { ...@@ -94,7 +94,6 @@ public final class DownloadManager {
private final Handler fileIOHandler; private final Handler fileIOHandler;
private final CopyOnWriteArraySet<Listener> listeners; private final CopyOnWriteArraySet<Listener> listeners;
private int nextDownloadId;
private boolean initialized; private boolean initialized;
private boolean released; private boolean released;
private boolean downloadsStopped; private boolean downloadsStopped;
...@@ -192,9 +191,8 @@ public final class DownloadManager { ...@@ -192,9 +191,8 @@ public final class DownloadManager {
* Handles the given action. * Handles the given action.
* *
* @param action The action to be executed. * @param action The action to be executed.
* @return The id of the newly created or the existing download.
*/ */
public int handleAction(DownloadAction action) { public void handleAction(DownloadAction action) {
Assertions.checkState(!released); Assertions.checkState(!released);
Download download = getDownloadForAction(action); Download download = getDownloadForAction(action);
if (initialized) { if (initialized) {
...@@ -207,7 +205,6 @@ public final class DownloadManager { ...@@ -207,7 +205,6 @@ public final class DownloadManager {
notifyListenersDownloadStateChange(download); notifyListenersDownloadStateChange(download);
} }
} }
return download.id;
} }
/** Returns the number of downloads. */ /** Returns the number of downloads. */
...@@ -216,13 +213,18 @@ public final class DownloadManager { ...@@ -216,13 +213,18 @@ public final class DownloadManager {
return downloads.size(); return downloads.size();
} }
/** Returns the state of a download, or null if no such download exists */ /**
* Returns {@link DownloadState} for the given content id, or null if no such download exists.
*
* @param id The unique content id.
* @return DownloadState for the given content id, or null if no such download exists.
*/
@Nullable @Nullable
public DownloadState getDownloadState(int downloadId) { public DownloadState getDownloadState(String id) {
Assertions.checkState(!released); Assertions.checkState(!released);
for (int i = 0; i < downloads.size(); i++) { for (int i = 0; i < downloads.size(); i++) {
Download download = downloads.get(i); Download download = downloads.get(i);
if (download.id == downloadId) { if (download.id.equals(id)) {
return download.getDownloadState(); return download.getDownloadState();
} }
} }
...@@ -288,8 +290,7 @@ public final class DownloadManager { ...@@ -288,8 +290,7 @@ public final class DownloadManager {
return download; return download;
} }
} }
Download download = Download download = new Download(this, downloaderFactory, action, minRetryCount);
new Download(nextDownloadId++, this, downloaderFactory, action, minRetryCount);
downloads.add(download); downloads.add(download);
logd("Download is added", download); logd("Download is added", download);
return download; return download;
...@@ -501,8 +502,8 @@ public final class DownloadManager { ...@@ -501,8 +502,8 @@ public final class DownloadManager {
} }
} }
/** The unique download id. */ /** The unique content id. */
public final int id; public final String id;
/** The action being executed. */ /** The action being executed. */
public final DownloadAction action; public final DownloadAction action;
/** The state of the download. */ /** The state of the download. */
...@@ -524,7 +525,6 @@ public final class DownloadManager { ...@@ -524,7 +525,6 @@ public final class DownloadManager {
@FailureReason public final int failureReason; @FailureReason public final int failureReason;
private DownloadState( private DownloadState(
int id,
DownloadAction action, DownloadAction action,
@State int state, @State int state,
float downloadPercentage, float downloadPercentage,
...@@ -533,7 +533,7 @@ public final class DownloadManager { ...@@ -533,7 +533,7 @@ public final class DownloadManager {
@FailureReason int failureReason) { @FailureReason int failureReason) {
Assertions.checkState( Assertions.checkState(
failureReason == FAILURE_REASON_NONE ? state != STATE_FAILED : state == STATE_FAILED); failureReason == FAILURE_REASON_NONE ? state != STATE_FAILED : state == STATE_FAILED);
this.id = id; this.id = action.id;
this.action = action; this.action = action;
this.state = state; this.state = state;
this.downloadPercentage = downloadPercentage; this.downloadPercentage = downloadPercentage;
...@@ -552,7 +552,7 @@ public final class DownloadManager { ...@@ -552,7 +552,7 @@ public final class DownloadManager {
@IntDef({STATE_QUEUED, STATE_COMPLETED}) @IntDef({STATE_QUEUED, STATE_COMPLETED})
public @interface TargetState {} public @interface TargetState {}
private final int id; private final String id;
private final DownloadManager downloadManager; private final DownloadManager downloadManager;
private final DownloaderFactory downloaderFactory; private final DownloaderFactory downloaderFactory;
private final int minRetryCount; private final int minRetryCount;
...@@ -571,12 +571,11 @@ public final class DownloadManager { ...@@ -571,12 +571,11 @@ public final class DownloadManager {
@MonotonicNonNull @DownloadState.FailureReason private int failureReason; @MonotonicNonNull @DownloadState.FailureReason private int failureReason;
private Download( private Download(
int id,
DownloadManager downloadManager, DownloadManager downloadManager,
DownloaderFactory downloaderFactory, DownloaderFactory downloaderFactory,
DownloadAction action, DownloadAction action,
int minRetryCount) { int minRetryCount) {
this.id = id; this.id = action.id;
this.downloadManager = downloadManager; this.downloadManager = downloadManager;
this.downloaderFactory = downloaderFactory; this.downloaderFactory = downloaderFactory;
this.action = action; this.action = action;
...@@ -615,7 +614,7 @@ public final class DownloadManager { ...@@ -615,7 +614,7 @@ public final class DownloadManager {
totalBytes = downloader.getTotalBytes(); totalBytes = downloader.getTotalBytes();
} }
return new DownloadState( return new DownloadState(
id, action, state, downloadPercentage, downloadedBytes, totalBytes, failureReason); action, state, downloadPercentage, downloadedBytes, totalBytes, failureReason);
} }
/** Returns whether the download is finished. */ /** Returns whether the download is finished. */
......
...@@ -33,7 +33,6 @@ import java.util.Arrays; ...@@ -33,7 +33,6 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -374,8 +373,8 @@ public class DownloadManagerTest { ...@@ -374,8 +373,8 @@ public class DownloadManagerTest {
DownloadState[] states = downloadManager.getAllDownloadStates(); DownloadState[] states = downloadManager.getAllDownloadStates();
assertThat(states).hasLength(3); assertThat(states).hasLength(3);
int[] taskIds = {task1.taskId, task2.taskId, task3.taskId}; String[] taskIds = {task1.taskId, task2.taskId, task3.taskId};
int[] stateTaskIds = {states[0].id, states[1].id, states[2].id}; String[] stateTaskIds = {states[0].id, states[1].id, states[2].id};
assertThat(stateTaskIds).isEqualTo(taskIds); assertThat(stateTaskIds).isEqualTo(taskIds);
} }
...@@ -471,13 +470,11 @@ public class DownloadManagerTest { ...@@ -471,13 +470,11 @@ public class DownloadManagerTest {
} }
private DownloadRunner postAction(DownloadAction action) { private DownloadRunner postAction(DownloadAction action) {
AtomicInteger taskIdHolder = new AtomicInteger(); runOnMainThread(() -> downloadManager.handleAction(action));
runOnMainThread(() -> taskIdHolder.set(downloadManager.handleAction(action)));
int taskId = taskIdHolder.get();
if (taskWrapper == null) { if (taskWrapper == null) {
taskWrapper = new TaskWrapper(taskId); taskWrapper = new TaskWrapper(action.id);
} else { } else {
assertThat(taskId).isEqualTo(taskWrapper.taskId); assertThat(action.id).isEqualTo(taskWrapper.taskId);
} }
return this; return this;
} }
...@@ -515,9 +512,9 @@ public class DownloadManagerTest { ...@@ -515,9 +512,9 @@ public class DownloadManagerTest {
} }
private final class TaskWrapper { private final class TaskWrapper {
private final int taskId; private final String taskId;
private TaskWrapper(int taskId) { private TaskWrapper(String taskId) {
this.taskId = taskId; this.taskId = taskId;
} }
...@@ -559,12 +556,12 @@ public class DownloadManagerTest { ...@@ -559,12 +556,12 @@ public class DownloadManagerTest {
if (o == null || getClass() != o.getClass()) { if (o == null || getClass() != o.getClass()) {
return false; return false;
} }
return taskId == ((TaskWrapper) o).taskId; return taskId.equals(((TaskWrapper) o).taskId);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return taskId; return taskId.hashCode();
} }
} }
......
...@@ -31,7 +31,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -31,7 +31,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
private final DownloadManager downloadManager; private final DownloadManager downloadManager;
private final DummyMainThread dummyMainThread; private final DummyMainThread dummyMainThread;
private final HashMap<Integer, ArrayBlockingQueue<Integer>> actionStates; private final HashMap<String, ArrayBlockingQueue<Integer>> actionStates;
private CountDownLatch downloadFinishedCondition; private CountDownLatch downloadFinishedCondition;
@DownloadState.FailureReason private int failureReason; @DownloadState.FailureReason private int failureReason;
...@@ -43,7 +43,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -43,7 +43,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
actionStates = new HashMap<>(); actionStates = new HashMap<>();
} }
public Integer pollStateChange(int taskId, long timeoutMs) throws InterruptedException { public Integer pollStateChange(String taskId, long timeoutMs) throws InterruptedException {
return getStateQueue(taskId).poll(timeoutMs, TimeUnit.MILLISECONDS); return getStateQueue(taskId).poll(timeoutMs, TimeUnit.MILLISECONDS);
} }
...@@ -96,7 +96,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -96,7 +96,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
assertThat(downloadFinishedCondition.await(TIMEOUT, TimeUnit.MILLISECONDS)).isTrue(); assertThat(downloadFinishedCondition.await(TIMEOUT, TimeUnit.MILLISECONDS)).isTrue();
} }
private ArrayBlockingQueue<Integer> getStateQueue(int taskId) { private ArrayBlockingQueue<Integer> getStateQueue(String taskId) {
synchronized (actionStates) { synchronized (actionStates) {
if (!actionStates.containsKey(taskId)) { if (!actionStates.containsKey(taskId)) {
actionStates.put(taskId, new ArrayBlockingQueue<>(10)); actionStates.put(taskId, new ArrayBlockingQueue<>(10));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment