Commit 8c624081 by olly Committed by Oliver Woodman

Rename start/stopDownloads to resume/pauseDownloads

PiperOrigin-RevId: 244216620
parent 138da6d5
...@@ -55,8 +55,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -55,8 +55,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
* Manages downloads. * Manages downloads.
* *
* <p>Normally a download manager should be accessed via a {@link DownloadService}. When a download * <p>Normally a download manager should be accessed via a {@link DownloadService}. When a download
* manager is used directly instead, downloads will be initially stopped and so must be started by * manager is used directly instead, downloads will be initially paused and so must be resumed by
* calling {@link #startDownloads()}. * calling {@link #resumeDownloads()}.
* *
* <p>A download manager instance must be accessed only from the thread that created it, unless that * <p>A download manager instance must be accessed only from the thread that created it, unless that
* thread does not have a {@link Looper}. In that case, it must be accessed only from the * thread does not have a {@link Looper}. In that case, it must be accessed only from the
...@@ -126,7 +126,7 @@ public final class DownloadManager { ...@@ -126,7 +126,7 @@ public final class DownloadManager {
// Messages posted to the background handler. // Messages posted to the background handler.
private static final int MSG_INITIALIZE = 0; private static final int MSG_INITIALIZE = 0;
private static final int MSG_SET_DOWNLOADS_STARTED = 1; private static final int MSG_SET_DOWNLOADS_RESUMED = 1;
private static final int MSG_SET_NOT_MET_REQUIREMENTS = 2; private static final int MSG_SET_NOT_MET_REQUIREMENTS = 2;
private static final int MSG_SET_STOP_REASON = 3; private static final int MSG_SET_STOP_REASON = 3;
private static final int MSG_ADD_DOWNLOAD = 4; private static final int MSG_ADD_DOWNLOAD = 4;
...@@ -179,7 +179,7 @@ public final class DownloadManager { ...@@ -179,7 +179,7 @@ public final class DownloadManager {
// Mutable fields that are accessed on the internal thread. // Mutable fields that are accessed on the internal thread.
@Requirements.RequirementFlags private int notMetRequirements; @Requirements.RequirementFlags private int notMetRequirements;
private boolean downloadsStarted; private boolean downloadsResumed;
private int simultaneousDownloads; private int simultaneousDownloads;
/** /**
...@@ -346,19 +346,19 @@ public final class DownloadManager { ...@@ -346,19 +346,19 @@ public final class DownloadManager {
return Collections.unmodifiableList(new ArrayList<>(downloads)); return Collections.unmodifiableList(new ArrayList<>(downloads));
} }
/** Starts all downloads except those that have a non-zero {@link Download#stopReason}. */ /** Resumes all downloads except those that have a non-zero {@link Download#stopReason}. */
public void startDownloads() { public void resumeDownloads() {
pendingMessages++; pendingMessages++;
internalHandler internalHandler
.obtainMessage(MSG_SET_DOWNLOADS_STARTED, /* downloadsStarted */ 1, /* unused */ 0) .obtainMessage(MSG_SET_DOWNLOADS_RESUMED, /* downloadsResumed */ 1, /* unused */ 0)
.sendToTarget(); .sendToTarget();
} }
/** Stops all downloads. */ /** Pauses all downloads. */
public void stopDownloads() { public void pauseDownloads() {
pendingMessages++; pendingMessages++;
internalHandler internalHandler
.obtainMessage(MSG_SET_DOWNLOADS_STARTED, /* downloadsStarted */ 0, /* unused */ 0) .obtainMessage(MSG_SET_DOWNLOADS_RESUMED, /* downloadsResumed */ 0, /* unused */ 0)
.sendToTarget(); .sendToTarget();
} }
...@@ -541,9 +541,9 @@ public final class DownloadManager { ...@@ -541,9 +541,9 @@ public final class DownloadManager {
int notMetRequirements = message.arg1; int notMetRequirements = message.arg1;
initializeInternal(notMetRequirements); initializeInternal(notMetRequirements);
break; break;
case MSG_SET_DOWNLOADS_STARTED: case MSG_SET_DOWNLOADS_RESUMED:
boolean downloadsStarted = message.arg1 != 0; boolean downloadsResumed = message.arg1 != 0;
setDownloadsStarted(downloadsStarted); setDownloadsResumed(downloadsResumed);
break; break;
case MSG_SET_NOT_MET_REQUIREMENTS: case MSG_SET_NOT_MET_REQUIREMENTS:
notMetRequirements = message.arg1; notMetRequirements = message.arg1;
...@@ -604,11 +604,11 @@ public final class DownloadManager { ...@@ -604,11 +604,11 @@ public final class DownloadManager {
} }
} }
private void setDownloadsStarted(boolean downloadsStarted) { private void setDownloadsResumed(boolean downloadsResumed) {
if (this.downloadsStarted == downloadsStarted) { if (this.downloadsResumed == downloadsResumed) {
return; return;
} }
this.downloadsStarted = downloadsStarted; this.downloadsResumed = downloadsResumed;
for (int i = 0; i < downloadInternals.size(); i++) { for (int i = 0; i < downloadInternals.size(); i++) {
downloadInternals.get(i).updateStopState(); downloadInternals.get(i).updateStopState();
} }
...@@ -813,7 +813,7 @@ public final class DownloadManager { ...@@ -813,7 +813,7 @@ public final class DownloadManager {
} }
private boolean canStartDownloads() { private boolean canStartDownloads() {
return downloadsStarted && notMetRequirements == 0; return downloadsResumed && notMetRequirements == 0;
} }
/* package */ static Download mergeRequest( /* package */ static Download mergeRequest(
......
...@@ -66,24 +66,24 @@ public abstract class DownloadService extends Service { ...@@ -66,24 +66,24 @@ public abstract class DownloadService extends Service {
public static final String ACTION_ADD = "com.google.android.exoplayer.downloadService.action.ADD"; public static final String ACTION_ADD = "com.google.android.exoplayer.downloadService.action.ADD";
/** /**
* Starts all downloads except those that have a non-zero {@link Download#stopReason}. Extras: * Resumes all downloads except those that have a non-zero {@link Download#stopReason}. Extras:
* *
* <ul> * <ul>
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}. * <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul> * </ul>
*/ */
public static final String ACTION_START = public static final String ACTION_RESUME =
"com.google.android.exoplayer.downloadService.action.START"; "com.google.android.exoplayer.downloadService.action.RESUME";
/** /**
* Stops all downloads. Extras: * Pauses all downloads. Extras:
* *
* <ul> * <ul>
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}. * <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul> * </ul>
*/ */
public static final String ACTION_STOP = public static final String ACTION_PAUSE =
"com.google.android.exoplayer.downloadService.action.STOP"; "com.google.android.exoplayer.downloadService.action.PAUSE";
/** /**
* Sets the stop reason for one or all downloads. To clear the stop reason, pass {@link * Sets the stop reason for one or all downloads. To clear the stop reason, pass {@link
...@@ -278,51 +278,51 @@ public abstract class DownloadService extends Service { ...@@ -278,51 +278,51 @@ public abstract class DownloadService extends Service {
} }
/** /**
* Builds an {@link Intent} for setting the stop reason for one or all downloads. To clear the * Builds an {@link Intent} for resuming all downloads.
* stop reason, pass {@link Download#STOP_REASON_NONE}.
* *
* @param context A {@link Context}. * @param context A {@link Context}.
* @param clazz The concrete download service being targeted by the intent. * @param clazz The concrete download service being targeted by the intent.
* @param id The content id, or {@code null} to set the stop reason for all downloads.
* @param stopReason An application defined stop reason.
* @param foreground Whether this intent will be used to start the service in the foreground. * @param foreground Whether this intent will be used to start the service in the foreground.
* @return Created Intent. * @return Created Intent.
*/ */
public static Intent buildSetStopReasonIntent( public static Intent buildResumeDownloadsIntent(
Context context, Context context, Class<? extends DownloadService> clazz, boolean foreground) {
Class<? extends DownloadService> clazz, return getIntent(context, clazz, ACTION_RESUME, foreground);
@Nullable String id,
int stopReason,
boolean foreground) {
return getIntent(context, clazz, ACTION_SET_STOP_REASON, foreground)
.putExtra(KEY_CONTENT_ID, id)
.putExtra(KEY_STOP_REASON, stopReason);
} }
/** /**
* Builds an {@link Intent} for starting all downloads. * Builds an {@link Intent} to pause all downloads.
* *
* @param context A {@link Context}. * @param context A {@link Context}.
* @param clazz The concrete download service being targeted by the intent. * @param clazz The concrete download service being targeted by the intent.
* @param foreground Whether this intent will be used to start the service in the foreground. * @param foreground Whether this intent will be used to start the service in the foreground.
* @return Created Intent. * @return Created Intent.
*/ */
public static Intent buildStartDownloadsIntent( public static Intent buildPauseDownloadsIntent(
Context context, Class<? extends DownloadService> clazz, boolean foreground) { Context context, Class<? extends DownloadService> clazz, boolean foreground) {
return getIntent(context, clazz, ACTION_START, foreground); return getIntent(context, clazz, ACTION_PAUSE, foreground);
} }
/** /**
* Builds an {@link Intent} for stopping all downloads. * Builds an {@link Intent} for setting the stop reason for one or all downloads. To clear the
* stop reason, pass {@link Download#STOP_REASON_NONE}.
* *
* @param context A {@link Context}. * @param context A {@link Context}.
* @param clazz The concrete download service being targeted by the intent. * @param clazz The concrete download service being targeted by the intent.
* @param id The content id, or {@code null} to set the stop reason for all downloads.
* @param stopReason An application defined stop reason.
* @param foreground Whether this intent will be used to start the service in the foreground. * @param foreground Whether this intent will be used to start the service in the foreground.
* @return Created Intent. * @return Created Intent.
*/ */
public static Intent buildStopDownloadsIntent( public static Intent buildSetStopReasonIntent(
Context context, Class<? extends DownloadService> clazz, boolean foreground) { Context context,
return getIntent(context, clazz, ACTION_STOP, foreground); Class<? extends DownloadService> clazz,
@Nullable String id,
int stopReason,
boolean foreground) {
return getIntent(context, clazz, ACTION_SET_STOP_REASON, foreground)
.putExtra(KEY_CONTENT_ID, id)
.putExtra(KEY_STOP_REASON, stopReason);
} }
/** /**
...@@ -343,6 +343,26 @@ public abstract class DownloadService extends Service { ...@@ -343,6 +343,26 @@ public abstract class DownloadService extends Service {
} }
/** /**
* Starts the service if not started already and adds a new download.
*
* @param context A {@link Context}.
* @param clazz The concrete download service to be started.
* @param downloadRequest The request to be executed.
* @param stopReason An initial stop reason for the download, or {@link Download#STOP_REASON_NONE}
* if the download should be started.
* @param foreground Whether the service is started in the foreground.
*/
public static void sendNewDownload(
Context context,
Class<? extends DownloadService> clazz,
DownloadRequest downloadRequest,
int stopReason,
boolean foreground) {
Intent intent = buildAddRequestIntent(context, clazz, downloadRequest, stopReason, foreground);
startService(context, intent, foreground);
}
/**
* Starts the service if not started already and removes a download. * Starts the service if not started already and removes a download.
* *
* @param context A {@link Context}. * @param context A {@link Context}.
...@@ -357,48 +377,48 @@ public abstract class DownloadService extends Service { ...@@ -357,48 +377,48 @@ public abstract class DownloadService extends Service {
} }
/** /**
* Starts the service if not started already and sets the stop reason for one or all downloads. To * Starts the service if not started already and resumes all downloads.
* clear stop reason, pass {@link Download#STOP_REASON_NONE}.
* *
* @param context A {@link Context}. * @param context A {@link Context}.
* @param clazz The concrete download service to be started. * @param clazz The concrete download service to be started.
* @param id The content id, or {@code null} to set the stop reason for all downloads.
* @param stopReason An application defined stop reason.
* @param foreground Whether the service is started in the foreground. * @param foreground Whether the service is started in the foreground.
*/ */
public static void sendStopReason( public static void sendResumeDownloads(
Context context, Context context, Class<? extends DownloadService> clazz, boolean foreground) {
Class<? extends DownloadService> clazz, Intent intent = buildResumeDownloadsIntent(context, clazz, foreground);
@Nullable String id,
int stopReason,
boolean foreground) {
Intent intent = buildSetStopReasonIntent(context, clazz, id, stopReason, foreground);
startService(context, intent, foreground); startService(context, intent, foreground);
} }
/** /**
* Starts the service if not started already and starts all downloads. * Starts the service if not started already and pauses all downloads.
* *
* @param context A {@link Context}. * @param context A {@link Context}.
* @param clazz The concrete download service to be started. * @param clazz The concrete download service to be started.
* @param foreground Whether the service is started in the foreground. * @param foreground Whether the service is started in the foreground.
*/ */
public static void sendStartDownloads( public static void sendPauseDownloads(
Context context, Class<? extends DownloadService> clazz, boolean foreground) { Context context, Class<? extends DownloadService> clazz, boolean foreground) {
Intent intent = buildStartDownloadsIntent(context, clazz, foreground); Intent intent = buildPauseDownloadsIntent(context, clazz, foreground);
startService(context, intent, foreground); startService(context, intent, foreground);
} }
/** /**
* Starts the service if not started already and stops all downloads. * Starts the service if not started already and sets the stop reason for one or all downloads. To
* clear stop reason, pass {@link Download#STOP_REASON_NONE}.
* *
* @param context A {@link Context}. * @param context A {@link Context}.
* @param clazz The concrete download service to be started. * @param clazz The concrete download service to be started.
* @param id The content id, or {@code null} to set the stop reason for all downloads.
* @param stopReason An application defined stop reason.
* @param foreground Whether the service is started in the foreground. * @param foreground Whether the service is started in the foreground.
*/ */
public static void sendStopDownloads( public static void sendStopReason(
Context context, Class<? extends DownloadService> clazz, boolean foreground) { Context context,
Intent intent = buildStopDownloadsIntent(context, clazz, foreground); Class<? extends DownloadService> clazz,
@Nullable String id,
int stopReason,
boolean foreground) {
Intent intent = buildSetStopReasonIntent(context, clazz, id, stopReason, foreground);
startService(context, intent, foreground); startService(context, intent, foreground);
} }
...@@ -438,7 +458,7 @@ public abstract class DownloadService extends Service { ...@@ -438,7 +458,7 @@ public abstract class DownloadService extends Service {
DownloadManagerHelper downloadManagerHelper = downloadManagerListeners.get(clazz); DownloadManagerHelper downloadManagerHelper = downloadManagerListeners.get(clazz);
if (downloadManagerHelper == null) { if (downloadManagerHelper == null) {
DownloadManager downloadManager = getDownloadManager(); DownloadManager downloadManager = getDownloadManager();
downloadManager.startDownloads(); downloadManager.resumeDownloads();
downloadManagerHelper = downloadManagerHelper =
new DownloadManagerHelper( new DownloadManagerHelper(
getApplicationContext(), downloadManager, getScheduler(), clazz); getApplicationContext(), downloadManager, getScheduler(), clazz);
...@@ -477,11 +497,11 @@ public abstract class DownloadService extends Service { ...@@ -477,11 +497,11 @@ public abstract class DownloadService extends Service {
downloadManager.addDownload(downloadRequest, stopReason); downloadManager.addDownload(downloadRequest, stopReason);
} }
break; break;
case ACTION_START: case ACTION_RESUME:
downloadManager.startDownloads(); downloadManager.resumeDownloads();
break; break;
case ACTION_STOP: case ACTION_PAUSE:
downloadManager.stopDownloads(); downloadManager.pauseDownloads();
break; break;
case ACTION_SET_STOP_REASON: case ACTION_SET_STOP_REASON:
if (!intent.hasExtra(KEY_STOP_REASON)) { if (!intent.hasExtra(KEY_STOP_REASON)) {
......
...@@ -368,7 +368,7 @@ public class DownloadManagerTest { ...@@ -368,7 +368,7 @@ public class DownloadManagerTest {
runner2.postDownloadRequest().postRemoveRequest().getTask().assertRemoving(); runner2.postDownloadRequest().postRemoveRequest().getTask().assertRemoving();
runner2.postDownloadRequest(); runner2.postDownloadRequest();
runOnMainThread(() -> downloadManager.stopDownloads()); runOnMainThread(() -> downloadManager.pauseDownloads());
runner1.getTask().assertStopped(); runner1.getTask().assertStopped();
...@@ -386,7 +386,7 @@ public class DownloadManagerTest { ...@@ -386,7 +386,7 @@ public class DownloadManagerTest {
// New download requests can be added but they don't start. // New download requests can be added but they don't start.
runner3.postDownloadRequest().getDownloader(0).assertDoesNotStart(); runner3.postDownloadRequest().getDownloader(0).assertDoesNotStart();
runOnMainThread(() -> downloadManager.startDownloads()); runOnMainThread(() -> downloadManager.resumeDownloads());
runner2.getDownloader(2).assertStarted().unblock(); runner2.getDownloader(2).assertStarted().unblock();
runner3.getDownloader(0).assertStarted().unblock(); runner3.getDownloader(0).assertStarted().unblock();
...@@ -532,7 +532,7 @@ public class DownloadManagerTest { ...@@ -532,7 +532,7 @@ public class DownloadManagerTest {
maxActiveDownloadTasks, maxActiveDownloadTasks,
MIN_RETRY_COUNT, MIN_RETRY_COUNT,
new Requirements(0)); new Requirements(0));
downloadManager.startDownloads(); downloadManager.resumeDownloads();
downloadManagerListener = downloadManagerListener =
new TestDownloadManagerListener(downloadManager, dummyMainThread); new TestDownloadManagerListener(downloadManager, dummyMainThread);
}); });
......
...@@ -268,7 +268,7 @@ public class DownloadManagerDashTest { ...@@ -268,7 +268,7 @@ public class DownloadManagerDashTest {
downloadManagerListener = downloadManagerListener =
new TestDownloadManagerListener( new TestDownloadManagerListener(
downloadManager, dummyMainThread, /* timeout= */ 3000); downloadManager, dummyMainThread, /* timeout= */ 3000);
downloadManager.startDownloads(); downloadManager.resumeDownloads();
}); });
} }
......
...@@ -126,7 +126,7 @@ public class DownloadServiceDashTest { ...@@ -126,7 +126,7 @@ public class DownloadServiceDashTest {
new Requirements(0)); new Requirements(0));
downloadManagerListener = downloadManagerListener =
new TestDownloadManagerListener(dashDownloadManager, dummyMainThread); new TestDownloadManagerListener(dashDownloadManager, dummyMainThread);
dashDownloadManager.startDownloads(); dashDownloadManager.resumeDownloads();
dashDownloadService = dashDownloadService =
new DownloadService(DownloadService.FOREGROUND_NOTIFICATION_ID_NONE) { new DownloadService(DownloadService.FOREGROUND_NOTIFICATION_ID_NONE) {
......
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