From 41f76bdb53e5ba82771f30ca643298036df7eb69 Mon Sep 17 00:00:00 2001 From: eguven Date: Thu, 13 Dec 2018 13:36:59 +0000 Subject: [PATCH] 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 --- .../exoplayer2/demo/DemoDownloadService.java | 8 +++-- .../exoplayer2/offline/DownloadAction.java | 11 ++++--- .../exoplayer2/offline/DownloadManager.java | 33 +++++++++---------- .../offline/DownloadManagerTest.java | 21 +++++------- .../testutil/TestDownloadManagerListener.java | 6 ++-- 5 files changed, 41 insertions(+), 38 deletions(-) diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java index 9671999bb2..adb0ae95ca 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoDownloadService.java @@ -31,12 +31,15 @@ public class DemoDownloadService extends DownloadService { private static final int JOB_ID = 1; private static final int FOREGROUND_NOTIFICATION_ID = 1; + private static int nextNotificationId = FOREGROUND_NOTIFICATION_ID + 1; + public DemoDownloadService() { super( FOREGROUND_NOTIFICATION_ID, DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL, CHANNEL_ID, R.string.exo_download_notification_channel_name); + nextNotificationId = FOREGROUND_NOTIFICATION_ID + 1; } @Override @@ -82,8 +85,9 @@ public class DemoDownloadService extends DownloadService { CHANNEL_ID, /* contentIntent= */ null, Util.fromUtf8Bytes(downloadState.action.data)); + } else { + return; } - int notificationId = FOREGROUND_NOTIFICATION_ID + 1 + downloadState.id; - NotificationUtil.setNotification(this, notificationId, notification); + NotificationUtil.setNotification(this, nextNotificationId++, notification); } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java index 2c7b5069b9..be3f8a503d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java @@ -108,6 +108,8 @@ public final class DownloadAction { /* data= */ null); } + /** The unique content id. */ + public final String id; /** The type of the action. */ public final String type; /** The uri being downloaded or removed. */ @@ -140,6 +142,7 @@ public final class DownloadAction { List keys, @Nullable String customCacheKey, @Nullable byte[] data) { + this.id = customCacheKey != null ? customCacheKey : uri.toString(); this.type = type; this.uri = uri; this.isRemoveAction = isRemoveAction; @@ -171,9 +174,7 @@ public final class DownloadAction { /** Returns whether this is an action for the same media as the {@code other}. */ public boolean isSameMedia(DownloadAction other) { - return customCacheKey == null - ? other.customCacheKey == null && uri.equals(other.uri) - : customCacheKey.equals(other.customCacheKey); + return id.equals(other.id); } /** Returns keys of tracks to be downloaded. */ @@ -187,7 +188,8 @@ public final class DownloadAction { return false; } DownloadAction that = (DownloadAction) o; - return type.equals(that.type) + return id.equals(that.id) + && type.equals(that.type) && uri.equals(that.uri) && isRemoveAction == that.isRemoveAction && keys.equals(that.keys) @@ -198,6 +200,7 @@ public final class DownloadAction { @Override public final int hashCode() { int result = type.hashCode(); + result = 31 * result + id.hashCode(); result = 31 * result + uri.hashCode(); result = 31 * result + (isRemoveAction ? 1 : 0); result = 31 * result + keys.hashCode(); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java index 9c6f0893fe..89aed9f010 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadManager.java @@ -94,7 +94,6 @@ public final class DownloadManager { private final Handler fileIOHandler; private final CopyOnWriteArraySet listeners; - private int nextDownloadId; private boolean initialized; private boolean released; private boolean downloadsStopped; @@ -192,9 +191,8 @@ public final class DownloadManager { * Handles the given action. * * @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); Download download = getDownloadForAction(action); if (initialized) { @@ -207,7 +205,6 @@ public final class DownloadManager { notifyListenersDownloadStateChange(download); } } - return download.id; } /** Returns the number of downloads. */ @@ -216,13 +213,18 @@ public final class DownloadManager { 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 - public DownloadState getDownloadState(int downloadId) { + public DownloadState getDownloadState(String id) { Assertions.checkState(!released); for (int i = 0; i < downloads.size(); i++) { Download download = downloads.get(i); - if (download.id == downloadId) { + if (download.id.equals(id)) { return download.getDownloadState(); } } @@ -288,8 +290,7 @@ public final class DownloadManager { return download; } } - Download download = - new Download(nextDownloadId++, this, downloaderFactory, action, minRetryCount); + Download download = new Download(this, downloaderFactory, action, minRetryCount); downloads.add(download); logd("Download is added", download); return download; @@ -501,8 +502,8 @@ public final class DownloadManager { } } - /** The unique download id. */ - public final int id; + /** The unique content id. */ + public final String id; /** The action being executed. */ public final DownloadAction action; /** The state of the download. */ @@ -524,7 +525,6 @@ public final class DownloadManager { @FailureReason public final int failureReason; private DownloadState( - int id, DownloadAction action, @State int state, float downloadPercentage, @@ -533,7 +533,7 @@ public final class DownloadManager { @FailureReason int failureReason) { Assertions.checkState( failureReason == FAILURE_REASON_NONE ? state != STATE_FAILED : state == STATE_FAILED); - this.id = id; + this.id = action.id; this.action = action; this.state = state; this.downloadPercentage = downloadPercentage; @@ -552,7 +552,7 @@ public final class DownloadManager { @IntDef({STATE_QUEUED, STATE_COMPLETED}) public @interface TargetState {} - private final int id; + private final String id; private final DownloadManager downloadManager; private final DownloaderFactory downloaderFactory; private final int minRetryCount; @@ -571,12 +571,11 @@ public final class DownloadManager { @MonotonicNonNull @DownloadState.FailureReason private int failureReason; private Download( - int id, DownloadManager downloadManager, DownloaderFactory downloaderFactory, DownloadAction action, int minRetryCount) { - this.id = id; + this.id = action.id; this.downloadManager = downloadManager; this.downloaderFactory = downloaderFactory; this.action = action; @@ -615,7 +614,7 @@ public final class DownloadManager { totalBytes = downloader.getTotalBytes(); } return new DownloadState( - id, action, state, downloadPercentage, downloadedBytes, totalBytes, failureReason); + action, state, downloadPercentage, downloadedBytes, totalBytes, failureReason); } /** Returns whether the download is finished. */ diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java index db498a84cf..e7d54b4141 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java @@ -33,7 +33,6 @@ import java.util.Arrays; import java.util.HashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -374,8 +373,8 @@ public class DownloadManagerTest { DownloadState[] states = downloadManager.getAllDownloadStates(); assertThat(states).hasLength(3); - int[] taskIds = {task1.taskId, task2.taskId, task3.taskId}; - int[] stateTaskIds = {states[0].id, states[1].id, states[2].id}; + String[] taskIds = {task1.taskId, task2.taskId, task3.taskId}; + String[] stateTaskIds = {states[0].id, states[1].id, states[2].id}; assertThat(stateTaskIds).isEqualTo(taskIds); } @@ -471,13 +470,11 @@ public class DownloadManagerTest { } private DownloadRunner postAction(DownloadAction action) { - AtomicInteger taskIdHolder = new AtomicInteger(); - runOnMainThread(() -> taskIdHolder.set(downloadManager.handleAction(action))); - int taskId = taskIdHolder.get(); + runOnMainThread(() -> downloadManager.handleAction(action)); if (taskWrapper == null) { - taskWrapper = new TaskWrapper(taskId); + taskWrapper = new TaskWrapper(action.id); } else { - assertThat(taskId).isEqualTo(taskWrapper.taskId); + assertThat(action.id).isEqualTo(taskWrapper.taskId); } return this; } @@ -515,9 +512,9 @@ public class DownloadManagerTest { } private final class TaskWrapper { - private final int taskId; + private final String taskId; - private TaskWrapper(int taskId) { + private TaskWrapper(String taskId) { this.taskId = taskId; } @@ -559,12 +556,12 @@ public class DownloadManagerTest { if (o == null || getClass() != o.getClass()) { return false; } - return taskId == ((TaskWrapper) o).taskId; + return taskId.equals(((TaskWrapper) o).taskId); } @Override public int hashCode() { - return taskId; + return taskId.hashCode(); } } diff --git a/testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java b/testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java index 3d939c8207..e7b850d52b 100644 --- a/testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java +++ b/testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java @@ -31,7 +31,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen private final DownloadManager downloadManager; private final DummyMainThread dummyMainThread; - private final HashMap> actionStates; + private final HashMap> actionStates; private CountDownLatch downloadFinishedCondition; @DownloadState.FailureReason private int failureReason; @@ -43,7 +43,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen 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); } @@ -96,7 +96,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen assertThat(downloadFinishedCondition.await(TIMEOUT, TimeUnit.MILLISECONDS)).isTrue(); } - private ArrayBlockingQueue getStateQueue(int taskId) { + private ArrayBlockingQueue getStateQueue(String taskId) { synchronized (actionStates) { if (!actionStates.containsKey(taskId)) { actionStates.put(taskId, new ArrayBlockingQueue<>(10));