mirror of
https://github.com/androidx/media.git
synced 2025-05-14 19:19:58 +08:00
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
This commit is contained in:
parent
044066f4b1
commit
41f76bdb53
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<StreamKey> 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();
|
||||
|
@ -94,7 +94,6 @@ public final class DownloadManager {
|
||||
private final Handler fileIOHandler;
|
||||
private final CopyOnWriteArraySet<Listener> 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. */
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
|
||||
|
||||
private final DownloadManager downloadManager;
|
||||
private final DummyMainThread dummyMainThread;
|
||||
private final HashMap<Integer, ArrayBlockingQueue<Integer>> actionStates;
|
||||
private final HashMap<String, ArrayBlockingQueue<Integer>> 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<Integer> getStateQueue(int taskId) {
|
||||
private ArrayBlockingQueue<Integer> getStateQueue(String taskId) {
|
||||
synchronized (actionStates) {
|
||||
if (!actionStates.containsKey(taskId)) {
|
||||
actionStates.put(taskId, new ArrayBlockingQueue<>(10));
|
||||
|
Loading…
x
Reference in New Issue
Block a user