Rename start/stopDownloads to resume/pauseDownloads

PiperOrigin-RevId: 244216620
This commit is contained in:
olly 2019-04-18 18:48:09 +01:00 committed by Oliver Woodman
parent 82af6899a0
commit 62964026b9
5 changed files with 105 additions and 85 deletions

View file

@ -55,8 +55,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
* Manages downloads.
*
* <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
* calling {@link #startDownloads()}.
* manager is used directly instead, downloads will be initially paused and so must be resumed by
* calling {@link #resumeDownloads()}.
*
* <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
@ -126,7 +126,7 @@ public final class DownloadManager {
// Messages posted to the background handler.
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_STOP_REASON = 3;
private static final int MSG_ADD_DOWNLOAD = 4;
@ -179,7 +179,7 @@ public final class DownloadManager {
// Mutable fields that are accessed on the internal thread.
@Requirements.RequirementFlags private int notMetRequirements;
private boolean downloadsStarted;
private boolean downloadsResumed;
private int simultaneousDownloads;
/**
@ -346,19 +346,19 @@ public final class DownloadManager {
return Collections.unmodifiableList(new ArrayList<>(downloads));
}
/** Starts all downloads except those that have a non-zero {@link Download#stopReason}. */
public void startDownloads() {
/** Resumes all downloads except those that have a non-zero {@link Download#stopReason}. */
public void resumeDownloads() {
pendingMessages++;
internalHandler
.obtainMessage(MSG_SET_DOWNLOADS_STARTED, /* downloadsStarted */ 1, /* unused */ 0)
.obtainMessage(MSG_SET_DOWNLOADS_RESUMED, /* downloadsResumed */ 1, /* unused */ 0)
.sendToTarget();
}
/** Stops all downloads. */
public void stopDownloads() {
/** Pauses all downloads. */
public void pauseDownloads() {
pendingMessages++;
internalHandler
.obtainMessage(MSG_SET_DOWNLOADS_STARTED, /* downloadsStarted */ 0, /* unused */ 0)
.obtainMessage(MSG_SET_DOWNLOADS_RESUMED, /* downloadsResumed */ 0, /* unused */ 0)
.sendToTarget();
}
@ -541,9 +541,9 @@ public final class DownloadManager {
int notMetRequirements = message.arg1;
initializeInternal(notMetRequirements);
break;
case MSG_SET_DOWNLOADS_STARTED:
boolean downloadsStarted = message.arg1 != 0;
setDownloadsStarted(downloadsStarted);
case MSG_SET_DOWNLOADS_RESUMED:
boolean downloadsResumed = message.arg1 != 0;
setDownloadsResumed(downloadsResumed);
break;
case MSG_SET_NOT_MET_REQUIREMENTS:
notMetRequirements = message.arg1;
@ -604,11 +604,11 @@ public final class DownloadManager {
}
}
private void setDownloadsStarted(boolean downloadsStarted) {
if (this.downloadsStarted == downloadsStarted) {
private void setDownloadsResumed(boolean downloadsResumed) {
if (this.downloadsResumed == downloadsResumed) {
return;
}
this.downloadsStarted = downloadsStarted;
this.downloadsResumed = downloadsResumed;
for (int i = 0; i < downloadInternals.size(); i++) {
downloadInternals.get(i).updateStopState();
}
@ -813,7 +813,7 @@ public final class DownloadManager {
}
private boolean canStartDownloads() {
return downloadsStarted && notMetRequirements == 0;
return downloadsResumed && notMetRequirements == 0;
}
/* package */ static Download mergeRequest(

View file

@ -66,24 +66,24 @@ public abstract class DownloadService extends Service {
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>
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul>
*/
public static final String ACTION_START =
"com.google.android.exoplayer.downloadService.action.START";
public static final String ACTION_RESUME =
"com.google.android.exoplayer.downloadService.action.RESUME";
/**
* Stops all downloads. Extras:
* Pauses all downloads. Extras:
*
* <ul>
* <li>{@link #KEY_FOREGROUND} - See {@link #KEY_FOREGROUND}.
* </ul>
*/
public static final String ACTION_STOP =
"com.google.android.exoplayer.downloadService.action.STOP";
public static final String ACTION_PAUSE =
"com.google.android.exoplayer.downloadService.action.PAUSE";
/**
* Sets the stop reason for one or all downloads. To clear the stop reason, pass {@link
@ -277,6 +277,32 @@ public abstract class DownloadService extends Service {
return getIntent(context, clazz, ACTION_REMOVE, foreground).putExtra(KEY_CONTENT_ID, id);
}
/**
* Builds an {@link Intent} for resuming all downloads.
*
* @param context A {@link Context}.
* @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.
* @return Created Intent.
*/
public static Intent buildResumeDownloadsIntent(
Context context, Class<? extends DownloadService> clazz, boolean foreground) {
return getIntent(context, clazz, ACTION_RESUME, foreground);
}
/**
* Builds an {@link Intent} to pause all downloads.
*
* @param context A {@link Context}.
* @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.
* @return Created Intent.
*/
public static Intent buildPauseDownloadsIntent(
Context context, Class<? extends DownloadService> clazz, boolean foreground) {
return getIntent(context, clazz, ACTION_PAUSE, foreground);
}
/**
* 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}.
@ -299,32 +325,6 @@ public abstract class DownloadService extends Service {
.putExtra(KEY_STOP_REASON, stopReason);
}
/**
* Builds an {@link Intent} for starting all downloads.
*
* @param context A {@link Context}.
* @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.
* @return Created Intent.
*/
public static Intent buildStartDownloadsIntent(
Context context, Class<? extends DownloadService> clazz, boolean foreground) {
return getIntent(context, clazz, ACTION_START, foreground);
}
/**
* Builds an {@link Intent} for stopping all downloads.
*
* @param context A {@link Context}.
* @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.
* @return Created Intent.
*/
public static Intent buildStopDownloadsIntent(
Context context, Class<? extends DownloadService> clazz, boolean foreground) {
return getIntent(context, clazz, ACTION_STOP, foreground);
}
/**
* Starts the service if not started already and adds a new download.
*
@ -342,6 +342,26 @@ public abstract class DownloadService extends Service {
startService(context, intent, foreground);
}
/**
* 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.
*
@ -356,6 +376,32 @@ public abstract class DownloadService extends Service {
startService(context, intent, foreground);
}
/**
* Starts the service if not started already and resumes all downloads.
*
* @param context A {@link Context}.
* @param clazz The concrete download service to be started.
* @param foreground Whether the service is started in the foreground.
*/
public static void sendResumeDownloads(
Context context, Class<? extends DownloadService> clazz, boolean foreground) {
Intent intent = buildResumeDownloadsIntent(context, clazz, foreground);
startService(context, intent, foreground);
}
/**
* Starts the service if not started already and pauses all downloads.
*
* @param context A {@link Context}.
* @param clazz The concrete download service to be started.
* @param foreground Whether the service is started in the foreground.
*/
public static void sendPauseDownloads(
Context context, Class<? extends DownloadService> clazz, boolean foreground) {
Intent intent = buildPauseDownloadsIntent(context, clazz, foreground);
startService(context, intent, foreground);
}
/**
* 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}.
@ -376,32 +422,6 @@ public abstract class DownloadService extends Service {
startService(context, intent, foreground);
}
/**
* Starts the service if not started already and starts all downloads.
*
* @param context A {@link Context}.
* @param clazz The concrete download service to be started.
* @param foreground Whether the service is started in the foreground.
*/
public static void sendStartDownloads(
Context context, Class<? extends DownloadService> clazz, boolean foreground) {
Intent intent = buildStartDownloadsIntent(context, clazz, foreground);
startService(context, intent, foreground);
}
/**
* Starts the service if not started already and stops all downloads.
*
* @param context A {@link Context}.
* @param clazz The concrete download service to be started.
* @param foreground Whether the service is started in the foreground.
*/
public static void sendStopDownloads(
Context context, Class<? extends DownloadService> clazz, boolean foreground) {
Intent intent = buildStopDownloadsIntent(context, clazz, foreground);
startService(context, intent, foreground);
}
/**
* Starts a download service to resume any ongoing downloads.
*
@ -438,7 +458,7 @@ public abstract class DownloadService extends Service {
DownloadManagerHelper downloadManagerHelper = downloadManagerListeners.get(clazz);
if (downloadManagerHelper == null) {
DownloadManager downloadManager = getDownloadManager();
downloadManager.startDownloads();
downloadManager.resumeDownloads();
downloadManagerHelper =
new DownloadManagerHelper(
getApplicationContext(), downloadManager, getScheduler(), clazz);
@ -477,11 +497,11 @@ public abstract class DownloadService extends Service {
downloadManager.addDownload(downloadRequest, stopReason);
}
break;
case ACTION_START:
downloadManager.startDownloads();
case ACTION_RESUME:
downloadManager.resumeDownloads();
break;
case ACTION_STOP:
downloadManager.stopDownloads();
case ACTION_PAUSE:
downloadManager.pauseDownloads();
break;
case ACTION_SET_STOP_REASON:
if (!intent.hasExtra(KEY_STOP_REASON)) {

View file

@ -368,7 +368,7 @@ public class DownloadManagerTest {
runner2.postDownloadRequest().postRemoveRequest().getTask().assertRemoving();
runner2.postDownloadRequest();
runOnMainThread(() -> downloadManager.stopDownloads());
runOnMainThread(() -> downloadManager.pauseDownloads());
runner1.getTask().assertStopped();
@ -386,7 +386,7 @@ public class DownloadManagerTest {
// New download requests can be added but they don't start.
runner3.postDownloadRequest().getDownloader(0).assertDoesNotStart();
runOnMainThread(() -> downloadManager.startDownloads());
runOnMainThread(() -> downloadManager.resumeDownloads());
runner2.getDownloader(2).assertStarted().unblock();
runner3.getDownloader(0).assertStarted().unblock();
@ -532,7 +532,7 @@ public class DownloadManagerTest {
maxActiveDownloadTasks,
MIN_RETRY_COUNT,
new Requirements(0));
downloadManager.startDownloads();
downloadManager.resumeDownloads();
downloadManagerListener =
new TestDownloadManagerListener(downloadManager, dummyMainThread);
});

View file

@ -267,7 +267,7 @@ public class DownloadManagerDashTest {
downloadManagerListener =
new TestDownloadManagerListener(
downloadManager, dummyMainThread, /* timeout= */ 3000);
downloadManager.startDownloads();
downloadManager.resumeDownloads();
});
}

View file

@ -125,7 +125,7 @@ public class DownloadServiceDashTest {
new Requirements(0));
downloadManagerListener =
new TestDownloadManagerListener(dashDownloadManager, dummyMainThread);
dashDownloadManager.startDownloads();
dashDownloadManager.resumeDownloads();
dashDownloadService =
new DownloadService(DownloadService.FOREGROUND_NOTIFICATION_ID_NONE) {