Commit 7723f5bd by olly Committed by Oliver Woodman

Clean up offline class names

This change is intended to resolve overloading of "Download",
where a DownloadTask could be an actual download task, or a
remove task.

Also cleaned up some documentation.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=194815058
parent c68e00e2
......@@ -18,7 +18,7 @@ package com.google.android.exoplayer2.demo;
import android.app.Notification;
import android.util.Pair;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.ProgressiveDownloadAction;
......@@ -75,21 +75,21 @@ public class DemoDownloadService extends DownloadService {
}
@Override
protected Notification getForegroundNotification(DownloadState[] downloadStates) {
protected Notification getForegroundNotification(TaskState[] taskStates) {
return DownloadNotificationUtil.createProgressNotification(
downloadStates, this, R.drawable.exo_controls_play, CHANNEL_ID, null);
taskStates, this, R.drawable.exo_controls_play, CHANNEL_ID, null);
}
@Override
protected void onStateChange(DownloadState downloadState) {
int notificationId = FOREGROUND_NOTIFICATION_ID + 1 + downloadState.taskId;
protected void onTaskStateChanged(TaskState taskState) {
int notificationId = FOREGROUND_NOTIFICATION_ID + 1 + taskState.taskId;
Notification downloadNotification =
DownloadNotificationUtil.createDownloadFinishedNotification(
downloadState,
taskState,
this,
R.drawable.exo_controls_play,
CHANNEL_ID,
downloadState.downloadAction.data,
taskState.action.data,
new ErrorMessageProvider<Throwable>() {
@Override
public Pair<Integer, String> getErrorMessage(Throwable throwable) {
......
......@@ -43,7 +43,7 @@ public abstract class DownloadAction {
}
/**
* Deserializes a {@link DownloadAction} from the {@code input}.
* Deserializes an action from the {@code input}.
*
* @param version Version of the data.
* @param input DataInputStream to read data from.
......@@ -55,17 +55,17 @@ public abstract class DownloadAction {
}
/**
* Deserializes one {@code action} which was serialized by {@link
* #serializeToStream(DownloadAction, OutputStream)} from the {@code input} using one of the
* {@link Deserializer}s which supports the type of the action.
* Deserializes one action that was serialized with {@link #serializeToStream(DownloadAction,
* OutputStream)} from the {@code input}, using the {@link Deserializer}s that supports the
* action's type.
*
* <p>The caller is responsible for closing the given {@link InputStream}.
*
* @param deserializers Array of {@link Deserializer}s to deserialize a {@link DownloadAction}.
* @param deserializers {@link Deserializer}s for supported actions.
* @param input Input stream to read serialized data.
* @return The deserialized {@link DownloadAction}.
* @throws IOException If there is an IO error from {@code input} or the action type isn't
* supported by any of the {@code deserializers}.
* @return The deserialized action.
* @throws IOException If there is an IO error reading from {@code input}, or if the action type
* isn't supported by any of the {@code deserializers}.
*/
public static DownloadAction deserializeFromStream(
Deserializer[] deserializers, InputStream input) throws IOException {
......@@ -79,10 +79,10 @@ public abstract class DownloadAction {
*
* <p>The caller is responsible for closing the given {@link InputStream}.
*
* @param deserializers Array of {@link Deserializer}s to deserialize a {@link DownloadAction}.
* @param deserializers {@link Deserializer}s for supported actions.
* @param input Input stream to read serialized data.
* @param version Master version of the serialization. See {@link DownloadAction#MASTER_VERSION}.
* @return The deserialized {@link DownloadAction}.
* @return The deserialized action.
* @throws IOException If there is an IO error from {@code input}.
* @throws DownloadException If the action type isn't supported by any of the {@code
* deserializers}.
......
......@@ -25,7 +25,7 @@ import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.util.Log;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.scheduler.RequirementsWatcher;
import com.google.android.exoplayer2.scheduler.Scheduler;
......@@ -272,21 +272,25 @@ public abstract class DownloadService extends Service {
/**
* Returns a notification to be displayed when this service running in the foreground.
*
* <p>This method is called when there is a download task state change and periodically while
* there is an active download. Update interval can be set using {@link #DownloadService(int,
* <p>This method is called when there is a task state change and periodically while there are
* active tasks. The periodic update interval can be set using {@link #DownloadService(int,
* long)}.
*
* <p>On API level 26 and above, it may be also called just before the service stops with an empty
* {@code downloadStates} array, returned notification is used to satisfy system requirements for
* foreground services.
* <p>On API level 26 and above, this method may also be called just before the service stops,
* with an empty {@code taskStates} array. The returned notification is used to satisfy system
* requirements for foreground services.
*
* @param downloadStates DownloadState for all tasks.
* @return A notification to be displayed when this service running in the foreground.
* @param taskStates The states of all current tasks.
* @return The foreground notification to display.
*/
protected abstract Notification getForegroundNotification(DownloadState[] downloadStates);
protected abstract Notification getForegroundNotification(TaskState[] taskStates);
/** Called when the download state changes. */
protected void onStateChange(DownloadState downloadState) {
/**
* Called when the state of a task changes.
*
* @param taskState The state of the task.
*/
protected void onTaskStateChanged(TaskState taskState) {
// Do nothing.
}
......@@ -308,9 +312,9 @@ public abstract class DownloadService extends Service {
private final class DownloadListener implements DownloadManager.DownloadListener {
@Override
public void onStateChange(DownloadManager downloadManager, DownloadState downloadState) {
DownloadService.this.onStateChange(downloadState);
if (downloadState.state == DownloadState.STATE_STARTED) {
public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) {
DownloadService.this.onTaskStateChanged(taskState);
if (taskState.state == TaskState.STATE_STARTED) {
foregroundNotificationUpdater.startPeriodicUpdates();
} else {
foregroundNotificationUpdater.update();
......@@ -349,8 +353,8 @@ public abstract class DownloadService extends Service {
}
public void update() {
DownloadState[] downloadStates = downloadManager.getDownloadStates();
startForeground(notificationId, getForegroundNotification(downloadStates));
TaskState[] taskStates = downloadManager.getAllTaskStates();
startForeground(notificationId, getForegroundNotification(taskStates));
notificationDisplayed = true;
if (periodicUpdatesStarted) {
handler.removeCallbacks(this);
......
......@@ -22,8 +22,8 @@ import android.os.ConditionVariable;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadListener;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState.State;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState.State;
import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.RobolectricUtil;
import com.google.android.exoplayer2.upstream.DummyDataSource;
......@@ -255,11 +255,11 @@ public class DownloadManagerTest {
downloadAction1.post().assertDoesNotStart();
downloadAction2.post().assertDoesNotStart();
DownloadState[] states = downloadManager.getDownloadStates();
TaskState[] states = downloadManager.getAllTaskStates();
assertThat(states).hasLength(3);
assertThat(states[0].downloadAction).isEqualTo(removeAction);
assertThat(states[1].downloadAction).isEqualTo(downloadAction1);
assertThat(states[2].downloadAction).isEqualTo(downloadAction2);
assertThat(states[0].action).isEqualTo(removeAction);
assertThat(states[1].action).isEqualTo(downloadAction1);
assertThat(states[2].action).isEqualTo(downloadAction2);
}
@Test
......@@ -503,11 +503,11 @@ public class DownloadManagerTest {
}
@Override
public void onStateChange(DownloadManager downloadManager, DownloadState downloadState) {
if (downloadState.state == DownloadState.STATE_ERROR && downloadError == null) {
downloadError = downloadState.error;
public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) {
if (taskState.state == TaskState.STATE_ERROR && downloadError == null) {
downloadError = taskState.error;
}
((FakeDownloadAction) downloadState.downloadAction).onStateChange(downloadState.state);
((FakeDownloadAction) taskState.action).onStateChange(taskState.state);
}
@Override
......@@ -580,23 +580,23 @@ public class DownloadManagerTest {
private FakeDownloadAction assertStarted() throws InterruptedException {
downloader.assertStarted(ASSERT_TRUE_TIMEOUT);
return assertState(DownloadState.STATE_STARTED);
return assertState(TaskState.STATE_STARTED);
}
private FakeDownloadAction assertEnded() {
return assertState(DownloadState.STATE_ENDED);
return assertState(TaskState.STATE_ENDED);
}
private FakeDownloadAction assertError() {
return assertState(DownloadState.STATE_ERROR);
return assertState(TaskState.STATE_ERROR);
}
private FakeDownloadAction assertCancelled() {
return assertState(DownloadState.STATE_CANCELED);
return assertState(TaskState.STATE_CANCELED);
}
private FakeDownloadAction assertStopped() {
return assertState(DownloadState.STATE_QUEUED);
return assertState(TaskState.STATE_QUEUED);
}
private FakeDownloadAction assertState(@State int expectedState) {
......@@ -619,13 +619,13 @@ public class DownloadManagerTest {
if (i > 0) {
sb.append(',');
}
sb.append(DownloadState.getStateString(receivedStates.get(i)));
sb.append(TaskState.getStateString(receivedStates.get(i)));
}
fail(
String.format(
Locale.US,
"expected:<%s> but was:<%s>",
DownloadState.getStateString(expectedState),
TaskState.getStateString(expectedState),
sb));
}
}
......
......@@ -25,7 +25,7 @@ import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.scheduler.Requirements;
......@@ -139,8 +139,7 @@ public class DownloadServiceDashTest {
}
@Override
protected Notification getForegroundNotification(
DownloadState[] downloadStates) {
protected Notification getForegroundNotification(TaskState[] taskStates) {
return Mockito.mock(Notification.class);
}
......
......@@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadListener;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.testutil.DummyMainThread;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
......@@ -40,9 +39,10 @@ import java.util.concurrent.TimeUnit;
}
@Override
public void onStateChange(DownloadManager downloadManager, DownloadState downloadState) {
if (downloadState.state == DownloadState.STATE_ERROR && downloadError == null) {
downloadError = downloadState.error;
public void onTaskStateChanged(
DownloadManager downloadManager, DownloadManager.TaskState taskState) {
if (taskState.state == DownloadManager.TaskState.STATE_ERROR && downloadError == null) {
downloadError = taskState.error;
}
}
......
......@@ -21,7 +21,7 @@ import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.offline.DownloadManager.DownloadState;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.util.ErrorMessageProvider;
/** Helper class to create notifications for downloads using {@link DownloadManager}. */
......@@ -32,18 +32,18 @@ public final class DownloadNotificationUtil {
private DownloadNotificationUtil() {}
/**
* Returns a progress notification for the given {@link DownloadState}s.
* Returns a progress notification for the given {@link TaskState}s.
*
* @param downloadStates States of the downloads.
* @param taskStates States of the downloads.
* @param context Used to access resources.
* @param smallIcon A small icon for the notification.
* @param channelId The id of the notification channel to use. Only required for API level 26 and
* above.
* @param message An optional message to display on the notification.
* @return A progress notification for the given {@link DownloadState}s.
* @return A progress notification for the given {@link TaskState}s.
*/
public static @Nullable Notification createProgressNotification(
DownloadState[] downloadStates,
TaskState[] taskStates,
Context context,
int smallIcon,
String channelId,
......@@ -52,16 +52,15 @@ public final class DownloadNotificationUtil {
int downloadTaskCount = 0;
boolean allDownloadPercentagesUnknown = true;
boolean haveDownloadedBytes = false;
for (DownloadState downloadState : downloadStates) {
if (downloadState.downloadAction.isRemoveAction
|| downloadState.state != DownloadState.STATE_STARTED) {
for (TaskState taskState : taskStates) {
if (taskState.action.isRemoveAction || taskState.state != TaskState.STATE_STARTED) {
continue;
}
if (downloadState.downloadPercentage != C.PERCENTAGE_UNSET) {
if (taskState.downloadPercentage != C.PERCENTAGE_UNSET) {
allDownloadPercentagesUnknown = false;
totalPercentage += downloadState.downloadPercentage;
totalPercentage += taskState.downloadPercentage;
}
haveDownloadedBytes |= downloadState.downloadedBytes > 0;
haveDownloadedBytes |= taskState.downloadedBytes > 0;
downloadTaskCount++;
}
......@@ -79,11 +78,11 @@ public final class DownloadNotificationUtil {
}
/**
* Returns a notification for a {@link DownloadState} which is in either {@link
* DownloadState#STATE_ENDED} or {@link DownloadState#STATE_ERROR} states. Returns null if it's
* some other state or it's state of a remove action.
* Returns a notification for a {@link TaskState} which is in either {@link TaskState#STATE_ENDED}
* or {@link TaskState#STATE_ERROR} states. Returns null if it's some other state or it's state of
* a remove action.
*
* @param downloadState State of the download.
* @param taskState State of the download.
* @param context Used to access resources.
* @param smallIcon A small icon for the notifications.
* @param channelId The id of the notification channel to use. Only required for API level 26 and
......@@ -92,27 +91,26 @@ public final class DownloadNotificationUtil {
* @param errorMessageProvider An optional {@link ErrorMessageProvider} for translating download
* errors into readable error messages. If not null and there is a download error then the
* error message is displayed instead of {@code message}.
* @return A notification for a {@link DownloadState} which is in either {@link
* DownloadState#STATE_ENDED} or {@link DownloadState#STATE_ERROR} states. Returns null if
* it's some other state or it's state of a remove action.
* @return A notification for a {@link TaskState} which is in either {@link TaskState#STATE_ENDED}
* or {@link TaskState#STATE_ERROR} states. Returns null if it's some other state or it's
* state of a remove action.
*/
public static @Nullable Notification createDownloadFinishedNotification(
DownloadState downloadState,
TaskState taskState,
Context context,
int smallIcon,
String channelId,
@Nullable String message,
@Nullable ErrorMessageProvider<Throwable> errorMessageProvider) {
if (downloadState.downloadAction.isRemoveAction
|| (downloadState.state != DownloadState.STATE_ENDED
&& downloadState.state != DownloadState.STATE_ERROR)) {
if (taskState.action.isRemoveAction
|| (taskState.state != TaskState.STATE_ENDED && taskState.state != TaskState.STATE_ERROR)) {
return null;
}
if (downloadState.error != null && errorMessageProvider != null) {
message = errorMessageProvider.getErrorMessage(downloadState.error).second;
if (taskState.error != null && errorMessageProvider != null) {
message = errorMessageProvider.getErrorMessage(taskState.error).second;
}
int titleStringId =
downloadState.state == DownloadState.STATE_ENDED
taskState.state == TaskState.STATE_ENDED
? R.string.exo_download_completed
: R.string.exo_download_failed;
NotificationCompat.Builder notificationBuilder =
......
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